2 014c66b6 2023-06-25 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 014c66b6 2023-06-25 op * Copyright (c) 2007-2016 Reyk Floeter <reyk@openbsd.org>
4 014c66b6 2023-06-25 op * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 014c66b6 2023-06-25 op * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 014c66b6 2023-06-25 op * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 014c66b6 2023-06-25 op * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 014c66b6 2023-06-25 op * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 014c66b6 2023-06-25 op * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 014c66b6 2023-06-25 op * Permission to use, copy, modify, and distribute this software for any
12 014c66b6 2023-06-25 op * purpose with or without fee is hereby granted, provided that the above
13 014c66b6 2023-06-25 op * copyright notice and this permission notice appear in all copies.
15 014c66b6 2023-06-25 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 014c66b6 2023-06-25 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 014c66b6 2023-06-25 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 014c66b6 2023-06-25 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 014c66b6 2023-06-25 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 014c66b6 2023-06-25 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 014c66b6 2023-06-25 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 014c66b6 2023-06-25 op #include <sys/queue.h>
28 014c66b6 2023-06-25 op #include <ctype.h>
29 014c66b6 2023-06-25 op #include <err.h>
30 014c66b6 2023-06-25 op #include <stdio.h>
31 014c66b6 2023-06-25 op #include <stdlib.h>
32 014c66b6 2023-06-25 op #include <stdarg.h>
33 014c66b6 2023-06-25 op #include <stdint.h>
34 014c66b6 2023-06-25 op #include <string.h>
35 014c66b6 2023-06-25 op #include <unistd.h>
37 014c66b6 2023-06-25 op #define YYERROR_VERBOSE 1
39 014c66b6 2023-06-25 op #ifndef nitems
40 014c66b6 2023-06-25 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 014c66b6 2023-06-25 op TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
44 014c66b6 2023-06-25 op static struct file {
45 014c66b6 2023-06-25 op TAILQ_ENTRY(file) entry;
48 014c66b6 2023-06-25 op size_t ungetpos;
49 014c66b6 2023-06-25 op size_t ungetsize;
50 014c66b6 2023-06-25 op unsigned char *ungetbuf;
51 014c66b6 2023-06-25 op int eof_reached;
54 014c66b6 2023-06-25 op } *file, *topfile;
55 014c66b6 2023-06-25 op int parse(FILE *, const char *);
56 014c66b6 2023-06-25 op struct file *pushfile(const char *, int);
57 014c66b6 2023-06-25 op int popfile(void);
58 014c66b6 2023-06-25 op int yyparse(void);
59 014c66b6 2023-06-25 op int yylex(void);
60 014c66b6 2023-06-25 op int yyerror(const char *, ...)
61 014c66b6 2023-06-25 op __attribute__((__format__ (printf, 1, 2)))
62 014c66b6 2023-06-25 op __attribute__((__nonnull__ (1)));
63 014c66b6 2023-06-25 op int kw_cmp(const void *, const void *);
64 014c66b6 2023-06-25 op int lookup(char *);
65 014c66b6 2023-06-25 op int igetc(void);
66 014c66b6 2023-06-25 op int lgetc(int);
67 014c66b6 2023-06-25 op void lungetc(int);
68 014c66b6 2023-06-25 op int findeol(void);
70 014c66b6 2023-06-25 op void dbg(void);
71 014c66b6 2023-06-25 op void printq(const char *);
73 014c66b6 2023-06-25 op extern int nodebug;
75 014c66b6 2023-06-25 op static FILE *fp;
77 014c66b6 2023-06-25 op static int block;
78 014c66b6 2023-06-25 op static int in_define;
79 014c66b6 2023-06-25 op static int errors;
80 014c66b6 2023-06-25 op static int lastline = -1;
82 014c66b6 2023-06-25 op typedef struct {
91 014c66b6 2023-06-25 op %token DEFINE ELSE END ERROR FINALLY FOR IF INCLUDE PRINTF
92 014c66b6 2023-06-25 op %token RENDER TQFOREACH UNSAFE URLESCAPE WHILE
93 014c66b6 2023-06-25 op %token <v.string> STRING
94 014c66b6 2023-06-25 op %type <v.string> string
95 014c66b6 2023-06-25 op %type <v.string> stringy
99 014c66b6 2023-06-25 op grammar : /* empty */
100 014c66b6 2023-06-25 op | grammar include
101 014c66b6 2023-06-25 op | grammar verbatim
102 014c66b6 2023-06-25 op | grammar block
103 014c66b6 2023-06-25 op | grammar error { file->errors++; }
106 014c66b6 2023-06-25 op include : INCLUDE STRING {
107 014c66b6 2023-06-25 op struct file *nfile;
109 014c66b6 2023-06-25 op if ((nfile = pushfile($2, 0)) == NULL) {
110 014c66b6 2023-06-25 op yyerror("failed to include file %s", $2);
116 014c66b6 2023-06-25 op file = nfile;
117 014c66b6 2023-06-25 op lungetc('\n');
121 014c66b6 2023-06-25 op verbatim : '!' verbatim1 '!' {
122 014c66b6 2023-06-25 op if (in_define) {
123 014c66b6 2023-06-25 op /* TODO: check template status and exit in case */
128 014c66b6 2023-06-25 op verbatim1 : /* empty */
129 014c66b6 2023-06-25 op | verbatim1 STRING {
130 014c66b6 2023-06-25 op if (*$2 != '\0') {
132 014c66b6 2023-06-25 op fprintf(fp, "%s\n", $2);
138 014c66b6 2023-06-25 op verbatims : /* empty */
139 014c66b6 2023-06-25 op | verbatims verbatim
142 014c66b6 2023-06-25 op raw : STRING {
144 014c66b6 2023-06-25 op fprintf(fp, "if ((tp_ret = tp->tp_puts(tp, ");
146 014c66b6 2023-06-25 op fputs(")) == -1) goto err;\n", fp);
152 014c66b6 2023-06-25 op block : define body end {
153 014c66b6 2023-06-25 op fputs("err:\n", fp);
154 014c66b6 2023-06-25 op fputs("return tp_ret;\n", fp);
155 014c66b6 2023-06-25 op fputs("}\n", fp);
156 014c66b6 2023-06-25 op in_define = 0;
158 014c66b6 2023-06-25 op | define body finally end {
159 014c66b6 2023-06-25 op fputs("return tp_ret;\n", fp);
160 014c66b6 2023-06-25 op fputs("}\n", fp);
161 014c66b6 2023-06-25 op in_define = 0;
165 014c66b6 2023-06-25 op define : '{' DEFINE string '}' {
166 014c66b6 2023-06-25 op in_define = 1;
169 014c66b6 2023-06-25 op fprintf(fp, "int\n%s\n{\n", $3);
170 014c66b6 2023-06-25 op fputs("int tp_ret = 0;\n", fp);
176 014c66b6 2023-06-25 op body : /* empty */
177 014c66b6 2023-06-25 op | body verbatim
179 014c66b6 2023-06-25 op | body special
182 014c66b6 2023-06-25 op special : '{' RENDER string '}' {
184 014c66b6 2023-06-25 op fprintf(fp, "if ((tp_ret = %s) == -1) goto err;\n",
189 014c66b6 2023-06-25 op | if body endif { fputs("}\n", fp); }
191 014c66b6 2023-06-25 op | '{' string '|' UNSAFE '}' {
194 014c66b6 2023-06-25 op "if ((tp_ret = tp->tp_puts(tp, %s)) == -1)\n",
196 014c66b6 2023-06-25 op fputs("goto err;\n", fp);
199 014c66b6 2023-06-25 op | '{' string '|' URLESCAPE '}' {
202 014c66b6 2023-06-25 op "if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
204 014c66b6 2023-06-25 op fputs("goto err;\n", fp);
207 014c66b6 2023-06-25 op | '{' string '}' {
210 014c66b6 2023-06-25 op "if ((tp_ret = tp->tp_escape(tp, %s)) == -1)\n",
212 014c66b6 2023-06-25 op fputs("goto err;\n", fp);
217 014c66b6 2023-06-25 op printf : '{' PRINTF {
219 014c66b6 2023-06-25 op fprintf(fp, "if (asprintf(&tp->tp_tmp, ");
220 014c66b6 2023-06-25 op } printfargs '}' {
221 014c66b6 2023-06-25 op fputs(") == -1)\n", fp);
222 014c66b6 2023-06-25 op fputs("goto err;\n", fp);
223 014c66b6 2023-06-25 op fputs("if ((tp_ret = tp->tp_escape(tp, tp->tp_tmp)) "
224 014c66b6 2023-06-25 op "== -1)\n", fp);
225 014c66b6 2023-06-25 op fputs("goto err;\n", fp);
226 014c66b6 2023-06-25 op fputs("free(tp->tp_tmp);\n", fp);
227 014c66b6 2023-06-25 op fputs("tp->tp_tmp = NULL;\n", fp);
231 014c66b6 2023-06-25 op printfargs : /* empty */
232 014c66b6 2023-06-25 op | printfargs STRING {
233 014c66b6 2023-06-25 op fprintf(fp, " %s", $2);
238 014c66b6 2023-06-25 op if : '{' IF stringy '}' {
240 014c66b6 2023-06-25 op fprintf(fp, "if (%s) {\n", $3);
246 014c66b6 2023-06-25 op | else body end
247 014c66b6 2023-06-25 op | elsif body endif
250 014c66b6 2023-06-25 op elsif : '{' ELSE IF stringy '}' {
252 014c66b6 2023-06-25 op fprintf(fp, "} else if (%s) {\n", $4);
257 014c66b6 2023-06-25 op else : '{' ELSE '}' {
259 014c66b6 2023-06-25 op fputs("} else {\n", fp);
263 014c66b6 2023-06-25 op loop : '{' FOR stringy '}' {
264 014c66b6 2023-06-25 op fprintf(fp, "for (%s) {\n", $3);
267 014c66b6 2023-06-25 op fputs("}\n", fp);
269 014c66b6 2023-06-25 op | '{' TQFOREACH STRING STRING STRING '}' {
270 014c66b6 2023-06-25 op fprintf(fp, "TAILQ_FOREACH(%s, %s, %s) {\n",
276 014c66b6 2023-06-25 op fputs("}\n", fp);
278 014c66b6 2023-06-25 op | '{' WHILE stringy '}' {
279 014c66b6 2023-06-25 op fprintf(fp, "while (%s) {\n", $3);
282 014c66b6 2023-06-25 op fputs("}\n", fp);
286 014c66b6 2023-06-25 op end : '{' END '}'
289 014c66b6 2023-06-25 op finally : '{' FINALLY '}' {
291 014c66b6 2023-06-25 op fputs("err:\n", fp);
295 014c66b6 2023-06-25 op string : STRING string {
296 014c66b6 2023-06-25 op if (asprintf(&$$, "%s %s", $1, $2) == -1)
297 014c66b6 2023-06-25 op err(1, "asprintf");
304 014c66b6 2023-06-25 op stringy : STRING
305 014c66b6 2023-06-25 op | STRING stringy {
306 014c66b6 2023-06-25 op if (asprintf(&$$, "%s %s", $1, $2) == -1)
307 014c66b6 2023-06-25 op err(1, "asprintf");
311 014c66b6 2023-06-25 op | '|' stringy {
312 014c66b6 2023-06-25 op if (asprintf(&$$, "|%s", $2) == -1)
313 014c66b6 2023-06-25 op err(1, "asprintf");
320 014c66b6 2023-06-25 op struct keywords {
321 014c66b6 2023-06-25 op const char *k_name;
326 014c66b6 2023-06-25 op yyerror(const char *fmt, ...)
331 014c66b6 2023-06-25 op file->errors++;
332 014c66b6 2023-06-25 op va_start(ap, fmt);
333 014c66b6 2023-06-25 op if (vasprintf(&msg, fmt, ap) == -1)
334 014c66b6 2023-06-25 op err(1, "yyerror vasprintf");
336 014c66b6 2023-06-25 op fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
342 014c66b6 2023-06-25 op kw_cmp(const void *k, const void *e)
344 014c66b6 2023-06-25 op return (strcmp(k, ((const struct keywords *)e)->k_name));
348 014c66b6 2023-06-25 op lookup(char *s)
350 014c66b6 2023-06-25 op /* this has to be sorted always */
351 014c66b6 2023-06-25 op static const struct keywords keywords[] = {
352 014c66b6 2023-06-25 op { "define", DEFINE },
353 014c66b6 2023-06-25 op { "else", ELSE },
354 014c66b6 2023-06-25 op { "end", END },
355 014c66b6 2023-06-25 op { "finally", FINALLY },
356 014c66b6 2023-06-25 op { "for", FOR },
357 014c66b6 2023-06-25 op { "if", IF },
358 014c66b6 2023-06-25 op { "include", INCLUDE },
359 014c66b6 2023-06-25 op { "printf", PRINTF },
360 014c66b6 2023-06-25 op { "render", RENDER },
361 014c66b6 2023-06-25 op { "tailq-foreach", TQFOREACH },
362 014c66b6 2023-06-25 op { "unsafe", UNSAFE },
363 014c66b6 2023-06-25 op { "urlescape", URLESCAPE },
364 014c66b6 2023-06-25 op { "while", WHILE },
366 014c66b6 2023-06-25 op const struct keywords *p;
368 014c66b6 2023-06-25 op p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
372 014c66b6 2023-06-25 op return (p->k_val);
373 014c66b6 2023-06-25 op else { fprintf(stderr, "found string >%s<\n", s);
374 014c66b6 2023-06-25 op return (STRING); }
377 014c66b6 2023-06-25 op #define START_EXPAND 1
378 014c66b6 2023-06-25 op #define DONE_EXPAND 2
380 014c66b6 2023-06-25 op static int expanding;
388 014c66b6 2023-06-25 op if (file->ungetpos > 0)
389 014c66b6 2023-06-25 op c = file->ungetbuf[--file->ungetpos];
391 014c66b6 2023-06-25 op c = getc(file->stream);
393 014c66b6 2023-06-25 op if (c == START_EXPAND)
394 014c66b6 2023-06-25 op expanding = 1;
395 014c66b6 2023-06-25 op else if (c == DONE_EXPAND)
396 014c66b6 2023-06-25 op expanding = 0;
404 014c66b6 2023-06-25 op lgetc(int quotec)
408 014c66b6 2023-06-25 op if (quotec) {
409 014c66b6 2023-06-25 op if ((c = igetc()) == EOF) {
410 014c66b6 2023-06-25 op yyerror("reached end of filewhile parsing "
411 014c66b6 2023-06-25 op "quoted string");
412 014c66b6 2023-06-25 op if (file == topfile || popfile() == EOF)
413 014c66b6 2023-06-25 op return (EOF);
414 014c66b6 2023-06-25 op return (quotec);
421 014c66b6 2023-06-25 op if (c == '\t' || c == ' ') {
422 014c66b6 2023-06-25 op /* Compress blanks to a sigle space. */
424 014c66b6 2023-06-25 op c = getc(file->stream);
425 014c66b6 2023-06-25 op } while (c == '\t' || c == ' ');
426 014c66b6 2023-06-25 op ungetc(c, file->stream);
431 014c66b6 2023-06-25 op if (c == EOF) {
433 014c66b6 2023-06-25 op * Fake EOL when hit EOF for the first time. This gets line
434 014c66b6 2023-06-25 op * count rigchtif last line included file is syntactically
435 014c66b6 2023-06-25 op * invalid and has no newline.
437 014c66b6 2023-06-25 op if (file->eof_reached == 0) {
438 014c66b6 2023-06-25 op file->eof_reached = 1;
439 014c66b6 2023-06-25 op return ('\n');
441 014c66b6 2023-06-25 op while (c == EOF) {
442 014c66b6 2023-06-25 op if (file == topfile || popfile() == EOF)
443 014c66b6 2023-06-25 op return (EOF);
451 014c66b6 2023-06-25 op lungetc(int c)
453 014c66b6 2023-06-25 op if (c == EOF)
456 014c66b6 2023-06-25 op if (file->ungetpos >= file->ungetsize) {
457 014c66b6 2023-06-25 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
458 014c66b6 2023-06-25 op if (p == NULL)
459 014c66b6 2023-06-25 op err(1, "reallocarray");
460 014c66b6 2023-06-25 op file->ungetbuf = p;
461 014c66b6 2023-06-25 op file->ungetsize *= 2;
463 014c66b6 2023-06-25 op file->ungetbuf[file->ungetpos++] = c;
467 014c66b6 2023-06-25 op findeol(void)
471 014c66b6 2023-06-25 op /* skip to either EOF or the first real EOL */
473 014c66b6 2023-06-25 op c = lgetc(0);
474 014c66b6 2023-06-25 op if (c == '\n') {
475 014c66b6 2023-06-25 op file->lineno++;
478 014c66b6 2023-06-25 op if (c == EOF)
481 014c66b6 2023-06-25 op return (ERROR);
487 014c66b6 2023-06-25 op char buf[8096];
488 014c66b6 2023-06-25 op char *p = buf;
491 014c66b6 2023-06-25 op int starting = 0;
492 014c66b6 2023-06-25 op int ending = 0;
493 014c66b6 2023-06-25 op int quote = 0;
495 014c66b6 2023-06-25 op if (!in_define && block == 0) {
496 014c66b6 2023-06-25 op while ((c = lgetc(0)) != '{' && c != EOF) {
497 014c66b6 2023-06-25 op if (c == '\n')
498 014c66b6 2023-06-25 op file->lineno++;
501 014c66b6 2023-06-25 op if (c == EOF)
505 014c66b6 2023-06-25 op c = lgetc(0);
506 014c66b6 2023-06-25 op if (c == '{' || c == '!') {
507 014c66b6 2023-06-25 op if (c == '{')
513 014c66b6 2023-06-25 op while ((x = lgetc(0)) == ' ' || x == '\t' || x == '\n')
514 014c66b6 2023-06-25 op if (x == '\n')
515 014c66b6 2023-06-25 op file->lineno++;
520 014c66b6 2023-06-25 op if (c == '\n')
521 014c66b6 2023-06-25 op file->lineno++;
525 014c66b6 2023-06-25 op while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
526 014c66b6 2023-06-25 op if (c == '\n')
527 014c66b6 2023-06-25 op file->lineno++;
530 014c66b6 2023-06-25 op if ((c = lgetc(0)) == '\n')
531 014c66b6 2023-06-25 op file->lineno++;
534 014c66b6 2023-06-25 op if (c == EOF) {
535 014c66b6 2023-06-25 op yyerror("unterminated block");
539 014c66b6 2023-06-25 op yylval.lineno = file->lineno;
541 014c66b6 2023-06-25 op if (block != 0 && c == block) {
542 014c66b6 2023-06-25 op if ((c = lgetc(0)) == '}') {
543 014c66b6 2023-06-25 op if (block == '!') {
545 014c66b6 2023-06-25 op return ('!');
548 014c66b6 2023-06-25 op return ('}');
554 014c66b6 2023-06-25 op if (in_define && block == 0) {
555 014c66b6 2023-06-25 op if (c == '{')
556 014c66b6 2023-06-25 op goto newblock;
559 014c66b6 2023-06-25 op if (starting) {
560 014c66b6 2023-06-25 op if (c == '!' || c == '{') {
562 014c66b6 2023-06-25 op lungetc('{');
565 014c66b6 2023-06-25 op starting = 0;
568 014c66b6 2023-06-25 op } else if (c == '{') {
569 014c66b6 2023-06-25 op starting = 1;
574 014c66b6 2023-06-25 op if ((size_t)(p - buf) >= sizeof(buf)) {
575 014c66b6 2023-06-25 op yyerror("string too long");
576 014c66b6 2023-06-25 op return (findeol());
578 014c66b6 2023-06-25 op } while ((c = lgetc(0)) != EOF && c != '\n');
580 014c66b6 2023-06-25 op if (c == EOF) {
581 014c66b6 2023-06-25 op yyerror("unterminated block");
584 014c66b6 2023-06-25 op if (c == '\n')
585 014c66b6 2023-06-25 op file->lineno++;
586 014c66b6 2023-06-25 op if ((yylval.v.string = strdup(buf)) == NULL)
587 014c66b6 2023-06-25 op err(1, "strdup");
588 014c66b6 2023-06-25 op return (STRING);
591 014c66b6 2023-06-25 op if (block == '!') {
593 014c66b6 2023-06-25 op if (ending) {
594 014c66b6 2023-06-25 op if (c == '}') {
596 014c66b6 2023-06-25 op lungetc(block);
602 014c66b6 2023-06-25 op } else if (c == '!') {
608 014c66b6 2023-06-25 op if ((size_t)(p - buf) >= sizeof(buf)) {
609 014c66b6 2023-06-25 op yyerror("line too long");
610 014c66b6 2023-06-25 op return (findeol());
612 014c66b6 2023-06-25 op } while ((c = lgetc(0)) != EOF && c != '\n');
615 014c66b6 2023-06-25 op if (c == EOF) {
616 014c66b6 2023-06-25 op yyerror("unterminated block");
619 014c66b6 2023-06-25 op if (c == '\n')
620 014c66b6 2023-06-25 op file->lineno++;
622 014c66b6 2023-06-25 op if ((yylval.v.string = strdup(buf)) == NULL)
623 014c66b6 2023-06-25 op err(1, "strdup");
624 014c66b6 2023-06-25 op return (STRING);
627 014c66b6 2023-06-25 op if (c == '|')
631 014c66b6 2023-06-25 op if (!quote && isspace((unsigned char)c))
634 014c66b6 2023-06-25 op if (c == '"')
635 014c66b6 2023-06-25 op quote = !quote;
637 014c66b6 2023-06-25 op if (!quote && c == '|') {
642 014c66b6 2023-06-25 op if (ending) {
643 014c66b6 2023-06-25 op if (c == '}') {
645 014c66b6 2023-06-25 op lungetc('}');
651 014c66b6 2023-06-25 op } else if (!quote && c == '}') {
657 014c66b6 2023-06-25 op if ((size_t)(p - buf) >= sizeof(buf)) {
658 014c66b6 2023-06-25 op yyerror("string too long");
659 014c66b6 2023-06-25 op return (findeol());
661 014c66b6 2023-06-25 op } while ((c = lgetc(0)) != EOF);
664 014c66b6 2023-06-25 op if (c == EOF) {
665 014c66b6 2023-06-25 op yyerror(quote ? "unterminated quote" : "unterminated block");
668 014c66b6 2023-06-25 op if (c == '\n')
669 014c66b6 2023-06-25 op file->lineno++;
670 014c66b6 2023-06-25 op if ((token = lookup(buf)) == STRING)
671 014c66b6 2023-06-25 op if ((yylval.v.string = strdup(buf)) == NULL)
672 014c66b6 2023-06-25 op err(1, "strdup");
673 014c66b6 2023-06-25 op return (token);
676 014c66b6 2023-06-25 op struct file *
677 014c66b6 2023-06-25 op pushfile(const char *name, int secret)
679 014c66b6 2023-06-25 op struct file *nfile;
681 014c66b6 2023-06-25 op if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
682 014c66b6 2023-06-25 op err(1, "calloc");
683 014c66b6 2023-06-25 op if ((nfile->name = strdup(name)) == NULL)
684 014c66b6 2023-06-25 op err(1, "strdup");
685 014c66b6 2023-06-25 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
686 014c66b6 2023-06-25 op warn("can't open %s", nfile->name);
687 014c66b6 2023-06-25 op free(nfile->name);
689 014c66b6 2023-06-25 op return (NULL);
691 014c66b6 2023-06-25 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
692 014c66b6 2023-06-25 op nfile->ungetsize = 16;
693 014c66b6 2023-06-25 op nfile->ungetbuf = malloc(nfile->ungetsize);
694 014c66b6 2023-06-25 op if (nfile->ungetbuf == NULL)
695 014c66b6 2023-06-25 op err(1, "malloc");
696 014c66b6 2023-06-25 op TAILQ_INSERT_TAIL(&files, nfile, entry);
697 014c66b6 2023-06-25 op return (nfile);
701 014c66b6 2023-06-25 op popfile(void)
703 014c66b6 2023-06-25 op struct file *prev;
705 014c66b6 2023-06-25 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
706 014c66b6 2023-06-25 op prev->errors += file->errors;
708 014c66b6 2023-06-25 op TAILQ_REMOVE(&files, file, entry);
709 014c66b6 2023-06-25 op fclose(file->stream);
710 014c66b6 2023-06-25 op free(file->name);
711 014c66b6 2023-06-25 op free(file->ungetbuf);
714 014c66b6 2023-06-25 op return (file ? 0 : EOF);
718 014c66b6 2023-06-25 op parse(FILE *outfile, const char *filename)
720 014c66b6 2023-06-25 op fp = outfile;
722 014c66b6 2023-06-25 op if ((file = pushfile(filename, 0)) == 0)
724 014c66b6 2023-06-25 op topfile = file;
727 014c66b6 2023-06-25 op errors = file->errors;
730 014c66b6 2023-06-25 op return (errors ? -1 : 0);
739 014c66b6 2023-06-25 op if (yylval.lineno == lastline + 1) {
740 014c66b6 2023-06-25 op lastline = yylval.lineno;
743 014c66b6 2023-06-25 op lastline = yylval.lineno;
745 014c66b6 2023-06-25 op fprintf(fp, "#line %d ", yylval.lineno);
746 014c66b6 2023-06-25 op printq(file->name);
747 014c66b6 2023-06-25 op putc('\n', fp);
751 014c66b6 2023-06-25 op printq(const char *str)
753 014c66b6 2023-06-25 op putc('"', fp);
754 014c66b6 2023-06-25 op for (; *str; ++str) {
755 014c66b6 2023-06-25 op if (*str == '"')
756 014c66b6 2023-06-25 op putc('\\', fp);
757 014c66b6 2023-06-25 op putc(*str, fp);
759 014c66b6 2023-06-25 op putc('"', fp);