Blob


1 /*
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.
10 *
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.
14 *
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.
22 */
24 %{
26 #include "compat.h"
28 #include <ctype.h>
29 #include <inttypes.h>
30 #include <limits.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <syslog.h>
36 #include "log.h"
37 #include "kamid.h"
38 #include "utils.h"
40 #include "script.h"
42 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
43 static struct file {
44 TAILQ_ENTRY(file) entry;
45 FILE *stream;
46 char *name;
47 size_t ungetpos;
48 size_t ungetsize;
49 u_char *ungetbuf;
50 int eof_reached;
51 int lineno;
52 int errors;
53 } *file, *topfile;
54 struct file *pushfile(const char *);
55 int popfile(void);
56 int yyparse(void);
57 int yylex(void);
58 int yyerror(const char *, ...)
59 __attribute__((__format__ (printf, 1, 2)))
60 __attribute__((__nonnull__ (1)));
61 int kw_cmp(const void *, const void *);
62 int lookup(char *);
63 int igetc(void);
64 int lgetc(int);
65 void lungetc(int);
66 int findeol(void);
68 typedef struct {
69 union {
70 struct op *op;
71 struct proc *proc;
72 char *str;
73 int64_t num;
74 } v;
75 int lineno;
76 } YYSTYPE;
78 %}
80 /*
81 * for bison:
82 * %define parse.error verbose
83 */
85 %token ASSERT
86 %token CONST
87 %token DIR
88 %token ERROR
89 %token INCLUDE
90 %token PROC
91 %token REPEAT
92 %token STR
93 %token TESTING
94 %token U8 U16 U32
96 %token <v.str> STRING SYMBOL
97 %token <v.num> NUMBER
99 %type <v.op> cast cexpr check expr funcall
100 %type <v.op> literal var varref
102 %type <v.proc> procname
104 %%
106 program : /* empty */
107 | program '\n'
108 | program include '\n'
109 | program const '\n'
110 | program proc '\n'
111 | program test '\n'
114 optnl : '\n' optnl /* zero or more newlines */
115 | /*empty*/
118 nl : '\n' optnl /* one or more newlines */
121 include : INCLUDE STRING {
122 struct file *nfile;
124 if ((nfile = pushfile($2)) == NULL) {
125 yyerror("failed to include file %s", $2);
126 free($2);
127 YYERROR;
129 free($2);
131 file = nfile;
132 lungetc('\n');
136 const : CONST SYMBOL '=' literal { global_set($2, $4); };
138 var : SYMBOL '=' expr { $$ = op_assign($1, $3); } ;
139 varref : SYMBOL { $$ = op_var($1); } ;
140 literal : STRING { $$ = op_lit_str($1); }
141 | NUMBER { $$ = op_lit_num($1); } ;
143 /*
144 * `expr '=' '=' expr` is ambiguous. furthermore, we're not
145 * interested in checking all the possibilities here.
146 */
147 cexpr : literal | varref | funcall ;
148 check : cexpr '=' '=' cexpr { $$ = op_cmp_eq($1, $4); } ;
150 expr : literal | funcall | varref | check | cast ;
152 cast : expr ':' U8 { $$ = op_cast($1, V_U8); }
153 | expr ':' U16 { $$ = op_cast($1, V_U16); }
154 | expr ':' U32 { $$ = op_cast($1, V_U32); }
155 | expr ':' STR { $$ = op_cast($1, V_STR); }
158 procname: SYMBOL {
159 if (($$ = proc_by_name($1)) == NULL) {
160 yyerror("unknown proc %s", $1);
161 free($1);
162 YYERROR;
164 free($1);
168 funcall : procname {
169 prepare_funcall();
170 } '(' args optcomma ')' {
171 $$ = op_funcall($1);
175 optcomma: /* empty */ | ',' ;
177 args : /* empty */
178 | args ',' expr { push_arg($3); }
179 | expr { push_arg($1); }
182 proc : PROC SYMBOL {
183 prepare_proc();
184 } '(' args ')' {
185 if (!proc_setup_body()) {
186 yyerror("invalid argument in proc `%s' definition",
187 $2);
188 free($2);
189 YYERROR;
191 } '{' optnl block '}' {
192 proc_done($2);
196 block : /* empty */
197 | block var '\n' { block_push($2); }
198 | block funcall '\n' { block_push($2); }
199 | block assert '\n'
202 assert : ASSERT check { block_push(op_assert($2)); }
203 /* | ASSERT '(' optnl massert ')' */
206 massert : /* empty */
207 | massert ',' optnl check { block_push(op_assert($4)); }
208 | check { block_push(op_assert($1)); }
211 test : TESTING STRING DIR STRING {
212 prepare_test();
213 } '{' optnl block '}' {
214 test_done($2, $4);
218 %%
220 struct keywords {
221 const char *k_name;
222 int k_val;
223 };
225 int
226 yyerror(const char *fmt, ...)
228 va_list ap;
229 char *msg;
231 file->errors++;
232 va_start(ap, fmt);
233 if (vasprintf(&msg, fmt, ap) == -1)
234 fatalx("yyerror vasprintf");
235 va_end(ap);
236 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
237 free(msg);
238 return 0;
241 int
242 kw_cmp(const void *k, const void *e)
244 return strcmp(k, ((const struct keywords *)e)->k_name);
247 int
248 lookup(char *s)
250 /* This has to be sorted always. */
251 static const struct keywords keywords[] = {
252 {"assert", ASSERT},
253 {"const", CONST},
254 {"dir", DIR},
255 {"include", INCLUDE},
256 {"proc", PROC},
257 {"repeat", REPEAT},
258 {"str", STR},
259 {"testing", TESTING},
260 {"u8", U8},
261 {"u16", U16},
262 {"u32", U32},
263 };
264 const struct keywords *p;
266 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
267 sizeof(keywords[0]), kw_cmp);
269 if (p)
270 return p->k_val;
271 else
272 return SYMBOL;
275 #define START_EXPAND 1
276 #define DONE_EXPAND 2
278 static int expanding;
280 int
281 igetc(void)
283 int c;
285 while (1) {
286 if (file->ungetpos > 0)
287 c = file->ungetbuf[--file->ungetpos];
288 else
289 c = getc(file->stream);
291 if (c == START_EXPAND)
292 expanding = 1;
293 else if (c == DONE_EXPAND)
294 expanding = 0;
295 else
296 break;
298 return c;
301 int
302 lgetc(int quotec)
304 int c, next;
306 if (quotec) {
307 if ((c = igetc()) == EOF) {
308 yyerror("reached end of file while parsing "
309 "quoted string");
310 if (file == topfile || popfile() == EOF)
311 return EOF;
312 return quotec;
314 return c;
317 while ((c = igetc()) == '\\') {
318 next = igetc();
319 if (next != '\n') {
320 c = next;
321 break;
323 yylval.lineno = file->lineno;
324 file->lineno++;
327 if (c == EOF) {
328 /*
329 * Fake EOL when hit EOF for the first time. This gets line
330 * count right if last line in included file is syntactically
331 * invalid and has no newline.
332 */
333 if (file->eof_reached == 0) {
334 file->eof_reached = 1;
335 return '\n';
337 while (c == EOF) {
338 if (file == topfile || popfile() == EOF)
339 return EOF;
340 c = igetc();
343 return c;
346 void
347 lungetc(int c)
349 if (c == EOF)
350 return;
352 if (file->ungetpos >= file->ungetsize) {
353 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
354 if (p == NULL)
355 err(1, "lungetc");
356 file->ungetbuf = p;
357 file->ungetsize *= 2;
359 file->ungetbuf[file->ungetpos++] = c;
362 int
363 findeol(void)
365 int c;
367 /* Skip to either EOF or the first real EOL. */
368 while (1) {
369 c = lgetc(0);
370 if (c == '\n') {
371 file->lineno++;
372 break;
374 if (c == EOF)
375 break;
377 return ERROR;
381 #if 0
382 int my_yylex(void);
384 int
385 yylex(void)
387 int x;
389 switch (x = my_yylex()) {
390 case ASSERT: puts("assert"); break;
391 case CONST: puts("const"); break;
392 case DIR: puts("dir"); break;
393 case ERROR: puts("error"); break;
394 case INCLUDE: puts("include"); break;
395 case PROC: puts("proc"); break;
396 case REPEAT: puts("repeat"); break;
397 case STR: puts(":str"); break;
398 case TESTING: puts("testing"); break;
399 case U8: puts(":u8"); break;
400 case U16: puts(":u16"); break;
401 case U32: puts(":u32"); break;
403 case STRING: printf("string \"%s\"\n", yylval.v.str); break;
404 case SYMBOL: printf("symbol %s\n", yylval.v.str); break;
405 case NUMBER: printf("number %"PRIu64"\n", yylval.v.num); break;
407 default:
408 printf("character ");
409 if (x == '\n')
410 printf("\\n");
411 else
412 printf("%c", x);
413 printf(" [0x%x]", x);
414 printf("\n");
415 break;
418 return x;
421 int
422 my_yylex(void)
423 #else
424 int
425 yylex(void)
426 #endif
428 unsigned char buf[8096];
429 unsigned char *p;
430 int quotec, next, c;
431 int token;
433 p = buf;
434 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\f')
435 ; /* nop */
437 yylval.lineno = file->lineno;
438 if (c == '#')
439 while ((c = lgetc(0)) != '\n' && c != EOF)
440 ; /* nop */
442 switch (c) {
443 case '\'':
444 case '\"':
445 quotec = c;
446 while (1) {
447 if ((c = lgetc(quotec)) == EOF)
448 return 0;
449 if (c == '\n') {
450 file->lineno++;
451 continue;
452 } else if (c == '\\') {
453 if ((next = lgetc(quotec)) == EOF)
454 return 0;
455 if (next == quotec || next == ' ' ||
456 next == '\t')
457 c = next;
458 else if (next == '\n') {
459 file->lineno++;
460 continue;
461 } else
462 lungetc(next);
463 } else if (c == quotec) {
464 *p = '\0';
465 break;
466 } else if (c == '\0') {
467 yyerror("syntax error");
468 return findeol();
471 if (p + 1 >= buf + sizeof(buf) - 1) {
472 yyerror("string too long");
473 return findeol();
476 *p++ = c;
479 yylval.v.str = xstrdup(buf);
480 return STRING;
483 #define allowed_to_end_number(x) \
484 (isspace(x) || x == ')' || x == ',' || x == '/' || x == '}' \
485 || x == '=')
487 if (c == '-' || isdigit(c)) {
488 do {
489 *p++ = c;
490 if ((size_t)(p-buf) >= sizeof(buf)) {
491 yyerror("string too long");
492 return findeol();
494 } while ((c = lgetc(0)) != EOF && isdigit(c));
495 lungetc(c);
496 if (p == buf + 1 && buf[0] == '-')
497 goto nodigits;
498 if (c == EOF || allowed_to_end_number(c)) {
499 const char *errstr = NULL;
501 *p = '\0';
502 yylval.v.num = strtonum(buf, INT64_MIN, INT64_MAX,
503 &errstr);
504 if (errstr) {
505 yyerror("\"%s\" invalid number: %s",
506 buf, errstr);
507 return findeol();
509 return NUMBER;
510 } else {
511 nodigits:
512 while (p > buf + 1)
513 lungetc(*--p);
514 c = *--p;
515 if (c == '-')
516 return c;
520 #define allowed_in_symbol(x) \
521 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
522 x != '{' && x != '}' && \
523 x != '!' && x != '=' && \
524 x != '#' && x != ','))
526 if (isalnum(c) || c == ':' || c == '_') {
527 do {
528 *p++ = c;
529 if ((size_t)(p-buf) >= sizeof(buf)) {
530 yyerror("string too long");
531 return findeol();
533 } while ((c = lgetc(0)) != EOF && (allowed_in_symbol(c)));
534 lungetc(c);
535 *p = '\0';
536 if ((token = lookup(buf)) == SYMBOL)
537 yylval.v.str = xstrdup(buf);
538 return token;
541 if (c == '\n') {
542 yylval.lineno = file->lineno;
543 file->lineno++;
545 if (c == EOF)
546 return 0;
547 return c;
550 struct file *
551 pushfile(const char *name)
553 struct file *nfile;
555 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
556 log_warn("calloc");
557 return NULL;
559 if ((nfile->name = strdup(name)) == NULL) {
560 log_warn("strdup");
561 free(nfile);
562 return NULL;
564 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
565 log_warn("%s", nfile->name);
566 free(nfile->name);
567 free(nfile);
568 return NULL;
570 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
571 nfile->ungetsize = 16;
572 nfile->ungetbuf = malloc(nfile->ungetsize);
573 if (nfile->ungetbuf == NULL) {
574 log_warn("malloc");
575 fclose(nfile->stream);
576 free(nfile->name);
577 free(nfile);
578 return NULL;
580 TAILQ_INSERT_TAIL(&files, nfile, entry);
581 return nfile;
584 int
585 popfile(void)
587 struct file *prev;
589 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
590 prev->errors += file->errors;
592 TAILQ_REMOVE(&files, file, entry);
593 fclose(file->stream);
594 free(file->name);
595 free(file->ungetbuf);
596 free(file);
597 file = prev;
598 return file ? 0 : EOF;
601 void
602 loadfile(const char *path)
604 int errors;
606 file = pushfile(path);
607 if (file == NULL)
608 err(1, "pushfile");
609 topfile = file;
611 yyparse();
612 errors = file->errors;
613 popfile();
615 if (errors)
616 errx(1, "can't load %s because of errors", path);