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.
47 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
49 TAILQ_ENTRY(file) entry;
59 struct file *pushfile(const char *);
63 int yyerror(const char *, ...)
64 __attribute__((__format__ (printf, 1, 2)))
65 __attribute__((__nonnull__ (1)));
66 int kw_cmp(const void *, const void *);
73 static int shouldfail;
89 * %define parse.error verbose
98 %token SHOULD_FAIL STR
103 %token <v.str> STRING SYMBOL
104 %token <v.num> NUMBER
106 %type <v.op> cast cexpr check expr faccess funcall
107 %type <v.op> literal sfail var varref vargs
109 %type <v.proc> procname
113 program : /* empty */
115 | program include '\n'
121 optnl : '\n' optnl /* zero or more newlines */
127 include : INCLUDE STRING {
130 if ((nfile = pushfile($2)) == NULL) {
131 yyerror("failed to include file %s", $2);
143 | CONST '(' optnl mconst ')'
146 mconst : consti nl | mconst consti nl ;
148 consti : SYMBOL '=' expr {
149 if (!global_set($1, $3)) {
150 yyerror("can't set %s: illegal expression", $1);
158 var : SYMBOL '=' expr { $$ = op_assign($1, $3); } ;
159 varref : SYMBOL { $$ = op_var($1); } ;
160 literal : STRING { $$ = op_lit_str($1); }
161 | NUMBER { $$ = op_lit_num($1); } ;
164 * `expr '=' '=' expr` is ambiguous. furthermore, we're not
165 * interested in checking all the possibilities here.
167 cexpr : literal | varref | funcall | faccess ;
168 check : cexpr '=' '=' cexpr { $$ = op_cmp_eq($1, $4); }
169 | cexpr '<' '=' cexpr { $$ = op_cmp_leq($1, $4); }
172 expr : literal | funcall | varref | check | cast | faccess | vargs ;
174 vargs : VARGS { $$ = op_vargs(); } ;
176 cast : expr ':' U8 { $$ = op_cast($1, V_U8); }
177 | expr ':' U16 { $$ = op_cast($1, V_U16); }
178 | expr ':' U32 { $$ = op_cast($1, V_U32); }
179 | expr ':' STR { $$ = op_cast($1, V_STR); }
182 faccess : varref '.' SYMBOL { $$ = op_faccess($1, $3); }
183 | faccess '.' SYMBOL { $$ = op_faccess($1, $3); }
187 if (($$ = proc_by_name($1)) == NULL) {
188 yyerror("unknown proc %s", $1);
198 } '(' args optcomma ')' {
203 proc = $$->v.funcall.proc;
204 argc = $$->v.funcall.argc;
206 if (argc != proc->minargs && !proc->vararg) {
207 yyerror("invalid arity for `%s': want %d arguments "
208 "but %d given.", $1->name, proc->minargs, argc);
209 /* TODO: recursively free $$ */
213 if (argc < proc->minargs && proc->vararg) {
214 yyerror("invalid arity for `%s': want at least %d "
215 "arguments but %d given.", $1->name, proc->minargs,
217 /* TODO: recursively free $$ */
223 optcomma: /* empty */ | ',' ;
228 | args ',' expr { push_arg($3); }
229 | args ',' dots { push_arg(op_rest()); }
230 | expr { push_arg($1); }
231 | dots { push_arg(op_rest()); }
237 if (!proc_setup_body()) {
238 yyerror("invalid argument in proc `%s' definition",
243 } '{' optnl block '}' {
249 | block var nl { block_push($2); }
250 | block funcall nl { block_push($2); }
252 | block sfail nl { block_push($2); }
255 sfail : SHOULD_FAIL expr { $$ = op_sfail($2, NULL); }
256 | SHOULD_FAIL expr ':' STRING { $$ = op_sfail($2, $4); }
259 assert : ASSERT asserti
260 | ASSERT '(' optnl massert ')'
263 massert : asserti nl | massert asserti nl ;
265 asserti : check { block_push(op_assert($1)); }
268 test : TESTING STRING {
270 } testopt '{' optnl block '}' {
271 test_done(shouldfail, $2);
276 testopt : /* empty */
277 | SHOULD_FAIL { shouldfail = 1; }
288 yyerror(const char *fmt, ...)
295 if (vasprintf(&msg, fmt, ap) == -1)
296 fatalx("yyerror vasprintf");
298 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
304 kw_cmp(const void *k, const void *e)
306 return strcmp(k, ((const struct keywords *)e)->k_name);
312 /* This has to be sorted always. */
313 static const struct keywords keywords[] = {
316 {"include", INCLUDE},
319 {"should-fail", SHOULD_FAIL},
321 {"testing", TESTING},
327 const struct keywords *p;
329 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
330 sizeof(keywords[0]), kw_cmp);
338 #define START_EXPAND 1
339 #define DONE_EXPAND 2
341 static int expanding;
349 if (file->ungetpos > 0)
350 c = file->ungetbuf[--file->ungetpos];
352 c = getc(file->stream);
354 if (c == START_EXPAND)
356 else if (c == DONE_EXPAND)
370 if ((c = igetc()) == EOF) {
371 yyerror("reached end of file while parsing "
373 if (file == topfile || popfile() == EOF)
380 while ((c = igetc()) == '\\') {
386 yylval.lineno = file->lineno;
392 * Fake EOL when hit EOF for the first time. This gets line
393 * count right if last line in included file is syntactically
394 * invalid and has no newline.
396 if (file->eof_reached == 0) {
397 file->eof_reached = 1;
401 if (file == topfile || popfile() == EOF)
415 if (file->ungetpos >= file->ungetsize) {
416 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
420 file->ungetsize *= 2;
422 file->ungetbuf[file->ungetpos++] = c;
430 /* Skip to either EOF or the first real EOL. */
452 switch (x = my_yylex()) {
453 case ASSERT: puts("assert"); break;
454 case CONST: puts("const"); 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)
674 char p[PATH_MAX], *dir;
677 * Ugly workaround: we really need a smarter `include', one that
678 * is able to resolve path relatively from the currently processed
679 * file. The workaround consist to save the current directory,
680 * chdir(2) to the script dirname and then jump back by mean of
684 if ((pwdfd = open(".", O_RDONLY|O_DIRECTORY)) == -1)
685 err(1, "can't open .");
686 strlcpy(p, path, sizeof(p));
688 if (chdir(dir) == -1)
689 err(1, "chdir %s", dir);
691 /* XXX: include the *basename* of the file after chdir */
692 strlcpy(p, path, sizeof(p));
693 file = pushfile(basename(p));
699 errors = file->errors;
706 errx(1, "can't load %s because of errors", path);