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
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);
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 string : STRING string {
296 if (asprintf(&$$, "%s %s", $1, $2) == -1)
297 err(1, "asprintf");
298 free($1);
299 free($2);
301 | STRING
304 stringy : STRING
305 | STRING stringy {
306 if (asprintf(&$$, "%s %s", $1, $2) == -1)
307 err(1, "asprintf");
308 free($1);
309 free($2);
311 | '|' stringy {
312 if (asprintf(&$$, "|%s", $2) == -1)
313 err(1, "asprintf");
314 free($2);
318 %%
320 struct keywords {
321 const char *k_name;
322 int k_val;
323 };
325 int
326 yyerror(const char *fmt, ...)
328 va_list ap;
329 char *msg;
331 file->errors++;
332 va_start(ap, fmt);
333 if (vasprintf(&msg, fmt, ap) == -1)
334 err(1, "yyerror vasprintf");
335 va_end(ap);
336 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
337 free(msg);
338 return (0);
341 int
342 kw_cmp(const void *k, const void *e)
344 return (strcmp(k, ((const struct keywords *)e)->k_name));
347 int
348 lookup(char *s)
350 /* this has to be sorted always */
351 static const struct keywords keywords[] = {
352 { "define", DEFINE },
353 { "else", ELSE },
354 { "end", END },
355 { "finally", FINALLY },
356 { "for", FOR },
357 { "if", IF },
358 { "include", INCLUDE },
359 { "printf", PRINTF },
360 { "render", RENDER },
361 { "tailq-foreach", TQFOREACH },
362 { "unsafe", UNSAFE },
363 { "urlescape", URLESCAPE },
364 { "while", WHILE },
365 };
366 const struct keywords *p;
368 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
369 kw_cmp);
371 if (p)
372 return (p->k_val);
373 else
374 return (STRING);
377 #define START_EXPAND 1
378 #define DONE_EXPAND 2
380 static int expanding;
382 int
383 igetc(void)
385 int c;
387 while (1) {
388 if (file->ungetpos > 0)
389 c = file->ungetbuf[--file->ungetpos];
390 else
391 c = getc(file->stream);
393 if (c == START_EXPAND)
394 expanding = 1;
395 else if (c == DONE_EXPAND)
396 expanding = 0;
397 else
398 break;
400 return (c);
403 int
404 lgetc(int quotec)
406 int c;
408 if (quotec) {
409 if ((c = igetc()) == EOF) {
410 yyerror("reached end of filewhile parsing "
411 "quoted string");
412 if (file == topfile || popfile() == EOF)
413 return (EOF);
414 return (quotec);
416 return (c);
419 c = igetc();
420 if (c == '\t' || c == ' ') {
421 /* Compress blanks to a sigle space. */
422 do {
423 c = getc(file->stream);
424 } while (c == '\t' || c == ' ');
425 ungetc(c, file->stream);
426 c = ' ';
429 if (c == EOF) {
430 /*
431 * Fake EOL when hit EOF for the first time. This gets line
432 * count rigchtif last line included file is syntactically
433 * invalid and has no newline.
434 */
435 if (file->eof_reached == 0) {
436 file->eof_reached = 1;
437 return ('\n');
439 while (c == EOF) {
440 if (file == topfile || popfile() == EOF)
441 return (EOF);
442 c = igetc();
445 return (c);
448 void
449 lungetc(int c)
451 if (c == EOF)
452 return;
454 if (file->ungetpos >= file->ungetsize) {
455 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
456 if (p == NULL)
457 err(1, "reallocarray");
458 file->ungetbuf = p;
459 file->ungetsize *= 2;
461 file->ungetbuf[file->ungetpos++] = c;
464 int
465 findeol(void)
467 int c;
469 /* skip to either EOF or the first real EOL */
470 while (1) {
471 c = lgetc(0);
472 if (c == '\n') {
473 file->lineno++;
474 break;
476 if (c == EOF)
477 break;
479 return (ERROR);
482 int
483 yylex(void)
485 char buf[8096];
486 char *p = buf;
487 int c;
488 int token;
489 int starting = 0;
490 int ending = 0;
491 int quote = 0;
493 if (!in_define && block == 0) {
494 while ((c = lgetc(0)) != '{' && c != EOF) {
495 if (c == '\n')
496 file->lineno++;
499 if (c == EOF)
500 return (0);
502 newblock:
503 c = lgetc(0);
504 if (c == '{' || c == '!') {
505 if (c == '{')
506 block = '}';
507 else
508 block = c;
509 return (c);
511 if (c == '\n')
512 file->lineno++;
515 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
516 if (c == '\n')
517 file->lineno++;
520 if (c == EOF) {
521 yyerror("unterminated block");
522 return (0);
525 yylval.lineno = file->lineno;
527 if (block != 0 && c == block) {
528 if ((c = lgetc(0)) == '}') {
529 if (block == '!') {
530 block = 0;
531 return ('!');
533 block = 0;
534 return ('}');
536 lungetc(c);
537 c = block;
540 if (in_define && block == 0) {
541 if (c == '{')
542 goto newblock;
544 do {
545 if (starting) {
546 if (c == '!' || c == '{') {
547 lungetc(c);
548 lungetc('{');
549 break;
551 starting = 0;
552 lungetc(c);
553 c = '{';
554 } else if (c == '{') {
555 starting = 1;
556 continue;
559 *p++ = c;
560 if ((size_t)(p - buf) >= sizeof(buf)) {
561 yyerror("string too long");
562 return (findeol());
564 } while ((c = lgetc(0)) != EOF && c != '\n');
565 *p = '\0';
566 if (c == EOF) {
567 yyerror("unterminated block");
568 return (0);
570 if (c == '\n')
571 file->lineno++;
572 if ((yylval.v.string = strdup(buf)) == NULL)
573 err(1, "strdup");
574 return (STRING);
577 if (block == '!') {
578 do {
579 if (ending) {
580 if (c == '}') {
581 lungetc(c);
582 lungetc(block);
583 break;
585 ending = 0;
586 lungetc(c);
587 c = block;
588 } else if (c == '!') {
589 ending = 1;
590 continue;
593 *p++ = c;
594 if ((size_t)(p - buf) >= sizeof(buf)) {
595 yyerror("line too long");
596 return (findeol());
598 } while ((c = lgetc(0)) != EOF && c != '\n');
599 *p = '\0';
601 if (c == EOF) {
602 yyerror("unterminated block");
603 return (0);
605 if (c == '\n')
606 file->lineno++;
608 if ((yylval.v.string = strdup(buf)) == NULL)
609 err(1, "strdup");
610 return (STRING);
613 if (c == '|')
614 return (c);
616 do {
617 if (!quote && isspace((unsigned char)c))
618 break;
620 if (c == '"')
621 quote = !quote;
623 if (!quote && c == '|') {
624 lungetc(c);
625 break;
628 if (ending) {
629 if (c == '}') {
630 lungetc(c);
631 lungetc('}');
632 break;
634 ending = 0;
635 lungetc(c);
636 c = block;
637 } else if (!quote && c == '}') {
638 ending = 1;
639 continue;
642 *p++ = c;
643 if ((size_t)(p - buf) >= sizeof(buf)) {
644 yyerror("string too long");
645 return (findeol());
647 } while ((c = lgetc(0)) != EOF);
648 *p = '\0';
650 if (c == EOF) {
651 yyerror(quote ? "unterminated quote" : "unterminated block");
652 return (0);
654 if (c == '\n')
655 file->lineno++;
656 if ((token = lookup(buf)) == STRING)
657 if ((yylval.v.string = strdup(buf)) == NULL)
658 err(1, "strdup");
659 return (token);
662 struct file *
663 pushfile(const char *name, int secret)
665 struct file *nfile;
667 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
668 err(1, "calloc");
669 if ((nfile->name = strdup(name)) == NULL)
670 err(1, "strdup");
671 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
672 warn("can't open %s", nfile->name);
673 free(nfile->name);
674 free(nfile);
675 return (NULL);
677 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
678 nfile->ungetsize = 16;
679 nfile->ungetbuf = malloc(nfile->ungetsize);
680 if (nfile->ungetbuf == NULL)
681 err(1, "malloc");
682 TAILQ_INSERT_TAIL(&files, nfile, entry);
683 return (nfile);
686 int
687 popfile(void)
689 struct file *prev;
691 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
692 prev->errors += file->errors;
694 TAILQ_REMOVE(&files, file, entry);
695 fclose(file->stream);
696 free(file->name);
697 free(file->ungetbuf);
698 free(file);
699 file = prev;
700 return (file ? 0 : EOF);
703 int
704 parse(FILE *outfile, const char *filename)
706 fp = outfile;
708 if ((file = pushfile(filename, 0)) == 0)
709 return (-1);
710 topfile = file;
712 yyparse();
713 errors = file->errors;
714 popfile();
716 return (errors ? -1 : 0);
719 void
720 dbg(void)
722 if (nodebug)
723 return;
725 if (yylval.lineno == lastline + 1) {
726 lastline = yylval.lineno;
727 return;
729 lastline = yylval.lineno;
731 fprintf(fp, "#line %d ", yylval.lineno);
732 printq(file->name);
733 putc('\n', fp);
736 void
737 printq(const char *str)
739 putc('"', fp);
740 for (; *str; ++str) {
741 if (*str == '"')
742 putc('\\', fp);
743 putc(*str, fp);
745 putc('"', fp);