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