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);
62 arraylen(const char **arr)
75 %token TPERMIT TDENY TAS TCMD TARGS
76 %token TNOPASS TPERSIST TKEEPENV TSETENV
87 rule: action ident target cmd {
89 r = calloc(1, sizeof(*r));
91 errx(1, "can't allocate rule");
92 r->action = $1.action;
93 r->options = $1.options;
94 r->envlist = $1.envlist;
98 r->cmdargs = $4.cmdargs;
99 if (nrules == maxrules) {
104 if (!(rules = reallocarray(rules, maxrules,
106 errx(1, "can't allocate rules");
111 action: TPERMIT options {
113 $$.options = $2.options;
114 $$.envlist = $2.envlist;
121 options: /* none */ {
125 $$.options = $1.options | $2.options;
126 $$.envlist = $1.envlist;
127 if (($$.options & (NOPASS|PERSIST)) == (NOPASS|PERSIST)) {
128 yyerror("can't combine nopass and persist");
133 yyerror("can't have two setenv sections");
136 $$.envlist = $2.envlist;
143 $$.options = PERSIST;
146 $$.options = KEEPENV;
148 } | TKEEPENV '{' envlist '}' {
150 if (!obsolete_warned) {
151 warnx("keepenv with list is obsolete");
154 $$.envlist = $3.envlist;
155 } | TSETENV '{' envlist '}' {
157 $$.envlist = $3.envlist;
160 envlist: /* empty */ {
162 } | envlist TSTRING {
163 int nenv = arraylen($1.envlist);
164 if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
166 errx(1, "can't allocate envlist");
167 $$.envlist[nenv] = $2.str;
168 $$.envlist[nenv + 1] = NULL;
176 target: /* optional */ {
182 cmd: /* optional */ {
185 } | TCMD TSTRING args {
187 $$.cmdargs = $3.cmdargs;
193 $$.cmdargs = $2.cmdargs;
196 argslist: /* empty */ {
198 } | argslist TSTRING {
199 int nargs = arraylen($1.cmdargs);
200 if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2,
202 errx(1, "can't allocate args");
203 $$.cmdargs[nargs] = $2.str;
204 $$.cmdargs[nargs + 1] = NULL;
210 yyerror(const char *fmt, ...)
215 vfprintf(stderr, fmt, va);
217 fprintf(stderr, " at line %d\n", yylval.lineno + 1);
221 static struct keyword {
226 { "permit", TPERMIT },
230 { "nopass", TNOPASS },
231 { "persist", TPERSIST },
232 { "keepenv", TKEEPENV },
233 { "setenv", TSETENV },
239 char buf[1024], *ebuf, *p, *str;
240 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
243 ebuf = buf + sizeof(buf);
246 /* skip whitespace first */
247 for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
250 /* check for special one-character constructions */
260 /* skip comments; NUL is allowed; no continuation */
261 while ((c = getc(yyfp)) != '\n')
271 /* parsing next word */
272 for (;; c = getc(yyfp), yylval.colno++) {
275 yyerror("unallowed character NUL in column %d",
286 yyerror("unterminated quotes in column %d",
296 yyerror("unterminated escape in column %d",
299 yyerror("unterminated quotes in column %d",
308 if (!escape && !quotes)
323 yyerror("too long line");
333 * There could be a number of reasons for empty buffer,
334 * and we handle all of them here, to avoid cluttering
339 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
344 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
345 if (strcmp(buf, keywords[i].word) == 0)
346 return keywords[i].token;
349 if ((str = strdup(buf)) == NULL)