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
91 %token <v.string> STRING
92 %type <v.string> string
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->tp_puts(tp, ");
144 fputs(")) == -1) goto err;\n", fp);
150 block : define body end {
152 fputs("return tp_ret;\n", fp);
156 | define body finally end {
157 fputs("return tp_ret;\n", fp);
163 define : '{' DEFINE string '}' {
167 fprintf(fp, "int\n%s\n{\n", $3);
168 fputs("int tp_ret = 0;\n", fp);
180 special : '{' RENDER string '}' {
182 fprintf(fp, "if ((tp_ret = %s) == -1) goto err;\n",
187 | if body endif { fputs("}\n", fp); }
189 | '{' string '|' UNSAFE '}' {
192 "if ((tp_ret = tp->tp_puts(tp, %s)) == -1)\n",
194 fputs("goto err;\n", fp);
197 | '{' string '|' URLESCAPE '}' {
200 "if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
202 fputs("goto err;\n", fp);
208 "if ((tp_ret = tp->tp_escape(tp, %s)) == -1)\n",
210 fputs("goto err;\n", fp);
215 printf : '{' PRINTF {
217 fprintf(fp, "if (asprintf(&tp->tp_tmp, ");
219 fputs(") == -1)\n", fp);
220 fputs("goto err;\n", fp);
221 fputs("if ((tp_ret = tp->tp_escape(tp, tp->tp_tmp)) "
223 fputs("goto err;\n", fp);
224 fputs("free(tp->tp_tmp);\n", fp);
225 fputs("tp->tp_tmp = NULL;\n", fp);
229 printfargs : /* empty */
230 | printfargs STRING {
231 fprintf(fp, " %s", $2);
236 if : '{' IF stringy '}' {
238 fprintf(fp, "if (%s) {\n", $3);
248 elsif : '{' ELSE IF stringy '}' {
250 fprintf(fp, "} else if (%s) {\n", $4);
255 else : '{' ELSE '}' {
257 fputs("} else {\n", fp);
261 loop : '{' FOR stringy '}' {
262 fprintf(fp, "for (%s) {\n", $3);
267 | '{' TQFOREACH STRING STRING STRING '}' {
268 fprintf(fp, "TAILQ_FOREACH(%s, %s, %s) {\n",
281 finally : '{' FINALLY '}' {
287 string : STRING string {
288 if (asprintf(&$$, "%s %s", $1, $2) == -1)
298 if (asprintf(&$$, "%s %s", $1, $2) == -1)
304 if (asprintf(&$$, "|%s", $2) == -1)
318 yyerror(const char *fmt, ...)
325 if (vasprintf(&msg, fmt, ap) == -1)
326 err(1, "yyerror vasprintf");
328 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
334 kw_cmp(const void *k, const void *e)
336 return (strcmp(k, ((const struct keywords *)e)->k_name));
342 /* this has to be sorted always */
343 static const struct keywords keywords[] = {
344 { "define", DEFINE },
347 { "finally", FINALLY },
350 { "include", INCLUDE },
351 { "printf", PRINTF },
352 { "render", RENDER },
353 { "tailq-foreach", TQFOREACH },
354 { "unsafe", UNSAFE },
355 { "urlescape", URLESCAPE },
357 const struct keywords *p;
359 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
368 #define START_EXPAND 1
369 #define DONE_EXPAND 2
371 static int expanding;
379 if (file->ungetpos > 0)
380 c = file->ungetbuf[--file->ungetpos];
382 c = getc(file->stream);
384 if (c == START_EXPAND)
386 else if (c == DONE_EXPAND)
400 if ((c = igetc()) == EOF) {
401 yyerror("reached end of filewhile parsing "
403 if (file == topfile || popfile() == EOF)
411 if (c == '\t' || c == ' ') {
412 /* Compress blanks to a sigle space. */
414 c = getc(file->stream);
415 } while (c == '\t' || c == ' ');
416 ungetc(c, file->stream);
422 * Fake EOL when hit EOF for the first time. This gets line
423 * count rigchtif last line included file is syntactically
424 * invalid and has no newline.
426 if (file->eof_reached == 0) {
427 file->eof_reached = 1;
431 if (file == topfile || popfile() == EOF)
445 if (file->ungetpos >= file->ungetsize) {
446 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
448 err(1, "reallocarray");
450 file->ungetsize *= 2;
452 file->ungetbuf[file->ungetpos++] = c;
460 /* skip to either EOF or the first real EOL */
484 if (!in_define && block == 0) {
485 while ((c = lgetc(0)) != '{' && c != EOF) {
495 if (c == '{' || c == '!') {
506 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
512 yyerror("unterminated block");
516 yylval.lineno = file->lineno;
518 if (block != 0 && c == block) {
519 if ((c = lgetc(0)) == '}') {
531 if (in_define && block == 0) {
537 if (c == '!' || c == '{') {
545 } else if (c == '{') {
551 if ((size_t)(p - buf) >= sizeof(buf)) {
552 yyerror("string too long");
555 } while ((c = lgetc(0)) != EOF && c != '\n');
558 yyerror("unterminated block");
563 if ((yylval.v.string = strdup(buf)) == NULL)
579 } else if (c == '!') {
585 if ((size_t)(p - buf) >= sizeof(buf)) {
586 yyerror("line too long");
589 } while ((c = lgetc(0)) != EOF && c != '\n');
593 yyerror("unterminated block");
599 if ((yylval.v.string = strdup(buf)) == NULL)
608 if (!quote && isspace((unsigned char)c))
614 if (!quote && c == '|') {
628 } else if (!quote && c == '}') {
634 if ((size_t)(p - buf) >= sizeof(buf)) {
635 yyerror("string too long");
638 } while ((c = lgetc(0)) != EOF);
642 yyerror(quote ? "unterminated quote" : "unterminated block");
647 if ((token = lookup(buf)) == STRING)
648 if ((yylval.v.string = strdup(buf)) == NULL)
654 pushfile(const char *name, int secret)
658 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
660 if ((nfile->name = strdup(name)) == NULL)
662 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
663 warn("can't open %s", nfile->name);
668 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
669 nfile->ungetsize = 16;
670 nfile->ungetbuf = malloc(nfile->ungetsize);
671 if (nfile->ungetbuf == NULL)
673 TAILQ_INSERT_TAIL(&files, nfile, entry);
682 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
683 prev->errors += file->errors;
685 TAILQ_REMOVE(&files, file, entry);
686 fclose(file->stream);
688 free(file->ungetbuf);
691 return (file ? 0 : EOF);
695 parse(FILE *outfile, const char *filename)
699 if ((file = pushfile(filename, 0)) == 0)
704 errors = file->errors;
707 return (errors ? -1 : 0);
716 if (yylval.lineno == lastline + 1) {
717 lastline = yylval.lineno;
720 lastline = yylval.lineno;
722 fprintf(fp, "#line %d ", yylval.lineno);
728 printq(const char *str)
731 for (; *str; ++str) {