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;
114 if (($$.options & (NOPASS|PERSIST)) == (NOPASS|PERSIST)) {
115 yyerror("can't combine nopass and persist");
120 yyerror("can't have two setenv sections");
123 $$.envlist = $2.envlist;
130 $$.options = PERSIST;
133 $$.options = KEEPENV;
135 } | TKEEPENV '{' envlist '}' {
137 if (!obsolete_warned) {
138 warnx("keepenv with list is obsolete");
141 $$.envlist = $3.envlist;
142 } | TSETENV '{' envlist '}' {
144 $$.envlist = $3.envlist;
147 envlist: /* empty */ {
149 } | envlist TSTRING {
150 int nenv = arraylen($1.envlist);
151 if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
153 errx(1, "can't allocate envlist");
154 $$.envlist[nenv] = $2.str;
155 $$.envlist[nenv + 1] = NULL;
163 target: /* optional */ {
169 cmd: /* optional */ {
172 } | TCMD TSTRING args {
174 $$.cmdargs = $3.cmdargs;
180 $$.cmdargs = $2.cmdargs;
183 argslist: /* empty */ {
185 } | argslist TSTRING {
186 int nargs = arraylen($1.cmdargs);
187 if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2,
189 errx(1, "can't allocate args");
190 $$.cmdargs[nargs] = $2.str;
191 $$.cmdargs[nargs + 1] = NULL;
197 yyerror(const char *fmt, ...)
202 vfprintf(stderr, fmt, va);
204 fprintf(stderr, " at line %d\n", yylval.lineno + 1);
213 { "permit", TPERMIT },
217 { "nopass", TNOPASS },
218 { "persist", TPERSIST },
219 { "keepenv", TKEEPENV },
220 { "setenv", TSETENV },
226 char buf[1024], *ebuf, *p, *str;
227 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
230 ebuf = buf + sizeof(buf);
233 /* skip whitespace first */
234 for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
237 /* check for special one-character constructions */
247 /* skip comments; NUL is allowed; no continuation */
248 while ((c = getc(yyfp)) != '\n')
258 /* parsing next word */
259 for (;; c = getc(yyfp), yylval.colno++) {
262 yyerror("unallowed character NUL in column %d",
273 yyerror("unterminated quotes in column %d",
283 yyerror("unterminated escape in column %d",
286 yyerror("unterminated quotes in column %d",
295 if (!escape && !quotes)
310 yyerror("too long line");
320 * There could be a number of reasons for empty buffer,
321 * and we handle all of them here, to avoid cluttering
326 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
331 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
332 if (strcmp(buf, keywords[i].word) == 0)
333 return keywords[i].token;
336 if ((str = strdup(buf)) == NULL)