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 c734c0e9 2021-08-03 op #include <inttypes.h>
30 c734c0e9 2021-08-03 op #include <limits.h>
31 c734c0e9 2021-08-03 op #include <stdio.h>
32 c734c0e9 2021-08-03 op #include <stdlib.h>
33 c734c0e9 2021-08-03 op #include <string.h>
34 c734c0e9 2021-08-03 op #include <syslog.h>
36 c734c0e9 2021-08-03 op #include "log.h"
37 c734c0e9 2021-08-03 op #include "kamid.h"
38 c734c0e9 2021-08-03 op #include "utils.h"
40 c734c0e9 2021-08-03 op #include "script.h"
42 c734c0e9 2021-08-03 op TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
43 c734c0e9 2021-08-03 op static struct file {
44 c734c0e9 2021-08-03 op TAILQ_ENTRY(file) entry;
47 c734c0e9 2021-08-03 op size_t ungetpos;
48 c734c0e9 2021-08-03 op size_t ungetsize;
49 c734c0e9 2021-08-03 op u_char *ungetbuf;
50 c734c0e9 2021-08-03 op int eof_reached;
53 c734c0e9 2021-08-03 op } *file, *topfile;
54 c734c0e9 2021-08-03 op struct file *pushfile(const char *);
55 c734c0e9 2021-08-03 op int popfile(void);
56 c734c0e9 2021-08-03 op int yyparse(void);
57 c734c0e9 2021-08-03 op int yylex(void);
58 c734c0e9 2021-08-03 op int yyerror(const char *, ...)
59 c734c0e9 2021-08-03 op __attribute__((__format__ (printf, 1, 2)))
60 c734c0e9 2021-08-03 op __attribute__((__nonnull__ (1)));
61 c734c0e9 2021-08-03 op int kw_cmp(const void *, const void *);
62 c734c0e9 2021-08-03 op int lookup(char *);
63 c734c0e9 2021-08-03 op int igetc(void);
64 c734c0e9 2021-08-03 op int lgetc(int);
65 c734c0e9 2021-08-03 op void lungetc(int);
66 c734c0e9 2021-08-03 op int findeol(void);
68 0b453b63 2021-08-07 op static int shouldfail;
70 c734c0e9 2021-08-03 op typedef struct {
72 c734c0e9 2021-08-03 op struct op *op;
73 c734c0e9 2021-08-03 op struct proc *proc;
84 c734c0e9 2021-08-03 op * %define parse.error verbose
91 c734c0e9 2021-08-03 op %token INCLUDE
94 0b453b63 2021-08-07 op %token SHOULD_FAIL STR
95 c734c0e9 2021-08-03 op %token TESTING
96 c734c0e9 2021-08-03 op %token U8 U16 U32
98 c734c0e9 2021-08-03 op %token <v.str> STRING SYMBOL
99 c734c0e9 2021-08-03 op %token <v.num> NUMBER
101 acf1403a 2021-08-05 op %type <v.op> cast cexpr check expr faccess funcall
102 8250ab19 2021-08-07 op %type <v.op> literal sfail var varref
104 c734c0e9 2021-08-03 op %type <v.proc> procname
108 c734c0e9 2021-08-03 op program : /* empty */
109 c734c0e9 2021-08-03 op | program '\n'
110 c734c0e9 2021-08-03 op | program include '\n'
111 c734c0e9 2021-08-03 op | program const '\n'
112 c734c0e9 2021-08-03 op | program proc '\n'
113 c734c0e9 2021-08-03 op | program test '\n'
116 c734c0e9 2021-08-03 op optnl : '\n' optnl /* zero or more newlines */
120 c01463a3 2021-08-04 op nl : '\n' optnl ;
122 c734c0e9 2021-08-03 op include : INCLUDE STRING {
123 c734c0e9 2021-08-03 op struct file *nfile;
125 c734c0e9 2021-08-03 op if ((nfile = pushfile($2)) == NULL) {
126 c734c0e9 2021-08-03 op yyerror("failed to include file %s", $2);
132 c734c0e9 2021-08-03 op file = nfile;
133 c734c0e9 2021-08-03 op lungetc('\n');
137 c01463a3 2021-08-04 op const : CONST consti
138 e0b4ca44 2021-08-04 op | CONST '(' optnl mconst ')'
141 c01463a3 2021-08-04 op mconst : consti nl | mconst consti nl ;
143 c01463a3 2021-08-04 op consti : SYMBOL '=' expr {
144 c01463a3 2021-08-04 op if (!global_set($1, $3)) {
145 a3097d05 2021-08-04 op yyerror("can't set %s: illegal expression", $1);
153 c734c0e9 2021-08-03 op var : SYMBOL '=' expr { $$ = op_assign($1, $3); } ;
154 c734c0e9 2021-08-03 op varref : SYMBOL { $$ = op_var($1); } ;
155 c734c0e9 2021-08-03 op literal : STRING { $$ = op_lit_str($1); }
156 c734c0e9 2021-08-03 op | NUMBER { $$ = op_lit_num($1); } ;
159 c734c0e9 2021-08-03 op * `expr '=' '=' expr` is ambiguous. furthermore, we're not
160 c734c0e9 2021-08-03 op * interested in checking all the possibilities here.
162 acf1403a 2021-08-05 op cexpr : literal | varref | funcall | faccess ;
163 c734c0e9 2021-08-03 op check : cexpr '=' '=' cexpr { $$ = op_cmp_eq($1, $4); } ;
165 acf1403a 2021-08-05 op expr : literal | funcall | varref | check | cast | faccess ;
167 c734c0e9 2021-08-03 op cast : expr ':' U8 { $$ = op_cast($1, V_U8); }
168 c734c0e9 2021-08-03 op | expr ':' U16 { $$ = op_cast($1, V_U16); }
169 c734c0e9 2021-08-03 op | expr ':' U32 { $$ = op_cast($1, V_U32); }
170 c734c0e9 2021-08-03 op | expr ':' STR { $$ = op_cast($1, V_STR); }
173 acf1403a 2021-08-05 op faccess : varref '.' SYMBOL { $$ = op_faccess($1, $3); }
174 acf1403a 2021-08-05 op | faccess '.' SYMBOL { $$ = op_faccess($1, $3); }
177 c734c0e9 2021-08-03 op procname: SYMBOL {
178 c734c0e9 2021-08-03 op if (($$ = proc_by_name($1)) == NULL) {
179 c734c0e9 2021-08-03 op yyerror("unknown proc %s", $1);
187 c734c0e9 2021-08-03 op funcall : procname {
188 d9d02161 2021-08-04 op prepare_funcall();
189 c734c0e9 2021-08-03 op } '(' args optcomma ')' {
190 7d79f46c 2021-08-04 op struct proc *proc;
193 d9d02161 2021-08-04 op $$ = op_funcall($1);
194 7d79f46c 2021-08-04 op proc = $$->v.funcall.proc;
195 7d79f46c 2021-08-04 op argc = $$->v.funcall.argc;
197 7d79f46c 2021-08-04 op if (argc != proc->minargs && !proc->vararg) {
198 7d79f46c 2021-08-04 op yyerror("invalid arity for `%s': want %d arguments "
199 7d79f46c 2021-08-04 op "but %d given.", $1->name, proc->minargs, argc);
200 c092e54b 2021-08-04 op /* TODO: recursively free $$ */
204 7d79f46c 2021-08-04 op if (argc < proc->minargs && proc->vararg) {
205 7d79f46c 2021-08-04 op yyerror("invalid arity for `%s': want at least %d "
206 7d79f46c 2021-08-04 op "arguments but %d given.", $1->name, proc->minargs,
208 7d79f46c 2021-08-04 op /* TODO: recursively free $$ */
214 c734c0e9 2021-08-03 op optcomma: /* empty */ | ',' ;
216 0e62706a 2021-08-04 op dots : '.' '.' '.' ;
218 c734c0e9 2021-08-03 op args : /* empty */
219 c734c0e9 2021-08-03 op | args ',' expr { push_arg($3); }
220 0e62706a 2021-08-04 op | args ',' dots { push_arg(op_rest()); }
221 c734c0e9 2021-08-03 op | expr { push_arg($1); }
222 0e62706a 2021-08-04 op | dots { push_arg(op_rest()); }
225 c734c0e9 2021-08-03 op proc : PROC SYMBOL {
226 d9d02161 2021-08-04 op prepare_proc();
227 c734c0e9 2021-08-03 op } '(' args ')' {
228 d9d02161 2021-08-04 op if (!proc_setup_body()) {
229 d9d02161 2021-08-04 op yyerror("invalid argument in proc `%s' definition",
234 c734c0e9 2021-08-03 op } '{' optnl block '}' {
235 d9d02161 2021-08-04 op proc_done($2);
239 c734c0e9 2021-08-03 op block : /* empty */
240 d7be3bcc 2021-08-04 op | block var nl { block_push($2); }
241 d7be3bcc 2021-08-04 op | block funcall nl { block_push($2); }
242 d7be3bcc 2021-08-04 op | block assert nl
243 8250ab19 2021-08-07 op | block sfail nl { block_push($2); }
246 8250ab19 2021-08-07 op sfail : SHOULD_FAIL expr { $$ = op_sfail($2, NULL); }
247 8250ab19 2021-08-07 op | SHOULD_FAIL expr ':' STRING { $$ = op_sfail($2, $4); }
250 e0b4ca44 2021-08-04 op assert : ASSERT asserti
251 e0b4ca44 2021-08-04 op | ASSERT '(' optnl massert ')'
254 e0b4ca44 2021-08-04 op massert : asserti nl | massert asserti nl ;
256 e0b4ca44 2021-08-04 op asserti : check { block_push(op_assert($1)); }
259 c734c0e9 2021-08-03 op test : TESTING STRING DIR STRING {
260 d9d02161 2021-08-04 op prepare_test();
261 0b453b63 2021-08-07 op } testopt '{' optnl block '}' {
262 0b453b63 2021-08-07 op test_done(shouldfail, $2, $4);
263 0b453b63 2021-08-07 op shouldfail = 0;
267 0b453b63 2021-08-07 op testopt : /* empty */
268 0b453b63 2021-08-07 op | SHOULD_FAIL { shouldfail = 1; }
273 c734c0e9 2021-08-03 op struct keywords {
274 c734c0e9 2021-08-03 op const char *k_name;
279 c734c0e9 2021-08-03 op yyerror(const char *fmt, ...)
284 c734c0e9 2021-08-03 op file->errors++;
285 c734c0e9 2021-08-03 op va_start(ap, fmt);
286 c734c0e9 2021-08-03 op if (vasprintf(&msg, fmt, ap) == -1)
287 c734c0e9 2021-08-03 op fatalx("yyerror vasprintf");
289 c734c0e9 2021-08-03 op logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
295 c734c0e9 2021-08-03 op kw_cmp(const void *k, const void *e)
297 c734c0e9 2021-08-03 op return strcmp(k, ((const struct keywords *)e)->k_name);
301 c734c0e9 2021-08-03 op lookup(char *s)
303 c734c0e9 2021-08-03 op /* This has to be sorted always. */
304 c734c0e9 2021-08-03 op static const struct keywords keywords[] = {
305 c734c0e9 2021-08-03 op {"assert", ASSERT},
306 c734c0e9 2021-08-03 op {"const", CONST},
307 c734c0e9 2021-08-03 op {"dir", DIR},
308 c734c0e9 2021-08-03 op {"include", INCLUDE},
309 c734c0e9 2021-08-03 op {"proc", PROC},
310 c734c0e9 2021-08-03 op {"repeat", REPEAT},
311 0b453b63 2021-08-07 op {"should-fail", SHOULD_FAIL},
312 c734c0e9 2021-08-03 op {"str", STR},
313 c734c0e9 2021-08-03 op {"testing", TESTING},
314 c734c0e9 2021-08-03 op {"u16", U16},
315 c734c0e9 2021-08-03 op {"u32", U32},
318 c734c0e9 2021-08-03 op const struct keywords *p;
320 c734c0e9 2021-08-03 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
321 c734c0e9 2021-08-03 op sizeof(keywords[0]), kw_cmp);
324 c734c0e9 2021-08-03 op return p->k_val;
326 c734c0e9 2021-08-03 op return SYMBOL;
329 c734c0e9 2021-08-03 op #define START_EXPAND 1
330 c734c0e9 2021-08-03 op #define DONE_EXPAND 2
332 c734c0e9 2021-08-03 op static int expanding;
340 c734c0e9 2021-08-03 op if (file->ungetpos > 0)
341 c734c0e9 2021-08-03 op c = file->ungetbuf[--file->ungetpos];
343 c734c0e9 2021-08-03 op c = getc(file->stream);
345 c734c0e9 2021-08-03 op if (c == START_EXPAND)
346 c734c0e9 2021-08-03 op expanding = 1;
347 c734c0e9 2021-08-03 op else if (c == DONE_EXPAND)
348 c734c0e9 2021-08-03 op expanding = 0;
356 c734c0e9 2021-08-03 op lgetc(int quotec)
360 c734c0e9 2021-08-03 op if (quotec) {
361 c734c0e9 2021-08-03 op if ((c = igetc()) == EOF) {
362 c734c0e9 2021-08-03 op yyerror("reached end of file while parsing "
363 c734c0e9 2021-08-03 op "quoted string");
364 c734c0e9 2021-08-03 op if (file == topfile || popfile() == EOF)
366 c734c0e9 2021-08-03 op return quotec;
371 c734c0e9 2021-08-03 op while ((c = igetc()) == '\\') {
372 c734c0e9 2021-08-03 op next = igetc();
373 c734c0e9 2021-08-03 op if (next != '\n') {
377 c734c0e9 2021-08-03 op yylval.lineno = file->lineno;
378 c734c0e9 2021-08-03 op file->lineno++;
381 c734c0e9 2021-08-03 op if (c == EOF) {
383 c734c0e9 2021-08-03 op * Fake EOL when hit EOF for the first time. This gets line
384 c734c0e9 2021-08-03 op * count right if last line in included file is syntactically
385 c734c0e9 2021-08-03 op * invalid and has no newline.
387 c734c0e9 2021-08-03 op if (file->eof_reached == 0) {
388 c734c0e9 2021-08-03 op file->eof_reached = 1;
391 c734c0e9 2021-08-03 op while (c == EOF) {
392 c734c0e9 2021-08-03 op if (file == topfile || popfile() == EOF)
401 c734c0e9 2021-08-03 op lungetc(int c)
403 c734c0e9 2021-08-03 op if (c == EOF)
406 c734c0e9 2021-08-03 op if (file->ungetpos >= file->ungetsize) {
407 c734c0e9 2021-08-03 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
408 c734c0e9 2021-08-03 op if (p == NULL)
409 c734c0e9 2021-08-03 op err(1, "lungetc");
410 c734c0e9 2021-08-03 op file->ungetbuf = p;
411 c734c0e9 2021-08-03 op file->ungetsize *= 2;
413 c734c0e9 2021-08-03 op file->ungetbuf[file->ungetpos++] = c;
417 c734c0e9 2021-08-03 op findeol(void)
421 c734c0e9 2021-08-03 op /* Skip to either EOF or the first real EOL. */
423 c734c0e9 2021-08-03 op c = lgetc(0);
424 c734c0e9 2021-08-03 op if (c == '\n') {
425 c734c0e9 2021-08-03 op file->lineno++;
428 c734c0e9 2021-08-03 op if (c == EOF)
431 c734c0e9 2021-08-03 op return ERROR;
436 c734c0e9 2021-08-03 op int my_yylex(void);
443 c734c0e9 2021-08-03 op switch (x = my_yylex()) {
444 c734c0e9 2021-08-03 op case ASSERT: puts("assert"); break;
445 c734c0e9 2021-08-03 op case CONST: puts("const"); break;
446 c734c0e9 2021-08-03 op case DIR: puts("dir"); break;
447 c734c0e9 2021-08-03 op case ERROR: puts("error"); break;
448 c734c0e9 2021-08-03 op case INCLUDE: puts("include"); break;
449 c734c0e9 2021-08-03 op case PROC: puts("proc"); break;
450 c734c0e9 2021-08-03 op case REPEAT: puts("repeat"); break;
451 c734c0e9 2021-08-03 op case STR: puts(":str"); break;
452 c734c0e9 2021-08-03 op case TESTING: puts("testing"); break;
453 c734c0e9 2021-08-03 op case U8: puts(":u8"); break;
454 c734c0e9 2021-08-03 op case U16: puts(":u16"); break;
455 c734c0e9 2021-08-03 op case U32: puts(":u32"); break;
457 c734c0e9 2021-08-03 op case STRING: printf("string \"%s\"\n", yylval.v.str); break;
458 c734c0e9 2021-08-03 op case SYMBOL: printf("symbol %s\n", yylval.v.str); break;
459 c734c0e9 2021-08-03 op case NUMBER: printf("number %"PRIu64"\n", yylval.v.num); break;
462 c734c0e9 2021-08-03 op printf("character ");
463 c734c0e9 2021-08-03 op if (x == '\n')
464 c734c0e9 2021-08-03 op printf("\\n");
466 c734c0e9 2021-08-03 op printf("%c", x);
467 c734c0e9 2021-08-03 op printf(" [0x%x]", x);
468 c734c0e9 2021-08-03 op printf("\n");
476 c734c0e9 2021-08-03 op my_yylex(void)
482 c734c0e9 2021-08-03 op unsigned char buf[8096];
483 c734c0e9 2021-08-03 op unsigned char *p;
484 c734c0e9 2021-08-03 op int quotec, next, c;
488 c734c0e9 2021-08-03 op while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\f')
491 c734c0e9 2021-08-03 op yylval.lineno = file->lineno;
492 c734c0e9 2021-08-03 op if (c == '#')
493 c734c0e9 2021-08-03 op while ((c = lgetc(0)) != '\n' && c != EOF)
504 c734c0e9 2021-08-03 op if ((c = lgetc(quotec)) == EOF)
506 c734c0e9 2021-08-03 op if (c == '\n') {
507 c734c0e9 2021-08-03 op file->lineno++;
509 c734c0e9 2021-08-03 op } else if (c == '\\') {
510 c734c0e9 2021-08-03 op if ((next = lgetc(quotec)) == EOF)
512 c734c0e9 2021-08-03 op if (next == quotec || next == ' ' ||
513 c734c0e9 2021-08-03 op next == '\t')
515 c734c0e9 2021-08-03 op else if (next == '\n') {
516 c734c0e9 2021-08-03 op file->lineno++;
519 c734c0e9 2021-08-03 op lungetc(next);
520 c734c0e9 2021-08-03 op } else if (c == quotec) {
523 c734c0e9 2021-08-03 op } else if (c == '\0') {
524 c734c0e9 2021-08-03 op yyerror("syntax error");
525 c734c0e9 2021-08-03 op return findeol();
528 c734c0e9 2021-08-03 op if (p + 1 >= buf + sizeof(buf) - 1) {
529 c734c0e9 2021-08-03 op yyerror("string too long");
530 c734c0e9 2021-08-03 op return findeol();
536 c734c0e9 2021-08-03 op yylval.v.str = xstrdup(buf);
537 c734c0e9 2021-08-03 op return STRING;
540 c734c0e9 2021-08-03 op #define allowed_to_end_number(x) \
541 c734c0e9 2021-08-03 op (isspace(x) || x == ')' || x == ',' || x == '/' || x == '}' \
542 1433867a 2021-08-04 op || x == '=' || x == ':')
544 c734c0e9 2021-08-03 op if (c == '-' || isdigit(c)) {
547 c734c0e9 2021-08-03 op if ((size_t)(p-buf) >= sizeof(buf)) {
548 c734c0e9 2021-08-03 op yyerror("string too long");
549 c734c0e9 2021-08-03 op return findeol();
551 c734c0e9 2021-08-03 op } while ((c = lgetc(0)) != EOF && isdigit(c));
553 c734c0e9 2021-08-03 op if (p == buf + 1 && buf[0] == '-')
554 c734c0e9 2021-08-03 op goto nodigits;
555 c734c0e9 2021-08-03 op if (c == EOF || allowed_to_end_number(c)) {
556 c734c0e9 2021-08-03 op const char *errstr = NULL;
559 c734c0e9 2021-08-03 op yylval.v.num = strtonum(buf, INT64_MIN, INT64_MAX,
561 c734c0e9 2021-08-03 op if (errstr) {
562 c734c0e9 2021-08-03 op yyerror("\"%s\" invalid number: %s",
563 c734c0e9 2021-08-03 op buf, errstr);
564 c734c0e9 2021-08-03 op return findeol();
566 c734c0e9 2021-08-03 op return NUMBER;
569 c734c0e9 2021-08-03 op while (p > buf + 1)
570 c734c0e9 2021-08-03 op lungetc(*--p);
572 c734c0e9 2021-08-03 op if (c == '-')
577 c734c0e9 2021-08-03 op #define allowed_in_symbol(x) \
578 c734c0e9 2021-08-03 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
579 c734c0e9 2021-08-03 op x != '{' && x != '}' && \
580 c734c0e9 2021-08-03 op x != '!' && x != '=' && \
581 e48a50b7 2021-08-07 op x != '#' && x != ',' && \
584 c734c0e9 2021-08-03 op if (isalnum(c) || c == ':' || c == '_') {
587 c734c0e9 2021-08-03 op if ((size_t)(p-buf) >= sizeof(buf)) {
588 c734c0e9 2021-08-03 op yyerror("string too long");
589 c734c0e9 2021-08-03 op return findeol();
591 c734c0e9 2021-08-03 op } while ((c = lgetc(0)) != EOF && (allowed_in_symbol(c)));
594 c734c0e9 2021-08-03 op if ((token = lookup(buf)) == SYMBOL)
595 c734c0e9 2021-08-03 op yylval.v.str = xstrdup(buf);
596 c734c0e9 2021-08-03 op return token;
599 c734c0e9 2021-08-03 op if (c == '\n') {
600 c734c0e9 2021-08-03 op yylval.lineno = file->lineno;
601 c734c0e9 2021-08-03 op file->lineno++;
603 c734c0e9 2021-08-03 op if (c == EOF)
608 c734c0e9 2021-08-03 op struct file *
609 c734c0e9 2021-08-03 op pushfile(const char *name)
611 c734c0e9 2021-08-03 op struct file *nfile;
613 c734c0e9 2021-08-03 op if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
614 c734c0e9 2021-08-03 op log_warn("calloc");
617 c734c0e9 2021-08-03 op if ((nfile->name = strdup(name)) == NULL) {
618 c734c0e9 2021-08-03 op log_warn("strdup");
622 c734c0e9 2021-08-03 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
623 c734c0e9 2021-08-03 op log_warn("%s", nfile->name);
624 c734c0e9 2021-08-03 op free(nfile->name);
628 c734c0e9 2021-08-03 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
629 c734c0e9 2021-08-03 op nfile->ungetsize = 16;
630 c734c0e9 2021-08-03 op nfile->ungetbuf = malloc(nfile->ungetsize);
631 c734c0e9 2021-08-03 op if (nfile->ungetbuf == NULL) {
632 c734c0e9 2021-08-03 op log_warn("malloc");
633 c734c0e9 2021-08-03 op fclose(nfile->stream);
634 c734c0e9 2021-08-03 op free(nfile->name);
638 c734c0e9 2021-08-03 op TAILQ_INSERT_TAIL(&files, nfile, entry);
639 c734c0e9 2021-08-03 op return nfile;
643 c734c0e9 2021-08-03 op popfile(void)
645 c734c0e9 2021-08-03 op struct file *prev;
647 c734c0e9 2021-08-03 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
648 c734c0e9 2021-08-03 op prev->errors += file->errors;
650 c734c0e9 2021-08-03 op TAILQ_REMOVE(&files, file, entry);
651 c734c0e9 2021-08-03 op fclose(file->stream);
652 c734c0e9 2021-08-03 op free(file->name);
653 c734c0e9 2021-08-03 op free(file->ungetbuf);
656 c734c0e9 2021-08-03 op return file ? 0 : EOF;
660 c734c0e9 2021-08-03 op loadfile(const char *path)
664 c734c0e9 2021-08-03 op file = pushfile(path);
665 c734c0e9 2021-08-03 op if (file == NULL)
666 c734c0e9 2021-08-03 op err(1, "pushfile");
667 c734c0e9 2021-08-03 op topfile = file;
670 c734c0e9 2021-08-03 op errors = file->errors;
674 c734c0e9 2021-08-03 op errx(1, "can't load %s because of errors", path);