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>
44 #define YYSTYPE yystype
52 void yyerror(const char *, ...);
58 %token TPERMIT TDENY TAS TCMD TARGS
59 %token TNOPASS TKEEPENV
70 rule: action ident target cmd {
72 r = calloc(1, sizeof(*r));
74 errx(1, "can't allocate rule");
75 r->action = $1.action;
76 r->options = $1.options;
77 r->envlist = $1.envlist;
81 r->cmdargs = $4.cmdargs;
82 if (nrules == maxrules) {
87 if (!(rules = reallocarray(rules, maxrules,
89 errx(1, "can't allocate rules");
94 action: TPERMIT options {
96 $$.options = $2.options;
97 $$.envlist = $2.envlist;
104 $$.options = $1.options | $2.options;
105 $$.envlist = $1.envlist;
108 yyerror("can't have two keepenv sections");
111 $$.envlist = $2.envlist;
117 $$.options = KEEPENV;
118 } | TKEEPENV '{' envlist '}' {
119 $$.options = KEEPENV;
120 $$.envlist = $3.envlist;
123 envlist: /* empty */ {
124 if (!($$.envlist = calloc(1, sizeof(char *))))
125 errx(1, "can't allocate envlist");
126 } | envlist TSTRING {
127 int nenv = arraylen($1.envlist);
128 if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
130 errx(1, "can't allocate envlist");
131 $$.envlist[nenv] = $2.str;
132 $$.envlist[nenv + 1] = NULL;
140 target: /* optional */ {
146 cmd: /* optional */ {
149 } | TCMD TSTRING args {
151 $$.cmdargs = $3.cmdargs;
157 $$.cmdargs = $2.cmdargs;
160 argslist: /* empty */ {
161 if (!($$.cmdargs = calloc(1, sizeof(char *))))
162 errx(1, "can't allocate args");
163 } | argslist TSTRING {
164 int nargs = arraylen($1.cmdargs);
165 if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2,
167 errx(1, "can't allocate args");
168 $$.cmdargs[nargs] = $2.str;
169 $$.cmdargs[nargs + 1] = NULL;
175 yyerror(const char *fmt, ...)
180 vfprintf(stderr, fmt, va);
182 fprintf(stderr, " at line %d\n", yylval.lineno + 1);
191 { "permit", TPERMIT },
195 { "nopass", TNOPASS },
196 { "keepenv", TKEEPENV },
202 char buf[1024], *ebuf, *p, *str;
203 int i, c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
206 ebuf = buf + sizeof(buf);
209 /* skip whitespace first */
210 for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
213 /* check for special one-character constructions */
223 /* skip comments; NUL is allowed; no continuation */
224 while ((c = getc(yyfp)) != '\n')
234 /* parsing next word */
235 for (;; c = getc(yyfp), yylval.colno++) {
238 yyerror("unallowed character NUL in column %d",
249 yyerror("unterminated quotes in column %d",
259 yyerror("unterminated escape in column %d",
262 yyerror("unterminated quotes in column %d",
271 if (!escape && !quotes)
286 yyerror("too long line");
296 * There could be a number of reasons for empty buffer,
297 * and we handle all of them here, to avoid cluttering
302 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
306 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
307 if (strcmp(buf, keywords[i].word) == 0)
308 return keywords[i].token;
311 if ((str = strdup(buf)) == NULL)