Blame


1 c734c0e9 2021-08-03 op /*
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.
10 c734c0e9 2021-08-03 op *
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.
14 c734c0e9 2021-08-03 op *
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.
22 c734c0e9 2021-08-03 op */
23 c734c0e9 2021-08-03 op
24 c734c0e9 2021-08-03 op %{
25 c734c0e9 2021-08-03 op
26 c734c0e9 2021-08-03 op #include "compat.h"
27 c734c0e9 2021-08-03 op
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>
37 c734c0e9 2021-08-03 op
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"
41 c734c0e9 2021-08-03 op
42 c734c0e9 2021-08-03 op #include "script.h"
43 c734c0e9 2021-08-03 op
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;
47 c734c0e9 2021-08-03 op FILE *stream;
48 c734c0e9 2021-08-03 op char *name;
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;
53 c734c0e9 2021-08-03 op int lineno;
54 c734c0e9 2021-08-03 op int errors;
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);
69 c734c0e9 2021-08-03 op
70 0b453b63 2021-08-07 op static int shouldfail;
71 0b453b63 2021-08-07 op
72 c734c0e9 2021-08-03 op typedef struct {
73 c734c0e9 2021-08-03 op union {
74 c734c0e9 2021-08-03 op struct op *op;
75 c734c0e9 2021-08-03 op struct proc *proc;
76 c734c0e9 2021-08-03 op char *str;
77 c734c0e9 2021-08-03 op int64_t num;
78 c734c0e9 2021-08-03 op } v;
79 c734c0e9 2021-08-03 op int lineno;
80 c734c0e9 2021-08-03 op } YYSTYPE;
81 c734c0e9 2021-08-03 op
82 c734c0e9 2021-08-03 op %}
83 c734c0e9 2021-08-03 op
84 c734c0e9 2021-08-03 op /*
85 c734c0e9 2021-08-03 op * for bison:
86 c734c0e9 2021-08-03 op * %define parse.error verbose
87 c734c0e9 2021-08-03 op */
88 c734c0e9 2021-08-03 op
89 c734c0e9 2021-08-03 op %token ASSERT
90 c734c0e9 2021-08-03 op %token CONST
91 c734c0e9 2021-08-03 op %token DIR
92 c734c0e9 2021-08-03 op %token ERROR
93 c734c0e9 2021-08-03 op %token INCLUDE
94 c734c0e9 2021-08-03 op %token PROC
95 c734c0e9 2021-08-03 op %token REPEAT
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
99 340ee97d 2021-08-08 op %token VARGS
100 c734c0e9 2021-08-03 op
101 c734c0e9 2021-08-03 op %token <v.str> STRING SYMBOL
102 c734c0e9 2021-08-03 op %token <v.num> NUMBER
103 c734c0e9 2021-08-03 op
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
106 c734c0e9 2021-08-03 op
107 c734c0e9 2021-08-03 op %type <v.proc> procname
108 c734c0e9 2021-08-03 op
109 c734c0e9 2021-08-03 op %%
110 c734c0e9 2021-08-03 op
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'
117 c734c0e9 2021-08-03 op ;
118 c734c0e9 2021-08-03 op
119 c734c0e9 2021-08-03 op optnl : '\n' optnl /* zero or more newlines */
120 c734c0e9 2021-08-03 op | /*empty*/
121 c734c0e9 2021-08-03 op ;
122 c734c0e9 2021-08-03 op
123 c01463a3 2021-08-04 op nl : '\n' optnl ;
124 c01463a3 2021-08-04 op
125 c734c0e9 2021-08-03 op include : INCLUDE STRING {
126 c734c0e9 2021-08-03 op struct file *nfile;
127 c734c0e9 2021-08-03 op
128 c734c0e9 2021-08-03 op if ((nfile = pushfile($2)) == NULL) {
129 c734c0e9 2021-08-03 op yyerror("failed to include file %s", $2);
130 c734c0e9 2021-08-03 op free($2);
131 c734c0e9 2021-08-03 op YYERROR;
132 c734c0e9 2021-08-03 op }
133 c734c0e9 2021-08-03 op free($2);
134 c734c0e9 2021-08-03 op
135 c734c0e9 2021-08-03 op file = nfile;
136 c734c0e9 2021-08-03 op lungetc('\n');
137 c734c0e9 2021-08-03 op }
138 c734c0e9 2021-08-03 op ;
139 c734c0e9 2021-08-03 op
140 c01463a3 2021-08-04 op const : CONST consti
141 e0b4ca44 2021-08-04 op | CONST '(' optnl mconst ')'
142 c01463a3 2021-08-04 op ;
143 c01463a3 2021-08-04 op
144 c01463a3 2021-08-04 op mconst : consti nl | mconst consti nl ;
145 c01463a3 2021-08-04 op
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);
149 c01463a3 2021-08-04 op free($1);
150 c01463a3 2021-08-04 op free_op($3);
151 c092e54b 2021-08-04 op YYERROR;
152 c092e54b 2021-08-04 op }
153 c01463a3 2021-08-04 op }
154 c01463a3 2021-08-04 op ;
155 c734c0e9 2021-08-03 op
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); } ;
160 c734c0e9 2021-08-03 op
161 c734c0e9 2021-08-03 op /*
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.
164 c734c0e9 2021-08-03 op */
165 acf1403a 2021-08-05 op cexpr : literal | varref | funcall | faccess ;
166 c734c0e9 2021-08-03 op check : cexpr '=' '=' cexpr { $$ = op_cmp_eq($1, $4); } ;
167 c734c0e9 2021-08-03 op
168 340ee97d 2021-08-08 op expr : literal | funcall | varref | check | cast | faccess | vargs ;
169 c734c0e9 2021-08-03 op
170 340ee97d 2021-08-08 op vargs : VARGS { $$ = op_vargs(); } ;
171 340ee97d 2021-08-08 op
172 c734c0e9 2021-08-03 op cast : expr ':' U8 { $$ = op_cast($1, V_U8); }
173 c734c0e9 2021-08-03 op | expr ':' U16 { $$ = op_cast($1, V_U16); }
174 c734c0e9 2021-08-03 op | expr ':' U32 { $$ = op_cast($1, V_U32); }
175 c734c0e9 2021-08-03 op | expr ':' STR { $$ = op_cast($1, V_STR); }
176 acf1403a 2021-08-05 op ;
177 acf1403a 2021-08-05 op
178 acf1403a 2021-08-05 op faccess : varref '.' SYMBOL { $$ = op_faccess($1, $3); }
179 acf1403a 2021-08-05 op | faccess '.' SYMBOL { $$ = op_faccess($1, $3); }
180 c734c0e9 2021-08-03 op ;
181 c734c0e9 2021-08-03 op
182 c734c0e9 2021-08-03 op procname: SYMBOL {
183 c734c0e9 2021-08-03 op if (($$ = proc_by_name($1)) == NULL) {
184 c734c0e9 2021-08-03 op yyerror("unknown proc %s", $1);
185 c734c0e9 2021-08-03 op free($1);
186 c734c0e9 2021-08-03 op YYERROR;
187 c734c0e9 2021-08-03 op }
188 c734c0e9 2021-08-03 op free($1);
189 c734c0e9 2021-08-03 op }
190 c734c0e9 2021-08-03 op ;
191 c734c0e9 2021-08-03 op
192 c734c0e9 2021-08-03 op funcall : procname {
193 d9d02161 2021-08-04 op prepare_funcall();
194 c734c0e9 2021-08-03 op } '(' args optcomma ')' {
195 7d79f46c 2021-08-04 op struct proc *proc;
196 7d79f46c 2021-08-04 op int argc;
197 7d79f46c 2021-08-04 op
198 d9d02161 2021-08-04 op $$ = op_funcall($1);
199 7d79f46c 2021-08-04 op proc = $$->v.funcall.proc;
200 7d79f46c 2021-08-04 op argc = $$->v.funcall.argc;
201 7d79f46c 2021-08-04 op
202 7d79f46c 2021-08-04 op if (argc != proc->minargs && !proc->vararg) {
203 7d79f46c 2021-08-04 op yyerror("invalid arity for `%s': want %d arguments "
204 7d79f46c 2021-08-04 op "but %d given.", $1->name, proc->minargs, argc);
205 c092e54b 2021-08-04 op /* TODO: recursively free $$ */
206 c092e54b 2021-08-04 op YYERROR;
207 c092e54b 2021-08-04 op }
208 7d79f46c 2021-08-04 op
209 7d79f46c 2021-08-04 op if (argc < proc->minargs && proc->vararg) {
210 7d79f46c 2021-08-04 op yyerror("invalid arity for `%s': want at least %d "
211 7d79f46c 2021-08-04 op "arguments but %d given.", $1->name, proc->minargs,
212 7d79f46c 2021-08-04 op argc);
213 7d79f46c 2021-08-04 op /* TODO: recursively free $$ */
214 7d79f46c 2021-08-04 op YYERROR;
215 7d79f46c 2021-08-04 op }
216 c734c0e9 2021-08-03 op }
217 c734c0e9 2021-08-03 op ;
218 c734c0e9 2021-08-03 op
219 c734c0e9 2021-08-03 op optcomma: /* empty */ | ',' ;
220 0e62706a 2021-08-04 op
221 0e62706a 2021-08-04 op dots : '.' '.' '.' ;
222 c734c0e9 2021-08-03 op
223 c734c0e9 2021-08-03 op args : /* empty */
224 c734c0e9 2021-08-03 op | args ',' expr { push_arg($3); }
225 0e62706a 2021-08-04 op | args ',' dots { push_arg(op_rest()); }
226 c734c0e9 2021-08-03 op | expr { push_arg($1); }
227 0e62706a 2021-08-04 op | dots { push_arg(op_rest()); }
228 c734c0e9 2021-08-03 op ;
229 c734c0e9 2021-08-03 op
230 c734c0e9 2021-08-03 op proc : PROC SYMBOL {
231 d9d02161 2021-08-04 op prepare_proc();
232 c734c0e9 2021-08-03 op } '(' args ')' {
233 d9d02161 2021-08-04 op if (!proc_setup_body()) {
234 d9d02161 2021-08-04 op yyerror("invalid argument in proc `%s' definition",
235 d9d02161 2021-08-04 op $2);
236 d9d02161 2021-08-04 op free($2);
237 d9d02161 2021-08-04 op YYERROR;
238 d9d02161 2021-08-04 op }
239 c734c0e9 2021-08-03 op } '{' optnl block '}' {
240 d9d02161 2021-08-04 op proc_done($2);
241 c734c0e9 2021-08-03 op }
242 c734c0e9 2021-08-03 op ;
243 c734c0e9 2021-08-03 op
244 c734c0e9 2021-08-03 op block : /* empty */
245 d7be3bcc 2021-08-04 op | block var nl { block_push($2); }
246 d7be3bcc 2021-08-04 op | block funcall nl { block_push($2); }
247 d7be3bcc 2021-08-04 op | block assert nl
248 8250ab19 2021-08-07 op | block sfail nl { block_push($2); }
249 c734c0e9 2021-08-03 op ;
250 c734c0e9 2021-08-03 op
251 8250ab19 2021-08-07 op sfail : SHOULD_FAIL expr { $$ = op_sfail($2, NULL); }
252 8250ab19 2021-08-07 op | SHOULD_FAIL expr ':' STRING { $$ = op_sfail($2, $4); }
253 8250ab19 2021-08-07 op ;
254 8250ab19 2021-08-07 op
255 e0b4ca44 2021-08-04 op assert : ASSERT asserti
256 e0b4ca44 2021-08-04 op | ASSERT '(' optnl massert ')'
257 c734c0e9 2021-08-03 op ;
258 c734c0e9 2021-08-03 op
259 e0b4ca44 2021-08-04 op massert : asserti nl | massert asserti nl ;
260 e0b4ca44 2021-08-04 op
261 e0b4ca44 2021-08-04 op asserti : check { block_push(op_assert($1)); }
262 c734c0e9 2021-08-03 op ;
263 c734c0e9 2021-08-03 op
264 c734c0e9 2021-08-03 op test : TESTING STRING DIR STRING {
265 d9d02161 2021-08-04 op prepare_test();
266 0b453b63 2021-08-07 op } testopt '{' optnl block '}' {
267 0b453b63 2021-08-07 op test_done(shouldfail, $2, $4);
268 0b453b63 2021-08-07 op shouldfail = 0;
269 c734c0e9 2021-08-03 op }
270 0b453b63 2021-08-07 op ;
271 0b453b63 2021-08-07 op
272 0b453b63 2021-08-07 op testopt : /* empty */
273 0b453b63 2021-08-07 op | SHOULD_FAIL { shouldfail = 1; }
274 c734c0e9 2021-08-03 op ;
275 c734c0e9 2021-08-03 op
276 c734c0e9 2021-08-03 op %%
277 c734c0e9 2021-08-03 op
278 c734c0e9 2021-08-03 op struct keywords {
279 c734c0e9 2021-08-03 op const char *k_name;
280 c734c0e9 2021-08-03 op int k_val;
281 c734c0e9 2021-08-03 op };
282 c734c0e9 2021-08-03 op
283 c734c0e9 2021-08-03 op int
284 c734c0e9 2021-08-03 op yyerror(const char *fmt, ...)
285 c734c0e9 2021-08-03 op {
286 c734c0e9 2021-08-03 op va_list ap;
287 c734c0e9 2021-08-03 op char *msg;
288 c734c0e9 2021-08-03 op
289 c734c0e9 2021-08-03 op file->errors++;
290 c734c0e9 2021-08-03 op va_start(ap, fmt);
291 c734c0e9 2021-08-03 op if (vasprintf(&msg, fmt, ap) == -1)
292 c734c0e9 2021-08-03 op fatalx("yyerror vasprintf");
293 c734c0e9 2021-08-03 op va_end(ap);
294 c734c0e9 2021-08-03 op logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
295 c734c0e9 2021-08-03 op free(msg);
296 c734c0e9 2021-08-03 op return 0;
297 c734c0e9 2021-08-03 op }
298 c734c0e9 2021-08-03 op
299 c734c0e9 2021-08-03 op int
300 c734c0e9 2021-08-03 op kw_cmp(const void *k, const void *e)
301 c734c0e9 2021-08-03 op {
302 c734c0e9 2021-08-03 op return strcmp(k, ((const struct keywords *)e)->k_name);
303 c734c0e9 2021-08-03 op }
304 c734c0e9 2021-08-03 op
305 c734c0e9 2021-08-03 op int
306 c734c0e9 2021-08-03 op lookup(char *s)
307 c734c0e9 2021-08-03 op {
308 c734c0e9 2021-08-03 op /* This has to be sorted always. */
309 c734c0e9 2021-08-03 op static const struct keywords keywords[] = {
310 c734c0e9 2021-08-03 op {"assert", ASSERT},
311 c734c0e9 2021-08-03 op {"const", CONST},
312 c734c0e9 2021-08-03 op {"dir", DIR},
313 c734c0e9 2021-08-03 op {"include", INCLUDE},
314 c734c0e9 2021-08-03 op {"proc", PROC},
315 c734c0e9 2021-08-03 op {"repeat", REPEAT},
316 0b453b63 2021-08-07 op {"should-fail", SHOULD_FAIL},
317 c734c0e9 2021-08-03 op {"str", STR},
318 c734c0e9 2021-08-03 op {"testing", TESTING},
319 c734c0e9 2021-08-03 op {"u16", U16},
320 c734c0e9 2021-08-03 op {"u32", U32},
321 d7786ef0 2021-08-04 op {"u8", U8},
322 340ee97d 2021-08-08 op {"vargs", VARGS},
323 c734c0e9 2021-08-03 op };
324 c734c0e9 2021-08-03 op const struct keywords *p;
325 c734c0e9 2021-08-03 op
326 c734c0e9 2021-08-03 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
327 c734c0e9 2021-08-03 op sizeof(keywords[0]), kw_cmp);
328 c734c0e9 2021-08-03 op
329 c734c0e9 2021-08-03 op if (p)
330 c734c0e9 2021-08-03 op return p->k_val;
331 c734c0e9 2021-08-03 op else
332 c734c0e9 2021-08-03 op return SYMBOL;
333 c734c0e9 2021-08-03 op }
334 c734c0e9 2021-08-03 op
335 c734c0e9 2021-08-03 op #define START_EXPAND 1
336 c734c0e9 2021-08-03 op #define DONE_EXPAND 2
337 c734c0e9 2021-08-03 op
338 c734c0e9 2021-08-03 op static int expanding;
339 c734c0e9 2021-08-03 op
340 c734c0e9 2021-08-03 op int
341 c734c0e9 2021-08-03 op igetc(void)
342 c734c0e9 2021-08-03 op {
343 c734c0e9 2021-08-03 op int c;
344 c734c0e9 2021-08-03 op
345 c734c0e9 2021-08-03 op while (1) {
346 c734c0e9 2021-08-03 op if (file->ungetpos > 0)
347 c734c0e9 2021-08-03 op c = file->ungetbuf[--file->ungetpos];
348 c734c0e9 2021-08-03 op else
349 c734c0e9 2021-08-03 op c = getc(file->stream);
350 c734c0e9 2021-08-03 op
351 c734c0e9 2021-08-03 op if (c == START_EXPAND)
352 c734c0e9 2021-08-03 op expanding = 1;
353 c734c0e9 2021-08-03 op else if (c == DONE_EXPAND)
354 c734c0e9 2021-08-03 op expanding = 0;
355 c734c0e9 2021-08-03 op else
356 c734c0e9 2021-08-03 op break;
357 c734c0e9 2021-08-03 op }
358 c734c0e9 2021-08-03 op return c;
359 c734c0e9 2021-08-03 op }
360 c734c0e9 2021-08-03 op
361 c734c0e9 2021-08-03 op int
362 c734c0e9 2021-08-03 op lgetc(int quotec)
363 c734c0e9 2021-08-03 op {
364 c734c0e9 2021-08-03 op int c, next;
365 c734c0e9 2021-08-03 op
366 c734c0e9 2021-08-03 op if (quotec) {
367 c734c0e9 2021-08-03 op if ((c = igetc()) == EOF) {
368 c734c0e9 2021-08-03 op yyerror("reached end of file while parsing "
369 c734c0e9 2021-08-03 op "quoted string");
370 c734c0e9 2021-08-03 op if (file == topfile || popfile() == EOF)
371 c734c0e9 2021-08-03 op return EOF;
372 c734c0e9 2021-08-03 op return quotec;
373 c734c0e9 2021-08-03 op }
374 c734c0e9 2021-08-03 op return c;
375 c734c0e9 2021-08-03 op }
376 c734c0e9 2021-08-03 op
377 c734c0e9 2021-08-03 op while ((c = igetc()) == '\\') {
378 c734c0e9 2021-08-03 op next = igetc();
379 c734c0e9 2021-08-03 op if (next != '\n') {
380 c734c0e9 2021-08-03 op c = next;
381 c734c0e9 2021-08-03 op break;
382 c734c0e9 2021-08-03 op }
383 c734c0e9 2021-08-03 op yylval.lineno = file->lineno;
384 c734c0e9 2021-08-03 op file->lineno++;
385 c734c0e9 2021-08-03 op }
386 c734c0e9 2021-08-03 op
387 c734c0e9 2021-08-03 op if (c == EOF) {
388 c734c0e9 2021-08-03 op /*
389 c734c0e9 2021-08-03 op * Fake EOL when hit EOF for the first time. This gets line
390 c734c0e9 2021-08-03 op * count right if last line in included file is syntactically
391 c734c0e9 2021-08-03 op * invalid and has no newline.
392 c734c0e9 2021-08-03 op */
393 c734c0e9 2021-08-03 op if (file->eof_reached == 0) {
394 c734c0e9 2021-08-03 op file->eof_reached = 1;
395 c734c0e9 2021-08-03 op return '\n';
396 c734c0e9 2021-08-03 op }
397 c734c0e9 2021-08-03 op while (c == EOF) {
398 c734c0e9 2021-08-03 op if (file == topfile || popfile() == EOF)
399 c734c0e9 2021-08-03 op return EOF;
400 c734c0e9 2021-08-03 op c = igetc();
401 c734c0e9 2021-08-03 op }
402 c734c0e9 2021-08-03 op }
403 c734c0e9 2021-08-03 op return c;
404 c734c0e9 2021-08-03 op }
405 c734c0e9 2021-08-03 op
406 c734c0e9 2021-08-03 op void
407 c734c0e9 2021-08-03 op lungetc(int c)
408 c734c0e9 2021-08-03 op {
409 c734c0e9 2021-08-03 op if (c == EOF)
410 c734c0e9 2021-08-03 op return;
411 c734c0e9 2021-08-03 op
412 c734c0e9 2021-08-03 op if (file->ungetpos >= file->ungetsize) {
413 c734c0e9 2021-08-03 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
414 c734c0e9 2021-08-03 op if (p == NULL)
415 c734c0e9 2021-08-03 op err(1, "lungetc");
416 c734c0e9 2021-08-03 op file->ungetbuf = p;
417 c734c0e9 2021-08-03 op file->ungetsize *= 2;
418 c734c0e9 2021-08-03 op }
419 c734c0e9 2021-08-03 op file->ungetbuf[file->ungetpos++] = c;
420 c734c0e9 2021-08-03 op }
421 c734c0e9 2021-08-03 op
422 c734c0e9 2021-08-03 op int
423 c734c0e9 2021-08-03 op findeol(void)
424 c734c0e9 2021-08-03 op {
425 c734c0e9 2021-08-03 op int c;
426 c734c0e9 2021-08-03 op
427 c734c0e9 2021-08-03 op /* Skip to either EOF or the first real EOL. */
428 c734c0e9 2021-08-03 op while (1) {
429 c734c0e9 2021-08-03 op c = lgetc(0);
430 c734c0e9 2021-08-03 op if (c == '\n') {
431 c734c0e9 2021-08-03 op file->lineno++;
432 c734c0e9 2021-08-03 op break;
433 c734c0e9 2021-08-03 op }
434 c734c0e9 2021-08-03 op if (c == EOF)
435 c734c0e9 2021-08-03 op break;
436 c734c0e9 2021-08-03 op }
437 c734c0e9 2021-08-03 op return ERROR;
438 c734c0e9 2021-08-03 op }
439 c734c0e9 2021-08-03 op
440 c734c0e9 2021-08-03 op
441 c734c0e9 2021-08-03 op #if 0
442 c734c0e9 2021-08-03 op int my_yylex(void);
443 c734c0e9 2021-08-03 op
444 c734c0e9 2021-08-03 op int
445 c734c0e9 2021-08-03 op yylex(void)
446 c734c0e9 2021-08-03 op {
447 c734c0e9 2021-08-03 op int x;
448 c734c0e9 2021-08-03 op
449 c734c0e9 2021-08-03 op switch (x = my_yylex()) {
450 c734c0e9 2021-08-03 op case ASSERT: puts("assert"); break;
451 c734c0e9 2021-08-03 op case CONST: puts("const"); break;
452 c734c0e9 2021-08-03 op case DIR: puts("dir"); break;
453 c734c0e9 2021-08-03 op case ERROR: puts("error"); break;
454 c734c0e9 2021-08-03 op case INCLUDE: puts("include"); break;
455 c734c0e9 2021-08-03 op case PROC: puts("proc"); break;
456 c734c0e9 2021-08-03 op case REPEAT: puts("repeat"); break;
457 c734c0e9 2021-08-03 op case STR: puts(":str"); break;
458 c734c0e9 2021-08-03 op case TESTING: puts("testing"); break;
459 c734c0e9 2021-08-03 op case U8: puts(":u8"); break;
460 c734c0e9 2021-08-03 op case U16: puts(":u16"); break;
461 c734c0e9 2021-08-03 op case U32: puts(":u32"); break;
462 c734c0e9 2021-08-03 op
463 c734c0e9 2021-08-03 op case STRING: printf("string \"%s\"\n", yylval.v.str); break;
464 c734c0e9 2021-08-03 op case SYMBOL: printf("symbol %s\n", yylval.v.str); break;
465 c734c0e9 2021-08-03 op case NUMBER: printf("number %"PRIu64"\n", yylval.v.num); break;
466 c734c0e9 2021-08-03 op
467 c734c0e9 2021-08-03 op default:
468 c734c0e9 2021-08-03 op printf("character ");
469 c734c0e9 2021-08-03 op if (x == '\n')
470 c734c0e9 2021-08-03 op printf("\\n");
471 c734c0e9 2021-08-03 op else
472 c734c0e9 2021-08-03 op printf("%c", x);
473 c734c0e9 2021-08-03 op printf(" [0x%x]", x);
474 c734c0e9 2021-08-03 op printf("\n");
475 c734c0e9 2021-08-03 op break;
476 c734c0e9 2021-08-03 op }
477 c734c0e9 2021-08-03 op
478 c734c0e9 2021-08-03 op return x;
479 c734c0e9 2021-08-03 op }
480 c734c0e9 2021-08-03 op
481 c734c0e9 2021-08-03 op int
482 c734c0e9 2021-08-03 op my_yylex(void)
483 c734c0e9 2021-08-03 op #else
484 c734c0e9 2021-08-03 op int
485 c734c0e9 2021-08-03 op yylex(void)
486 c734c0e9 2021-08-03 op #endif
487 c734c0e9 2021-08-03 op {
488 c734c0e9 2021-08-03 op unsigned char buf[8096];
489 c734c0e9 2021-08-03 op unsigned char *p;
490 c734c0e9 2021-08-03 op int quotec, next, c;
491 c734c0e9 2021-08-03 op int token;
492 c734c0e9 2021-08-03 op
493 c734c0e9 2021-08-03 op p = buf;
494 c734c0e9 2021-08-03 op while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\f')
495 c734c0e9 2021-08-03 op ; /* nop */
496 c734c0e9 2021-08-03 op
497 c734c0e9 2021-08-03 op yylval.lineno = file->lineno;
498 c734c0e9 2021-08-03 op if (c == '#')
499 c734c0e9 2021-08-03 op while ((c = lgetc(0)) != '\n' && c != EOF)
500 c734c0e9 2021-08-03 op ; /* nop */
501 c734c0e9 2021-08-03 op
502 c734c0e9 2021-08-03 op switch (c) {
503 1433867a 2021-08-04 op case ':':
504 1433867a 2021-08-04 op return c;
505 1433867a 2021-08-04 op break;
506 c734c0e9 2021-08-03 op case '\'':
507 c734c0e9 2021-08-03 op case '\"':
508 c734c0e9 2021-08-03 op quotec = c;
509 c734c0e9 2021-08-03 op while (1) {
510 c734c0e9 2021-08-03 op if ((c = lgetc(quotec)) == EOF)
511 c734c0e9 2021-08-03 op return 0;
512 c734c0e9 2021-08-03 op if (c == '\n') {
513 c734c0e9 2021-08-03 op file->lineno++;
514 c734c0e9 2021-08-03 op continue;
515 c734c0e9 2021-08-03 op } else if (c == '\\') {
516 c734c0e9 2021-08-03 op if ((next = lgetc(quotec)) == EOF)
517 c734c0e9 2021-08-03 op return 0;
518 c734c0e9 2021-08-03 op if (next == quotec || next == ' ' ||
519 c734c0e9 2021-08-03 op next == '\t')
520 c734c0e9 2021-08-03 op c = next;
521 c734c0e9 2021-08-03 op else if (next == '\n') {
522 c734c0e9 2021-08-03 op file->lineno++;
523 c734c0e9 2021-08-03 op continue;
524 c734c0e9 2021-08-03 op } else
525 c734c0e9 2021-08-03 op lungetc(next);
526 c734c0e9 2021-08-03 op } else if (c == quotec) {
527 c734c0e9 2021-08-03 op *p = '\0';
528 c734c0e9 2021-08-03 op break;
529 c734c0e9 2021-08-03 op } else if (c == '\0') {
530 c734c0e9 2021-08-03 op yyerror("syntax error");
531 c734c0e9 2021-08-03 op return findeol();
532 c734c0e9 2021-08-03 op }
533 c734c0e9 2021-08-03 op
534 c734c0e9 2021-08-03 op if (p + 1 >= buf + sizeof(buf) - 1) {
535 c734c0e9 2021-08-03 op yyerror("string too long");
536 c734c0e9 2021-08-03 op return findeol();
537 c734c0e9 2021-08-03 op }
538 c734c0e9 2021-08-03 op
539 c734c0e9 2021-08-03 op *p++ = c;
540 c734c0e9 2021-08-03 op }
541 c734c0e9 2021-08-03 op
542 c734c0e9 2021-08-03 op yylval.v.str = xstrdup(buf);
543 c734c0e9 2021-08-03 op return STRING;
544 c734c0e9 2021-08-03 op }
545 c734c0e9 2021-08-03 op
546 c734c0e9 2021-08-03 op #define allowed_to_end_number(x) \
547 c734c0e9 2021-08-03 op (isspace(x) || x == ')' || x == ',' || x == '/' || x == '}' \
548 1433867a 2021-08-04 op || x == '=' || x == ':')
549 c734c0e9 2021-08-03 op
550 c734c0e9 2021-08-03 op if (c == '-' || isdigit(c)) {
551 c734c0e9 2021-08-03 op do {
552 c734c0e9 2021-08-03 op *p++ = c;
553 c734c0e9 2021-08-03 op if ((size_t)(p-buf) >= sizeof(buf)) {
554 c734c0e9 2021-08-03 op yyerror("string too long");
555 c734c0e9 2021-08-03 op return findeol();
556 c734c0e9 2021-08-03 op }
557 ef33a69e 2021-08-07 op } while ((c = lgetc(0)) != EOF && (isdigit(c) || c == 'x'));
558 c734c0e9 2021-08-03 op lungetc(c);
559 c734c0e9 2021-08-03 op if (p == buf + 1 && buf[0] == '-')
560 c734c0e9 2021-08-03 op goto nodigits;
561 c734c0e9 2021-08-03 op if (c == EOF || allowed_to_end_number(c)) {
562 ef33a69e 2021-08-07 op char *ep;
563 c734c0e9 2021-08-03 op
564 c734c0e9 2021-08-03 op *p = '\0';
565 ef33a69e 2021-08-07 op errno = 0;
566 ef33a69e 2021-08-07 op yylval.v.num = strtoll(buf, &ep, 0);
567 7abcb0db 2021-12-15 op if (*ep != '\0' || (errno == ERANGE &&
568 ef33a69e 2021-08-07 op (yylval.v.num == LONG_MAX ||
569 7abcb0db 2021-12-15 op yylval.v.num == LONG_MIN))) {
570 ef33a69e 2021-08-07 op yyerror("\"%s\" invalid number or out of range",
571 ef33a69e 2021-08-07 op buf);
572 c734c0e9 2021-08-03 op return findeol();
573 c734c0e9 2021-08-03 op }
574 ef33a69e 2021-08-07 op
575 c734c0e9 2021-08-03 op return NUMBER;
576 c734c0e9 2021-08-03 op } else {
577 c734c0e9 2021-08-03 op nodigits:
578 c734c0e9 2021-08-03 op while (p > buf + 1)
579 c734c0e9 2021-08-03 op lungetc(*--p);
580 c734c0e9 2021-08-03 op c = *--p;
581 c734c0e9 2021-08-03 op if (c == '-')
582 c734c0e9 2021-08-03 op return c;
583 c734c0e9 2021-08-03 op }
584 c734c0e9 2021-08-03 op }
585 c734c0e9 2021-08-03 op
586 c734c0e9 2021-08-03 op #define allowed_in_symbol(x) \
587 c734c0e9 2021-08-03 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
588 c734c0e9 2021-08-03 op x != '{' && x != '}' && \
589 c734c0e9 2021-08-03 op x != '!' && x != '=' && \
590 e48a50b7 2021-08-07 op x != '#' && x != ',' && \
591 02fc4f51 2021-08-07 op x != '.' && x != ':'))
592 c734c0e9 2021-08-03 op
593 c734c0e9 2021-08-03 op if (isalnum(c) || c == ':' || c == '_') {
594 c734c0e9 2021-08-03 op do {
595 c734c0e9 2021-08-03 op *p++ = c;
596 c734c0e9 2021-08-03 op if ((size_t)(p-buf) >= sizeof(buf)) {
597 c734c0e9 2021-08-03 op yyerror("string too long");
598 c734c0e9 2021-08-03 op return findeol();
599 c734c0e9 2021-08-03 op }
600 c734c0e9 2021-08-03 op } while ((c = lgetc(0)) != EOF && (allowed_in_symbol(c)));
601 c734c0e9 2021-08-03 op lungetc(c);
602 c734c0e9 2021-08-03 op *p = '\0';
603 c734c0e9 2021-08-03 op if ((token = lookup(buf)) == SYMBOL)
604 c734c0e9 2021-08-03 op yylval.v.str = xstrdup(buf);
605 c734c0e9 2021-08-03 op return token;
606 c734c0e9 2021-08-03 op }
607 c734c0e9 2021-08-03 op
608 c734c0e9 2021-08-03 op if (c == '\n') {
609 c734c0e9 2021-08-03 op yylval.lineno = file->lineno;
610 c734c0e9 2021-08-03 op file->lineno++;
611 c734c0e9 2021-08-03 op }
612 c734c0e9 2021-08-03 op if (c == EOF)
613 c734c0e9 2021-08-03 op return 0;
614 c734c0e9 2021-08-03 op return c;
615 c734c0e9 2021-08-03 op }
616 c734c0e9 2021-08-03 op
617 c734c0e9 2021-08-03 op struct file *
618 c734c0e9 2021-08-03 op pushfile(const char *name)
619 c734c0e9 2021-08-03 op {
620 c734c0e9 2021-08-03 op struct file *nfile;
621 c734c0e9 2021-08-03 op
622 c734c0e9 2021-08-03 op if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
623 c734c0e9 2021-08-03 op log_warn("calloc");
624 c734c0e9 2021-08-03 op return NULL;
625 c734c0e9 2021-08-03 op }
626 c734c0e9 2021-08-03 op if ((nfile->name = strdup(name)) == NULL) {
627 c734c0e9 2021-08-03 op log_warn("strdup");
628 c734c0e9 2021-08-03 op free(nfile);
629 c734c0e9 2021-08-03 op return NULL;
630 c734c0e9 2021-08-03 op }
631 c734c0e9 2021-08-03 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
632 c734c0e9 2021-08-03 op log_warn("%s", nfile->name);
633 c734c0e9 2021-08-03 op free(nfile->name);
634 c734c0e9 2021-08-03 op free(nfile);
635 c734c0e9 2021-08-03 op return NULL;
636 c734c0e9 2021-08-03 op }
637 c734c0e9 2021-08-03 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
638 c734c0e9 2021-08-03 op nfile->ungetsize = 16;
639 c734c0e9 2021-08-03 op nfile->ungetbuf = malloc(nfile->ungetsize);
640 c734c0e9 2021-08-03 op if (nfile->ungetbuf == NULL) {
641 c734c0e9 2021-08-03 op log_warn("malloc");
642 c734c0e9 2021-08-03 op fclose(nfile->stream);
643 c734c0e9 2021-08-03 op free(nfile->name);
644 c734c0e9 2021-08-03 op free(nfile);
645 c734c0e9 2021-08-03 op return NULL;
646 c734c0e9 2021-08-03 op }
647 c734c0e9 2021-08-03 op TAILQ_INSERT_TAIL(&files, nfile, entry);
648 c734c0e9 2021-08-03 op return nfile;
649 c734c0e9 2021-08-03 op }
650 c734c0e9 2021-08-03 op
651 c734c0e9 2021-08-03 op int
652 c734c0e9 2021-08-03 op popfile(void)
653 c734c0e9 2021-08-03 op {
654 c734c0e9 2021-08-03 op struct file *prev;
655 c734c0e9 2021-08-03 op
656 c734c0e9 2021-08-03 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
657 c734c0e9 2021-08-03 op prev->errors += file->errors;
658 c734c0e9 2021-08-03 op
659 c734c0e9 2021-08-03 op TAILQ_REMOVE(&files, file, entry);
660 c734c0e9 2021-08-03 op fclose(file->stream);
661 c734c0e9 2021-08-03 op free(file->name);
662 c734c0e9 2021-08-03 op free(file->ungetbuf);
663 c734c0e9 2021-08-03 op free(file);
664 c734c0e9 2021-08-03 op file = prev;
665 c734c0e9 2021-08-03 op return file ? 0 : EOF;
666 c734c0e9 2021-08-03 op }
667 c734c0e9 2021-08-03 op
668 c734c0e9 2021-08-03 op void
669 c734c0e9 2021-08-03 op loadfile(const char *path)
670 c734c0e9 2021-08-03 op {
671 c734c0e9 2021-08-03 op int errors;
672 c734c0e9 2021-08-03 op
673 c734c0e9 2021-08-03 op file = pushfile(path);
674 c734c0e9 2021-08-03 op if (file == NULL)
675 c734c0e9 2021-08-03 op err(1, "pushfile");
676 c734c0e9 2021-08-03 op topfile = file;
677 c734c0e9 2021-08-03 op
678 c734c0e9 2021-08-03 op yyparse();
679 c734c0e9 2021-08-03 op errors = file->errors;
680 c734c0e9 2021-08-03 op popfile();
681 c734c0e9 2021-08-03 op
682 c734c0e9 2021-08-03 op if (errors)
683 c734c0e9 2021-08-03 op errx(1, "can't load %s because of errors", path);
684 c734c0e9 2021-08-03 op }