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