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->tp_puts(tp, ");
145 printq($1);
146 fputs(")) == -1) goto err;\n", fp);
148 free($1);
152 block : define body end {
153 fputs("err:\n", fp);
154 fputs("return tp_ret;\n", fp);
155 fputs("}\n", fp);
156 in_define = 0;
158 | define body finally end {
159 fputs("return tp_ret;\n", fp);
160 fputs("}\n", fp);
161 in_define = 0;
165 define : '{' DEFINE string '}' {
166 in_define = 1;
168 dbg();
169 fprintf(fp, "int\n%s\n{\n", $3);
170 fputs("int tp_ret = 0;\n", fp);
172 free($3);
176 body : /* empty */
177 | body verbatim
178 | body raw
179 | body special
182 special : '{' RENDER string '}' {
183 dbg();
184 fprintf(fp, "if ((tp_ret = %s) == -1) goto err;\n",
185 $3);
186 free($3);
188 | printf
189 | if body endif { fputs("}\n", fp); }
190 | loop
191 | '{' string '|' UNSAFE '}' {
192 dbg();
193 fprintf(fp,
194 "if ((tp_ret = tp->tp_puts(tp, %s)) == -1)\n",
195 $2);
196 fputs("goto err;\n", fp);
197 free($2);
199 | '{' string '|' URLESCAPE '}' {
200 dbg();
201 fprintf(fp,
202 "if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
203 $2);
204 fputs("goto err;\n", fp);
205 free($2);
207 | '{' string '}' {
208 dbg();
209 fprintf(fp,
210 "if ((tp_ret = tp->tp_escape(tp, %s)) == -1)\n",
211 $2);
212 fputs("goto err;\n", fp);
213 free($2);
217 printf : '{' PRINTF {
218 dbg();
219 fprintf(fp, "if (asprintf(&tp->tp_tmp, ");
220 } printfargs '}' {
221 fputs(") == -1)\n", fp);
222 fputs("goto err;\n", fp);
223 fputs("if ((tp_ret = tp->tp_escape(tp, tp->tp_tmp)) "
224 "== -1)\n", fp);
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);
234 free($2);
238 if : '{' IF stringy '}' {
239 dbg();
240 fprintf(fp, "if (%s) {\n", $3);
241 free($3);
245 endif : end
246 | else body end
247 | elsif body endif
250 elsif : '{' ELSE IF stringy '}' {
251 dbg();
252 fprintf(fp, "} else if (%s) {\n", $4);
253 free($4);
257 else : '{' ELSE '}' {
258 dbg();
259 fputs("} else {\n", fp);
263 loop : '{' FOR stringy '}' {
264 fprintf(fp, "for (%s) {\n", $3);
265 free($3);
266 } body end {
267 fputs("}\n", fp);
269 | '{' TQFOREACH STRING STRING STRING '}' {
270 fprintf(fp, "TAILQ_FOREACH(%s, %s, %s) {\n",
271 $3, $4, $5);
272 free($3);
273 free($4);
274 free($5);
275 } body end {
276 fputs("}\n", fp);
278 | '{' WHILE stringy '}' {
279 fprintf(fp, "while (%s) {\n", $3);
280 free($3);
281 } body end {
282 fputs("}\n", fp);
286 end : '{' END '}'
289 finally : '{' FINALLY '}' {
290 dbg();
291 fputs("err:\n", fp);
292 } verbatims
295 nstring : STRING nstring {
296 if (asprintf(&$$, "%s%s", $1, $2) == -1)
297 err(1, "asprintf");
298 free($1);
299 free($2);
301 | STRING
304 string : STRING string {
305 if (asprintf(&$$, "%s %s", $1, $2) == -1)
306 err(1, "asprintf");
307 free($1);
308 free($2);
310 | STRING
313 stringy : STRING
314 | STRING stringy {
315 if (asprintf(&$$, "%s %s", $1, $2) == -1)
316 err(1, "asprintf");
317 free($1);
318 free($2);
320 | '|' stringy {
321 if (asprintf(&$$, "|%s", $2) == -1)
322 err(1, "asprintf");
323 free($2);
327 %%
329 struct keywords {
330 const char *k_name;
331 int k_val;
332 };
334 int
335 yyerror(const char *fmt, ...)
337 va_list ap;
338 char *msg;
340 file->errors++;
341 va_start(ap, fmt);
342 if (vasprintf(&msg, fmt, ap) == -1)
343 err(1, "yyerror vasprintf");
344 va_end(ap);
345 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
346 free(msg);
347 return (0);
350 int
351 kw_cmp(const void *k, const void *e)
353 return (strcmp(k, ((const struct keywords *)e)->k_name));
356 int
357 lookup(char *s)
359 /* this has to be sorted always */
360 static const struct keywords keywords[] = {
361 { "define", DEFINE },
362 { "else", ELSE },
363 { "end", END },
364 { "finally", FINALLY },
365 { "for", FOR },
366 { "if", IF },
367 { "include", INCLUDE },
368 { "printf", PRINTF },
369 { "render", RENDER },
370 { "tailq-foreach", TQFOREACH },
371 { "unsafe", UNSAFE },
372 { "urlescape", URLESCAPE },
373 { "while", WHILE },
374 };
375 const struct keywords *p;
377 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
378 kw_cmp);
380 if (p)
381 return (p->k_val);
382 else
383 return (STRING);
386 #define START_EXPAND 1
387 #define DONE_EXPAND 2
389 static int expanding;
391 int
392 igetc(void)
394 int c;
396 while (1) {
397 if (file->ungetpos > 0)
398 c = file->ungetbuf[--file->ungetpos];
399 else
400 c = getc(file->stream);
402 if (c == START_EXPAND)
403 expanding = 1;
404 else if (c == DONE_EXPAND)
405 expanding = 0;
406 else
407 break;
409 return (c);
412 int
413 lgetc(int quotec)
415 int c;
417 if (quotec) {
418 if ((c = igetc()) == EOF) {
419 yyerror("reached end of filewhile parsing "
420 "quoted string");
421 if (file == topfile || popfile() == EOF)
422 return (EOF);
423 return (quotec);
425 return (c);
428 c = igetc();
429 if (c == '\t' || c == ' ') {
430 /* Compress blanks to a sigle space. */
431 do {
432 c = getc(file->stream);
433 } while (c == '\t' || c == ' ');
434 ungetc(c, file->stream);
435 c = ' ';
438 if (c == EOF) {
439 /*
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.
443 */
444 if (file->eof_reached == 0) {
445 file->eof_reached = 1;
446 return ('\n');
448 while (c == EOF) {
449 if (file == topfile || popfile() == EOF)
450 return (EOF);
451 c = igetc();
454 return (c);
457 void
458 lungetc(int c)
460 if (c == EOF)
461 return;
463 if (file->ungetpos >= file->ungetsize) {
464 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
465 if (p == NULL)
466 err(1, "reallocarray");
467 file->ungetbuf = p;
468 file->ungetsize *= 2;
470 file->ungetbuf[file->ungetpos++] = c;
473 int
474 findeol(void)
476 int c;
478 /* skip to either EOF or the first real EOL */
479 while (1) {
480 c = lgetc(0);
481 if (c == '\n') {
482 file->lineno++;
483 break;
485 if (c == EOF)
486 break;
488 return (ERROR);
491 int
492 yylex(void)
494 char buf[8096];
495 char *p = buf;
496 int c;
497 int token;
498 int starting = 0;
499 int ending = 0;
500 int quote = 0;
502 if (!in_define && block == 0) {
503 while ((c = lgetc(0)) != '{' && c != EOF) {
504 if (c == '\n')
505 file->lineno++;
508 if (c == EOF)
509 return (0);
511 newblock:
512 c = lgetc(0);
513 if (c == '{' || c == '!') {
514 if (c == '{')
515 block = '}';
516 else
517 block = c;
518 return (c);
520 if (c == '\n')
521 file->lineno++;
524 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
525 if (c == '\n')
526 file->lineno++;
529 if (c == EOF) {
530 yyerror("unterminated block");
531 return (0);
534 yylval.lineno = file->lineno;
536 if (block != 0 && c == block) {
537 if ((c = lgetc(0)) == '}') {
538 if (block == '!') {
539 block = 0;
540 return ('!');
542 block = 0;
543 return ('}');
545 lungetc(c);
546 c = block;
549 if (in_define && block == 0) {
550 if (c == '{')
551 goto newblock;
553 do {
554 if (starting) {
555 if (c == '!' || c == '{') {
556 lungetc(c);
557 lungetc('{');
558 break;
560 starting = 0;
561 lungetc(c);
562 c = '{';
563 } else if (c == '{') {
564 starting = 1;
565 continue;
566 } else if (c == '\n')
567 break;
569 *p++ = c;
570 if ((size_t)(p - buf) >= sizeof(buf)) {
571 yyerror("string too long");
572 return (findeol());
574 } while ((c = lgetc(0)) != EOF);
575 *p = '\0';
576 if (c == EOF) {
577 yyerror("unterminated block");
578 return (0);
580 if (c == '\n')
581 file->lineno++;
582 if ((yylval.v.string = strdup(buf)) == NULL)
583 err(1, "strdup");
584 return (STRING);
587 if (block == '!') {
588 do {
589 if (ending) {
590 if (c == '}') {
591 lungetc(c);
592 lungetc(block);
593 break;
595 ending = 0;
596 lungetc(c);
597 c = block;
598 } else if (c == '!') {
599 ending = 1;
600 continue;
601 } else if (c == '\n')
602 break;
604 *p++ = c;
605 if ((size_t)(p - buf) >= sizeof(buf)) {
606 yyerror("line too long");
607 return (findeol());
609 } while ((c = lgetc(0)) != EOF);
610 *p = '\0';
612 if (c == EOF) {
613 yyerror("unterminated block");
614 return (0);
616 if (c == '\n')
617 file->lineno++;
619 if ((yylval.v.string = strdup(buf)) == NULL)
620 err(1, "strdup");
621 return (STRING);
624 if (c == '|')
625 return (c);
627 do {
628 if (!quote && isspace((unsigned char)c))
629 break;
631 if (c == '"')
632 quote = !quote;
634 if (!quote && c == '|') {
635 lungetc(c);
636 break;
639 if (ending) {
640 if (c == '}') {
641 lungetc(c);
642 lungetc('}');
643 break;
645 ending = 0;
646 lungetc(c);
647 c = block;
648 } else if (!quote && c == '}') {
649 ending = 1;
650 continue;
653 *p++ = c;
654 if ((size_t)(p - buf) >= sizeof(buf)) {
655 yyerror("string too long");
656 return (findeol());
658 } while ((c = lgetc(0)) != EOF);
659 *p = '\0';
661 if (c == EOF) {
662 yyerror(quote ? "unterminated quote" : "unterminated block");
663 return (0);
665 if (c == '\n')
666 file->lineno++;
667 if ((token = lookup(buf)) == STRING)
668 if ((yylval.v.string = strdup(buf)) == NULL)
669 err(1, "strdup");
670 return (token);
673 struct file *
674 pushfile(const char *name, int secret)
676 struct file *nfile;
678 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
679 err(1, "calloc");
680 if ((nfile->name = strdup(name)) == NULL)
681 err(1, "strdup");
682 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
683 warn("can't open %s", nfile->name);
684 free(nfile->name);
685 free(nfile);
686 return (NULL);
688 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
689 nfile->ungetsize = 16;
690 nfile->ungetbuf = malloc(nfile->ungetsize);
691 if (nfile->ungetbuf == NULL)
692 err(1, "malloc");
693 TAILQ_INSERT_TAIL(&files, nfile, entry);
694 return (nfile);
697 int
698 popfile(void)
700 struct file *prev;
702 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
703 prev->errors += file->errors;
705 TAILQ_REMOVE(&files, file, entry);
706 fclose(file->stream);
707 free(file->name);
708 free(file->ungetbuf);
709 free(file);
710 file = prev;
711 return (file ? 0 : EOF);
714 int
715 parse(FILE *outfile, const char *filename)
717 fp = outfile;
719 if ((file = pushfile(filename, 0)) == 0)
720 return (-1);
721 topfile = file;
723 yyparse();
724 errors = file->errors;
725 popfile();
727 return (errors ? -1 : 0);
730 void
731 dbg(void)
733 if (nodebug)
734 return;
736 if (yylval.lineno == lastline + 1) {
737 lastline = yylval.lineno;
738 return;
740 lastline = yylval.lineno;
742 fprintf(fp, "#line %d ", yylval.lineno);
743 printq(file->name);
744 putc('\n', fp);
747 void
748 printq(const char *str)
750 putc('"', fp);
751 for (; *str; ++str) {
752 if (*str == '"')
753 putc('\\', fp);
754 putc(*str, fp);
756 putc('"', fp);