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 *);
86 %token DEFINE ELSE END ERROR ESCAPE FINALLY IF INCLUDE
87 %token RENDER TQFOREACH UNSAFE URLESCAPE
88 %token <v.string> STRING
89 %type <v.string> string
97 | grammar error { file->errors++; }
100 include : INCLUDE STRING {
103 if ((nfile = pushfile($2, 0)) == NULL) {
104 yyerror("failed to include file %s", $2);
115 verbatim : '!' verbatim1 '!' {
117 /* TODO: check template status and exit in case */
122 verbatim1 : /* empty */
132 verbatims : /* empty */
138 printf("if (tp->tp_puts(tp, ");
140 printf(") == -1) goto err;\n");
146 block : define body end {
148 puts("return tp->tp_ret;");
152 | define body finally end {
153 puts("return tp->tp_ret;");
159 define : '{' DEFINE string '}' {
163 printf("int\n%s\n{\n", $3);
175 special : '{' RENDER string '}' {
177 if (strrchr($3, ')') != NULL)
178 printf("if (%s == -1) goto err;\n", $3);
180 printf("if (%s != NULL && %s(tp) == -1) "
181 "goto err;\n", $3, $3);
184 | if body endif { puts("}"); }
186 | '{' string '|' ESCAPE '}' {
188 printf("if (tp->tp_escape(tp, %s) == -1) goto err;\n",
192 | '{' string '|' UNSAFE '}' {
194 printf("if (tp->tp_puts(tp, %s) == -1) goto err;\n",
198 | '{' string '|' URLESCAPE '}' {
200 printf("if (tp_urlescape(tp, %s) == -1) goto err;\n",
206 printf("if (tp->tp_escape(tp, %s) == -1) goto err;\n",
212 if : '{' IF string '}' {
214 printf("if (%s) {\n", $3);
224 elsif : '{' ELSE IF string '}' {
226 printf("} else if (%s) {\n", $4);
231 else : '{' ELSE '}' {
237 loop : '{' TQFOREACH STRING STRING STRING '}' {
238 printf("TAILQ_FOREACH(%s, %s, %s) {\n",
251 finally : '{' FINALLY '}' {
257 string : STRING string {
258 if (asprintf(&$$, "%s %s", $1, $2) == -1)
274 yyerror(const char *fmt, ...)
281 if (vasprintf(&msg, fmt, ap) == -1)
282 err(1, "yyerror vasprintf");
284 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
290 kw_cmp(const void *k, const void *e)
292 return (strcmp(k, ((const struct keywords *)e)->k_name));
298 /* this has to be sorted always */
299 static const struct keywords keywords[] = {
300 { "define", DEFINE },
303 { "escape", ESCAPE },
304 { "finally", FINALLY },
306 { "include", INCLUDE },
307 { "render", RENDER },
308 { "tailq-foreach", TQFOREACH },
309 { "unsafe", UNSAFE },
310 { "urlescape", URLESCAPE },
312 const struct keywords *p;
314 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
323 #define START_EXPAND 1
324 #define DONE_EXPAND 2
326 static int expanding;
334 if (file->ungetpos > 0)
335 c = file->ungetbuf[--file->ungetpos];
337 c = getc(file->stream);
339 if (c == START_EXPAND)
341 else if (c == DONE_EXPAND)
355 if ((c = igetc()) == EOF) {
356 yyerror("reached end of filewhile parsing "
358 if (file == topfile || popfile() == EOF)
365 while ((c = igetc()) == '\\') {
371 yylval.lineno = file->lineno;
374 if (c == '\t' || c == ' ') {
375 /* Compress blanks to a sigle space. */
377 c = getc(file->stream);
378 } while (c == '\t' || c == ' ');
379 ungetc(c, file->stream);
385 * Fake EOL when hit EOF for the first time. This gets line
386 * count rigchtif last line included file is syntactically
387 * invalid and has no newline.
389 if (file->eof_reached == 0) {
390 file->eof_reached = 1;
394 if (file == topfile || popfile() == EOF)
408 if (file->ungetpos >= file->ungetsize) {
409 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
411 err(1, "reallocarray");
413 file->ungetsize *= 2;
415 file->ungetbuf[file->ungetpos++] = c;
423 /* skip to either EOF or the first real EOL */
446 if (!in_define && block == 0) {
447 while ((c = lgetc(0)) != '{' && c != EOF) {
457 if (c == '{' || c == '!') {
468 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
474 yyerror("unterminated block");
478 yylval.lineno = file->lineno;
480 if (block != 0 && c == block) {
481 if ((c = lgetc(0)) == '}') {
493 if (in_define && block == 0) {
499 if (c == '!' || c == '{') {
507 } else if (c == '{') {
513 if ((size_t)(p - buf) >= sizeof(buf)) {
514 yyerror("string too long");
517 } while ((c = lgetc(0)) != EOF && c != '\n');
520 yyerror("unterminated block");
525 if ((yylval.v.string = strdup(buf)) == NULL)
541 } else if (c == '!') {
547 if ((size_t)(p - buf) >= sizeof(buf)) {
548 yyerror("line too long");
551 } while ((c = lgetc(0)) != EOF && c != '\n');
555 yyerror("unterminated block");
561 if ((yylval.v.string = strdup(buf)) == NULL)
584 } else if (c == '}') {
590 if ((size_t)(p - buf) >= sizeof(buf)) {
591 yyerror("string too long");
594 } while ((c = lgetc(0)) != EOF && !isspace((unsigned char)c));
598 yyerror("unterminated block");
603 if ((token = lookup(buf)) == STRING)
604 if ((yylval.v.string = strdup(buf)) == NULL)
610 pushfile(const char *name, int secret)
614 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
616 if ((nfile->name = strdup(name)) == NULL)
618 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
619 warn("can't open %s", nfile->name);
624 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
625 nfile->ungetsize = 16;
626 nfile->ungetbuf = malloc(nfile->ungetsize);
627 if (nfile->ungetbuf == NULL)
629 TAILQ_INSERT_TAIL(&files, nfile, entry);
638 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
639 prev->errors += file->errors;
641 TAILQ_REMOVE(&files, file, entry);
642 fclose(file->stream);
644 free(file->ungetbuf);
647 return (file ? 0 : EOF);
651 parse(const char *filename)
653 if ((file = pushfile(filename, 0)) == 0)
658 errors = file->errors;
661 return (errors ? -1 : 0);
670 printf("#line %d ", yylval.lineno);
676 printq(const char *str)
679 for (; *str; ++str) {