1 /* input stream management */
8 char path
[64]; /* for file buffers */
10 char *buf
; /* for string buffers */
12 int unbuf
[32]; /* 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
];
25 static char **args_init(char **args
);
26 static void args_free(char **args
);
28 static void in_new(void)
30 struct inbuf
*next
= xmalloc(sizeof(*next
));
31 memset(next
, 0, sizeof(*next
));
36 void in_push(char *s
, char **args
)
40 buf
->buf
= xmalloc(len
+ 1);
43 buf
->args
= args
? args_init(args
) : NULL
;
46 void in_so(char *path
)
48 FILE *fin
= path
&& path
[0] ? fopen(path
, "r") : stdin
;
54 snprintf(buf
->path
, sizeof(buf
->path
), "%s", path
);
58 void in_lf(char *path
, int lnum
)
60 struct inbuf
*cur
= buf
;
61 while (cur
&& !cur
->fin
)
64 snprintf(cur
->path
, sizeof(cur
->path
), "%s", path
);
68 void in_queue(char *path
)
71 snprintf(files
[nfiles
++], PATHLEN
- 1, "%s", path
? path
: "");
74 static void in_pop(void)
76 struct inbuf
*old
= buf
;
80 if (old
->fin
&& old
->fin
!= stdin
)
86 void in_nx(char *path
)
101 static int in_nextfile(void)
103 while (!buf
&& cfile
< nfiles
)
104 in_so(files
[cfile
++]);
111 while (buf
|| !in_nextfile()) {
113 return buf
->unbuf
[--buf
->un
];
114 if (buf
->buf
&& buf
->pos
< buf
->len
)
116 if (!buf
->buf
&& (c
= getc(buf
->fin
)) >= 0) {
123 return buf
? (unsigned char) buf
->buf
[buf
->pos
++] : -1;
130 if (buf
&& buf
->un
< sizeof(buf
->unbuf
))
131 buf
->unbuf
[buf
->un
++] = c
;
136 return buf
&& buf
->un
? buf
->unbuf
[buf
->un
- 1] : -1;
141 struct inbuf
*cur
= buf
;
142 while (cur
&& !cur
->args
)
144 return cur
&& cur
->args
&& cur
->args
[i
- 1] ? cur
->args
[i
- 1] : "";
149 struct inbuf
*cur
= buf
;
151 while (cur
&& !cur
->args
)
153 while (cur
&& cur
->args
&& cur
->args
[n
])
158 char *in_filename(void)
160 struct inbuf
*cur
= buf
;
161 while (cur
&& !cur
->fin
)
163 return cur
&& cur
->path
[0] ? cur
->path
: "-";
168 struct inbuf
*cur
= buf
;
169 while (cur
&& !cur
->fin
)
171 return cur
? cur
->lnum
: 0;
174 static char **args_init(char **args
)
176 char **out
= xmalloc(NARGS
* sizeof(*out
));
178 for (i
= 0; i
< NARGS
; i
++) {
181 int len
= strlen(args
[i
]) + 1;
182 out
[i
] = xmalloc(len
);
183 memcpy(out
[i
], args
[i
], len
);
189 static void args_free(char **args
)
192 for (i
= 0; i
< NARGS
; i
++)