1 /* input stream management */
8 char path
[64]; /* for file buffers */
10 char *buf
; /* for string buffers */
12 int unbuf
[8]; /* unread characters */
13 int un
; /* number of unread characters */
16 int lnum
; /* file line number */
17 int nl
; /* read \n, if the previous char was not */
21 static struct inbuf
*buf
;
22 static char files
[NFILES
][PATHLEN
];
25 static int in_last
[2] = {'\n'}; /* the last chars returned from in_next() */
27 static char **args_init(char **args
);
28 static void args_free(char **args
);
30 static void in_new(void)
32 struct inbuf
*next
= malloc(sizeof(*next
));
33 memset(next
, 0, sizeof(*next
));
38 void in_push(char *s
, char **args
)
42 buf
->buf
= malloc(len
+ 1);
45 buf
->args
= args
? args_init(args
) : NULL
;
48 void in_pushnl(char *s
, char **args
)
54 void in_so(char *path
)
56 FILE *fin
= path
&& path
[0] ? fopen(path
, "r") : stdin
;
62 snprintf(buf
->path
, sizeof(buf
->path
), "%s", path
);
66 void in_lf(char *path
, int lnum
)
68 struct inbuf
*cur
= buf
;
69 while (cur
&& !cur
->fin
)
72 snprintf(cur
->path
, sizeof(cur
->path
), "%s", path
);
76 void in_queue(char *path
)
79 snprintf(files
[nfiles
++], PATHLEN
- 1, "%s", path
? path
: "");
82 static void in_pop(void)
84 struct inbuf
*old
= buf
;
88 if (old
->fin
&& old
->fin
!= stdin
)
94 void in_nx(char *path
)
109 static int in_nextfile(void)
111 while (!buf
&& cfile
< nfiles
)
112 in_so(files
[cfile
++]);
116 static int in_read(void)
119 while (buf
|| !in_nextfile()) {
121 return buf
->unbuf
[--buf
->un
];
122 if (buf
->nl
-- > 0 && in_last
[0] != '\n')
124 if (buf
->buf
&& buf
->pos
< buf
->len
)
126 if (!buf
->buf
&& (c
= getc(buf
->fin
)) >= 0) {
133 return buf
? (unsigned char) buf
->buf
[buf
->pos
++] : -1;
140 in_last
[1] = in_last
[0];
150 in_last
[0] = in_last
[1];
152 buf
->unbuf
[buf
->un
++] = c
;
157 return buf
&& buf
->un
? buf
->unbuf
[buf
->un
- 1] : -1;
162 struct inbuf
*cur
= buf
;
163 while (cur
&& !cur
->args
)
165 return cur
&& cur
->args
&& cur
->args
[i
- 1] ? cur
->args
[i
- 1] : "";
170 struct inbuf
*cur
= buf
;
172 while (cur
&& !cur
->args
)
174 while (cur
&& cur
->args
&& cur
->args
[n
])
179 char *in_filename(void)
181 struct inbuf
*cur
= buf
;
182 while (cur
&& !cur
->fin
)
184 return cur
&& cur
->path
[0] ? cur
->path
: "-";
189 struct inbuf
*cur
= buf
;
190 while (cur
&& !cur
->fin
)
195 static char **args_init(char **args
)
197 char **out
= malloc(NARGS
* sizeof(*out
));
199 for (i
= 0; i
< NARGS
; i
++) {
202 int len
= strlen(args
[i
]) + 1;
203 out
[i
] = malloc(len
);
204 memcpy(out
[i
], args
[i
], len
);
210 static void args_free(char **args
)
213 for (i
= 0; i
< NARGS
; i
++)