1 /* $OpenBSD: parse.y,v 1.3 2015/07/16 22:33:01 zhuk Exp $ */
3 * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
40 #define YYSTYPE yystype
47 void yyerror(const char *, ...);
53 %token TPERMIT TDENY TAS TCMD
54 %token TNOPASS TKEEPENV
64 rule: action ident target cmd {
66 r = calloc(1, sizeof(*r));
68 errx(1, "can't allocate rule");
69 r->action = $1.action;
70 r->options = $1.options;
71 r->envlist = $1.envlist;
75 if (nrules == maxrules) {
80 if (!(rules = reallocarray(rules, maxrules, sizeof(*rules))))
81 errx(1, "can't allocate rules");
86 action: TPERMIT options {
88 $$.options = $2.options;
89 $$.envlist = $2.envlist;
96 $$.options = $1.options | $2.options;
97 $$.envlist = $1.envlist;
100 errx(1, "can't have two keepenv sections");
102 $$.envlist = $2.envlist;
108 $$.options = KEEPENV;
109 } | TKEEPENV '{' envlist '}' {
110 $$.options = KEEPENV;
111 $$.envlist = $3.envlist;
114 envlist: /* empty */ {
115 if (!($$.envlist = calloc(1, sizeof(char *))))
116 errx(1, "can't allocate envlist");
117 } | envlist TSTRING {
118 int nenv = arraylen($1.envlist);
119 if (!($$.envlist = reallocarray($1.envlist, nenv + 2, sizeof(char *))))
120 errx(1, "can't allocate envlist");
121 $$.envlist[nenv] = $2.str;
122 $$.envlist[nenv + 1] = NULL;
130 target: /* optional */ {
136 cmd: /* optional */ {
145 yyerror(const char *fmt, ...)
158 { "permit", TPERMIT },
161 { "nopass", TNOPASS },
162 { "keepenv", TKEEPENV },
168 char buf[1024], *ebuf, *p, *str;
172 ebuf = buf + sizeof(buf);
173 while ((c = getc(yyfp)) == ' ' || c == '\t')
181 while ((c = getc(yyfp)) != '\n' && c != EOF)
182 ; /* skip comments */
202 yyerror("too much stuff");
209 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
210 if (strcmp(buf, keywords[i].word) == 0)
211 return keywords[i].token;
213 if ((str = strdup(buf)) == NULL)