7 char path
[64]; /* for file buffers */
9 char *buf
; /* for string buffers */
11 int unbuf
[8]; /* unread characters */
12 int un
; /* number of unread characters */
15 int nl
; /* read \n, if the previous char was not */
19 static struct inbuf
*buf
;
20 static char files
[NFILES
][PATHLEN
];
23 static int in_last
[2] = {'\n'}; /* the last chars returned from in_next() */
25 static char **args_init(char **args
);
26 static void args_free(char **args
);
28 static void in_new(void)
30 struct inbuf
*next
= malloc(sizeof(*next
));
31 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_so(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 void in_nx(char *path
)
96 static int in_nextfile(void)
98 while (!buf
&& cfile
< nfiles
)
99 in_so(files
[cfile
++]);
103 static int in_read(void)
106 while (buf
|| !in_nextfile()) {
108 return buf
->unbuf
[--buf
->un
];
109 if (buf
->nl
-- > 0 && in_last
[0] != '\n')
111 if (buf
->buf
&& buf
->pos
< buf
->len
)
113 if (!buf
->buf
&& (c
= getc(buf
->fin
)) >= 0)
117 return buf
? (unsigned char) buf
->buf
[buf
->pos
++] : -1;
124 in_last
[1] = in_last
[0];
134 in_last
[0] = in_last
[1];
136 buf
->unbuf
[buf
->un
++] = c
;
141 return buf
&& buf
->un
? buf
->unbuf
[buf
->un
- 1] : -1;
146 struct inbuf
*cur
= buf
;
147 while (cur
&& !cur
->args
)
149 return cur
&& cur
->args
&& cur
->args
[i
- 1] ? cur
->args
[i
- 1] : "";
154 struct inbuf
*cur
= buf
;
156 while (cur
&& !cur
->args
)
158 while (cur
&& cur
->args
&& cur
->args
[n
])
163 char *in_filename(void)
165 struct inbuf
*cur
= buf
;
166 while (cur
&& !cur
->fin
)
168 return cur
&& cur
->path
[0] ? cur
->path
: "-";
171 static char **args_init(char **args
)
173 char **out
= malloc(NARGS
* sizeof(*out
));
175 for (i
= 0; i
< NARGS
; i
++) {
178 int len
= strlen(args
[i
]) + 1;
179 out
[i
] = malloc(len
);
180 memcpy(out
[i
], args
[i
], len
);
186 static void args_free(char **args
)
189 for (i
= 0; i
< NARGS
; i
++)