2 c734c0e9 2021-08-03 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 c734c0e9 2021-08-03 op * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
4 c734c0e9 2021-08-03 op * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 c734c0e9 2021-08-03 op * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 c734c0e9 2021-08-03 op * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 c734c0e9 2021-08-03 op * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 c734c0e9 2021-08-03 op * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 c734c0e9 2021-08-03 op * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 c734c0e9 2021-08-03 op * Permission to use, copy, modify, and distribute this software for any
12 c734c0e9 2021-08-03 op * purpose with or without fee is hereby granted, provided that the above
13 c734c0e9 2021-08-03 op * copyright notice and this permission notice appear in all copies.
15 c734c0e9 2021-08-03 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 c734c0e9 2021-08-03 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 c734c0e9 2021-08-03 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 c734c0e9 2021-08-03 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 c734c0e9 2021-08-03 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 c734c0e9 2021-08-03 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 c734c0e9 2021-08-03 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 c734c0e9 2021-08-03 op #include "compat.h"
28 c734c0e9 2021-08-03 op #include <ctype.h>
29 ef33a69e 2021-08-07 op #include <errno.h>
30 c734c0e9 2021-08-03 op #include <inttypes.h>
31 ef33a69e 2021-08-07 op #include <limits.h>
32 c734c0e9 2021-08-03 op #include <limits.h>
33 c734c0e9 2021-08-03 op #include <stdio.h>
34 c734c0e9 2021-08-03 op #include <stdlib.h>
35 c734c0e9 2021-08-03 op #include <string.h>
36 c734c0e9 2021-08-03 op #include <syslog.h>
38 c734c0e9 2021-08-03 op #include "log.h"
39 c734c0e9 2021-08-03 op #include "kamid.h"
40 c734c0e9 2021-08-03 op #include "utils.h"
42 c734c0e9 2021-08-03 op #include "script.h"
44 c734c0e9 2021-08-03 op TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
45 c734c0e9 2021-08-03 op static struct file {
46 c734c0e9 2021-08-03 op TAILQ_ENTRY(file) entry;
49 c734c0e9 2021-08-03 op size_t ungetpos;
50 c734c0e9 2021-08-03 op size_t ungetsize;
51 c734c0e9 2021-08-03 op u_char *ungetbuf;
52 c734c0e9 2021-08-03 op int eof_reached;
55 c734c0e9 2021-08-03 op } *file, *topfile;
56 c734c0e9 2021-08-03 op struct file *pushfile(const char *);
57 c734c0e9 2021-08-03 op int popfile(void);
58 c734c0e9 2021-08-03 op int yyparse(void);
59 c734c0e9 2021-08-03 op int yylex(void);
60 c734c0e9 2021-08-03 op int yyerror(const char *, ...)
61 c734c0e9 2021-08-03 op __attribute__((__format__ (printf, 1, 2)))
62 c734c0e9 2021-08-03 op __attribute__((__nonnull__ (1)));
63 c734c0e9 2021-08-03 op int kw_cmp(const void *, const void *);
64 c734c0e9 2021-08-03 op int lookup(char *);
65 c734c0e9 2021-08-03 op int igetc(void);
66 c734c0e9 2021-08-03 op int lgetc(int);
67 c734c0e9 2021-08-03 op void lungetc(int);
68 c734c0e9 2021-08-03 op int findeol(void);
70 0b453b63 2021-08-07 op static int shouldfail;
72 c734c0e9 2021-08-03 op typedef struct {
74 c734c0e9 2021-08-03 op struct op *op;
75 c734c0e9 2021-08-03 op struct proc *proc;
86 c734c0e9 2021-08-03 op * %define parse.error verbose
93 c734c0e9 2021-08-03 op %token INCLUDE
96 0b453b63 2021-08-07 op %token SHOULD_FAIL STR
97 c734c0e9 2021-08-03 op %token TESTING
98 c734c0e9 2021-08-03 op %token U8 U16 U32
101 c734c0e9 2021-08-03 op %token <v.str> STRING SYMBOL
102 c734c0e9 2021-08-03 op %token <v.num> NUMBER
104 acf1403a 2021-08-05 op %type <v.op> cast cexpr check expr faccess funcall
105 340ee97d 2021-08-08 op %type <v.op> literal sfail var varref vargs
107 c734c0e9 2021-08-03 op %type <v.proc> procname
111 c734c0e9 2021-08-03 op program : /* empty */
112 c734c0e9 2021-08-03 op | program '\n'
113 c734c0e9 2021-08-03 op | program include '\n'
114 c734c0e9 2021-08-03 op | program const '\n'
115 c734c0e9 2021-08-03 op | program proc '\n'
116 c734c0e9 2021-08-03 op | program test '\n'
119 c734c0e9 2021-08-03 op optnl : '\n' optnl /* zero or more newlines */
123 c01463a3 2021-08-04 op nl : '\n' optnl ;
125 c734c0e9 2021-08-03 op include : INCLUDE STRING {
126 c734c0e9 2021-08-03 op struct file *nfile;
128 c734c0e9 2021-08-03 op if ((nfile = pushfile($2)) == NULL) {
129 c734c0e9 2021-08-03 op yyerror("failed to include file %s", $2);
135 c734c0e9 2021-08-03 op file = nfile;
136 c734c0e9 2021-08-03 op lungetc('\n');
140 c01463a3 2021-08-04 op const : CONST consti
141 e0b4ca44 2021-08-04 op | CONST '(' optnl mconst ')'
144 c01463a3 2021-08-04 op mconst : consti nl | mconst consti nl ;
146 c01463a3 2021-08-04 op consti : SYMBOL '=' expr {
147 c01463a3 2021-08-04 op if (!global_set($1, $3)) {
148 a3097d05 2021-08-04 op yyerror("can't set %s: illegal expression", $1);
156 c734c0e9 2021-08-03 op var : SYMBOL '=' expr { $$ = op_assign($1, $3); } ;
157 c734c0e9 2021-08-03 op varref : SYMBOL { $$ = op_var($1); } ;
158 c734c0e9 2021-08-03 op literal : STRING { $$ = op_lit_str($1); }
159 c734c0e9 2021-08-03 op | NUMBER { $$ = op_lit_num($1); } ;
162 c734c0e9 2021-08-03 op * `expr '=' '=' expr` is ambiguous. furthermore, we're not
163 c734c0e9 2021-08-03 op * interested in checking all the possibilities here.
165 acf1403a 2021-08-05 op cexpr : literal | varref | funcall | faccess ;
166 423f02f5 2021-12-28 cage check : cexpr '=' '=' cexpr { $$ = op_cmp_eq($1, $4); }
167 423f02f5 2021-12-28 cage | cexpr '<' '=' cexpr { $$ = op_cmp_leq($1, $4); }
170 340ee97d 2021-08-08 op expr : literal | funcall | varref | check | cast | faccess | vargs ;
172 340ee97d 2021-08-08 op vargs : VARGS { $$ = op_vargs(); } ;
174 c734c0e9 2021-08-03 op cast : expr ':' U8 { $$ = op_cast($1, V_U8); }
175 c734c0e9 2021-08-03 op | expr ':' U16 { $$ = op_cast($1, V_U16); }
176 c734c0e9 2021-08-03 op | expr ':' U32 { $$ = op_cast($1, V_U32); }
177 c734c0e9 2021-08-03 op | expr ':' STR { $$ = op_cast($1, V_STR); }
180 acf1403a 2021-08-05 op faccess : varref '.' SYMBOL { $$ = op_faccess($1, $3); }
181 acf1403a 2021-08-05 op | faccess '.' SYMBOL { $$ = op_faccess($1, $3); }
184 c734c0e9 2021-08-03 op procname: SYMBOL {
185 c734c0e9 2021-08-03 op if (($$ = proc_by_name($1)) == NULL) {
186 c734c0e9 2021-08-03 op yyerror("unknown proc %s", $1);
194 c734c0e9 2021-08-03 op funcall : procname {
195 d9d02161 2021-08-04 op prepare_funcall();
196 c734c0e9 2021-08-03 op } '(' args optcomma ')' {
197 7d79f46c 2021-08-04 op struct proc *proc;
200 d9d02161 2021-08-04 op $$ = op_funcall($1);
201 7d79f46c 2021-08-04 op proc = $$->v.funcall.proc;
202 7d79f46c 2021-08-04 op argc = $$->v.funcall.argc;
204 7d79f46c 2021-08-04 op if (argc != proc->minargs && !proc->vararg) {
205 7d79f46c 2021-08-04 op yyerror("invalid arity for `%s': want %d arguments "
206 7d79f46c 2021-08-04 op "but %d given.", $1->name, proc->minargs, argc);
207 c092e54b 2021-08-04 op /* TODO: recursively free $$ */
211 7d79f46c 2021-08-04 op if (argc < proc->minargs && proc->vararg) {
212 7d79f46c 2021-08-04 op yyerror("invalid arity for `%s': want at least %d "
213 7d79f46c 2021-08-04 op "arguments but %d given.", $1->name, proc->minargs,
215 7d79f46c 2021-08-04 op /* TODO: recursively free $$ */
221 c734c0e9 2021-08-03 op optcomma: /* empty */ | ',' ;
223 0e62706a 2021-08-04 op dots : '.' '.' '.' ;
225 c734c0e9 2021-08-03 op args : /* empty */
226 c734c0e9 2021-08-03 op | args ',' expr { push_arg($3); }
227 0e62706a 2021-08-04 op | args ',' dots { push_arg(op_rest()); }
228 c734c0e9 2021-08-03 op | expr { push_arg($1); }
229 0e62706a 2021-08-04 op | dots { push_arg(op_rest()); }
232 c734c0e9 2021-08-03 op proc : PROC SYMBOL {
233 d9d02161 2021-08-04 op prepare_proc();
234 c734c0e9 2021-08-03 op } '(' args ')' {
235 d9d02161 2021-08-04 op if (!proc_setup_body()) {
236 d9d02161 2021-08-04 op yyerror("invalid argument in proc `%s' definition",
241 c734c0e9 2021-08-03 op } '{' optnl block '}' {
242 d9d02161 2021-08-04 op proc_done($2);
246 c734c0e9 2021-08-03 op block : /* empty */
247 d7be3bcc 2021-08-04 op | block var nl { block_push($2); }
248 d7be3bcc 2021-08-04 op | block funcall nl { block_push($2); }
249 d7be3bcc 2021-08-04 op | block assert nl
250 8250ab19 2021-08-07 op | block sfail nl { block_push($2); }
253 8250ab19 2021-08-07 op sfail : SHOULD_FAIL expr { $$ = op_sfail($2, NULL); }
254 8250ab19 2021-08-07 op | SHOULD_FAIL expr ':' STRING { $$ = op_sfail($2, $4); }
257 e0b4ca44 2021-08-04 op assert : ASSERT asserti
258 e0b4ca44 2021-08-04 op | ASSERT '(' optnl massert ')'
261 e0b4ca44 2021-08-04 op massert : asserti nl | massert asserti nl ;
263 e0b4ca44 2021-08-04 op asserti : check { block_push(op_assert($1)); }
266 c734c0e9 2021-08-03 op test : TESTING STRING DIR STRING {
267 d9d02161 2021-08-04 op prepare_test();
268 0b453b63 2021-08-07 op } testopt '{' optnl block '}' {
269 0b453b63 2021-08-07 op test_done(shouldfail, $2, $4);
270 0b453b63 2021-08-07 op shouldfail = 0;
274 0b453b63 2021-08-07 op testopt : /* empty */
275 0b453b63 2021-08-07 op | SHOULD_FAIL { shouldfail = 1; }
280 c734c0e9 2021-08-03 op struct keywords {
281 c734c0e9 2021-08-03 op const char *k_name;
286 c734c0e9 2021-08-03 op yyerror(const char *fmt, ...)
291 c734c0e9 2021-08-03 op file->errors++;
292 c734c0e9 2021-08-03 op va_start(ap, fmt);
293 c734c0e9 2021-08-03 op if (vasprintf(&msg, fmt, ap) == -1)
294 c734c0e9 2021-08-03 op fatalx("yyerror vasprintf");
296 c734c0e9 2021-08-03 op logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
302 c734c0e9 2021-08-03 op kw_cmp(const void *k, const void *e)
304 c734c0e9 2021-08-03 op return strcmp(k, ((const struct keywords *)e)->k_name);
308 c734c0e9 2021-08-03 op lookup(char *s)
310 c734c0e9 2021-08-03 op /* This has to be sorted always. */
311 c734c0e9 2021-08-03 op static const struct keywords keywords[] = {
312 c734c0e9 2021-08-03 op {"assert", ASSERT},
313 c734c0e9 2021-08-03 op {"const", CONST},
314 c734c0e9 2021-08-03 op {"dir", DIR},
315 c734c0e9 2021-08-03 op {"include", INCLUDE},
316 c734c0e9 2021-08-03 op {"proc", PROC},
317 c734c0e9 2021-08-03 op {"repeat", REPEAT},
318 0b453b63 2021-08-07 op {"should-fail", SHOULD_FAIL},
319 c734c0e9 2021-08-03 op {"str", STR},
320 c734c0e9 2021-08-03 op {"testing", TESTING},
321 c734c0e9 2021-08-03 op {"u16", U16},
322 c734c0e9 2021-08-03 op {"u32", U32},
324 340ee97d 2021-08-08 op {"vargs", VARGS},
326 c734c0e9 2021-08-03 op const struct keywords *p;
328 c734c0e9 2021-08-03 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
329 c734c0e9 2021-08-03 op sizeof(keywords[0]), kw_cmp);
332 c734c0e9 2021-08-03 op return p->k_val;
334 c734c0e9 2021-08-03 op return SYMBOL;
337 c734c0e9 2021-08-03 op #define START_EXPAND 1
338 c734c0e9 2021-08-03 op #define DONE_EXPAND 2
340 c734c0e9 2021-08-03 op static int expanding;
348 c734c0e9 2021-08-03 op if (file->ungetpos > 0)
349 c734c0e9 2021-08-03 op c = file->ungetbuf[--file->ungetpos];
351 c734c0e9 2021-08-03 op c = getc(file->stream);
353 c734c0e9 2021-08-03 op if (c == START_EXPAND)
354 c734c0e9 2021-08-03 op expanding = 1;
355 c734c0e9 2021-08-03 op else if (c == DONE_EXPAND)
356 c734c0e9 2021-08-03 op expanding = 0;
364 c734c0e9 2021-08-03 op lgetc(int quotec)
368 c734c0e9 2021-08-03 op if (quotec) {
369 c734c0e9 2021-08-03 op if ((c = igetc()) == EOF) {
370 c734c0e9 2021-08-03 op yyerror("reached end of file while parsing "
371 c734c0e9 2021-08-03 op "quoted string");
372 c734c0e9 2021-08-03 op if (file == topfile || popfile() == EOF)
374 c734c0e9 2021-08-03 op return quotec;
379 c734c0e9 2021-08-03 op while ((c = igetc()) == '\\') {
380 c734c0e9 2021-08-03 op next = igetc();
381 c734c0e9 2021-08-03 op if (next != '\n') {
385 c734c0e9 2021-08-03 op yylval.lineno = file->lineno;
386 c734c0e9 2021-08-03 op file->lineno++;
389 c734c0e9 2021-08-03 op if (c == EOF) {
391 c734c0e9 2021-08-03 op * Fake EOL when hit EOF for the first time. This gets line
392 c734c0e9 2021-08-03 op * count right if last line in included file is syntactically
393 c734c0e9 2021-08-03 op * invalid and has no newline.
395 c734c0e9 2021-08-03 op if (file->eof_reached == 0) {
396 c734c0e9 2021-08-03 op file->eof_reached = 1;
399 c734c0e9 2021-08-03 op while (c == EOF) {
400 c734c0e9 2021-08-03 op if (file == topfile || popfile() == EOF)
409 c734c0e9 2021-08-03 op lungetc(int c)
411 c734c0e9 2021-08-03 op if (c == EOF)
414 c734c0e9 2021-08-03 op if (file->ungetpos >= file->ungetsize) {
415 c734c0e9 2021-08-03 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
416 c734c0e9 2021-08-03 op if (p == NULL)
417 c734c0e9 2021-08-03 op err(1, "lungetc");
418 c734c0e9 2021-08-03 op file->ungetbuf = p;
419 c734c0e9 2021-08-03 op file->ungetsize *= 2;
421 c734c0e9 2021-08-03 op file->ungetbuf[file->ungetpos++] = c;
425 c734c0e9 2021-08-03 op findeol(void)
429 c734c0e9 2021-08-03 op /* Skip to either EOF or the first real EOL. */
431 c734c0e9 2021-08-03 op c = lgetc(0);
432 c734c0e9 2021-08-03 op if (c == '\n') {
433 c734c0e9 2021-08-03 op file->lineno++;
436 c734c0e9 2021-08-03 op if (c == EOF)
439 c734c0e9 2021-08-03 op return ERROR;
444 c734c0e9 2021-08-03 op int my_yylex(void);
451 c734c0e9 2021-08-03 op switch (x = my_yylex()) {
452 c734c0e9 2021-08-03 op case ASSERT: puts("assert"); break;
453 c734c0e9 2021-08-03 op case CONST: puts("const"); break;
454 c734c0e9 2021-08-03 op case DIR: puts("dir"); break;
455 c734c0e9 2021-08-03 op case ERROR: puts("error"); break;
456 c734c0e9 2021-08-03 op case INCLUDE: puts("include"); break;
457 c734c0e9 2021-08-03 op case PROC: puts("proc"); break;
458 c734c0e9 2021-08-03 op case REPEAT: puts("repeat"); break;
459 c734c0e9 2021-08-03 op case STR: puts(":str"); break;
460 c734c0e9 2021-08-03 op case TESTING: puts("testing"); break;
461 c734c0e9 2021-08-03 op case U8: puts(":u8"); break;
462 c734c0e9 2021-08-03 op case U16: puts(":u16"); break;
463 c734c0e9 2021-08-03 op case U32: puts(":u32"); break;
465 c734c0e9 2021-08-03 op case STRING: printf("string \"%s\"\n", yylval.v.str); break;
466 c734c0e9 2021-08-03 op case SYMBOL: printf("symbol %s\n", yylval.v.str); break;
467 c734c0e9 2021-08-03 op case NUMBER: printf("number %"PRIu64"\n", yylval.v.num); break;
470 c734c0e9 2021-08-03 op printf("character ");
471 c734c0e9 2021-08-03 op if (x == '\n')
472 c734c0e9 2021-08-03 op printf("\\n");
474 c734c0e9 2021-08-03 op printf("%c", x);
475 c734c0e9 2021-08-03 op printf(" [0x%x]", x);
476 c734c0e9 2021-08-03 op printf("\n");
484 c734c0e9 2021-08-03 op my_yylex(void)
490 c734c0e9 2021-08-03 op unsigned char buf[8096];
491 c734c0e9 2021-08-03 op unsigned char *p;
492 c734c0e9 2021-08-03 op int quotec, next, c;
496 c734c0e9 2021-08-03 op while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\f')
499 c734c0e9 2021-08-03 op yylval.lineno = file->lineno;
500 c734c0e9 2021-08-03 op if (c == '#')
501 c734c0e9 2021-08-03 op while ((c = lgetc(0)) != '\n' && c != EOF)
512 c734c0e9 2021-08-03 op if ((c = lgetc(quotec)) == EOF)
514 c734c0e9 2021-08-03 op if (c == '\n') {
515 c734c0e9 2021-08-03 op file->lineno++;
517 c734c0e9 2021-08-03 op } else if (c == '\\') {
518 c734c0e9 2021-08-03 op if ((next = lgetc(quotec)) == EOF)
520 c734c0e9 2021-08-03 op if (next == quotec || next == ' ' ||
521 c734c0e9 2021-08-03 op next == '\t')
523 c734c0e9 2021-08-03 op else if (next == '\n') {
524 c734c0e9 2021-08-03 op file->lineno++;
527 c734c0e9 2021-08-03 op lungetc(next);
528 c734c0e9 2021-08-03 op } else if (c == quotec) {
531 c734c0e9 2021-08-03 op } else if (c == '\0') {
532 c734c0e9 2021-08-03 op yyerror("syntax error");
533 c734c0e9 2021-08-03 op return findeol();
536 c734c0e9 2021-08-03 op if (p + 1 >= buf + sizeof(buf) - 1) {
537 c734c0e9 2021-08-03 op yyerror("string too long");
538 c734c0e9 2021-08-03 op return findeol();
544 c734c0e9 2021-08-03 op yylval.v.str = xstrdup(buf);
545 c734c0e9 2021-08-03 op return STRING;
548 c734c0e9 2021-08-03 op #define allowed_to_end_number(x) \
549 c734c0e9 2021-08-03 op (isspace(x) || x == ')' || x == ',' || x == '/' || x == '}' \
550 1433867a 2021-08-04 op || x == '=' || x == ':')
552 c734c0e9 2021-08-03 op if (c == '-' || isdigit(c)) {
555 c734c0e9 2021-08-03 op if ((size_t)(p-buf) >= sizeof(buf)) {
556 c734c0e9 2021-08-03 op yyerror("string too long");
557 c734c0e9 2021-08-03 op return findeol();
559 ef33a69e 2021-08-07 op } while ((c = lgetc(0)) != EOF && (isdigit(c) || c == 'x'));
561 c734c0e9 2021-08-03 op if (p == buf + 1 && buf[0] == '-')
562 c734c0e9 2021-08-03 op goto nodigits;
563 c734c0e9 2021-08-03 op if (c == EOF || allowed_to_end_number(c)) {
568 ef33a69e 2021-08-07 op yylval.v.num = strtoll(buf, &ep, 0);
569 7abcb0db 2021-12-15 op if (*ep != '\0' || (errno == ERANGE &&
570 ef33a69e 2021-08-07 op (yylval.v.num == LONG_MAX ||
571 7abcb0db 2021-12-15 op yylval.v.num == LONG_MIN))) {
572 ef33a69e 2021-08-07 op yyerror("\"%s\" invalid number or out of range",
574 c734c0e9 2021-08-03 op return findeol();
577 c734c0e9 2021-08-03 op return NUMBER;
580 c734c0e9 2021-08-03 op while (p > buf + 1)
581 c734c0e9 2021-08-03 op lungetc(*--p);
583 c734c0e9 2021-08-03 op if (c == '-')
588 c734c0e9 2021-08-03 op #define allowed_in_symbol(x) \
589 c734c0e9 2021-08-03 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
590 c734c0e9 2021-08-03 op x != '{' && x != '}' && \
591 c734c0e9 2021-08-03 op x != '!' && x != '=' && \
592 e48a50b7 2021-08-07 op x != '#' && x != ',' && \
593 02fc4f51 2021-08-07 op x != '.' && x != ':'))
595 c734c0e9 2021-08-03 op if (isalnum(c) || c == ':' || c == '_') {
598 c734c0e9 2021-08-03 op if ((size_t)(p-buf) >= sizeof(buf)) {
599 c734c0e9 2021-08-03 op yyerror("string too long");
600 c734c0e9 2021-08-03 op return findeol();
602 c734c0e9 2021-08-03 op } while ((c = lgetc(0)) != EOF && (allowed_in_symbol(c)));
605 c734c0e9 2021-08-03 op if ((token = lookup(buf)) == SYMBOL)
606 c734c0e9 2021-08-03 op yylval.v.str = xstrdup(buf);
607 c734c0e9 2021-08-03 op return token;
610 c734c0e9 2021-08-03 op if (c == '\n') {
611 c734c0e9 2021-08-03 op yylval.lineno = file->lineno;
612 c734c0e9 2021-08-03 op file->lineno++;
614 c734c0e9 2021-08-03 op if (c == EOF)
619 c734c0e9 2021-08-03 op struct file *
620 c734c0e9 2021-08-03 op pushfile(const char *name)
622 c734c0e9 2021-08-03 op struct file *nfile;
624 c734c0e9 2021-08-03 op if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
625 c734c0e9 2021-08-03 op log_warn("calloc");
628 c734c0e9 2021-08-03 op if ((nfile->name = strdup(name)) == NULL) {
629 c734c0e9 2021-08-03 op log_warn("strdup");
633 c734c0e9 2021-08-03 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
634 c734c0e9 2021-08-03 op log_warn("%s", nfile->name);
635 c734c0e9 2021-08-03 op free(nfile->name);
639 c734c0e9 2021-08-03 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
640 c734c0e9 2021-08-03 op nfile->ungetsize = 16;
641 c734c0e9 2021-08-03 op nfile->ungetbuf = malloc(nfile->ungetsize);
642 c734c0e9 2021-08-03 op if (nfile->ungetbuf == NULL) {
643 c734c0e9 2021-08-03 op log_warn("malloc");
644 c734c0e9 2021-08-03 op fclose(nfile->stream);
645 c734c0e9 2021-08-03 op free(nfile->name);
649 c734c0e9 2021-08-03 op TAILQ_INSERT_TAIL(&files, nfile, entry);
650 c734c0e9 2021-08-03 op return nfile;
654 c734c0e9 2021-08-03 op popfile(void)
656 c734c0e9 2021-08-03 op struct file *prev;
658 c734c0e9 2021-08-03 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
659 c734c0e9 2021-08-03 op prev->errors += file->errors;
661 c734c0e9 2021-08-03 op TAILQ_REMOVE(&files, file, entry);
662 c734c0e9 2021-08-03 op fclose(file->stream);
663 c734c0e9 2021-08-03 op free(file->name);
664 c734c0e9 2021-08-03 op free(file->ungetbuf);
667 c734c0e9 2021-08-03 op return file ? 0 : EOF;
671 c734c0e9 2021-08-03 op loadfile(const char *path)
675 c734c0e9 2021-08-03 op file = pushfile(path);
676 c734c0e9 2021-08-03 op if (file == NULL)
677 c734c0e9 2021-08-03 op err(1, "pushfile");
678 c734c0e9 2021-08-03 op topfile = file;
681 c734c0e9 2021-08-03 op errors = file->errors;
685 c734c0e9 2021-08-03 op errx(1, "can't load %s because of errors", path);