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.
44 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
46 TAILQ_ENTRY(file) entry;
56 struct file *pushfile(const char *);
60 int yyerror(const char *, ...)
61 __attribute__((__format__ (printf, 1, 2)))
62 __attribute__((__nonnull__ (1)));
63 int kw_cmp(const void *, const void *);
70 static int shouldfail;
86 * %define parse.error verbose
96 %token SHOULD_FAIL STR
101 %token <v.str> STRING SYMBOL
102 %token <v.num> NUMBER
104 %type <v.op> cast cexpr check expr faccess funcall
105 %type <v.op> literal sfail var varref vargs
107 %type <v.proc> procname
111 program : /* empty */
113 | program include '\n'
119 optnl : '\n' optnl /* zero or more newlines */
125 include : INCLUDE STRING {
128 if ((nfile = pushfile($2)) == NULL) {
129 yyerror("failed to include file %s", $2);
141 | CONST '(' optnl mconst ')'
144 mconst : consti nl | mconst consti nl ;
146 consti : SYMBOL '=' expr {
147 if (!global_set($1, $3)) {
148 yyerror("can't set %s: illegal expression", $1);
156 var : SYMBOL '=' expr { $$ = op_assign($1, $3); } ;
157 varref : SYMBOL { $$ = op_var($1); } ;
158 literal : STRING { $$ = op_lit_str($1); }
159 | NUMBER { $$ = op_lit_num($1); } ;
162 * `expr '=' '=' expr` is ambiguous. furthermore, we're not
163 * interested in checking all the possibilities here.
165 cexpr : literal | varref | funcall | faccess ;
166 check : cexpr '=' '=' cexpr { $$ = op_cmp_eq($1, $4); }
167 | cexpr '<' '=' cexpr { $$ = op_cmp_leq($1, $4); }
170 expr : literal | funcall | varref | check | cast | faccess | vargs ;
172 vargs : VARGS { $$ = op_vargs(); } ;
174 cast : expr ':' U8 { $$ = op_cast($1, V_U8); }
175 | expr ':' U16 { $$ = op_cast($1, V_U16); }
176 | expr ':' U32 { $$ = op_cast($1, V_U32); }
177 | expr ':' STR { $$ = op_cast($1, V_STR); }
180 faccess : varref '.' SYMBOL { $$ = op_faccess($1, $3); }
181 | faccess '.' SYMBOL { $$ = op_faccess($1, $3); }
185 if (($$ = proc_by_name($1)) == NULL) {
186 yyerror("unknown proc %s", $1);
196 } '(' args optcomma ')' {
201 proc = $$->v.funcall.proc;
202 argc = $$->v.funcall.argc;
204 if (argc != proc->minargs && !proc->vararg) {
205 yyerror("invalid arity for `%s': want %d arguments "
206 "but %d given.", $1->name, proc->minargs, argc);
207 /* TODO: recursively free $$ */
211 if (argc < proc->minargs && proc->vararg) {
212 yyerror("invalid arity for `%s': want at least %d "
213 "arguments but %d given.", $1->name, proc->minargs,
215 /* TODO: recursively free $$ */
221 optcomma: /* empty */ | ',' ;
226 | args ',' expr { push_arg($3); }
227 | args ',' dots { push_arg(op_rest()); }
228 | expr { push_arg($1); }
229 | dots { push_arg(op_rest()); }
235 if (!proc_setup_body()) {
236 yyerror("invalid argument in proc `%s' definition",
241 } '{' optnl block '}' {
247 | block var nl { block_push($2); }
248 | block funcall nl { block_push($2); }
250 | block sfail nl { block_push($2); }
253 sfail : SHOULD_FAIL expr { $$ = op_sfail($2, NULL); }
254 | SHOULD_FAIL expr ':' STRING { $$ = op_sfail($2, $4); }
257 assert : ASSERT asserti
258 | ASSERT '(' optnl massert ')'
261 massert : asserti nl | massert asserti nl ;
263 asserti : check { block_push(op_assert($1)); }
266 test : TESTING STRING DIR STRING {
268 } testopt '{' optnl block '}' {
269 test_done(shouldfail, $2, $4);
274 testopt : /* empty */
275 | SHOULD_FAIL { shouldfail = 1; }
286 yyerror(const char *fmt, ...)
293 if (vasprintf(&msg, fmt, ap) == -1)
294 fatalx("yyerror vasprintf");
296 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
302 kw_cmp(const void *k, const void *e)
304 return strcmp(k, ((const struct keywords *)e)->k_name);
310 /* This has to be sorted always. */
311 static const struct keywords keywords[] = {
315 {"include", INCLUDE},
318 {"should-fail", SHOULD_FAIL},
320 {"testing", TESTING},
326 const struct keywords *p;
328 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
329 sizeof(keywords[0]), kw_cmp);
337 #define START_EXPAND 1
338 #define DONE_EXPAND 2
340 static int expanding;
348 if (file->ungetpos > 0)
349 c = file->ungetbuf[--file->ungetpos];
351 c = getc(file->stream);
353 if (c == START_EXPAND)
355 else if (c == DONE_EXPAND)
369 if ((c = igetc()) == EOF) {
370 yyerror("reached end of file while parsing "
372 if (file == topfile || popfile() == EOF)
379 while ((c = igetc()) == '\\') {
385 yylval.lineno = file->lineno;
391 * Fake EOL when hit EOF for the first time. This gets line
392 * count right if last line in included file is syntactically
393 * invalid and has no newline.
395 if (file->eof_reached == 0) {
396 file->eof_reached = 1;
400 if (file == topfile || popfile() == EOF)
414 if (file->ungetpos >= file->ungetsize) {
415 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
419 file->ungetsize *= 2;
421 file->ungetbuf[file->ungetpos++] = c;
429 /* Skip to either EOF or the first real EOL. */
451 switch (x = my_yylex()) {
452 case ASSERT: puts("assert"); break;
453 case CONST: puts("const"); break;
454 case DIR: puts("dir"); break;
455 case ERROR: puts("error"); break;
456 case INCLUDE: puts("include"); break;
457 case PROC: puts("proc"); break;
458 case REPEAT: puts("repeat"); break;
459 case STR: puts(":str"); break;
460 case TESTING: puts("testing"); break;
461 case U8: puts(":u8"); break;
462 case U16: puts(":u16"); break;
463 case U32: puts(":u32"); break;
465 case STRING: printf("string \"%s\"\n", yylval.v.str); break;
466 case SYMBOL: printf("symbol %s\n", yylval.v.str); break;
467 case NUMBER: printf("number %"PRIu64"\n", yylval.v.num); break;
470 printf("character ");
475 printf(" [0x%x]", x);
490 unsigned char buf[8096];
496 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\f')
499 yylval.lineno = file->lineno;
501 while ((c = lgetc(0)) != '\n' && c != EOF)
512 if ((c = lgetc(quotec)) == EOF)
517 } else if (c == '\\') {
518 if ((next = lgetc(quotec)) == EOF)
520 if (next == quotec || next == ' ' ||
523 else if (next == '\n') {
528 } else if (c == quotec) {
531 } else if (c == '\0') {
532 yyerror("syntax error");
536 if (p + 1 >= buf + sizeof(buf) - 1) {
537 yyerror("string too long");
544 yylval.v.str = xstrdup(buf);
548 #define allowed_to_end_number(x) \
549 (isspace(x) || x == ')' || x == ',' || x == '/' || x == '}' \
550 || x == '=' || x == ':')
552 if (c == '-' || isdigit(c)) {
555 if ((size_t)(p-buf) >= sizeof(buf)) {
556 yyerror("string too long");
559 } while ((c = lgetc(0)) != EOF && (isdigit(c) || c == 'x'));
561 if (p == buf + 1 && buf[0] == '-')
563 if (c == EOF || allowed_to_end_number(c)) {
568 yylval.v.num = strtoll(buf, &ep, 0);
569 if (*ep != '\0' || (errno == ERANGE &&
570 (yylval.v.num == LONG_MAX ||
571 yylval.v.num == LONG_MIN))) {
572 yyerror("\"%s\" invalid number or out of range",
588 #define allowed_in_symbol(x) \
589 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
590 x != '{' && x != '}' && \
591 x != '!' && x != '=' && \
592 x != '#' && x != ',' && \
593 x != '.' && x != ':'))
595 if (isalnum(c) || c == ':' || c == '_') {
598 if ((size_t)(p-buf) >= sizeof(buf)) {
599 yyerror("string too long");
602 } while ((c = lgetc(0)) != EOF && (allowed_in_symbol(c)));
605 if ((token = lookup(buf)) == SYMBOL)
606 yylval.v.str = xstrdup(buf);
611 yylval.lineno = file->lineno;
620 pushfile(const char *name)
624 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
628 if ((nfile->name = strdup(name)) == NULL) {
633 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
634 log_warn("%s", nfile->name);
639 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
640 nfile->ungetsize = 16;
641 nfile->ungetbuf = malloc(nfile->ungetsize);
642 if (nfile->ungetbuf == NULL) {
644 fclose(nfile->stream);
649 TAILQ_INSERT_TAIL(&files, nfile, entry);
658 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
659 prev->errors += file->errors;
661 TAILQ_REMOVE(&files, file, entry);
662 fclose(file->stream);
664 free(file->ungetbuf);
667 return file ? 0 : EOF;
671 loadfile(const char *path)
675 file = pushfile(path);
681 errors = file->errors;
685 errx(1, "can't load %s because of errors", path);