2 b2b17923 2022-12-17 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 2c02675e 2022-12-14 op * Copyright (c) 2007-2016 Reyk Floeter <reyk@openbsd.org>
4 2c02675e 2022-12-14 op * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 2c02675e 2022-12-14 op * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 2c02675e 2022-12-14 op * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 2c02675e 2022-12-14 op * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 2c02675e 2022-12-14 op * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 2c02675e 2022-12-14 op * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 2c02675e 2022-12-14 op * Permission to use, copy, modify, and distribute this software for any
12 2c02675e 2022-12-14 op * purpose with or without fee is hereby granted, provided that the above
13 2c02675e 2022-12-14 op * copyright notice and this permission notice appear in all copies.
15 2c02675e 2022-12-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 2c02675e 2022-12-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 2c02675e 2022-12-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 2c02675e 2022-12-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 2c02675e 2022-12-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 2c02675e 2022-12-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 2c02675e 2022-12-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 2c02675e 2022-12-14 op #include <sys/queue.h>
28 2c02675e 2022-12-14 op #include <ctype.h>
29 2c02675e 2022-12-14 op #include <err.h>
30 2c02675e 2022-12-14 op #include <stdio.h>
31 2c02675e 2022-12-14 op #include <stdlib.h>
32 2c02675e 2022-12-14 op #include <stdarg.h>
33 2c02675e 2022-12-14 op #include <stdint.h>
34 2c02675e 2022-12-14 op #include <string.h>
35 2c02675e 2022-12-14 op #include <unistd.h>
37 2c02675e 2022-12-14 op #ifndef nitems
38 2c02675e 2022-12-14 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 2c02675e 2022-12-14 op TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
42 2c02675e 2022-12-14 op static struct file {
43 2c02675e 2022-12-14 op TAILQ_ENTRY(file) entry;
46 2c02675e 2022-12-14 op size_t ungetpos;
47 2c02675e 2022-12-14 op size_t ungetsize;
48 2c02675e 2022-12-14 op unsigned char *ungetbuf;
49 2c02675e 2022-12-14 op int eof_reached;
52 2c02675e 2022-12-14 op } *file, *topfile;
53 2c02675e 2022-12-14 op int parse(FILE *, const char *);
54 2c02675e 2022-12-14 op struct file *pushfile(const char *, int);
55 2c02675e 2022-12-14 op int popfile(void);
56 2c02675e 2022-12-14 op int yyparse(void);
57 2c02675e 2022-12-14 op int yylex(void);
58 2c02675e 2022-12-14 op int yyerror(const char *, ...)
59 2c02675e 2022-12-14 op __attribute__((__format__ (printf, 1, 2)))
60 2c02675e 2022-12-14 op __attribute__((__nonnull__ (1)));
61 2c02675e 2022-12-14 op int kw_cmp(const void *, const void *);
62 2c02675e 2022-12-14 op int lookup(char *);
63 2c02675e 2022-12-14 op int igetc(void);
64 2c02675e 2022-12-14 op int lgetc(int);
65 2c02675e 2022-12-14 op void lungetc(int);
66 2c02675e 2022-12-14 op int findeol(void);
68 2c02675e 2022-12-14 op void dbg(void);
69 2c02675e 2022-12-14 op void printq(const char *);
71 2c02675e 2022-12-14 op extern int nodebug;
73 2c02675e 2022-12-14 op static FILE *fp;
75 2c02675e 2022-12-14 op static int block;
76 2c02675e 2022-12-14 op static int in_define;
77 2c02675e 2022-12-14 op static int errors;
78 2c02675e 2022-12-14 op static int lastline = -1;
80 2c02675e 2022-12-14 op typedef struct {
89 2c02675e 2022-12-14 op %token DEFINE ELSE END ERROR FINALLY FOR IF INCLUDE PRINTF
90 0f297329 2023-01-06 op %token RENDER TQFOREACH UNSAFE URLESCAPE WHILE
91 2c02675e 2022-12-14 op %token <v.string> STRING
92 2c02675e 2022-12-14 op %type <v.string> string
93 2c02675e 2022-12-14 op %type <v.string> stringy
97 2c02675e 2022-12-14 op grammar : /* empty */
98 2c02675e 2022-12-14 op | grammar include
99 2c02675e 2022-12-14 op | grammar verbatim
100 2c02675e 2022-12-14 op | grammar block
101 2c02675e 2022-12-14 op | grammar error { file->errors++; }
104 2c02675e 2022-12-14 op include : INCLUDE STRING {
105 2c02675e 2022-12-14 op struct file *nfile;
107 2c02675e 2022-12-14 op if ((nfile = pushfile($2, 0)) == NULL) {
108 2c02675e 2022-12-14 op yyerror("failed to include file %s", $2);
114 2c02675e 2022-12-14 op file = nfile;
115 2c02675e 2022-12-14 op lungetc('\n');
119 2c02675e 2022-12-14 op verbatim : '!' verbatim1 '!' {
120 2c02675e 2022-12-14 op if (in_define) {
121 2c02675e 2022-12-14 op /* TODO: check template status and exit in case */
126 2c02675e 2022-12-14 op verbatim1 : /* empty */
127 2c02675e 2022-12-14 op | verbatim1 STRING {
128 2c02675e 2022-12-14 op if (*$2 != '\0') {
130 2c02675e 2022-12-14 op fprintf(fp, "%s\n", $2);
136 2c02675e 2022-12-14 op verbatims : /* empty */
137 2c02675e 2022-12-14 op | verbatims verbatim
140 2c02675e 2022-12-14 op raw : STRING {
142 2c02675e 2022-12-14 op fprintf(fp, "if ((tp_ret = tp->tp_puts(tp, ");
144 2c02675e 2022-12-14 op fputs(")) == -1) goto err;\n", fp);
150 2c02675e 2022-12-14 op block : define body end {
151 2c02675e 2022-12-14 op fputs("err:\n", fp);
152 2c02675e 2022-12-14 op fputs("return tp_ret;\n", fp);
153 2c02675e 2022-12-14 op fputs("}\n", fp);
154 2c02675e 2022-12-14 op in_define = 0;
156 2c02675e 2022-12-14 op | define body finally end {
157 2c02675e 2022-12-14 op fputs("return tp_ret;\n", fp);
158 2c02675e 2022-12-14 op fputs("}\n", fp);
159 2c02675e 2022-12-14 op in_define = 0;
163 2c02675e 2022-12-14 op define : '{' DEFINE string '}' {
164 2c02675e 2022-12-14 op in_define = 1;
167 2c02675e 2022-12-14 op fprintf(fp, "int\n%s\n{\n", $3);
168 2c02675e 2022-12-14 op fputs("int tp_ret = 0;\n", fp);
174 2c02675e 2022-12-14 op body : /* empty */
175 2c02675e 2022-12-14 op | body verbatim
177 2c02675e 2022-12-14 op | body special
180 2c02675e 2022-12-14 op special : '{' RENDER string '}' {
182 2c02675e 2022-12-14 op fprintf(fp, "if ((tp_ret = %s) == -1) goto err;\n",
187 2c02675e 2022-12-14 op | if body endif { fputs("}\n", fp); }
189 2c02675e 2022-12-14 op | '{' string '|' UNSAFE '}' {
192 2c02675e 2022-12-14 op "if ((tp_ret = tp->tp_puts(tp, %s)) == -1)\n",
194 2c02675e 2022-12-14 op fputs("goto err;\n", fp);
197 2c02675e 2022-12-14 op | '{' string '|' URLESCAPE '}' {
200 2c02675e 2022-12-14 op "if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
202 2c02675e 2022-12-14 op fputs("goto err;\n", fp);
205 2c02675e 2022-12-14 op | '{' string '}' {
208 2c02675e 2022-12-14 op "if ((tp_ret = tp->tp_escape(tp, %s)) == -1)\n",
210 2c02675e 2022-12-14 op fputs("goto err;\n", fp);
215 2c02675e 2022-12-14 op printf : '{' PRINTF {
217 2c02675e 2022-12-14 op fprintf(fp, "if (asprintf(&tp->tp_tmp, ");
218 2c02675e 2022-12-14 op } printfargs '}' {
219 2c02675e 2022-12-14 op fputs(") == -1)\n", fp);
220 2c02675e 2022-12-14 op fputs("goto err;\n", fp);
221 2c02675e 2022-12-14 op fputs("if ((tp_ret = tp->tp_escape(tp, tp->tp_tmp)) "
222 2c02675e 2022-12-14 op "== -1)\n", fp);
223 2c02675e 2022-12-14 op fputs("goto err;\n", fp);
224 2c02675e 2022-12-14 op fputs("free(tp->tp_tmp);\n", fp);
225 2c02675e 2022-12-14 op fputs("tp->tp_tmp = NULL;\n", fp);
229 2c02675e 2022-12-14 op printfargs : /* empty */
230 2c02675e 2022-12-14 op | printfargs STRING {
231 2c02675e 2022-12-14 op fprintf(fp, " %s", $2);
236 2c02675e 2022-12-14 op if : '{' IF stringy '}' {
238 2c02675e 2022-12-14 op fprintf(fp, "if (%s) {\n", $3);
244 2c02675e 2022-12-14 op | else body end
245 2c02675e 2022-12-14 op | elsif body endif
248 2c02675e 2022-12-14 op elsif : '{' ELSE IF stringy '}' {
250 2c02675e 2022-12-14 op fprintf(fp, "} else if (%s) {\n", $4);
255 2c02675e 2022-12-14 op else : '{' ELSE '}' {
257 2c02675e 2022-12-14 op fputs("} else {\n", fp);
261 2c02675e 2022-12-14 op loop : '{' FOR stringy '}' {
262 2c02675e 2022-12-14 op fprintf(fp, "for (%s) {\n", $3);
265 2c02675e 2022-12-14 op fputs("}\n", fp);
267 2c02675e 2022-12-14 op | '{' TQFOREACH STRING STRING STRING '}' {
268 2c02675e 2022-12-14 op fprintf(fp, "TAILQ_FOREACH(%s, %s, %s) {\n",
274 2c02675e 2022-12-14 op fputs("}\n", fp);
276 0f297329 2023-01-06 op | '{' WHILE stringy '}' {
277 0f297329 2023-01-06 op fprintf(fp, "while (%s) {\n", $3);
280 0f297329 2023-01-06 op fputs("}\n", fp);
284 2c02675e 2022-12-14 op end : '{' END '}'
287 2c02675e 2022-12-14 op finally : '{' FINALLY '}' {
289 2c02675e 2022-12-14 op fputs("err:\n", fp);
293 2c02675e 2022-12-14 op string : STRING string {
294 2c02675e 2022-12-14 op if (asprintf(&$$, "%s %s", $1, $2) == -1)
295 2c02675e 2022-12-14 op err(1, "asprintf");
302 2c02675e 2022-12-14 op stringy : STRING
303 2c02675e 2022-12-14 op | STRING stringy {
304 2c02675e 2022-12-14 op if (asprintf(&$$, "%s %s", $1, $2) == -1)
305 2c02675e 2022-12-14 op err(1, "asprintf");
309 2c02675e 2022-12-14 op | '|' stringy {
310 2c02675e 2022-12-14 op if (asprintf(&$$, "|%s", $2) == -1)
311 2c02675e 2022-12-14 op err(1, "asprintf");
318 2c02675e 2022-12-14 op struct keywords {
319 2c02675e 2022-12-14 op const char *k_name;
324 2c02675e 2022-12-14 op yyerror(const char *fmt, ...)
329 2c02675e 2022-12-14 op file->errors++;
330 2c02675e 2022-12-14 op va_start(ap, fmt);
331 2c02675e 2022-12-14 op if (vasprintf(&msg, fmt, ap) == -1)
332 2c02675e 2022-12-14 op err(1, "yyerror vasprintf");
334 2c02675e 2022-12-14 op fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
340 2c02675e 2022-12-14 op kw_cmp(const void *k, const void *e)
342 2c02675e 2022-12-14 op return (strcmp(k, ((const struct keywords *)e)->k_name));
346 2c02675e 2022-12-14 op lookup(char *s)
348 2c02675e 2022-12-14 op /* this has to be sorted always */
349 2c02675e 2022-12-14 op static const struct keywords keywords[] = {
350 2c02675e 2022-12-14 op { "define", DEFINE },
351 2c02675e 2022-12-14 op { "else", ELSE },
352 2c02675e 2022-12-14 op { "end", END },
353 2c02675e 2022-12-14 op { "finally", FINALLY },
354 2c02675e 2022-12-14 op { "for", FOR },
355 2c02675e 2022-12-14 op { "if", IF },
356 2c02675e 2022-12-14 op { "include", INCLUDE },
357 2c02675e 2022-12-14 op { "printf", PRINTF },
358 2c02675e 2022-12-14 op { "render", RENDER },
359 2c02675e 2022-12-14 op { "tailq-foreach", TQFOREACH },
360 2c02675e 2022-12-14 op { "unsafe", UNSAFE },
361 2c02675e 2022-12-14 op { "urlescape", URLESCAPE },
362 0f297329 2023-01-06 op { "while", WHILE },
364 2c02675e 2022-12-14 op const struct keywords *p;
366 2c02675e 2022-12-14 op p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
370 2c02675e 2022-12-14 op return (p->k_val);
372 2c02675e 2022-12-14 op return (STRING);
375 2c02675e 2022-12-14 op #define START_EXPAND 1
376 2c02675e 2022-12-14 op #define DONE_EXPAND 2
378 2c02675e 2022-12-14 op static int expanding;
386 2c02675e 2022-12-14 op if (file->ungetpos > 0)
387 2c02675e 2022-12-14 op c = file->ungetbuf[--file->ungetpos];
389 2c02675e 2022-12-14 op c = getc(file->stream);
391 2c02675e 2022-12-14 op if (c == START_EXPAND)
392 2c02675e 2022-12-14 op expanding = 1;
393 2c02675e 2022-12-14 op else if (c == DONE_EXPAND)
394 2c02675e 2022-12-14 op expanding = 0;
402 2c02675e 2022-12-14 op lgetc(int quotec)
406 2c02675e 2022-12-14 op if (quotec) {
407 2c02675e 2022-12-14 op if ((c = igetc()) == EOF) {
408 2c02675e 2022-12-14 op yyerror("reached end of filewhile parsing "
409 2c02675e 2022-12-14 op "quoted string");
410 2c02675e 2022-12-14 op if (file == topfile || popfile() == EOF)
411 2c02675e 2022-12-14 op return (EOF);
412 2c02675e 2022-12-14 op return (quotec);
418 2c02675e 2022-12-14 op if (c == '\t' || c == ' ') {
419 2c02675e 2022-12-14 op /* Compress blanks to a sigle space. */
421 2c02675e 2022-12-14 op c = getc(file->stream);
422 2c02675e 2022-12-14 op } while (c == '\t' || c == ' ');
423 2c02675e 2022-12-14 op ungetc(c, file->stream);
427 2c02675e 2022-12-14 op if (c == EOF) {
429 2c02675e 2022-12-14 op * Fake EOL when hit EOF for the first time. This gets line
430 2c02675e 2022-12-14 op * count rigchtif last line included file is syntactically
431 2c02675e 2022-12-14 op * invalid and has no newline.
433 2c02675e 2022-12-14 op if (file->eof_reached == 0) {
434 2c02675e 2022-12-14 op file->eof_reached = 1;
435 2c02675e 2022-12-14 op return ('\n');
437 2c02675e 2022-12-14 op while (c == EOF) {
438 2c02675e 2022-12-14 op if (file == topfile || popfile() == EOF)
439 2c02675e 2022-12-14 op return (EOF);
447 2c02675e 2022-12-14 op lungetc(int c)
449 2c02675e 2022-12-14 op if (c == EOF)
452 2c02675e 2022-12-14 op if (file->ungetpos >= file->ungetsize) {
453 2c02675e 2022-12-14 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
454 2c02675e 2022-12-14 op if (p == NULL)
455 2c02675e 2022-12-14 op err(1, "reallocarray");
456 2c02675e 2022-12-14 op file->ungetbuf = p;
457 2c02675e 2022-12-14 op file->ungetsize *= 2;
459 2c02675e 2022-12-14 op file->ungetbuf[file->ungetpos++] = c;
463 2c02675e 2022-12-14 op findeol(void)
467 2c02675e 2022-12-14 op /* skip to either EOF or the first real EOL */
469 2c02675e 2022-12-14 op c = lgetc(0);
470 2c02675e 2022-12-14 op if (c == '\n') {
471 2c02675e 2022-12-14 op file->lineno++;
474 2c02675e 2022-12-14 op if (c == EOF)
477 2c02675e 2022-12-14 op return (ERROR);
483 2c02675e 2022-12-14 op char buf[8096];
484 2c02675e 2022-12-14 op char *p = buf;
487 2c02675e 2022-12-14 op int starting = 0;
488 2c02675e 2022-12-14 op int ending = 0;
489 2c02675e 2022-12-14 op int quote = 0;
491 2c02675e 2022-12-14 op if (!in_define && block == 0) {
492 2c02675e 2022-12-14 op while ((c = lgetc(0)) != '{' && c != EOF) {
493 2c02675e 2022-12-14 op if (c == '\n')
494 2c02675e 2022-12-14 op file->lineno++;
497 2c02675e 2022-12-14 op if (c == EOF)
501 2c02675e 2022-12-14 op c = lgetc(0);
502 2c02675e 2022-12-14 op if (c == '{' || c == '!') {
503 2c02675e 2022-12-14 op if (c == '{')
509 2c02675e 2022-12-14 op if (c == '\n')
510 2c02675e 2022-12-14 op file->lineno++;
513 2c02675e 2022-12-14 op while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
514 2c02675e 2022-12-14 op if (c == '\n')
515 2c02675e 2022-12-14 op file->lineno++;
518 2c02675e 2022-12-14 op if (c == EOF) {
519 2c02675e 2022-12-14 op yyerror("unterminated block");
523 2c02675e 2022-12-14 op yylval.lineno = file->lineno;
525 2c02675e 2022-12-14 op if (block != 0 && c == block) {
526 2c02675e 2022-12-14 op if ((c = lgetc(0)) == '}') {
527 2c02675e 2022-12-14 op if (block == '!') {
529 2c02675e 2022-12-14 op return ('!');
532 2c02675e 2022-12-14 op return ('}');
538 2c02675e 2022-12-14 op if (in_define && block == 0) {
539 2c02675e 2022-12-14 op if (c == '{')
540 2c02675e 2022-12-14 op goto newblock;
543 2c02675e 2022-12-14 op if (starting) {
544 2c02675e 2022-12-14 op if (c == '!' || c == '{') {
546 2c02675e 2022-12-14 op lungetc('{');
549 2c02675e 2022-12-14 op starting = 0;
552 2c02675e 2022-12-14 op } else if (c == '{') {
553 2c02675e 2022-12-14 op starting = 1;
558 2c02675e 2022-12-14 op if ((size_t)(p - buf) >= sizeof(buf)) {
559 2c02675e 2022-12-14 op yyerror("string too long");
560 2c02675e 2022-12-14 op return (findeol());
562 2c02675e 2022-12-14 op } while ((c = lgetc(0)) != EOF && c != '\n');
564 2c02675e 2022-12-14 op if (c == EOF) {
565 2c02675e 2022-12-14 op yyerror("unterminated block");
568 2c02675e 2022-12-14 op if (c == '\n')
569 2c02675e 2022-12-14 op file->lineno++;
570 2c02675e 2022-12-14 op if ((yylval.v.string = strdup(buf)) == NULL)
571 2c02675e 2022-12-14 op err(1, "strdup");
572 2c02675e 2022-12-14 op return (STRING);
575 2c02675e 2022-12-14 op if (block == '!') {
577 2c02675e 2022-12-14 op if (ending) {
578 2c02675e 2022-12-14 op if (c == '}') {
580 2c02675e 2022-12-14 op lungetc(block);
586 2c02675e 2022-12-14 op } else if (c == '!') {
592 2c02675e 2022-12-14 op if ((size_t)(p - buf) >= sizeof(buf)) {
593 2c02675e 2022-12-14 op yyerror("line too long");
594 2c02675e 2022-12-14 op return (findeol());
596 2c02675e 2022-12-14 op } while ((c = lgetc(0)) != EOF && c != '\n');
599 2c02675e 2022-12-14 op if (c == EOF) {
600 2c02675e 2022-12-14 op yyerror("unterminated block");
603 2c02675e 2022-12-14 op if (c == '\n')
604 2c02675e 2022-12-14 op file->lineno++;
606 2c02675e 2022-12-14 op if ((yylval.v.string = strdup(buf)) == NULL)
607 2c02675e 2022-12-14 op err(1, "strdup");
608 2c02675e 2022-12-14 op return (STRING);
611 2c02675e 2022-12-14 op if (c == '|')
615 2c02675e 2022-12-14 op if (!quote && isspace((unsigned char)c))
618 2c02675e 2022-12-14 op if (c == '"')
619 2c02675e 2022-12-14 op quote = !quote;
621 2c02675e 2022-12-14 op if (!quote && c == '|') {
626 2c02675e 2022-12-14 op if (ending) {
627 2c02675e 2022-12-14 op if (c == '}') {
629 2c02675e 2022-12-14 op lungetc('}');
635 2c02675e 2022-12-14 op } else if (!quote && c == '}') {
641 2c02675e 2022-12-14 op if ((size_t)(p - buf) >= sizeof(buf)) {
642 2c02675e 2022-12-14 op yyerror("string too long");
643 2c02675e 2022-12-14 op return (findeol());
645 2c02675e 2022-12-14 op } while ((c = lgetc(0)) != EOF);
648 2c02675e 2022-12-14 op if (c == EOF) {
649 2c02675e 2022-12-14 op yyerror(quote ? "unterminated quote" : "unterminated block");
652 2c02675e 2022-12-14 op if (c == '\n')
653 2c02675e 2022-12-14 op file->lineno++;
654 2c02675e 2022-12-14 op if ((token = lookup(buf)) == STRING)
655 2c02675e 2022-12-14 op if ((yylval.v.string = strdup(buf)) == NULL)
656 2c02675e 2022-12-14 op err(1, "strdup");
657 2c02675e 2022-12-14 op return (token);
660 2c02675e 2022-12-14 op struct file *
661 2c02675e 2022-12-14 op pushfile(const char *name, int secret)
663 2c02675e 2022-12-14 op struct file *nfile;
665 2c02675e 2022-12-14 op if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
666 2c02675e 2022-12-14 op err(1, "calloc");
667 2c02675e 2022-12-14 op if ((nfile->name = strdup(name)) == NULL)
668 2c02675e 2022-12-14 op err(1, "strdup");
669 2c02675e 2022-12-14 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
670 2c02675e 2022-12-14 op warn("can't open %s", nfile->name);
671 2c02675e 2022-12-14 op free(nfile->name);
673 2c02675e 2022-12-14 op return (NULL);
675 2c02675e 2022-12-14 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
676 2c02675e 2022-12-14 op nfile->ungetsize = 16;
677 2c02675e 2022-12-14 op nfile->ungetbuf = malloc(nfile->ungetsize);
678 2c02675e 2022-12-14 op if (nfile->ungetbuf == NULL)
679 2c02675e 2022-12-14 op err(1, "malloc");
680 2c02675e 2022-12-14 op TAILQ_INSERT_TAIL(&files, nfile, entry);
681 2c02675e 2022-12-14 op return (nfile);
685 2c02675e 2022-12-14 op popfile(void)
687 2c02675e 2022-12-14 op struct file *prev;
689 2c02675e 2022-12-14 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
690 2c02675e 2022-12-14 op prev->errors += file->errors;
692 2c02675e 2022-12-14 op TAILQ_REMOVE(&files, file, entry);
693 2c02675e 2022-12-14 op fclose(file->stream);
694 2c02675e 2022-12-14 op free(file->name);
695 2c02675e 2022-12-14 op free(file->ungetbuf);
698 2c02675e 2022-12-14 op return (file ? 0 : EOF);
702 2c02675e 2022-12-14 op parse(FILE *outfile, const char *filename)
704 2c02675e 2022-12-14 op fp = outfile;
706 2c02675e 2022-12-14 op if ((file = pushfile(filename, 0)) == 0)
708 2c02675e 2022-12-14 op topfile = file;
711 2c02675e 2022-12-14 op errors = file->errors;
714 2c02675e 2022-12-14 op return (errors ? -1 : 0);
723 2c02675e 2022-12-14 op if (yylval.lineno == lastline + 1) {
724 2c02675e 2022-12-14 op lastline = yylval.lineno;
727 2c02675e 2022-12-14 op lastline = yylval.lineno;
729 2c02675e 2022-12-14 op fprintf(fp, "#line %d ", yylval.lineno);
730 2c02675e 2022-12-14 op printq(file->name);
731 2c02675e 2022-12-14 op putc('\n', fp);
735 2c02675e 2022-12-14 op printq(const char *str)
737 2c02675e 2022-12-14 op putc('"', fp);
738 2c02675e 2022-12-14 op for (; *str; ++str) {
739 2c02675e 2022-12-14 op if (*str == '"')
740 2c02675e 2022-12-14 op putc('\\', fp);
741 2c02675e 2022-12-14 op putc(*str, fp);
743 2c02675e 2022-12-14 op putc('"', fp);