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;
76 static int lastline = -1;
78 typedef struct {
79 union {
80 char *string;
81 } v;
82 int lineno;
83 } YYSTYPE;
85 %}
87 %token DEFINE ELSE END ERROR FINALLY IF INCLUDE PRINTF
88 %token RENDER TQFOREACH UNSAFE URLESCAPE
89 %token <v.string> STRING
90 %type <v.string> string
92 %%
94 grammar : /* empty */
95 | grammar include
96 | grammar verbatim
97 | grammar block
98 | grammar error { file->errors++; }
99 ;
101 include : INCLUDE STRING {
102 struct file *nfile;
104 if ((nfile = pushfile($2, 0)) == NULL) {
105 yyerror("failed to include file %s", $2);
106 free($2);
107 YYERROR;
109 free($2);
111 file = nfile;
112 lungetc('\n');
116 verbatim : '!' verbatim1 '!' {
117 if (in_define) {
118 /* TODO: check template status and exit in case */
123 verbatim1 : /* empty */
124 | verbatim1 STRING {
125 if (*$2 != '\0') {
126 dbg();
127 puts($2);
129 free($2);
133 verbatims : /* empty */
134 | verbatims verbatim
137 raw : STRING {
138 dbg();
139 printf("if ((tp_ret = tp->tp_puts(tp, ");
140 printq($1);
141 printf(")) == -1) goto err;\n");
143 free($1);
147 block : define body end {
148 printf("err:\n");
149 puts("return tp_ret;");
150 puts("}");
151 in_define = 0;
153 | define body finally end {
154 puts("return tp_ret;");
155 puts("}");
156 in_define = 0;
160 define : '{' DEFINE string '}' {
161 in_define = 1;
163 dbg();
164 printf("int\n%s\n{\n", $3);
165 puts("int tp_ret = 0;");
167 free($3);
171 body : /* empty */
172 | body verbatim
173 | body raw
174 | body special
177 special : '{' RENDER string '}' {
178 dbg();
179 printf("if ((tp_ret = %s) == -1) goto err;\n", $3);
180 free($3);
182 | printf
183 | if body endif { puts("}"); }
184 | loop
185 | '{' string '|' UNSAFE '}' {
186 dbg();
187 printf("if ((tp_ret = tp->tp_puts(tp, %s)) == -1)\n",
188 $2);
189 puts("goto err;");
190 free($2);
192 | '{' string '|' URLESCAPE '}' {
193 dbg();
194 printf("if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
195 $2);
196 puts("goto err;");
197 free($2);
199 | '{' string '}' {
200 dbg();
201 printf("if ((tp_ret = tp->tp_escape(tp, %s)) == -1)\n",
202 $2);
203 puts("goto err;");
204 free($2);
208 printf : '{' PRINTF {
209 dbg();
210 printf("if (asprintf(&tp->tp_tmp, ");
211 } printfargs '}' {
212 printf(") == -1)\n goto err;\n");
213 puts("if ((tp_ret = tp->tp_escape(tp, tp->tp_tmp)) "
214 "== -1)");
215 puts("goto err;");
216 puts("free(tp->tp_tmp);");
217 puts("tp->tp_tmp = NULL;");
221 printfargs : /* empty */
222 | printfargs STRING {
223 printf(" %s", $2);
224 free($2);
227 if : '{' IF string '}' {
228 dbg();
229 printf("if (%s) {\n", $3);
230 free($3);
234 endif : end
235 | else body end
236 | elsif body endif
239 elsif : '{' ELSE IF string '}' {
240 dbg();
241 printf("} else if (%s) {\n", $4);
242 free($4);
246 else : '{' ELSE '}' {
247 dbg();
248 puts("} else {");
252 loop : '{' TQFOREACH STRING STRING STRING '}' {
253 printf("TAILQ_FOREACH(%s, %s, %s) {\n",
254 $3, $4, $5);
255 free($3);
256 free($4);
257 free($5);
258 } body end {
259 puts("}");
263 end : '{' END '}'
266 finally : '{' FINALLY '}' {
267 dbg();
268 puts("err:");
269 } verbatims
272 string : STRING string {
273 if (asprintf(&$$, "%s %s", $1, $2) == -1)
274 err(1, "asprintf");
275 free($1);
276 free($2);
278 | STRING
281 %%
283 struct keywords {
284 const char *k_name;
285 int k_val;
286 };
288 int
289 yyerror(const char *fmt, ...)
291 va_list ap;
292 char *msg;
294 file->errors++;
295 va_start(ap, fmt);
296 if (vasprintf(&msg, fmt, ap) == -1)
297 err(1, "yyerror vasprintf");
298 va_end(ap);
299 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
300 free(msg);
301 return (0);
304 int
305 kw_cmp(const void *k, const void *e)
307 return (strcmp(k, ((const struct keywords *)e)->k_name));
310 int
311 lookup(char *s)
313 /* this has to be sorted always */
314 static const struct keywords keywords[] = {
315 { "define", DEFINE },
316 { "else", ELSE },
317 { "end", END },
318 { "finally", FINALLY },
319 { "if", IF },
320 { "include", INCLUDE },
321 { "printf", PRINTF },
322 { "render", RENDER },
323 { "tailq-foreach", TQFOREACH },
324 { "unsafe", UNSAFE },
325 { "urlescape", URLESCAPE },
326 };
327 const struct keywords *p;
329 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
330 kw_cmp);
332 if (p)
333 return (p->k_val);
334 else
335 return (STRING);
338 #define START_EXPAND 1
339 #define DONE_EXPAND 2
341 static int expanding;
343 int
344 igetc(void)
346 int c;
348 while (1) {
349 if (file->ungetpos > 0)
350 c = file->ungetbuf[--file->ungetpos];
351 else
352 c = getc(file->stream);
354 if (c == START_EXPAND)
355 expanding = 1;
356 else if (c == DONE_EXPAND)
357 expanding = 0;
358 else
359 break;
361 return (c);
364 int
365 lgetc(int quotec)
367 int c;
369 if (quotec) {
370 if ((c = igetc()) == EOF) {
371 yyerror("reached end of filewhile parsing "
372 "quoted string");
373 if (file == topfile || popfile() == EOF)
374 return (EOF);
375 return (quotec);
377 return (c);
380 c = igetc();
381 if (c == '\t' || c == ' ') {
382 /* Compress blanks to a sigle space. */
383 do {
384 c = getc(file->stream);
385 } while (c == '\t' || c == ' ');
386 ungetc(c, file->stream);
387 c = ' ';
390 if (c == EOF) {
391 /*
392 * Fake EOL when hit EOF for the first time. This gets line
393 * count rigchtif last line included file is syntactically
394 * invalid and has no newline.
395 */
396 if (file->eof_reached == 0) {
397 file->eof_reached = 1;
398 return ('\n');
400 while (c == EOF) {
401 if (file == topfile || popfile() == EOF)
402 return (EOF);
403 c = igetc();
406 return (c);
409 void
410 lungetc(int c)
412 if (c == EOF)
413 return;
415 if (file->ungetpos >= file->ungetsize) {
416 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
417 if (p == NULL)
418 err(1, "reallocarray");
419 file->ungetbuf = p;
420 file->ungetsize *= 2;
422 file->ungetbuf[file->ungetpos++] = c;
425 int
426 findeol(void)
428 int c;
430 /* skip to either EOF or the first real EOL */
431 while (1) {
432 c = lgetc(0);
433 if (c == '\n') {
434 file->lineno++;
435 break;
437 if (c == EOF)
438 break;
440 return (ERROR);
443 int
444 yylex(void)
446 char buf[8096];
447 char *p = buf;
448 int c;
449 int token;
450 int starting = 0;
451 int ending = 0;
453 if (!in_define && block == 0) {
454 while ((c = lgetc(0)) != '{' && c != EOF) {
455 if (c == '\n')
456 file->lineno++;
459 if (c == EOF)
460 return (0);
462 newblock:
463 c = lgetc(0);
464 if (c == '{' || c == '!') {
465 if (c == '{')
466 block = '}';
467 else
468 block = c;
469 return (c);
471 if (c == '\n')
472 file->lineno++;
475 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
476 if (c == '\n')
477 file->lineno++;
480 if (c == EOF) {
481 yyerror("unterminated block");
482 return (0);
485 yylval.lineno = file->lineno;
487 if (block != 0 && c == block) {
488 if ((c = lgetc(0)) == '}') {
489 if (block == '!') {
490 block = 0;
491 return ('!');
493 block = 0;
494 return ('}');
496 lungetc(c);
497 c = block;
500 if (in_define && block == 0) {
501 if (c == '{')
502 goto newblock;
504 do {
505 if (starting) {
506 if (c == '!' || c == '{') {
507 lungetc('{');
508 lungetc(c);
509 break;
511 starting = 0;
512 lungetc(c);
513 c = '{';
514 } else if (c == '{') {
515 starting = 1;
516 continue;
519 *p++ = c;
520 if ((size_t)(p - buf) >= sizeof(buf)) {
521 yyerror("string too long");
522 return (findeol());
524 } while ((c = lgetc(0)) != EOF && c != '\n');
525 *p = '\0';
526 if (c == EOF) {
527 yyerror("unterminated block");
528 return (0);
530 if (c == '\n')
531 file->lineno++;
532 if ((yylval.v.string = strdup(buf)) == NULL)
533 err(1, "strdup");
534 return (STRING);
537 if (block == '!') {
538 do {
539 if (ending) {
540 if (c == '}') {
541 lungetc(c);
542 lungetc(block);
543 break;
545 ending = 0;
546 lungetc(c);
547 c = block;
548 } else if (c == '!') {
549 ending = 1;
550 continue;
553 *p++ = c;
554 if ((size_t)(p - buf) >= sizeof(buf)) {
555 yyerror("line too long");
556 return (findeol());
558 } while ((c = lgetc(0)) != EOF && c != '\n');
559 *p = '\0';
561 if (c == EOF) {
562 yyerror("unterminated block");
563 return (0);
565 if (c == '\n')
566 file->lineno++;
568 if ((yylval.v.string = strdup(buf)) == NULL)
569 err(1, "strdup");
570 return (STRING);
573 if (c == '|')
574 return (c);
576 do {
577 if (c == '|') {
578 lungetc(c);
579 break;
582 if (ending) {
583 if (c == '}') {
584 lungetc(c);
585 lungetc('}');
586 break;
588 ending = 0;
589 lungetc(c);
590 c = block;
591 } else if (c == '}') {
592 ending = 1;
593 continue;
596 *p++ = c;
597 if ((size_t)(p - buf) >= sizeof(buf)) {
598 yyerror("string too long");
599 return (findeol());
601 } while ((c = lgetc(0)) != EOF && !isspace((unsigned char)c));
602 *p = '\0';
604 if (c == EOF) {
605 yyerror("unterminated block");
606 return (0);
608 if (c == '\n')
609 file->lineno++;
610 if ((token = lookup(buf)) == STRING)
611 if ((yylval.v.string = strdup(buf)) == NULL)
612 err(1, "strdup");
613 return (token);
616 struct file *
617 pushfile(const char *name, int secret)
619 struct file *nfile;
621 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
622 err(1, "calloc");
623 if ((nfile->name = strdup(name)) == NULL)
624 err(1, "strdup");
625 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
626 warn("can't open %s", nfile->name);
627 free(nfile->name);
628 free(nfile);
629 return (NULL);
631 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
632 nfile->ungetsize = 16;
633 nfile->ungetbuf = malloc(nfile->ungetsize);
634 if (nfile->ungetbuf == NULL)
635 err(1, "malloc");
636 TAILQ_INSERT_TAIL(&files, nfile, entry);
637 return (nfile);
640 int
641 popfile(void)
643 struct file *prev;
645 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
646 prev->errors += file->errors;
648 TAILQ_REMOVE(&files, file, entry);
649 fclose(file->stream);
650 free(file->name);
651 free(file->ungetbuf);
652 free(file);
653 file = prev;
654 return (file ? 0 : EOF);
657 int
658 parse(const char *filename)
660 if ((file = pushfile(filename, 0)) == 0)
661 return (-1);
662 topfile = file;
664 yyparse();
665 errors = file->errors;
666 popfile();
668 return (errors ? -1 : 0);
671 void
672 dbg(void)
674 if (nodebug)
675 return;
677 if (yylval.lineno == lastline + 1) {
678 lastline = yylval.lineno;
679 return;
681 lastline = yylval.lineno;
683 printf("#line %d ", yylval.lineno);
684 printq(file->name);
685 putchar('\n');
688 void
689 printq(const char *str)
691 putchar('"');
692 for (; *str; ++str) {
693 if (*str == '"')
694 putchar('\\');
695 putchar(*str);
697 putchar('"');