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 *);
68 static int shouldfail;
84 * %define parse.error verbose
94 %token SHOULD_FAIL STR
98 %token <v.str> STRING SYMBOL
101 %type <v.op> cast cexpr check expr faccess funcall
102 %type <v.op> literal sfail var varref
104 %type <v.proc> procname
108 program : /* empty */
110 | program include '\n'
116 optnl : '\n' optnl /* zero or more newlines */
122 include : INCLUDE STRING {
125 if ((nfile = pushfile($2)) == NULL) {
126 yyerror("failed to include file %s", $2);
138 | CONST '(' optnl mconst ')'
141 mconst : consti nl | mconst consti nl ;
143 consti : SYMBOL '=' expr {
144 if (!global_set($1, $3)) {
145 yyerror("can't set %s: illegal expression", $1);
153 var : SYMBOL '=' expr { $$ = op_assign($1, $3); } ;
154 varref : SYMBOL { $$ = op_var($1); } ;
155 literal : STRING { $$ = op_lit_str($1); }
156 | NUMBER { $$ = op_lit_num($1); } ;
159 * `expr '=' '=' expr` is ambiguous. furthermore, we're not
160 * interested in checking all the possibilities here.
162 cexpr : literal | varref | funcall | faccess ;
163 check : cexpr '=' '=' cexpr { $$ = op_cmp_eq($1, $4); } ;
165 expr : literal | funcall | varref | check | cast | faccess ;
167 cast : expr ':' U8 { $$ = op_cast($1, V_U8); }
168 | expr ':' U16 { $$ = op_cast($1, V_U16); }
169 | expr ':' U32 { $$ = op_cast($1, V_U32); }
170 | expr ':' STR { $$ = op_cast($1, V_STR); }
173 faccess : varref '.' SYMBOL { $$ = op_faccess($1, $3); }
174 | faccess '.' SYMBOL { $$ = op_faccess($1, $3); }
178 if (($$ = proc_by_name($1)) == NULL) {
179 yyerror("unknown proc %s", $1);
189 } '(' args optcomma ')' {
194 proc = $$->v.funcall.proc;
195 argc = $$->v.funcall.argc;
197 if (argc != proc->minargs && !proc->vararg) {
198 yyerror("invalid arity for `%s': want %d arguments "
199 "but %d given.", $1->name, proc->minargs, argc);
200 /* TODO: recursively free $$ */
204 if (argc < proc->minargs && proc->vararg) {
205 yyerror("invalid arity for `%s': want at least %d "
206 "arguments but %d given.", $1->name, proc->minargs,
208 /* TODO: recursively free $$ */
214 optcomma: /* empty */ | ',' ;
219 | args ',' expr { push_arg($3); }
220 | args ',' dots { push_arg(op_rest()); }
221 | expr { push_arg($1); }
222 | dots { push_arg(op_rest()); }
228 if (!proc_setup_body()) {
229 yyerror("invalid argument in proc `%s' definition",
234 } '{' optnl block '}' {
240 | block var nl { block_push($2); }
241 | block funcall nl { block_push($2); }
243 | block sfail nl { block_push($2); }
246 sfail : SHOULD_FAIL expr { $$ = op_sfail($2, NULL); }
247 | SHOULD_FAIL expr ':' STRING { $$ = op_sfail($2, $4); }
250 assert : ASSERT asserti
251 | ASSERT '(' optnl massert ')'
254 massert : asserti nl | massert asserti nl ;
256 asserti : check { block_push(op_assert($1)); }
259 test : TESTING STRING DIR STRING {
261 } testopt '{' optnl block '}' {
262 test_done(shouldfail, $2, $4);
267 testopt : /* empty */
268 | SHOULD_FAIL { shouldfail = 1; }
279 yyerror(const char *fmt, ...)
286 if (vasprintf(&msg, fmt, ap) == -1)
287 fatalx("yyerror vasprintf");
289 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
295 kw_cmp(const void *k, const void *e)
297 return strcmp(k, ((const struct keywords *)e)->k_name);
303 /* This has to be sorted always. */
304 static const struct keywords keywords[] = {
308 {"include", INCLUDE},
311 {"should-fail", SHOULD_FAIL},
313 {"testing", TESTING},
318 const struct keywords *p;
320 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
321 sizeof(keywords[0]), kw_cmp);
329 #define START_EXPAND 1
330 #define DONE_EXPAND 2
332 static int expanding;
340 if (file->ungetpos > 0)
341 c = file->ungetbuf[--file->ungetpos];
343 c = getc(file->stream);
345 if (c == START_EXPAND)
347 else if (c == DONE_EXPAND)
361 if ((c = igetc()) == EOF) {
362 yyerror("reached end of file while parsing "
364 if (file == topfile || popfile() == EOF)
371 while ((c = igetc()) == '\\') {
377 yylval.lineno = file->lineno;
383 * Fake EOL when hit EOF for the first time. This gets line
384 * count right if last line in included file is syntactically
385 * invalid and has no newline.
387 if (file->eof_reached == 0) {
388 file->eof_reached = 1;
392 if (file == topfile || popfile() == EOF)
406 if (file->ungetpos >= file->ungetsize) {
407 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
411 file->ungetsize *= 2;
413 file->ungetbuf[file->ungetpos++] = c;
421 /* Skip to either EOF or the first real EOL. */
443 switch (x = my_yylex()) {
444 case ASSERT: puts("assert"); break;
445 case CONST: puts("const"); break;
446 case DIR: puts("dir"); break;
447 case ERROR: puts("error"); break;
448 case INCLUDE: puts("include"); break;
449 case PROC: puts("proc"); break;
450 case REPEAT: puts("repeat"); break;
451 case STR: puts(":str"); break;
452 case TESTING: puts("testing"); break;
453 case U8: puts(":u8"); break;
454 case U16: puts(":u16"); break;
455 case U32: puts(":u32"); break;
457 case STRING: printf("string \"%s\"\n", yylval.v.str); break;
458 case SYMBOL: printf("symbol %s\n", yylval.v.str); break;
459 case NUMBER: printf("number %"PRIu64"\n", yylval.v.num); break;
462 printf("character ");
467 printf(" [0x%x]", x);
482 unsigned char buf[8096];
488 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\f')
491 yylval.lineno = file->lineno;
493 while ((c = lgetc(0)) != '\n' && c != EOF)
504 if ((c = lgetc(quotec)) == EOF)
509 } else if (c == '\\') {
510 if ((next = lgetc(quotec)) == EOF)
512 if (next == quotec || next == ' ' ||
515 else if (next == '\n') {
520 } else if (c == quotec) {
523 } else if (c == '\0') {
524 yyerror("syntax error");
528 if (p + 1 >= buf + sizeof(buf) - 1) {
529 yyerror("string too long");
536 yylval.v.str = xstrdup(buf);
540 #define allowed_to_end_number(x) \
541 (isspace(x) || x == ')' || x == ',' || x == '/' || x == '}' \
542 || x == '=' || x == ':')
544 if (c == '-' || isdigit(c)) {
547 if ((size_t)(p-buf) >= sizeof(buf)) {
548 yyerror("string too long");
551 } while ((c = lgetc(0)) != EOF && isdigit(c));
553 if (p == buf + 1 && buf[0] == '-')
555 if (c == EOF || allowed_to_end_number(c)) {
556 const char *errstr = NULL;
559 yylval.v.num = strtonum(buf, INT64_MIN, INT64_MAX,
562 yyerror("\"%s\" invalid number: %s",
577 #define allowed_in_symbol(x) \
578 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
579 x != '{' && x != '}' && \
580 x != '!' && x != '=' && \
581 x != '#' && x != ',' && \
584 if (isalnum(c) || c == ':' || c == '_') {
587 if ((size_t)(p-buf) >= sizeof(buf)) {
588 yyerror("string too long");
591 } while ((c = lgetc(0)) != EOF && (allowed_in_symbol(c)));
594 if ((token = lookup(buf)) == SYMBOL)
595 yylval.v.str = xstrdup(buf);
600 yylval.lineno = file->lineno;
609 pushfile(const char *name)
613 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
617 if ((nfile->name = strdup(name)) == NULL) {
622 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
623 log_warn("%s", nfile->name);
628 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
629 nfile->ungetsize = 16;
630 nfile->ungetbuf = malloc(nfile->ungetsize);
631 if (nfile->ungetbuf == NULL) {
633 fclose(nfile->stream);
638 TAILQ_INSERT_TAIL(&files, nfile, entry);
647 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
648 prev->errors += file->errors;
650 TAILQ_REMOVE(&files, file, entry);
651 fclose(file->stream);
653 free(file->ungetbuf);
656 return file ? 0 : EOF;
660 loadfile(const char *path)
664 file = pushfile(path);
670 errors = file->errors;
674 errx(1, "can't load %s because of errors", path);