2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
4 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
42 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
44 TAILQ_ENTRY(file) entry;
54 struct file *pushfile(const char *);
58 int yyerror(const char *, ...)
59 __attribute__((__format__ (printf, 1, 2)))
60 __attribute__((__nonnull__ (1)));
61 int kw_cmp(const void *, const void *);
82 * %define parse.error verbose
96 %token <v.str> STRING SYMBOL
99 %type <v.op> cast cexpr check expr funcall
100 %type <v.op> literal var varref
102 %type <v.proc> procname
106 program : /* empty */
108 | program include '\n'
114 optnl : '\n' optnl /* zero or more newlines */
118 include : INCLUDE STRING {
121 if ((nfile = pushfile($2)) == NULL) {
122 yyerror("failed to include file %s", $2);
133 const : CONST SYMBOL '=' literal {
134 if (!global_set($2, $4)) {
135 yyerror("constant expression is not a literal");
142 var : SYMBOL '=' expr { $$ = op_assign($1, $3); } ;
143 varref : SYMBOL { $$ = op_var($1); } ;
144 literal : STRING { $$ = op_lit_str($1); }
145 | NUMBER { $$ = op_lit_num($1); } ;
148 * `expr '=' '=' expr` is ambiguous. furthermore, we're not
149 * interested in checking all the possibilities here.
151 cexpr : literal | varref | funcall ;
152 check : cexpr '=' '=' cexpr { $$ = op_cmp_eq($1, $4); } ;
154 expr : literal | funcall | varref | check | cast ;
156 cast : expr ':' U8 { $$ = op_cast($1, V_U8); }
157 | expr ':' U16 { $$ = op_cast($1, V_U16); }
158 | expr ':' U32 { $$ = op_cast($1, V_U32); }
159 | expr ':' STR { $$ = op_cast($1, V_STR); }
163 if (($$ = proc_by_name($1)) == NULL) {
164 yyerror("unknown proc %s", $1);
174 } '(' args optcomma ')' {
176 if ($$->v.funcall.argc != $$->v.funcall.proc->minargs) {
177 yyerror("invalid arity for `%s'",
179 /* TODO: recursively free $$ */
185 optcomma: /* empty */ | ',' ;
188 | args ',' expr { push_arg($3); }
189 | expr { push_arg($1); }
195 if (!proc_setup_body()) {
196 yyerror("invalid argument in proc `%s' definition",
201 } '{' optnl block '}' {
207 | block var '\n' { block_push($2); }
208 | block funcall '\n' { block_push($2); }
212 assert : ASSERT check { block_push(op_assert($2)); }
213 /* | ASSERT '(' optnl massert ')' */
216 massert : /* empty */
217 | massert ',' optnl check { block_push(op_assert($4)); }
218 | check { block_push(op_assert($1)); }
221 test : TESTING STRING DIR STRING {
223 } '{' optnl block '}' {
236 yyerror(const char *fmt, ...)
243 if (vasprintf(&msg, fmt, ap) == -1)
244 fatalx("yyerror vasprintf");
246 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
252 kw_cmp(const void *k, const void *e)
254 return strcmp(k, ((const struct keywords *)e)->k_name);
260 /* This has to be sorted always. */
261 static const struct keywords keywords[] = {
265 {"include", INCLUDE},
269 {"testing", TESTING},
274 const struct keywords *p;
276 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
277 sizeof(keywords[0]), kw_cmp);
285 #define START_EXPAND 1
286 #define DONE_EXPAND 2
288 static int expanding;
296 if (file->ungetpos > 0)
297 c = file->ungetbuf[--file->ungetpos];
299 c = getc(file->stream);
301 if (c == START_EXPAND)
303 else if (c == DONE_EXPAND)
317 if ((c = igetc()) == EOF) {
318 yyerror("reached end of file while parsing "
320 if (file == topfile || popfile() == EOF)
327 while ((c = igetc()) == '\\') {
333 yylval.lineno = file->lineno;
339 * Fake EOL when hit EOF for the first time. This gets line
340 * count right if last line in included file is syntactically
341 * invalid and has no newline.
343 if (file->eof_reached == 0) {
344 file->eof_reached = 1;
348 if (file == topfile || popfile() == EOF)
362 if (file->ungetpos >= file->ungetsize) {
363 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
367 file->ungetsize *= 2;
369 file->ungetbuf[file->ungetpos++] = c;
377 /* Skip to either EOF or the first real EOL. */
399 switch (x = my_yylex()) {
400 case ASSERT: puts("assert"); break;
401 case CONST: puts("const"); break;
402 case DIR: puts("dir"); break;
403 case ERROR: puts("error"); break;
404 case INCLUDE: puts("include"); break;
405 case PROC: puts("proc"); break;
406 case REPEAT: puts("repeat"); break;
407 case STR: puts(":str"); break;
408 case TESTING: puts("testing"); break;
409 case U8: puts(":u8"); break;
410 case U16: puts(":u16"); break;
411 case U32: puts(":u32"); break;
413 case STRING: printf("string \"%s\"\n", yylval.v.str); break;
414 case SYMBOL: printf("symbol %s\n", yylval.v.str); break;
415 case NUMBER: printf("number %"PRIu64"\n", yylval.v.num); break;
418 printf("character ");
423 printf(" [0x%x]", x);
438 unsigned char buf[8096];
444 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\f')
447 yylval.lineno = file->lineno;
449 while ((c = lgetc(0)) != '\n' && c != EOF)
460 if ((c = lgetc(quotec)) == EOF)
465 } else if (c == '\\') {
466 if ((next = lgetc(quotec)) == EOF)
468 if (next == quotec || next == ' ' ||
471 else if (next == '\n') {
476 } else if (c == quotec) {
479 } else if (c == '\0') {
480 yyerror("syntax error");
484 if (p + 1 >= buf + sizeof(buf) - 1) {
485 yyerror("string too long");
492 yylval.v.str = xstrdup(buf);
496 #define allowed_to_end_number(x) \
497 (isspace(x) || x == ')' || x == ',' || x == '/' || x == '}' \
498 || x == '=' || x == ':')
500 if (c == '-' || isdigit(c)) {
503 if ((size_t)(p-buf) >= sizeof(buf)) {
504 yyerror("string too long");
507 } while ((c = lgetc(0)) != EOF && isdigit(c));
509 if (p == buf + 1 && buf[0] == '-')
511 if (c == EOF || allowed_to_end_number(c)) {
512 const char *errstr = NULL;
515 yylval.v.num = strtonum(buf, INT64_MIN, INT64_MAX,
518 yyerror("\"%s\" invalid number: %s",
533 #define allowed_in_symbol(x) \
534 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
535 x != '{' && x != '}' && \
536 x != '!' && x != '=' && \
537 x != '#' && x != ','))
539 if (isalnum(c) || c == ':' || c == '_') {
542 if ((size_t)(p-buf) >= sizeof(buf)) {
543 yyerror("string too long");
546 } while ((c = lgetc(0)) != EOF && (allowed_in_symbol(c)));
549 if ((token = lookup(buf)) == SYMBOL)
550 yylval.v.str = xstrdup(buf);
555 yylval.lineno = file->lineno;
564 pushfile(const char *name)
568 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
572 if ((nfile->name = strdup(name)) == NULL) {
577 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
578 log_warn("%s", nfile->name);
583 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
584 nfile->ungetsize = 16;
585 nfile->ungetbuf = malloc(nfile->ungetsize);
586 if (nfile->ungetbuf == NULL) {
588 fclose(nfile->stream);
593 TAILQ_INSERT_TAIL(&files, nfile, entry);
602 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
603 prev->errors += file->errors;
605 TAILQ_REMOVE(&files, file, entry);
606 fclose(file->stream);
608 free(file->ungetbuf);
611 return file ? 0 : EOF;
615 loadfile(const char *path)
619 file = pushfile(path);
625 errors = file->errors;
629 errx(1, "can't load %s because of errors", path);