2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 fb1a36c0 2022-01-09 op * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
4 fb1a36c0 2022-01-09 op * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 fb1a36c0 2022-01-09 op * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 fb1a36c0 2022-01-09 op * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 fb1a36c0 2022-01-09 op * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 fb1a36c0 2022-01-09 op * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 fb1a36c0 2022-01-09 op * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
12 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
13 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
15 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 fb1a36c0 2022-01-09 op #include <sys/queue.h>
28 fb1a36c0 2022-01-09 op #include <ctype.h>
29 fb1a36c0 2022-01-09 op #include <err.h>
30 fb1a36c0 2022-01-09 op #include <errno.h>
31 fb1a36c0 2022-01-09 op #include <event.h>
32 fb1a36c0 2022-01-09 op #include <inttypes.h>
33 fb1a36c0 2022-01-09 op #include <limits.h>
34 fb1a36c0 2022-01-09 op #include <limits.h>
35 fb1a36c0 2022-01-09 op #include <stdio.h>
36 fb1a36c0 2022-01-09 op #include <stdlib.h>
37 fb1a36c0 2022-01-09 op #include <string.h>
38 fb1a36c0 2022-01-09 op #include <syslog.h>
40 fb1a36c0 2022-01-09 op #include "log.h"
41 fb1a36c0 2022-01-09 op #include "kami.h"
42 fb1a36c0 2022-01-09 op #include "utils.h"
44 fb1a36c0 2022-01-09 op #include "script.h"
46 fb1a36c0 2022-01-09 op TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
47 fb1a36c0 2022-01-09 op static struct file {
48 fb1a36c0 2022-01-09 op TAILQ_ENTRY(file) entry;
51 fb1a36c0 2022-01-09 op size_t ungetpos;
52 fb1a36c0 2022-01-09 op size_t ungetsize;
53 fb1a36c0 2022-01-09 op u_char *ungetbuf;
54 fb1a36c0 2022-01-09 op int eof_reached;
57 fb1a36c0 2022-01-09 op } *file, *topfile;
58 fb1a36c0 2022-01-09 op struct file *pushfile(const char *);
59 fb1a36c0 2022-01-09 op int popfile(void);
60 fb1a36c0 2022-01-09 op int yyparse(void);
61 fb1a36c0 2022-01-09 op int yylex(void);
62 fb1a36c0 2022-01-09 op int yyerror(const char *, ...)
63 fb1a36c0 2022-01-09 op __attribute__((__format__ (printf, 1, 2)))
64 fb1a36c0 2022-01-09 op __attribute__((__nonnull__ (1)));
65 fb1a36c0 2022-01-09 op int kw_cmp(const void *, const void *);
66 fb1a36c0 2022-01-09 op int lookup(char *);
67 fb1a36c0 2022-01-09 op int igetc(void);
68 fb1a36c0 2022-01-09 op int lgetc(int);
69 fb1a36c0 2022-01-09 op void lungetc(int);
70 fb1a36c0 2022-01-09 op int findeol(void);
72 fb1a36c0 2022-01-09 op static int shouldfail;
74 fb1a36c0 2022-01-09 op typedef struct {
76 fb1a36c0 2022-01-09 op struct op *op;
77 fb1a36c0 2022-01-09 op struct proc *proc;
88 fb1a36c0 2022-01-09 op * %define parse.error verbose
95 fb1a36c0 2022-01-09 op %token INCLUDE
98 fb1a36c0 2022-01-09 op %token SHOULD_FAIL STR
99 fb1a36c0 2022-01-09 op %token TESTING
100 fb1a36c0 2022-01-09 op %token U8 U16 U32
103 fb1a36c0 2022-01-09 op %token <v.str> STRING SYMBOL
104 fb1a36c0 2022-01-09 op %token <v.num> NUMBER
106 fb1a36c0 2022-01-09 op %type <v.op> cast cexpr check expr faccess funcall
107 fb1a36c0 2022-01-09 op %type <v.op> literal sfail var varref vargs
109 fb1a36c0 2022-01-09 op %type <v.proc> procname
113 fb1a36c0 2022-01-09 op program : /* empty */
114 fb1a36c0 2022-01-09 op | program '\n'
115 fb1a36c0 2022-01-09 op | program include '\n'
116 fb1a36c0 2022-01-09 op | program const '\n'
117 fb1a36c0 2022-01-09 op | program proc '\n'
118 fb1a36c0 2022-01-09 op | program test '\n'
121 fb1a36c0 2022-01-09 op optnl : '\n' optnl /* zero or more newlines */
125 fb1a36c0 2022-01-09 op nl : '\n' optnl ;
127 fb1a36c0 2022-01-09 op include : INCLUDE STRING {
128 fb1a36c0 2022-01-09 op struct file *nfile;
130 fb1a36c0 2022-01-09 op if ((nfile = pushfile($2)) == NULL) {
131 fb1a36c0 2022-01-09 op yyerror("failed to include file %s", $2);
137 fb1a36c0 2022-01-09 op file = nfile;
138 fb1a36c0 2022-01-09 op lungetc('\n');
142 fb1a36c0 2022-01-09 op const : CONST consti
143 fb1a36c0 2022-01-09 op | CONST '(' optnl mconst ')'
146 fb1a36c0 2022-01-09 op mconst : consti nl | mconst consti nl ;
148 fb1a36c0 2022-01-09 op consti : SYMBOL '=' expr {
149 fb1a36c0 2022-01-09 op if (!global_set($1, $3)) {
150 fb1a36c0 2022-01-09 op yyerror("can't set %s: illegal expression", $1);
158 fb1a36c0 2022-01-09 op var : SYMBOL '=' expr { $$ = op_assign($1, $3); } ;
159 fb1a36c0 2022-01-09 op varref : SYMBOL { $$ = op_var($1); } ;
160 fb1a36c0 2022-01-09 op literal : STRING { $$ = op_lit_str($1); }
161 fb1a36c0 2022-01-09 op | NUMBER { $$ = op_lit_num($1); } ;
164 fb1a36c0 2022-01-09 op * `expr '=' '=' expr` is ambiguous. furthermore, we're not
165 fb1a36c0 2022-01-09 op * interested in checking all the possibilities here.
167 fb1a36c0 2022-01-09 op cexpr : literal | varref | funcall | faccess ;
168 fb1a36c0 2022-01-09 op check : cexpr '=' '=' cexpr { $$ = op_cmp_eq($1, $4); }
169 fb1a36c0 2022-01-09 op | cexpr '<' '=' cexpr { $$ = op_cmp_leq($1, $4); }
172 fb1a36c0 2022-01-09 op expr : literal | funcall | varref | check | cast | faccess | vargs ;
174 fb1a36c0 2022-01-09 op vargs : VARGS { $$ = op_vargs(); } ;
176 fb1a36c0 2022-01-09 op cast : expr ':' U8 { $$ = op_cast($1, V_U8); }
177 fb1a36c0 2022-01-09 op | expr ':' U16 { $$ = op_cast($1, V_U16); }
178 fb1a36c0 2022-01-09 op | expr ':' U32 { $$ = op_cast($1, V_U32); }
179 fb1a36c0 2022-01-09 op | expr ':' STR { $$ = op_cast($1, V_STR); }
182 fb1a36c0 2022-01-09 op faccess : varref '.' SYMBOL { $$ = op_faccess($1, $3); }
183 fb1a36c0 2022-01-09 op | faccess '.' SYMBOL { $$ = op_faccess($1, $3); }
186 fb1a36c0 2022-01-09 op procname: SYMBOL {
187 fb1a36c0 2022-01-09 op if (($$ = proc_by_name($1)) == NULL) {
188 fb1a36c0 2022-01-09 op yyerror("unknown proc %s", $1);
196 fb1a36c0 2022-01-09 op funcall : procname {
197 fb1a36c0 2022-01-09 op prepare_funcall();
198 fb1a36c0 2022-01-09 op } '(' args optcomma ')' {
199 fb1a36c0 2022-01-09 op struct proc *proc;
202 fb1a36c0 2022-01-09 op $$ = op_funcall($1);
203 fb1a36c0 2022-01-09 op proc = $$->v.funcall.proc;
204 fb1a36c0 2022-01-09 op argc = $$->v.funcall.argc;
206 fb1a36c0 2022-01-09 op if (argc != proc->minargs && !proc->vararg) {
207 fb1a36c0 2022-01-09 op yyerror("invalid arity for `%s': want %d arguments "
208 fb1a36c0 2022-01-09 op "but %d given.", $1->name, proc->minargs, argc);
209 fb1a36c0 2022-01-09 op /* TODO: recursively free $$ */
213 fb1a36c0 2022-01-09 op if (argc < proc->minargs && proc->vararg) {
214 fb1a36c0 2022-01-09 op yyerror("invalid arity for `%s': want at least %d "
215 fb1a36c0 2022-01-09 op "arguments but %d given.", $1->name, proc->minargs,
217 fb1a36c0 2022-01-09 op /* TODO: recursively free $$ */
223 fb1a36c0 2022-01-09 op optcomma: /* empty */ | ',' ;
225 fb1a36c0 2022-01-09 op dots : '.' '.' '.' ;
227 fb1a36c0 2022-01-09 op args : /* empty */
228 fb1a36c0 2022-01-09 op | args ',' expr { push_arg($3); }
229 fb1a36c0 2022-01-09 op | args ',' dots { push_arg(op_rest()); }
230 fb1a36c0 2022-01-09 op | expr { push_arg($1); }
231 fb1a36c0 2022-01-09 op | dots { push_arg(op_rest()); }
234 fb1a36c0 2022-01-09 op proc : PROC SYMBOL {
235 fb1a36c0 2022-01-09 op prepare_proc();
236 fb1a36c0 2022-01-09 op } '(' args ')' {
237 fb1a36c0 2022-01-09 op if (!proc_setup_body()) {
238 fb1a36c0 2022-01-09 op yyerror("invalid argument in proc `%s' definition",
243 fb1a36c0 2022-01-09 op } '{' optnl block '}' {
244 fb1a36c0 2022-01-09 op proc_done($2);
248 fb1a36c0 2022-01-09 op block : /* empty */
249 fb1a36c0 2022-01-09 op | block var nl { block_push($2); }
250 fb1a36c0 2022-01-09 op | block funcall nl { block_push($2); }
251 fb1a36c0 2022-01-09 op | block assert nl
252 fb1a36c0 2022-01-09 op | block sfail nl { block_push($2); }
255 fb1a36c0 2022-01-09 op sfail : SHOULD_FAIL expr { $$ = op_sfail($2, NULL); }
256 fb1a36c0 2022-01-09 op | SHOULD_FAIL expr ':' STRING { $$ = op_sfail($2, $4); }
259 fb1a36c0 2022-01-09 op assert : ASSERT asserti
260 fb1a36c0 2022-01-09 op | ASSERT '(' optnl massert ')'
263 fb1a36c0 2022-01-09 op massert : asserti nl | massert asserti nl ;
265 fb1a36c0 2022-01-09 op asserti : check { block_push(op_assert($1)); }
268 fb1a36c0 2022-01-09 op test : TESTING STRING DIR STRING {
269 fb1a36c0 2022-01-09 op prepare_test();
270 fb1a36c0 2022-01-09 op } testopt '{' optnl block '}' {
271 fb1a36c0 2022-01-09 op test_done(shouldfail, $2, $4);
272 fb1a36c0 2022-01-09 op shouldfail = 0;
276 fb1a36c0 2022-01-09 op testopt : /* empty */
277 fb1a36c0 2022-01-09 op | SHOULD_FAIL { shouldfail = 1; }
282 fb1a36c0 2022-01-09 op struct keywords {
283 fb1a36c0 2022-01-09 op const char *k_name;
288 fb1a36c0 2022-01-09 op yyerror(const char *fmt, ...)
293 fb1a36c0 2022-01-09 op file->errors++;
294 fb1a36c0 2022-01-09 op va_start(ap, fmt);
295 fb1a36c0 2022-01-09 op if (vasprintf(&msg, fmt, ap) == -1)
296 fb1a36c0 2022-01-09 op fatalx("yyerror vasprintf");
298 fb1a36c0 2022-01-09 op logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
304 fb1a36c0 2022-01-09 op kw_cmp(const void *k, const void *e)
306 fb1a36c0 2022-01-09 op return strcmp(k, ((const struct keywords *)e)->k_name);
310 fb1a36c0 2022-01-09 op lookup(char *s)
312 fb1a36c0 2022-01-09 op /* This has to be sorted always. */
313 fb1a36c0 2022-01-09 op static const struct keywords keywords[] = {
314 fb1a36c0 2022-01-09 op {"assert", ASSERT},
315 fb1a36c0 2022-01-09 op {"const", CONST},
316 fb1a36c0 2022-01-09 op {"dir", DIR},
317 fb1a36c0 2022-01-09 op {"include", INCLUDE},
318 fb1a36c0 2022-01-09 op {"proc", PROC},
319 fb1a36c0 2022-01-09 op {"repeat", REPEAT},
320 fb1a36c0 2022-01-09 op {"should-fail", SHOULD_FAIL},
321 fb1a36c0 2022-01-09 op {"str", STR},
322 fb1a36c0 2022-01-09 op {"testing", TESTING},
323 fb1a36c0 2022-01-09 op {"u16", U16},
324 fb1a36c0 2022-01-09 op {"u32", U32},
326 fb1a36c0 2022-01-09 op {"vargs", VARGS},
328 fb1a36c0 2022-01-09 op const struct keywords *p;
330 fb1a36c0 2022-01-09 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
331 fb1a36c0 2022-01-09 op sizeof(keywords[0]), kw_cmp);
334 fb1a36c0 2022-01-09 op return p->k_val;
336 fb1a36c0 2022-01-09 op return SYMBOL;
339 fb1a36c0 2022-01-09 op #define START_EXPAND 1
340 fb1a36c0 2022-01-09 op #define DONE_EXPAND 2
342 fb1a36c0 2022-01-09 op static int expanding;
350 fb1a36c0 2022-01-09 op if (file->ungetpos > 0)
351 fb1a36c0 2022-01-09 op c = file->ungetbuf[--file->ungetpos];
353 fb1a36c0 2022-01-09 op c = getc(file->stream);
355 fb1a36c0 2022-01-09 op if (c == START_EXPAND)
356 fb1a36c0 2022-01-09 op expanding = 1;
357 fb1a36c0 2022-01-09 op else if (c == DONE_EXPAND)
358 fb1a36c0 2022-01-09 op expanding = 0;
366 fb1a36c0 2022-01-09 op lgetc(int quotec)
370 fb1a36c0 2022-01-09 op if (quotec) {
371 fb1a36c0 2022-01-09 op if ((c = igetc()) == EOF) {
372 fb1a36c0 2022-01-09 op yyerror("reached end of file while parsing "
373 fb1a36c0 2022-01-09 op "quoted string");
374 fb1a36c0 2022-01-09 op if (file == topfile || popfile() == EOF)
376 fb1a36c0 2022-01-09 op return quotec;
381 fb1a36c0 2022-01-09 op while ((c = igetc()) == '\\') {
382 fb1a36c0 2022-01-09 op next = igetc();
383 fb1a36c0 2022-01-09 op if (next != '\n') {
387 fb1a36c0 2022-01-09 op yylval.lineno = file->lineno;
388 fb1a36c0 2022-01-09 op file->lineno++;
391 fb1a36c0 2022-01-09 op if (c == EOF) {
393 fb1a36c0 2022-01-09 op * Fake EOL when hit EOF for the first time. This gets line
394 fb1a36c0 2022-01-09 op * count right if last line in included file is syntactically
395 fb1a36c0 2022-01-09 op * invalid and has no newline.
397 fb1a36c0 2022-01-09 op if (file->eof_reached == 0) {
398 fb1a36c0 2022-01-09 op file->eof_reached = 1;
401 fb1a36c0 2022-01-09 op while (c == EOF) {
402 fb1a36c0 2022-01-09 op if (file == topfile || popfile() == EOF)
411 fb1a36c0 2022-01-09 op lungetc(int c)
413 fb1a36c0 2022-01-09 op if (c == EOF)
416 fb1a36c0 2022-01-09 op if (file->ungetpos >= file->ungetsize) {
417 fb1a36c0 2022-01-09 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
418 fb1a36c0 2022-01-09 op if (p == NULL)
419 fb1a36c0 2022-01-09 op err(1, "lungetc");
420 fb1a36c0 2022-01-09 op file->ungetbuf = p;
421 fb1a36c0 2022-01-09 op file->ungetsize *= 2;
423 fb1a36c0 2022-01-09 op file->ungetbuf[file->ungetpos++] = c;
427 fb1a36c0 2022-01-09 op findeol(void)
431 fb1a36c0 2022-01-09 op /* Skip to either EOF or the first real EOL. */
433 fb1a36c0 2022-01-09 op c = lgetc(0);
434 fb1a36c0 2022-01-09 op if (c == '\n') {
435 fb1a36c0 2022-01-09 op file->lineno++;
438 fb1a36c0 2022-01-09 op if (c == EOF)
441 fb1a36c0 2022-01-09 op return ERROR;
446 fb1a36c0 2022-01-09 op int my_yylex(void);
453 fb1a36c0 2022-01-09 op switch (x = my_yylex()) {
454 fb1a36c0 2022-01-09 op case ASSERT: puts("assert"); break;
455 fb1a36c0 2022-01-09 op case CONST: puts("const"); break;
456 fb1a36c0 2022-01-09 op case DIR: puts("dir"); break;
457 fb1a36c0 2022-01-09 op case ERROR: puts("error"); break;
458 fb1a36c0 2022-01-09 op case INCLUDE: puts("include"); break;
459 fb1a36c0 2022-01-09 op case PROC: puts("proc"); break;
460 fb1a36c0 2022-01-09 op case REPEAT: puts("repeat"); break;
461 fb1a36c0 2022-01-09 op case STR: puts(":str"); break;
462 fb1a36c0 2022-01-09 op case TESTING: puts("testing"); break;
463 fb1a36c0 2022-01-09 op case U8: puts(":u8"); break;
464 fb1a36c0 2022-01-09 op case U16: puts(":u16"); break;
465 fb1a36c0 2022-01-09 op case U32: puts(":u32"); break;
467 fb1a36c0 2022-01-09 op case STRING: printf("string \"%s\"\n", yylval.v.str); break;
468 fb1a36c0 2022-01-09 op case SYMBOL: printf("symbol %s\n", yylval.v.str); break;
469 fb1a36c0 2022-01-09 op case NUMBER: printf("number %"PRIu64"\n", yylval.v.num); break;
472 fb1a36c0 2022-01-09 op printf("character ");
473 fb1a36c0 2022-01-09 op if (x == '\n')
474 fb1a36c0 2022-01-09 op printf("\\n");
476 fb1a36c0 2022-01-09 op printf("%c", x);
477 fb1a36c0 2022-01-09 op printf(" [0x%x]", x);
478 fb1a36c0 2022-01-09 op printf("\n");
486 fb1a36c0 2022-01-09 op my_yylex(void)
492 fb1a36c0 2022-01-09 op unsigned char buf[8096];
493 fb1a36c0 2022-01-09 op unsigned char *p;
494 fb1a36c0 2022-01-09 op int quotec, next, c;
498 fb1a36c0 2022-01-09 op while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\f')
501 fb1a36c0 2022-01-09 op yylval.lineno = file->lineno;
502 fb1a36c0 2022-01-09 op if (c == '#')
503 fb1a36c0 2022-01-09 op while ((c = lgetc(0)) != '\n' && c != EOF)
514 fb1a36c0 2022-01-09 op if ((c = lgetc(quotec)) == EOF)
516 fb1a36c0 2022-01-09 op if (c == '\n') {
517 fb1a36c0 2022-01-09 op file->lineno++;
519 fb1a36c0 2022-01-09 op } else if (c == '\\') {
520 fb1a36c0 2022-01-09 op if ((next = lgetc(quotec)) == EOF)
522 fb1a36c0 2022-01-09 op if (next == quotec || next == ' ' ||
523 fb1a36c0 2022-01-09 op next == '\t')
525 fb1a36c0 2022-01-09 op else if (next == '\n') {
526 fb1a36c0 2022-01-09 op file->lineno++;
529 fb1a36c0 2022-01-09 op lungetc(next);
530 fb1a36c0 2022-01-09 op } else if (c == quotec) {
533 fb1a36c0 2022-01-09 op } else if (c == '\0') {
534 fb1a36c0 2022-01-09 op yyerror("syntax error");
535 fb1a36c0 2022-01-09 op return findeol();
538 fb1a36c0 2022-01-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
539 fb1a36c0 2022-01-09 op yyerror("string too long");
540 fb1a36c0 2022-01-09 op return findeol();
546 fb1a36c0 2022-01-09 op yylval.v.str = xstrdup(buf);
547 fb1a36c0 2022-01-09 op return STRING;
550 fb1a36c0 2022-01-09 op #define allowed_to_end_number(x) \
551 fb1a36c0 2022-01-09 op (isspace(x) || x == ')' || x == ',' || x == '/' || x == '}' \
552 fb1a36c0 2022-01-09 op || x == '=' || x == ':')
554 fb1a36c0 2022-01-09 op if (c == '-' || isdigit(c)) {
557 fb1a36c0 2022-01-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
558 fb1a36c0 2022-01-09 op yyerror("string too long");
559 fb1a36c0 2022-01-09 op return findeol();
561 fb1a36c0 2022-01-09 op } while ((c = lgetc(0)) != EOF && (isdigit(c) || c == 'x'));
563 fb1a36c0 2022-01-09 op if (p == buf + 1 && buf[0] == '-')
564 fb1a36c0 2022-01-09 op goto nodigits;
565 fb1a36c0 2022-01-09 op if (c == EOF || allowed_to_end_number(c)) {
570 fb1a36c0 2022-01-09 op yylval.v.num = strtoll(buf, &ep, 0);
571 fb1a36c0 2022-01-09 op if (*ep != '\0' || (errno == ERANGE &&
572 fb1a36c0 2022-01-09 op (yylval.v.num == LONG_MAX ||
573 fb1a36c0 2022-01-09 op yylval.v.num == LONG_MIN))) {
574 fb1a36c0 2022-01-09 op yyerror("\"%s\" invalid number or out of range",
576 fb1a36c0 2022-01-09 op return findeol();
579 fb1a36c0 2022-01-09 op return NUMBER;
582 fb1a36c0 2022-01-09 op while (p > buf + 1)
583 fb1a36c0 2022-01-09 op lungetc(*--p);
585 fb1a36c0 2022-01-09 op if (c == '-')
590 fb1a36c0 2022-01-09 op #define allowed_in_symbol(x) \
591 fb1a36c0 2022-01-09 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
592 fb1a36c0 2022-01-09 op x != '{' && x != '}' && \
593 fb1a36c0 2022-01-09 op x != '!' && x != '=' && \
594 fb1a36c0 2022-01-09 op x != '#' && x != ',' && \
595 fb1a36c0 2022-01-09 op x != '.' && x != ':'))
597 fb1a36c0 2022-01-09 op if (isalnum(c) || c == ':' || c == '_') {
600 fb1a36c0 2022-01-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
601 fb1a36c0 2022-01-09 op yyerror("string too long");
602 fb1a36c0 2022-01-09 op return findeol();
604 fb1a36c0 2022-01-09 op } while ((c = lgetc(0)) != EOF && (allowed_in_symbol(c)));
607 fb1a36c0 2022-01-09 op if ((token = lookup(buf)) == SYMBOL)
608 fb1a36c0 2022-01-09 op yylval.v.str = xstrdup(buf);
609 fb1a36c0 2022-01-09 op return token;
612 fb1a36c0 2022-01-09 op if (c == '\n') {
613 fb1a36c0 2022-01-09 op yylval.lineno = file->lineno;
614 fb1a36c0 2022-01-09 op file->lineno++;
616 fb1a36c0 2022-01-09 op if (c == EOF)
621 fb1a36c0 2022-01-09 op struct file *
622 fb1a36c0 2022-01-09 op pushfile(const char *name)
624 fb1a36c0 2022-01-09 op struct file *nfile;
626 fb1a36c0 2022-01-09 op if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
627 fb1a36c0 2022-01-09 op log_warn("calloc");
630 fb1a36c0 2022-01-09 op if ((nfile->name = strdup(name)) == NULL) {
631 fb1a36c0 2022-01-09 op log_warn("strdup");
635 fb1a36c0 2022-01-09 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
636 fb1a36c0 2022-01-09 op log_warn("%s", nfile->name);
637 fb1a36c0 2022-01-09 op free(nfile->name);
641 fb1a36c0 2022-01-09 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
642 fb1a36c0 2022-01-09 op nfile->ungetsize = 16;
643 fb1a36c0 2022-01-09 op nfile->ungetbuf = malloc(nfile->ungetsize);
644 fb1a36c0 2022-01-09 op if (nfile->ungetbuf == NULL) {
645 fb1a36c0 2022-01-09 op log_warn("malloc");
646 fb1a36c0 2022-01-09 op fclose(nfile->stream);
647 fb1a36c0 2022-01-09 op free(nfile->name);
651 fb1a36c0 2022-01-09 op TAILQ_INSERT_TAIL(&files, nfile, entry);
652 fb1a36c0 2022-01-09 op return nfile;
656 fb1a36c0 2022-01-09 op popfile(void)
658 fb1a36c0 2022-01-09 op struct file *prev;
660 fb1a36c0 2022-01-09 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
661 fb1a36c0 2022-01-09 op prev->errors += file->errors;
663 fb1a36c0 2022-01-09 op TAILQ_REMOVE(&files, file, entry);
664 fb1a36c0 2022-01-09 op fclose(file->stream);
665 fb1a36c0 2022-01-09 op free(file->name);
666 fb1a36c0 2022-01-09 op free(file->ungetbuf);
669 fb1a36c0 2022-01-09 op return file ? 0 : EOF;
673 fb1a36c0 2022-01-09 op loadfile(const char *path)
677 fb1a36c0 2022-01-09 op file = pushfile(path);
678 fb1a36c0 2022-01-09 op if (file == NULL)
679 fb1a36c0 2022-01-09 op err(1, "pushfile");
680 fb1a36c0 2022-01-09 op topfile = file;
683 fb1a36c0 2022-01-09 op errors = file->errors;
687 fb1a36c0 2022-01-09 op errx(1, "can't load %s because of errors", path);