]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/ipfilter/lib/parsefields.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / contrib / ipfilter / lib / parsefields.c
1 #include "ipf.h"
2 #include <err.h>
3
4 extern int nohdrfields;
5
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 = realloc(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 }