Blame


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