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;
48 unsigned char *ungetbuf;
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
91 %type <v.string> stringy
99 | grammar error { file->errors++; }
102 include : INCLUDE STRING {
105 if ((nfile = pushfile($2, 0)) == NULL) {
106 yyerror("failed to include file %s", $2);
117 verbatim : '!' verbatim1 '!' {
119 /* TODO: check template status and exit in case */
124 verbatim1 : /* empty */
134 verbatims : /* empty */
140 printf("if ((tp_ret = tp->tp_puts(tp, ");
142 puts(")) == -1) goto err;");
148 block : define body end {
150 puts("return tp_ret;");
154 | define body finally end {
155 puts("return tp_ret;");
161 define : '{' DEFINE string '}' {
165 printf("int\n%s\n{\n", $3);
166 puts("int tp_ret = 0;");
178 special : '{' RENDER string '}' {
180 printf("if ((tp_ret = %s) == -1) goto err;\n", $3);
184 | if body endif { puts("}"); }
186 | '{' string '|' UNSAFE '}' {
188 printf("if ((tp_ret = tp->tp_puts(tp, %s)) == -1)\n",
193 | '{' string '|' URLESCAPE '}' {
195 printf("if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
202 printf("if ((tp_ret = tp->tp_escape(tp, %s)) == -1)\n",
209 printf : '{' PRINTF {
211 printf("if (asprintf(&tp->tp_tmp, ");
215 puts("if ((tp_ret = tp->tp_escape(tp, tp->tp_tmp)) "
218 puts("free(tp->tp_tmp);");
219 puts("tp->tp_tmp = NULL;");
223 printfargs : /* empty */
224 | printfargs STRING {
230 if : '{' IF stringy '}' {
232 printf("if (%s) {\n", $3);
242 elsif : '{' ELSE IF stringy '}' {
244 printf("} else if (%s) {\n", $4);
249 else : '{' ELSE '}' {
255 loop : '{' TQFOREACH STRING STRING STRING '}' {
256 printf("TAILQ_FOREACH(%s, %s, %s) {\n",
269 finally : '{' FINALLY '}' {
275 string : STRING string {
276 if (asprintf(&$$, "%s %s", $1, $2) == -1)
286 if (asprintf(&$$, "%s %s", $1, $2) == -1)
292 if (asprintf(&$$, "|%s", $2) == -1)
306 yyerror(const char *fmt, ...)
313 if (vasprintf(&msg, fmt, ap) == -1)
314 err(1, "yyerror vasprintf");
316 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
322 kw_cmp(const void *k, const void *e)
324 return (strcmp(k, ((const struct keywords *)e)->k_name));
330 /* this has to be sorted always */
331 static const struct keywords keywords[] = {
332 { "define", DEFINE },
335 { "finally", FINALLY },
337 { "include", INCLUDE },
338 { "printf", PRINTF },
339 { "render", RENDER },
340 { "tailq-foreach", TQFOREACH },
341 { "unsafe", UNSAFE },
342 { "urlescape", URLESCAPE },
344 const struct keywords *p;
346 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
355 #define START_EXPAND 1
356 #define DONE_EXPAND 2
358 static int expanding;
366 if (file->ungetpos > 0)
367 c = file->ungetbuf[--file->ungetpos];
369 c = getc(file->stream);
371 if (c == START_EXPAND)
373 else if (c == DONE_EXPAND)
387 if ((c = igetc()) == EOF) {
388 yyerror("reached end of filewhile parsing "
390 if (file == topfile || popfile() == EOF)
398 if (c == '\t' || c == ' ') {
399 /* Compress blanks to a sigle space. */
401 c = getc(file->stream);
402 } while (c == '\t' || c == ' ');
403 ungetc(c, file->stream);
409 * Fake EOL when hit EOF for the first time. This gets line
410 * count rigchtif last line included file is syntactically
411 * invalid and has no newline.
413 if (file->eof_reached == 0) {
414 file->eof_reached = 1;
418 if (file == topfile || popfile() == EOF)
432 if (file->ungetpos >= file->ungetsize) {
433 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
435 err(1, "reallocarray");
437 file->ungetsize *= 2;
439 file->ungetbuf[file->ungetpos++] = c;
447 /* skip to either EOF or the first real EOL */
470 if (!in_define && block == 0) {
471 while ((c = lgetc(0)) != '{' && c != EOF) {
481 if (c == '{' || c == '!') {
492 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
498 yyerror("unterminated block");
502 yylval.lineno = file->lineno;
504 if (block != 0 && c == block) {
505 if ((c = lgetc(0)) == '}') {
517 if (in_define && block == 0) {
523 if (c == '!' || c == '{') {
531 } else if (c == '{') {
537 if ((size_t)(p - buf) >= sizeof(buf)) {
538 yyerror("string too long");
541 } while ((c = lgetc(0)) != EOF && c != '\n');
544 yyerror("unterminated block");
549 if ((yylval.v.string = strdup(buf)) == NULL)
565 } else if (c == '!') {
571 if ((size_t)(p - buf) >= sizeof(buf)) {
572 yyerror("line too long");
575 } while ((c = lgetc(0)) != EOF && c != '\n');
579 yyerror("unterminated block");
585 if ((yylval.v.string = strdup(buf)) == NULL)
608 } else if (c == '}') {
614 if ((size_t)(p - buf) >= sizeof(buf)) {
615 yyerror("string too long");
618 } while ((c = lgetc(0)) != EOF && !isspace((unsigned char)c));
622 yyerror("unterminated block");
627 if ((token = lookup(buf)) == STRING)
628 if ((yylval.v.string = strdup(buf)) == NULL)
634 pushfile(const char *name, int secret)
638 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
640 if ((nfile->name = strdup(name)) == NULL)
642 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
643 warn("can't open %s", nfile->name);
648 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
649 nfile->ungetsize = 16;
650 nfile->ungetbuf = malloc(nfile->ungetsize);
651 if (nfile->ungetbuf == NULL)
653 TAILQ_INSERT_TAIL(&files, nfile, entry);
662 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
663 prev->errors += file->errors;
665 TAILQ_REMOVE(&files, file, entry);
666 fclose(file->stream);
668 free(file->ungetbuf);
671 return (file ? 0 : EOF);
675 parse(const char *filename)
677 if ((file = pushfile(filename, 0)) == 0)
682 errors = file->errors;
685 return (errors ? -1 : 0);
694 if (yylval.lineno == lastline + 1) {
695 lastline = yylval.lineno;
698 lastline = yylval.lineno;
700 printf("#line %d ", yylval.lineno);
706 printq(const char *str)
709 for (; *str; ++str) {