1 #include "ipf.h"
2
3 extern int nohdrfields;
4
parsefields(table,arg)5 wordtab_t *parsefields(table, arg)
6 wordtab_t *table;
7 char *arg;
8 {
9 wordtab_t *f, *fields;
10 char *s, *t;
11 int num;
12
13 fields = NULL;
14 num = 0;
15
16 for (s = strtok(arg, ","); s != NULL; s = strtok(NULL, ",")) {
17 t = strchr(s, '=');
18 if (t != NULL) {
19 *t++ = '\0';
20 if (*t == '\0')
21 nohdrfields = 1;
22 }
23
24 f = findword(table, s);
25 if (f == NULL) {
26 fprintf(stderr, "Unknown field '%s'\n", s);
27 exit(1);
28 }
29
30 num++;
31 if (fields == NULL) {
32 fields = malloc(2 * sizeof(*fields));
33 } else {
34 fields = realloc(fields, (num + 1) * sizeof(*fields));
35 }
36
37 if (t == NULL) {
38 fields[num - 1].w_word = f->w_word;
39 } else {
40 fields[num - 1].w_word = t;
41 }
42 fields[num - 1].w_value = f->w_value;
43 fields[num].w_word = NULL;
44 fields[num].w_value = 0;
45 }
46
47 return fields;
48 }
49