Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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 #ifndef nitems
38 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
39 #endif
41 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
42 static struct file {
43 TAILQ_ENTRY(file) entry;
44 FILE *stream;
45 char *name;
46 size_t ungetpos;
47 size_t ungetsize;
48 u_char *ungetbuf;
49 int eof_reached;
50 int lineno;
51 int errors;
52 } *file, *topfile;
53 int parse(const char *);
54 struct file *pushfile(const char *, int);
55 int popfile(void);
56 int yyparse(void);
57 int yylex(void);
58 int yyerror(const char *, ...)
59 __attribute__((__format__ (printf, 1, 2)))
60 __attribute__((__nonnull__ (1)));
61 int kw_cmp(const void *, const void *);
62 int lookup(char *);
63 int igetc(void);
64 int lgetc(int);
65 void lungetc(int);
66 int findeol(void);
68 void dbg(void);
69 void printq(const char *);
71 extern int nodebug;
73 static int block;
74 static int in_define;
75 static int errors;
77 typedef struct {
78 union {
79 char *string;
80 } v;
81 int lineno;
82 } YYSTYPE;
84 %}
86 %token DEFINE ELSE END ERROR ESCAPE FINALLY IF INCLUDE
87 %token RENDER TQFOREACH UNSAFE URLESCAPE
88 %token <v.string> STRING
89 %type <v.string> string
91 %%
93 grammar : /* empty */
94 | grammar include
95 | grammar verbatim
96 | grammar block
97 | grammar error { file->errors++; }
98 ;
100 include : INCLUDE STRING {
101 struct file *nfile;
103 if ((nfile = pushfile($2, 0)) == NULL) {
104 yyerror("failed to include file %s", $2);
105 free($2);
106 YYERROR;
108 free($2);
110 file = nfile;
111 lungetc('\n');
115 verbatim : '!' verbatim1 '!' {
116 if (in_define) {
117 /* TODO: check template status and exit in case */
122 verbatim1 : /* empty */
123 | verbatim1 STRING {
124 if (*$2 != '\0') {
125 dbg();
126 puts($2);
128 free($2);
132 verbatims : /* empty */
133 | verbatims verbatim
136 raw : STRING {
137 dbg();
138 printf("if (tp->tp_puts(tp, ");
139 printq($1);
140 printf(") == -1) goto err;\n");
142 free($1);
146 block : define body end {
147 printf("err:\n");
148 puts("return tp->tp_ret;");
149 puts("}");
150 in_define = 0;
152 | define body finally end {
153 puts("return tp->tp_ret;");
154 puts("}");
155 in_define = 0;
159 define : '{' DEFINE string '}' {
160 in_define = 1;
162 dbg();
163 printf("int\n%s\n{\n", $3);
165 free($3);
169 body : /* empty */
170 | body verbatim
171 | body raw
172 | body special
175 special : '{' RENDER string '}' {
176 dbg();
177 if (strrchr($3, ')') != NULL)
178 printf("if (%s == -1) goto err;\n", $3);
179 else
180 printf("if (%s != NULL && %s(tp) == -1) "
181 "goto err;\n", $3, $3);
182 free($3);
184 | if body endif { puts("}"); }
185 | loop
186 | '{' string '|' ESCAPE '}' {
187 dbg();
188 printf("if (tp->tp_escape(tp, %s) == -1) goto err;\n",
189 $2);
190 free($2);
192 | '{' string '|' UNSAFE '}' {
193 dbg();
194 printf("if (tp->tp_puts(tp, %s) == -1) goto err;\n",
195 $2);
196 free($2);
198 | '{' string '|' URLESCAPE '}' {
199 dbg();
200 printf("if (tp_urlescape(tp, %s) == -1) goto err;\n",
201 $2);
202 free($2);
204 | '{' string '}' {
205 dbg();
206 printf("if (tp->tp_escape(tp, %s) == -1) goto err;\n",
207 $2);
208 free($2);
212 if : '{' IF string '}' {
213 dbg();
214 printf("if (%s) {\n", $3);
215 free($3);
219 endif : end
220 | else body end
221 | elsif body endif
224 elsif : '{' ELSE IF string '}' {
225 dbg();
226 printf("} else if (%s) {\n", $4);
227 free($4);
231 else : '{' ELSE '}' {
232 dbg();
233 puts("} else {");
237 loop : '{' TQFOREACH STRING STRING STRING '}' {
238 printf("TAILQ_FOREACH(%s, %s, %s) {\n",
239 $3, $4, $5);
240 free($3);
241 free($4);
242 free($5);
243 } body end {
244 puts("}");
248 end : '{' END '}'
251 finally : '{' FINALLY '}' {
252 dbg();
253 puts("err:");
254 } verbatims
257 string : STRING string {
258 if (asprintf(&$$, "%s %s", $1, $2) == -1)
259 err(1, "asprintf");
260 free($1);
261 free($2);
263 | STRING
266 %%
268 struct keywords {
269 const char *k_name;
270 int k_val;
271 };
273 int
274 yyerror(const char *fmt, ...)
276 va_list ap;
277 char *msg;
279 file->errors++;
280 va_start(ap, fmt);
281 if (vasprintf(&msg, fmt, ap) == -1)
282 err(1, "yyerror vasprintf");
283 va_end(ap);
284 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
285 free(msg);
286 return (0);
289 int
290 kw_cmp(const void *k, const void *e)
292 return (strcmp(k, ((const struct keywords *)e)->k_name));
295 int
296 lookup(char *s)
298 /* this has to be sorted always */
299 static const struct keywords keywords[] = {
300 { "define", DEFINE },
301 { "else", ELSE },
302 { "end", END },
303 { "escape", ESCAPE },
304 { "finally", FINALLY },
305 { "if", IF },
306 { "include", INCLUDE },
307 { "render", RENDER },
308 { "tailq-foreach", TQFOREACH },
309 { "unsafe", UNSAFE },
310 { "urlescape", URLESCAPE },
311 };
312 const struct keywords *p;
314 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
315 kw_cmp);
317 if (p)
318 return (p->k_val);
319 else
320 return (STRING);
323 #define START_EXPAND 1
324 #define DONE_EXPAND 2
326 static int expanding;
328 int
329 igetc(void)
331 int c;
333 while (1) {
334 if (file->ungetpos > 0)
335 c = file->ungetbuf[--file->ungetpos];
336 else
337 c = getc(file->stream);
339 if (c == START_EXPAND)
340 expanding = 1;
341 else if (c == DONE_EXPAND)
342 expanding = 0;
343 else
344 break;
346 return (c);
349 int
350 lgetc(int quotec)
352 int c, next;
354 if (quotec) {
355 if ((c = igetc()) == EOF) {
356 yyerror("reached end of filewhile parsing "
357 "quoted string");
358 if (file == topfile || popfile() == EOF)
359 return (EOF);
360 return (quotec);
362 return (c);
365 while ((c = igetc()) == '\\') {
366 next = igetc();
367 if (next != '\n') {
368 c = next;
369 break;
371 yylval.lineno = file->lineno;
372 file->lineno++;
374 if (c == '\t' || c == ' ') {
375 /* Compress blanks to a sigle space. */
376 do {
377 c = getc(file->stream);
378 } while (c == '\t' || c == ' ');
379 ungetc(c, file->stream);
380 c = ' ';
383 if (c == EOF) {
384 /*
385 * Fake EOL when hit EOF for the first time. This gets line
386 * count rigchtif last line included file is syntactically
387 * invalid and has no newline.
388 */
389 if (file->eof_reached == 0) {
390 file->eof_reached = 1;
391 return ('\n');
393 while (c == EOF) {
394 if (file == topfile || popfile() == EOF)
395 return (EOF);
396 c = igetc();
399 return (c);
402 void
403 lungetc(int c)
405 if (c == EOF)
406 return;
408 if (file->ungetpos >= file->ungetsize) {
409 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
410 if (p == NULL)
411 err(1, "reallocarray");
412 file->ungetbuf = p;
413 file->ungetsize *= 2;
415 file->ungetbuf[file->ungetpos++] = c;
418 int
419 findeol(void)
421 int c;
423 /* skip to either EOF or the first real EOL */
424 while (1) {
425 c = lgetc(0);
426 if (c == '\n') {
427 file->lineno++;
428 break;
430 if (c == EOF)
431 break;
433 return (ERROR);
436 int
437 yylex(void)
439 char buf[8096];
440 char *p = buf;
441 int c;
442 int token;
443 int starting = 0;
444 int ending = 0;
446 if (!in_define && block == 0) {
447 while ((c = lgetc(0)) != '{' && c != EOF) {
448 if (c == '\n')
449 file->lineno++;
452 if (c == EOF)
453 return (0);
455 newblock:
456 c = lgetc(0);
457 if (c == '{' || c == '!') {
458 if (c == '{')
459 block = '}';
460 else
461 block = c;
462 return (c);
464 if (c == '\n')
465 file->lineno++;
468 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
469 if (c == '\n')
470 file->lineno++;
473 if (c == EOF) {
474 yyerror("unterminated block");
475 return (0);
478 yylval.lineno = file->lineno;
480 if (block != 0 && c == block) {
481 if ((c = lgetc(0)) == '}') {
482 if (block == '!') {
483 block = 0;
484 return ('!');
486 block = 0;
487 return ('}');
489 lungetc(c);
490 c = block;
493 if (in_define && block == 0) {
494 if (c == '{')
495 goto newblock;
497 do {
498 if (starting) {
499 if (c == '!' || c == '{') {
500 lungetc('{');
501 lungetc(c);
502 break;
504 starting = 0;
505 lungetc(c);
506 c = '{';
507 } else if (c == '{') {
508 starting = 1;
509 continue;
512 *p++ = c;
513 if ((size_t)(p - buf) >= sizeof(buf)) {
514 yyerror("string too long");
515 return (findeol());
517 } while ((c = lgetc(0)) != EOF && c != '\n');
518 *p = '\0';
519 if (c == EOF) {
520 yyerror("unterminated block");
521 return (0);
523 if (c == '\n')
524 file->lineno++;
525 if ((yylval.v.string = strdup(buf)) == NULL)
526 err(1, "strdup");
527 return (STRING);
530 if (block == '!') {
531 do {
532 if (ending) {
533 if (c == '}') {
534 lungetc(c);
535 lungetc(block);
536 break;
538 ending = 0;
539 lungetc(c);
540 c = block;
541 } else if (c == '!') {
542 ending = 1;
543 continue;
546 *p++ = c;
547 if ((size_t)(p - buf) >= sizeof(buf)) {
548 yyerror("line too long");
549 return (findeol());
551 } while ((c = lgetc(0)) != EOF && c != '\n');
552 *p = '\0';
554 if (c == EOF) {
555 yyerror("unterminated block");
556 return (0);
558 if (c == '\n')
559 file->lineno++;
561 if ((yylval.v.string = strdup(buf)) == NULL)
562 err(1, "strdup");
563 return (STRING);
566 if (c == '|')
567 return (c);
569 do {
570 if (c == '|') {
571 lungetc(c);
572 break;
575 if (ending) {
576 if (c == '}') {
577 lungetc(c);
578 lungetc('}');
579 break;
581 ending = 0;
582 lungetc(c);
583 c = block;
584 } else if (c == '}') {
585 ending = 1;
586 continue;
589 *p++ = c;
590 if ((size_t)(p - buf) >= sizeof(buf)) {
591 yyerror("string too long");
592 return (findeol());
594 } while ((c = lgetc(0)) != EOF && !isspace((unsigned char)c));
595 *p = '\0';
597 if (c == EOF) {
598 yyerror("unterminated block");
599 return (0);
601 if (c == '\n')
602 file->lineno++;
603 if ((token = lookup(buf)) == STRING)
604 if ((yylval.v.string = strdup(buf)) == NULL)
605 err(1, "strdup");
606 return (token);
609 struct file *
610 pushfile(const char *name, int secret)
612 struct file *nfile;
614 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
615 err(1, "calloc");
616 if ((nfile->name = strdup(name)) == NULL)
617 err(1, "strdup");
618 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
619 warn("can't open %s", nfile->name);
620 free(nfile->name);
621 free(nfile);
622 return (NULL);
624 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
625 nfile->ungetsize = 16;
626 nfile->ungetbuf = malloc(nfile->ungetsize);
627 if (nfile->ungetbuf == NULL)
628 err(1, "malloc");
629 TAILQ_INSERT_TAIL(&files, nfile, entry);
630 return (nfile);
633 int
634 popfile(void)
636 struct file *prev;
638 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
639 prev->errors += file->errors;
641 TAILQ_REMOVE(&files, file, entry);
642 fclose(file->stream);
643 free(file->name);
644 free(file->ungetbuf);
645 free(file);
646 file = prev;
647 return (file ? 0 : EOF);
650 int
651 parse(const char *filename)
653 if ((file = pushfile(filename, 0)) == 0)
654 return (-1);
655 topfile = file;
657 yyparse();
658 errors = file->errors;
659 popfile();
661 return (errors ? -1 : 0);
664 void
665 dbg(void)
667 if (nodebug)
668 return;
670 printf("#line %d ", yylval.lineno);
671 printq(file->name);
672 putchar('\n');
675 void
676 printq(const char *str)
678 putchar('"');
679 for (; *str; ++str) {
680 if (*str == '"')
681 putchar('\\');
682 putchar(*str);
684 putchar('"');