2 * Copyright (c) 2021, 2022 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
95 %token SHOULD_FAIL STR
100 %token <v.str> STRING SYMBOL
101 %token <v.num> NUMBER
103 %type <v.op> cast cexpr check expr faccess funcall
104 %type <v.op> literal sfail var varref vargs
106 %type <v.proc> procname
110 program : /* empty */
112 | program include '\n'
118 optnl : '\n' optnl /* zero or more newlines */
124 include : INCLUDE STRING {
127 if ((nfile = pushfile($2)) == NULL) {
128 yyerror("failed to include file %s", $2);
140 | CONST '(' optnl mconst ')'
143 mconst : consti nl | mconst consti nl ;
145 consti : SYMBOL '=' expr {
146 if (!global_set($1, $3)) {
147 yyerror("can't set %s: illegal expression", $1);
155 var : SYMBOL '=' expr { $$ = op_assign($1, $3); } ;
156 varref : SYMBOL { $$ = op_var($1); } ;
157 literal : STRING { $$ = op_lit_str($1); }
158 | NUMBER { $$ = op_lit_num($1); } ;
161 * `expr '=' '=' expr` is ambiguous. furthermore, we're not
162 * interested in checking all the possibilities here.
164 cexpr : literal | varref | funcall | faccess ;
165 check : cexpr '=' '=' cexpr { $$ = op_cmp_eq($1, $4); }
166 | cexpr '<' '=' cexpr { $$ = op_cmp_leq($1, $4); }
169 expr : literal | funcall | varref | check | cast | faccess | vargs ;
171 vargs : VARGS { $$ = op_vargs(); } ;
173 cast : expr ':' U8 { $$ = op_cast($1, V_U8); }
174 | expr ':' U16 { $$ = op_cast($1, V_U16); }
175 | expr ':' U32 { $$ = op_cast($1, V_U32); }
176 | expr ':' STR { $$ = op_cast($1, V_STR); }
179 faccess : varref '.' SYMBOL { $$ = op_faccess($1, $3); }
180 | faccess '.' SYMBOL { $$ = op_faccess($1, $3); }
184 if (($$ = proc_by_name($1)) == NULL) {
185 yyerror("unknown proc %s", $1);
195 } '(' args optcomma ')' {
200 proc = $$->v.funcall.proc;
201 argc = $$->v.funcall.argc;
203 if (argc != proc->minargs && !proc->vararg) {
204 yyerror("invalid arity for `%s': want %d arguments "
205 "but %d given.", $1->name, proc->minargs, argc);
206 /* TODO: recursively free $$ */
210 if (argc < proc->minargs && proc->vararg) {
211 yyerror("invalid arity for `%s': want at least %d "
212 "arguments but %d given.", $1->name, proc->minargs,
214 /* TODO: recursively free $$ */
220 optcomma: /* empty */ | ',' ;
225 | args ',' expr { push_arg($3); }
226 | args ',' dots { push_arg(op_rest()); }
227 | expr { push_arg($1); }
228 | dots { push_arg(op_rest()); }
234 if (!proc_setup_body()) {
235 yyerror("invalid argument in proc `%s' definition",
240 } '{' optnl block '}' {
246 | block var nl { block_push($2); }
247 | block funcall nl { block_push($2); }
249 | block sfail nl { block_push($2); }
252 sfail : SHOULD_FAIL expr { $$ = op_sfail($2, NULL); }
253 | SHOULD_FAIL expr ':' STRING { $$ = op_sfail($2, $4); }
256 assert : ASSERT asserti
257 | ASSERT '(' optnl massert ')'
260 massert : asserti nl | massert asserti nl ;
262 asserti : check { block_push(op_assert($1)); }
265 test : TESTING STRING {
267 } testopt '{' optnl block '}' {
268 test_done(shouldfail, $2);
273 testopt : /* empty */
274 | SHOULD_FAIL { shouldfail = 1; }
285 yyerror(const char *fmt, ...)
292 if (vasprintf(&msg, fmt, ap) == -1)
293 fatalx("yyerror vasprintf");
295 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
301 kw_cmp(const void *k, const void *e)
303 return strcmp(k, ((const struct keywords *)e)->k_name);
309 /* This has to be sorted always. */
310 static const struct keywords keywords[] = {
313 {"include", INCLUDE},
316 {"should-fail", SHOULD_FAIL},
318 {"testing", TESTING},
324 const struct keywords *p;
326 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
327 sizeof(keywords[0]), kw_cmp);
335 #define START_EXPAND 1
336 #define DONE_EXPAND 2
338 static int expanding;
346 if (file->ungetpos > 0)
347 c = file->ungetbuf[--file->ungetpos];
349 c = getc(file->stream);
351 if (c == START_EXPAND)
353 else if (c == DONE_EXPAND)
367 if ((c = igetc()) == EOF) {
368 yyerror("reached end of file while parsing "
370 if (file == topfile || popfile() == EOF)
377 while ((c = igetc()) == '\\') {
383 yylval.lineno = file->lineno;
389 * Fake EOL when hit EOF for the first time. This gets line
390 * count right if last line in included file is syntactically
391 * invalid and has no newline.
393 if (file->eof_reached == 0) {
394 file->eof_reached = 1;
398 if (file == topfile || popfile() == EOF)
412 if (file->ungetpos >= file->ungetsize) {
413 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
417 file->ungetsize *= 2;
419 file->ungetbuf[file->ungetpos++] = c;
427 /* Skip to either EOF or the first real EOL. */
449 switch (x = my_yylex()) {
450 case ASSERT: puts("assert"); break;
451 case CONST: puts("const"); break;
452 case ERROR: puts("error"); break;
453 case INCLUDE: puts("include"); break;
454 case PROC: puts("proc"); break;
455 case REPEAT: puts("repeat"); break;
456 case STR: puts(":str"); break;
457 case TESTING: puts("testing"); break;
458 case U8: puts(":u8"); break;
459 case U16: puts(":u16"); break;
460 case U32: puts(":u32"); break;
462 case STRING: printf("string \"%s\"\n", yylval.v.str); break;
463 case SYMBOL: printf("symbol %s\n", yylval.v.str); break;
464 case NUMBER: printf("number %"PRIu64"\n", yylval.v.num); break;
467 printf("character ");
472 printf(" [0x%x]", x);
487 unsigned char buf[8096];
493 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\f')
496 yylval.lineno = file->lineno;
498 while ((c = lgetc(0)) != '\n' && c != EOF)
509 if ((c = lgetc(quotec)) == EOF)
514 } else if (c == '\\') {
515 if ((next = lgetc(quotec)) == EOF)
517 if (next == quotec || next == ' ' ||
520 else if (next == '\n') {
525 } else if (c == quotec) {
528 } else if (c == '\0') {
529 yyerror("syntax error");
533 if (p + 1 >= buf + sizeof(buf) - 1) {
534 yyerror("string too long");
541 yylval.v.str = xstrdup(buf);
545 #define allowed_to_end_number(x) \
546 (isspace(x) || x == ')' || x == ',' || x == '/' || x == '}' \
547 || x == '=' || x == ':')
549 if (c == '-' || isdigit(c)) {
552 if ((size_t)(p-buf) >= sizeof(buf)) {
553 yyerror("string too long");
556 } while ((c = lgetc(0)) != EOF && (isdigit(c) || c == 'x'));
558 if (p == buf + 1 && buf[0] == '-')
560 if (c == EOF || allowed_to_end_number(c)) {
565 yylval.v.num = strtoll(buf, &ep, 0);
566 if (*ep != '\0' || (errno == ERANGE &&
567 (yylval.v.num == LONG_MAX ||
568 yylval.v.num == LONG_MIN))) {
569 yyerror("\"%s\" invalid number or out of range",
585 #define allowed_in_symbol(x) \
586 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
587 x != '{' && x != '}' && \
588 x != '!' && x != '=' && \
589 x != '#' && x != ',' && \
590 x != '.' && x != ':'))
592 if (isalnum(c) || c == ':' || c == '_') {
595 if ((size_t)(p-buf) >= sizeof(buf)) {
596 yyerror("string too long");
599 } while ((c = lgetc(0)) != EOF && (allowed_in_symbol(c)));
602 if ((token = lookup(buf)) == SYMBOL)
603 yylval.v.str = xstrdup(buf);
608 yylval.lineno = file->lineno;
617 pushfile(const char *name)
621 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
625 if ((nfile->name = strdup(name)) == NULL) {
630 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
631 log_warn("%s", nfile->name);
636 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
637 nfile->ungetsize = 16;
638 nfile->ungetbuf = malloc(nfile->ungetsize);
639 if (nfile->ungetbuf == NULL) {
641 fclose(nfile->stream);
646 TAILQ_INSERT_TAIL(&files, nfile, entry);
655 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
656 prev->errors += file->errors;
658 TAILQ_REMOVE(&files, file, entry);
659 fclose(file->stream);
661 free(file->ungetbuf);
664 return file ? 0 : EOF;
668 loadfile(const char *path)
672 file = pushfile(path);
678 errors = file->errors;
682 errx(1, "can't load %s because of errors", path);