2 52afe358 2022-12-17 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 bff58270 2022-09-19 op * Copyright (c) 2007-2016 Reyk Floeter <reyk@openbsd.org>
4 bff58270 2022-09-19 op * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 bff58270 2022-09-19 op * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 bff58270 2022-09-19 op * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 bff58270 2022-09-19 op * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 bff58270 2022-09-19 op * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 bff58270 2022-09-19 op * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 bff58270 2022-09-19 op * Permission to use, copy, modify, and distribute this software for any
12 bff58270 2022-09-19 op * purpose with or without fee is hereby granted, provided that the above
13 bff58270 2022-09-19 op * copyright notice and this permission notice appear in all copies.
15 bff58270 2022-09-19 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 bff58270 2022-09-19 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 bff58270 2022-09-19 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 bff58270 2022-09-19 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 bff58270 2022-09-19 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 bff58270 2022-09-19 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 bff58270 2022-09-19 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 bff58270 2022-09-19 op #include <sys/queue.h>
28 bff58270 2022-09-19 op #include <ctype.h>
29 bff58270 2022-09-19 op #include <err.h>
30 bff58270 2022-09-19 op #include <stdio.h>
31 bff58270 2022-09-19 op #include <stdlib.h>
32 bff58270 2022-09-19 op #include <stdarg.h>
33 bff58270 2022-09-19 op #include <stdint.h>
34 bff58270 2022-09-19 op #include <string.h>
35 bff58270 2022-09-19 op #include <unistd.h>
37 bff58270 2022-09-19 op #ifndef nitems
38 bff58270 2022-09-19 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 bff58270 2022-09-19 op TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
42 bff58270 2022-09-19 op static struct file {
43 bff58270 2022-09-19 op TAILQ_ENTRY(file) entry;
46 bff58270 2022-09-19 op size_t ungetpos;
47 bff58270 2022-09-19 op size_t ungetsize;
48 8eb6aff8 2022-09-30 op unsigned char *ungetbuf;
49 bff58270 2022-09-19 op int eof_reached;
52 bff58270 2022-09-19 op } *file, *topfile;
53 14c31a43 2022-11-25 op int parse(FILE *, const char *);
54 bff58270 2022-09-19 op struct file *pushfile(const char *, int);
55 bff58270 2022-09-19 op int popfile(void);
56 bff58270 2022-09-19 op int yyparse(void);
57 bff58270 2022-09-19 op int yylex(void);
58 bff58270 2022-09-19 op int yyerror(const char *, ...)
59 bff58270 2022-09-19 op __attribute__((__format__ (printf, 1, 2)))
60 bff58270 2022-09-19 op __attribute__((__nonnull__ (1)));
61 bff58270 2022-09-19 op int kw_cmp(const void *, const void *);
62 bff58270 2022-09-19 op int lookup(char *);
63 bff58270 2022-09-19 op int igetc(void);
64 bff58270 2022-09-19 op int lgetc(int);
65 bff58270 2022-09-19 op void lungetc(int);
66 bff58270 2022-09-19 op int findeol(void);
68 bff58270 2022-09-19 op void dbg(void);
69 bff58270 2022-09-19 op void printq(const char *);
71 bff58270 2022-09-19 op extern int nodebug;
73 14c31a43 2022-11-25 op static FILE *fp;
75 bff58270 2022-09-19 op static int block;
76 bff58270 2022-09-19 op static int in_define;
77 bff58270 2022-09-19 op static int errors;
78 9a90a3d1 2022-09-29 op static int lastline = -1;
80 bff58270 2022-09-19 op typedef struct {
89 9c75d045 2022-10-03 op %token DEFINE ELSE END ERROR FINALLY FOR IF INCLUDE PRINTF
90 3e8b34ba 2023-01-07 op %token RENDER TQFOREACH UNSAFE URLESCAPE WHILE
91 bff58270 2022-09-19 op %token <v.string> STRING
92 279f742d 2023-09-12 op %type <v.string> string nstring
93 6125bd61 2022-09-29 op %type <v.string> stringy
97 bff58270 2022-09-19 op grammar : /* empty */
98 bff58270 2022-09-19 op | grammar include
99 bff58270 2022-09-19 op | grammar verbatim
100 bff58270 2022-09-19 op | grammar block
101 bff58270 2022-09-19 op | grammar error { file->errors++; }
104 bff58270 2022-09-19 op include : INCLUDE STRING {
105 bff58270 2022-09-19 op struct file *nfile;
107 bff58270 2022-09-19 op if ((nfile = pushfile($2, 0)) == NULL) {
108 bff58270 2022-09-19 op yyerror("failed to include file %s", $2);
114 bff58270 2022-09-19 op file = nfile;
115 bff58270 2022-09-19 op lungetc('\n');
119 bff58270 2022-09-19 op verbatim : '!' verbatim1 '!' {
120 bff58270 2022-09-19 op if (in_define) {
121 bff58270 2022-09-19 op /* TODO: check template status and exit in case */
126 bff58270 2022-09-19 op verbatim1 : /* empty */
127 bff58270 2022-09-19 op | verbatim1 STRING {
128 bff58270 2022-09-19 op if (*$2 != '\0') {
130 14c31a43 2022-11-25 op fprintf(fp, "%s\n", $2);
136 bff58270 2022-09-19 op verbatims : /* empty */
137 bff58270 2022-09-19 op | verbatims verbatim
140 279f742d 2023-09-12 op raw : nstring {
142 18906bcc 2023-09-13 op fprintf(fp, "if ((tp_ret = tp_write(tp, ");
144 18906bcc 2023-09-13 op fprintf(fp, ", %zu)) == -1) goto err;\n",
151 bff58270 2022-09-19 op block : define body end {
152 14c31a43 2022-11-25 op fputs("err:\n", fp);
153 14c31a43 2022-11-25 op fputs("return tp_ret;\n", fp);
154 14c31a43 2022-11-25 op fputs("}\n", fp);
155 bff58270 2022-09-19 op in_define = 0;
157 bff58270 2022-09-19 op | define body finally end {
158 14c31a43 2022-11-25 op fputs("return tp_ret;\n", fp);
159 14c31a43 2022-11-25 op fputs("}\n", fp);
160 bff58270 2022-09-19 op in_define = 0;
164 bff58270 2022-09-19 op define : '{' DEFINE string '}' {
165 bff58270 2022-09-19 op in_define = 1;
168 14c31a43 2022-11-25 op fprintf(fp, "int\n%s\n{\n", $3);
169 14c31a43 2022-11-25 op fputs("int tp_ret = 0;\n", fp);
175 bff58270 2022-09-19 op body : /* empty */
176 bff58270 2022-09-19 op | body verbatim
178 bff58270 2022-09-19 op | body special
181 bff58270 2022-09-19 op special : '{' RENDER string '}' {
183 14c31a43 2022-11-25 op fprintf(fp, "if ((tp_ret = %s) == -1) goto err;\n",
188 14c31a43 2022-11-25 op | if body endif { fputs("}\n", fp); }
190 bff58270 2022-09-19 op | '{' string '|' UNSAFE '}' {
193 18906bcc 2023-09-13 op "if ((tp_ret = tp_writes(tp, %s)) == -1)\n",
195 14c31a43 2022-11-25 op fputs("goto err;\n", fp);
198 bff58270 2022-09-19 op | '{' string '|' URLESCAPE '}' {
201 14c31a43 2022-11-25 op "if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
203 14c31a43 2022-11-25 op fputs("goto err;\n", fp);
206 bff58270 2022-09-19 op | '{' string '}' {
209 18906bcc 2023-09-13 op "if ((tp_ret = tp_htmlescape(tp, %s)) == -1)\n",
211 14c31a43 2022-11-25 op fputs("goto err;\n", fp);
216 83e0f74d 2022-09-29 op printf : '{' PRINTF {
218 14c31a43 2022-11-25 op fprintf(fp, "if (asprintf(&tp->tp_tmp, ");
219 83e0f74d 2022-09-29 op } printfargs '}' {
220 14c31a43 2022-11-25 op fputs(") == -1)\n", fp);
221 14c31a43 2022-11-25 op fputs("goto err;\n", fp);
222 18906bcc 2023-09-13 op fputs("if ((tp_ret = tp_htmlescape(tp, tp->tp_tmp)) "
223 14c31a43 2022-11-25 op "== -1)\n", fp);
224 14c31a43 2022-11-25 op fputs("goto err;\n", fp);
225 14c31a43 2022-11-25 op fputs("free(tp->tp_tmp);\n", fp);
226 14c31a43 2022-11-25 op fputs("tp->tp_tmp = NULL;\n", fp);
230 83e0f74d 2022-09-29 op printfargs : /* empty */
231 83e0f74d 2022-09-29 op | printfargs STRING {
232 14c31a43 2022-11-25 op fprintf(fp, " %s", $2);
237 6125bd61 2022-09-29 op if : '{' IF stringy '}' {
239 14c31a43 2022-11-25 op fprintf(fp, "if (%s) {\n", $3);
245 bff58270 2022-09-19 op | else body end
246 bff58270 2022-09-19 op | elsif body endif
249 6125bd61 2022-09-29 op elsif : '{' ELSE IF stringy '}' {
251 14c31a43 2022-11-25 op fprintf(fp, "} else if (%s) {\n", $4);
256 bff58270 2022-09-19 op else : '{' ELSE '}' {
258 14c31a43 2022-11-25 op fputs("} else {\n", fp);
262 9c75d045 2022-10-03 op loop : '{' FOR stringy '}' {
263 14c31a43 2022-11-25 op fprintf(fp, "for (%s) {\n", $3);
266 14c31a43 2022-11-25 op fputs("}\n", fp);
268 9c75d045 2022-10-03 op | '{' TQFOREACH STRING STRING STRING '}' {
269 14c31a43 2022-11-25 op fprintf(fp, "TAILQ_FOREACH(%s, %s, %s) {\n",
275 14c31a43 2022-11-25 op fputs("}\n", fp);
277 3e8b34ba 2023-01-07 op | '{' WHILE stringy '}' {
278 3e8b34ba 2023-01-07 op fprintf(fp, "while (%s) {\n", $3);
281 3e8b34ba 2023-01-07 op fputs("}\n", fp);
285 bff58270 2022-09-19 op end : '{' END '}'
288 bff58270 2022-09-19 op finally : '{' FINALLY '}' {
290 14c31a43 2022-11-25 op fputs("err:\n", fp);
294 279f742d 2023-09-12 op nstring : STRING nstring {
295 279f742d 2023-09-12 op if (asprintf(&$$, "%s%s", $1, $2) == -1)
296 279f742d 2023-09-12 op err(1, "asprintf");
303 bff58270 2022-09-19 op string : STRING string {
304 bff58270 2022-09-19 op if (asprintf(&$$, "%s %s", $1, $2) == -1)
305 bff58270 2022-09-19 op err(1, "asprintf");
312 6125bd61 2022-09-29 op stringy : STRING
313 6125bd61 2022-09-29 op | STRING stringy {
314 6125bd61 2022-09-29 op if (asprintf(&$$, "%s %s", $1, $2) == -1)
315 6125bd61 2022-09-29 op err(1, "asprintf");
319 6125bd61 2022-09-29 op | '|' stringy {
320 6125bd61 2022-09-29 op if (asprintf(&$$, "|%s", $2) == -1)
321 6125bd61 2022-09-29 op err(1, "asprintf");
328 bff58270 2022-09-19 op struct keywords {
329 bff58270 2022-09-19 op const char *k_name;
334 bff58270 2022-09-19 op yyerror(const char *fmt, ...)
339 bff58270 2022-09-19 op file->errors++;
340 bff58270 2022-09-19 op va_start(ap, fmt);
341 bff58270 2022-09-19 op if (vasprintf(&msg, fmt, ap) == -1)
342 bff58270 2022-09-19 op err(1, "yyerror vasprintf");
344 bff58270 2022-09-19 op fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
350 bff58270 2022-09-19 op kw_cmp(const void *k, const void *e)
352 bff58270 2022-09-19 op return (strcmp(k, ((const struct keywords *)e)->k_name));
356 bff58270 2022-09-19 op lookup(char *s)
358 bff58270 2022-09-19 op /* this has to be sorted always */
359 bff58270 2022-09-19 op static const struct keywords keywords[] = {
360 bff58270 2022-09-19 op { "define", DEFINE },
361 bff58270 2022-09-19 op { "else", ELSE },
362 bff58270 2022-09-19 op { "end", END },
363 bff58270 2022-09-19 op { "finally", FINALLY },
364 9c75d045 2022-10-03 op { "for", FOR },
365 bff58270 2022-09-19 op { "if", IF },
366 bff58270 2022-09-19 op { "include", INCLUDE },
367 83e0f74d 2022-09-29 op { "printf", PRINTF },
368 bff58270 2022-09-19 op { "render", RENDER },
369 bff58270 2022-09-19 op { "tailq-foreach", TQFOREACH },
370 bff58270 2022-09-19 op { "unsafe", UNSAFE },
371 bff58270 2022-09-19 op { "urlescape", URLESCAPE },
372 3e8b34ba 2023-01-07 op { "while", WHILE },
374 bff58270 2022-09-19 op const struct keywords *p;
376 bff58270 2022-09-19 op p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
380 bff58270 2022-09-19 op return (p->k_val);
382 bff58270 2022-09-19 op return (STRING);
385 bff58270 2022-09-19 op #define START_EXPAND 1
386 bff58270 2022-09-19 op #define DONE_EXPAND 2
388 bff58270 2022-09-19 op static int expanding;
396 bff58270 2022-09-19 op if (file->ungetpos > 0)
397 bff58270 2022-09-19 op c = file->ungetbuf[--file->ungetpos];
399 bff58270 2022-09-19 op c = getc(file->stream);
401 bff58270 2022-09-19 op if (c == START_EXPAND)
402 bff58270 2022-09-19 op expanding = 1;
403 bff58270 2022-09-19 op else if (c == DONE_EXPAND)
404 bff58270 2022-09-19 op expanding = 0;
412 bff58270 2022-09-19 op lgetc(int quotec)
416 bff58270 2022-09-19 op if (quotec) {
417 bff58270 2022-09-19 op if ((c = igetc()) == EOF) {
418 bff58270 2022-09-19 op yyerror("reached end of filewhile parsing "
419 bff58270 2022-09-19 op "quoted string");
420 bff58270 2022-09-19 op if (file == topfile || popfile() == EOF)
421 bff58270 2022-09-19 op return (EOF);
422 bff58270 2022-09-19 op return (quotec);
428 bff58270 2022-09-19 op if (c == '\t' || c == ' ') {
429 bff58270 2022-09-19 op /* Compress blanks to a sigle space. */
431 bff58270 2022-09-19 op c = getc(file->stream);
432 bff58270 2022-09-19 op } while (c == '\t' || c == ' ');
433 bff58270 2022-09-19 op ungetc(c, file->stream);
437 bff58270 2022-09-19 op if (c == EOF) {
439 bff58270 2022-09-19 op * Fake EOL when hit EOF for the first time. This gets line
440 d023b3ba 2023-03-26 op * count right if last line in included file is syntactically
441 bff58270 2022-09-19 op * invalid and has no newline.
443 bff58270 2022-09-19 op if (file->eof_reached == 0) {
444 bff58270 2022-09-19 op file->eof_reached = 1;
445 bff58270 2022-09-19 op return ('\n');
447 bff58270 2022-09-19 op while (c == EOF) {
448 bff58270 2022-09-19 op if (file == topfile || popfile() == EOF)
449 bff58270 2022-09-19 op return (EOF);
457 bff58270 2022-09-19 op lungetc(int c)
459 bff58270 2022-09-19 op if (c == EOF)
462 bff58270 2022-09-19 op if (file->ungetpos >= file->ungetsize) {
463 bff58270 2022-09-19 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
464 bff58270 2022-09-19 op if (p == NULL)
465 bff58270 2022-09-19 op err(1, "reallocarray");
466 bff58270 2022-09-19 op file->ungetbuf = p;
467 bff58270 2022-09-19 op file->ungetsize *= 2;
469 bff58270 2022-09-19 op file->ungetbuf[file->ungetpos++] = c;
473 bff58270 2022-09-19 op findeol(void)
477 bff58270 2022-09-19 op /* skip to either EOF or the first real EOL */
479 bff58270 2022-09-19 op c = lgetc(0);
480 bff58270 2022-09-19 op if (c == '\n') {
481 bff58270 2022-09-19 op file->lineno++;
484 bff58270 2022-09-19 op if (c == EOF)
487 bff58270 2022-09-19 op return (ERROR);
493 bff58270 2022-09-19 op char buf[8096];
494 bff58270 2022-09-19 op char *p = buf;
497 bff58270 2022-09-19 op int starting = 0;
498 bff58270 2022-09-19 op int ending = 0;
499 14d86fd9 2022-12-04 op int quote = 0;
501 bff58270 2022-09-19 op if (!in_define && block == 0) {
502 bff58270 2022-09-19 op while ((c = lgetc(0)) != '{' && c != EOF) {
503 bff58270 2022-09-19 op if (c == '\n')
504 bff58270 2022-09-19 op file->lineno++;
507 bff58270 2022-09-19 op if (c == EOF)
511 bff58270 2022-09-19 op c = lgetc(0);
512 bff58270 2022-09-19 op if (c == '{' || c == '!') {
513 bff58270 2022-09-19 op if (c == '{')
519 bff58270 2022-09-19 op if (c == '\n')
520 bff58270 2022-09-19 op file->lineno++;
523 bff58270 2022-09-19 op while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
524 bff58270 2022-09-19 op if (c == '\n')
525 bff58270 2022-09-19 op file->lineno++;
528 bff58270 2022-09-19 op if (c == EOF) {
529 bff58270 2022-09-19 op yyerror("unterminated block");
533 bff58270 2022-09-19 op yylval.lineno = file->lineno;
535 bff58270 2022-09-19 op if (block != 0 && c == block) {
536 bff58270 2022-09-19 op if ((c = lgetc(0)) == '}') {
537 bff58270 2022-09-19 op if (block == '!') {
539 bff58270 2022-09-19 op return ('!');
542 bff58270 2022-09-19 op return ('}');
548 bff58270 2022-09-19 op if (in_define && block == 0) {
549 bff58270 2022-09-19 op if (c == '{')
550 bff58270 2022-09-19 op goto newblock;
553 bff58270 2022-09-19 op if (starting) {
554 bff58270 2022-09-19 op if (c == '!' || c == '{') {
556 c063deb1 2022-10-21 op lungetc('{');
559 bff58270 2022-09-19 op starting = 0;
562 bff58270 2022-09-19 op } else if (c == '{') {
563 bff58270 2022-09-19 op starting = 1;
565 8e6adf95 2023-04-13 op } else if (c == '\n')
569 bff58270 2022-09-19 op if ((size_t)(p - buf) >= sizeof(buf)) {
570 bff58270 2022-09-19 op yyerror("string too long");
571 bff58270 2022-09-19 op return (findeol());
573 8e6adf95 2023-04-13 op } while ((c = lgetc(0)) != EOF);
575 bff58270 2022-09-19 op if (c == EOF) {
576 bff58270 2022-09-19 op yyerror("unterminated block");
579 bff58270 2022-09-19 op if (c == '\n')
580 bff58270 2022-09-19 op file->lineno++;
581 bff58270 2022-09-19 op if ((yylval.v.string = strdup(buf)) == NULL)
582 bff58270 2022-09-19 op err(1, "strdup");
583 bff58270 2022-09-19 op return (STRING);
586 bff58270 2022-09-19 op if (block == '!') {
588 bff58270 2022-09-19 op if (ending) {
589 bff58270 2022-09-19 op if (c == '}') {
591 bff58270 2022-09-19 op lungetc(block);
597 bff58270 2022-09-19 op } else if (c == '!') {
600 8e6adf95 2023-04-13 op } else if (c == '\n')
604 bff58270 2022-09-19 op if ((size_t)(p - buf) >= sizeof(buf)) {
605 bff58270 2022-09-19 op yyerror("line too long");
606 bff58270 2022-09-19 op return (findeol());
608 8e6adf95 2023-04-13 op } while ((c = lgetc(0)) != EOF);
611 bff58270 2022-09-19 op if (c == EOF) {
612 bff58270 2022-09-19 op yyerror("unterminated block");
615 bff58270 2022-09-19 op if (c == '\n')
616 bff58270 2022-09-19 op file->lineno++;
618 bff58270 2022-09-19 op if ((yylval.v.string = strdup(buf)) == NULL)
619 bff58270 2022-09-19 op err(1, "strdup");
620 bff58270 2022-09-19 op return (STRING);
623 bff58270 2022-09-19 op if (c == '|')
627 14d86fd9 2022-12-04 op if (!quote && isspace((unsigned char)c))
630 14d86fd9 2022-12-04 op if (c == '"')
631 14d86fd9 2022-12-04 op quote = !quote;
633 14d86fd9 2022-12-04 op if (!quote && c == '|') {
638 bff58270 2022-09-19 op if (ending) {
639 bff58270 2022-09-19 op if (c == '}') {
641 bff58270 2022-09-19 op lungetc('}');
647 14d86fd9 2022-12-04 op } else if (!quote && c == '}') {
653 bff58270 2022-09-19 op if ((size_t)(p - buf) >= sizeof(buf)) {
654 bff58270 2022-09-19 op yyerror("string too long");
655 bff58270 2022-09-19 op return (findeol());
657 14d86fd9 2022-12-04 op } while ((c = lgetc(0)) != EOF);
660 bff58270 2022-09-19 op if (c == EOF) {
661 14d86fd9 2022-12-04 op yyerror(quote ? "unterminated quote" : "unterminated block");
664 bff58270 2022-09-19 op if (c == '\n')
665 bff58270 2022-09-19 op file->lineno++;
666 bff58270 2022-09-19 op if ((token = lookup(buf)) == STRING)
667 bff58270 2022-09-19 op if ((yylval.v.string = strdup(buf)) == NULL)
668 bff58270 2022-09-19 op err(1, "strdup");
669 bff58270 2022-09-19 op return (token);
672 bff58270 2022-09-19 op struct file *
673 bff58270 2022-09-19 op pushfile(const char *name, int secret)
675 bff58270 2022-09-19 op struct file *nfile;
677 bff58270 2022-09-19 op if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
678 bff58270 2022-09-19 op err(1, "calloc");
679 bff58270 2022-09-19 op if ((nfile->name = strdup(name)) == NULL)
680 bff58270 2022-09-19 op err(1, "strdup");
681 bff58270 2022-09-19 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
682 bff58270 2022-09-19 op warn("can't open %s", nfile->name);
683 bff58270 2022-09-19 op free(nfile->name);
685 bff58270 2022-09-19 op return (NULL);
687 bff58270 2022-09-19 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
688 bff58270 2022-09-19 op nfile->ungetsize = 16;
689 bff58270 2022-09-19 op nfile->ungetbuf = malloc(nfile->ungetsize);
690 bff58270 2022-09-19 op if (nfile->ungetbuf == NULL)
691 bff58270 2022-09-19 op err(1, "malloc");
692 bff58270 2022-09-19 op TAILQ_INSERT_TAIL(&files, nfile, entry);
693 bff58270 2022-09-19 op return (nfile);
697 bff58270 2022-09-19 op popfile(void)
699 bff58270 2022-09-19 op struct file *prev;
701 bff58270 2022-09-19 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
702 bff58270 2022-09-19 op prev->errors += file->errors;
704 bff58270 2022-09-19 op TAILQ_REMOVE(&files, file, entry);
705 bff58270 2022-09-19 op fclose(file->stream);
706 bff58270 2022-09-19 op free(file->name);
707 bff58270 2022-09-19 op free(file->ungetbuf);
710 bff58270 2022-09-19 op return (file ? 0 : EOF);
714 14c31a43 2022-11-25 op parse(FILE *outfile, const char *filename)
716 14c31a43 2022-11-25 op fp = outfile;
718 bff58270 2022-09-19 op if ((file = pushfile(filename, 0)) == 0)
720 bff58270 2022-09-19 op topfile = file;
723 bff58270 2022-09-19 op errors = file->errors;
726 bff58270 2022-09-19 op return (errors ? -1 : 0);
735 9a90a3d1 2022-09-29 op if (yylval.lineno == lastline + 1) {
736 9a90a3d1 2022-09-29 op lastline = yylval.lineno;
739 9a90a3d1 2022-09-29 op lastline = yylval.lineno;
741 14c31a43 2022-11-25 op fprintf(fp, "#line %d ", yylval.lineno);
742 bff58270 2022-09-19 op printq(file->name);
743 14c31a43 2022-11-25 op putc('\n', fp);
747 bff58270 2022-09-19 op printq(const char *str)
749 14c31a43 2022-11-25 op putc('"', fp);
750 bff58270 2022-09-19 op for (; *str; ++str) {
751 bff58270 2022-09-19 op if (*str == '"')
752 14c31a43 2022-11-25 op putc('\\', fp);
753 14c31a43 2022-11-25 op putc(*str, fp);
755 14c31a43 2022-11-25 op putc('"', fp);