1 /* $OpenBSD: parse.y,v 1.9 2015/07/22 20:15:24 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>
44 #define YYSTYPE yystype
52 void yyerror(const char *, ...);
58 %token TPERMIT TDENY TAS TCMD TARGS
59 %token TNOPASS TKEEPENV
70 rule: action ident target cmd {
72 r = calloc(1, sizeof(*r));
74 errx(1, "can't allocate rule");
75 r->action = $1.action;
76 r->options = $1.options;
77 r->envlist = $1.envlist;
81 r->cmdargs = $4.cmdargs;
82 if (nrules == maxrules) {
87 if (!(rules = reallocarray(rules, maxrules,
89 errx(1, "can't allocate rules");
94 action: TPERMIT options {
96 $$.options = $2.options;
97 $$.envlist = $2.envlist;
104 $$.options = $1.options | $2.options;
105 $$.envlist = $1.envlist;
108 yyerror("can't have two keepenv sections");
111 $$.envlist = $2.envlist;
117 $$.options = KEEPENV;
118 } | TKEEPENV '{' envlist '}' {
119 $$.options = KEEPENV;
120 $$.envlist = $3.envlist;
123 envlist: /* empty */ {
124 if (!($$.envlist = calloc(1, sizeof(char *))))
125 errx(1, "can't allocate envlist");
126 } | envlist TSTRING {
127 int nenv = arraylen($1.envlist);
128 if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
130 errx(1, "can't allocate envlist");
131 $$.envlist[nenv] = $2.str;
132 $$.envlist[nenv + 1] = NULL;
140 target: /* optional */ {
146 cmd: /* optional */ {
149 } | TCMD TSTRING args {
151 $$.cmdargs = $3.cmdargs;
157 $$.cmdargs = $2.cmdargs;
160 argslist: /* empty */ {
161 if (!($$.cmdargs = calloc(1, sizeof(char *))))
162 errx(1, "can't allocate args");
163 } | argslist TSTRING {
164 int nargs = arraylen($1.cmdargs);
165 if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2, sizeof(char *))))
166 errx(1, "can't allocate args");
167 $$.cmdargs[nargs] = $2.str;
168 $$.cmdargs[nargs + 1] = NULL;
174 yyerror(const char *fmt, ...)
179 vfprintf(stderr, fmt, va);
181 fprintf(stderr, " at line %d\n", yylval.lineno + 1);
190 { "permit", TPERMIT },
194 { "nopass", TNOPASS },
195 { "keepenv", TKEEPENV },
201 char buf[1024], *ebuf, *p, *str;
202 int i, c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
205 ebuf = buf + sizeof(buf);
208 /* skip whitespace first */
209 for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
212 /* check for special one-character constructions */
222 /* skip comments; NUL is allowed; no continuation */
223 while ((c = getc(yyfp)) != '\n')
233 /* parsing next word */
234 for (;; c = getc(yyfp), yylval.colno++) {
237 yyerror("unallowed character NUL in column %d", yylval.colno + 1);
247 yyerror("unterminated quotes in column %d",
257 yyerror("unterminated escape in column %d",
260 yyerror("unterminated quotes in column %d",
269 if (!escape && !quotes)
284 yyerror("too long line");
294 * There could be a number of reasons for empty buffer, and we handle
295 * all of them here, to avoid cluttering the main loop.
299 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
303 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
304 if (strcmp(buf, keywords[i].word) == 0)
305 return keywords[i].token;
308 if ((str = strdup(buf)) == NULL)