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
56 static int obsolete_warned = 0;
58 static void yyerror(const char *, ...);
59 static int yylex(void);
63 %token TPERMIT TDENY TAS TCMD TARGS
64 %token TNOPASS TPERSIST TKEEPENV TSETENV
75 rule: action ident target cmd {
77 r = calloc(1, sizeof(*r));
79 errx(1, "can't allocate rule");
80 r->action = $1.action;
81 r->options = $1.options;
82 r->envlist = $1.envlist;
86 r->cmdargs = $4.cmdargs;
87 if (nrules == maxrules) {
92 if (!(rules = reallocarray(rules, maxrules,
94 errx(1, "can't allocate rules");
99 action: TPERMIT options {
101 $$.options = $2.options;
102 $$.envlist = $2.envlist;
109 options: /* none */ {
113 $$.options = $1.options | $2.options;
114 $$.envlist = $1.envlist;
115 if (($$.options & (NOPASS|PERSIST)) == (NOPASS|PERSIST)) {
116 yyerror("can't combine nopass and persist");
121 yyerror("can't have two setenv sections");
124 $$.envlist = $2.envlist;
131 $$.options = PERSIST;
134 $$.options = KEEPENV;
136 } | TKEEPENV '{' envlist '}' {
138 if (!obsolete_warned) {
139 warnx("keepenv with list is obsolete");
142 $$.envlist = $3.envlist;
143 } | TSETENV '{' envlist '}' {
145 $$.envlist = $3.envlist;
148 envlist: /* empty */ {
150 } | envlist TSTRING {
151 int nenv = arraylen($1.envlist);
152 if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
154 errx(1, "can't allocate envlist");
155 $$.envlist[nenv] = $2.str;
156 $$.envlist[nenv + 1] = NULL;
164 target: /* optional */ {
170 cmd: /* optional */ {
173 } | TCMD TSTRING args {
175 $$.cmdargs = $3.cmdargs;
181 $$.cmdargs = $2.cmdargs;
184 argslist: /* empty */ {
186 } | argslist TSTRING {
187 int nargs = arraylen($1.cmdargs);
188 if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2,
190 errx(1, "can't allocate args");
191 $$.cmdargs[nargs] = $2.str;
192 $$.cmdargs[nargs + 1] = NULL;
198 yyerror(const char *fmt, ...)
203 vfprintf(stderr, fmt, va);
205 fprintf(stderr, " at line %d\n", yylval.lineno + 1);
209 static struct keyword {
214 { "permit", TPERMIT },
218 { "nopass", TNOPASS },
219 { "persist", TPERSIST },
220 { "keepenv", TKEEPENV },
221 { "setenv", TSETENV },
227 char buf[1024], *ebuf, *p, *str;
228 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
231 ebuf = buf + sizeof(buf);
234 /* skip whitespace first */
235 for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
238 /* check for special one-character constructions */
248 /* skip comments; NUL is allowed; no continuation */
249 while ((c = getc(yyfp)) != '\n')
259 /* parsing next word */
260 for (;; c = getc(yyfp), yylval.colno++) {
263 yyerror("unallowed character NUL in column %d",
274 yyerror("unterminated quotes in column %d",
284 yyerror("unterminated escape in column %d",
287 yyerror("unterminated quotes in column %d",
296 if (!escape && !quotes)
311 yyerror("too long line");
321 * There could be a number of reasons for empty buffer,
322 * and we handle all of them here, to avoid cluttering
327 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
332 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
333 if (strcmp(buf, keywords[i].word) == 0)
334 return keywords[i].token;
337 if ((str = strdup(buf)) == NULL)