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 2c02675e 2022-12-14 op %type <v.string> string
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 2c02675e 2022-12-14 op raw : STRING {
141 2c02675e 2022-12-14 op dbg();
142 2c02675e 2022-12-14 op fprintf(fp, "if ((tp_ret = tp->tp_puts(tp, ");
143 2c02675e 2022-12-14 op printq($1);
144 2c02675e 2022-12-14 op fputs(")) == -1) goto err;\n", fp);
145 2c02675e 2022-12-14 op
146 2c02675e 2022-12-14 op free($1);
147 2c02675e 2022-12-14 op }
148 2c02675e 2022-12-14 op ;
149 2c02675e 2022-12-14 op
150 2c02675e 2022-12-14 op block : define body end {
151 2c02675e 2022-12-14 op fputs("err:\n", fp);
152 2c02675e 2022-12-14 op fputs("return tp_ret;\n", fp);
153 2c02675e 2022-12-14 op fputs("}\n", fp);
154 2c02675e 2022-12-14 op in_define = 0;
155 2c02675e 2022-12-14 op }
156 2c02675e 2022-12-14 op | define body finally end {
157 2c02675e 2022-12-14 op fputs("return tp_ret;\n", fp);
158 2c02675e 2022-12-14 op fputs("}\n", fp);
159 2c02675e 2022-12-14 op in_define = 0;
160 2c02675e 2022-12-14 op }
161 2c02675e 2022-12-14 op ;
162 2c02675e 2022-12-14 op
163 2c02675e 2022-12-14 op define : '{' DEFINE string '}' {
164 2c02675e 2022-12-14 op in_define = 1;
165 2c02675e 2022-12-14 op
166 2c02675e 2022-12-14 op dbg();
167 2c02675e 2022-12-14 op fprintf(fp, "int\n%s\n{\n", $3);
168 2c02675e 2022-12-14 op fputs("int tp_ret = 0;\n", fp);
169 2c02675e 2022-12-14 op
170 2c02675e 2022-12-14 op free($3);
171 2c02675e 2022-12-14 op }
172 2c02675e 2022-12-14 op ;
173 2c02675e 2022-12-14 op
174 2c02675e 2022-12-14 op body : /* empty */
175 2c02675e 2022-12-14 op | body verbatim
176 2c02675e 2022-12-14 op | body raw
177 2c02675e 2022-12-14 op | body special
178 2c02675e 2022-12-14 op ;
179 2c02675e 2022-12-14 op
180 2c02675e 2022-12-14 op special : '{' RENDER string '}' {
181 2c02675e 2022-12-14 op dbg();
182 2c02675e 2022-12-14 op fprintf(fp, "if ((tp_ret = %s) == -1) goto err;\n",
183 2c02675e 2022-12-14 op $3);
184 2c02675e 2022-12-14 op free($3);
185 2c02675e 2022-12-14 op }
186 2c02675e 2022-12-14 op | printf
187 2c02675e 2022-12-14 op | if body endif { fputs("}\n", fp); }
188 2c02675e 2022-12-14 op | loop
189 2c02675e 2022-12-14 op | '{' string '|' UNSAFE '}' {
190 2c02675e 2022-12-14 op dbg();
191 2c02675e 2022-12-14 op fprintf(fp,
192 2c02675e 2022-12-14 op "if ((tp_ret = tp->tp_puts(tp, %s)) == -1)\n",
193 2c02675e 2022-12-14 op $2);
194 2c02675e 2022-12-14 op fputs("goto err;\n", fp);
195 2c02675e 2022-12-14 op free($2);
196 2c02675e 2022-12-14 op }
197 2c02675e 2022-12-14 op | '{' string '|' URLESCAPE '}' {
198 2c02675e 2022-12-14 op dbg();
199 2c02675e 2022-12-14 op fprintf(fp,
200 2c02675e 2022-12-14 op "if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
201 2c02675e 2022-12-14 op $2);
202 2c02675e 2022-12-14 op fputs("goto err;\n", fp);
203 2c02675e 2022-12-14 op free($2);
204 2c02675e 2022-12-14 op }
205 2c02675e 2022-12-14 op | '{' string '}' {
206 2c02675e 2022-12-14 op dbg();
207 2c02675e 2022-12-14 op fprintf(fp,
208 2c02675e 2022-12-14 op "if ((tp_ret = tp->tp_escape(tp, %s)) == -1)\n",
209 2c02675e 2022-12-14 op $2);
210 2c02675e 2022-12-14 op fputs("goto err;\n", fp);
211 2c02675e 2022-12-14 op free($2);
212 2c02675e 2022-12-14 op }
213 2c02675e 2022-12-14 op ;
214 2c02675e 2022-12-14 op
215 2c02675e 2022-12-14 op printf : '{' PRINTF {
216 2c02675e 2022-12-14 op dbg();
217 2c02675e 2022-12-14 op fprintf(fp, "if (asprintf(&tp->tp_tmp, ");
218 2c02675e 2022-12-14 op } printfargs '}' {
219 2c02675e 2022-12-14 op fputs(") == -1)\n", fp);
220 2c02675e 2022-12-14 op fputs("goto err;\n", fp);
221 2c02675e 2022-12-14 op fputs("if ((tp_ret = tp->tp_escape(tp, tp->tp_tmp)) "
222 2c02675e 2022-12-14 op "== -1)\n", fp);
223 2c02675e 2022-12-14 op fputs("goto err;\n", fp);
224 2c02675e 2022-12-14 op fputs("free(tp->tp_tmp);\n", fp);
225 2c02675e 2022-12-14 op fputs("tp->tp_tmp = NULL;\n", fp);
226 2c02675e 2022-12-14 op }
227 2c02675e 2022-12-14 op ;
228 2c02675e 2022-12-14 op
229 2c02675e 2022-12-14 op printfargs : /* empty */
230 2c02675e 2022-12-14 op | printfargs STRING {
231 2c02675e 2022-12-14 op fprintf(fp, " %s", $2);
232 2c02675e 2022-12-14 op free($2);
233 2c02675e 2022-12-14 op }
234 2c02675e 2022-12-14 op ;
235 2c02675e 2022-12-14 op
236 2c02675e 2022-12-14 op if : '{' IF stringy '}' {
237 2c02675e 2022-12-14 op dbg();
238 2c02675e 2022-12-14 op fprintf(fp, "if (%s) {\n", $3);
239 2c02675e 2022-12-14 op free($3);
240 2c02675e 2022-12-14 op }
241 2c02675e 2022-12-14 op ;
242 2c02675e 2022-12-14 op
243 2c02675e 2022-12-14 op endif : end
244 2c02675e 2022-12-14 op | else body end
245 2c02675e 2022-12-14 op | elsif body endif
246 2c02675e 2022-12-14 op ;
247 2c02675e 2022-12-14 op
248 2c02675e 2022-12-14 op elsif : '{' ELSE IF stringy '}' {
249 2c02675e 2022-12-14 op dbg();
250 2c02675e 2022-12-14 op fprintf(fp, "} else if (%s) {\n", $4);
251 2c02675e 2022-12-14 op free($4);
252 2c02675e 2022-12-14 op }
253 2c02675e 2022-12-14 op ;
254 2c02675e 2022-12-14 op
255 2c02675e 2022-12-14 op else : '{' ELSE '}' {
256 2c02675e 2022-12-14 op dbg();
257 2c02675e 2022-12-14 op fputs("} else {\n", fp);
258 2c02675e 2022-12-14 op }
259 2c02675e 2022-12-14 op ;
260 2c02675e 2022-12-14 op
261 2c02675e 2022-12-14 op loop : '{' FOR stringy '}' {
262 2c02675e 2022-12-14 op fprintf(fp, "for (%s) {\n", $3);
263 2c02675e 2022-12-14 op free($3);
264 2c02675e 2022-12-14 op } body end {
265 2c02675e 2022-12-14 op fputs("}\n", fp);
266 2c02675e 2022-12-14 op }
267 2c02675e 2022-12-14 op | '{' TQFOREACH STRING STRING STRING '}' {
268 2c02675e 2022-12-14 op fprintf(fp, "TAILQ_FOREACH(%s, %s, %s) {\n",
269 2c02675e 2022-12-14 op $3, $4, $5);
270 2c02675e 2022-12-14 op free($3);
271 2c02675e 2022-12-14 op free($4);
272 2c02675e 2022-12-14 op free($5);
273 2c02675e 2022-12-14 op } body end {
274 2c02675e 2022-12-14 op fputs("}\n", fp);
275 2c02675e 2022-12-14 op }
276 0f297329 2023-01-06 op | '{' WHILE stringy '}' {
277 0f297329 2023-01-06 op fprintf(fp, "while (%s) {\n", $3);
278 0f297329 2023-01-06 op free($3);
279 0f297329 2023-01-06 op } body end {
280 0f297329 2023-01-06 op fputs("}\n", fp);
281 0f297329 2023-01-06 op }
282 2c02675e 2022-12-14 op ;
283 2c02675e 2022-12-14 op
284 2c02675e 2022-12-14 op end : '{' END '}'
285 2c02675e 2022-12-14 op ;
286 2c02675e 2022-12-14 op
287 2c02675e 2022-12-14 op finally : '{' FINALLY '}' {
288 2c02675e 2022-12-14 op dbg();
289 2c02675e 2022-12-14 op fputs("err:\n", fp);
290 2c02675e 2022-12-14 op } verbatims
291 2c02675e 2022-12-14 op ;
292 2c02675e 2022-12-14 op
293 2c02675e 2022-12-14 op string : STRING string {
294 2c02675e 2022-12-14 op if (asprintf(&$$, "%s %s", $1, $2) == -1)
295 2c02675e 2022-12-14 op err(1, "asprintf");
296 2c02675e 2022-12-14 op free($1);
297 2c02675e 2022-12-14 op free($2);
298 2c02675e 2022-12-14 op }
299 2c02675e 2022-12-14 op | STRING
300 2c02675e 2022-12-14 op ;
301 2c02675e 2022-12-14 op
302 2c02675e 2022-12-14 op stringy : STRING
303 2c02675e 2022-12-14 op | STRING stringy {
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 | '|' stringy {
310 2c02675e 2022-12-14 op if (asprintf(&$$, "|%s", $2) == -1)
311 2c02675e 2022-12-14 op err(1, "asprintf");
312 2c02675e 2022-12-14 op free($2);
313 2c02675e 2022-12-14 op }
314 2c02675e 2022-12-14 op ;
315 2c02675e 2022-12-14 op
316 2c02675e 2022-12-14 op %%
317 2c02675e 2022-12-14 op
318 2c02675e 2022-12-14 op struct keywords {
319 2c02675e 2022-12-14 op const char *k_name;
320 2c02675e 2022-12-14 op int k_val;
321 2c02675e 2022-12-14 op };
322 2c02675e 2022-12-14 op
323 2c02675e 2022-12-14 op int
324 2c02675e 2022-12-14 op yyerror(const char *fmt, ...)
325 2c02675e 2022-12-14 op {
326 2c02675e 2022-12-14 op va_list ap;
327 2c02675e 2022-12-14 op char *msg;
328 2c02675e 2022-12-14 op
329 2c02675e 2022-12-14 op file->errors++;
330 2c02675e 2022-12-14 op va_start(ap, fmt);
331 2c02675e 2022-12-14 op if (vasprintf(&msg, fmt, ap) == -1)
332 2c02675e 2022-12-14 op err(1, "yyerror vasprintf");
333 2c02675e 2022-12-14 op va_end(ap);
334 2c02675e 2022-12-14 op fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
335 2c02675e 2022-12-14 op free(msg);
336 2c02675e 2022-12-14 op return (0);
337 2c02675e 2022-12-14 op }
338 2c02675e 2022-12-14 op
339 2c02675e 2022-12-14 op int
340 2c02675e 2022-12-14 op kw_cmp(const void *k, const void *e)
341 2c02675e 2022-12-14 op {
342 2c02675e 2022-12-14 op return (strcmp(k, ((const struct keywords *)e)->k_name));
343 2c02675e 2022-12-14 op }
344 2c02675e 2022-12-14 op
345 2c02675e 2022-12-14 op int
346 2c02675e 2022-12-14 op lookup(char *s)
347 2c02675e 2022-12-14 op {
348 2c02675e 2022-12-14 op /* this has to be sorted always */
349 2c02675e 2022-12-14 op static const struct keywords keywords[] = {
350 2c02675e 2022-12-14 op { "define", DEFINE },
351 2c02675e 2022-12-14 op { "else", ELSE },
352 2c02675e 2022-12-14 op { "end", END },
353 2c02675e 2022-12-14 op { "finally", FINALLY },
354 2c02675e 2022-12-14 op { "for", FOR },
355 2c02675e 2022-12-14 op { "if", IF },
356 2c02675e 2022-12-14 op { "include", INCLUDE },
357 2c02675e 2022-12-14 op { "printf", PRINTF },
358 2c02675e 2022-12-14 op { "render", RENDER },
359 2c02675e 2022-12-14 op { "tailq-foreach", TQFOREACH },
360 2c02675e 2022-12-14 op { "unsafe", UNSAFE },
361 2c02675e 2022-12-14 op { "urlescape", URLESCAPE },
362 0f297329 2023-01-06 op { "while", WHILE },
363 2c02675e 2022-12-14 op };
364 2c02675e 2022-12-14 op const struct keywords *p;
365 2c02675e 2022-12-14 op
366 2c02675e 2022-12-14 op p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
367 2c02675e 2022-12-14 op kw_cmp);
368 2c02675e 2022-12-14 op
369 2c02675e 2022-12-14 op if (p)
370 2c02675e 2022-12-14 op return (p->k_val);
371 2c02675e 2022-12-14 op else
372 2c02675e 2022-12-14 op return (STRING);
373 2c02675e 2022-12-14 op }
374 2c02675e 2022-12-14 op
375 2c02675e 2022-12-14 op #define START_EXPAND 1
376 2c02675e 2022-12-14 op #define DONE_EXPAND 2
377 2c02675e 2022-12-14 op
378 2c02675e 2022-12-14 op static int expanding;
379 2c02675e 2022-12-14 op
380 2c02675e 2022-12-14 op int
381 2c02675e 2022-12-14 op igetc(void)
382 2c02675e 2022-12-14 op {
383 2c02675e 2022-12-14 op int c;
384 2c02675e 2022-12-14 op
385 2c02675e 2022-12-14 op while (1) {
386 2c02675e 2022-12-14 op if (file->ungetpos > 0)
387 2c02675e 2022-12-14 op c = file->ungetbuf[--file->ungetpos];
388 2c02675e 2022-12-14 op else
389 2c02675e 2022-12-14 op c = getc(file->stream);
390 2c02675e 2022-12-14 op
391 2c02675e 2022-12-14 op if (c == START_EXPAND)
392 2c02675e 2022-12-14 op expanding = 1;
393 2c02675e 2022-12-14 op else if (c == DONE_EXPAND)
394 2c02675e 2022-12-14 op expanding = 0;
395 2c02675e 2022-12-14 op else
396 2c02675e 2022-12-14 op break;
397 2c02675e 2022-12-14 op }
398 2c02675e 2022-12-14 op return (c);
399 2c02675e 2022-12-14 op }
400 2c02675e 2022-12-14 op
401 2c02675e 2022-12-14 op int
402 2c02675e 2022-12-14 op lgetc(int quotec)
403 2c02675e 2022-12-14 op {
404 2c02675e 2022-12-14 op int c;
405 2c02675e 2022-12-14 op
406 2c02675e 2022-12-14 op if (quotec) {
407 2c02675e 2022-12-14 op if ((c = igetc()) == EOF) {
408 2c02675e 2022-12-14 op yyerror("reached end of filewhile parsing "
409 2c02675e 2022-12-14 op "quoted string");
410 2c02675e 2022-12-14 op if (file == topfile || popfile() == EOF)
411 2c02675e 2022-12-14 op return (EOF);
412 2c02675e 2022-12-14 op return (quotec);
413 2c02675e 2022-12-14 op }
414 2c02675e 2022-12-14 op return (c);
415 2c02675e 2022-12-14 op }
416 2c02675e 2022-12-14 op
417 2c02675e 2022-12-14 op c = igetc();
418 2c02675e 2022-12-14 op if (c == '\t' || c == ' ') {
419 2c02675e 2022-12-14 op /* Compress blanks to a sigle space. */
420 2c02675e 2022-12-14 op do {
421 2c02675e 2022-12-14 op c = getc(file->stream);
422 2c02675e 2022-12-14 op } while (c == '\t' || c == ' ');
423 2c02675e 2022-12-14 op ungetc(c, file->stream);
424 2c02675e 2022-12-14 op c = ' ';
425 2c02675e 2022-12-14 op }
426 2c02675e 2022-12-14 op
427 2c02675e 2022-12-14 op if (c == EOF) {
428 2c02675e 2022-12-14 op /*
429 2c02675e 2022-12-14 op * Fake EOL when hit EOF for the first time. This gets line
430 fdd67010 2023-03-26 op * count right if last line in included file is syntactically
431 2c02675e 2022-12-14 op * invalid and has no newline.
432 2c02675e 2022-12-14 op */
433 2c02675e 2022-12-14 op if (file->eof_reached == 0) {
434 2c02675e 2022-12-14 op file->eof_reached = 1;
435 2c02675e 2022-12-14 op return ('\n');
436 2c02675e 2022-12-14 op }
437 2c02675e 2022-12-14 op while (c == EOF) {
438 2c02675e 2022-12-14 op if (file == topfile || popfile() == EOF)
439 2c02675e 2022-12-14 op return (EOF);
440 2c02675e 2022-12-14 op c = igetc();
441 2c02675e 2022-12-14 op }
442 2c02675e 2022-12-14 op }
443 2c02675e 2022-12-14 op return (c);
444 2c02675e 2022-12-14 op }
445 2c02675e 2022-12-14 op
446 2c02675e 2022-12-14 op void
447 2c02675e 2022-12-14 op lungetc(int c)
448 2c02675e 2022-12-14 op {
449 2c02675e 2022-12-14 op if (c == EOF)
450 2c02675e 2022-12-14 op return;
451 2c02675e 2022-12-14 op
452 2c02675e 2022-12-14 op if (file->ungetpos >= file->ungetsize) {
453 2c02675e 2022-12-14 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
454 2c02675e 2022-12-14 op if (p == NULL)
455 2c02675e 2022-12-14 op err(1, "reallocarray");
456 2c02675e 2022-12-14 op file->ungetbuf = p;
457 2c02675e 2022-12-14 op file->ungetsize *= 2;
458 2c02675e 2022-12-14 op }
459 2c02675e 2022-12-14 op file->ungetbuf[file->ungetpos++] = c;
460 2c02675e 2022-12-14 op }
461 2c02675e 2022-12-14 op
462 2c02675e 2022-12-14 op int
463 2c02675e 2022-12-14 op findeol(void)
464 2c02675e 2022-12-14 op {
465 2c02675e 2022-12-14 op int c;
466 2c02675e 2022-12-14 op
467 2c02675e 2022-12-14 op /* skip to either EOF or the first real EOL */
468 2c02675e 2022-12-14 op while (1) {
469 2c02675e 2022-12-14 op c = lgetc(0);
470 2c02675e 2022-12-14 op if (c == '\n') {
471 2c02675e 2022-12-14 op file->lineno++;
472 2c02675e 2022-12-14 op break;
473 2c02675e 2022-12-14 op }
474 2c02675e 2022-12-14 op if (c == EOF)
475 2c02675e 2022-12-14 op break;
476 2c02675e 2022-12-14 op }
477 2c02675e 2022-12-14 op return (ERROR);
478 2c02675e 2022-12-14 op }
479 2c02675e 2022-12-14 op
480 2c02675e 2022-12-14 op int
481 2c02675e 2022-12-14 op yylex(void)
482 2c02675e 2022-12-14 op {
483 2c02675e 2022-12-14 op char buf[8096];
484 2c02675e 2022-12-14 op char *p = buf;
485 2c02675e 2022-12-14 op int c;
486 2c02675e 2022-12-14 op int token;
487 2c02675e 2022-12-14 op int starting = 0;
488 2c02675e 2022-12-14 op int ending = 0;
489 2c02675e 2022-12-14 op int quote = 0;
490 2c02675e 2022-12-14 op
491 2c02675e 2022-12-14 op if (!in_define && block == 0) {
492 2c02675e 2022-12-14 op while ((c = lgetc(0)) != '{' && c != EOF) {
493 2c02675e 2022-12-14 op if (c == '\n')
494 2c02675e 2022-12-14 op file->lineno++;
495 2c02675e 2022-12-14 op }
496 2c02675e 2022-12-14 op
497 2c02675e 2022-12-14 op if (c == EOF)
498 2c02675e 2022-12-14 op return (0);
499 2c02675e 2022-12-14 op
500 2c02675e 2022-12-14 op newblock:
501 2c02675e 2022-12-14 op c = lgetc(0);
502 2c02675e 2022-12-14 op if (c == '{' || c == '!') {
503 2c02675e 2022-12-14 op if (c == '{')
504 2c02675e 2022-12-14 op block = '}';
505 2c02675e 2022-12-14 op else
506 2c02675e 2022-12-14 op block = c;
507 2c02675e 2022-12-14 op return (c);
508 2c02675e 2022-12-14 op }
509 2c02675e 2022-12-14 op if (c == '\n')
510 2c02675e 2022-12-14 op file->lineno++;
511 2c02675e 2022-12-14 op }
512 2c02675e 2022-12-14 op
513 2c02675e 2022-12-14 op while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
514 2c02675e 2022-12-14 op if (c == '\n')
515 2c02675e 2022-12-14 op file->lineno++;
516 2c02675e 2022-12-14 op }
517 2c02675e 2022-12-14 op
518 2c02675e 2022-12-14 op if (c == EOF) {
519 2c02675e 2022-12-14 op yyerror("unterminated block");
520 2c02675e 2022-12-14 op return (0);
521 2c02675e 2022-12-14 op }
522 2c02675e 2022-12-14 op
523 2c02675e 2022-12-14 op yylval.lineno = file->lineno;
524 2c02675e 2022-12-14 op
525 2c02675e 2022-12-14 op if (block != 0 && c == block) {
526 2c02675e 2022-12-14 op if ((c = lgetc(0)) == '}') {
527 2c02675e 2022-12-14 op if (block == '!') {
528 2c02675e 2022-12-14 op block = 0;
529 2c02675e 2022-12-14 op return ('!');
530 2c02675e 2022-12-14 op }
531 2c02675e 2022-12-14 op block = 0;
532 2c02675e 2022-12-14 op return ('}');
533 2c02675e 2022-12-14 op }
534 2c02675e 2022-12-14 op lungetc(c);
535 2c02675e 2022-12-14 op c = block;
536 2c02675e 2022-12-14 op }
537 2c02675e 2022-12-14 op
538 2c02675e 2022-12-14 op if (in_define && block == 0) {
539 2c02675e 2022-12-14 op if (c == '{')
540 2c02675e 2022-12-14 op goto newblock;
541 2c02675e 2022-12-14 op
542 2c02675e 2022-12-14 op do {
543 2c02675e 2022-12-14 op if (starting) {
544 2c02675e 2022-12-14 op if (c == '!' || c == '{') {
545 2c02675e 2022-12-14 op lungetc(c);
546 2c02675e 2022-12-14 op lungetc('{');
547 2c02675e 2022-12-14 op break;
548 2c02675e 2022-12-14 op }
549 2c02675e 2022-12-14 op starting = 0;
550 2c02675e 2022-12-14 op lungetc(c);
551 2c02675e 2022-12-14 op c = '{';
552 2c02675e 2022-12-14 op } else if (c == '{') {
553 2c02675e 2022-12-14 op starting = 1;
554 2c02675e 2022-12-14 op continue;
555 97267ffd 2023-04-03 op } else if (c == '\n')
556 97267ffd 2023-04-03 op break;
557 2c02675e 2022-12-14 op
558 2c02675e 2022-12-14 op *p++ = c;
559 2c02675e 2022-12-14 op if ((size_t)(p - buf) >= sizeof(buf)) {
560 2c02675e 2022-12-14 op yyerror("string too long");
561 2c02675e 2022-12-14 op return (findeol());
562 2c02675e 2022-12-14 op }
563 97267ffd 2023-04-03 op } while ((c = lgetc(0)) != EOF);
564 2c02675e 2022-12-14 op *p = '\0';
565 2c02675e 2022-12-14 op if (c == EOF) {
566 2c02675e 2022-12-14 op yyerror("unterminated block");
567 2c02675e 2022-12-14 op return (0);
568 2c02675e 2022-12-14 op }
569 2c02675e 2022-12-14 op if (c == '\n')
570 2c02675e 2022-12-14 op file->lineno++;
571 2c02675e 2022-12-14 op if ((yylval.v.string = strdup(buf)) == NULL)
572 2c02675e 2022-12-14 op err(1, "strdup");
573 2c02675e 2022-12-14 op return (STRING);
574 2c02675e 2022-12-14 op }
575 2c02675e 2022-12-14 op
576 2c02675e 2022-12-14 op if (block == '!') {
577 2c02675e 2022-12-14 op do {
578 2c02675e 2022-12-14 op if (ending) {
579 2c02675e 2022-12-14 op if (c == '}') {
580 2c02675e 2022-12-14 op lungetc(c);
581 2c02675e 2022-12-14 op lungetc(block);
582 2c02675e 2022-12-14 op break;
583 2c02675e 2022-12-14 op }
584 2c02675e 2022-12-14 op ending = 0;
585 2c02675e 2022-12-14 op lungetc(c);
586 2c02675e 2022-12-14 op c = block;
587 2c02675e 2022-12-14 op } else if (c == '!') {
588 2c02675e 2022-12-14 op ending = 1;
589 2c02675e 2022-12-14 op continue;
590 97267ffd 2023-04-03 op } else if (c == '\n')
591 97267ffd 2023-04-03 op break;
592 2c02675e 2022-12-14 op
593 2c02675e 2022-12-14 op *p++ = c;
594 2c02675e 2022-12-14 op if ((size_t)(p - buf) >= sizeof(buf)) {
595 2c02675e 2022-12-14 op yyerror("line too long");
596 2c02675e 2022-12-14 op return (findeol());
597 2c02675e 2022-12-14 op }
598 97267ffd 2023-04-03 op } while ((c = lgetc(0)) != EOF);
599 2c02675e 2022-12-14 op *p = '\0';
600 2c02675e 2022-12-14 op
601 2c02675e 2022-12-14 op if (c == EOF) {
602 2c02675e 2022-12-14 op yyerror("unterminated block");
603 2c02675e 2022-12-14 op return (0);
604 2c02675e 2022-12-14 op }
605 2c02675e 2022-12-14 op if (c == '\n')
606 2c02675e 2022-12-14 op file->lineno++;
607 2c02675e 2022-12-14 op
608 2c02675e 2022-12-14 op if ((yylval.v.string = strdup(buf)) == NULL)
609 2c02675e 2022-12-14 op err(1, "strdup");
610 2c02675e 2022-12-14 op return (STRING);
611 2c02675e 2022-12-14 op }
612 2c02675e 2022-12-14 op
613 2c02675e 2022-12-14 op if (c == '|')
614 2c02675e 2022-12-14 op return (c);
615 2c02675e 2022-12-14 op
616 2c02675e 2022-12-14 op do {
617 2c02675e 2022-12-14 op if (!quote && isspace((unsigned char)c))
618 2c02675e 2022-12-14 op break;
619 2c02675e 2022-12-14 op
620 2c02675e 2022-12-14 op if (c == '"')
621 2c02675e 2022-12-14 op quote = !quote;
622 2c02675e 2022-12-14 op
623 2c02675e 2022-12-14 op if (!quote && c == '|') {
624 2c02675e 2022-12-14 op lungetc(c);
625 2c02675e 2022-12-14 op break;
626 2c02675e 2022-12-14 op }
627 2c02675e 2022-12-14 op
628 2c02675e 2022-12-14 op if (ending) {
629 2c02675e 2022-12-14 op if (c == '}') {
630 2c02675e 2022-12-14 op lungetc(c);
631 2c02675e 2022-12-14 op lungetc('}');
632 2c02675e 2022-12-14 op break;
633 2c02675e 2022-12-14 op }
634 2c02675e 2022-12-14 op ending = 0;
635 2c02675e 2022-12-14 op lungetc(c);
636 2c02675e 2022-12-14 op c = block;
637 2c02675e 2022-12-14 op } else if (!quote && c == '}') {
638 2c02675e 2022-12-14 op ending = 1;
639 2c02675e 2022-12-14 op continue;
640 2c02675e 2022-12-14 op }
641 2c02675e 2022-12-14 op
642 2c02675e 2022-12-14 op *p++ = c;
643 2c02675e 2022-12-14 op if ((size_t)(p - buf) >= sizeof(buf)) {
644 2c02675e 2022-12-14 op yyerror("string too long");
645 2c02675e 2022-12-14 op return (findeol());
646 2c02675e 2022-12-14 op }
647 2c02675e 2022-12-14 op } while ((c = lgetc(0)) != EOF);
648 2c02675e 2022-12-14 op *p = '\0';
649 2c02675e 2022-12-14 op
650 2c02675e 2022-12-14 op if (c == EOF) {
651 2c02675e 2022-12-14 op yyerror(quote ? "unterminated quote" : "unterminated block");
652 2c02675e 2022-12-14 op return (0);
653 2c02675e 2022-12-14 op }
654 2c02675e 2022-12-14 op if (c == '\n')
655 2c02675e 2022-12-14 op file->lineno++;
656 2c02675e 2022-12-14 op if ((token = lookup(buf)) == STRING)
657 2c02675e 2022-12-14 op if ((yylval.v.string = strdup(buf)) == NULL)
658 2c02675e 2022-12-14 op err(1, "strdup");
659 2c02675e 2022-12-14 op return (token);
660 2c02675e 2022-12-14 op }
661 2c02675e 2022-12-14 op
662 2c02675e 2022-12-14 op struct file *
663 2c02675e 2022-12-14 op pushfile(const char *name, int secret)
664 2c02675e 2022-12-14 op {
665 2c02675e 2022-12-14 op struct file *nfile;
666 2c02675e 2022-12-14 op
667 2c02675e 2022-12-14 op if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
668 2c02675e 2022-12-14 op err(1, "calloc");
669 2c02675e 2022-12-14 op if ((nfile->name = strdup(name)) == NULL)
670 2c02675e 2022-12-14 op err(1, "strdup");
671 2c02675e 2022-12-14 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
672 2c02675e 2022-12-14 op warn("can't open %s", nfile->name);
673 2c02675e 2022-12-14 op free(nfile->name);
674 2c02675e 2022-12-14 op free(nfile);
675 2c02675e 2022-12-14 op return (NULL);
676 2c02675e 2022-12-14 op }
677 2c02675e 2022-12-14 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
678 2c02675e 2022-12-14 op nfile->ungetsize = 16;
679 2c02675e 2022-12-14 op nfile->ungetbuf = malloc(nfile->ungetsize);
680 2c02675e 2022-12-14 op if (nfile->ungetbuf == NULL)
681 2c02675e 2022-12-14 op err(1, "malloc");
682 2c02675e 2022-12-14 op TAILQ_INSERT_TAIL(&files, nfile, entry);
683 2c02675e 2022-12-14 op return (nfile);
684 2c02675e 2022-12-14 op }
685 2c02675e 2022-12-14 op
686 2c02675e 2022-12-14 op int
687 2c02675e 2022-12-14 op popfile(void)
688 2c02675e 2022-12-14 op {
689 2c02675e 2022-12-14 op struct file *prev;
690 2c02675e 2022-12-14 op
691 2c02675e 2022-12-14 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
692 2c02675e 2022-12-14 op prev->errors += file->errors;
693 2c02675e 2022-12-14 op
694 2c02675e 2022-12-14 op TAILQ_REMOVE(&files, file, entry);
695 2c02675e 2022-12-14 op fclose(file->stream);
696 2c02675e 2022-12-14 op free(file->name);
697 2c02675e 2022-12-14 op free(file->ungetbuf);
698 2c02675e 2022-12-14 op free(file);
699 2c02675e 2022-12-14 op file = prev;
700 2c02675e 2022-12-14 op return (file ? 0 : EOF);
701 2c02675e 2022-12-14 op }
702 2c02675e 2022-12-14 op
703 2c02675e 2022-12-14 op int
704 2c02675e 2022-12-14 op parse(FILE *outfile, const char *filename)
705 2c02675e 2022-12-14 op {
706 2c02675e 2022-12-14 op fp = outfile;
707 2c02675e 2022-12-14 op
708 2c02675e 2022-12-14 op if ((file = pushfile(filename, 0)) == 0)
709 2c02675e 2022-12-14 op return (-1);
710 2c02675e 2022-12-14 op topfile = file;
711 2c02675e 2022-12-14 op
712 2c02675e 2022-12-14 op yyparse();
713 2c02675e 2022-12-14 op errors = file->errors;
714 2c02675e 2022-12-14 op popfile();
715 2c02675e 2022-12-14 op
716 2c02675e 2022-12-14 op return (errors ? -1 : 0);
717 2c02675e 2022-12-14 op }
718 2c02675e 2022-12-14 op
719 2c02675e 2022-12-14 op void
720 2c02675e 2022-12-14 op dbg(void)
721 2c02675e 2022-12-14 op {
722 2c02675e 2022-12-14 op if (nodebug)
723 2c02675e 2022-12-14 op return;
724 2c02675e 2022-12-14 op
725 2c02675e 2022-12-14 op if (yylval.lineno == lastline + 1) {
726 2c02675e 2022-12-14 op lastline = yylval.lineno;
727 2c02675e 2022-12-14 op return;
728 2c02675e 2022-12-14 op }
729 2c02675e 2022-12-14 op lastline = yylval.lineno;
730 2c02675e 2022-12-14 op
731 2c02675e 2022-12-14 op fprintf(fp, "#line %d ", yylval.lineno);
732 2c02675e 2022-12-14 op printq(file->name);
733 2c02675e 2022-12-14 op putc('\n', fp);
734 2c02675e 2022-12-14 op }
735 2c02675e 2022-12-14 op
736 2c02675e 2022-12-14 op void
737 2c02675e 2022-12-14 op printq(const char *str)
738 2c02675e 2022-12-14 op {
739 2c02675e 2022-12-14 op putc('"', fp);
740 2c02675e 2022-12-14 op for (; *str; ++str) {
741 2c02675e 2022-12-14 op if (*str == '"')
742 2c02675e 2022-12-14 op putc('\\', fp);
743 2c02675e 2022-12-14 op putc(*str, fp);
744 2c02675e 2022-12-14 op }
745 2c02675e 2022-12-14 op putc('"', fp);
746 2c02675e 2022-12-14 op }