xref: /freebsd/sbin/ipf/libipf/parsefields.c (revision 44bc30192139b0b3c95510ab3b35802bcc6d63e4)
141edb306SCy Schubert #include "ipf.h"
241edb306SCy Schubert #include <err.h>
341edb306SCy Schubert 
441edb306SCy Schubert extern int nohdrfields;
541edb306SCy Schubert 
parsefields(wordtab_t * table,char * arg)6efeb8bffSCy Schubert wordtab_t *parsefields(wordtab_t *table, char *arg)
741edb306SCy Schubert {
841edb306SCy Schubert 	wordtab_t *f, *fields;
941edb306SCy Schubert 	char *s, *t;
1041edb306SCy Schubert 	int num;
1141edb306SCy Schubert 
1241edb306SCy Schubert 	fields = NULL;
1341edb306SCy Schubert 	num = 0;
1441edb306SCy Schubert 
1541edb306SCy Schubert 	for (s = strtok(arg, ","); s != NULL; s = strtok(NULL, ",")) {
1641edb306SCy Schubert 		t = strchr(s, '=');
1741edb306SCy Schubert 		if (t != NULL) {
1841edb306SCy Schubert 			*t++ = '\0';
1941edb306SCy Schubert 			if (*t == '\0')
2041edb306SCy Schubert 				nohdrfields = 1;
2141edb306SCy Schubert 		}
2241edb306SCy Schubert 
2341edb306SCy Schubert 		f = findword(table, s);
2441edb306SCy Schubert 		if (f == NULL) {
2541edb306SCy Schubert 			fprintf(stderr, "Unknown field '%s'\n", s);
2641edb306SCy Schubert 			exit(1);
2741edb306SCy Schubert 		}
2841edb306SCy Schubert 
2941edb306SCy Schubert 		num++;
3041edb306SCy Schubert 		if (fields == NULL) {
3141edb306SCy Schubert 			fields = malloc(2 * sizeof(*fields));
3241edb306SCy Schubert 		} else {
3341edb306SCy Schubert 			fields = reallocarray(fields, num + 1, sizeof(*fields));
3441edb306SCy Schubert 			if (fields == NULL) {
3541edb306SCy Schubert 				warnx("memory allocation error at %d in %s in %s", __LINE__, __FUNCTION__, __FILE__);
3641edb306SCy Schubert 				abort();
3741edb306SCy Schubert 			}
3841edb306SCy Schubert 		}
3941edb306SCy Schubert 
4041edb306SCy Schubert 		if (t == NULL) {
4141edb306SCy Schubert 			fields[num - 1].w_word = f->w_word;
4241edb306SCy Schubert 		} else {
4341edb306SCy Schubert 			fields[num - 1].w_word = t;
4441edb306SCy Schubert 		}
4541edb306SCy Schubert 		fields[num - 1].w_value = f->w_value;
4641edb306SCy Schubert 		fields[num].w_word = NULL;
4741edb306SCy Schubert 		fields[num].w_value = 0;
4841edb306SCy Schubert 	}
4941edb306SCy Schubert 
50*2582ae57SCy Schubert 	return (fields);
5141edb306SCy Schubert }
52