Blame


1 6509b181 2022-12-30 thomas /*
2 96b8c570 2022-12-30 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 6509b181 2022-12-30 thomas * Copyright (c) 2007-2016 Reyk Floeter <reyk@openbsd.org>
4 6509b181 2022-12-30 thomas * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 6509b181 2022-12-30 thomas * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 6509b181 2022-12-30 thomas * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 6509b181 2022-12-30 thomas * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 6509b181 2022-12-30 thomas * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 6509b181 2022-12-30 thomas * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 6509b181 2022-12-30 thomas *
11 6509b181 2022-12-30 thomas * Permission to use, copy, modify, and distribute this software for any
12 6509b181 2022-12-30 thomas * purpose with or without fee is hereby granted, provided that the above
13 6509b181 2022-12-30 thomas * copyright notice and this permission notice appear in all copies.
14 6509b181 2022-12-30 thomas *
15 6509b181 2022-12-30 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 6509b181 2022-12-30 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 6509b181 2022-12-30 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 6509b181 2022-12-30 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 6509b181 2022-12-30 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 6509b181 2022-12-30 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 6509b181 2022-12-30 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 6509b181 2022-12-30 thomas */
23 6509b181 2022-12-30 thomas
24 6509b181 2022-12-30 thomas %{
25 6509b181 2022-12-30 thomas
26 6509b181 2022-12-30 thomas #include <sys/queue.h>
27 6509b181 2022-12-30 thomas
28 6509b181 2022-12-30 thomas #include <ctype.h>
29 6509b181 2022-12-30 thomas #include <err.h>
30 6509b181 2022-12-30 thomas #include <stdio.h>
31 6509b181 2022-12-30 thomas #include <stdlib.h>
32 6509b181 2022-12-30 thomas #include <stdarg.h>
33 6509b181 2022-12-30 thomas #include <stdint.h>
34 6509b181 2022-12-30 thomas #include <string.h>
35 6509b181 2022-12-30 thomas #include <unistd.h>
36 cb11302c 2022-12-30 thomas
37 cb11302c 2022-12-30 thomas #include "got_compat.h"
38 6509b181 2022-12-30 thomas
39 6509b181 2022-12-30 thomas #ifndef nitems
40 6509b181 2022-12-30 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 6509b181 2022-12-30 thomas #endif
42 6509b181 2022-12-30 thomas
43 6509b181 2022-12-30 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
44 6509b181 2022-12-30 thomas static struct file {
45 6509b181 2022-12-30 thomas TAILQ_ENTRY(file) entry;
46 6509b181 2022-12-30 thomas FILE *stream;
47 6509b181 2022-12-30 thomas char *name;
48 6509b181 2022-12-30 thomas size_t ungetpos;
49 6509b181 2022-12-30 thomas size_t ungetsize;
50 6509b181 2022-12-30 thomas unsigned char *ungetbuf;
51 6509b181 2022-12-30 thomas int eof_reached;
52 6509b181 2022-12-30 thomas int lineno;
53 6509b181 2022-12-30 thomas int errors;
54 6509b181 2022-12-30 thomas } *file, *topfile;
55 6509b181 2022-12-30 thomas int parse(FILE *, const char *);
56 6509b181 2022-12-30 thomas struct file *pushfile(const char *, int);
57 6509b181 2022-12-30 thomas int popfile(void);
58 6509b181 2022-12-30 thomas int yyparse(void);
59 6509b181 2022-12-30 thomas int yylex(void);
60 6509b181 2022-12-30 thomas int yyerror(const char *, ...)
61 6509b181 2022-12-30 thomas __attribute__((__format__ (printf, 1, 2)))
62 6509b181 2022-12-30 thomas __attribute__((__nonnull__ (1)));
63 6509b181 2022-12-30 thomas int kw_cmp(const void *, const void *);
64 6509b181 2022-12-30 thomas int lookup(char *);
65 6509b181 2022-12-30 thomas int igetc(void);
66 6509b181 2022-12-30 thomas int lgetc(int);
67 6509b181 2022-12-30 thomas void lungetc(int);
68 6509b181 2022-12-30 thomas int findeol(void);
69 6509b181 2022-12-30 thomas
70 6509b181 2022-12-30 thomas void dbg(void);
71 6509b181 2022-12-30 thomas void printq(const char *);
72 6509b181 2022-12-30 thomas
73 6509b181 2022-12-30 thomas extern int nodebug;
74 6509b181 2022-12-30 thomas
75 6509b181 2022-12-30 thomas static FILE *fp;
76 6509b181 2022-12-30 thomas
77 6509b181 2022-12-30 thomas static int block;
78 6509b181 2022-12-30 thomas static int in_define;
79 6509b181 2022-12-30 thomas static int errors;
80 6509b181 2022-12-30 thomas static int lastline = -1;
81 6509b181 2022-12-30 thomas
82 6509b181 2022-12-30 thomas typedef struct {
83 6509b181 2022-12-30 thomas union {
84 6509b181 2022-12-30 thomas char *string;
85 6509b181 2022-12-30 thomas } v;
86 6509b181 2022-12-30 thomas int lineno;
87 6509b181 2022-12-30 thomas } YYSTYPE;
88 6509b181 2022-12-30 thomas
89 6509b181 2022-12-30 thomas %}
90 6509b181 2022-12-30 thomas
91 6509b181 2022-12-30 thomas %token DEFINE ELSE END ERROR FINALLY FOR IF INCLUDE PRINTF
92 3594b24d 2023-01-06 thomas %token RENDER TQFOREACH UNSAFE URLESCAPE WHILE
93 6509b181 2022-12-30 thomas %token <v.string> STRING
94 d28713db 2023-09-12 thomas %type <v.string> string nstring
95 6509b181 2022-12-30 thomas %type <v.string> stringy
96 6509b181 2022-12-30 thomas
97 6509b181 2022-12-30 thomas %%
98 6509b181 2022-12-30 thomas
99 6509b181 2022-12-30 thomas grammar : /* empty */
100 6509b181 2022-12-30 thomas | grammar include
101 6509b181 2022-12-30 thomas | grammar verbatim
102 6509b181 2022-12-30 thomas | grammar block
103 6509b181 2022-12-30 thomas | grammar error { file->errors++; }
104 6509b181 2022-12-30 thomas ;
105 6509b181 2022-12-30 thomas
106 6509b181 2022-12-30 thomas include : INCLUDE STRING {
107 6509b181 2022-12-30 thomas struct file *nfile;
108 6509b181 2022-12-30 thomas
109 6509b181 2022-12-30 thomas if ((nfile = pushfile($2, 0)) == NULL) {
110 6509b181 2022-12-30 thomas yyerror("failed to include file %s", $2);
111 6509b181 2022-12-30 thomas free($2);
112 6509b181 2022-12-30 thomas YYERROR;
113 6509b181 2022-12-30 thomas }
114 6509b181 2022-12-30 thomas free($2);
115 6509b181 2022-12-30 thomas
116 6509b181 2022-12-30 thomas file = nfile;
117 6509b181 2022-12-30 thomas lungetc('\n');
118 6509b181 2022-12-30 thomas }
119 6509b181 2022-12-30 thomas ;
120 6509b181 2022-12-30 thomas
121 6509b181 2022-12-30 thomas verbatim : '!' verbatim1 '!' {
122 6509b181 2022-12-30 thomas if (in_define) {
123 6509b181 2022-12-30 thomas /* TODO: check template status and exit in case */
124 6509b181 2022-12-30 thomas }
125 6509b181 2022-12-30 thomas }
126 6509b181 2022-12-30 thomas ;
127 6509b181 2022-12-30 thomas
128 6509b181 2022-12-30 thomas verbatim1 : /* empty */
129 6509b181 2022-12-30 thomas | verbatim1 STRING {
130 6509b181 2022-12-30 thomas if (*$2 != '\0') {
131 6509b181 2022-12-30 thomas dbg();
132 6509b181 2022-12-30 thomas fprintf(fp, "%s\n", $2);
133 6509b181 2022-12-30 thomas }
134 6509b181 2022-12-30 thomas free($2);
135 6509b181 2022-12-30 thomas }
136 6509b181 2022-12-30 thomas ;
137 6509b181 2022-12-30 thomas
138 6509b181 2022-12-30 thomas verbatims : /* empty */
139 6509b181 2022-12-30 thomas | verbatims verbatim
140 6509b181 2022-12-30 thomas ;
141 6509b181 2022-12-30 thomas
142 d28713db 2023-09-12 thomas raw : nstring {
143 6509b181 2022-12-30 thomas dbg();
144 d8bf4f25 2023-09-14 thomas fprintf(fp, "if ((tp_ret = tp_write(tp, ");
145 6509b181 2022-12-30 thomas printq($1);
146 d8bf4f25 2023-09-14 thomas fprintf(fp, ", %zu)) == -1) goto err;\n",
147 d8bf4f25 2023-09-14 thomas strlen($1));
148 6509b181 2022-12-30 thomas
149 6509b181 2022-12-30 thomas free($1);
150 6509b181 2022-12-30 thomas }
151 6509b181 2022-12-30 thomas ;
152 6509b181 2022-12-30 thomas
153 6509b181 2022-12-30 thomas block : define body end {
154 6509b181 2022-12-30 thomas fputs("err:\n", fp);
155 6509b181 2022-12-30 thomas fputs("return tp_ret;\n", fp);
156 6509b181 2022-12-30 thomas fputs("}\n", fp);
157 6509b181 2022-12-30 thomas in_define = 0;
158 6509b181 2022-12-30 thomas }
159 6509b181 2022-12-30 thomas | define body finally end {
160 6509b181 2022-12-30 thomas fputs("return tp_ret;\n", fp);
161 6509b181 2022-12-30 thomas fputs("}\n", fp);
162 6509b181 2022-12-30 thomas in_define = 0;
163 6509b181 2022-12-30 thomas }
164 6509b181 2022-12-30 thomas ;
165 6509b181 2022-12-30 thomas
166 6509b181 2022-12-30 thomas define : '{' DEFINE string '}' {
167 6509b181 2022-12-30 thomas in_define = 1;
168 6509b181 2022-12-30 thomas
169 6509b181 2022-12-30 thomas dbg();
170 6509b181 2022-12-30 thomas fprintf(fp, "int\n%s\n{\n", $3);
171 6509b181 2022-12-30 thomas fputs("int tp_ret = 0;\n", fp);
172 6509b181 2022-12-30 thomas
173 6509b181 2022-12-30 thomas free($3);
174 6509b181 2022-12-30 thomas }
175 6509b181 2022-12-30 thomas ;
176 6509b181 2022-12-30 thomas
177 6509b181 2022-12-30 thomas body : /* empty */
178 6509b181 2022-12-30 thomas | body verbatim
179 6509b181 2022-12-30 thomas | body raw
180 6509b181 2022-12-30 thomas | body special
181 6509b181 2022-12-30 thomas ;
182 6509b181 2022-12-30 thomas
183 6509b181 2022-12-30 thomas special : '{' RENDER string '}' {
184 6509b181 2022-12-30 thomas dbg();
185 6509b181 2022-12-30 thomas fprintf(fp, "if ((tp_ret = %s) == -1) goto err;\n",
186 6509b181 2022-12-30 thomas $3);
187 6509b181 2022-12-30 thomas free($3);
188 6509b181 2022-12-30 thomas }
189 6509b181 2022-12-30 thomas | printf
190 6509b181 2022-12-30 thomas | if body endif { fputs("}\n", fp); }
191 6509b181 2022-12-30 thomas | loop
192 6509b181 2022-12-30 thomas | '{' string '|' UNSAFE '}' {
193 6509b181 2022-12-30 thomas dbg();
194 6509b181 2022-12-30 thomas fprintf(fp,
195 d8bf4f25 2023-09-14 thomas "if ((tp_ret = tp_writes(tp, %s)) == -1)\n",
196 6509b181 2022-12-30 thomas $2);
197 6509b181 2022-12-30 thomas fputs("goto err;\n", fp);
198 6509b181 2022-12-30 thomas free($2);
199 6509b181 2022-12-30 thomas }
200 6509b181 2022-12-30 thomas | '{' string '|' URLESCAPE '}' {
201 6509b181 2022-12-30 thomas dbg();
202 6509b181 2022-12-30 thomas fprintf(fp,
203 6509b181 2022-12-30 thomas "if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
204 6509b181 2022-12-30 thomas $2);
205 6509b181 2022-12-30 thomas fputs("goto err;\n", fp);
206 6509b181 2022-12-30 thomas free($2);
207 6509b181 2022-12-30 thomas }
208 6509b181 2022-12-30 thomas | '{' string '}' {
209 6509b181 2022-12-30 thomas dbg();
210 6509b181 2022-12-30 thomas fprintf(fp,
211 d8bf4f25 2023-09-14 thomas "if ((tp_ret = tp_htmlescape(tp, %s)) == -1)\n",
212 6509b181 2022-12-30 thomas $2);
213 6509b181 2022-12-30 thomas fputs("goto err;\n", fp);
214 6509b181 2022-12-30 thomas free($2);
215 6509b181 2022-12-30 thomas }
216 6509b181 2022-12-30 thomas ;
217 6509b181 2022-12-30 thomas
218 6509b181 2022-12-30 thomas printf : '{' PRINTF {
219 6509b181 2022-12-30 thomas dbg();
220 6509b181 2022-12-30 thomas fprintf(fp, "if (asprintf(&tp->tp_tmp, ");
221 6509b181 2022-12-30 thomas } printfargs '}' {
222 6509b181 2022-12-30 thomas fputs(") == -1)\n", fp);
223 6509b181 2022-12-30 thomas fputs("goto err;\n", fp);
224 d8bf4f25 2023-09-14 thomas fputs("if ((tp_ret = tp_htmlescape(tp, tp->tp_tmp)) "
225 6509b181 2022-12-30 thomas "== -1)\n", fp);
226 6509b181 2022-12-30 thomas fputs("goto err;\n", fp);
227 6509b181 2022-12-30 thomas fputs("free(tp->tp_tmp);\n", fp);
228 6509b181 2022-12-30 thomas fputs("tp->tp_tmp = NULL;\n", fp);
229 6509b181 2022-12-30 thomas }
230 6509b181 2022-12-30 thomas ;
231 6509b181 2022-12-30 thomas
232 6509b181 2022-12-30 thomas printfargs : /* empty */
233 6509b181 2022-12-30 thomas | printfargs STRING {
234 6509b181 2022-12-30 thomas fprintf(fp, " %s", $2);
235 6509b181 2022-12-30 thomas free($2);
236 6509b181 2022-12-30 thomas }
237 6509b181 2022-12-30 thomas ;
238 6509b181 2022-12-30 thomas
239 6509b181 2022-12-30 thomas if : '{' IF stringy '}' {
240 6509b181 2022-12-30 thomas dbg();
241 6509b181 2022-12-30 thomas fprintf(fp, "if (%s) {\n", $3);
242 6509b181 2022-12-30 thomas free($3);
243 6509b181 2022-12-30 thomas }
244 6509b181 2022-12-30 thomas ;
245 6509b181 2022-12-30 thomas
246 6509b181 2022-12-30 thomas endif : end
247 6509b181 2022-12-30 thomas | else body end
248 6509b181 2022-12-30 thomas | elsif body endif
249 6509b181 2022-12-30 thomas ;
250 6509b181 2022-12-30 thomas
251 6509b181 2022-12-30 thomas elsif : '{' ELSE IF stringy '}' {
252 6509b181 2022-12-30 thomas dbg();
253 6509b181 2022-12-30 thomas fprintf(fp, "} else if (%s) {\n", $4);
254 6509b181 2022-12-30 thomas free($4);
255 6509b181 2022-12-30 thomas }
256 6509b181 2022-12-30 thomas ;
257 6509b181 2022-12-30 thomas
258 6509b181 2022-12-30 thomas else : '{' ELSE '}' {
259 6509b181 2022-12-30 thomas dbg();
260 6509b181 2022-12-30 thomas fputs("} else {\n", fp);
261 6509b181 2022-12-30 thomas }
262 6509b181 2022-12-30 thomas ;
263 6509b181 2022-12-30 thomas
264 6509b181 2022-12-30 thomas loop : '{' FOR stringy '}' {
265 6509b181 2022-12-30 thomas fprintf(fp, "for (%s) {\n", $3);
266 6509b181 2022-12-30 thomas free($3);
267 6509b181 2022-12-30 thomas } body end {
268 6509b181 2022-12-30 thomas fputs("}\n", fp);
269 6509b181 2022-12-30 thomas }
270 6509b181 2022-12-30 thomas | '{' TQFOREACH STRING STRING STRING '}' {
271 6509b181 2022-12-30 thomas fprintf(fp, "TAILQ_FOREACH(%s, %s, %s) {\n",
272 6509b181 2022-12-30 thomas $3, $4, $5);
273 6509b181 2022-12-30 thomas free($3);
274 6509b181 2022-12-30 thomas free($4);
275 6509b181 2022-12-30 thomas free($5);
276 6509b181 2022-12-30 thomas } body end {
277 6509b181 2022-12-30 thomas fputs("}\n", fp);
278 6509b181 2022-12-30 thomas }
279 3594b24d 2023-01-06 thomas | '{' WHILE stringy '}' {
280 3594b24d 2023-01-06 thomas fprintf(fp, "while (%s) {\n", $3);
281 3594b24d 2023-01-06 thomas free($3);
282 3594b24d 2023-01-06 thomas } body end {
283 3594b24d 2023-01-06 thomas fputs("}\n", fp);
284 3594b24d 2023-01-06 thomas }
285 6509b181 2022-12-30 thomas ;
286 6509b181 2022-12-30 thomas
287 6509b181 2022-12-30 thomas end : '{' END '}'
288 6509b181 2022-12-30 thomas ;
289 6509b181 2022-12-30 thomas
290 6509b181 2022-12-30 thomas finally : '{' FINALLY '}' {
291 6509b181 2022-12-30 thomas dbg();
292 6509b181 2022-12-30 thomas fputs("err:\n", fp);
293 6509b181 2022-12-30 thomas } verbatims
294 6509b181 2022-12-30 thomas ;
295 6509b181 2022-12-30 thomas
296 d28713db 2023-09-12 thomas nstring : STRING nstring {
297 d28713db 2023-09-12 thomas if (asprintf(&$$, "%s%s", $1, $2) == -1)
298 d28713db 2023-09-12 thomas err(1, "asprintf");
299 d28713db 2023-09-12 thomas free($1);
300 d28713db 2023-09-12 thomas free($2);
301 d28713db 2023-09-12 thomas }
302 d28713db 2023-09-12 thomas | STRING
303 d28713db 2023-09-12 thomas ;
304 d28713db 2023-09-12 thomas
305 6509b181 2022-12-30 thomas string : STRING string {
306 6509b181 2022-12-30 thomas if (asprintf(&$$, "%s %s", $1, $2) == -1)
307 6509b181 2022-12-30 thomas err(1, "asprintf");
308 6509b181 2022-12-30 thomas free($1);
309 6509b181 2022-12-30 thomas free($2);
310 6509b181 2022-12-30 thomas }
311 6509b181 2022-12-30 thomas | STRING
312 6509b181 2022-12-30 thomas ;
313 6509b181 2022-12-30 thomas
314 6509b181 2022-12-30 thomas stringy : STRING
315 6509b181 2022-12-30 thomas | STRING stringy {
316 6509b181 2022-12-30 thomas if (asprintf(&$$, "%s %s", $1, $2) == -1)
317 6509b181 2022-12-30 thomas err(1, "asprintf");
318 6509b181 2022-12-30 thomas free($1);
319 6509b181 2022-12-30 thomas free($2);
320 6509b181 2022-12-30 thomas }
321 6509b181 2022-12-30 thomas | '|' stringy {
322 6509b181 2022-12-30 thomas if (asprintf(&$$, "|%s", $2) == -1)
323 6509b181 2022-12-30 thomas err(1, "asprintf");
324 6509b181 2022-12-30 thomas free($2);
325 6509b181 2022-12-30 thomas }
326 6509b181 2022-12-30 thomas ;
327 6509b181 2022-12-30 thomas
328 6509b181 2022-12-30 thomas %%
329 6509b181 2022-12-30 thomas
330 6509b181 2022-12-30 thomas struct keywords {
331 6509b181 2022-12-30 thomas const char *k_name;
332 6509b181 2022-12-30 thomas int k_val;
333 6509b181 2022-12-30 thomas };
334 6509b181 2022-12-30 thomas
335 6509b181 2022-12-30 thomas int
336 6509b181 2022-12-30 thomas yyerror(const char *fmt, ...)
337 6509b181 2022-12-30 thomas {
338 6509b181 2022-12-30 thomas va_list ap;
339 6509b181 2022-12-30 thomas char *msg;
340 6509b181 2022-12-30 thomas
341 6509b181 2022-12-30 thomas file->errors++;
342 6509b181 2022-12-30 thomas va_start(ap, fmt);
343 6509b181 2022-12-30 thomas if (vasprintf(&msg, fmt, ap) == -1)
344 6509b181 2022-12-30 thomas err(1, "yyerror vasprintf");
345 6509b181 2022-12-30 thomas va_end(ap);
346 6509b181 2022-12-30 thomas fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
347 6509b181 2022-12-30 thomas free(msg);
348 6509b181 2022-12-30 thomas return (0);
349 6509b181 2022-12-30 thomas }
350 6509b181 2022-12-30 thomas
351 6509b181 2022-12-30 thomas int
352 6509b181 2022-12-30 thomas kw_cmp(const void *k, const void *e)
353 6509b181 2022-12-30 thomas {
354 6509b181 2022-12-30 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
355 6509b181 2022-12-30 thomas }
356 6509b181 2022-12-30 thomas
357 6509b181 2022-12-30 thomas int
358 6509b181 2022-12-30 thomas lookup(char *s)
359 6509b181 2022-12-30 thomas {
360 6509b181 2022-12-30 thomas /* this has to be sorted always */
361 6509b181 2022-12-30 thomas static const struct keywords keywords[] = {
362 6509b181 2022-12-30 thomas { "define", DEFINE },
363 6509b181 2022-12-30 thomas { "else", ELSE },
364 6509b181 2022-12-30 thomas { "end", END },
365 6509b181 2022-12-30 thomas { "finally", FINALLY },
366 6509b181 2022-12-30 thomas { "for", FOR },
367 6509b181 2022-12-30 thomas { "if", IF },
368 6509b181 2022-12-30 thomas { "include", INCLUDE },
369 6509b181 2022-12-30 thomas { "printf", PRINTF },
370 6509b181 2022-12-30 thomas { "render", RENDER },
371 6509b181 2022-12-30 thomas { "tailq-foreach", TQFOREACH },
372 6509b181 2022-12-30 thomas { "unsafe", UNSAFE },
373 6509b181 2022-12-30 thomas { "urlescape", URLESCAPE },
374 3594b24d 2023-01-06 thomas { "while", WHILE },
375 6509b181 2022-12-30 thomas };
376 6509b181 2022-12-30 thomas const struct keywords *p;
377 6509b181 2022-12-30 thomas
378 6509b181 2022-12-30 thomas p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
379 6509b181 2022-12-30 thomas kw_cmp);
380 6509b181 2022-12-30 thomas
381 6509b181 2022-12-30 thomas if (p)
382 6509b181 2022-12-30 thomas return (p->k_val);
383 6509b181 2022-12-30 thomas else
384 6509b181 2022-12-30 thomas return (STRING);
385 6509b181 2022-12-30 thomas }
386 6509b181 2022-12-30 thomas
387 6509b181 2022-12-30 thomas #define START_EXPAND 1
388 6509b181 2022-12-30 thomas #define DONE_EXPAND 2
389 6509b181 2022-12-30 thomas
390 6509b181 2022-12-30 thomas static int expanding;
391 6509b181 2022-12-30 thomas
392 6509b181 2022-12-30 thomas int
393 6509b181 2022-12-30 thomas igetc(void)
394 6509b181 2022-12-30 thomas {
395 6509b181 2022-12-30 thomas int c;
396 6509b181 2022-12-30 thomas
397 6509b181 2022-12-30 thomas while (1) {
398 6509b181 2022-12-30 thomas if (file->ungetpos > 0)
399 6509b181 2022-12-30 thomas c = file->ungetbuf[--file->ungetpos];
400 6509b181 2022-12-30 thomas else
401 6509b181 2022-12-30 thomas c = getc(file->stream);
402 6509b181 2022-12-30 thomas
403 6509b181 2022-12-30 thomas if (c == START_EXPAND)
404 6509b181 2022-12-30 thomas expanding = 1;
405 6509b181 2022-12-30 thomas else if (c == DONE_EXPAND)
406 6509b181 2022-12-30 thomas expanding = 0;
407 6509b181 2022-12-30 thomas else
408 6509b181 2022-12-30 thomas break;
409 6509b181 2022-12-30 thomas }
410 6509b181 2022-12-30 thomas return (c);
411 6509b181 2022-12-30 thomas }
412 6509b181 2022-12-30 thomas
413 6509b181 2022-12-30 thomas int
414 6509b181 2022-12-30 thomas lgetc(int quotec)
415 6509b181 2022-12-30 thomas {
416 6509b181 2022-12-30 thomas int c;
417 6509b181 2022-12-30 thomas
418 6509b181 2022-12-30 thomas if (quotec) {
419 6509b181 2022-12-30 thomas if ((c = igetc()) == EOF) {
420 6509b181 2022-12-30 thomas yyerror("reached end of filewhile parsing "
421 6509b181 2022-12-30 thomas "quoted string");
422 6509b181 2022-12-30 thomas if (file == topfile || popfile() == EOF)
423 6509b181 2022-12-30 thomas return (EOF);
424 6509b181 2022-12-30 thomas return (quotec);
425 6509b181 2022-12-30 thomas }
426 6509b181 2022-12-30 thomas return (c);
427 6509b181 2022-12-30 thomas }
428 6509b181 2022-12-30 thomas
429 6509b181 2022-12-30 thomas c = igetc();
430 6509b181 2022-12-30 thomas if (c == '\t' || c == ' ') {
431 6509b181 2022-12-30 thomas /* Compress blanks to a sigle space. */
432 6509b181 2022-12-30 thomas do {
433 6509b181 2022-12-30 thomas c = getc(file->stream);
434 6509b181 2022-12-30 thomas } while (c == '\t' || c == ' ');
435 6509b181 2022-12-30 thomas ungetc(c, file->stream);
436 6509b181 2022-12-30 thomas c = ' ';
437 6509b181 2022-12-30 thomas }
438 6509b181 2022-12-30 thomas
439 6509b181 2022-12-30 thomas if (c == EOF) {
440 6509b181 2022-12-30 thomas /*
441 6509b181 2022-12-30 thomas * Fake EOL when hit EOF for the first time. This gets line
442 f39a6046 2023-04-01 thomas * count right if last line in included file is syntactically
443 6509b181 2022-12-30 thomas * invalid and has no newline.
444 6509b181 2022-12-30 thomas */
445 6509b181 2022-12-30 thomas if (file->eof_reached == 0) {
446 6509b181 2022-12-30 thomas file->eof_reached = 1;
447 6509b181 2022-12-30 thomas return ('\n');
448 6509b181 2022-12-30 thomas }
449 6509b181 2022-12-30 thomas while (c == EOF) {
450 6509b181 2022-12-30 thomas if (file == topfile || popfile() == EOF)
451 6509b181 2022-12-30 thomas return (EOF);
452 6509b181 2022-12-30 thomas c = igetc();
453 6509b181 2022-12-30 thomas }
454 6509b181 2022-12-30 thomas }
455 6509b181 2022-12-30 thomas return (c);
456 6509b181 2022-12-30 thomas }
457 6509b181 2022-12-30 thomas
458 6509b181 2022-12-30 thomas void
459 6509b181 2022-12-30 thomas lungetc(int c)
460 6509b181 2022-12-30 thomas {
461 6509b181 2022-12-30 thomas if (c == EOF)
462 6509b181 2022-12-30 thomas return;
463 6509b181 2022-12-30 thomas
464 6509b181 2022-12-30 thomas if (file->ungetpos >= file->ungetsize) {
465 6509b181 2022-12-30 thomas void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
466 6509b181 2022-12-30 thomas if (p == NULL)
467 6509b181 2022-12-30 thomas err(1, "reallocarray");
468 6509b181 2022-12-30 thomas file->ungetbuf = p;
469 6509b181 2022-12-30 thomas file->ungetsize *= 2;
470 6509b181 2022-12-30 thomas }
471 6509b181 2022-12-30 thomas file->ungetbuf[file->ungetpos++] = c;
472 6509b181 2022-12-30 thomas }
473 6509b181 2022-12-30 thomas
474 6509b181 2022-12-30 thomas int
475 6509b181 2022-12-30 thomas findeol(void)
476 6509b181 2022-12-30 thomas {
477 6509b181 2022-12-30 thomas int c;
478 6509b181 2022-12-30 thomas
479 6509b181 2022-12-30 thomas /* skip to either EOF or the first real EOL */
480 6509b181 2022-12-30 thomas while (1) {
481 6509b181 2022-12-30 thomas c = lgetc(0);
482 6509b181 2022-12-30 thomas if (c == '\n') {
483 6509b181 2022-12-30 thomas file->lineno++;
484 6509b181 2022-12-30 thomas break;
485 6509b181 2022-12-30 thomas }
486 6509b181 2022-12-30 thomas if (c == EOF)
487 6509b181 2022-12-30 thomas break;
488 6509b181 2022-12-30 thomas }
489 6509b181 2022-12-30 thomas return (ERROR);
490 6509b181 2022-12-30 thomas }
491 6509b181 2022-12-30 thomas
492 6509b181 2022-12-30 thomas int
493 6509b181 2022-12-30 thomas yylex(void)
494 6509b181 2022-12-30 thomas {
495 6509b181 2022-12-30 thomas char buf[8096];
496 6509b181 2022-12-30 thomas char *p = buf;
497 6509b181 2022-12-30 thomas int c;
498 6509b181 2022-12-30 thomas int token;
499 6509b181 2022-12-30 thomas int starting = 0;
500 6509b181 2022-12-30 thomas int ending = 0;
501 6509b181 2022-12-30 thomas int quote = 0;
502 6509b181 2022-12-30 thomas
503 6509b181 2022-12-30 thomas if (!in_define && block == 0) {
504 6509b181 2022-12-30 thomas while ((c = lgetc(0)) != '{' && c != EOF) {
505 6509b181 2022-12-30 thomas if (c == '\n')
506 6509b181 2022-12-30 thomas file->lineno++;
507 6509b181 2022-12-30 thomas }
508 6509b181 2022-12-30 thomas
509 6509b181 2022-12-30 thomas if (c == EOF)
510 6509b181 2022-12-30 thomas return (0);
511 6509b181 2022-12-30 thomas
512 6509b181 2022-12-30 thomas newblock:
513 6509b181 2022-12-30 thomas c = lgetc(0);
514 6509b181 2022-12-30 thomas if (c == '{' || c == '!') {
515 6509b181 2022-12-30 thomas if (c == '{')
516 6509b181 2022-12-30 thomas block = '}';
517 6509b181 2022-12-30 thomas else
518 6509b181 2022-12-30 thomas block = c;
519 6509b181 2022-12-30 thomas return (c);
520 6509b181 2022-12-30 thomas }
521 6509b181 2022-12-30 thomas if (c == '\n')
522 6509b181 2022-12-30 thomas file->lineno++;
523 6509b181 2022-12-30 thomas }
524 6509b181 2022-12-30 thomas
525 6509b181 2022-12-30 thomas while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
526 6509b181 2022-12-30 thomas if (c == '\n')
527 6509b181 2022-12-30 thomas file->lineno++;
528 6509b181 2022-12-30 thomas }
529 6509b181 2022-12-30 thomas
530 6509b181 2022-12-30 thomas if (c == EOF) {
531 6509b181 2022-12-30 thomas yyerror("unterminated block");
532 6509b181 2022-12-30 thomas return (0);
533 6509b181 2022-12-30 thomas }
534 6509b181 2022-12-30 thomas
535 6509b181 2022-12-30 thomas yylval.lineno = file->lineno;
536 6509b181 2022-12-30 thomas
537 6509b181 2022-12-30 thomas if (block != 0 && c == block) {
538 6509b181 2022-12-30 thomas if ((c = lgetc(0)) == '}') {
539 6509b181 2022-12-30 thomas if (block == '!') {
540 6509b181 2022-12-30 thomas block = 0;
541 6509b181 2022-12-30 thomas return ('!');
542 6509b181 2022-12-30 thomas }
543 6509b181 2022-12-30 thomas block = 0;
544 6509b181 2022-12-30 thomas return ('}');
545 6509b181 2022-12-30 thomas }
546 6509b181 2022-12-30 thomas lungetc(c);
547 6509b181 2022-12-30 thomas c = block;
548 6509b181 2022-12-30 thomas }
549 6509b181 2022-12-30 thomas
550 6509b181 2022-12-30 thomas if (in_define && block == 0) {
551 6509b181 2022-12-30 thomas if (c == '{')
552 6509b181 2022-12-30 thomas goto newblock;
553 6509b181 2022-12-30 thomas
554 6509b181 2022-12-30 thomas do {
555 6509b181 2022-12-30 thomas if (starting) {
556 6509b181 2022-12-30 thomas if (c == '!' || c == '{') {
557 6509b181 2022-12-30 thomas lungetc(c);
558 6509b181 2022-12-30 thomas lungetc('{');
559 6509b181 2022-12-30 thomas break;
560 6509b181 2022-12-30 thomas }
561 6509b181 2022-12-30 thomas starting = 0;
562 6509b181 2022-12-30 thomas lungetc(c);
563 6509b181 2022-12-30 thomas c = '{';
564 6509b181 2022-12-30 thomas } else if (c == '{') {
565 6509b181 2022-12-30 thomas starting = 1;
566 6509b181 2022-12-30 thomas continue;
567 84efe063 2023-04-04 thomas } else if (c == '\n')
568 84efe063 2023-04-04 thomas break;
569 6509b181 2022-12-30 thomas
570 6509b181 2022-12-30 thomas *p++ = c;
571 6509b181 2022-12-30 thomas if ((size_t)(p - buf) >= sizeof(buf)) {
572 6509b181 2022-12-30 thomas yyerror("string too long");
573 6509b181 2022-12-30 thomas return (findeol());
574 6509b181 2022-12-30 thomas }
575 84efe063 2023-04-04 thomas } while ((c = lgetc(0)) != EOF);
576 6509b181 2022-12-30 thomas *p = '\0';
577 6509b181 2022-12-30 thomas if (c == EOF) {
578 6509b181 2022-12-30 thomas yyerror("unterminated block");
579 6509b181 2022-12-30 thomas return (0);
580 6509b181 2022-12-30 thomas }
581 6509b181 2022-12-30 thomas if (c == '\n')
582 6509b181 2022-12-30 thomas file->lineno++;
583 6509b181 2022-12-30 thomas if ((yylval.v.string = strdup(buf)) == NULL)
584 6509b181 2022-12-30 thomas err(1, "strdup");
585 6509b181 2022-12-30 thomas return (STRING);
586 6509b181 2022-12-30 thomas }
587 6509b181 2022-12-30 thomas
588 6509b181 2022-12-30 thomas if (block == '!') {
589 6509b181 2022-12-30 thomas do {
590 6509b181 2022-12-30 thomas if (ending) {
591 6509b181 2022-12-30 thomas if (c == '}') {
592 6509b181 2022-12-30 thomas lungetc(c);
593 6509b181 2022-12-30 thomas lungetc(block);
594 6509b181 2022-12-30 thomas break;
595 6509b181 2022-12-30 thomas }
596 6509b181 2022-12-30 thomas ending = 0;
597 6509b181 2022-12-30 thomas lungetc(c);
598 6509b181 2022-12-30 thomas c = block;
599 6509b181 2022-12-30 thomas } else if (c == '!') {
600 6509b181 2022-12-30 thomas ending = 1;
601 6509b181 2022-12-30 thomas continue;
602 84efe063 2023-04-04 thomas } else if (c == '\n')
603 84efe063 2023-04-04 thomas break;
604 6509b181 2022-12-30 thomas
605 6509b181 2022-12-30 thomas *p++ = c;
606 6509b181 2022-12-30 thomas if ((size_t)(p - buf) >= sizeof(buf)) {
607 6509b181 2022-12-30 thomas yyerror("line too long");
608 6509b181 2022-12-30 thomas return (findeol());
609 6509b181 2022-12-30 thomas }
610 84efe063 2023-04-04 thomas } while ((c = lgetc(0)) != EOF);
611 6509b181 2022-12-30 thomas *p = '\0';
612 6509b181 2022-12-30 thomas
613 6509b181 2022-12-30 thomas if (c == EOF) {
614 6509b181 2022-12-30 thomas yyerror("unterminated block");
615 6509b181 2022-12-30 thomas return (0);
616 6509b181 2022-12-30 thomas }
617 6509b181 2022-12-30 thomas if (c == '\n')
618 6509b181 2022-12-30 thomas file->lineno++;
619 6509b181 2022-12-30 thomas
620 6509b181 2022-12-30 thomas if ((yylval.v.string = strdup(buf)) == NULL)
621 6509b181 2022-12-30 thomas err(1, "strdup");
622 6509b181 2022-12-30 thomas return (STRING);
623 6509b181 2022-12-30 thomas }
624 6509b181 2022-12-30 thomas
625 6509b181 2022-12-30 thomas if (c == '|')
626 6509b181 2022-12-30 thomas return (c);
627 6509b181 2022-12-30 thomas
628 6509b181 2022-12-30 thomas do {
629 6509b181 2022-12-30 thomas if (!quote && isspace((unsigned char)c))
630 6509b181 2022-12-30 thomas break;
631 6509b181 2022-12-30 thomas
632 6509b181 2022-12-30 thomas if (c == '"')
633 6509b181 2022-12-30 thomas quote = !quote;
634 6509b181 2022-12-30 thomas
635 6509b181 2022-12-30 thomas if (!quote && c == '|') {
636 6509b181 2022-12-30 thomas lungetc(c);
637 6509b181 2022-12-30 thomas break;
638 6509b181 2022-12-30 thomas }
639 6509b181 2022-12-30 thomas
640 6509b181 2022-12-30 thomas if (ending) {
641 6509b181 2022-12-30 thomas if (c == '}') {
642 6509b181 2022-12-30 thomas lungetc(c);
643 6509b181 2022-12-30 thomas lungetc('}');
644 6509b181 2022-12-30 thomas break;
645 6509b181 2022-12-30 thomas }
646 6509b181 2022-12-30 thomas ending = 0;
647 6509b181 2022-12-30 thomas lungetc(c);
648 6509b181 2022-12-30 thomas c = block;
649 6509b181 2022-12-30 thomas } else if (!quote && c == '}') {
650 6509b181 2022-12-30 thomas ending = 1;
651 6509b181 2022-12-30 thomas continue;
652 6509b181 2022-12-30 thomas }
653 6509b181 2022-12-30 thomas
654 6509b181 2022-12-30 thomas *p++ = c;
655 6509b181 2022-12-30 thomas if ((size_t)(p - buf) >= sizeof(buf)) {
656 6509b181 2022-12-30 thomas yyerror("string too long");
657 6509b181 2022-12-30 thomas return (findeol());
658 6509b181 2022-12-30 thomas }
659 6509b181 2022-12-30 thomas } while ((c = lgetc(0)) != EOF);
660 6509b181 2022-12-30 thomas *p = '\0';
661 6509b181 2022-12-30 thomas
662 6509b181 2022-12-30 thomas if (c == EOF) {
663 6509b181 2022-12-30 thomas yyerror(quote ? "unterminated quote" : "unterminated block");
664 6509b181 2022-12-30 thomas return (0);
665 6509b181 2022-12-30 thomas }
666 6509b181 2022-12-30 thomas if (c == '\n')
667 6509b181 2022-12-30 thomas file->lineno++;
668 6509b181 2022-12-30 thomas if ((token = lookup(buf)) == STRING)
669 6509b181 2022-12-30 thomas if ((yylval.v.string = strdup(buf)) == NULL)
670 6509b181 2022-12-30 thomas err(1, "strdup");
671 6509b181 2022-12-30 thomas return (token);
672 6509b181 2022-12-30 thomas }
673 6509b181 2022-12-30 thomas
674 6509b181 2022-12-30 thomas struct file *
675 6509b181 2022-12-30 thomas pushfile(const char *name, int secret)
676 6509b181 2022-12-30 thomas {
677 6509b181 2022-12-30 thomas struct file *nfile;
678 6509b181 2022-12-30 thomas
679 6509b181 2022-12-30 thomas if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
680 6509b181 2022-12-30 thomas err(1, "calloc");
681 6509b181 2022-12-30 thomas if ((nfile->name = strdup(name)) == NULL)
682 6509b181 2022-12-30 thomas err(1, "strdup");
683 6509b181 2022-12-30 thomas if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
684 6509b181 2022-12-30 thomas warn("can't open %s", nfile->name);
685 6509b181 2022-12-30 thomas free(nfile->name);
686 6509b181 2022-12-30 thomas free(nfile);
687 6509b181 2022-12-30 thomas return (NULL);
688 6509b181 2022-12-30 thomas }
689 6509b181 2022-12-30 thomas nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
690 6509b181 2022-12-30 thomas nfile->ungetsize = 16;
691 6509b181 2022-12-30 thomas nfile->ungetbuf = malloc(nfile->ungetsize);
692 6509b181 2022-12-30 thomas if (nfile->ungetbuf == NULL)
693 6509b181 2022-12-30 thomas err(1, "malloc");
694 6509b181 2022-12-30 thomas TAILQ_INSERT_TAIL(&files, nfile, entry);
695 6509b181 2022-12-30 thomas return (nfile);
696 6509b181 2022-12-30 thomas }
697 6509b181 2022-12-30 thomas
698 6509b181 2022-12-30 thomas int
699 6509b181 2022-12-30 thomas popfile(void)
700 6509b181 2022-12-30 thomas {
701 6509b181 2022-12-30 thomas struct file *prev;
702 6509b181 2022-12-30 thomas
703 6509b181 2022-12-30 thomas if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
704 6509b181 2022-12-30 thomas prev->errors += file->errors;
705 6509b181 2022-12-30 thomas
706 6509b181 2022-12-30 thomas TAILQ_REMOVE(&files, file, entry);
707 6509b181 2022-12-30 thomas fclose(file->stream);
708 6509b181 2022-12-30 thomas free(file->name);
709 6509b181 2022-12-30 thomas free(file->ungetbuf);
710 6509b181 2022-12-30 thomas free(file);
711 6509b181 2022-12-30 thomas file = prev;
712 6509b181 2022-12-30 thomas return (file ? 0 : EOF);
713 6509b181 2022-12-30 thomas }
714 6509b181 2022-12-30 thomas
715 6509b181 2022-12-30 thomas int
716 6509b181 2022-12-30 thomas parse(FILE *outfile, const char *filename)
717 6509b181 2022-12-30 thomas {
718 6509b181 2022-12-30 thomas fp = outfile;
719 6509b181 2022-12-30 thomas
720 6509b181 2022-12-30 thomas if ((file = pushfile(filename, 0)) == 0)
721 6509b181 2022-12-30 thomas return (-1);
722 6509b181 2022-12-30 thomas topfile = file;
723 6509b181 2022-12-30 thomas
724 6509b181 2022-12-30 thomas yyparse();
725 6509b181 2022-12-30 thomas errors = file->errors;
726 6509b181 2022-12-30 thomas popfile();
727 6509b181 2022-12-30 thomas
728 6509b181 2022-12-30 thomas return (errors ? -1 : 0);
729 6509b181 2022-12-30 thomas }
730 6509b181 2022-12-30 thomas
731 6509b181 2022-12-30 thomas void
732 6509b181 2022-12-30 thomas dbg(void)
733 6509b181 2022-12-30 thomas {
734 6509b181 2022-12-30 thomas if (nodebug)
735 6509b181 2022-12-30 thomas return;
736 6509b181 2022-12-30 thomas
737 6509b181 2022-12-30 thomas if (yylval.lineno == lastline + 1) {
738 6509b181 2022-12-30 thomas lastline = yylval.lineno;
739 6509b181 2022-12-30 thomas return;
740 6509b181 2022-12-30 thomas }
741 6509b181 2022-12-30 thomas lastline = yylval.lineno;
742 6509b181 2022-12-30 thomas
743 6509b181 2022-12-30 thomas fprintf(fp, "#line %d ", yylval.lineno);
744 6509b181 2022-12-30 thomas printq(file->name);
745 6509b181 2022-12-30 thomas putc('\n', fp);
746 6509b181 2022-12-30 thomas }
747 6509b181 2022-12-30 thomas
748 6509b181 2022-12-30 thomas void
749 6509b181 2022-12-30 thomas printq(const char *str)
750 6509b181 2022-12-30 thomas {
751 6509b181 2022-12-30 thomas putc('"', fp);
752 6509b181 2022-12-30 thomas for (; *str; ++str) {
753 6509b181 2022-12-30 thomas if (*str == '"')
754 6509b181 2022-12-30 thomas putc('\\', fp);
755 6509b181 2022-12-30 thomas putc(*str, fp);
756 6509b181 2022-12-30 thomas }
757 6509b181 2022-12-30 thomas putc('"', fp);
758 6509b181 2022-12-30 thomas }