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 #define YYERROR_VERBOSE 1
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 { fprintf(stderr, "found string >%s<\n", s);
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 0
421 if (c == '\t' || c == ' ') {
422 /* Compress blanks to a sigle space. */
423 do {
424 c = getc(file->stream);
425 } while (c == '\t' || c == ' ');
426 ungetc(c, file->stream);
427 c = ' ';
429 #endif
431 if (c == EOF) {
432 /*
433 * Fake EOL when hit EOF for the first time. This gets line
434 * count rigchtif last line included file is syntactically
435 * invalid and has no newline.
436 */
437 if (file->eof_reached == 0) {
438 file->eof_reached = 1;
439 return ('\n');
441 while (c == EOF) {
442 if (file == topfile || popfile() == EOF)
443 return (EOF);
444 c = igetc();
447 return (c);
450 void
451 lungetc(int c)
453 if (c == EOF)
454 return;
456 if (file->ungetpos >= file->ungetsize) {
457 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
458 if (p == NULL)
459 err(1, "reallocarray");
460 file->ungetbuf = p;
461 file->ungetsize *= 2;
463 file->ungetbuf[file->ungetpos++] = c;
466 int
467 findeol(void)
469 int c;
471 /* skip to either EOF or the first real EOL */
472 while (1) {
473 c = lgetc(0);
474 if (c == '\n') {
475 file->lineno++;
476 break;
478 if (c == EOF)
479 break;
481 return (ERROR);
484 int
485 yylex(void)
487 char buf[8096];
488 char *p = buf;
489 int c;
490 int token;
491 int starting = 0;
492 int ending = 0;
493 int quote = 0;
495 if (!in_define && block == 0) {
496 while ((c = lgetc(0)) != '{' && c != EOF) {
497 if (c == '\n')
498 file->lineno++;
501 if (c == EOF)
502 return (0);
504 newblock:
505 c = lgetc(0);
506 if (c == '{' || c == '!') {
507 if (c == '{')
508 block = '}';
509 else
510 block = c;
512 int x;
513 while ((x = lgetc(0)) == ' ' || x == '\t' || x == '\n')
514 if (x == '\n')
515 file->lineno++;
516 lungetc(x);
518 return (c);
520 if (c == '\n')
521 file->lineno++;
524 #if 0
525 while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
526 if (c == '\n')
527 file->lineno++;
529 #else
530 if ((c = lgetc(0)) == '\n')
531 file->lineno++;
532 #endif
534 if (c == EOF) {
535 yyerror("unterminated block");
536 return (0);
539 yylval.lineno = file->lineno;
541 if (block != 0 && c == block) {
542 if ((c = lgetc(0)) == '}') {
543 if (block == '!') {
544 block = 0;
545 return ('!');
547 block = 0;
548 return ('}');
550 lungetc(c);
551 c = block;
554 if (in_define && block == 0) {
555 if (c == '{')
556 goto newblock;
558 do {
559 if (starting) {
560 if (c == '!' || c == '{') {
561 lungetc(c);
562 lungetc('{');
563 break;
565 starting = 0;
566 lungetc(c);
567 c = '{';
568 } else if (c == '{') {
569 starting = 1;
570 continue;
573 *p++ = c;
574 if ((size_t)(p - buf) >= sizeof(buf)) {
575 yyerror("string too long");
576 return (findeol());
578 } while ((c = lgetc(0)) != EOF && c != '\n');
579 *p = '\0';
580 if (c == EOF) {
581 yyerror("unterminated block");
582 return (0);
584 if (c == '\n')
585 file->lineno++;
586 if ((yylval.v.string = strdup(buf)) == NULL)
587 err(1, "strdup");
588 return (STRING);
591 if (block == '!') {
592 do {
593 if (ending) {
594 if (c == '}') {
595 lungetc(c);
596 lungetc(block);
597 break;
599 ending = 0;
600 lungetc(c);
601 c = block;
602 } else if (c == '!') {
603 ending = 1;
604 continue;
607 *p++ = c;
608 if ((size_t)(p - buf) >= sizeof(buf)) {
609 yyerror("line too long");
610 return (findeol());
612 } while ((c = lgetc(0)) != EOF && c != '\n');
613 *p = '\0';
615 if (c == EOF) {
616 yyerror("unterminated block");
617 return (0);
619 if (c == '\n')
620 file->lineno++;
622 if ((yylval.v.string = strdup(buf)) == NULL)
623 err(1, "strdup");
624 return (STRING);
627 if (c == '|')
628 return (c);
630 do {
631 if (!quote && isspace((unsigned char)c))
632 break;
634 if (c == '"')
635 quote = !quote;
637 if (!quote && c == '|') {
638 lungetc(c);
639 break;
642 if (ending) {
643 if (c == '}') {
644 lungetc(c);
645 lungetc('}');
646 break;
648 ending = 0;
649 lungetc(c);
650 c = block;
651 } else if (!quote && c == '}') {
652 ending = 1;
653 continue;
656 *p++ = c;
657 if ((size_t)(p - buf) >= sizeof(buf)) {
658 yyerror("string too long");
659 return (findeol());
661 } while ((c = lgetc(0)) != EOF);
662 *p = '\0';
664 if (c == EOF) {
665 yyerror(quote ? "unterminated quote" : "unterminated block");
666 return (0);
668 if (c == '\n')
669 file->lineno++;
670 if ((token = lookup(buf)) == STRING)
671 if ((yylval.v.string = strdup(buf)) == NULL)
672 err(1, "strdup");
673 return (token);
676 struct file *
677 pushfile(const char *name, int secret)
679 struct file *nfile;
681 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
682 err(1, "calloc");
683 if ((nfile->name = strdup(name)) == NULL)
684 err(1, "strdup");
685 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
686 warn("can't open %s", nfile->name);
687 free(nfile->name);
688 free(nfile);
689 return (NULL);
691 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
692 nfile->ungetsize = 16;
693 nfile->ungetbuf = malloc(nfile->ungetsize);
694 if (nfile->ungetbuf == NULL)
695 err(1, "malloc");
696 TAILQ_INSERT_TAIL(&files, nfile, entry);
697 return (nfile);
700 int
701 popfile(void)
703 struct file *prev;
705 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
706 prev->errors += file->errors;
708 TAILQ_REMOVE(&files, file, entry);
709 fclose(file->stream);
710 free(file->name);
711 free(file->ungetbuf);
712 free(file);
713 file = prev;
714 return (file ? 0 : EOF);
717 int
718 parse(FILE *outfile, const char *filename)
720 fp = outfile;
722 if ((file = pushfile(filename, 0)) == 0)
723 return (-1);
724 topfile = file;
726 yyparse();
727 errors = file->errors;
728 popfile();
730 return (errors ? -1 : 0);
733 void
734 dbg(void)
736 if (nodebug)
737 return;
739 if (yylval.lineno == lastline + 1) {
740 lastline = yylval.lineno;
741 return;
743 lastline = yylval.lineno;
745 fprintf(fp, "#line %d ", yylval.lineno);
746 printq(file->name);
747 putc('\n', fp);
750 void
751 printq(const char *str)
753 putc('"', fp);
754 for (; *str; ++str) {
755 if (*str == '"')
756 putc('\\', fp);
757 putc(*str, fp);
759 putc('"', fp);