Blame


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