2 96b8c570 2022-12-30 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 6509b181 2022-12-30 thomas * Copyright (c) 2007-2016 Reyk Floeter <reyk@openbsd.org>
4 6509b181 2022-12-30 thomas * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 6509b181 2022-12-30 thomas * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 6509b181 2022-12-30 thomas * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 6509b181 2022-12-30 thomas * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 6509b181 2022-12-30 thomas * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 6509b181 2022-12-30 thomas * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 6509b181 2022-12-30 thomas * Permission to use, copy, modify, and distribute this software for any
12 6509b181 2022-12-30 thomas * purpose with or without fee is hereby granted, provided that the above
13 6509b181 2022-12-30 thomas * copyright notice and this permission notice appear in all copies.
15 6509b181 2022-12-30 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 6509b181 2022-12-30 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 6509b181 2022-12-30 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 6509b181 2022-12-30 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 6509b181 2022-12-30 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 6509b181 2022-12-30 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 6509b181 2022-12-30 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 6509b181 2022-12-30 thomas #include <sys/queue.h>
28 6509b181 2022-12-30 thomas #include <ctype.h>
29 6509b181 2022-12-30 thomas #include <err.h>
30 6509b181 2022-12-30 thomas #include <stdio.h>
31 6509b181 2022-12-30 thomas #include <stdlib.h>
32 6509b181 2022-12-30 thomas #include <stdarg.h>
33 6509b181 2022-12-30 thomas #include <stdint.h>
34 6509b181 2022-12-30 thomas #include <string.h>
35 6509b181 2022-12-30 thomas #include <unistd.h>
37 6509b181 2022-12-30 thomas #ifndef nitems
38 6509b181 2022-12-30 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 6509b181 2022-12-30 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
42 6509b181 2022-12-30 thomas static struct file {
43 6509b181 2022-12-30 thomas TAILQ_ENTRY(file) entry;
44 6509b181 2022-12-30 thomas FILE *stream;
45 6509b181 2022-12-30 thomas char *name;
46 6509b181 2022-12-30 thomas size_t ungetpos;
47 6509b181 2022-12-30 thomas size_t ungetsize;
48 6509b181 2022-12-30 thomas unsigned char *ungetbuf;
49 6509b181 2022-12-30 thomas int eof_reached;
50 6509b181 2022-12-30 thomas int lineno;
51 6509b181 2022-12-30 thomas int errors;
52 6509b181 2022-12-30 thomas } *file, *topfile;
53 6509b181 2022-12-30 thomas int parse(FILE *, const char *);
54 6509b181 2022-12-30 thomas struct file *pushfile(const char *, int);
55 6509b181 2022-12-30 thomas int popfile(void);
56 6509b181 2022-12-30 thomas int yyparse(void);
57 6509b181 2022-12-30 thomas int yylex(void);
58 6509b181 2022-12-30 thomas int yyerror(const char *, ...)
59 6509b181 2022-12-30 thomas __attribute__((__format__ (printf, 1, 2)))
60 6509b181 2022-12-30 thomas __attribute__((__nonnull__ (1)));
61 6509b181 2022-12-30 thomas int kw_cmp(const void *, const void *);
62 6509b181 2022-12-30 thomas int lookup(char *);
63 6509b181 2022-12-30 thomas int igetc(void);
64 6509b181 2022-12-30 thomas int lgetc(int);
65 6509b181 2022-12-30 thomas void lungetc(int);
66 6509b181 2022-12-30 thomas int findeol(void);
68 6509b181 2022-12-30 thomas void dbg(void);
69 6509b181 2022-12-30 thomas void printq(const char *);
71 6509b181 2022-12-30 thomas extern int nodebug;
73 6509b181 2022-12-30 thomas static FILE *fp;
75 6509b181 2022-12-30 thomas static int block;
76 6509b181 2022-12-30 thomas static int in_define;
77 6509b181 2022-12-30 thomas static int errors;
78 6509b181 2022-12-30 thomas static int lastline = -1;
80 6509b181 2022-12-30 thomas typedef struct {
82 6509b181 2022-12-30 thomas char *string;
84 6509b181 2022-12-30 thomas int lineno;
85 6509b181 2022-12-30 thomas } YYSTYPE;
89 6509b181 2022-12-30 thomas %token DEFINE ELSE END ERROR FINALLY FOR IF INCLUDE PRINTF
90 6509b181 2022-12-30 thomas %token RENDER TQFOREACH UNSAFE URLESCAPE
91 6509b181 2022-12-30 thomas %token <v.string> STRING
92 6509b181 2022-12-30 thomas %type <v.string> string
93 6509b181 2022-12-30 thomas %type <v.string> stringy
97 6509b181 2022-12-30 thomas grammar : /* empty */
98 6509b181 2022-12-30 thomas | grammar include
99 6509b181 2022-12-30 thomas | grammar verbatim
100 6509b181 2022-12-30 thomas | grammar block
101 6509b181 2022-12-30 thomas | grammar error { file->errors++; }
104 6509b181 2022-12-30 thomas include : INCLUDE STRING {
105 6509b181 2022-12-30 thomas struct file *nfile;
107 6509b181 2022-12-30 thomas if ((nfile = pushfile($2, 0)) == NULL) {
108 6509b181 2022-12-30 thomas yyerror("failed to include file %s", $2);
109 6509b181 2022-12-30 thomas free($2);
112 6509b181 2022-12-30 thomas free($2);
114 6509b181 2022-12-30 thomas file = nfile;
115 6509b181 2022-12-30 thomas lungetc('\n');
119 6509b181 2022-12-30 thomas verbatim : '!' verbatim1 '!' {
120 6509b181 2022-12-30 thomas if (in_define) {
121 6509b181 2022-12-30 thomas /* TODO: check template status and exit in case */
126 6509b181 2022-12-30 thomas verbatim1 : /* empty */
127 6509b181 2022-12-30 thomas | verbatim1 STRING {
128 6509b181 2022-12-30 thomas if (*$2 != '\0') {
130 6509b181 2022-12-30 thomas fprintf(fp, "%s\n", $2);
132 6509b181 2022-12-30 thomas free($2);
136 6509b181 2022-12-30 thomas verbatims : /* empty */
137 6509b181 2022-12-30 thomas | verbatims verbatim
140 6509b181 2022-12-30 thomas raw : STRING {
142 6509b181 2022-12-30 thomas fprintf(fp, "if ((tp_ret = tp->tp_puts(tp, ");
143 6509b181 2022-12-30 thomas printq($1);
144 6509b181 2022-12-30 thomas fputs(")) == -1) goto err;\n", fp);
146 6509b181 2022-12-30 thomas free($1);
150 6509b181 2022-12-30 thomas block : define body end {
151 6509b181 2022-12-30 thomas fputs("err:\n", fp);
152 6509b181 2022-12-30 thomas fputs("return tp_ret;\n", fp);
153 6509b181 2022-12-30 thomas fputs("}\n", fp);
154 6509b181 2022-12-30 thomas in_define = 0;
156 6509b181 2022-12-30 thomas | define body finally end {
157 6509b181 2022-12-30 thomas fputs("return tp_ret;\n", fp);
158 6509b181 2022-12-30 thomas fputs("}\n", fp);
159 6509b181 2022-12-30 thomas in_define = 0;
163 6509b181 2022-12-30 thomas define : '{' DEFINE string '}' {
164 6509b181 2022-12-30 thomas in_define = 1;
167 6509b181 2022-12-30 thomas fprintf(fp, "int\n%s\n{\n", $3);
168 6509b181 2022-12-30 thomas fputs("int tp_ret = 0;\n", fp);
170 6509b181 2022-12-30 thomas free($3);
174 6509b181 2022-12-30 thomas body : /* empty */
175 6509b181 2022-12-30 thomas | body verbatim
176 6509b181 2022-12-30 thomas | body raw
177 6509b181 2022-12-30 thomas | body special
180 6509b181 2022-12-30 thomas special : '{' RENDER string '}' {
182 6509b181 2022-12-30 thomas fprintf(fp, "if ((tp_ret = %s) == -1) goto err;\n",
184 6509b181 2022-12-30 thomas free($3);
187 6509b181 2022-12-30 thomas | if body endif { fputs("}\n", fp); }
189 6509b181 2022-12-30 thomas | '{' string '|' UNSAFE '}' {
191 6509b181 2022-12-30 thomas fprintf(fp,
192 6509b181 2022-12-30 thomas "if ((tp_ret = tp->tp_puts(tp, %s)) == -1)\n",
194 6509b181 2022-12-30 thomas fputs("goto err;\n", fp);
195 6509b181 2022-12-30 thomas free($2);
197 6509b181 2022-12-30 thomas | '{' string '|' URLESCAPE '}' {
199 6509b181 2022-12-30 thomas fprintf(fp,
200 6509b181 2022-12-30 thomas "if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
202 6509b181 2022-12-30 thomas fputs("goto err;\n", fp);
203 6509b181 2022-12-30 thomas free($2);
205 6509b181 2022-12-30 thomas | '{' string '}' {
207 6509b181 2022-12-30 thomas fprintf(fp,
208 6509b181 2022-12-30 thomas "if ((tp_ret = tp->tp_escape(tp, %s)) == -1)\n",
210 6509b181 2022-12-30 thomas fputs("goto err;\n", fp);
211 6509b181 2022-12-30 thomas free($2);
215 6509b181 2022-12-30 thomas printf : '{' PRINTF {
217 6509b181 2022-12-30 thomas fprintf(fp, "if (asprintf(&tp->tp_tmp, ");
218 6509b181 2022-12-30 thomas } printfargs '}' {
219 6509b181 2022-12-30 thomas fputs(") == -1)\n", fp);
220 6509b181 2022-12-30 thomas fputs("goto err;\n", fp);
221 6509b181 2022-12-30 thomas fputs("if ((tp_ret = tp->tp_escape(tp, tp->tp_tmp)) "
222 6509b181 2022-12-30 thomas "== -1)\n", fp);
223 6509b181 2022-12-30 thomas fputs("goto err;\n", fp);
224 6509b181 2022-12-30 thomas fputs("free(tp->tp_tmp);\n", fp);
225 6509b181 2022-12-30 thomas fputs("tp->tp_tmp = NULL;\n", fp);
229 6509b181 2022-12-30 thomas printfargs : /* empty */
230 6509b181 2022-12-30 thomas | printfargs STRING {
231 6509b181 2022-12-30 thomas fprintf(fp, " %s", $2);
232 6509b181 2022-12-30 thomas free($2);
236 6509b181 2022-12-30 thomas if : '{' IF stringy '}' {
238 6509b181 2022-12-30 thomas fprintf(fp, "if (%s) {\n", $3);
239 6509b181 2022-12-30 thomas free($3);
243 6509b181 2022-12-30 thomas endif : end
244 6509b181 2022-12-30 thomas | else body end
245 6509b181 2022-12-30 thomas | elsif body endif
248 6509b181 2022-12-30 thomas elsif : '{' ELSE IF stringy '}' {
250 6509b181 2022-12-30 thomas fprintf(fp, "} else if (%s) {\n", $4);
251 6509b181 2022-12-30 thomas free($4);
255 6509b181 2022-12-30 thomas else : '{' ELSE '}' {
257 6509b181 2022-12-30 thomas fputs("} else {\n", fp);
261 6509b181 2022-12-30 thomas loop : '{' FOR stringy '}' {
262 6509b181 2022-12-30 thomas fprintf(fp, "for (%s) {\n", $3);
263 6509b181 2022-12-30 thomas free($3);
264 6509b181 2022-12-30 thomas } body end {
265 6509b181 2022-12-30 thomas fputs("}\n", fp);
267 6509b181 2022-12-30 thomas | '{' TQFOREACH STRING STRING STRING '}' {
268 6509b181 2022-12-30 thomas fprintf(fp, "TAILQ_FOREACH(%s, %s, %s) {\n",
269 6509b181 2022-12-30 thomas $3, $4, $5);
270 6509b181 2022-12-30 thomas free($3);
271 6509b181 2022-12-30 thomas free($4);
272 6509b181 2022-12-30 thomas free($5);
273 6509b181 2022-12-30 thomas } body end {
274 6509b181 2022-12-30 thomas fputs("}\n", fp);
278 6509b181 2022-12-30 thomas end : '{' END '}'
281 6509b181 2022-12-30 thomas finally : '{' FINALLY '}' {
283 6509b181 2022-12-30 thomas fputs("err:\n", fp);
284 6509b181 2022-12-30 thomas } verbatims
287 6509b181 2022-12-30 thomas string : STRING string {
288 6509b181 2022-12-30 thomas if (asprintf(&$$, "%s %s", $1, $2) == -1)
289 6509b181 2022-12-30 thomas err(1, "asprintf");
290 6509b181 2022-12-30 thomas free($1);
291 6509b181 2022-12-30 thomas free($2);
296 6509b181 2022-12-30 thomas stringy : STRING
297 6509b181 2022-12-30 thomas | STRING stringy {
298 6509b181 2022-12-30 thomas if (asprintf(&$$, "%s %s", $1, $2) == -1)
299 6509b181 2022-12-30 thomas err(1, "asprintf");
300 6509b181 2022-12-30 thomas free($1);
301 6509b181 2022-12-30 thomas free($2);
303 6509b181 2022-12-30 thomas | '|' stringy {
304 6509b181 2022-12-30 thomas if (asprintf(&$$, "|%s", $2) == -1)
305 6509b181 2022-12-30 thomas err(1, "asprintf");
306 6509b181 2022-12-30 thomas free($2);
312 6509b181 2022-12-30 thomas struct keywords {
313 6509b181 2022-12-30 thomas const char *k_name;
314 6509b181 2022-12-30 thomas int k_val;
318 6509b181 2022-12-30 thomas yyerror(const char *fmt, ...)
320 6509b181 2022-12-30 thomas va_list ap;
321 6509b181 2022-12-30 thomas char *msg;
323 6509b181 2022-12-30 thomas file->errors++;
324 6509b181 2022-12-30 thomas va_start(ap, fmt);
325 6509b181 2022-12-30 thomas if (vasprintf(&msg, fmt, ap) == -1)
326 6509b181 2022-12-30 thomas err(1, "yyerror vasprintf");
327 6509b181 2022-12-30 thomas va_end(ap);
328 6509b181 2022-12-30 thomas fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
329 6509b181 2022-12-30 thomas free(msg);
330 6509b181 2022-12-30 thomas return (0);
334 6509b181 2022-12-30 thomas kw_cmp(const void *k, const void *e)
336 6509b181 2022-12-30 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
340 6509b181 2022-12-30 thomas lookup(char *s)
342 6509b181 2022-12-30 thomas /* this has to be sorted always */
343 6509b181 2022-12-30 thomas static const struct keywords keywords[] = {
344 6509b181 2022-12-30 thomas { "define", DEFINE },
345 6509b181 2022-12-30 thomas { "else", ELSE },
346 6509b181 2022-12-30 thomas { "end", END },
347 6509b181 2022-12-30 thomas { "finally", FINALLY },
348 6509b181 2022-12-30 thomas { "for", FOR },
349 6509b181 2022-12-30 thomas { "if", IF },
350 6509b181 2022-12-30 thomas { "include", INCLUDE },
351 6509b181 2022-12-30 thomas { "printf", PRINTF },
352 6509b181 2022-12-30 thomas { "render", RENDER },
353 6509b181 2022-12-30 thomas { "tailq-foreach", TQFOREACH },
354 6509b181 2022-12-30 thomas { "unsafe", UNSAFE },
355 6509b181 2022-12-30 thomas { "urlescape", URLESCAPE },
357 6509b181 2022-12-30 thomas const struct keywords *p;
359 6509b181 2022-12-30 thomas p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
363 6509b181 2022-12-30 thomas return (p->k_val);
365 6509b181 2022-12-30 thomas return (STRING);
368 6509b181 2022-12-30 thomas #define START_EXPAND 1
369 6509b181 2022-12-30 thomas #define DONE_EXPAND 2
371 6509b181 2022-12-30 thomas static int expanding;
374 6509b181 2022-12-30 thomas igetc(void)
378 6509b181 2022-12-30 thomas while (1) {
379 6509b181 2022-12-30 thomas if (file->ungetpos > 0)
380 6509b181 2022-12-30 thomas c = file->ungetbuf[--file->ungetpos];
382 6509b181 2022-12-30 thomas c = getc(file->stream);
384 6509b181 2022-12-30 thomas if (c == START_EXPAND)
385 6509b181 2022-12-30 thomas expanding = 1;
386 6509b181 2022-12-30 thomas else if (c == DONE_EXPAND)
387 6509b181 2022-12-30 thomas expanding = 0;
391 6509b181 2022-12-30 thomas return (c);
395 6509b181 2022-12-30 thomas lgetc(int quotec)
399 6509b181 2022-12-30 thomas if (quotec) {
400 6509b181 2022-12-30 thomas if ((c = igetc()) == EOF) {
401 6509b181 2022-12-30 thomas yyerror("reached end of filewhile parsing "
402 6509b181 2022-12-30 thomas "quoted string");
403 6509b181 2022-12-30 thomas if (file == topfile || popfile() == EOF)
404 6509b181 2022-12-30 thomas return (EOF);
405 6509b181 2022-12-30 thomas return (quotec);
407 6509b181 2022-12-30 thomas return (c);
410 6509b181 2022-12-30 thomas c = igetc();
411 6509b181 2022-12-30 thomas if (c == '\t' || c == ' ') {
412 6509b181 2022-12-30 thomas /* Compress blanks to a sigle space. */
414 6509b181 2022-12-30 thomas c = getc(file->stream);
415 6509b181 2022-12-30 thomas } while (c == '\t' || c == ' ');
416 6509b181 2022-12-30 thomas ungetc(c, file->stream);
420 6509b181 2022-12-30 thomas if (c == EOF) {
422 6509b181 2022-12-30 thomas * Fake EOL when hit EOF for the first time. This gets line
423 6509b181 2022-12-30 thomas * count rigchtif last line included file is syntactically
424 6509b181 2022-12-30 thomas * invalid and has no newline.
426 6509b181 2022-12-30 thomas if (file->eof_reached == 0) {
427 6509b181 2022-12-30 thomas file->eof_reached = 1;
428 6509b181 2022-12-30 thomas return ('\n');
430 6509b181 2022-12-30 thomas while (c == EOF) {
431 6509b181 2022-12-30 thomas if (file == topfile || popfile() == EOF)
432 6509b181 2022-12-30 thomas return (EOF);
433 6509b181 2022-12-30 thomas c = igetc();
436 6509b181 2022-12-30 thomas return (c);
440 6509b181 2022-12-30 thomas lungetc(int c)
442 6509b181 2022-12-30 thomas if (c == EOF)
445 6509b181 2022-12-30 thomas if (file->ungetpos >= file->ungetsize) {
446 6509b181 2022-12-30 thomas void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
447 6509b181 2022-12-30 thomas if (p == NULL)
448 6509b181 2022-12-30 thomas err(1, "reallocarray");
449 6509b181 2022-12-30 thomas file->ungetbuf = p;
450 6509b181 2022-12-30 thomas file->ungetsize *= 2;
452 6509b181 2022-12-30 thomas file->ungetbuf[file->ungetpos++] = c;
456 6509b181 2022-12-30 thomas findeol(void)
460 6509b181 2022-12-30 thomas /* skip to either EOF or the first real EOL */
461 6509b181 2022-12-30 thomas while (1) {
462 6509b181 2022-12-30 thomas c = lgetc(0);
463 6509b181 2022-12-30 thomas if (c == '\n') {
464 6509b181 2022-12-30 thomas file->lineno++;
467 6509b181 2022-12-30 thomas if (c == EOF)
470 6509b181 2022-12-30 thomas return (ERROR);
474 6509b181 2022-12-30 thomas yylex(void)
476 6509b181 2022-12-30 thomas char buf[8096];
477 6509b181 2022-12-30 thomas char *p = buf;
479 6509b181 2022-12-30 thomas int token;
480 6509b181 2022-12-30 thomas int starting = 0;
481 6509b181 2022-12-30 thomas int ending = 0;
482 6509b181 2022-12-30 thomas int quote = 0;
484 6509b181 2022-12-30 thomas if (!in_define && block == 0) {
485 6509b181 2022-12-30 thomas while ((c = lgetc(0)) != '{' && c != EOF) {
486 6509b181 2022-12-30 thomas if (c == '\n')
487 6509b181 2022-12-30 thomas file->lineno++;
490 6509b181 2022-12-30 thomas if (c == EOF)
491 6509b181 2022-12-30 thomas return (0);
493 6509b181 2022-12-30 thomas newblock:
494 6509b181 2022-12-30 thomas c = lgetc(0);
495 6509b181 2022-12-30 thomas if (c == '{' || c == '!') {
496 6509b181 2022-12-30 thomas if (c == '{')
497 6509b181 2022-12-30 thomas block = '}';
499 6509b181 2022-12-30 thomas block = c;
500 6509b181 2022-12-30 thomas return (c);
502 6509b181 2022-12-30 thomas if (c == '\n')
503 6509b181 2022-12-30 thomas file->lineno++;
506 6509b181 2022-12-30 thomas while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
507 6509b181 2022-12-30 thomas if (c == '\n')
508 6509b181 2022-12-30 thomas file->lineno++;
511 6509b181 2022-12-30 thomas if (c == EOF) {
512 6509b181 2022-12-30 thomas yyerror("unterminated block");
513 6509b181 2022-12-30 thomas return (0);
516 6509b181 2022-12-30 thomas yylval.lineno = file->lineno;
518 6509b181 2022-12-30 thomas if (block != 0 && c == block) {
519 6509b181 2022-12-30 thomas if ((c = lgetc(0)) == '}') {
520 6509b181 2022-12-30 thomas if (block == '!') {
521 6509b181 2022-12-30 thomas block = 0;
522 6509b181 2022-12-30 thomas return ('!');
524 6509b181 2022-12-30 thomas block = 0;
525 6509b181 2022-12-30 thomas return ('}');
527 6509b181 2022-12-30 thomas lungetc(c);
528 6509b181 2022-12-30 thomas c = block;
531 6509b181 2022-12-30 thomas if (in_define && block == 0) {
532 6509b181 2022-12-30 thomas if (c == '{')
533 6509b181 2022-12-30 thomas goto newblock;
536 6509b181 2022-12-30 thomas if (starting) {
537 6509b181 2022-12-30 thomas if (c == '!' || c == '{') {
538 6509b181 2022-12-30 thomas lungetc(c);
539 6509b181 2022-12-30 thomas lungetc('{');
542 6509b181 2022-12-30 thomas starting = 0;
543 6509b181 2022-12-30 thomas lungetc(c);
545 6509b181 2022-12-30 thomas } else if (c == '{') {
546 6509b181 2022-12-30 thomas starting = 1;
547 6509b181 2022-12-30 thomas continue;
550 6509b181 2022-12-30 thomas *p++ = c;
551 6509b181 2022-12-30 thomas if ((size_t)(p - buf) >= sizeof(buf)) {
552 6509b181 2022-12-30 thomas yyerror("string too long");
553 6509b181 2022-12-30 thomas return (findeol());
555 6509b181 2022-12-30 thomas } while ((c = lgetc(0)) != EOF && c != '\n');
556 6509b181 2022-12-30 thomas *p = '\0';
557 6509b181 2022-12-30 thomas if (c == EOF) {
558 6509b181 2022-12-30 thomas yyerror("unterminated block");
559 6509b181 2022-12-30 thomas return (0);
561 6509b181 2022-12-30 thomas if (c == '\n')
562 6509b181 2022-12-30 thomas file->lineno++;
563 6509b181 2022-12-30 thomas if ((yylval.v.string = strdup(buf)) == NULL)
564 6509b181 2022-12-30 thomas err(1, "strdup");
565 6509b181 2022-12-30 thomas return (STRING);
568 6509b181 2022-12-30 thomas if (block == '!') {
570 6509b181 2022-12-30 thomas if (ending) {
571 6509b181 2022-12-30 thomas if (c == '}') {
572 6509b181 2022-12-30 thomas lungetc(c);
573 6509b181 2022-12-30 thomas lungetc(block);
576 6509b181 2022-12-30 thomas ending = 0;
577 6509b181 2022-12-30 thomas lungetc(c);
578 6509b181 2022-12-30 thomas c = block;
579 6509b181 2022-12-30 thomas } else if (c == '!') {
580 6509b181 2022-12-30 thomas ending = 1;
581 6509b181 2022-12-30 thomas continue;
584 6509b181 2022-12-30 thomas *p++ = c;
585 6509b181 2022-12-30 thomas if ((size_t)(p - buf) >= sizeof(buf)) {
586 6509b181 2022-12-30 thomas yyerror("line too long");
587 6509b181 2022-12-30 thomas return (findeol());
589 6509b181 2022-12-30 thomas } while ((c = lgetc(0)) != EOF && c != '\n');
590 6509b181 2022-12-30 thomas *p = '\0';
592 6509b181 2022-12-30 thomas if (c == EOF) {
593 6509b181 2022-12-30 thomas yyerror("unterminated block");
594 6509b181 2022-12-30 thomas return (0);
596 6509b181 2022-12-30 thomas if (c == '\n')
597 6509b181 2022-12-30 thomas file->lineno++;
599 6509b181 2022-12-30 thomas if ((yylval.v.string = strdup(buf)) == NULL)
600 6509b181 2022-12-30 thomas err(1, "strdup");
601 6509b181 2022-12-30 thomas return (STRING);
604 6509b181 2022-12-30 thomas if (c == '|')
605 6509b181 2022-12-30 thomas return (c);
608 6509b181 2022-12-30 thomas if (!quote && isspace((unsigned char)c))
611 6509b181 2022-12-30 thomas if (c == '"')
612 6509b181 2022-12-30 thomas quote = !quote;
614 6509b181 2022-12-30 thomas if (!quote && c == '|') {
615 6509b181 2022-12-30 thomas lungetc(c);
619 6509b181 2022-12-30 thomas if (ending) {
620 6509b181 2022-12-30 thomas if (c == '}') {
621 6509b181 2022-12-30 thomas lungetc(c);
622 6509b181 2022-12-30 thomas lungetc('}');
625 6509b181 2022-12-30 thomas ending = 0;
626 6509b181 2022-12-30 thomas lungetc(c);
627 6509b181 2022-12-30 thomas c = block;
628 6509b181 2022-12-30 thomas } else if (!quote && c == '}') {
629 6509b181 2022-12-30 thomas ending = 1;
630 6509b181 2022-12-30 thomas continue;
633 6509b181 2022-12-30 thomas *p++ = c;
634 6509b181 2022-12-30 thomas if ((size_t)(p - buf) >= sizeof(buf)) {
635 6509b181 2022-12-30 thomas yyerror("string too long");
636 6509b181 2022-12-30 thomas return (findeol());
638 6509b181 2022-12-30 thomas } while ((c = lgetc(0)) != EOF);
639 6509b181 2022-12-30 thomas *p = '\0';
641 6509b181 2022-12-30 thomas if (c == EOF) {
642 6509b181 2022-12-30 thomas yyerror(quote ? "unterminated quote" : "unterminated block");
643 6509b181 2022-12-30 thomas return (0);
645 6509b181 2022-12-30 thomas if (c == '\n')
646 6509b181 2022-12-30 thomas file->lineno++;
647 6509b181 2022-12-30 thomas if ((token = lookup(buf)) == STRING)
648 6509b181 2022-12-30 thomas if ((yylval.v.string = strdup(buf)) == NULL)
649 6509b181 2022-12-30 thomas err(1, "strdup");
650 6509b181 2022-12-30 thomas return (token);
653 6509b181 2022-12-30 thomas struct file *
654 6509b181 2022-12-30 thomas pushfile(const char *name, int secret)
656 6509b181 2022-12-30 thomas struct file *nfile;
658 6509b181 2022-12-30 thomas if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
659 6509b181 2022-12-30 thomas err(1, "calloc");
660 6509b181 2022-12-30 thomas if ((nfile->name = strdup(name)) == NULL)
661 6509b181 2022-12-30 thomas err(1, "strdup");
662 6509b181 2022-12-30 thomas if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
663 6509b181 2022-12-30 thomas warn("can't open %s", nfile->name);
664 6509b181 2022-12-30 thomas free(nfile->name);
665 6509b181 2022-12-30 thomas free(nfile);
666 6509b181 2022-12-30 thomas return (NULL);
668 6509b181 2022-12-30 thomas nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
669 6509b181 2022-12-30 thomas nfile->ungetsize = 16;
670 6509b181 2022-12-30 thomas nfile->ungetbuf = malloc(nfile->ungetsize);
671 6509b181 2022-12-30 thomas if (nfile->ungetbuf == NULL)
672 6509b181 2022-12-30 thomas err(1, "malloc");
673 6509b181 2022-12-30 thomas TAILQ_INSERT_TAIL(&files, nfile, entry);
674 6509b181 2022-12-30 thomas return (nfile);
678 6509b181 2022-12-30 thomas popfile(void)
680 6509b181 2022-12-30 thomas struct file *prev;
682 6509b181 2022-12-30 thomas if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
683 6509b181 2022-12-30 thomas prev->errors += file->errors;
685 6509b181 2022-12-30 thomas TAILQ_REMOVE(&files, file, entry);
686 6509b181 2022-12-30 thomas fclose(file->stream);
687 6509b181 2022-12-30 thomas free(file->name);
688 6509b181 2022-12-30 thomas free(file->ungetbuf);
689 6509b181 2022-12-30 thomas free(file);
690 6509b181 2022-12-30 thomas file = prev;
691 6509b181 2022-12-30 thomas return (file ? 0 : EOF);
695 6509b181 2022-12-30 thomas parse(FILE *outfile, const char *filename)
697 6509b181 2022-12-30 thomas fp = outfile;
699 6509b181 2022-12-30 thomas if ((file = pushfile(filename, 0)) == 0)
700 6509b181 2022-12-30 thomas return (-1);
701 6509b181 2022-12-30 thomas topfile = file;
703 6509b181 2022-12-30 thomas yyparse();
704 6509b181 2022-12-30 thomas errors = file->errors;
705 6509b181 2022-12-30 thomas popfile();
707 6509b181 2022-12-30 thomas return (errors ? -1 : 0);
711 6509b181 2022-12-30 thomas dbg(void)
713 6509b181 2022-12-30 thomas if (nodebug)
716 6509b181 2022-12-30 thomas if (yylval.lineno == lastline + 1) {
717 6509b181 2022-12-30 thomas lastline = yylval.lineno;
720 6509b181 2022-12-30 thomas lastline = yylval.lineno;
722 6509b181 2022-12-30 thomas fprintf(fp, "#line %d ", yylval.lineno);
723 6509b181 2022-12-30 thomas printq(file->name);
724 6509b181 2022-12-30 thomas putc('\n', fp);
728 6509b181 2022-12-30 thomas printq(const char *str)
730 6509b181 2022-12-30 thomas putc('"', fp);
731 6509b181 2022-12-30 thomas for (; *str; ++str) {
732 6509b181 2022-12-30 thomas if (*str == '"')
733 6509b181 2022-12-30 thomas putc('\\', fp);
734 6509b181 2022-12-30 thomas putc(*str, fp);
736 6509b181 2022-12-30 thomas putc('"', fp);