1 /* $OpenBSD: parse.y,v 1.10 2015/07/24 06:36:42 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>
47 #define YYSTYPE yystype
54 int obsolete_warned = 0;
56 void yyerror(const char *, ...);
62 %token TPERMIT TDENY TAS TCMD TARGS
63 %token TNOPASS TPERSIST TKEEPENV TSETENV
74 rule: action ident target cmd {
76 r = calloc(1, sizeof(*r));
78 errx(1, "can't allocate rule");
79 r->action = $1.action;
80 r->options = $1.options;
81 r->envlist = $1.envlist;
85 r->cmdargs = $4.cmdargs;
86 if (nrules == maxrules) {
91 if (!(rules = reallocarray(rules, maxrules,
93 errx(1, "can't allocate rules");
98 action: TPERMIT options {
100 $$.options = $2.options;
101 $$.envlist = $2.envlist;
108 options: /* none */ {
112 $$.options = $1.options | $2.options;
113 $$.envlist = $1.envlist;
116 yyerror("can't have two setenv sections");
119 $$.envlist = $2.envlist;
126 $$.options = PERSIST;
129 $$.options = KEEPENV;
131 } | TKEEPENV '{' envlist '}' {
133 if (!obsolete_warned) {
134 warnx("keepenv with list is obsolete");
137 $$.envlist = $3.envlist;
138 } | TSETENV '{' envlist '}' {
140 $$.envlist = $3.envlist;
143 envlist: /* empty */ {
145 } | envlist TSTRING {
146 int nenv = arraylen($1.envlist);
147 if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
149 errx(1, "can't allocate envlist");
150 $$.envlist[nenv] = $2.str;
151 $$.envlist[nenv + 1] = NULL;
159 target: /* optional */ {
165 cmd: /* optional */ {
168 } | TCMD TSTRING args {
170 $$.cmdargs = $3.cmdargs;
176 $$.cmdargs = $2.cmdargs;
179 argslist: /* empty */ {
181 } | argslist TSTRING {
182 int nargs = arraylen($1.cmdargs);
183 if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2,
185 errx(1, "can't allocate args");
186 $$.cmdargs[nargs] = $2.str;
187 $$.cmdargs[nargs + 1] = NULL;
193 yyerror(const char *fmt, ...)
198 vfprintf(stderr, fmt, va);
200 fprintf(stderr, " at line %d\n", yylval.lineno + 1);
209 { "permit", TPERMIT },
213 { "nopass", TNOPASS },
214 { "persist", TPERSIST },
215 { "keepenv", TKEEPENV },
216 { "setenv", TSETENV },
222 char buf[1024], *ebuf, *p, *str;
223 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
226 ebuf = buf + sizeof(buf);
229 /* skip whitespace first */
230 for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
233 /* check for special one-character constructions */
243 /* skip comments; NUL is allowed; no continuation */
244 while ((c = getc(yyfp)) != '\n')
254 /* parsing next word */
255 for (;; c = getc(yyfp), yylval.colno++) {
258 yyerror("unallowed character NUL in column %d",
269 yyerror("unterminated quotes in column %d",
279 yyerror("unterminated escape in column %d",
282 yyerror("unterminated quotes in column %d",
291 if (!escape && !quotes)
306 yyerror("too long line");
316 * There could be a number of reasons for empty buffer,
317 * and we handle all of them here, to avoid cluttering
322 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
327 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
328 if (strcmp(buf, keywords[i].word) == 0)
329 return keywords[i].token;
332 if ((str = strdup(buf)) == NULL)