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
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",
276 | '{' WHILE stringy '}' {
277 fprintf(fp, "while (%s) {\n", $3);
287 finally : '{' FINALLY '}' {
293 string : STRING string {
294 if (asprintf(&$$, "%s %s", $1, $2) == -1)
304 if (asprintf(&$$, "%s %s", $1, $2) == -1)
310 if (asprintf(&$$, "|%s", $2) == -1)
324 yyerror(const char *fmt, ...)
331 if (vasprintf(&msg, fmt, ap) == -1)
332 err(1, "yyerror vasprintf");
334 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
340 kw_cmp(const void *k, const void *e)
342 return (strcmp(k, ((const struct keywords *)e)->k_name));
348 /* this has to be sorted always */
349 static const struct keywords keywords[] = {
350 { "define", DEFINE },
353 { "finally", FINALLY },
356 { "include", INCLUDE },
357 { "printf", PRINTF },
358 { "render", RENDER },
359 { "tailq-foreach", TQFOREACH },
360 { "unsafe", UNSAFE },
361 { "urlescape", URLESCAPE },
364 const struct keywords *p;
366 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
375 #define START_EXPAND 1
376 #define DONE_EXPAND 2
378 static int expanding;
386 if (file->ungetpos > 0)
387 c = file->ungetbuf[--file->ungetpos];
389 c = getc(file->stream);
391 if (c == START_EXPAND)
393 else if (c == DONE_EXPAND)
407 if ((c = igetc()) == EOF) {
408 yyerror("reached end of filewhile parsing "
410 if (file == topfile || popfile() == EOF)
418 if (c == '\t' || c == ' ') {
419 /* Compress blanks to a sigle space. */
421 c = getc(file->stream);
422 } while (c == '\t' || c == ' ');
423 ungetc(c, file->stream);
429 * Fake EOL when hit EOF for the first time. This gets line
430 * count rigchtif last line included file is syntactically
431 * invalid and has no newline.
433 if (file->eof_reached == 0) {
434 file->eof_reached = 1;
438 if (file == topfile || popfile() == EOF)
452 if (file->ungetpos >= file->ungetsize) {
453 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
455 err(1, "reallocarray");
457 file->ungetsize *= 2;
459 file->ungetbuf[file->ungetpos++] = c;
467 /* skip to either EOF or the first real EOL */
491 if (!in_define && block == 0) {
492 while ((c = lgetc(0)) != '{' && c != EOF) {
502 if (c == '{' || c == '!') {
513 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
519 yyerror("unterminated block");
523 yylval.lineno = file->lineno;
525 if (block != 0 && c == block) {
526 if ((c = lgetc(0)) == '}') {
538 if (in_define && block == 0) {
544 if (c == '!' || c == '{') {
552 } else if (c == '{') {
558 if ((size_t)(p - buf) >= sizeof(buf)) {
559 yyerror("string too long");
562 } while ((c = lgetc(0)) != EOF && c != '\n');
565 yyerror("unterminated block");
570 if ((yylval.v.string = strdup(buf)) == NULL)
586 } else if (c == '!') {
592 if ((size_t)(p - buf) >= sizeof(buf)) {
593 yyerror("line too long");
596 } while ((c = lgetc(0)) != EOF && c != '\n');
600 yyerror("unterminated block");
606 if ((yylval.v.string = strdup(buf)) == NULL)
615 if (!quote && isspace((unsigned char)c))
621 if (!quote && c == '|') {
635 } else if (!quote && c == '}') {
641 if ((size_t)(p - buf) >= sizeof(buf)) {
642 yyerror("string too long");
645 } while ((c = lgetc(0)) != EOF);
649 yyerror(quote ? "unterminated quote" : "unterminated block");
654 if ((token = lookup(buf)) == STRING)
655 if ((yylval.v.string = strdup(buf)) == NULL)
661 pushfile(const char *name, int secret)
665 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
667 if ((nfile->name = strdup(name)) == NULL)
669 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
670 warn("can't open %s", nfile->name);
675 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
676 nfile->ungetsize = 16;
677 nfile->ungetbuf = malloc(nfile->ungetsize);
678 if (nfile->ungetbuf == NULL)
680 TAILQ_INSERT_TAIL(&files, nfile, entry);
689 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
690 prev->errors += file->errors;
692 TAILQ_REMOVE(&files, file, entry);
693 fclose(file->stream);
695 free(file->ungetbuf);
698 return (file ? 0 : EOF);
702 parse(FILE *outfile, const char *filename)
706 if ((file = pushfile(filename, 0)) == 0)
711 errors = file->errors;
714 return (errors ? -1 : 0);
723 if (yylval.lineno == lastline + 1) {
724 lastline = yylval.lineno;
727 lastline = yylval.lineno;
729 fprintf(fp, "#line %d ", yylval.lineno);
735 printq(const char *str)
738 for (; *str; ++str) {