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 nl : '\n' optnl /* one or more newlines */
121 include : INCLUDE STRING {
124 if ((nfile = pushfile($2)) == NULL) {
125 yyerror("failed to include file %s", $2);
136 const : CONST SYMBOL '=' literal { global_set($2, $4); };
138 var : SYMBOL '=' expr { $$ = op_assign($1, $3); } ;
139 varref : SYMBOL { $$ = op_var($1); } ;
140 literal : STRING { $$ = op_lit_str($1); }
141 | NUMBER { $$ = op_lit_num($1); } ;
144 * `expr '=' '=' expr` is ambiguous. furthermore, we're not
145 * interested in checking all the possibilities here.
147 cexpr : literal | varref | funcall ;
148 check : cexpr '=' '=' cexpr { $$ = op_cmp_eq($1, $4); } ;
150 expr : literal | funcall | varref | check | cast ;
152 cast : expr ':' U8 { $$ = op_cast($1, V_U8); }
153 | expr ':' U16 { $$ = op_cast($1, V_U16); }
154 | expr ':' U32 { $$ = op_cast($1, V_U32); }
155 | expr ':' STR { $$ = op_cast($1, V_STR); }
159 if (($$ = proc_by_name($1)) == NULL) {
160 yyerror("unknown proc %s", $1);
170 } '(' args optcomma ')' {
175 optcomma: /* empty */ | ',' ;
178 | args ',' expr { push_arg($3); }
179 | expr { push_arg($1); }
185 if (!proc_setup_body()) {
186 yyerror("invalid argument in proc `%s' definition",
191 } '{' optnl block '}' {
197 | block var '\n' { block_push($2); }
198 | block funcall '\n' { block_push($2); }
202 assert : ASSERT check { block_push(op_assert($2)); }
203 /* | ASSERT '(' optnl massert ')' */
206 massert : /* empty */
207 | massert ',' optnl check { block_push(op_assert($4)); }
208 | check { block_push(op_assert($1)); }
211 test : TESTING STRING DIR STRING {
213 } '{' optnl block '}' {
226 yyerror(const char *fmt, ...)
233 if (vasprintf(&msg, fmt, ap) == -1)
234 fatalx("yyerror vasprintf");
236 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
242 kw_cmp(const void *k, const void *e)
244 return strcmp(k, ((const struct keywords *)e)->k_name);
250 /* This has to be sorted always. */
251 static const struct keywords keywords[] = {
255 {"include", INCLUDE},
259 {"testing", TESTING},
264 const struct keywords *p;
266 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
267 sizeof(keywords[0]), kw_cmp);
275 #define START_EXPAND 1
276 #define DONE_EXPAND 2
278 static int expanding;
286 if (file->ungetpos > 0)
287 c = file->ungetbuf[--file->ungetpos];
289 c = getc(file->stream);
291 if (c == START_EXPAND)
293 else if (c == DONE_EXPAND)
307 if ((c = igetc()) == EOF) {
308 yyerror("reached end of file while parsing "
310 if (file == topfile || popfile() == EOF)
317 while ((c = igetc()) == '\\') {
323 yylval.lineno = file->lineno;
329 * Fake EOL when hit EOF for the first time. This gets line
330 * count right if last line in included file is syntactically
331 * invalid and has no newline.
333 if (file->eof_reached == 0) {
334 file->eof_reached = 1;
338 if (file == topfile || popfile() == EOF)
352 if (file->ungetpos >= file->ungetsize) {
353 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
357 file->ungetsize *= 2;
359 file->ungetbuf[file->ungetpos++] = c;
367 /* Skip to either EOF or the first real EOL. */
389 switch (x = my_yylex()) {
390 case ASSERT: puts("assert"); break;
391 case CONST: puts("const"); break;
392 case DIR: puts("dir"); break;
393 case ERROR: puts("error"); break;
394 case INCLUDE: puts("include"); break;
395 case PROC: puts("proc"); break;
396 case REPEAT: puts("repeat"); break;
397 case STR: puts(":str"); break;
398 case TESTING: puts("testing"); break;
399 case U8: puts(":u8"); break;
400 case U16: puts(":u16"); break;
401 case U32: puts(":u32"); break;
403 case STRING: printf("string \"%s\"\n", yylval.v.str); break;
404 case SYMBOL: printf("symbol %s\n", yylval.v.str); break;
405 case NUMBER: printf("number %"PRIu64"\n", yylval.v.num); break;
408 printf("character ");
413 printf(" [0x%x]", x);
428 unsigned char buf[8096];
434 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\f')
437 yylval.lineno = file->lineno;
439 while ((c = lgetc(0)) != '\n' && c != EOF)
447 if ((c = lgetc(quotec)) == EOF)
452 } else if (c == '\\') {
453 if ((next = lgetc(quotec)) == EOF)
455 if (next == quotec || next == ' ' ||
458 else if (next == '\n') {
463 } else if (c == quotec) {
466 } else if (c == '\0') {
467 yyerror("syntax error");
471 if (p + 1 >= buf + sizeof(buf) - 1) {
472 yyerror("string too long");
479 yylval.v.str = xstrdup(buf);
483 #define allowed_to_end_number(x) \
484 (isspace(x) || x == ')' || x == ',' || x == '/' || x == '}' \
487 if (c == '-' || isdigit(c)) {
490 if ((size_t)(p-buf) >= sizeof(buf)) {
491 yyerror("string too long");
494 } while ((c = lgetc(0)) != EOF && isdigit(c));
496 if (p == buf + 1 && buf[0] == '-')
498 if (c == EOF || allowed_to_end_number(c)) {
499 const char *errstr = NULL;
502 yylval.v.num = strtonum(buf, INT64_MIN, INT64_MAX,
505 yyerror("\"%s\" invalid number: %s",
520 #define allowed_in_symbol(x) \
521 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
522 x != '{' && x != '}' && \
523 x != '!' && x != '=' && \
524 x != '#' && x != ','))
526 if (isalnum(c) || c == ':' || c == '_') {
529 if ((size_t)(p-buf) >= sizeof(buf)) {
530 yyerror("string too long");
533 } while ((c = lgetc(0)) != EOF && (allowed_in_symbol(c)));
536 if ((token = lookup(buf)) == SYMBOL)
537 yylval.v.str = xstrdup(buf);
542 yylval.lineno = file->lineno;
551 pushfile(const char *name)
555 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
559 if ((nfile->name = strdup(name)) == NULL) {
564 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
565 log_warn("%s", nfile->name);
570 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
571 nfile->ungetsize = 16;
572 nfile->ungetbuf = malloc(nfile->ungetsize);
573 if (nfile->ungetbuf == NULL) {
575 fclose(nfile->stream);
580 TAILQ_INSERT_TAIL(&files, nfile, entry);
589 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
590 prev->errors += file->errors;
592 TAILQ_REMOVE(&files, file, entry);
593 fclose(file->stream);
595 free(file->ungetbuf);
598 return file ? 0 : EOF;
602 loadfile(const char *path)
606 file = pushfile(path);
612 errors = file->errors;
616 errx(1, "can't load %s because of errors", path);