Blame


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