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 lnum
; /* file line number */
16 int nl
; /* read \n, if the previous char was not */
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
= malloc(sizeof(*next
));
32 memset(next
, 0, sizeof(*next
));
37 void in_push(char *s
, char **args
)
41 buf
->buf
= malloc(len
+ 1);
44 buf
->args
= args
? args_init(args
) : NULL
;
47 void in_pushnl(char *s
, char **args
)
53 void in_so(char *path
)
55 FILE *fin
= path
&& path
[0] ? fopen(path
, "r") : stdin
;
61 snprintf(buf
->path
, sizeof(buf
->path
) - 1, "%s", path
);
65 void in_lf(char *path
, int lnum
)
67 struct inbuf
*cur
= buf
;
68 while (cur
&& !cur
->fin
)
71 strcpy(cur
->path
, path
);
75 void in_queue(char *path
)
78 snprintf(files
[nfiles
++], PATHLEN
- 1, "%s", path
? path
: "");
81 static void in_pop(void)
83 struct inbuf
*old
= buf
;
87 if (old
->fin
&& old
->path
[0])
93 void in_nx(char *path
)
108 static int in_nextfile(void)
110 while (!buf
&& cfile
< nfiles
)
111 in_so(files
[cfile
++]);
115 static int in_read(void)
118 while (buf
|| !in_nextfile()) {
120 return buf
->unbuf
[--buf
->un
];
121 if (buf
->nl
-- > 0 && in_last
[0] != '\n')
123 if (buf
->buf
&& buf
->pos
< buf
->len
)
125 if (!buf
->buf
&& (c
= getc(buf
->fin
)) >= 0) {
132 return buf
? (unsigned char) buf
->buf
[buf
->pos
++] : -1;
139 in_last
[1] = in_last
[0];
149 in_last
[0] = in_last
[1];
151 buf
->unbuf
[buf
->un
++] = c
;
156 return buf
&& buf
->un
? buf
->unbuf
[buf
->un
- 1] : -1;
161 struct inbuf
*cur
= buf
;
162 while (cur
&& !cur
->args
)
164 return cur
&& cur
->args
&& cur
->args
[i
- 1] ? cur
->args
[i
- 1] : "";
169 struct inbuf
*cur
= buf
;
171 while (cur
&& !cur
->args
)
173 while (cur
&& cur
->args
&& cur
->args
[n
])
178 char *in_filename(void)
180 struct inbuf
*cur
= buf
;
181 while (cur
&& !cur
->fin
)
183 return cur
&& cur
->path
[0] ? cur
->path
: "-";
188 struct inbuf
*cur
= buf
;
189 while (cur
&& !cur
->fin
)
194 static char **args_init(char **args
)
196 char **out
= malloc(NARGS
* sizeof(*out
));
198 for (i
= 0; i
< NARGS
; i
++) {
201 int len
= strlen(args
[i
]) + 1;
202 out
[i
] = malloc(len
);
203 memcpy(out
[i
], args
[i
], len
);
209 static void args_free(char **args
)
212 for (i
= 0; i
< NARGS
; i
++)