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
55 void yyerror(const char *, ...);
61 %token TPERMIT TDENY TAS TCMD TARGS
62 %token TNOPASS TKEEPENV
73 rule: action ident target cmd {
75 r = calloc(1, sizeof(*r));
77 errx(1, "can't allocate rule");
78 r->action = $1.action;
79 r->options = $1.options;
80 r->envlist = $1.envlist;
84 r->cmdargs = $4.cmdargs;
85 if (nrules == maxrules) {
90 if (!(rules = reallocarray(rules, maxrules,
92 errx(1, "can't allocate rules");
97 action: TPERMIT options {
99 $$.options = $2.options;
100 $$.envlist = $2.envlist;
107 $$.options = $1.options | $2.options;
108 $$.envlist = $1.envlist;
111 yyerror("can't have two keepenv sections");
114 $$.envlist = $2.envlist;
120 $$.options = KEEPENV;
121 } | TKEEPENV '{' envlist '}' {
122 $$.options = KEEPENV;
123 $$.envlist = $3.envlist;
126 envlist: /* empty */ {
127 if (!($$.envlist = calloc(1, sizeof(char *))))
128 errx(1, "can't allocate envlist");
129 } | envlist TSTRING {
130 int nenv = arraylen($1.envlist);
131 if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
133 errx(1, "can't allocate envlist");
134 $$.envlist[nenv] = $2.str;
135 $$.envlist[nenv + 1] = NULL;
143 target: /* optional */ {
149 cmd: /* optional */ {
152 } | TCMD TSTRING args {
154 $$.cmdargs = $3.cmdargs;
160 $$.cmdargs = $2.cmdargs;
163 argslist: /* empty */ {
164 if (!($$.cmdargs = calloc(1, sizeof(char *))))
165 errx(1, "can't allocate args");
166 } | argslist TSTRING {
167 int nargs = arraylen($1.cmdargs);
168 if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2,
170 errx(1, "can't allocate args");
171 $$.cmdargs[nargs] = $2.str;
172 $$.cmdargs[nargs + 1] = NULL;
178 yyerror(const char *fmt, ...)
183 vfprintf(stderr, fmt, va);
185 fprintf(stderr, " at line %d\n", yylval.lineno + 1);
194 { "permit", TPERMIT },
198 { "nopass", TNOPASS },
199 { "keepenv", TKEEPENV },
205 char buf[1024], *ebuf, *p, *str;
206 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
209 ebuf = buf + sizeof(buf);
212 /* skip whitespace first */
213 for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
216 /* check for special one-character constructions */
226 /* skip comments; NUL is allowed; no continuation */
227 while ((c = getc(yyfp)) != '\n')
237 /* parsing next word */
238 for (;; c = getc(yyfp), yylval.colno++) {
241 yyerror("unallowed character NUL in column %d",
252 yyerror("unterminated quotes in column %d",
262 yyerror("unterminated escape in column %d",
265 yyerror("unterminated quotes in column %d",
274 if (!escape && !quotes)
289 yyerror("too long line");
299 * There could be a number of reasons for empty buffer,
300 * and we handle all of them here, to avoid cluttering
305 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
310 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
311 if (strcmp(buf, keywords[i].word) == 0)
312 return keywords[i].token;
315 if ((str = strdup(buf)) == NULL)