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
93 %token <v.string> STRING
94 %type <v.string> string
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 : STRING {
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);
280 end : '{' END '}'
283 finally : '{' FINALLY '}' {
284 dbg();
285 fputs("err:\n", fp);
286 } verbatims
289 string : STRING string {
290 if (asprintf(&$$, "%s %s", $1, $2) == -1)
291 err(1, "asprintf");
292 free($1);
293 free($2);
295 | STRING
298 stringy : STRING
299 | STRING stringy {
300 if (asprintf(&$$, "%s %s", $1, $2) == -1)
301 err(1, "asprintf");
302 free($1);
303 free($2);
305 | '|' stringy {
306 if (asprintf(&$$, "|%s", $2) == -1)
307 err(1, "asprintf");
308 free($2);
312 %%
314 struct keywords {
315 const char *k_name;
316 int k_val;
317 };
319 int
320 yyerror(const char *fmt, ...)
322 va_list ap;
323 char *msg;
325 file->errors++;
326 va_start(ap, fmt);
327 if (vasprintf(&msg, fmt, ap) == -1)
328 err(1, "yyerror vasprintf");
329 va_end(ap);
330 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
331 free(msg);
332 return (0);
335 int
336 kw_cmp(const void *k, const void *e)
338 return (strcmp(k, ((const struct keywords *)e)->k_name));
341 int
342 lookup(char *s)
344 /* this has to be sorted always */
345 static const struct keywords keywords[] = {
346 { "define", DEFINE },
347 { "else", ELSE },
348 { "end", END },
349 { "finally", FINALLY },
350 { "for", FOR },
351 { "if", IF },
352 { "include", INCLUDE },
353 { "printf", PRINTF },
354 { "render", RENDER },
355 { "tailq-foreach", TQFOREACH },
356 { "unsafe", UNSAFE },
357 { "urlescape", URLESCAPE },
358 };
359 const struct keywords *p;
361 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
362 kw_cmp);
364 if (p)
365 return (p->k_val);
366 else
367 return (STRING);
370 #define START_EXPAND 1
371 #define DONE_EXPAND 2
373 static int expanding;
375 int
376 igetc(void)
378 int c;
380 while (1) {
381 if (file->ungetpos > 0)
382 c = file->ungetbuf[--file->ungetpos];
383 else
384 c = getc(file->stream);
386 if (c == START_EXPAND)
387 expanding = 1;
388 else if (c == DONE_EXPAND)
389 expanding = 0;
390 else
391 break;
393 return (c);
396 int
397 lgetc(int quotec)
399 int c;
401 if (quotec) {
402 if ((c = igetc()) == EOF) {
403 yyerror("reached end of filewhile parsing "
404 "quoted string");
405 if (file == topfile || popfile() == EOF)
406 return (EOF);
407 return (quotec);
409 return (c);
412 c = igetc();
413 if (c == '\t' || c == ' ') {
414 /* Compress blanks to a sigle space. */
415 do {
416 c = getc(file->stream);
417 } while (c == '\t' || c == ' ');
418 ungetc(c, file->stream);
419 c = ' ';
422 if (c == EOF) {
423 /*
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.
427 */
428 if (file->eof_reached == 0) {
429 file->eof_reached = 1;
430 return ('\n');
432 while (c == EOF) {
433 if (file == topfile || popfile() == EOF)
434 return (EOF);
435 c = igetc();
438 return (c);
441 void
442 lungetc(int c)
444 if (c == EOF)
445 return;
447 if (file->ungetpos >= file->ungetsize) {
448 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
449 if (p == NULL)
450 err(1, "reallocarray");
451 file->ungetbuf = p;
452 file->ungetsize *= 2;
454 file->ungetbuf[file->ungetpos++] = c;
457 int
458 findeol(void)
460 int c;
462 /* skip to either EOF or the first real EOL */
463 while (1) {
464 c = lgetc(0);
465 if (c == '\n') {
466 file->lineno++;
467 break;
469 if (c == EOF)
470 break;
472 return (ERROR);
475 int
476 yylex(void)
478 char buf[8096];
479 char *p = buf;
480 int c;
481 int token;
482 int starting = 0;
483 int ending = 0;
484 int quote = 0;
486 if (!in_define && block == 0) {
487 while ((c = lgetc(0)) != '{' && c != EOF) {
488 if (c == '\n')
489 file->lineno++;
492 if (c == EOF)
493 return (0);
495 newblock:
496 c = lgetc(0);
497 if (c == '{' || c == '!') {
498 if (c == '{')
499 block = '}';
500 else
501 block = c;
502 return (c);
504 if (c == '\n')
505 file->lineno++;
508 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
509 if (c == '\n')
510 file->lineno++;
513 if (c == EOF) {
514 yyerror("unterminated block");
515 return (0);
518 yylval.lineno = file->lineno;
520 if (block != 0 && c == block) {
521 if ((c = lgetc(0)) == '}') {
522 if (block == '!') {
523 block = 0;
524 return ('!');
526 block = 0;
527 return ('}');
529 lungetc(c);
530 c = block;
533 if (in_define && block == 0) {
534 if (c == '{')
535 goto newblock;
537 do {
538 if (starting) {
539 if (c == '!' || c == '{') {
540 lungetc(c);
541 lungetc('{');
542 break;
544 starting = 0;
545 lungetc(c);
546 c = '{';
547 } else if (c == '{') {
548 starting = 1;
549 continue;
552 *p++ = c;
553 if ((size_t)(p - buf) >= sizeof(buf)) {
554 yyerror("string too long");
555 return (findeol());
557 } while ((c = lgetc(0)) != EOF && c != '\n');
558 *p = '\0';
559 if (c == EOF) {
560 yyerror("unterminated block");
561 return (0);
563 if (c == '\n')
564 file->lineno++;
565 if ((yylval.v.string = strdup(buf)) == NULL)
566 err(1, "strdup");
567 return (STRING);
570 if (block == '!') {
571 do {
572 if (ending) {
573 if (c == '}') {
574 lungetc(c);
575 lungetc(block);
576 break;
578 ending = 0;
579 lungetc(c);
580 c = block;
581 } else if (c == '!') {
582 ending = 1;
583 continue;
586 *p++ = c;
587 if ((size_t)(p - buf) >= sizeof(buf)) {
588 yyerror("line too long");
589 return (findeol());
591 } while ((c = lgetc(0)) != EOF && c != '\n');
592 *p = '\0';
594 if (c == EOF) {
595 yyerror("unterminated block");
596 return (0);
598 if (c == '\n')
599 file->lineno++;
601 if ((yylval.v.string = strdup(buf)) == NULL)
602 err(1, "strdup");
603 return (STRING);
606 if (c == '|')
607 return (c);
609 do {
610 if (!quote && isspace((unsigned char)c))
611 break;
613 if (c == '"')
614 quote = !quote;
616 if (!quote && c == '|') {
617 lungetc(c);
618 break;
621 if (ending) {
622 if (c == '}') {
623 lungetc(c);
624 lungetc('}');
625 break;
627 ending = 0;
628 lungetc(c);
629 c = block;
630 } else if (!quote && c == '}') {
631 ending = 1;
632 continue;
635 *p++ = c;
636 if ((size_t)(p - buf) >= sizeof(buf)) {
637 yyerror("string too long");
638 return (findeol());
640 } while ((c = lgetc(0)) != EOF);
641 *p = '\0';
643 if (c == EOF) {
644 yyerror(quote ? "unterminated quote" : "unterminated block");
645 return (0);
647 if (c == '\n')
648 file->lineno++;
649 if ((token = lookup(buf)) == STRING)
650 if ((yylval.v.string = strdup(buf)) == NULL)
651 err(1, "strdup");
652 return (token);
655 struct file *
656 pushfile(const char *name, int secret)
658 struct file *nfile;
660 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
661 err(1, "calloc");
662 if ((nfile->name = strdup(name)) == NULL)
663 err(1, "strdup");
664 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
665 warn("can't open %s", nfile->name);
666 free(nfile->name);
667 free(nfile);
668 return (NULL);
670 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
671 nfile->ungetsize = 16;
672 nfile->ungetbuf = malloc(nfile->ungetsize);
673 if (nfile->ungetbuf == NULL)
674 err(1, "malloc");
675 TAILQ_INSERT_TAIL(&files, nfile, entry);
676 return (nfile);
679 int
680 popfile(void)
682 struct file *prev;
684 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
685 prev->errors += file->errors;
687 TAILQ_REMOVE(&files, file, entry);
688 fclose(file->stream);
689 free(file->name);
690 free(file->ungetbuf);
691 free(file);
692 file = prev;
693 return (file ? 0 : EOF);
696 int
697 parse(FILE *outfile, const char *filename)
699 fp = outfile;
701 if ((file = pushfile(filename, 0)) == 0)
702 return (-1);
703 topfile = file;
705 yyparse();
706 errors = file->errors;
707 popfile();
709 return (errors ? -1 : 0);
712 void
713 dbg(void)
715 if (nodebug)
716 return;
718 if (yylval.lineno == lastline + 1) {
719 lastline = yylval.lineno;
720 return;
722 lastline = yylval.lineno;
724 fprintf(fp, "#line %d ", yylval.lineno);
725 printq(file->name);
726 putc('\n', fp);
729 void
730 printq(const char *str)
732 putc('"', fp);
733 for (; *str; ++str) {
734 if (*str == '"')
735 putc('\\', fp);
736 putc(*str, fp);
738 putc('"', fp);