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>
48 #define YYSTYPE yystype
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 } | TSETENV '{' strlist '}' {
150 $$.envlist = $3.strlist;
153 strlist: /* empty */ {
154 if (!($$.strlist = calloc(1, sizeof(char *))))
155 errx(1, "can't allocate strlist");
156 } | strlist TSTRING {
157 int nstr = arraylen($1.strlist);
158 if (!($$.strlist = reallocarray($1.strlist, nstr + 2,
160 errx(1, "can't allocate strlist");
161 $$.strlist[nstr] = $2.str;
162 $$.strlist[nstr + 1] = NULL;
170 target: /* optional */ {
176 cmd: /* optional */ {
179 } | TCMD TSTRING args {
181 $$.cmdargs = $3.cmdargs;
187 $$.cmdargs = $2.strlist;
193 yyerror(const char *fmt, ...)
198 vfprintf(stderr, fmt, va);
200 fprintf(stderr, " at line %d\n", yylval.lineno + 1);
204 static struct keyword {
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)