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); } ;
168 expr : literal | funcall | varref | check | cast | faccess | vargs ;
170 vargs : VARGS { $$ = op_vargs(); } ;
172 cast : expr ':' U8 { $$ = op_cast($1, V_U8); }
173 | expr ':' U16 { $$ = op_cast($1, V_U16); }
174 | expr ':' U32 { $$ = op_cast($1, V_U32); }
175 | expr ':' STR { $$ = op_cast($1, V_STR); }
178 faccess : varref '.' SYMBOL { $$ = op_faccess($1, $3); }
179 | faccess '.' SYMBOL { $$ = op_faccess($1, $3); }
183 if (($$ = proc_by_name($1)) == NULL) {
184 yyerror("unknown proc %s", $1);
194 } '(' args optcomma ')' {
199 proc = $$->v.funcall.proc;
200 argc = $$->v.funcall.argc;
202 if (argc != proc->minargs && !proc->vararg) {
203 yyerror("invalid arity for `%s': want %d arguments "
204 "but %d given.", $1->name, proc->minargs, argc);
205 /* TODO: recursively free $$ */
209 if (argc < proc->minargs && proc->vararg) {
210 yyerror("invalid arity for `%s': want at least %d "
211 "arguments but %d given.", $1->name, proc->minargs,
213 /* TODO: recursively free $$ */
219 optcomma: /* empty */ | ',' ;
224 | args ',' expr { push_arg($3); }
225 | args ',' dots { push_arg(op_rest()); }
226 | expr { push_arg($1); }
227 | dots { push_arg(op_rest()); }
233 if (!proc_setup_body()) {
234 yyerror("invalid argument in proc `%s' definition",
239 } '{' optnl block '}' {
245 | block var nl { block_push($2); }
246 | block funcall nl { block_push($2); }
248 | block sfail nl { block_push($2); }
251 sfail : SHOULD_FAIL expr { $$ = op_sfail($2, NULL); }
252 | SHOULD_FAIL expr ':' STRING { $$ = op_sfail($2, $4); }
255 assert : ASSERT asserti
256 | ASSERT '(' optnl massert ')'
259 massert : asserti nl | massert asserti nl ;
261 asserti : check { block_push(op_assert($1)); }
264 test : TESTING STRING DIR STRING {
266 } testopt '{' optnl block '}' {
267 test_done(shouldfail, $2, $4);
272 testopt : /* empty */
273 | SHOULD_FAIL { shouldfail = 1; }
284 yyerror(const char *fmt, ...)
291 if (vasprintf(&msg, fmt, ap) == -1)
292 fatalx("yyerror vasprintf");
294 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
300 kw_cmp(const void *k, const void *e)
302 return strcmp(k, ((const struct keywords *)e)->k_name);
308 /* This has to be sorted always. */
309 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 DIR: puts("dir"); break;
453 case ERROR: puts("error"); break;
454 case INCLUDE: puts("include"); break;
455 case PROC: puts("proc"); break;
456 case REPEAT: puts("repeat"); break;
457 case STR: puts(":str"); break;
458 case TESTING: puts("testing"); break;
459 case U8: puts(":u8"); break;
460 case U16: puts(":u16"); break;
461 case U32: puts(":u32"); break;
463 case STRING: printf("string \"%s\"\n", yylval.v.str); break;
464 case SYMBOL: printf("symbol %s\n", yylval.v.str); break;
465 case NUMBER: printf("number %"PRIu64"\n", yylval.v.num); break;
468 printf("character ");
473 printf(" [0x%x]", x);
488 unsigned char buf[8096];
494 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\f')
497 yylval.lineno = file->lineno;
499 while ((c = lgetc(0)) != '\n' && c != EOF)
510 if ((c = lgetc(quotec)) == EOF)
515 } else if (c == '\\') {
516 if ((next = lgetc(quotec)) == EOF)
518 if (next == quotec || next == ' ' ||
521 else if (next == '\n') {
526 } else if (c == quotec) {
529 } else if (c == '\0') {
530 yyerror("syntax error");
534 if (p + 1 >= buf + sizeof(buf) - 1) {
535 yyerror("string too long");
542 yylval.v.str = xstrdup(buf);
546 #define allowed_to_end_number(x) \
547 (isspace(x) || x == ')' || x == ',' || x == '/' || x == '}' \
548 || x == '=' || x == ':')
550 if (c == '-' || isdigit(c)) {
553 if ((size_t)(p-buf) >= sizeof(buf)) {
554 yyerror("string too long");
557 } while ((c = lgetc(0)) != EOF && (isdigit(c) || c == 'x'));
559 if (p == buf + 1 && buf[0] == '-')
561 if (c == EOF || allowed_to_end_number(c)) {
566 yylval.v.num = strtoll(buf, &ep, 0);
567 if (*ep != '\0' || (errno == ERANGE &&
568 (yylval.v.num == LONG_MAX ||
569 yylval.v.num == LONG_MIN))) {
570 yyerror("\"%s\" invalid number or out of range",
586 #define allowed_in_symbol(x) \
587 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
588 x != '{' && x != '}' && \
589 x != '!' && x != '=' && \
590 x != '#' && x != ',' && \
591 x != '.' && x != ':'))
593 if (isalnum(c) || c == ':' || c == '_') {
596 if ((size_t)(p-buf) >= sizeof(buf)) {
597 yyerror("string too long");
600 } while ((c = lgetc(0)) != EOF && (allowed_in_symbol(c)));
603 if ((token = lookup(buf)) == SYMBOL)
604 yylval.v.str = xstrdup(buf);
609 yylval.lineno = file->lineno;
618 pushfile(const char *name)
622 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
626 if ((nfile->name = strdup(name)) == NULL) {
631 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
632 log_warn("%s", nfile->name);
637 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
638 nfile->ungetsize = 16;
639 nfile->ungetbuf = malloc(nfile->ungetsize);
640 if (nfile->ungetbuf == NULL) {
642 fclose(nfile->stream);
647 TAILQ_INSERT_TAIL(&files, nfile, entry);
656 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
657 prev->errors += file->errors;
659 TAILQ_REMOVE(&files, file, entry);
660 fclose(file->stream);
662 free(file->ungetbuf);
665 return file ? 0 : EOF;
669 loadfile(const char *path)
673 file = pushfile(path);
679 errors = file->errors;
683 errx(1, "can't load %s because of errors", path);