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
93 %token <v.string> STRING
94 %type <v.string> string
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",
283 finally : '{' FINALLY '}' {
289 string : STRING string {
290 if (asprintf(&$$, "%s %s", $1, $2) == -1)
300 if (asprintf(&$$, "%s %s", $1, $2) == -1)
306 if (asprintf(&$$, "|%s", $2) == -1)
320 yyerror(const char *fmt, ...)
327 if (vasprintf(&msg, fmt, ap) == -1)
328 err(1, "yyerror vasprintf");
330 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
336 kw_cmp(const void *k, const void *e)
338 return (strcmp(k, ((const struct keywords *)e)->k_name));
344 /* this has to be sorted always */
345 static const struct keywords keywords[] = {
346 { "define", DEFINE },
349 { "finally", FINALLY },
352 { "include", INCLUDE },
353 { "printf", PRINTF },
354 { "render", RENDER },
355 { "tailq-foreach", TQFOREACH },
356 { "unsafe", UNSAFE },
357 { "urlescape", URLESCAPE },
359 const struct keywords *p;
361 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
370 #define START_EXPAND 1
371 #define DONE_EXPAND 2
373 static int expanding;
381 if (file->ungetpos > 0)
382 c = file->ungetbuf[--file->ungetpos];
384 c = getc(file->stream);
386 if (c == START_EXPAND)
388 else if (c == DONE_EXPAND)
402 if ((c = igetc()) == EOF) {
403 yyerror("reached end of filewhile parsing "
405 if (file == topfile || popfile() == EOF)
413 if (c == '\t' || c == ' ') {
414 /* Compress blanks to a sigle space. */
416 c = getc(file->stream);
417 } while (c == '\t' || c == ' ');
418 ungetc(c, file->stream);
424 * Fake EOL when hit EOF for the first time. This gets line
425 * count rigchtif last line included file is syntactically
426 * invalid and has no newline.
428 if (file->eof_reached == 0) {
429 file->eof_reached = 1;
433 if (file == topfile || popfile() == EOF)
447 if (file->ungetpos >= file->ungetsize) {
448 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
450 err(1, "reallocarray");
452 file->ungetsize *= 2;
454 file->ungetbuf[file->ungetpos++] = c;
462 /* skip to either EOF or the first real EOL */
486 if (!in_define && block == 0) {
487 while ((c = lgetc(0)) != '{' && c != EOF) {
497 if (c == '{' || c == '!') {
508 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
514 yyerror("unterminated block");
518 yylval.lineno = file->lineno;
520 if (block != 0 && c == block) {
521 if ((c = lgetc(0)) == '}') {
533 if (in_define && block == 0) {
539 if (c == '!' || c == '{') {
547 } else if (c == '{') {
553 if ((size_t)(p - buf) >= sizeof(buf)) {
554 yyerror("string too long");
557 } while ((c = lgetc(0)) != EOF && c != '\n');
560 yyerror("unterminated block");
565 if ((yylval.v.string = strdup(buf)) == NULL)
581 } else if (c == '!') {
587 if ((size_t)(p - buf) >= sizeof(buf)) {
588 yyerror("line too long");
591 } while ((c = lgetc(0)) != EOF && c != '\n');
595 yyerror("unterminated block");
601 if ((yylval.v.string = strdup(buf)) == NULL)
610 if (!quote && isspace((unsigned char)c))
616 if (!quote && c == '|') {
630 } else if (!quote && c == '}') {
636 if ((size_t)(p - buf) >= sizeof(buf)) {
637 yyerror("string too long");
640 } while ((c = lgetc(0)) != EOF);
644 yyerror(quote ? "unterminated quote" : "unterminated block");
649 if ((token = lookup(buf)) == STRING)
650 if ((yylval.v.string = strdup(buf)) == NULL)
656 pushfile(const char *name, int secret)
660 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
662 if ((nfile->name = strdup(name)) == NULL)
664 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
665 warn("can't open %s", nfile->name);
670 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
671 nfile->ungetsize = 16;
672 nfile->ungetbuf = malloc(nfile->ungetsize);
673 if (nfile->ungetbuf == NULL)
675 TAILQ_INSERT_TAIL(&files, nfile, entry);
684 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
685 prev->errors += file->errors;
687 TAILQ_REMOVE(&files, file, entry);
688 fclose(file->stream);
690 free(file->ungetbuf);
693 return (file ? 0 : EOF);
697 parse(FILE *outfile, const char *filename)
701 if ((file = pushfile(filename, 0)) == 0)
706 errors = file->errors;
709 return (errors ? -1 : 0);
718 if (yylval.lineno == lastline + 1) {
719 lastline = yylval.lineno;
722 lastline = yylval.lineno;
724 fprintf(fp, "#line %d ", yylval.lineno);
730 printq(const char *str)
733 for (; *str; ++str) {