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 */
20 static struct inbuf
*buf
;
21 static char files
[NFILES
][PATHLEN
];
24 static int in_last
[2] = {'\n'}; /* the last chars returned from in_next() */
26 static char **args_init(char **args
);
27 static void args_free(char **args
);
29 static void in_new(void)
31 struct inbuf
*next
= xmalloc(sizeof(*next
));
32 memset(next
, 0, sizeof(*next
));
37 void in_push(char *s
, char **args
)
41 buf
->buf
= xmalloc(len
+ 1);
44 buf
->args
= args
? args_init(args
) : NULL
;
47 void in_so(char *path
)
49 FILE *fin
= path
&& path
[0] ? fopen(path
, "r") : stdin
;
55 snprintf(buf
->path
, sizeof(buf
->path
), "%s", path
);
59 void in_lf(char *path
, int lnum
)
61 struct inbuf
*cur
= buf
;
62 while (cur
&& !cur
->fin
)
65 snprintf(cur
->path
, sizeof(cur
->path
), "%s", path
);
69 void in_queue(char *path
)
72 snprintf(files
[nfiles
++], PATHLEN
- 1, "%s", path
? path
: "");
75 static void in_pop(void)
77 struct inbuf
*old
= buf
;
81 if (old
->fin
&& old
->fin
!= stdin
)
87 void in_nx(char *path
)
102 static int in_nextfile(void)
104 while (!buf
&& cfile
< nfiles
)
105 in_so(files
[cfile
++]);
109 static int in_read(void)
112 while (buf
|| !in_nextfile()) {
114 return buf
->unbuf
[--buf
->un
];
115 if (buf
->buf
&& buf
->pos
< buf
->len
)
117 if (!buf
->buf
&& (c
= getc(buf
->fin
)) >= 0) {
124 return buf
? (unsigned char) buf
->buf
[buf
->pos
++] : -1;
131 in_last
[1] = in_last
[0];
141 in_last
[0] = in_last
[1];
143 buf
->unbuf
[buf
->un
++] = c
;
148 return buf
&& buf
->un
? buf
->unbuf
[buf
->un
- 1] : -1;
153 struct inbuf
*cur
= buf
;
154 while (cur
&& !cur
->args
)
156 return cur
&& cur
->args
&& cur
->args
[i
- 1] ? cur
->args
[i
- 1] : "";
161 struct inbuf
*cur
= buf
;
163 while (cur
&& !cur
->args
)
165 while (cur
&& cur
->args
&& cur
->args
[n
])
170 char *in_filename(void)
172 struct inbuf
*cur
= buf
;
173 while (cur
&& !cur
->fin
)
175 return cur
&& cur
->path
[0] ? cur
->path
: "-";
180 struct inbuf
*cur
= buf
;
181 while (cur
&& !cur
->fin
)
186 static char **args_init(char **args
)
188 char **out
= xmalloc(NARGS
* sizeof(*out
));
190 for (i
= 0; i
< NARGS
; i
++) {
193 int len
= strlen(args
[i
]) + 1;
194 out
[i
] = xmalloc(len
);
195 memcpy(out
[i
], args
[i
], len
);
201 static void args_free(char **args
)
204 for (i
= 0; i
< NARGS
; i
++)