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 unsigned 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
91 %type <v.string> stringy
93 %%
95 grammar : /* empty */
96 | grammar include
97 | grammar verbatim
98 | grammar block
99 | grammar error { file->errors++; }
102 include : INCLUDE STRING {
103 struct file *nfile;
105 if ((nfile = pushfile($2, 0)) == NULL) {
106 yyerror("failed to include file %s", $2);
107 free($2);
108 YYERROR;
110 free($2);
112 file = nfile;
113 lungetc('\n');
117 verbatim : '!' verbatim1 '!' {
118 if (in_define) {
119 /* TODO: check template status and exit in case */
124 verbatim1 : /* empty */
125 | verbatim1 STRING {
126 if (*$2 != '\0') {
127 dbg();
128 puts($2);
130 free($2);
134 verbatims : /* empty */
135 | verbatims verbatim
138 raw : STRING {
139 dbg();
140 printf("if ((tp_ret = tp->tp_puts(tp, ");
141 printq($1);
142 puts(")) == -1) goto err;");
144 free($1);
148 block : define body end {
149 puts("err:");
150 puts("return tp_ret;");
151 puts("}");
152 in_define = 0;
154 | define body finally end {
155 puts("return tp_ret;");
156 puts("}");
157 in_define = 0;
161 define : '{' DEFINE string '}' {
162 in_define = 1;
164 dbg();
165 printf("int\n%s\n{\n", $3);
166 puts("int tp_ret = 0;");
168 free($3);
172 body : /* empty */
173 | body verbatim
174 | body raw
175 | body special
178 special : '{' RENDER string '}' {
179 dbg();
180 printf("if ((tp_ret = %s) == -1) goto err;\n", $3);
181 free($3);
183 | printf
184 | if body endif { puts("}"); }
185 | loop
186 | '{' string '|' UNSAFE '}' {
187 dbg();
188 printf("if ((tp_ret = tp->tp_puts(tp, %s)) == -1)\n",
189 $2);
190 puts("goto err;");
191 free($2);
193 | '{' string '|' URLESCAPE '}' {
194 dbg();
195 printf("if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
196 $2);
197 puts("goto err;");
198 free($2);
200 | '{' string '}' {
201 dbg();
202 printf("if ((tp_ret = tp->tp_escape(tp, %s)) == -1)\n",
203 $2);
204 puts("goto err;");
205 free($2);
209 printf : '{' PRINTF {
210 dbg();
211 printf("if (asprintf(&tp->tp_tmp, ");
212 } printfargs '}' {
213 puts(") == -1)");
214 puts("goto err;");
215 puts("if ((tp_ret = tp->tp_escape(tp, tp->tp_tmp)) "
216 "== -1)");
217 puts("goto err;");
218 puts("free(tp->tp_tmp);");
219 puts("tp->tp_tmp = NULL;");
223 printfargs : /* empty */
224 | printfargs STRING {
225 printf(" %s", $2);
226 free($2);
230 if : '{' IF stringy '}' {
231 dbg();
232 printf("if (%s) {\n", $3);
233 free($3);
237 endif : end
238 | else body end
239 | elsif body endif
242 elsif : '{' ELSE IF stringy '}' {
243 dbg();
244 printf("} else if (%s) {\n", $4);
245 free($4);
249 else : '{' ELSE '}' {
250 dbg();
251 puts("} else {");
255 loop : '{' TQFOREACH STRING STRING STRING '}' {
256 printf("TAILQ_FOREACH(%s, %s, %s) {\n",
257 $3, $4, $5);
258 free($3);
259 free($4);
260 free($5);
261 } body end {
262 puts("}");
266 end : '{' END '}'
269 finally : '{' FINALLY '}' {
270 dbg();
271 puts("err:");
272 } verbatims
275 string : STRING string {
276 if (asprintf(&$$, "%s %s", $1, $2) == -1)
277 err(1, "asprintf");
278 free($1);
279 free($2);
281 | STRING
284 stringy : STRING
285 | STRING stringy {
286 if (asprintf(&$$, "%s %s", $1, $2) == -1)
287 err(1, "asprintf");
288 free($1);
289 free($2);
291 | '|' stringy {
292 if (asprintf(&$$, "|%s", $2) == -1)
293 err(1, "asprintf");
294 free($2);
298 %%
300 struct keywords {
301 const char *k_name;
302 int k_val;
303 };
305 int
306 yyerror(const char *fmt, ...)
308 va_list ap;
309 char *msg;
311 file->errors++;
312 va_start(ap, fmt);
313 if (vasprintf(&msg, fmt, ap) == -1)
314 err(1, "yyerror vasprintf");
315 va_end(ap);
316 fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
317 free(msg);
318 return (0);
321 int
322 kw_cmp(const void *k, const void *e)
324 return (strcmp(k, ((const struct keywords *)e)->k_name));
327 int
328 lookup(char *s)
330 /* this has to be sorted always */
331 static const struct keywords keywords[] = {
332 { "define", DEFINE },
333 { "else", ELSE },
334 { "end", END },
335 { "finally", FINALLY },
336 { "if", IF },
337 { "include", INCLUDE },
338 { "printf", PRINTF },
339 { "render", RENDER },
340 { "tailq-foreach", TQFOREACH },
341 { "unsafe", UNSAFE },
342 { "urlescape", URLESCAPE },
343 };
344 const struct keywords *p;
346 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
347 kw_cmp);
349 if (p)
350 return (p->k_val);
351 else
352 return (STRING);
355 #define START_EXPAND 1
356 #define DONE_EXPAND 2
358 static int expanding;
360 int
361 igetc(void)
363 int c;
365 while (1) {
366 if (file->ungetpos > 0)
367 c = file->ungetbuf[--file->ungetpos];
368 else
369 c = getc(file->stream);
371 if (c == START_EXPAND)
372 expanding = 1;
373 else if (c == DONE_EXPAND)
374 expanding = 0;
375 else
376 break;
378 return (c);
381 int
382 lgetc(int quotec)
384 int c;
386 if (quotec) {
387 if ((c = igetc()) == EOF) {
388 yyerror("reached end of filewhile parsing "
389 "quoted string");
390 if (file == topfile || popfile() == EOF)
391 return (EOF);
392 return (quotec);
394 return (c);
397 c = igetc();
398 if (c == '\t' || c == ' ') {
399 /* Compress blanks to a sigle space. */
400 do {
401 c = getc(file->stream);
402 } while (c == '\t' || c == ' ');
403 ungetc(c, file->stream);
404 c = ' ';
407 if (c == EOF) {
408 /*
409 * Fake EOL when hit EOF for the first time. This gets line
410 * count rigchtif last line included file is syntactically
411 * invalid and has no newline.
412 */
413 if (file->eof_reached == 0) {
414 file->eof_reached = 1;
415 return ('\n');
417 while (c == EOF) {
418 if (file == topfile || popfile() == EOF)
419 return (EOF);
420 c = igetc();
423 return (c);
426 void
427 lungetc(int c)
429 if (c == EOF)
430 return;
432 if (file->ungetpos >= file->ungetsize) {
433 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
434 if (p == NULL)
435 err(1, "reallocarray");
436 file->ungetbuf = p;
437 file->ungetsize *= 2;
439 file->ungetbuf[file->ungetpos++] = c;
442 int
443 findeol(void)
445 int c;
447 /* skip to either EOF or the first real EOL */
448 while (1) {
449 c = lgetc(0);
450 if (c == '\n') {
451 file->lineno++;
452 break;
454 if (c == EOF)
455 break;
457 return (ERROR);
460 int
461 yylex(void)
463 char buf[8096];
464 char *p = buf;
465 int c;
466 int token;
467 int starting = 0;
468 int ending = 0;
470 if (!in_define && block == 0) {
471 while ((c = lgetc(0)) != '{' && c != EOF) {
472 if (c == '\n')
473 file->lineno++;
476 if (c == EOF)
477 return (0);
479 newblock:
480 c = lgetc(0);
481 if (c == '{' || c == '!') {
482 if (c == '{')
483 block = '}';
484 else
485 block = c;
486 return (c);
488 if (c == '\n')
489 file->lineno++;
492 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
493 if (c == '\n')
494 file->lineno++;
497 if (c == EOF) {
498 yyerror("unterminated block");
499 return (0);
502 yylval.lineno = file->lineno;
504 if (block != 0 && c == block) {
505 if ((c = lgetc(0)) == '}') {
506 if (block == '!') {
507 block = 0;
508 return ('!');
510 block = 0;
511 return ('}');
513 lungetc(c);
514 c = block;
517 if (in_define && block == 0) {
518 if (c == '{')
519 goto newblock;
521 do {
522 if (starting) {
523 if (c == '!' || c == '{') {
524 lungetc('{');
525 lungetc(c);
526 break;
528 starting = 0;
529 lungetc(c);
530 c = '{';
531 } else if (c == '{') {
532 starting = 1;
533 continue;
536 *p++ = c;
537 if ((size_t)(p - buf) >= sizeof(buf)) {
538 yyerror("string too long");
539 return (findeol());
541 } while ((c = lgetc(0)) != EOF && c != '\n');
542 *p = '\0';
543 if (c == EOF) {
544 yyerror("unterminated block");
545 return (0);
547 if (c == '\n')
548 file->lineno++;
549 if ((yylval.v.string = strdup(buf)) == NULL)
550 err(1, "strdup");
551 return (STRING);
554 if (block == '!') {
555 do {
556 if (ending) {
557 if (c == '}') {
558 lungetc(c);
559 lungetc(block);
560 break;
562 ending = 0;
563 lungetc(c);
564 c = block;
565 } else if (c == '!') {
566 ending = 1;
567 continue;
570 *p++ = c;
571 if ((size_t)(p - buf) >= sizeof(buf)) {
572 yyerror("line too long");
573 return (findeol());
575 } while ((c = lgetc(0)) != EOF && c != '\n');
576 *p = '\0';
578 if (c == EOF) {
579 yyerror("unterminated block");
580 return (0);
582 if (c == '\n')
583 file->lineno++;
585 if ((yylval.v.string = strdup(buf)) == NULL)
586 err(1, "strdup");
587 return (STRING);
590 if (c == '|')
591 return (c);
593 do {
594 if (c == '|') {
595 lungetc(c);
596 break;
599 if (ending) {
600 if (c == '}') {
601 lungetc(c);
602 lungetc('}');
603 break;
605 ending = 0;
606 lungetc(c);
607 c = block;
608 } else if (c == '}') {
609 ending = 1;
610 continue;
613 *p++ = c;
614 if ((size_t)(p - buf) >= sizeof(buf)) {
615 yyerror("string too long");
616 return (findeol());
618 } while ((c = lgetc(0)) != EOF && !isspace((unsigned char)c));
619 *p = '\0';
621 if (c == EOF) {
622 yyerror("unterminated block");
623 return (0);
625 if (c == '\n')
626 file->lineno++;
627 if ((token = lookup(buf)) == STRING)
628 if ((yylval.v.string = strdup(buf)) == NULL)
629 err(1, "strdup");
630 return (token);
633 struct file *
634 pushfile(const char *name, int secret)
636 struct file *nfile;
638 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
639 err(1, "calloc");
640 if ((nfile->name = strdup(name)) == NULL)
641 err(1, "strdup");
642 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
643 warn("can't open %s", nfile->name);
644 free(nfile->name);
645 free(nfile);
646 return (NULL);
648 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
649 nfile->ungetsize = 16;
650 nfile->ungetbuf = malloc(nfile->ungetsize);
651 if (nfile->ungetbuf == NULL)
652 err(1, "malloc");
653 TAILQ_INSERT_TAIL(&files, nfile, entry);
654 return (nfile);
657 int
658 popfile(void)
660 struct file *prev;
662 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
663 prev->errors += file->errors;
665 TAILQ_REMOVE(&files, file, entry);
666 fclose(file->stream);
667 free(file->name);
668 free(file->ungetbuf);
669 free(file);
670 file = prev;
671 return (file ? 0 : EOF);
674 int
675 parse(const char *filename)
677 if ((file = pushfile(filename, 0)) == 0)
678 return (-1);
679 topfile = file;
681 yyparse();
682 errors = file->errors;
683 popfile();
685 return (errors ? -1 : 0);
688 void
689 dbg(void)
691 if (nodebug)
692 return;
694 if (yylval.lineno == lastline + 1) {
695 lastline = yylval.lineno;
696 return;
698 lastline = yylval.lineno;
700 printf("#line %d ", yylval.lineno);
701 printq(file->name);
702 putchar('\n');
705 void
706 printq(const char *str)
708 putchar('"');
709 for (; *str; ++str) {
710 if (*str == '"')
711 putchar('\\');
712 putchar(*str);
714 putchar('"');