7 char path
[64]; /* for file buffers */
9 char *buf
; /* for string buffers */
14 int nl
; /* read \n, if the previous char was not */
18 static struct inbuf
*buf
;
19 static char files
[NFILES
][PATHLEN
];
24 static char **args_init(char **args
);
25 static void args_free(char **args
);
27 static void in_new(void)
29 struct inbuf
*next
= malloc(sizeof(*next
));
30 memset(next
, 0, sizeof(*next
));
36 void in_push(char *s
, char **args
)
40 buf
->buf
= malloc(len
+ 1);
43 buf
->args
= args
? args_init(args
) : NULL
;
46 void in_pushnl(char *s
, char **args
)
52 void in_source(char *path
)
54 FILE *fin
= path
&& path
[0] ? fopen(path
, "r") : stdin
;
59 snprintf(buf
->path
, sizeof(buf
->path
) - 1, "%s", path
);
63 void in_queue(char *path
)
66 snprintf(files
[nfiles
++], PATHLEN
- 1, "%s", path
? path
: "");
69 static void in_pop(void)
71 struct inbuf
*old
= buf
;
75 if (old
->fin
&& old
->path
[0])
81 static int in_nextfile(void)
83 while (!buf
&& cfile
< nfiles
)
84 in_source(files
[cfile
++]);
88 static int in_read(void)
91 while (buf
|| !in_nextfile()) {
92 if (buf
->nl
-- > 0 && in_last
!= '\n')
94 if (buf
->buf
&& buf
->pos
< buf
->len
)
96 if (!buf
->buf
&& (c
= getc(buf
->fin
)) >= 0)
102 /* replacing \\ with \ only for buffers inserted via in_push() */
103 if (buf
->buf
[buf
->pos
] == '\\' && buf
->buf
[buf
->pos
+ 1] == '\\')
105 return buf
->buf
[buf
->pos
++];
111 if (!buf
&& in_nextfile())
130 struct inbuf
*cur
= buf
;
131 while (cur
&& !cur
->args
)
133 return cur
&& cur
->args
&& cur
->args
[i
- 1] ? cur
->args
[i
- 1] : "";
136 char *in_filename(void)
138 struct inbuf
*cur
= buf
;
139 while (cur
&& !cur
->fin
)
141 return cur
&& cur
->path
[0] ? cur
->path
: "-";
144 static char **args_init(char **args
)
146 char **out
= malloc(NARGS
* sizeof(*out
));
148 for (i
= 0; i
< NARGS
; i
++) {
151 int len
= strlen(args
[i
]) + 1;
152 out
[i
] = malloc(len
);
153 memcpy(out
[i
], args
[i
], len
);
159 static void args_free(char **args
)
162 for (i
= 0; i
< NARGS
; i
++)