2 bff58270 2022-09-19 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 bff58270 2022-09-19 op * Copyright (c) 2007-2016 Reyk Floeter <reyk@openbsd.org>
4 bff58270 2022-09-19 op * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 bff58270 2022-09-19 op * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 bff58270 2022-09-19 op * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 bff58270 2022-09-19 op * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 bff58270 2022-09-19 op * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 bff58270 2022-09-19 op * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 bff58270 2022-09-19 op * Permission to use, copy, modify, and distribute this software for any
12 bff58270 2022-09-19 op * purpose with or without fee is hereby granted, provided that the above
13 bff58270 2022-09-19 op * copyright notice and this permission notice appear in all copies.
15 bff58270 2022-09-19 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 bff58270 2022-09-19 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 bff58270 2022-09-19 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 bff58270 2022-09-19 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 bff58270 2022-09-19 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 bff58270 2022-09-19 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 bff58270 2022-09-19 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 bff58270 2022-09-19 op #include <sys/queue.h>
28 bff58270 2022-09-19 op #include <ctype.h>
29 bff58270 2022-09-19 op #include <err.h>
30 bff58270 2022-09-19 op #include <stdio.h>
31 bff58270 2022-09-19 op #include <stdlib.h>
32 bff58270 2022-09-19 op #include <stdarg.h>
33 bff58270 2022-09-19 op #include <stdint.h>
34 bff58270 2022-09-19 op #include <string.h>
35 bff58270 2022-09-19 op #include <unistd.h>
37 bff58270 2022-09-19 op #ifndef nitems
38 bff58270 2022-09-19 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 bff58270 2022-09-19 op TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
42 bff58270 2022-09-19 op static struct file {
43 bff58270 2022-09-19 op TAILQ_ENTRY(file) entry;
46 bff58270 2022-09-19 op size_t ungetpos;
47 bff58270 2022-09-19 op size_t ungetsize;
48 bff58270 2022-09-19 op u_char *ungetbuf;
49 bff58270 2022-09-19 op int eof_reached;
52 bff58270 2022-09-19 op } *file, *topfile;
53 bff58270 2022-09-19 op int parse(const char *);
54 bff58270 2022-09-19 op struct file *pushfile(const char *, int);
55 bff58270 2022-09-19 op int popfile(void);
56 bff58270 2022-09-19 op int yyparse(void);
57 bff58270 2022-09-19 op int yylex(void);
58 bff58270 2022-09-19 op int yyerror(const char *, ...)
59 bff58270 2022-09-19 op __attribute__((__format__ (printf, 1, 2)))
60 bff58270 2022-09-19 op __attribute__((__nonnull__ (1)));
61 bff58270 2022-09-19 op int kw_cmp(const void *, const void *);
62 bff58270 2022-09-19 op int lookup(char *);
63 bff58270 2022-09-19 op int igetc(void);
64 bff58270 2022-09-19 op int lgetc(int);
65 bff58270 2022-09-19 op void lungetc(int);
66 bff58270 2022-09-19 op int findeol(void);
68 bff58270 2022-09-19 op void dbg(void);
69 bff58270 2022-09-19 op void printq(const char *);
71 bff58270 2022-09-19 op extern int nodebug;
73 bff58270 2022-09-19 op static int block;
74 bff58270 2022-09-19 op static int in_define;
75 bff58270 2022-09-19 op static int errors;
76 bff58270 2022-09-19 op static char lerr[32];
78 bff58270 2022-09-19 op typedef struct {
87 bff58270 2022-09-19 op %token DEFINE ELSE END ERROR ESCAPE FINALLY IF INCLUDE
88 bff58270 2022-09-19 op %token RENDER TQFOREACH UNSAFE URLESCAPE
89 bff58270 2022-09-19 op %token <v.string> STRING
90 bff58270 2022-09-19 op %type <v.string> string
94 bff58270 2022-09-19 op grammar : /* empty */
95 bff58270 2022-09-19 op | grammar include
96 bff58270 2022-09-19 op | grammar verbatim
97 bff58270 2022-09-19 op | grammar block
98 bff58270 2022-09-19 op | grammar error { file->errors++; }
101 bff58270 2022-09-19 op include : INCLUDE STRING {
102 bff58270 2022-09-19 op struct file *nfile;
104 bff58270 2022-09-19 op if ((nfile = pushfile($2, 0)) == NULL) {
105 bff58270 2022-09-19 op yyerror("failed to include file %s", $2);
111 bff58270 2022-09-19 op file = nfile;
112 bff58270 2022-09-19 op lungetc('\n');
116 bff58270 2022-09-19 op verbatim : '!' verbatim1 '!' {
117 bff58270 2022-09-19 op if (in_define) {
118 bff58270 2022-09-19 op /* TODO: check template status and exit in case */
123 bff58270 2022-09-19 op verbatim1 : /* empty */
124 bff58270 2022-09-19 op | verbatim1 STRING {
125 bff58270 2022-09-19 op if (*$2 != '\0') {
133 bff58270 2022-09-19 op verbatims : /* empty */
134 bff58270 2022-09-19 op | verbatims verbatim
137 bff58270 2022-09-19 op raw : STRING {
139 bff58270 2022-09-19 op printf("if (tp->tp_puts(tp, ");
141 bff58270 2022-09-19 op printf(") == -1) goto %s;\n", lerr);
147 bff58270 2022-09-19 op block : define body end {
148 bff58270 2022-09-19 op printf("%s:\n", lerr);
149 bff58270 2022-09-19 op puts("return tp->tp_ret;");
151 bff58270 2022-09-19 op in_define = 0;
153 bff58270 2022-09-19 op | define body finally end {
154 bff58270 2022-09-19 op puts("return tp->tp_ret;");
156 bff58270 2022-09-19 op in_define = 0;
160 bff58270 2022-09-19 op define : '{' DEFINE string '}' {
161 bff58270 2022-09-19 op in_define = 1;
162 bff58270 2022-09-19 op (void)snprintf(lerr, sizeof(lerr), "err%llu",
163 bff58270 2022-09-19 op (unsigned long long)arc4random());
166 bff58270 2022-09-19 op printf("int\n%s\n{\n", $3);
172 bff58270 2022-09-19 op body : /* empty */
173 bff58270 2022-09-19 op | body verbatim
175 bff58270 2022-09-19 op | body special
178 bff58270 2022-09-19 op special : '{' RENDER string '}' {
180 bff58270 2022-09-19 op if (strrchr($3, ')') != NULL)
181 bff58270 2022-09-19 op printf("if (%s == -1) goto %s;\n",
184 bff58270 2022-09-19 op printf("if (%s != NULL && %s(tp) == -1) "
185 bff58270 2022-09-19 op "goto %s;\n", $3, $3, lerr);
188 bff58270 2022-09-19 op | if body endif { puts("}"); }
190 bff58270 2022-09-19 op | '{' string '|' ESCAPE '}' {
192 bff58270 2022-09-19 op printf("if (tp->tp_escape(tp, %s) == -1) goto %s;\n",
196 bff58270 2022-09-19 op | '{' string '|' UNSAFE '}' {
198 bff58270 2022-09-19 op printf("if (tp->tp_puts(tp, %s) == -1) goto %s;\n",
202 bff58270 2022-09-19 op | '{' string '|' URLESCAPE '}' {
204 bff58270 2022-09-19 op printf("if (tp_urlescape(tp, %s) == -1) goto %s;\n",
208 bff58270 2022-09-19 op | '{' string '}' {
210 bff58270 2022-09-19 op printf("if (tp->tp_escape(tp, %s) == -1) goto %s;\n",
216 bff58270 2022-09-19 op if : '{' IF string '}' {
218 bff58270 2022-09-19 op printf("if (%s) {\n", $3);
224 bff58270 2022-09-19 op | else body end
225 bff58270 2022-09-19 op | elsif body endif
228 bff58270 2022-09-19 op elsif : '{' ELSE IF string '}' {
230 bff58270 2022-09-19 op printf("} else if (%s) {\n", $4);
235 bff58270 2022-09-19 op else : '{' ELSE '}' {
237 bff58270 2022-09-19 op puts("} else {");
241 bff58270 2022-09-19 op loop : '{' TQFOREACH STRING STRING STRING '}' {
242 bff58270 2022-09-19 op printf("TAILQ_FOREACH(%s, %s, %s) {\n",
252 bff58270 2022-09-19 op end : '{' END '}'
255 bff58270 2022-09-19 op finally : '{' FINALLY '}' {
257 bff58270 2022-09-19 op printf("%s:\n", lerr);
261 bff58270 2022-09-19 op string : STRING string {
262 bff58270 2022-09-19 op if (asprintf(&$$, "%s %s", $1, $2) == -1)
263 bff58270 2022-09-19 op err(1, "asprintf");
272 bff58270 2022-09-19 op struct keywords {
273 bff58270 2022-09-19 op const char *k_name;
278 bff58270 2022-09-19 op yyerror(const char *fmt, ...)
283 bff58270 2022-09-19 op file->errors++;
284 bff58270 2022-09-19 op va_start(ap, fmt);
285 bff58270 2022-09-19 op if (vasprintf(&msg, fmt, ap) == -1)
286 bff58270 2022-09-19 op err(1, "yyerror vasprintf");
288 bff58270 2022-09-19 op fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
294 bff58270 2022-09-19 op kw_cmp(const void *k, const void *e)
296 bff58270 2022-09-19 op return (strcmp(k, ((const struct keywords *)e)->k_name));
300 bff58270 2022-09-19 op lookup(char *s)
302 bff58270 2022-09-19 op /* this has to be sorted always */
303 bff58270 2022-09-19 op static const struct keywords keywords[] = {
304 bff58270 2022-09-19 op { "define", DEFINE },
305 bff58270 2022-09-19 op { "else", ELSE },
306 bff58270 2022-09-19 op { "end", END },
307 bff58270 2022-09-19 op { "escape", ESCAPE },
308 bff58270 2022-09-19 op { "finally", FINALLY },
309 bff58270 2022-09-19 op { "if", IF },
310 bff58270 2022-09-19 op { "include", INCLUDE },
311 bff58270 2022-09-19 op { "render", RENDER },
312 bff58270 2022-09-19 op { "tailq-foreach", TQFOREACH },
313 bff58270 2022-09-19 op { "unsafe", UNSAFE },
314 bff58270 2022-09-19 op { "urlescape", URLESCAPE },
316 bff58270 2022-09-19 op const struct keywords *p;
318 bff58270 2022-09-19 op p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
322 bff58270 2022-09-19 op return (p->k_val);
324 bff58270 2022-09-19 op return (STRING);
327 bff58270 2022-09-19 op #define START_EXPAND 1
328 bff58270 2022-09-19 op #define DONE_EXPAND 2
330 bff58270 2022-09-19 op static int expanding;
338 bff58270 2022-09-19 op if (file->ungetpos > 0)
339 bff58270 2022-09-19 op c = file->ungetbuf[--file->ungetpos];
341 bff58270 2022-09-19 op c = getc(file->stream);
343 bff58270 2022-09-19 op if (c == START_EXPAND)
344 bff58270 2022-09-19 op expanding = 1;
345 bff58270 2022-09-19 op else if (c == DONE_EXPAND)
346 bff58270 2022-09-19 op expanding = 0;
354 bff58270 2022-09-19 op lgetc(int quotec)
358 bff58270 2022-09-19 op if (quotec) {
359 bff58270 2022-09-19 op if ((c = igetc()) == EOF) {
360 bff58270 2022-09-19 op yyerror("reached end of filewhile parsing "
361 bff58270 2022-09-19 op "quoted string");
362 bff58270 2022-09-19 op if (file == topfile || popfile() == EOF)
363 bff58270 2022-09-19 op return (EOF);
364 bff58270 2022-09-19 op return (quotec);
369 bff58270 2022-09-19 op while ((c = igetc()) == '\\') {
370 bff58270 2022-09-19 op next = igetc();
371 bff58270 2022-09-19 op if (next != '\n') {
375 bff58270 2022-09-19 op yylval.lineno = file->lineno;
376 bff58270 2022-09-19 op file->lineno++;
378 bff58270 2022-09-19 op if (c == '\t' || c == ' ') {
379 bff58270 2022-09-19 op /* Compress blanks to a sigle space. */
381 bff58270 2022-09-19 op c = getc(file->stream);
382 bff58270 2022-09-19 op } while (c == '\t' || c == ' ');
383 bff58270 2022-09-19 op ungetc(c, file->stream);
387 bff58270 2022-09-19 op if (c == EOF) {
389 bff58270 2022-09-19 op * Fake EOL when hit EOF for the first time. This gets line
390 bff58270 2022-09-19 op * count rigchtif last line included file is syntactically
391 bff58270 2022-09-19 op * invalid and has no newline.
393 bff58270 2022-09-19 op if (file->eof_reached == 0) {
394 bff58270 2022-09-19 op file->eof_reached = 1;
395 bff58270 2022-09-19 op return ('\n');
397 bff58270 2022-09-19 op while (c == EOF) {
398 bff58270 2022-09-19 op if (file == topfile || popfile() == EOF)
399 bff58270 2022-09-19 op return (EOF);
407 bff58270 2022-09-19 op lungetc(int c)
409 bff58270 2022-09-19 op if (c == EOF)
412 bff58270 2022-09-19 op if (file->ungetpos >= file->ungetsize) {
413 bff58270 2022-09-19 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
414 bff58270 2022-09-19 op if (p == NULL)
415 bff58270 2022-09-19 op err(1, "reallocarray");
416 bff58270 2022-09-19 op file->ungetbuf = p;
417 bff58270 2022-09-19 op file->ungetsize *= 2;
419 bff58270 2022-09-19 op file->ungetbuf[file->ungetpos++] = c;
423 bff58270 2022-09-19 op findeol(void)
427 bff58270 2022-09-19 op /* skip to either EOF or the first real EOL */
429 bff58270 2022-09-19 op c = lgetc(0);
430 bff58270 2022-09-19 op if (c == '\n') {
431 bff58270 2022-09-19 op file->lineno++;
434 bff58270 2022-09-19 op if (c == EOF)
437 bff58270 2022-09-19 op return (ERROR);
443 bff58270 2022-09-19 op char buf[8096];
444 bff58270 2022-09-19 op char *p = buf;
447 bff58270 2022-09-19 op int starting = 0;
448 bff58270 2022-09-19 op int ending = 0;
450 bff58270 2022-09-19 op if (!in_define && block == 0) {
451 bff58270 2022-09-19 op while ((c = lgetc(0)) != '{' && c != EOF) {
452 bff58270 2022-09-19 op if (c == '\n')
453 bff58270 2022-09-19 op file->lineno++;
456 bff58270 2022-09-19 op if (c == EOF)
460 bff58270 2022-09-19 op c = lgetc(0);
461 bff58270 2022-09-19 op if (c == '{' || c == '!') {
462 bff58270 2022-09-19 op if (c == '{')
468 bff58270 2022-09-19 op if (c == '\n')
469 bff58270 2022-09-19 op file->lineno++;
472 bff58270 2022-09-19 op while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
473 bff58270 2022-09-19 op if (c == '\n')
474 bff58270 2022-09-19 op file->lineno++;
477 bff58270 2022-09-19 op if (c == EOF) {
478 bff58270 2022-09-19 op yyerror("unterminated block");
482 bff58270 2022-09-19 op yylval.lineno = file->lineno;
484 bff58270 2022-09-19 op if (block != 0 && c == block) {
485 bff58270 2022-09-19 op if ((c = lgetc(0)) == '}') {
486 bff58270 2022-09-19 op if (block == '!') {
488 bff58270 2022-09-19 op return ('!');
491 bff58270 2022-09-19 op return ('}');
497 bff58270 2022-09-19 op if (in_define && block == 0) {
498 bff58270 2022-09-19 op if (c == '{')
499 bff58270 2022-09-19 op goto newblock;
502 bff58270 2022-09-19 op if (starting) {
503 bff58270 2022-09-19 op if (c == '!' || c == '{') {
504 bff58270 2022-09-19 op lungetc('{');
508 bff58270 2022-09-19 op starting = 0;
511 bff58270 2022-09-19 op } else if (c == '{') {
512 bff58270 2022-09-19 op starting = 1;
517 bff58270 2022-09-19 op if ((size_t)(p - buf) >= sizeof(buf)) {
518 bff58270 2022-09-19 op yyerror("string too long");
519 bff58270 2022-09-19 op return (findeol());
521 bff58270 2022-09-19 op } while ((c = lgetc(0)) != EOF && c != '\n');
523 bff58270 2022-09-19 op if (c == EOF) {
524 bff58270 2022-09-19 op yyerror("unterminated block");
527 bff58270 2022-09-19 op if (c == '\n')
528 bff58270 2022-09-19 op file->lineno++;
529 bff58270 2022-09-19 op if ((yylval.v.string = strdup(buf)) == NULL)
530 bff58270 2022-09-19 op err(1, "strdup");
531 bff58270 2022-09-19 op return (STRING);
534 bff58270 2022-09-19 op if (block == '!') {
536 bff58270 2022-09-19 op if (ending) {
537 bff58270 2022-09-19 op if (c == '}') {
539 bff58270 2022-09-19 op lungetc(block);
545 bff58270 2022-09-19 op } else if (c == '!') {
551 bff58270 2022-09-19 op if ((size_t)(p - buf) >= sizeof(buf)) {
552 bff58270 2022-09-19 op yyerror("line too long");
553 bff58270 2022-09-19 op return (findeol());
555 bff58270 2022-09-19 op } while ((c = lgetc(0)) != EOF && c != '\n');
558 bff58270 2022-09-19 op if (c == EOF) {
559 bff58270 2022-09-19 op yyerror("unterminated block");
562 bff58270 2022-09-19 op if (c == '\n')
563 bff58270 2022-09-19 op file->lineno++;
565 bff58270 2022-09-19 op if ((yylval.v.string = strdup(buf)) == NULL)
566 bff58270 2022-09-19 op err(1, "strdup");
567 bff58270 2022-09-19 op return (STRING);
570 bff58270 2022-09-19 op if (c == '|')
574 bff58270 2022-09-19 op if (c == '|') {
579 bff58270 2022-09-19 op if (ending) {
580 bff58270 2022-09-19 op if (c == '}') {
582 bff58270 2022-09-19 op lungetc('}');
588 bff58270 2022-09-19 op } else if (c == '}') {
594 bff58270 2022-09-19 op if ((size_t)(p - buf) >= sizeof(buf)) {
595 bff58270 2022-09-19 op yyerror("string too long");
596 bff58270 2022-09-19 op return (findeol());
598 bff58270 2022-09-19 op } while ((c = lgetc(0)) != EOF && !isspace((unsigned char)c));
601 bff58270 2022-09-19 op if (c == EOF) {
602 bff58270 2022-09-19 op yyerror("unterminated block");
605 bff58270 2022-09-19 op if (c == '\n')
606 bff58270 2022-09-19 op file->lineno++;
607 bff58270 2022-09-19 op if ((token = lookup(buf)) == STRING)
608 bff58270 2022-09-19 op if ((yylval.v.string = strdup(buf)) == NULL)
609 bff58270 2022-09-19 op err(1, "strdup");
610 bff58270 2022-09-19 op return (token);
613 bff58270 2022-09-19 op struct file *
614 bff58270 2022-09-19 op pushfile(const char *name, int secret)
616 bff58270 2022-09-19 op struct file *nfile;
618 bff58270 2022-09-19 op if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
619 bff58270 2022-09-19 op err(1, "calloc");
620 bff58270 2022-09-19 op if ((nfile->name = strdup(name)) == NULL)
621 bff58270 2022-09-19 op err(1, "strdup");
622 bff58270 2022-09-19 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
623 bff58270 2022-09-19 op warn("can't open %s", nfile->name);
624 bff58270 2022-09-19 op free(nfile->name);
626 bff58270 2022-09-19 op return (NULL);
628 bff58270 2022-09-19 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
629 bff58270 2022-09-19 op nfile->ungetsize = 16;
630 bff58270 2022-09-19 op nfile->ungetbuf = malloc(nfile->ungetsize);
631 bff58270 2022-09-19 op if (nfile->ungetbuf == NULL)
632 bff58270 2022-09-19 op err(1, "malloc");
633 bff58270 2022-09-19 op TAILQ_INSERT_TAIL(&files, nfile, entry);
634 bff58270 2022-09-19 op return (nfile);
638 bff58270 2022-09-19 op popfile(void)
640 bff58270 2022-09-19 op struct file *prev;
642 bff58270 2022-09-19 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
643 bff58270 2022-09-19 op prev->errors += file->errors;
645 bff58270 2022-09-19 op TAILQ_REMOVE(&files, file, entry);
646 bff58270 2022-09-19 op fclose(file->stream);
647 bff58270 2022-09-19 op free(file->name);
648 bff58270 2022-09-19 op free(file->ungetbuf);
651 bff58270 2022-09-19 op return (file ? 0 : EOF);
655 bff58270 2022-09-19 op parse(const char *filename)
657 bff58270 2022-09-19 op if ((file = pushfile(filename, 0)) == 0)
659 bff58270 2022-09-19 op topfile = file;
662 bff58270 2022-09-19 op errors = file->errors;
665 bff58270 2022-09-19 op return (errors ? -1 : 0);
674 bff58270 2022-09-19 op printf("#line %d ", yylval.lineno);
675 bff58270 2022-09-19 op printq(file->name);
676 bff58270 2022-09-19 op putchar('\n');
680 bff58270 2022-09-19 op printq(const char *str)
682 bff58270 2022-09-19 op putchar('"');
683 bff58270 2022-09-19 op for (; *str; ++str) {
684 bff58270 2022-09-19 op if (*str == '"')
685 bff58270 2022-09-19 op putchar('\\');
686 bff58270 2022-09-19 op putchar(*str);
688 bff58270 2022-09-19 op putchar('"');