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