xref: /netbsd/sbin/ifconfig/parse.c (revision 252d8de5)
1 /*	$NetBSD: parse.c,v 1.20 2020/06/07 06:02:58 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 David Young.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: parse.c,v 1.20 2020/06/07 06:02:58 thorpej Exp $");
31 #endif /* not lint */
32 
33 #include <err.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <netdb.h>
37 #include <stddef.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include <arpa/inet.h>
43 #include <sys/param.h>
44 #include <net/if.h>
45 #include <net/if_dl.h>
46 #include <netatalk/at.h>
47 
48 #include "env.h"
49 #include "parse.h"
50 #include "util.h"
51 
52 #ifdef DEBUG
53 #define dbg_warnx(__fmt, ...)	warnx(__fmt, __VA_ARGS__)
54 #else
55 #define dbg_warnx(__fmt, ...)	/* empty */
56 #endif
57 
58 static int parser_default_init(struct parser *);
59 static int pbranch_init(struct parser *);
60 static int pkw_init(struct parser *);
61 
62 static int pterm_match(const struct parser *, const struct match *,
63     struct match *, int, const char *);
64 
65 static int paddr_match(const struct parser *, const struct match *,
66     struct match *, int, const char *);
67 
68 static int pbranch_match(const struct parser *, const struct match *,
69     struct match *, int, const char *);
70 
71 static int piface_match(const struct parser *, const struct match *,
72     struct match *, int, const char *);
73 
74 static int pstr_match(const struct parser *, const struct match *,
75     struct match *, int, const char *);
76 
77 static int pinteger_match(const struct parser *, const struct match *,
78     struct match *, int, const char *);
79 
80 static int pkw_match(const struct parser *, const struct match *,
81     struct match *, int, const char *);
82 
83 const struct parser_methods pterm_methods = {
84 	  .pm_match = pterm_match
85 	, .pm_init = NULL
86 };
87 
88 const struct parser_methods pstr_methods = {
89 	  .pm_match = pstr_match
90 	, .pm_init = parser_default_init
91 };
92 
93 const struct parser_methods pinteger_methods = {
94 	  .pm_match = pinteger_match
95 	, .pm_init = parser_default_init
96 };
97 
98 const struct parser_methods paddr_methods = {
99 	  .pm_match = paddr_match
100 	, .pm_init = parser_default_init
101 };
102 
103 const struct parser_methods piface_methods = {
104 	  .pm_match = piface_match
105 	, .pm_init = parser_default_init
106 };
107 
108 const struct parser_methods pbranch_methods = {
109 	  .pm_match = pbranch_match
110 	, .pm_init = pbranch_init
111 };
112 
113 const struct parser_methods pkw_methods = {
114 	  .pm_match = pkw_match
115 	, .pm_init = pkw_init
116 };
117 
118 static int
match_setenv(const struct match * im,struct match * om,const char * key,prop_object_t o)119 match_setenv(const struct match *im, struct match *om, const char *key,
120     prop_object_t o)
121 {
122 	if (im == NULL)
123 		om->m_env = prop_dictionary_create();
124 	else
125 		om->m_env = prop_dictionary_copy(im->m_env);
126 
127 	if (om->m_env == NULL)
128 		goto delobj;
129 
130 	if (key != NULL && !prop_dictionary_set(om->m_env, key, o))
131 		goto deldict;
132 
133 	if (o != NULL)
134 		prop_object_release((prop_object_t)o);
135 
136 	return 0;
137 deldict:
138 	prop_object_release((prop_object_t)om->m_env);
139 	om->m_env = NULL;
140 delobj:
141 	prop_object_release((prop_object_t)o);
142 	errno = ENOMEM;
143 	return -1;
144 }
145 
146 int
pstr_match(const struct parser * p,const struct match * im,struct match * om,int argidx,const char * arg)147 pstr_match(const struct parser *p, const struct match *im, struct match *om,
148     int argidx, const char *arg)
149 {
150 	prop_object_t o;
151 	const struct pstr *ps = (const struct pstr *)p;
152 	uint8_t buf[128];
153 	int len;
154 
155 	if (arg == NULL) {
156 		errno = EINVAL;
157 		return -1;
158 	}
159 
160 	len = (int)sizeof(buf);
161 	if (get_string(arg, NULL, buf, &len, ps->ps_hexok) == NULL) {
162 		errno = EINVAL;
163 		return -1;
164 	}
165 
166 	o = (prop_object_t)prop_data_create_copy(buf, len);
167 
168 	if (o == NULL) {
169 		errno = ENOMEM;
170 		return -1;
171 	}
172 
173 	if (match_setenv(im, om, ps->ps_key, o) == -1)
174 		return -1;
175 
176 	om->m_argidx = argidx;
177 	om->m_parser = p;
178 	om->m_nextparser = p->p_nextparser;
179 
180 	return 0;
181 }
182 
183 int
pinteger_match(const struct parser * p,const struct match * im,struct match * om,int argidx,const char * arg)184 pinteger_match(const struct parser *p, const struct match *im, struct match *om,
185     int argidx, const char *arg)
186 {
187 	prop_object_t o;
188 	const struct pinteger *pi = (const struct pinteger *)p;
189 	char *end;
190 	int64_t val;
191 
192 	if (arg == NULL) {
193 		errno = EINVAL;
194 		return -1;
195 	}
196 
197 	val = strtoimax(arg, &end, pi->pi_base);
198 	if ((val == INTMAX_MIN || val == INTMAX_MAX) && errno == ERANGE)
199 		return -1;
200 
201 	if (*end != '\0') {
202 		errno = EINVAL;
203 		return -1;
204 	}
205 
206 	if (val < pi->pi_min || val > pi->pi_max) {
207 		errno = ERANGE;
208 		return -1;
209 	}
210 
211 	o = (prop_object_t)prop_number_create_signed(val);
212 
213 	if (o == NULL) {
214 		errno = ENOMEM;
215 		return -1;
216 	}
217 
218 	if (match_setenv(im, om, pi->pi_key, o) == -1)
219 		return -1;
220 
221 	om->m_argidx = argidx;
222 	om->m_parser = p;
223 	om->m_nextparser = p->p_nextparser;
224 
225 	return 0;
226 }
227 
228 static int
parse_linkaddr(const char * addr,struct sockaddr_storage * ss)229 parse_linkaddr(const char *addr, struct sockaddr_storage *ss)
230 {
231 	static const size_t maxlen =
232 	    sizeof(*ss) - offsetof(struct sockaddr_dl, sdl_data[0]);
233 	enum {
234 		LLADDR_S_INITIAL = 0,
235 		LLADDR_S_ONE_OCTET = 1,
236 		LLADDR_S_TWO_OCTETS = 2,
237 		LLADDR_S_COLON = 3
238 	} state = LLADDR_S_INITIAL;
239 	uint8_t octet = 0, val;
240 	struct sockaddr_dl *sdl;
241 	const char *p;
242 	size_t i;
243 
244 	memset(ss, 0, sizeof(*ss));
245 	ss->ss_family = AF_LINK;
246 	sdl = (struct sockaddr_dl *)ss;
247 
248 	for (i = 0, p = addr; i < maxlen; p++) {
249 		dbg_warnx("%s.%d: *p == %c, state %d", __func__, __LINE__, *p,
250 		    state);
251 		if (*p == '\0') {
252 			dbg_warnx("%s.%d", __func__, __LINE__);
253 			if (state != LLADDR_S_ONE_OCTET &&
254 			    state != LLADDR_S_TWO_OCTETS)
255 				return -1;
256 			dbg_warnx("%s.%d", __func__, __LINE__);
257 			sdl->sdl_data[i++] = octet;
258 			sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data)
259 			    + i * sizeof(sdl->sdl_data[0]);
260 			sdl->sdl_alen = i;
261 			return 0;
262 		}
263 		if (*p == ':') {
264 			dbg_warnx("%s.%d", __func__, __LINE__);
265 			if (state != LLADDR_S_ONE_OCTET &&
266 			    state != LLADDR_S_TWO_OCTETS)
267 				return -1;
268 			dbg_warnx("%s.%d", __func__, __LINE__);
269 			sdl->sdl_data[i++] = octet;
270 			state = LLADDR_S_COLON;
271 			continue;
272 		}
273 		if ('a' <= *p && *p <= 'f')
274 			val = 10 + *p - 'a';
275 		else if ('A' <= *p && *p <= 'F')
276 			val = 10 + *p - 'A';
277 		else if ('0' <= *p && *p <= '9')
278 			val = *p - '0';
279 		else
280 			return -1;
281 
282 		dbg_warnx("%s.%d", __func__, __LINE__);
283 		if (state == LLADDR_S_ONE_OCTET) {
284 			state = LLADDR_S_TWO_OCTETS;
285 			octet <<= 4;
286 			octet |= val;
287 		} else if (state != LLADDR_S_INITIAL && state != LLADDR_S_COLON)
288 			return -1;
289 		else {
290 			state = LLADDR_S_ONE_OCTET;
291 			octet = val;
292 		}
293 		dbg_warnx("%s.%d", __func__, __LINE__);
294 	}
295 	return -1;
296 }
297 
298 static int
paddr_match(const struct parser * p,const struct match * im,struct match * om,int argidx,const char * arg0)299 paddr_match(const struct parser *p, const struct match *im, struct match *om,
300     int argidx, const char *arg0)
301 {
302 	unsigned int net, node;
303 	int nread;
304 	union {
305 		struct sockaddr sa;
306 		struct sockaddr_at sat;
307 		struct sockaddr_in sin;
308 		struct sockaddr_storage ss;
309 	} u;
310 	const struct paddr *pa = (const struct paddr *)p;
311 	prop_data_t d;
312 	prop_object_t o;
313 	int64_t af0;
314 	int af, rc;
315 	struct paddr_prefix *pfx, *mask;
316 	const struct sockaddr *sa = NULL;
317 	struct addrinfo hints, *result = NULL;
318 	char *arg, *end, *plen = NULL, *servname0;
319 	const char *servname;
320 	long prefixlen = -1;
321 	size_t len;
322 
323 	if (arg0 == NULL) {
324 		errno = EINVAL;
325 		return -1;
326 	}
327 
328 	if (pa->pa_activator != NULL &&
329 	    prop_dictionary_get(im->m_env, pa->pa_activator) == NULL)
330 		return -1;
331 
332 	if (pa->pa_deactivator != NULL &&
333 	    prop_dictionary_get(im->m_env, pa->pa_deactivator) != NULL)
334 		return -1;
335 
336 	if (!prop_dictionary_get_int64(im->m_env, "af", &af0))
337 		af = AF_UNSPEC;
338 	else
339 		af = af0;
340 
341 	memset(&u, 0, sizeof(u));
342 
343 	switch (af) {
344 	case AF_UNSPEC:
345 	case AF_INET:
346 	case AF_INET6:
347 		if ((arg = strdup(arg0)) == NULL)
348 			return -1;
349 
350 		servname0 = arg;
351 		(void)strsep(&servname0, ",");
352 		servname = (servname0 == NULL) ? "0" : servname0;
353 
354 		if (pa->pa_maskkey == NULL)
355 			;
356 		else if ((plen = strrchr(arg, '/')) != NULL)
357 			*plen++ = '\0';
358 
359 		memset(&hints, 0, sizeof(hints));
360 
361 		hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE;
362 		hints.ai_family = af;
363 		hints.ai_socktype = SOCK_DGRAM;
364 
365 		for (;;) {
366 			rc = getaddrinfo(arg, servname, &hints, &result);
367 			if (rc == 0) {
368 				if (result->ai_next == NULL)
369 					sa = result->ai_addr;
370 				else
371 					errno = EMLINK;
372 				break;
373 			} else if ((hints.ai_flags & AI_NUMERICHOST) != 0 &&
374 			    (af == AF_INET || af == AF_UNSPEC) &&
375 			    inet_aton(arg, &u.sin.sin_addr) == 1) {
376 				u.sin.sin_family = AF_INET;
377 				u.sin.sin_len = sizeof(u.sin);
378 				sa = &u.sa;
379 				break;
380 			} else if ((hints.ai_flags & AI_NUMERICHOST) == 0 ||
381 				 rc != EAI_NONAME) {
382 				errno = ENOENT;
383 				break;
384 			}
385 			hints.ai_flags &= ~AI_NUMERICHOST;
386 		}
387 
388 
389 		if (plen == NULL)
390 			prefixlen = -1;
391 		else {
392 			prefixlen = strtol(plen, &end, 10);
393 			if (end != NULL && *end != '\0')
394 				sa = NULL;
395 			if (prefixlen < 0 || prefixlen >= UINT8_MAX) {
396 				errno = ERANGE;
397 				sa = NULL;
398 			}
399 		}
400 
401 		free(arg);
402 		if (sa != NULL || af != AF_UNSPEC)
403 			break;
404 		/*FALLTHROUGH*/
405 	case AF_APPLETALK:
406 		if (sscanf(arg0, "%u.%u%n", &net, &node, &nread) == 2 &&
407 		    net != 0 && net <= 0xffff && node != 0 && node <= 0xfe &&
408 		    arg0[nread] == '\0') {
409 			u.sat.sat_family = AF_APPLETALK;
410 			u.sat.sat_len = sizeof(u.sat);
411 			u.sat.sat_addr.s_net = htons(net);
412 			u.sat.sat_addr.s_node = node;
413 			sa = &u.sa;
414 		}
415 		break;
416 	case AF_LINK:
417 		if (parse_linkaddr(arg0, &u.ss) == -1)
418 			sa = NULL;
419 		else
420 			sa = &u.sa;
421 		break;
422 	}
423 
424 	if (sa == NULL)
425 		return -1;
426 
427 	len = offsetof(struct paddr_prefix, pfx_addr) + sa->sa_len;
428 
429 	if ((pfx = malloc(len)) == NULL)
430 		return -1;
431 
432 #if 0
433 	{
434 		int i;
435 
436 		for (i = 0; i < sa->sa_len; i++)
437 			printf(" %02x", ((const uint8_t *)sa)[i]);
438 		printf("\n");
439 	}
440 #endif
441 
442 	pfx->pfx_len = (int16_t)prefixlen;
443 	memcpy(&pfx->pfx_addr, sa, sa->sa_len);
444 	af = sa->sa_family;
445 
446 	if (result != NULL)
447 		freeaddrinfo(result);
448 
449 	o = (prop_object_t)prop_data_create_copy(pfx, len);
450 
451 	free(pfx);
452 
453 	if (o == NULL)
454 		return -1;
455 
456 	if (match_setenv(im, om, pa->pa_addrkey, o) == -1)
457 		return -1;
458 
459 	if (pa->pa_maskkey != NULL && plen != NULL) {
460 		size_t masklen;
461 
462 		if ((mask = prefixlen_to_mask(af, prefixlen)) == NULL) {
463 			err(EXIT_FAILURE, "%s: prefixlen_to_mask(%d, %ld)",
464 			    __func__, af, prefixlen);
465 			return -1;
466 		}
467 
468 		masklen = paddr_prefix_size(mask);
469 
470 		d = prop_data_create_copy(mask, masklen);
471 		free(mask);
472 
473 		if (d == NULL) {
474 			err(EXIT_FAILURE, "%s: prop_data_create_copy",
475 			    __func__);
476 			return -1;
477 		}
478 
479 		rc = prop_dictionary_set(om->m_env, pa->pa_maskkey,
480 		    (prop_object_t)d) ? 0 : -1;
481 
482 		prop_object_release((prop_object_t)d);
483 
484 		if (rc != 0) {
485 			err(EXIT_FAILURE, "%s: prop_dictionary_set", __func__);
486 			return rc;
487 		}
488 	}
489 
490 	om->m_argidx = argidx;
491 	om->m_parser = p;
492 	om->m_nextparser = p->p_nextparser;
493 	return 0;
494 }
495 
496 static int
pterm_match(const struct parser * p,const struct match * im,struct match * om,int argidx,const char * arg)497 pterm_match(const struct parser *p, const struct match *im,
498     struct match *om, int argidx, const char *arg)
499 {
500 	const struct pterm *pt = (const struct pterm *)p;
501 	prop_bool_t b;
502 
503 	if (arg != NULL) {
504 		errno = EINVAL;
505 		return -1;
506 	}
507 	b = prop_bool_create(true);
508 
509 	if (match_setenv(im, om, pt->pt_key, (prop_object_t)b) == -1)
510 		return -1;
511 
512 	om->m_argidx = argidx;
513 	om->m_parser = p;
514 	om->m_nextparser = NULL;
515 	return 0;
516 }
517 
518 static int
piface_match(const struct parser * p,const struct match * im,struct match * om,int argidx,const char * arg)519 piface_match(const struct parser *p, const struct match *im,
520     struct match *om, int argidx, const char *arg)
521 {
522 	const struct piface *pif = (const struct piface *)p;
523 	prop_object_t o;
524 
525 	if (arg == NULL || strlen(arg) > IFNAMSIZ) {
526 		errno = EINVAL;
527 		return -1;
528 	}
529 
530 	if ((o = (prop_object_t)prop_string_create_copy(arg)) == NULL) {
531 		errno = ENOMEM;
532 		return -1;
533 	}
534 
535 	if (match_setenv(im, om, pif->pif_key, o) == -1)
536 		return -1;
537 
538 	om->m_argidx = argidx;
539 	om->m_parser = p;
540 	om->m_nextparser = p->p_nextparser;
541 	return 0;
542 }
543 
544 static void
match_cleanup(struct match * dst)545 match_cleanup(struct match *dst)
546 {
547 	if (dst->m_env != NULL)
548 		prop_object_release((prop_object_t)dst->m_env);
549 	memset(dst, 0, sizeof(*dst));
550 }
551 
552 static void
match_copy(struct match * dst,const struct match * src)553 match_copy(struct match *dst, const struct match *src)
554 {
555 	match_cleanup(dst);
556 
557 	prop_object_retain((prop_object_t)src->m_env);
558 	*dst = *src;
559 }
560 
561 static int
pbranch_match(const struct parser * p,const struct match * im,struct match * om,int argidx,const char * arg)562 pbranch_match(const struct parser *p, const struct match *im,
563     struct match *om, int argidx, const char *arg)
564 {
565 	const struct parser *nextp;
566 	struct branch *b;
567 	const struct pbranch *pb = (const struct pbranch *)p;
568 	struct match tmpm;
569 	int nforbid = 0, nmatch = 0, rc;
570 	parser_match_t matchfunc;
571 
572 	memset(&tmpm, 0, sizeof(tmpm));
573 
574 	SIMPLEQ_FOREACH(b, &pb->pb_branches, b_next) {
575 		nextp = b->b_nextparser;
576 		dbg_warnx("%s: b->b_nextparser %p [%s]", __func__,
577 		    nextp, nextp ? nextp->p_name : "(null)");
578 		if (nextp == NULL) {
579 			if (arg == NULL) {
580 				nmatch++;
581 				match_setenv(im, om, NULL, NULL);
582 				om->m_nextparser = NULL;
583 				om->m_parser = p;
584 				om->m_argidx = argidx;
585 			}
586 			continue;
587 		}
588 		matchfunc = nextp->p_methods->pm_match;
589 		rc = (*matchfunc)(nextp, im, &tmpm, argidx, arg);
590 		if (rc == 0) {
591 			match_copy(om, &tmpm);
592 			match_cleanup(&tmpm);
593 			nmatch++;
594 			dbg_warnx("%s: branch %s ok", __func__, nextp->p_name);
595 			if (pb->pb_match_first)
596 				break;
597 		} else if (rc == 1) {
598 			nforbid++;
599 			if (pb->pb_match_first)
600 				break;
601 		} else {
602 			dbg_warnx("%s: fail branch %s", __func__,
603 			    nextp->p_name);
604 		}
605 	}
606 	switch (nmatch) {
607 	case 0:
608 		errno = ENOENT;
609 		return (nforbid == 0) ? -1 : 1;
610 	case 1:
611 		dbg_warnx("%s: branch ok", __func__);
612 		return 0;
613 	default:
614 		match_cleanup(om);
615 		errno = EMLINK;
616 		return -1;
617 	}
618 }
619 
620 static int
pkw_match(const struct parser * p,const struct match * im,struct match * om,int argidx,const char * arg)621 pkw_match(const struct parser *p, const struct match *im,
622     struct match *om, int argidx, const char *arg)
623 {
624 	prop_object_t o = NULL;
625 	struct kwinst *k;
626 	union kwval *u = NULL;
627 	const struct pkw *pk = (const struct pkw *)p;
628 
629 	if (arg == NULL) {
630 		errno = EINVAL;
631 		return -1;
632 	}
633 
634 	SIMPLEQ_FOREACH(k, &pk->pk_keywords, k_next) {
635 		if (k->k_act != NULL &&
636 		    prop_dictionary_get(im->m_env, k->k_act) == NULL)
637 			continue;
638 
639 		if (k->k_neg && arg[0] == '-' &&
640 		    strcmp(k->k_word, arg + 1) == 0)
641 			u = &k->k_negu;
642 		else if (strcmp(k->k_word, arg) == 0)
643 			u = &k->k_u;
644 		else
645 			continue;
646 
647 		if (k->k_altdeact != NULL &&
648 		    prop_dictionary_get(im->m_env, k->k_altdeact) != NULL)
649 			return 1;
650 
651 		if (k->k_deact != NULL &&
652 		    prop_dictionary_get(im->m_env, k->k_deact) != NULL)
653 			return 1;
654 		break;
655 	}
656 	if (k == NULL) {
657 		errno = ENOENT;
658 		return -1;
659 	}
660 	switch (k->k_type) {
661 	case KW_T_NONE:
662 		break;
663 	case KW_T_BOOL:
664 		o = (prop_object_t)prop_bool_create(u->u_bool);
665 		if (o == NULL)
666 			goto err;
667 		break;
668 	case KW_T_INT:
669 		o = (prop_object_t)prop_number_create_signed(u->u_sint);
670 		if (o == NULL)
671 			goto err;
672 		break;
673 	case KW_T_UINT:
674 		o = (prop_object_t)prop_number_create_unsigned(u->u_uint);
675 		if (o == NULL)
676 			goto err;
677 		break;
678 	case KW_T_OBJ:
679 		o = u->u_obj;
680 		break;
681 	case KW_T_STR:
682 		o = (prop_object_t)prop_string_create_nocopy(u->u_str);
683 		if (o == NULL)
684 			goto err;
685 		break;
686 	default:
687 		errx(EXIT_FAILURE, "unknown keyword type %d", k->k_type);
688 	}
689 
690 	if (match_setenv(im, om, (o == NULL) ? NULL : k->k_key, o) == -1)
691 		return -1;
692 
693 	om->m_argidx = argidx;
694 	om->m_parser = p;
695 	om->m_nextparser = k->k_nextparser;
696 	om->m_exec = k->k_exec;
697 	return 0;
698 err:
699 	errno = ENOMEM;
700 	return -1;
701 }
702 
703 struct paddr *
paddr_create(const char * name,parser_exec_t pexec,const char * addrkey,const char * maskkey,struct parser * next)704 paddr_create(const char *name, parser_exec_t pexec, const char *addrkey,
705     const char *maskkey, struct parser *next)
706 {
707 	struct paddr *pa;
708 
709 	if ((pa = calloc(sizeof(*pa), 1)) == NULL)
710 		return NULL;
711 
712 	pa->pa_parser.p_methods = &paddr_methods;
713 	pa->pa_parser.p_exec = pexec;
714 	pa->pa_parser.p_name = name;
715 	pa->pa_parser.p_nextparser = next;
716 
717 	pa->pa_addrkey = addrkey;
718 	pa->pa_maskkey = maskkey;
719 
720 	return pa;
721 }
722 
723 struct piface *
piface_create(const char * name,parser_exec_t pexec,const char * defkey,struct parser * defnext)724 piface_create(const char *name, parser_exec_t pexec, const char *defkey,
725     struct parser *defnext)
726 {
727 	struct piface *pif;
728 
729 	if ((pif = calloc(sizeof(*pif), 1)) == NULL)
730 		return NULL;
731 
732 	pif->pif_parser.p_methods = &piface_methods;
733 	pif->pif_parser.p_exec = pexec;
734 	pif->pif_parser.p_name = name;
735 	pif->pif_parser.p_nextparser = defnext;
736 
737 	pif->pif_key = defkey;
738 
739 	return pif;
740 }
741 
742 int
pbranch_addbranch(struct pbranch * pb,struct parser * p)743 pbranch_addbranch(struct pbranch *pb, struct parser *p)
744 {
745 	struct branch *b;
746 
747 	if ((b = malloc(sizeof(*b))) == NULL)
748 		return -1;
749 	b->b_nextparser = p;
750 	SIMPLEQ_INSERT_HEAD(&pb->pb_branches, b, b_next);
751 	pb->pb_parser.p_initialized = false;
752 	return parser_init(&pb->pb_parser);
753 }
754 
755 int
pbranch_setbranches(struct pbranch * pb,const struct branch * brs,size_t nbr)756 pbranch_setbranches(struct pbranch *pb, const struct branch *brs, size_t nbr)
757 {
758 	struct branch *b;
759 	size_t i;
760 
761 	dbg_warnx("%s: nbr %zu", __func__, nbr);
762 
763 	while ((b = SIMPLEQ_FIRST(&pb->pb_branches)) != NULL) {
764 		SIMPLEQ_REMOVE_HEAD(&pb->pb_branches, b_next);
765 		free(b);
766 	}
767 
768 	for (i = 0; i < nbr; i++) {
769 		if ((b = malloc(sizeof(*b))) == NULL)
770 			goto err;
771 		*b = brs[i];
772 		dbg_warnx("%s: b->b_nextparser %p [%s]", __func__,
773 		    b->b_nextparser, b->b_nextparser ? b->b_nextparser->p_name
774 		    : "(null)");
775 		SIMPLEQ_INSERT_TAIL(&pb->pb_branches, b, b_next);
776 	}
777 
778 	return 0;
779 err:
780 	while ((b = SIMPLEQ_FIRST(&pb->pb_branches)) != NULL) {
781 		SIMPLEQ_REMOVE_HEAD(&pb->pb_branches, b_next);
782 		free(b);
783 	}
784 	return -1;
785 }
786 
787 static int
pbranch_init(struct parser * p)788 pbranch_init(struct parser *p)
789 {
790 	struct branch *b;
791 	struct pbranch *pb = (struct pbranch *)p;
792 	struct parser *np;
793 
794 	if (pb->pb_nbrinit == 0)
795 		;
796 	else if (pbranch_setbranches(pb, pb->pb_brinit, pb->pb_nbrinit) == -1)
797 		return -1;
798 
799 	pb->pb_nbrinit = 0;
800 
801 	SIMPLEQ_FOREACH(b, &pb->pb_branches, b_next) {
802 		np = b->b_nextparser;
803 		if (np != NULL && parser_init(np) == -1)
804 			return -1;
805 	}
806 	return 0;
807 }
808 
809 struct pbranch *
pbranch_create(const char * name,const struct branch * brs,size_t nbr,bool match_first)810 pbranch_create(const char *name, const struct branch *brs, size_t nbr,
811     bool match_first)
812 {
813 	struct pbranch *pb;
814 
815 	dbg_warnx("%s: nbr %zu", __func__, nbr);
816 
817 	if ((pb = calloc(1, sizeof(*pb))) == NULL)
818 		return NULL;
819 
820 	pb->pb_parser.p_methods = &pbranch_methods;
821 	pb->pb_parser.p_name = name;
822 
823 	SIMPLEQ_INIT(&pb->pb_branches);
824 
825 	if (pbranch_setbranches(pb, brs, nbr) == -1)
826 		goto post_pb_err;
827 
828 	pb->pb_match_first = match_first;
829 	return pb;
830 post_pb_err:
831 	free(pb);
832 	return NULL;
833 }
834 
835 static int
parser_default_init(struct parser * p)836 parser_default_init(struct parser *p)
837 {
838 	struct parser *np;
839 
840 	np = p->p_nextparser;
841 	if (np != NULL && parser_init(np) == -1)
842 		return -1;
843 
844 	return 0;
845 }
846 
847 static int
pkw_setwords(struct pkw * pk,parser_exec_t defexec,const char * defkey,const struct kwinst * kws,size_t nkw,struct parser * defnext)848 pkw_setwords(struct pkw *pk, parser_exec_t defexec, const char *defkey,
849     const struct kwinst *kws, size_t nkw, struct parser *defnext)
850 {
851 	struct kwinst *k;
852 	size_t i;
853 
854 	for (i = 0; i < nkw; i++) {
855 		if (kws[i].k_word == NULL)
856 			continue;
857 		if ((k = malloc(sizeof(*k))) == NULL)
858 			goto post_pk_err;
859 		*k = kws[i];
860 		if (k->k_nextparser == NULL)
861 			k->k_nextparser = defnext;
862 		if (k->k_key == NULL)
863 			k->k_key = defkey;
864 		if (k->k_exec == NULL)
865 			k->k_exec = defexec;
866 		SIMPLEQ_INSERT_TAIL(&pk->pk_keywords, k, k_next);
867 	}
868 	return 0;
869 
870 post_pk_err:
871 	while ((k = SIMPLEQ_FIRST(&pk->pk_keywords)) != NULL) {
872 		SIMPLEQ_REMOVE_HEAD(&pk->pk_keywords, k_next);
873 		free(k);
874 	}
875 	return -1;
876 }
877 
878 static int
pkw_init(struct parser * p)879 pkw_init(struct parser *p)
880 {
881 	struct kwinst *k;
882 	struct pkw *pk = (struct pkw *)p;
883 	struct parser *np;
884 
885 	if (pk->pk_nkwinit == 0)
886 		;
887 	else if (pkw_setwords(pk, pk->pk_execinit, pk->pk_keyinit,
888 	    pk->pk_kwinit, pk->pk_nkwinit, pk->pk_nextinit) == -1)
889 		return -1;
890 
891 	pk->pk_nkwinit = 0;
892 
893 	SIMPLEQ_FOREACH(k, &pk->pk_keywords, k_next) {
894 		np = k->k_nextparser;
895 		if (np != NULL && parser_init(np) == -1)
896 			return -1;
897 	}
898 	return 0;
899 }
900 
901 struct pkw *
pkw_create(const char * name,parser_exec_t defexec,const char * defkey,const struct kwinst * kws,size_t nkw,struct parser * defnext)902 pkw_create(const char *name, parser_exec_t defexec, const char *defkey,
903     const struct kwinst *kws, size_t nkw, struct parser *defnext)
904 {
905 	struct pkw *pk;
906 
907 	if ((pk = calloc(1, sizeof(*pk))) == NULL)
908 		return NULL;
909 
910 	pk->pk_parser.p_methods = &pkw_methods;
911 	pk->pk_parser.p_exec = defexec;
912 	pk->pk_parser.p_name = name;
913 
914 	SIMPLEQ_INIT(&pk->pk_keywords);
915 
916 	if (pkw_setwords(pk, defexec, defkey, kws, nkw, defnext) == -1)
917 		goto err;
918 
919 	return pk;
920 err:
921 	free(pk);
922 	return NULL;
923 }
924 
925 int
parse(int argc,char ** argv,const struct parser * p0,struct match * matches,size_t * nmatch,int * narg)926 parse(int argc, char **argv, const struct parser *p0, struct match *matches,
927     size_t *nmatch, int *narg)
928 {
929 	int i, rc = 0;
930 	struct match *lastm = NULL, *m = matches;
931 	const struct parser *p = p0;
932 
933 	for (i = 0; i < argc && p != NULL; i++) {
934 		if ((size_t)(m - matches) >= *nmatch) {
935 			errno = EFBIG;
936 			rc = -1;
937 			break;
938 		}
939 		rc = (*p->p_methods->pm_match)(p, lastm, m, i, argv[i]);
940 		if (rc != 0)
941 			goto out;
942 		p = m->m_nextparser;
943 		lastm = m++;
944 	}
945 	for (; (size_t)(m - matches) < *nmatch && p != NULL; ) {
946 		rc = (*p->p_methods->pm_match)(p, lastm, m, i, NULL);
947 		if (rc != 0)
948 			break;
949 		p = m->m_nextparser;
950 		lastm = m++;
951 	}
952 out:
953 	*nmatch = m - matches;
954 	*narg = i;
955 	return rc;
956 }
957 
958 int
matches_exec(const struct match * matches,prop_dictionary_t oenv,size_t nmatch)959 matches_exec(const struct match *matches, prop_dictionary_t oenv, size_t nmatch)
960 {
961 	size_t i;
962 	int rc = 0;
963 	const struct match *m;
964 	parser_exec_t pexec;
965 	prop_dictionary_t d;
966 
967 	for (i = 0; i < nmatch; i++) {
968 		m = &matches[i];
969 		dbg_warnx("%s.%d: i %zu", __func__, __LINE__, i);
970 		pexec = (m->m_parser->p_exec != NULL)
971 		    ? m->m_parser->p_exec : m->m_exec;
972 		if (pexec == NULL)
973 			continue;
974 		dbg_warnx("%s.%d: m->m_parser->p_name %s", __func__, __LINE__,
975 		    m->m_parser->p_name);
976 		d = prop_dictionary_augment(m->m_env, oenv);
977 		rc = (*pexec)(d, oenv);
978 		prop_object_release((prop_object_t)d);
979 		if (rc == -1)
980 			break;
981 	}
982 	return rc;
983 }
984 
985 int
parser_init(struct parser * p)986 parser_init(struct parser *p)
987 {
988 	if (p->p_initialized)
989 		return 0;
990 	p->p_initialized = true;
991 	if (p->p_methods->pm_init == NULL)
992 		return 0;
993 	return (*p->p_methods->pm_init)(p);
994 }
995