Blob


1 /*
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.
10 *
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.
14 *
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.
22 */
24 %{
26 #include <sys/queue.h>
28 #include <ctype.h>
29 #include <err.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <unistd.h>
37 #include "got_compat.h"
39 #ifndef nitems
40 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 #endif
43 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
44 static struct file {
45 TAILQ_ENTRY(file) entry;
46 FILE *stream;
47 char *name;
48 size_t ungetpos;
49 size_t ungetsize;
50 unsigned char *ungetbuf;
51 int eof_reached;
52 int lineno;
53 int errors;
54 } *file, *topfile;
55 int parse(FILE *, const char *);
56 struct file *pushfile(const char *, int);
57 int popfile(void);
58 int yyparse(void);
59 int yylex(void);
60 int yyerror(const char *, ...)
61 __attribute__((__format__ (printf, 1, 2)))
62 __attribute__((__nonnull__ (1)));
63 int kw_cmp(const void *, const void *);
64 int lookup(char *);
65 int igetc(void);
66 int lgetc(int);
67 void lungetc(int);
68 int findeol(void);
70 void dbg(void);
71 void printq(const char *);
73 extern int nodebug;
75 static FILE *fp;
77 static int block;
78 static int in_define;
79 static int errors;
80 static int lastline = -1;
82 typedef struct {
83 union {
84 char *string;
85 } v;
86 int lineno;
87 } YYSTYPE;
89 %}
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
97 %%
99 grammar : /* empty */
100 | grammar include
101 | grammar verbatim
102 | grammar block
103 | grammar error { file->errors++; }
106 include : INCLUDE STRING {
107 struct file *nfile;
109 if ((nfile = pushfile($2, 0)) == NULL) {
110 yyerror("failed to include file %s", $2);
111 free($2);
112 YYERROR;
114 free($2);
116 file = nfile;
117 lungetc('\n');
121 verbatim : '!' verbatim1 '!' {
122 if (in_define) {
123 /* TODO: check template status and exit in case */
128 verbatim1 : /* empty */
129 | verbatim1 STRING {
130 if (*$2 != '\0') {
131 dbg();
132 fprintf(fp, "%s\n", $2);
134 free($2);
138 verbatims : /* empty */
139 | verbatims verbatim
142 raw : nstring {
143 dbg();
144 fprintf(fp, "if ((tp_ret = tp_write(tp, ");
145 printq($1);
146 fprintf(fp, ", %zu)) == -1) goto err;\n",
147 strlen($1));
149 free($1);
153 block : define body end {
154 fputs("err:\n", fp);
155 fputs("return tp_ret;\n", fp);
156 fputs("}\n", fp);
157 in_define = 0;
159 | define body finally end {
160 fputs("return tp_ret;\n", fp);
161 fputs("}\n", fp);
162 in_define = 0;
166 define : '{' DEFINE string '}' {
167 in_define = 1;
169 dbg();
170 fprintf(fp, "int\n%s\n{\n", $3);
171 fputs("int tp_ret = 0;\n", fp);
173 free($3);
177 body : /* empty */
178 | body verbatim
179 | body raw
180 | body special
183 special : '{' RENDER string '}' {
184 dbg();
185 fprintf(fp, "if ((tp_ret = %s) == -1) goto err;\n",
186 $3);
187 free($3);
189 | printf
190 | if body endif { fputs("}\n", fp); }
191 | loop
192 | '{' string '|' UNSAFE '}' {
193 dbg();
194 fprintf(fp,
195 "if ((tp_ret = tp_writes(tp, %s)) == -1)\n",
196 $2);
197 fputs("goto err;\n", fp);
198 free($2);
200 | '{' string '|' URLESCAPE '}' {
201 dbg();
202 fprintf(fp,
203 "if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
204 $2);
205 fputs("goto err;\n", fp);
206 free($2);
208 | '{' string '}' {
209 dbg();
210 fprintf(fp,
211 "if ((tp_ret = tp_htmlescape(tp, %s)) == -1)\n",
212 $2);
213 fputs("goto err;\n", fp);
214 free($2);
218 printf : '{' PRINTF {
219 dbg();
220 fprintf(fp, "if (asprintf(&tp->tp_tmp, ");
221 } printfargs '}' {
222 fputs(") == -1)\n", fp);
223 fputs("goto err;\n", fp);
224 fputs("if ((tp_ret = tp_htmlescape(tp, tp->tp_tmp)) "
225 "== -1)\n", fp);
226 fputs("goto err;\n", fp);
227 fputs("free(tp->tp_tmp);\n", fp);
228 fputs("tp->tp_tmp = NULL;\n", fp);
232 printfargs : /* empty */
233 | printfargs STRING {
234 fprintf(fp, " %s", $2);
235 free($2);
239 if : '{' IF stringy '}' {
240 dbg();
241 fprintf(fp, "if (%s) {\n", $3);
242 free($3);
246 endif : end
247 | else body end
248 | elsif body endif
251 elsif : '{' ELSE IF stringy '}' {
252 dbg();
253 fprintf(fp, "} else if (%s) {\n", $4);
254 free($4);
258 else : '{' ELSE '}' {
259 dbg();
260 fputs("} else {\n", fp);
264 loop : '{' FOR stringy '}' {
265 fprintf(fp, "for (%s) {\n", $3);
266 free($3);
267 } body end {
268 fputs("}\n", fp);
270 | '{' TQFOREACH STRING STRING STRING '}' {
271 fprintf(fp, "TAILQ_FOREACH(%s, %s, %s) {\n",
272 $3, $4, $5);
273 free($3);
274 free($4);
275 free($5);
276 } body end {
277 fputs("}\n", fp);
279 | '{' WHILE stringy '}' {
280 fprintf(fp, "while (%s) {\n", $3);
281 free($3);
282 } body end {
283 fputs("}\n", fp);
287 end : '{' END '}'
290 finally : '{' FINALLY '}' {
291 dbg();
292 fputs("err:\n", fp);
293 } verbatims
296 nstring : STRING nstring {
297 if (asprintf(&$$, "%s%s", $1, $2) == -1)
298 err(1, "asprintf");
299 free($1);
300 free($2);
302 | STRING
305 string : STRING string {
306 if (asprintf(&$$, "%s %s", $1, $2) == -1)
307 err(1, "asprintf");
308 free($1);
309 free($2);
311 | STRING
314 stringy : STRING
315 | STRING stringy {
316 if (asprintf(&$$, "%s %s", $1, $2) == -1)
317 err(1, "asprintf");
318 free($1);
319 free($2);
321 | '|' stringy {
322 if (asprintf(&$$, "|%s", $2) == -1)
323 err(1, "asprintf");
324 free($2);
328 %%
330 struct keywords {
331 const char *k_name;
332 int k_val;
333 };
335 int
336 yyerror(const char *fmt, ...)
338 va_list ap;
339 char *msg;
341 file->errors++;
342 va_start(ap, fmt);
343 if (vasprintf(&msg, fmt, ap) == -1)
344 err(1, "yyerror vasprintf");
345 va_end(ap);
346 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
347 free(msg);
348 return (0);
351 int
352 kw_cmp(const void *k, const void *e)
354 return (strcmp(k, ((const struct keywords *)e)->k_name));
357 int
358 lookup(char *s)
360 /* this has to be sorted always */
361 static const struct keywords keywords[] = {
362 { "define", DEFINE },
363 { "else", ELSE },
364 { "end", END },
365 { "finally", FINALLY },
366 { "for", FOR },
367 { "if", IF },
368 { "include", INCLUDE },
369 { "printf", PRINTF },
370 { "render", RENDER },
371 { "tailq-foreach", TQFOREACH },
372 { "unsafe", UNSAFE },
373 { "urlescape", URLESCAPE },
374 { "while", WHILE },
375 };
376 const struct keywords *p;
378 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
379 kw_cmp);
381 if (p)
382 return (p->k_val);
383 else
384 return (STRING);
387 #define START_EXPAND 1
388 #define DONE_EXPAND 2
390 static int expanding;
392 int
393 igetc(void)
395 int c;
397 while (1) {
398 if (file->ungetpos > 0)
399 c = file->ungetbuf[--file->ungetpos];
400 else
401 c = getc(file->stream);
403 if (c == START_EXPAND)
404 expanding = 1;
405 else if (c == DONE_EXPAND)
406 expanding = 0;
407 else
408 break;
410 return (c);
413 int
414 lgetc(int quotec)
416 int c;
418 if (quotec) {
419 if ((c = igetc()) == EOF) {
420 yyerror("reached end of filewhile parsing "
421 "quoted string");
422 if (file == topfile || popfile() == EOF)
423 return (EOF);
424 return (quotec);
426 return (c);
429 c = igetc();
430 if (c == '\t' || c == ' ') {
431 /* Compress blanks to a sigle space. */
432 do {
433 c = getc(file->stream);
434 } while (c == '\t' || c == ' ');
435 ungetc(c, file->stream);
436 c = ' ';
439 if (c == EOF) {
440 /*
441 * Fake EOL when hit EOF for the first time. This gets line
442 * count right if last line in included file is syntactically
443 * invalid and has no newline.
444 */
445 if (file->eof_reached == 0) {
446 file->eof_reached = 1;
447 return ('\n');
449 while (c == EOF) {
450 if (file == topfile || popfile() == EOF)
451 return (EOF);
452 c = igetc();
455 return (c);
458 void
459 lungetc(int c)
461 if (c == EOF)
462 return;
464 if (file->ungetpos >= file->ungetsize) {
465 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
466 if (p == NULL)
467 err(1, "reallocarray");
468 file->ungetbuf = p;
469 file->ungetsize *= 2;
471 file->ungetbuf[file->ungetpos++] = c;
474 int
475 findeol(void)
477 int c;
479 /* skip to either EOF or the first real EOL */
480 while (1) {
481 c = lgetc(0);
482 if (c == '\n') {
483 file->lineno++;
484 break;
486 if (c == EOF)
487 break;
489 return (ERROR);
492 int
493 yylex(void)
495 char buf[8096];
496 char *p = buf;
497 int c;
498 int token;
499 int starting = 0;
500 int ending = 0;
501 int quote = 0;
503 if (!in_define && block == 0) {
504 while ((c = lgetc(0)) != '{' && c != EOF) {
505 if (c == '\n')
506 file->lineno++;
509 if (c == EOF)
510 return (0);
512 newblock:
513 c = lgetc(0);
514 if (c == '{' || c == '!') {
515 if (c == '{')
516 block = '}';
517 else
518 block = c;
519 return (c);
521 if (c == '\n')
522 file->lineno++;
525 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
526 if (c == '\n')
527 file->lineno++;
530 if (c == EOF) {
531 yyerror("unterminated block");
532 return (0);
535 yylval.lineno = file->lineno;
537 if (block != 0 && c == block) {
538 if ((c = lgetc(0)) == '}') {
539 if (block == '!') {
540 block = 0;
541 return ('!');
543 block = 0;
544 return ('}');
546 lungetc(c);
547 c = block;
550 if (in_define && block == 0) {
551 if (c == '{')
552 goto newblock;
554 do {
555 if (starting) {
556 if (c == '!' || c == '{') {
557 lungetc(c);
558 lungetc('{');
559 break;
561 starting = 0;
562 lungetc(c);
563 c = '{';
564 } else if (c == '{') {
565 starting = 1;
566 continue;
567 } else if (c == '\n')
568 break;
570 *p++ = c;
571 if ((size_t)(p - buf) >= sizeof(buf)) {
572 yyerror("string too long");
573 return (findeol());
575 } while ((c = lgetc(0)) != EOF);
576 *p = '\0';
577 if (c == EOF) {
578 yyerror("unterminated block");
579 return (0);
581 if (c == '\n')
582 file->lineno++;
583 if ((yylval.v.string = strdup(buf)) == NULL)
584 err(1, "strdup");
585 return (STRING);
588 if (block == '!') {
589 do {
590 if (ending) {
591 if (c == '}') {
592 lungetc(c);
593 lungetc(block);
594 break;
596 ending = 0;
597 lungetc(c);
598 c = block;
599 } else if (c == '!') {
600 ending = 1;
601 continue;
602 } else if (c == '\n')
603 break;
605 *p++ = c;
606 if ((size_t)(p - buf) >= sizeof(buf)) {
607 yyerror("line too long");
608 return (findeol());
610 } while ((c = lgetc(0)) != EOF);
611 *p = '\0';
613 if (c == EOF) {
614 yyerror("unterminated block");
615 return (0);
617 if (c == '\n')
618 file->lineno++;
620 if ((yylval.v.string = strdup(buf)) == NULL)
621 err(1, "strdup");
622 return (STRING);
625 if (c == '|')
626 return (c);
628 do {
629 if (!quote && isspace((unsigned char)c))
630 break;
632 if (c == '"')
633 quote = !quote;
635 if (!quote && c == '|') {
636 lungetc(c);
637 break;
640 if (ending) {
641 if (c == '}') {
642 lungetc(c);
643 lungetc('}');
644 break;
646 ending = 0;
647 lungetc(c);
648 c = block;
649 } else if (!quote && c == '}') {
650 ending = 1;
651 continue;
654 *p++ = c;
655 if ((size_t)(p - buf) >= sizeof(buf)) {
656 yyerror("string too long");
657 return (findeol());
659 } while ((c = lgetc(0)) != EOF);
660 *p = '\0';
662 if (c == EOF) {
663 yyerror(quote ? "unterminated quote" : "unterminated block");
664 return (0);
666 if (c == '\n')
667 file->lineno++;
668 if ((token = lookup(buf)) == STRING)
669 if ((yylval.v.string = strdup(buf)) == NULL)
670 err(1, "strdup");
671 return (token);
674 struct file *
675 pushfile(const char *name, int secret)
677 struct file *nfile;
679 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
680 err(1, "calloc");
681 if ((nfile->name = strdup(name)) == NULL)
682 err(1, "strdup");
683 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
684 warn("can't open %s", nfile->name);
685 free(nfile->name);
686 free(nfile);
687 return (NULL);
689 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
690 nfile->ungetsize = 16;
691 nfile->ungetbuf = malloc(nfile->ungetsize);
692 if (nfile->ungetbuf == NULL)
693 err(1, "malloc");
694 TAILQ_INSERT_TAIL(&files, nfile, entry);
695 return (nfile);
698 int
699 popfile(void)
701 struct file *prev;
703 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
704 prev->errors += file->errors;
706 TAILQ_REMOVE(&files, file, entry);
707 fclose(file->stream);
708 free(file->name);
709 free(file->ungetbuf);
710 free(file);
711 file = prev;
712 return (file ? 0 : EOF);
715 int
716 parse(FILE *outfile, const char *filename)
718 fp = outfile;
720 if ((file = pushfile(filename, 0)) == 0)
721 return (-1);
722 topfile = file;
724 yyparse();
725 errors = file->errors;
726 popfile();
728 return (errors ? -1 : 0);
731 void
732 dbg(void)
734 if (nodebug)
735 return;
737 if (yylval.lineno == lastline + 1) {
738 lastline = yylval.lineno;
739 return;
741 lastline = yylval.lineno;
743 fprintf(fp, "#line %d ", yylval.lineno);
744 printq(file->name);
745 putc('\n', fp);
748 void
749 printq(const char *str)
751 putc('"', fp);
752 for (; *str; ++str) {
753 if (*str == '"')
754 putc('\\', fp);
755 putc(*str, fp);
757 putc('"', fp);