2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2007-2016 Reyk Floeter <reyk@openbsd.org>
4 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 #include <sys/queue.h>
38 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
43 TAILQ_ENTRY(file) entry;
53 int parse(const char *);
54 struct file *pushfile(const char *, int);
58 int yyerror(const char *, ...)
59 __attribute__((__format__ (printf, 1, 2)))
60 __attribute__((__nonnull__ (1)));
61 int kw_cmp(const void *, const void *);
69 void printq(const char *);
87 %token DEFINE ELSE END ERROR ESCAPE FINALLY IF INCLUDE
88 %token RENDER TQFOREACH UNSAFE URLESCAPE
89 %token <v.string> STRING
90 %type <v.string> string
98 | grammar error { file->errors++; }
101 include : INCLUDE STRING {
104 if ((nfile = pushfile($2, 0)) == NULL) {
105 yyerror("failed to include file %s", $2);
116 verbatim : '!' verbatim1 '!' {
118 /* TODO: check template status and exit in case */
123 verbatim1 : /* empty */
133 verbatims : /* empty */
139 printf("if (tp->tp_puts(tp, ");
141 printf(") == -1) goto %s;\n", lerr);
147 block : define body end {
148 printf("%s:\n", lerr);
149 puts("return tp->tp_ret;");
153 | define body finally end {
154 puts("return tp->tp_ret;");
160 define : '{' DEFINE string '}' {
162 (void)snprintf(lerr, sizeof(lerr), "err%llu",
163 (unsigned long long)arc4random());
166 printf("int\n%s\n{\n", $3);
178 special : '{' RENDER string '}' {
180 if (strrchr($3, ')') != NULL)
181 printf("if (%s == -1) goto %s;\n",
184 printf("if (%s != NULL && %s(tp) == -1) "
185 "goto %s;\n", $3, $3, lerr);
188 | if body endif { puts("}"); }
190 | '{' string '|' ESCAPE '}' {
192 printf("if (tp->tp_escape(tp, %s) == -1) goto %s;\n",
196 | '{' string '|' UNSAFE '}' {
198 printf("if (tp->tp_puts(tp, %s) == -1) goto %s;\n",
202 | '{' string '|' URLESCAPE '}' {
204 printf("if (tp_urlescape(tp, %s) == -1) goto %s;\n",
210 printf("if (tp->tp_escape(tp, %s) == -1) goto %s;\n",
216 if : '{' IF string '}' {
218 printf("if (%s) {\n", $3);
228 elsif : '{' ELSE IF string '}' {
230 printf("} else if (%s) {\n", $4);
235 else : '{' ELSE '}' {
241 loop : '{' TQFOREACH STRING STRING STRING '}' {
242 printf("TAILQ_FOREACH(%s, %s, %s) {\n",
255 finally : '{' FINALLY '}' {
257 printf("%s:\n", lerr);
261 string : STRING string {
262 if (asprintf(&$$, "%s %s", $1, $2) == -1)
278 yyerror(const char *fmt, ...)
285 if (vasprintf(&msg, fmt, ap) == -1)
286 err(1, "yyerror vasprintf");
288 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
294 kw_cmp(const void *k, const void *e)
296 return (strcmp(k, ((const struct keywords *)e)->k_name));
302 /* this has to be sorted always */
303 static const struct keywords keywords[] = {
304 { "define", DEFINE },
307 { "escape", ESCAPE },
308 { "finally", FINALLY },
310 { "include", INCLUDE },
311 { "render", RENDER },
312 { "tailq-foreach", TQFOREACH },
313 { "unsafe", UNSAFE },
314 { "urlescape", URLESCAPE },
316 const struct keywords *p;
318 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
327 #define START_EXPAND 1
328 #define DONE_EXPAND 2
330 static int expanding;
338 if (file->ungetpos > 0)
339 c = file->ungetbuf[--file->ungetpos];
341 c = getc(file->stream);
343 if (c == START_EXPAND)
345 else if (c == DONE_EXPAND)
359 if ((c = igetc()) == EOF) {
360 yyerror("reached end of filewhile parsing "
362 if (file == topfile || popfile() == EOF)
369 while ((c = igetc()) == '\\') {
375 yylval.lineno = file->lineno;
378 if (c == '\t' || c == ' ') {
379 /* Compress blanks to a sigle space. */
381 c = getc(file->stream);
382 } while (c == '\t' || c == ' ');
383 ungetc(c, file->stream);
389 * Fake EOL when hit EOF for the first time. This gets line
390 * count rigchtif last line included file is syntactically
391 * invalid and has no newline.
393 if (file->eof_reached == 0) {
394 file->eof_reached = 1;
398 if (file == topfile || popfile() == EOF)
412 if (file->ungetpos >= file->ungetsize) {
413 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
415 err(1, "reallocarray");
417 file->ungetsize *= 2;
419 file->ungetbuf[file->ungetpos++] = c;
427 /* skip to either EOF or the first real EOL */
450 if (!in_define && block == 0) {
451 while ((c = lgetc(0)) != '{' && c != EOF) {
461 if (c == '{' || c == '!') {
472 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
478 yyerror("unterminated block");
482 yylval.lineno = file->lineno;
484 if (block != 0 && c == block) {
485 if ((c = lgetc(0)) == '}') {
497 if (in_define && block == 0) {
503 if (c == '!' || c == '{') {
511 } else if (c == '{') {
517 if ((size_t)(p - buf) >= sizeof(buf)) {
518 yyerror("string too long");
521 } while ((c = lgetc(0)) != EOF && c != '\n');
524 yyerror("unterminated block");
529 if ((yylval.v.string = strdup(buf)) == NULL)
545 } else if (c == '!') {
551 if ((size_t)(p - buf) >= sizeof(buf)) {
552 yyerror("line too long");
555 } while ((c = lgetc(0)) != EOF && c != '\n');
559 yyerror("unterminated block");
565 if ((yylval.v.string = strdup(buf)) == NULL)
588 } else if (c == '}') {
594 if ((size_t)(p - buf) >= sizeof(buf)) {
595 yyerror("string too long");
598 } while ((c = lgetc(0)) != EOF && !isspace((unsigned char)c));
602 yyerror("unterminated block");
607 if ((token = lookup(buf)) == STRING)
608 if ((yylval.v.string = strdup(buf)) == NULL)
614 pushfile(const char *name, int secret)
618 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
620 if ((nfile->name = strdup(name)) == NULL)
622 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
623 warn("can't open %s", nfile->name);
628 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
629 nfile->ungetsize = 16;
630 nfile->ungetbuf = malloc(nfile->ungetsize);
631 if (nfile->ungetbuf == NULL)
633 TAILQ_INSERT_TAIL(&files, nfile, entry);
642 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
643 prev->errors += file->errors;
645 TAILQ_REMOVE(&files, file, entry);
646 fclose(file->stream);
648 free(file->ungetbuf);
651 return (file ? 0 : EOF);
655 parse(const char *filename)
657 if ((file = pushfile(filename, 0)) == 0)
662 errors = file->errors;
665 return (errors ? -1 : 0);
674 printf("#line %d ", yylval.lineno);
680 printq(const char *str)
683 for (; *str; ++str) {