portable: object_qid.c make portable
[got-portable.git] / template / parse.y
blob1e6a9fd07ea3c0e471801ffd54afd0a5773dc68c
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.
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.
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.
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;
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
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);
320 struct keywords {
321 const char *k_name;
322 int k_val;
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);
342 kw_cmp(const void *k, const void *e)
344 return (strcmp(k, ((const struct keywords *)e)->k_name));
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 },
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;
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);
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) {
431 * Fake EOL when hit EOF for the first time. This gets line
432 * count right if last line in included file is syntactically
433 * invalid and has no newline.
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;
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);
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;
557 } else if (c == '\n')
558 break;
560 *p++ = c;
561 if ((size_t)(p - buf) >= sizeof(buf)) {
562 yyerror("string too long");
563 return (findeol());
565 } while ((c = lgetc(0)) != EOF);
566 *p = '\0';
567 if (c == EOF) {
568 yyerror("unterminated block");
569 return (0);
571 if (c == '\n')
572 file->lineno++;
573 if ((yylval.v.string = strdup(buf)) == NULL)
574 err(1, "strdup");
575 return (STRING);
578 if (block == '!') {
579 do {
580 if (ending) {
581 if (c == '}') {
582 lungetc(c);
583 lungetc(block);
584 break;
586 ending = 0;
587 lungetc(c);
588 c = block;
589 } else if (c == '!') {
590 ending = 1;
591 continue;
592 } else if (c == '\n')
593 break;
595 *p++ = c;
596 if ((size_t)(p - buf) >= sizeof(buf)) {
597 yyerror("line too long");
598 return (findeol());
600 } while ((c = lgetc(0)) != EOF);
601 *p = '\0';
603 if (c == EOF) {
604 yyerror("unterminated block");
605 return (0);
607 if (c == '\n')
608 file->lineno++;
610 if ((yylval.v.string = strdup(buf)) == NULL)
611 err(1, "strdup");
612 return (STRING);
615 if (c == '|')
616 return (c);
618 do {
619 if (!quote && isspace((unsigned char)c))
620 break;
622 if (c == '"')
623 quote = !quote;
625 if (!quote && c == '|') {
626 lungetc(c);
627 break;
630 if (ending) {
631 if (c == '}') {
632 lungetc(c);
633 lungetc('}');
634 break;
636 ending = 0;
637 lungetc(c);
638 c = block;
639 } else if (!quote && c == '}') {
640 ending = 1;
641 continue;
644 *p++ = c;
645 if ((size_t)(p - buf) >= sizeof(buf)) {
646 yyerror("string too long");
647 return (findeol());
649 } while ((c = lgetc(0)) != EOF);
650 *p = '\0';
652 if (c == EOF) {
653 yyerror(quote ? "unterminated quote" : "unterminated block");
654 return (0);
656 if (c == '\n')
657 file->lineno++;
658 if ((token = lookup(buf)) == STRING)
659 if ((yylval.v.string = strdup(buf)) == NULL)
660 err(1, "strdup");
661 return (token);
664 struct file *
665 pushfile(const char *name, int secret)
667 struct file *nfile;
669 if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
670 err(1, "calloc");
671 if ((nfile->name = strdup(name)) == NULL)
672 err(1, "strdup");
673 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
674 warn("can't open %s", nfile->name);
675 free(nfile->name);
676 free(nfile);
677 return (NULL);
679 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
680 nfile->ungetsize = 16;
681 nfile->ungetbuf = malloc(nfile->ungetsize);
682 if (nfile->ungetbuf == NULL)
683 err(1, "malloc");
684 TAILQ_INSERT_TAIL(&files, nfile, entry);
685 return (nfile);
689 popfile(void)
691 struct file *prev;
693 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
694 prev->errors += file->errors;
696 TAILQ_REMOVE(&files, file, entry);
697 fclose(file->stream);
698 free(file->name);
699 free(file->ungetbuf);
700 free(file);
701 file = prev;
702 return (file ? 0 : EOF);
706 parse(FILE *outfile, const char *filename)
708 fp = outfile;
710 if ((file = pushfile(filename, 0)) == 0)
711 return (-1);
712 topfile = file;
714 yyparse();
715 errors = file->errors;
716 popfile();
718 return (errors ? -1 : 0);
721 void
722 dbg(void)
724 if (nodebug)
725 return;
727 if (yylval.lineno == lastline + 1) {
728 lastline = yylval.lineno;
729 return;
731 lastline = yylval.lineno;
733 fprintf(fp, "#line %d ", yylval.lineno);
734 printq(file->name);
735 putc('\n', fp);
738 void
739 printq(const char *str)
741 putc('"', fp);
742 for (; *str; ++str) {
743 if (*str == '"')
744 putc('\\', fp);
745 putc(*str, fp);
747 putc('"', fp);