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 *);
76 static int lastline = -1;
87 %token DEFINE ELSE END ERROR FINALLY IF INCLUDE PRINTF
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_ret = tp->tp_puts(tp, ");
141 printf(")) == -1) goto err;\n");
147 block : define body end {
149 puts("return tp_ret;");
153 | define body finally end {
154 puts("return tp_ret;");
160 define : '{' DEFINE string '}' {
164 printf("int\n%s\n{\n", $3);
165 puts("int tp_ret = 0;");
177 special : '{' RENDER string '}' {
179 printf("if ((tp_ret = %s) == -1) goto err;\n", $3);
183 | if body endif { puts("}"); }
185 | '{' string '|' UNSAFE '}' {
187 printf("if ((tp_ret = tp->tp_puts(tp, %s)) == -1)\n",
192 | '{' string '|' URLESCAPE '}' {
194 printf("if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
201 printf("if ((tp_ret = tp->tp_escape(tp, %s)) == -1)\n",
208 printf : '{' PRINTF {
210 printf("if (asprintf(&tp->tp_tmp, ");
212 printf(") == -1)\n goto err;\n");
213 puts("if ((tp_ret = tp->tp_escape(tp, tp->tp_tmp)) "
216 puts("free(tp->tp_tmp);");
217 puts("tp->tp_tmp = NULL;");
221 printfargs : /* empty */
222 | printfargs STRING {
227 if : '{' IF string '}' {
229 printf("if (%s) {\n", $3);
239 elsif : '{' ELSE IF string '}' {
241 printf("} else if (%s) {\n", $4);
246 else : '{' ELSE '}' {
252 loop : '{' TQFOREACH STRING STRING STRING '}' {
253 printf("TAILQ_FOREACH(%s, %s, %s) {\n",
266 finally : '{' FINALLY '}' {
272 string : STRING string {
273 if (asprintf(&$$, "%s %s", $1, $2) == -1)
289 yyerror(const char *fmt, ...)
296 if (vasprintf(&msg, fmt, ap) == -1)
297 err(1, "yyerror vasprintf");
299 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
305 kw_cmp(const void *k, const void *e)
307 return (strcmp(k, ((const struct keywords *)e)->k_name));
313 /* this has to be sorted always */
314 static const struct keywords keywords[] = {
315 { "define", DEFINE },
318 { "finally", FINALLY },
320 { "include", INCLUDE },
321 { "printf", PRINTF },
322 { "render", RENDER },
323 { "tailq-foreach", TQFOREACH },
324 { "unsafe", UNSAFE },
325 { "urlescape", URLESCAPE },
327 const struct keywords *p;
329 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
338 #define START_EXPAND 1
339 #define DONE_EXPAND 2
341 static int expanding;
349 if (file->ungetpos > 0)
350 c = file->ungetbuf[--file->ungetpos];
352 c = getc(file->stream);
354 if (c == START_EXPAND)
356 else if (c == DONE_EXPAND)
370 if ((c = igetc()) == EOF) {
371 yyerror("reached end of filewhile parsing "
373 if (file == topfile || popfile() == EOF)
381 if (c == '\t' || c == ' ') {
382 /* Compress blanks to a sigle space. */
384 c = getc(file->stream);
385 } while (c == '\t' || c == ' ');
386 ungetc(c, file->stream);
392 * Fake EOL when hit EOF for the first time. This gets line
393 * count rigchtif last line included file is syntactically
394 * invalid and has no newline.
396 if (file->eof_reached == 0) {
397 file->eof_reached = 1;
401 if (file == topfile || popfile() == EOF)
415 if (file->ungetpos >= file->ungetsize) {
416 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
418 err(1, "reallocarray");
420 file->ungetsize *= 2;
422 file->ungetbuf[file->ungetpos++] = c;
430 /* skip to either EOF or the first real EOL */
453 if (!in_define && block == 0) {
454 while ((c = lgetc(0)) != '{' && c != EOF) {
464 if (c == '{' || c == '!') {
475 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
481 yyerror("unterminated block");
485 yylval.lineno = file->lineno;
487 if (block != 0 && c == block) {
488 if ((c = lgetc(0)) == '}') {
500 if (in_define && block == 0) {
506 if (c == '!' || c == '{') {
514 } else if (c == '{') {
520 if ((size_t)(p - buf) >= sizeof(buf)) {
521 yyerror("string too long");
524 } while ((c = lgetc(0)) != EOF && c != '\n');
527 yyerror("unterminated block");
532 if ((yylval.v.string = strdup(buf)) == NULL)
548 } else if (c == '!') {
554 if ((size_t)(p - buf) >= sizeof(buf)) {
555 yyerror("line too long");
558 } while ((c = lgetc(0)) != EOF && c != '\n');
562 yyerror("unterminated block");
568 if ((yylval.v.string = strdup(buf)) == NULL)
591 } else if (c == '}') {
597 if ((size_t)(p - buf) >= sizeof(buf)) {
598 yyerror("string too long");
601 } while ((c = lgetc(0)) != EOF && !isspace((unsigned char)c));
605 yyerror("unterminated block");
610 if ((token = lookup(buf)) == STRING)
611 if ((yylval.v.string = strdup(buf)) == NULL)
617 pushfile(const char *name, int secret)
621 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
623 if ((nfile->name = strdup(name)) == NULL)
625 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
626 warn("can't open %s", nfile->name);
631 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
632 nfile->ungetsize = 16;
633 nfile->ungetbuf = malloc(nfile->ungetsize);
634 if (nfile->ungetbuf == NULL)
636 TAILQ_INSERT_TAIL(&files, nfile, entry);
645 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
646 prev->errors += file->errors;
648 TAILQ_REMOVE(&files, file, entry);
649 fclose(file->stream);
651 free(file->ungetbuf);
654 return (file ? 0 : EOF);
658 parse(const char *filename)
660 if ((file = pushfile(filename, 0)) == 0)
665 errors = file->errors;
668 return (errors ? -1 : 0);
677 if (yylval.lineno == lastline + 1) {
678 lastline = yylval.lineno;
681 lastline = yylval.lineno;
683 printf("#line %d ", yylval.lineno);
689 printq(const char *str)
692 for (; *str; ++str) {