Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / sbin / ifconfig / parse.c
blobdff4a224aa3edc3aeeeac0c43dbf1987725f8938
1 /* $NetBSD: parse.c,v 1.13 2009/01/18 00:24:29 lukem Exp $ */
3 /*-
4 * Copyright (c) 2008 David Young. All rights reserved.
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.
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.
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: parse.c,v 1.13 2009/01/18 00:24:29 lukem Exp $");
31 #endif /* not lint */
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>
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 #include <netiso/iso.h>
49 #include "env.h"
50 #include "parse.h"
51 #include "util.h"
53 #define dbg_warnx(__fmt, ...) /* empty */
55 static int parser_default_init(struct parser *);
56 static int pbranch_init(struct parser *);
57 static int pkw_init(struct parser *);
59 static int pterm_match(const struct parser *, const struct match *,
60 struct match *, int, const char *);
62 static int paddr_match(const struct parser *, const struct match *,
63 struct match *, int, const char *);
65 static int pbranch_match(const struct parser *, const struct match *,
66 struct match *, int, const char *);
68 static int piface_match(const struct parser *, const struct match *,
69 struct match *, int, const char *);
71 static int pstr_match(const struct parser *, const struct match *,
72 struct match *, int, const char *);
74 static int pinteger_match(const struct parser *, const struct match *,
75 struct match *, int, const char *);
77 static int pkw_match(const struct parser *, const struct match *,
78 struct match *, int, const char *);
80 const struct parser_methods pterm_methods = {
81 .pm_match = pterm_match
82 , .pm_init = NULL
85 const struct parser_methods pstr_methods = {
86 .pm_match = pstr_match
87 , .pm_init = parser_default_init
90 const struct parser_methods pinteger_methods = {
91 .pm_match = pinteger_match
92 , .pm_init = parser_default_init
95 const struct parser_methods paddr_methods = {
96 .pm_match = paddr_match
97 , .pm_init = parser_default_init
100 const struct parser_methods piface_methods = {
101 .pm_match = piface_match
102 , .pm_init = parser_default_init
105 const struct parser_methods pbranch_methods = {
106 .pm_match = pbranch_match
107 , .pm_init = pbranch_init
110 const struct parser_methods pkw_methods = {
111 .pm_match = pkw_match
112 , .pm_init = pkw_init
115 static int
116 match_setenv(const struct match *im, struct match *om, const char *key,
117 prop_object_t o)
119 if (im == NULL)
120 om->m_env = prop_dictionary_create();
121 else
122 om->m_env = prop_dictionary_copy(im->m_env);
124 if (om->m_env == NULL)
125 goto delobj;
127 if (key != NULL && !prop_dictionary_set(om->m_env, key, o))
128 goto deldict;
130 if (o != NULL)
131 prop_object_release((prop_object_t)o);
133 return 0;
134 deldict:
135 prop_object_release((prop_object_t)om->m_env);
136 om->m_env = NULL;
137 delobj:
138 prop_object_release((prop_object_t)o);
139 errno = ENOMEM;
140 return -1;
144 pstr_match(const struct parser *p, const struct match *im, struct match *om,
145 int argidx, const char *arg)
147 prop_object_t o;
148 const struct pstr *ps = (const struct pstr *)p;
149 uint8_t buf[128];
150 int len;
152 if (arg == NULL) {
153 errno = EINVAL;
154 return -1;
157 len = (int)sizeof(buf);
158 if (get_string(arg, NULL, buf, &len) == NULL) {
159 errno = EINVAL;
160 return -1;
163 o = (prop_object_t)prop_data_create_data(buf, len);
165 if (o == NULL) {
166 errno = ENOMEM;
167 return -1;
170 if (match_setenv(im, om, ps->ps_key, o) == -1)
171 return -1;
173 om->m_argidx = argidx;
174 om->m_parser = p;
175 om->m_nextparser = p->p_nextparser;
177 return 0;
181 pinteger_match(const struct parser *p, const struct match *im, struct match *om,
182 int argidx, const char *arg)
184 prop_object_t o;
185 const struct pinteger *pi = (const struct pinteger *)p;
186 char *end;
187 int64_t val;
189 if (arg == NULL) {
190 errno = EINVAL;
191 return -1;
194 val = strtoimax(arg, &end, pi->pi_base);
195 if ((val == INTMAX_MIN || val == INTMAX_MAX) && errno == ERANGE)
196 return -1;
198 if (*end != '\0') {
199 errno = EINVAL;
200 return -1;
203 if (val < pi->pi_min || val > pi->pi_max) {
204 errno = ERANGE;
205 return -1;
208 o = (prop_object_t)prop_number_create_integer(val);
210 if (o == NULL) {
211 errno = ENOMEM;
212 return -1;
215 if (match_setenv(im, om, pi->pi_key, o) == -1)
216 return -1;
218 om->m_argidx = argidx;
219 om->m_parser = p;
220 om->m_nextparser = p->p_nextparser;
222 return 0;
225 static int
226 parse_linkaddr(const char *addr, struct sockaddr_storage *ss)
228 static const size_t maxlen =
229 sizeof(*ss) - offsetof(struct sockaddr_dl, sdl_data[0]);
230 enum {
231 LLADDR_S_INITIAL = 0,
232 LLADDR_S_ONE_OCTET = 1,
233 LLADDR_S_TWO_OCTETS = 2,
234 LLADDR_S_COLON = 3
235 } state = LLADDR_S_INITIAL;
236 uint8_t octet = 0, val;
237 struct sockaddr_dl *sdl;
238 const char *p;
239 size_t i;
241 memset(ss, 0, sizeof(*ss));
242 ss->ss_family = AF_LINK;
243 sdl = (struct sockaddr_dl *)ss;
245 for (i = 0, p = addr; i < maxlen; p++) {
246 dbg_warnx("%s.%d: *p == %c, state %d", __func__, __LINE__, *p,
247 state);
248 if (*p == '\0') {
249 dbg_warnx("%s.%d", __func__, __LINE__);
250 if (state != LLADDR_S_ONE_OCTET &&
251 state != LLADDR_S_TWO_OCTETS)
252 return -1;
253 dbg_warnx("%s.%d", __func__, __LINE__);
254 sdl->sdl_data[i++] = octet;
255 sdl->sdl_len =
256 offsetof(struct sockaddr_dl, sdl_data[i]);
257 sdl->sdl_alen = i;
258 return 0;
260 if (*p == ':') {
261 dbg_warnx("%s.%d", __func__, __LINE__);
262 if (state != LLADDR_S_ONE_OCTET &&
263 state != LLADDR_S_TWO_OCTETS)
264 return -1;
265 dbg_warnx("%s.%d", __func__, __LINE__);
266 sdl->sdl_data[i++] = octet;
267 state = LLADDR_S_COLON;
268 continue;
270 if ('a' <= *p && *p <= 'f')
271 val = 10 + *p - 'a';
272 else if ('A' <= *p && *p <= 'F')
273 val = 10 + *p - 'A';
274 else if ('0' <= *p && *p <= '9')
275 val = *p - '0';
276 else
277 return -1;
279 dbg_warnx("%s.%d", __func__, __LINE__);
280 if (state == LLADDR_S_ONE_OCTET) {
281 state = LLADDR_S_TWO_OCTETS;
282 octet <<= 4;
283 octet |= val;
284 } else if (state != LLADDR_S_INITIAL && state != LLADDR_S_COLON)
285 return -1;
286 else {
287 state = LLADDR_S_ONE_OCTET;
288 octet = val;
290 dbg_warnx("%s.%d", __func__, __LINE__);
292 return -1;
295 static int
296 paddr_match(const struct parser *p, const struct match *im, struct match *om,
297 int argidx, const char *arg0)
299 unsigned int net, node;
300 int nread;
301 union {
302 struct sockaddr sa;
303 struct sockaddr_at sat;
304 struct sockaddr_iso siso;
305 struct sockaddr_in sin;
306 struct sockaddr_storage ss;
307 } u;
308 const struct paddr *pa = (const struct paddr *)p;
309 prop_data_t d;
310 prop_object_t o;
311 int64_t af0;
312 int af, rc;
313 struct paddr_prefix *pfx, *mask;
314 const struct sockaddr *sa = NULL;
315 struct addrinfo hints, *result = NULL;
316 char *arg, *end, *plen = NULL, *servname0;
317 const char *servname;
318 long prefixlen = -1;
319 size_t len;
321 if (arg0 == NULL) {
322 errno = EINVAL;
323 return -1;
326 if (pa->pa_activator != NULL &&
327 prop_dictionary_get(im->m_env, pa->pa_activator) == NULL)
328 return -1;
330 if (pa->pa_deactivator != NULL &&
331 prop_dictionary_get(im->m_env, pa->pa_deactivator) != NULL)
332 return -1;
334 if (!prop_dictionary_get_int64(im->m_env, "af", &af0))
335 af = AF_UNSPEC;
336 else
337 af = af0;
339 memset(&u, 0, sizeof(u));
341 switch (af) {
342 case AF_UNSPEC:
343 case AF_INET:
344 case AF_INET6:
345 if ((arg = strdup(arg0)) == NULL)
346 return -1;
348 servname0 = arg;
349 (void)strsep(&servname0, ",");
350 servname = (servname0 == NULL) ? "0" : servname0;
352 if (pa->pa_maskkey == NULL)
354 else if ((plen = strrchr(arg, '/')) != NULL)
355 *plen++ = '\0';
357 memset(&hints, 0, sizeof(hints));
359 hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE;
360 hints.ai_family = af;
361 hints.ai_socktype = SOCK_DGRAM;
363 for (;;) {
364 rc = getaddrinfo(arg, servname, &hints, &result);
365 if (rc == 0) {
366 if (result->ai_next == NULL)
367 sa = result->ai_addr;
368 else
369 errno = EMLINK;
370 break;
371 } else if ((hints.ai_flags & AI_NUMERICHOST) != 0 &&
372 (af == AF_INET || af == AF_UNSPEC) &&
373 inet_aton(arg, &u.sin.sin_addr) == 1) {
374 u.sin.sin_family = AF_INET;
375 u.sin.sin_len = sizeof(u.sin);
376 sa = &u.sa;
377 break;
378 } else if ((hints.ai_flags & AI_NUMERICHOST) == 0 ||
379 rc != EAI_NONAME) {
380 errno = ENOENT;
381 break;
383 hints.ai_flags &= ~AI_NUMERICHOST;
387 if (plen == NULL)
388 prefixlen = -1;
389 else {
390 prefixlen = strtol(plen, &end, 10);
391 if (end != NULL && *end != '\0')
392 sa = NULL;
393 if (prefixlen < 0 || prefixlen >= UINT8_MAX) {
394 errno = ERANGE;
395 sa = NULL;
399 free(arg);
400 if (sa != NULL || af != AF_UNSPEC)
401 break;
402 /*FALLTHROUGH*/
403 case AF_APPLETALK:
404 if (sscanf(arg0, "%u.%u%n", &net, &node, &nread) == 2 &&
405 net != 0 && net <= 0xffff && node != 0 && node <= 0xfe &&
406 arg0[nread] == '\0') {
407 u.sat.sat_family = AF_APPLETALK;
408 u.sat.sat_len = sizeof(u.sat);
409 u.sat.sat_addr.s_net = htons(net);
410 u.sat.sat_addr.s_node = node;
411 sa = &u.sa;
413 break;
414 case AF_ISO:
415 u.siso.siso_len = sizeof(u.siso);
416 u.siso.siso_family = AF_ISO;
417 /* XXX iso_addr(3) matches ANYTHING! */
418 u.siso.siso_addr = *iso_addr(arg0);
419 sa = &u.sa;
420 break;
421 case AF_LINK:
422 if (parse_linkaddr(arg0, &u.ss) == -1)
423 sa = NULL;
424 else
425 sa = &u.sa;
426 break;
429 if (sa == NULL)
430 return -1;
432 len = offsetof(struct paddr_prefix, pfx_addr) + sa->sa_len;
434 if ((pfx = malloc(len)) == NULL)
435 return -1;
437 #if 0
439 int i;
441 for (i = 0; i < sa->sa_len; i++)
442 printf(" %02x", ((const uint8_t *)sa)[i]);
443 printf("\n");
445 #endif
447 pfx->pfx_len = (int16_t)prefixlen;
448 memcpy(&pfx->pfx_addr, sa, sa->sa_len);
449 af = sa->sa_family;
451 if (result != NULL)
452 freeaddrinfo(result);
454 o = (prop_object_t)prop_data_create_data(pfx, len);
456 free(pfx);
458 if (o == NULL)
459 return -1;
461 if (match_setenv(im, om, pa->pa_addrkey, o) == -1)
462 return -1;
464 if (pa->pa_maskkey != NULL && plen != NULL) {
465 size_t masklen;
467 if ((mask = prefixlen_to_mask(af, prefixlen)) == NULL) {
468 err(EXIT_FAILURE, "%s: prefixlen_to_mask(%d, %ld)",
469 __func__, af, prefixlen);
470 return -1;
473 masklen = paddr_prefix_size(mask);
475 d = prop_data_create_data(mask, masklen);
476 free(mask);
478 if (d == NULL) {
479 err(EXIT_FAILURE, "%s: prop_data_create_data",
480 __func__);
481 return -1;
484 rc = prop_dictionary_set(om->m_env, pa->pa_maskkey,
485 (prop_object_t)d) ? 0 : -1;
487 prop_object_release((prop_object_t)d);
489 if (rc != 0) {
490 err(EXIT_FAILURE, "%s: prop_dictionary_set", __func__);
491 return rc;
495 om->m_argidx = argidx;
496 om->m_parser = p;
497 om->m_nextparser = p->p_nextparser;
498 return 0;
501 static int
502 pterm_match(const struct parser *p, const struct match *im,
503 struct match *om, int argidx, const char *arg)
505 const struct pterm *pt = (const struct pterm *)p;
506 prop_bool_t b;
508 if (arg != NULL) {
509 errno = EINVAL;
510 return -1;
512 b = prop_bool_create(true);
514 if (match_setenv(im, om, pt->pt_key, (prop_object_t)b) == -1)
515 return -1;
517 om->m_argidx = argidx;
518 om->m_parser = p;
519 om->m_nextparser = NULL;
520 return 0;
523 static int
524 piface_match(const struct parser *p, const struct match *im,
525 struct match *om, int argidx, const char *arg)
527 const struct piface *pif = (const struct piface *)p;
528 prop_object_t o;
530 if (arg == NULL || strlen(arg) > IFNAMSIZ) {
531 errno = EINVAL;
532 return -1;
535 if ((o = (prop_object_t)prop_string_create_cstring(arg)) == NULL) {
536 errno = ENOMEM;
537 return -1;
540 if (match_setenv(im, om, pif->pif_key, o) == -1)
541 return -1;
543 om->m_argidx = argidx;
544 om->m_parser = p;
545 om->m_nextparser = p->p_nextparser;
546 return 0;
549 static void
550 match_cleanup(struct match *dst)
552 if (dst->m_env != NULL)
553 prop_object_release((prop_object_t)dst->m_env);
554 memset(dst, 0, sizeof(*dst));
557 static void
558 match_copy(struct match *dst, const struct match *src)
560 match_cleanup(dst);
562 prop_object_retain((prop_object_t)src->m_env);
563 *dst = *src;
566 static int
567 pbranch_match(const struct parser *p, const struct match *im,
568 struct match *om, int argidx, const char *arg)
570 const struct parser *nextp;
571 struct branch *b;
572 const struct pbranch *pb = (const struct pbranch *)p;
573 struct match tmpm;
574 int nforbid = 0, nmatch = 0, rc;
575 parser_match_t matchfunc;
577 memset(&tmpm, 0, sizeof(tmpm));
579 SIMPLEQ_FOREACH(b, &pb->pb_branches, b_next) {
580 dbg_warnx("%s: b->b_nextparser %p", __func__,
581 (const void *)b->b_nextparser);
582 nextp = b->b_nextparser;
583 if (nextp == NULL) {
584 if (arg == NULL) {
585 nmatch++;
586 match_setenv(im, om, NULL, NULL);
587 om->m_nextparser = NULL;
588 om->m_parser = p;
589 om->m_argidx = argidx;
591 continue;
593 matchfunc = nextp->p_methods->pm_match;
594 rc = (*matchfunc)(nextp, im, &tmpm, argidx, arg);
595 if (rc == 0) {
596 match_copy(om, &tmpm);
597 match_cleanup(&tmpm);
598 nmatch++;
599 dbg_warnx("%s: branch %s ok", __func__, nextp->p_name);
600 if (pb->pb_match_first)
601 break;
602 } else if (rc == 1) {
603 nforbid++;
604 if (pb->pb_match_first)
605 break;
606 } else {
607 dbg_warnx("%s: fail branch %s", __func__,
608 nextp->p_name);
611 switch (nmatch) {
612 case 0:
613 errno = ENOENT;
614 return (nforbid == 0) ? -1 : 1;
615 case 1:
616 dbg_warnx("%s: branch ok", __func__);
617 return 0;
618 default:
619 match_cleanup(om);
620 errno = EMLINK;
621 return -1;
625 static int
626 pkw_match(const struct parser *p, const struct match *im,
627 struct match *om, int argidx, const char *arg)
629 prop_object_t o = NULL;
630 struct kwinst *k;
631 union kwval *u = NULL;
632 const struct pkw *pk = (const struct pkw *)p;
634 if (arg == NULL) {
635 errno = EINVAL;
636 return -1;
639 SIMPLEQ_FOREACH(k, &pk->pk_keywords, k_next) {
640 if (k->k_act != NULL &&
641 prop_dictionary_get(im->m_env, k->k_act) == NULL)
642 continue;
644 if (k->k_neg && arg[0] == '-' &&
645 strcmp(k->k_word, arg + 1) == 0)
646 u = &k->k_negu;
647 else if (strcmp(k->k_word, arg) == 0)
648 u = &k->k_u;
649 else
650 continue;
652 if (k->k_altdeact != NULL &&
653 prop_dictionary_get(im->m_env, k->k_altdeact) != NULL)
654 return 1;
656 if (k->k_deact != NULL &&
657 prop_dictionary_get(im->m_env, k->k_deact) != NULL)
658 return 1;
659 break;
661 if (k == NULL) {
662 errno = ENOENT;
663 return -1;
665 switch (k->k_type) {
666 case KW_T_NONE:
667 break;
668 case KW_T_BOOL:
669 o = (prop_object_t)prop_bool_create(u->u_bool);
670 if (o == NULL)
671 goto err;
672 break;
673 case KW_T_INT:
674 o = (prop_object_t)prop_number_create_integer(u->u_sint);
675 if (o == NULL)
676 goto err;
677 break;
678 case KW_T_UINT:
679 o = (prop_object_t)prop_number_create_unsigned_integer(
680 u->u_uint);
681 if (o == NULL)
682 goto err;
683 break;
684 case KW_T_OBJ:
685 o = u->u_obj;
686 break;
687 case KW_T_STR:
688 o = (prop_object_t)prop_string_create_cstring_nocopy(u->u_str);
689 if (o == NULL)
690 goto err;
691 break;
692 default:
693 errx(EXIT_FAILURE, "unknown keyword type %d", k->k_type);
696 if (match_setenv(im, om, (o == NULL) ? NULL : k->k_key, o) == -1)
697 return -1;
699 om->m_argidx = argidx;
700 om->m_parser = p;
701 om->m_nextparser = k->k_nextparser;
702 om->m_exec = k->k_exec;
703 return 0;
704 err:
705 errno = ENOMEM;
706 return -1;
709 struct paddr *
710 paddr_create(const char *name, parser_exec_t pexec, const char *addrkey,
711 const char *maskkey, struct parser *next)
713 struct paddr *pa;
715 if ((pa = calloc(sizeof(*pa), 1)) == NULL)
716 return NULL;
718 pa->pa_parser.p_methods = &paddr_methods;
719 pa->pa_parser.p_exec = pexec;
720 pa->pa_parser.p_name = name;
721 pa->pa_parser.p_nextparser = next;
723 pa->pa_addrkey = addrkey;
724 pa->pa_maskkey = maskkey;
726 return pa;
729 struct piface *
730 piface_create(const char *name, parser_exec_t pexec, const char *defkey,
731 struct parser *defnext)
733 struct piface *pif;
735 if ((pif = calloc(sizeof(*pif), 1)) == NULL)
736 return NULL;
738 pif->pif_parser.p_methods = &piface_methods;
739 pif->pif_parser.p_exec = pexec;
740 pif->pif_parser.p_name = name;
741 pif->pif_parser.p_nextparser = defnext;
743 pif->pif_key = defkey;
745 return pif;
749 pbranch_addbranch(struct pbranch *pb, struct parser *p)
751 struct branch *b;
753 if ((b = malloc(sizeof(*b))) == NULL)
754 return -1;
755 b->b_nextparser = p;
756 SIMPLEQ_INSERT_HEAD(&pb->pb_branches, b, b_next);
757 pb->pb_parser.p_initialized = false;
758 return parser_init(&pb->pb_parser);
762 pbranch_setbranches(struct pbranch *pb, const struct branch *brs, size_t nbr)
764 struct branch *b;
765 size_t i;
767 dbg_warnx("%s: nbr %zu", __func__, nbr);
769 while ((b = SIMPLEQ_FIRST(&pb->pb_branches)) != NULL) {
770 SIMPLEQ_REMOVE_HEAD(&pb->pb_branches, b_next);
771 free(b);
774 for (i = 0; i < nbr; i++) {
775 if ((b = malloc(sizeof(*b))) == NULL)
776 goto err;
777 *b = brs[i];
778 dbg_warnx("%s: b->b_nextparser %p", __func__,
779 (const void *)b->b_nextparser);
780 SIMPLEQ_INSERT_TAIL(&pb->pb_branches, b, b_next);
783 return 0;
784 err:
785 while ((b = SIMPLEQ_FIRST(&pb->pb_branches)) != NULL) {
786 SIMPLEQ_REMOVE_HEAD(&pb->pb_branches, b_next);
787 free(b);
789 return -1;
792 static int
793 pbranch_init(struct parser *p)
795 struct branch *b;
796 struct pbranch *pb = (struct pbranch *)p;
797 struct parser *np;
799 if (pb->pb_nbrinit == 0)
801 else if (pbranch_setbranches(pb, pb->pb_brinit, pb->pb_nbrinit) == -1)
802 return -1;
804 pb->pb_nbrinit = 0;
806 SIMPLEQ_FOREACH(b, &pb->pb_branches, b_next) {
807 np = b->b_nextparser;
808 if (np != NULL && parser_init(np) == -1)
809 return -1;
811 return 0;
814 struct pbranch *
815 pbranch_create(const char *name, const struct branch *brs, size_t nbr,
816 bool match_first)
818 struct pbranch *pb;
820 dbg_warnx("%s: nbr %zu", __func__, nbr);
822 if ((pb = calloc(1, sizeof(*pb))) == NULL)
823 return NULL;
825 pb->pb_parser.p_methods = &pbranch_methods;
826 pb->pb_parser.p_name = name;
828 SIMPLEQ_INIT(&pb->pb_branches);
830 if (pbranch_setbranches(pb, brs, nbr) == -1)
831 goto post_pb_err;
833 pb->pb_match_first = match_first;
834 return pb;
835 post_pb_err:
836 free(pb);
837 return NULL;
840 static int
841 parser_default_init(struct parser *p)
843 struct parser *np;
845 np = p->p_nextparser;
846 if (np != NULL && parser_init(np) == -1)
847 return -1;
849 return 0;
852 static int
853 pkw_setwords(struct pkw *pk, parser_exec_t defexec, const char *defkey,
854 const struct kwinst *kws, size_t nkw, struct parser *defnext)
856 struct kwinst *k;
857 size_t i;
859 for (i = 0; i < nkw; i++) {
860 if (kws[i].k_word == NULL)
861 continue;
862 if ((k = malloc(sizeof(*k))) == NULL)
863 goto post_pk_err;
864 *k = kws[i];
865 if (k->k_nextparser == NULL)
866 k->k_nextparser = defnext;
867 if (k->k_key == NULL)
868 k->k_key = defkey;
869 if (k->k_exec == NULL)
870 k->k_exec = defexec;
871 SIMPLEQ_INSERT_TAIL(&pk->pk_keywords, k, k_next);
873 return 0;
875 post_pk_err:
876 while ((k = SIMPLEQ_FIRST(&pk->pk_keywords)) != NULL) {
877 SIMPLEQ_REMOVE_HEAD(&pk->pk_keywords, k_next);
878 free(k);
880 return -1;
883 static int
884 pkw_init(struct parser *p)
886 struct kwinst *k;
887 struct pkw *pk = (struct pkw *)p;
888 struct parser *np;
890 if (pk->pk_nkwinit == 0)
892 else if (pkw_setwords(pk, pk->pk_execinit, pk->pk_keyinit,
893 pk->pk_kwinit, pk->pk_nkwinit, pk->pk_nextinit) == -1)
894 return -1;
896 pk->pk_nkwinit = 0;
898 SIMPLEQ_FOREACH(k, &pk->pk_keywords, k_next) {
899 np = k->k_nextparser;
900 if (np != NULL && parser_init(np) == -1)
901 return -1;
903 return 0;
906 struct pkw *
907 pkw_create(const char *name, parser_exec_t defexec, const char *defkey,
908 const struct kwinst *kws, size_t nkw, struct parser *defnext)
910 struct pkw *pk;
912 if ((pk = calloc(1, sizeof(*pk))) == NULL)
913 return NULL;
915 pk->pk_parser.p_methods = &pkw_methods;
916 pk->pk_parser.p_exec = defexec;
917 pk->pk_parser.p_name = name;
919 SIMPLEQ_INIT(&pk->pk_keywords);
921 if (pkw_setwords(pk, defexec, defkey, kws, nkw, defnext) == -1)
922 goto err;
924 return pk;
925 err:
926 free(pk);
927 return NULL;
931 parse(int argc, char **argv, const struct parser *p0, struct match *matches,
932 size_t *nmatch, int *narg)
934 int i, rc = 0;
935 struct match *lastm = NULL, *m = matches;
936 const struct parser *p = p0;
938 for (i = 0; i < argc && p != NULL; i++) {
939 if ((size_t)(m - matches) >= *nmatch) {
940 errno = EFBIG;
941 rc = -1;
942 break;
944 rc = (*p->p_methods->pm_match)(p, lastm, m, i, argv[i]);
945 if (rc != 0)
946 goto out;
947 p = m->m_nextparser;
948 lastm = m++;
950 for (; (size_t)(m - matches) < *nmatch && p != NULL; ) {
951 rc = (*p->p_methods->pm_match)(p, lastm, m, i, NULL);
952 if (rc != 0)
953 break;
954 p = m->m_nextparser;
955 lastm = m++;
957 out:
958 *nmatch = m - matches;
959 *narg = i;
960 return rc;
964 matches_exec(const struct match *matches, prop_dictionary_t oenv, size_t nmatch)
966 size_t i;
967 int rc = 0;
968 const struct match *m;
969 parser_exec_t pexec;
970 prop_dictionary_t d;
972 for (i = 0; i < nmatch; i++) {
973 m = &matches[i];
974 dbg_warnx("%s.%d: i %zu", __func__, __LINE__, i);
975 pexec = (m->m_parser->p_exec != NULL)
976 ? m->m_parser->p_exec : m->m_exec;
977 if (pexec == NULL)
978 continue;
979 dbg_warnx("%s.%d: m->m_parser->p_name %s", __func__, __LINE__,
980 m->m_parser->p_name);
981 d = prop_dictionary_augment(m->m_env, oenv);
982 rc = (*pexec)(d, oenv);
983 prop_object_release((prop_object_t)d);
984 if (rc == -1)
985 break;
987 return rc;
991 parser_init(struct parser *p)
993 if (p->p_initialized)
994 return 0;
995 p->p_initialized = true;
996 if (p->p_methods->pm_init == NULL)
997 return 0;
998 return (*p->p_methods->pm_init)(p);