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