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
57 static void yyerror(const char *, ...);
58 static int yylex(void);
61 arraylen(const char **arr)
74 %token TPERMIT TDENY TAS TCMD TARGS
75 %token TNOPASS TPERSIST TKEEPENV TSETENV
86 rule: action ident target cmd {
88 r = calloc(1, sizeof(*r));
90 errx(1, "can't allocate rule");
91 r->action = $1.action;
92 r->options = $1.options;
93 r->envlist = $1.envlist;
97 r->cmdargs = $4.cmdargs;
98 if (nrules == maxrules) {
103 if (!(rules = reallocarray(rules, maxrules,
105 errx(1, "can't allocate rules");
110 action: TPERMIT options {
112 $$.options = $2.options;
113 $$.envlist = $2.envlist;
120 options: /* none */ {
124 $$.options = $1.options | $2.options;
125 $$.envlist = $1.envlist;
126 if (($$.options & (NOPASS|PERSIST)) == (NOPASS|PERSIST)) {
127 yyerror("can't combine nopass and persist");
132 yyerror("can't have two setenv sections");
135 $$.envlist = $2.envlist;
142 $$.options = PERSIST;
145 $$.options = KEEPENV;
147 } | TSETENV '{' envlist '}' {
149 $$.envlist = $3.envlist;
152 envlist: /* empty */ {
154 } | envlist TSTRING {
155 int nenv = arraylen($1.envlist);
156 if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
158 errx(1, "can't allocate envlist");
159 $$.envlist[nenv] = $2.str;
160 $$.envlist[nenv + 1] = NULL;
168 target: /* optional */ {
174 cmd: /* optional */ {
177 } | TCMD TSTRING args {
179 $$.cmdargs = $3.cmdargs;
185 $$.cmdargs = $2.cmdargs;
188 argslist: /* empty */ {
190 } | argslist TSTRING {
191 int nargs = arraylen($1.cmdargs);
192 if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2,
194 errx(1, "can't allocate args");
195 $$.cmdargs[nargs] = $2.str;
196 $$.cmdargs[nargs + 1] = NULL;
202 yyerror(const char *fmt, ...)
207 vfprintf(stderr, fmt, va);
209 fprintf(stderr, " at line %d\n", yylval.lineno + 1);
213 static struct keyword {
218 { "permit", TPERMIT },
222 { "nopass", TNOPASS },
223 { "persist", TPERSIST },
224 { "keepenv", TKEEPENV },
225 { "setenv", TSETENV },
231 char buf[1024], *ebuf, *p, *str;
232 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
235 ebuf = buf + sizeof(buf);
238 /* skip whitespace first */
239 for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
242 /* check for special one-character constructions */
252 /* skip comments; NUL is allowed; no continuation */
253 while ((c = getc(yyfp)) != '\n')
263 /* parsing next word */
264 for (;; c = getc(yyfp), yylval.colno++) {
267 yyerror("unallowed character NUL in column %d",
278 yyerror("unterminated quotes in column %d",
288 yyerror("unterminated escape in column %d",
291 yyerror("unterminated quotes in column %d",
300 if (!escape && !quotes)
315 yyerror("too long line");
325 * There could be a number of reasons for empty buffer,
326 * and we handle all of them here, to avoid cluttering
331 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
336 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
337 if (strcmp(buf, keywords[i].word) == 0)
338 return keywords[i].token;
341 if ((str = strdup(buf)) == NULL)