2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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;
48 unsigned char *ungetbuf;
53 int parse(FILE *, 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 *);
78 static int lastline = -1;
89 %token DEFINE ELSE END ERROR FINALLY FOR IF INCLUDE PRINTF
90 %token RENDER TQFOREACH UNSAFE URLESCAPE WHILE
91 %token <v.string> STRING
92 %type <v.string> string nstring
93 %type <v.string> stringy
101 | grammar error { file->errors++; }
104 include : INCLUDE STRING {
107 if ((nfile = pushfile($2, 0)) == NULL) {
108 yyerror("failed to include file %s", $2);
119 verbatim : '!' verbatim1 '!' {
121 /* TODO: check template status and exit in case */
126 verbatim1 : /* empty */
130 fprintf(fp, "%s\n", $2);
136 verbatims : /* empty */
142 fprintf(fp, "if ((tp_ret = tp_write(tp, ");
144 fprintf(fp, ", %zu)) == -1) goto err;\n",
151 block : define body end {
153 fputs("return tp_ret;\n", fp);
157 | define body finally end {
158 fputs("return tp_ret;\n", fp);
164 define : '{' DEFINE string '}' {
168 fprintf(fp, "int\n%s\n{\n", $3);
169 fputs("int tp_ret = 0;\n", fp);
181 special : '{' RENDER string '}' {
183 fprintf(fp, "if ((tp_ret = %s) == -1) goto err;\n",
188 | if body endif { fputs("}\n", fp); }
190 | '{' string '|' UNSAFE '}' {
193 "if ((tp_ret = tp_writes(tp, %s)) == -1)\n",
195 fputs("goto err;\n", fp);
198 | '{' string '|' URLESCAPE '}' {
201 "if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
203 fputs("goto err;\n", fp);
209 "if ((tp_ret = tp_htmlescape(tp, %s)) == -1)\n",
211 fputs("goto err;\n", fp);
216 printf : '{' PRINTF {
218 fprintf(fp, "if (asprintf(&tp->tp_tmp, ");
220 fputs(") == -1)\n", fp);
221 fputs("goto err;\n", fp);
222 fputs("if ((tp_ret = tp_htmlescape(tp, tp->tp_tmp)) "
224 fputs("goto err;\n", fp);
225 fputs("free(tp->tp_tmp);\n", fp);
226 fputs("tp->tp_tmp = NULL;\n", fp);
230 printfargs : /* empty */
231 | printfargs STRING {
232 fprintf(fp, " %s", $2);
237 if : '{' IF stringy '}' {
239 fprintf(fp, "if (%s) {\n", $3);
249 elsif : '{' ELSE IF stringy '}' {
251 fprintf(fp, "} else if (%s) {\n", $4);
256 else : '{' ELSE '}' {
258 fputs("} else {\n", fp);
262 loop : '{' FOR stringy '}' {
263 fprintf(fp, "for (%s) {\n", $3);
268 | '{' TQFOREACH STRING STRING STRING '}' {
269 fprintf(fp, "TAILQ_FOREACH(%s, %s, %s) {\n",
277 | '{' WHILE stringy '}' {
278 fprintf(fp, "while (%s) {\n", $3);
288 finally : '{' FINALLY '}' {
294 nstring : STRING nstring {
295 if (asprintf(&$$, "%s%s", $1, $2) == -1)
303 string : STRING string {
304 if (asprintf(&$$, "%s %s", $1, $2) == -1)
314 if (asprintf(&$$, "%s %s", $1, $2) == -1)
320 if (asprintf(&$$, "|%s", $2) == -1)
334 yyerror(const char *fmt, ...)
341 if (vasprintf(&msg, fmt, ap) == -1)
342 err(1, "yyerror vasprintf");
344 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
350 kw_cmp(const void *k, const void *e)
352 return (strcmp(k, ((const struct keywords *)e)->k_name));
358 /* this has to be sorted always */
359 static const struct keywords keywords[] = {
360 { "define", DEFINE },
363 { "finally", FINALLY },
366 { "include", INCLUDE },
367 { "printf", PRINTF },
368 { "render", RENDER },
369 { "tailq-foreach", TQFOREACH },
370 { "unsafe", UNSAFE },
371 { "urlescape", URLESCAPE },
374 const struct keywords *p;
376 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
385 #define START_EXPAND 1
386 #define DONE_EXPAND 2
388 static int expanding;
396 if (file->ungetpos > 0)
397 c = file->ungetbuf[--file->ungetpos];
399 c = getc(file->stream);
401 if (c == START_EXPAND)
403 else if (c == DONE_EXPAND)
417 if ((c = igetc()) == EOF) {
418 yyerror("reached end of filewhile parsing "
420 if (file == topfile || popfile() == EOF)
428 if (c == '\t' || c == ' ') {
429 /* Compress blanks to a sigle space. */
431 c = getc(file->stream);
432 } while (c == '\t' || c == ' ');
433 ungetc(c, file->stream);
439 * Fake EOL when hit EOF for the first time. This gets line
440 * count right if last line in included file is syntactically
441 * invalid and has no newline.
443 if (file->eof_reached == 0) {
444 file->eof_reached = 1;
448 if (file == topfile || popfile() == EOF)
462 if (file->ungetpos >= file->ungetsize) {
463 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
465 err(1, "reallocarray");
467 file->ungetsize *= 2;
469 file->ungetbuf[file->ungetpos++] = c;
477 /* skip to either EOF or the first real EOL */
501 if (!in_define && block == 0) {
502 while ((c = lgetc(0)) != '{' && c != EOF) {
512 if (c == '{' || c == '!') {
523 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
529 yyerror("unterminated block");
533 yylval.lineno = file->lineno;
535 if (block != 0 && c == block) {
536 if ((c = lgetc(0)) == '}') {
548 if (in_define && block == 0) {
554 if (c == '!' || c == '{') {
562 } else if (c == '{') {
565 } else if (c == '\n')
569 if ((size_t)(p - buf) >= sizeof(buf)) {
570 yyerror("string too long");
573 } while ((c = lgetc(0)) != EOF);
576 yyerror("unterminated block");
581 if ((yylval.v.string = strdup(buf)) == NULL)
597 } else if (c == '!') {
600 } else if (c == '\n')
604 if ((size_t)(p - buf) >= sizeof(buf)) {
605 yyerror("line too long");
608 } while ((c = lgetc(0)) != EOF);
612 yyerror("unterminated block");
618 if ((yylval.v.string = strdup(buf)) == NULL)
627 if (!quote && isspace((unsigned char)c))
633 if (!quote && c == '|') {
647 } else if (!quote && c == '}') {
653 if ((size_t)(p - buf) >= sizeof(buf)) {
654 yyerror("string too long");
657 } while ((c = lgetc(0)) != EOF);
661 yyerror(quote ? "unterminated quote" : "unterminated block");
666 if ((token = lookup(buf)) == STRING)
667 if ((yylval.v.string = strdup(buf)) == NULL)
673 pushfile(const char *name, int secret)
677 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
679 if ((nfile->name = strdup(name)) == NULL)
681 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
682 warn("can't open %s", nfile->name);
687 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
688 nfile->ungetsize = 16;
689 nfile->ungetbuf = malloc(nfile->ungetsize);
690 if (nfile->ungetbuf == NULL)
692 TAILQ_INSERT_TAIL(&files, nfile, entry);
701 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
702 prev->errors += file->errors;
704 TAILQ_REMOVE(&files, file, entry);
705 fclose(file->stream);
707 free(file->ungetbuf);
710 return (file ? 0 : EOF);
714 parse(FILE *outfile, const char *filename)
718 if ((file = pushfile(filename, 0)) == 0)
723 errors = file->errors;
726 return (errors ? -1 : 0);
735 if (yylval.lineno == lastline + 1) {
736 lastline = yylval.lineno;
739 lastline = yylval.lineno;
741 fprintf(fp, "#line %d ", yylval.lineno);
747 printq(const char *str)
750 for (; *str; ++str) {