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>
37 #include "got_compat.h"
40 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
45 TAILQ_ENTRY(file) entry;
50 unsigned char *ungetbuf;
55 int parse(FILE *, const char *);
56 struct file *pushfile(const char *, int);
60 int yyerror(const char *, ...)
61 __attribute__((__format__ (printf, 1, 2)))
62 __attribute__((__nonnull__ (1)));
63 int kw_cmp(const void *, const void *);
71 void printq(const char *);
80 static int lastline = -1;
91 %token DEFINE ELSE END ERROR FINALLY FOR IF INCLUDE PRINTF
92 %token RENDER TQFOREACH UNSAFE URLESCAPE WHILE
93 %token <v.string> STRING
94 %type <v.string> string nstring
95 %type <v.string> stringy
103 | grammar error { file->errors++; }
106 include : INCLUDE STRING {
109 if ((nfile = pushfile($2, 0)) == NULL) {
110 yyerror("failed to include file %s", $2);
121 verbatim : '!' verbatim1 '!' {
123 /* TODO: check template status and exit in case */
128 verbatim1 : /* empty */
132 fprintf(fp, "%s\n", $2);
138 verbatims : /* empty */
144 fprintf(fp, "if ((tp_ret = tp->tp_puts(tp, ");
146 fputs(")) == -1) goto err;\n", fp);
152 block : define body end {
154 fputs("return tp_ret;\n", fp);
158 | define body finally end {
159 fputs("return tp_ret;\n", fp);
165 define : '{' DEFINE string '}' {
169 fprintf(fp, "int\n%s\n{\n", $3);
170 fputs("int tp_ret = 0;\n", fp);
182 special : '{' RENDER string '}' {
184 fprintf(fp, "if ((tp_ret = %s) == -1) goto err;\n",
189 | if body endif { fputs("}\n", fp); }
191 | '{' string '|' UNSAFE '}' {
194 "if ((tp_ret = tp->tp_puts(tp, %s)) == -1)\n",
196 fputs("goto err;\n", fp);
199 | '{' string '|' URLESCAPE '}' {
202 "if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
204 fputs("goto err;\n", fp);
210 "if ((tp_ret = tp->tp_escape(tp, %s)) == -1)\n",
212 fputs("goto err;\n", fp);
217 printf : '{' PRINTF {
219 fprintf(fp, "if (asprintf(&tp->tp_tmp, ");
221 fputs(") == -1)\n", fp);
222 fputs("goto err;\n", fp);
223 fputs("if ((tp_ret = tp->tp_escape(tp, tp->tp_tmp)) "
225 fputs("goto err;\n", fp);
226 fputs("free(tp->tp_tmp);\n", fp);
227 fputs("tp->tp_tmp = NULL;\n", fp);
231 printfargs : /* empty */
232 | printfargs STRING {
233 fprintf(fp, " %s", $2);
238 if : '{' IF stringy '}' {
240 fprintf(fp, "if (%s) {\n", $3);
250 elsif : '{' ELSE IF stringy '}' {
252 fprintf(fp, "} else if (%s) {\n", $4);
257 else : '{' ELSE '}' {
259 fputs("} else {\n", fp);
263 loop : '{' FOR stringy '}' {
264 fprintf(fp, "for (%s) {\n", $3);
269 | '{' TQFOREACH STRING STRING STRING '}' {
270 fprintf(fp, "TAILQ_FOREACH(%s, %s, %s) {\n",
278 | '{' WHILE stringy '}' {
279 fprintf(fp, "while (%s) {\n", $3);
289 finally : '{' FINALLY '}' {
295 nstring : STRING nstring {
296 if (asprintf(&$$, "%s%s", $1, $2) == -1)
304 string : STRING string {
305 if (asprintf(&$$, "%s %s", $1, $2) == -1)
315 if (asprintf(&$$, "%s %s", $1, $2) == -1)
321 if (asprintf(&$$, "|%s", $2) == -1)
335 yyerror(const char *fmt, ...)
342 if (vasprintf(&msg, fmt, ap) == -1)
343 err(1, "yyerror vasprintf");
345 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
351 kw_cmp(const void *k, const void *e)
353 return (strcmp(k, ((const struct keywords *)e)->k_name));
359 /* this has to be sorted always */
360 static const struct keywords keywords[] = {
361 { "define", DEFINE },
364 { "finally", FINALLY },
367 { "include", INCLUDE },
368 { "printf", PRINTF },
369 { "render", RENDER },
370 { "tailq-foreach", TQFOREACH },
371 { "unsafe", UNSAFE },
372 { "urlescape", URLESCAPE },
375 const struct keywords *p;
377 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
386 #define START_EXPAND 1
387 #define DONE_EXPAND 2
389 static int expanding;
397 if (file->ungetpos > 0)
398 c = file->ungetbuf[--file->ungetpos];
400 c = getc(file->stream);
402 if (c == START_EXPAND)
404 else if (c == DONE_EXPAND)
418 if ((c = igetc()) == EOF) {
419 yyerror("reached end of filewhile parsing "
421 if (file == topfile || popfile() == EOF)
429 if (c == '\t' || c == ' ') {
430 /* Compress blanks to a sigle space. */
432 c = getc(file->stream);
433 } while (c == '\t' || c == ' ');
434 ungetc(c, file->stream);
440 * Fake EOL when hit EOF for the first time. This gets line
441 * count right if last line in included file is syntactically
442 * invalid and has no newline.
444 if (file->eof_reached == 0) {
445 file->eof_reached = 1;
449 if (file == topfile || popfile() == EOF)
463 if (file->ungetpos >= file->ungetsize) {
464 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
466 err(1, "reallocarray");
468 file->ungetsize *= 2;
470 file->ungetbuf[file->ungetpos++] = c;
478 /* skip to either EOF or the first real EOL */
502 if (!in_define && block == 0) {
503 while ((c = lgetc(0)) != '{' && c != EOF) {
513 if (c == '{' || c == '!') {
524 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
530 yyerror("unterminated block");
534 yylval.lineno = file->lineno;
536 if (block != 0 && c == block) {
537 if ((c = lgetc(0)) == '}') {
549 if (in_define && block == 0) {
555 if (c == '!' || c == '{') {
563 } else if (c == '{') {
566 } else if (c == '\n')
570 if ((size_t)(p - buf) >= sizeof(buf)) {
571 yyerror("string too long");
574 } while ((c = lgetc(0)) != EOF);
577 yyerror("unterminated block");
582 if ((yylval.v.string = strdup(buf)) == NULL)
598 } else if (c == '!') {
601 } else if (c == '\n')
605 if ((size_t)(p - buf) >= sizeof(buf)) {
606 yyerror("line too long");
609 } while ((c = lgetc(0)) != EOF);
613 yyerror("unterminated block");
619 if ((yylval.v.string = strdup(buf)) == NULL)
628 if (!quote && isspace((unsigned char)c))
634 if (!quote && c == '|') {
648 } else if (!quote && c == '}') {
654 if ((size_t)(p - buf) >= sizeof(buf)) {
655 yyerror("string too long");
658 } while ((c = lgetc(0)) != EOF);
662 yyerror(quote ? "unterminated quote" : "unterminated block");
667 if ((token = lookup(buf)) == STRING)
668 if ((yylval.v.string = strdup(buf)) == NULL)
674 pushfile(const char *name, int secret)
678 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
680 if ((nfile->name = strdup(name)) == NULL)
682 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
683 warn("can't open %s", nfile->name);
688 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
689 nfile->ungetsize = 16;
690 nfile->ungetbuf = malloc(nfile->ungetsize);
691 if (nfile->ungetbuf == NULL)
693 TAILQ_INSERT_TAIL(&files, nfile, entry);
702 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
703 prev->errors += file->errors;
705 TAILQ_REMOVE(&files, file, entry);
706 fclose(file->stream);
708 free(file->ungetbuf);
711 return (file ? 0 : EOF);
715 parse(FILE *outfile, const char *filename)
719 if ((file = pushfile(filename, 0)) == 0)
724 errors = file->errors;
727 return (errors ? -1 : 0);
736 if (yylval.lineno == lastline + 1) {
737 lastline = yylval.lineno;
740 lastline = yylval.lineno;
742 fprintf(fp, "#line %d ", yylval.lineno);
748 printq(const char *str)
751 for (; *str; ++str) {