-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.c
325 lines (293 loc) · 6.98 KB
/
io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* this i/o machinery is a unification of macro expansion, include files
* and source file buffering. these are stacked, so we can push
* include files and save our position at each level
*
* we don't do any character processing at all at this level.
* if there are nasty control characters, etc, we pass them up.
* not our job; except nulls. those are dirty; the first null is eof.
*/
#include "ccc.h"
// #include <fcntl.h>
/*
* the incoming character stream interface
* a zero is EOF
*/
char curchar; // the current character
char nextchar; // the next char - can change if macro
int lineno; // line number for error messages
char *filename; // current file name
int column; // this is reset to 0 when we see a newline
int nextcol = 0;
char namebuf[128];
/*
* the formal definition of offset is the first unread character.
* this is effectively the lookahead character, nextchar.
* if we have not read anything from this buffer yet, it is zero.
* advance() places this character into curchar.
* if we do a macro insertion, it is after curchar
*/
#define TBSIZE 1024 /* text buffer size */
struct textbuf {
int fd; // if == -1, macro buffer
char *name; // filename or macro name
char *storage; // data - free when done
short offset; // always points at nextchar.
short valid; // total valid in buffer
short lineno; // current line # in file
struct textbuf *prev; // a stack
} *tbtop;
#ifdef DEBUG
void
cdump(char *tag)
{
struct textbuf *t = tbtop;
char cs[20];
char ns[20];
char tbuf[100];
if (!(VERBOSE(V_IO))) {
return;
}
if (tag) {
printf("%s:\n", tag);
}
if (curchar <= ' ') {
sprintf(cs, "0x%x", curchar);
} else {
sprintf(cs, "%c", curchar);
}
if (nextchar <= ' ') {
sprintf(ns, "0x%x", nextchar);
} else {
sprintf(ns, "%c", nextchar);
}
if (t) {
sprintf(tbuf, "%s %x %x %s %s",
tag, tbtop->offset, tbtop->valid, cs, ns);
hexdump(tbuf, t->storage, t->valid);
}
}
#else
#define cdump(x)
#endif
struct include {
char *path;
struct include *next;
} *includes;
/*
* add a path to the include search list
*/
void
add_include(char *s)
{
struct include *i, *ip;
i = malloc(sizeof(*i));
i->path = strdup(s);
i->next = 0;
if (includes) {
ip = includes;
while (ip->next) {
ip = ip->next;
}
ip->next = i;
} else {
includes = i;
}
if (VERBOSE(V_CPP)) {
printf("add_include: %s\n", s);
}
}
/*
* if sys is true, then file was included using <> filename delimiters
*/
void
insertfile(char *name, int sys)
{
struct textbuf *t;
struct include *i;
#ifdef DEBUG
if (VERBOSE(V_IO)) {
printf("insertfile: %s\n", name);
}
#endif
if (!includes) {
add_include(".");
}
t = malloc(sizeof(*t));
/*
* try the filename in all the include path entries. first hit wins
*/
for (i = includes; i; i = i->next) {
strcpy(namebuf, i->path);
strcat(namebuf, "/");
strcat(namebuf, name);
t->fd = open(namebuf, 0);
if (t->fd > 0) {
break;
}
}
if (t->fd == -1) {
perror(namebuf);
free(t);
return;
}
t->name = strdup(namebuf);
t->lineno = t->offset = t->valid = 0;
t->storage = malloc(TBSIZE);
t->prev = tbtop;
tbtop = t;
filename = name;
}
/*
* when we encounter a macro invocation FOO(x,y) in the input stream,
* we replace it with the definition of FOO with parameter substitution
* effectively pushing this text into the stream. if that expansion in
* turn has macro invocations, that causes another push.
* there's an easy and effective optimization that checks to see if a macro
* will fit in the portion of the buffer that we have read already. if
* so, we copy the macro before the current cursor and back up to the
* start of the macro expansion.
* important: we push BETWEEN curchar and nextchar.
*/
void
insertmacro(char *name, char *macbuf)
{
struct textbuf *t;
int l;
l = strlen(macbuf); // our macro without the terminating null
if (VERBOSE(V_MACRO)) {
printf("insert macro %s %d $%s$\n", name, l, macbuf);
}
t = tbtop;
/* does it fit */
if (t->offset > l) {
cdump("before");
t->offset -= l;
strncpy(&t->storage[t->offset], macbuf, l);
curchar = t->storage[t->offset++];
nextchar = t->storage[t->offset];
cdump("after");
return;
}
/* if it does not */
t = malloc(sizeof(*t));
t->fd = -1;
t->name = strdup(name);
t->lineno = lineno;
t->offset = 0;
t->storage = strdup(macbuf);
t->valid = strlen(t->storage);
t->prev = tbtop;
tbtop = t;
filename = name;
}
void
tbdump(struct textbuf *t)
{
printf("textbuf: %s fd: %d offset: %d valid: %d lineno %d\n",
t->name, t->fd, t->offset, t->valid, t->lineno);
}
void
dump()
{
struct textbuf *t = tbtop;
while (t) {
tbdump(t);
t = t->prev;
}
}
/*
* read the next character into curchar
* side effects: updating line and column
*/
void
advance()
{
struct textbuf *t = tbtop;
curchar = nextchar;
/* if no textbuf, are at eof */
if (!t) {
nextchar = 0;
goto done;
}
/* do we have a valid nextchar? */
if (t->offset < t->valid) {
nextchar = t->storage[++t->offset];
goto done;
}
/* if we have a file open, read some more of it */
if (t->fd != -1) {
t->valid = read(t->fd, t->storage, TBSIZE);
t->offset = 0;
if (t->valid > 0) { // read worked
nextchar = t->storage[0];
goto done;
}
close(t->fd);
}
/* closed file or empty macro buffer - pop */
tbtop = t->prev;
free(t->storage);
free(t->name);
free(t);
if (tbtop) {
lineno = tbtop->lineno;
filename = tbtop->name;
}
done:
column = nextcol;
if (curchar == 0) {
nextcol = 0;
} else if (curchar == '\n') {
nextcol = 0;
lineno++;
} else {
nextcol++;
}
if (nextchar == '\t') nextchar = ' ';
cdump("advance");
}
/*
* prime the pump
*/
void
ioinit()
{
lineno = 1;
advance();
advance();
column = 0;
}
struct textbuf *cpp;
#define CPP_BUF 256
void
cpp_flush()
{
if (cpp->offset) {
write(cpp_file, cpp->storage, cpp->offset);
}
cpp->offset = 0;
}
void
cpp_out(char *s, int len)
{
if (!cpp) {
cpp = malloc(sizeof(*cpp));
cpp->storage = malloc(CPP_BUF);
cpp->fd = cpp_file;
cpp->name = cpp_file_name;
cpp->offset = 0;
cpp->valid = 0;
cpp->prev = 0;
}
if (!s)
return;
if ((cpp->offset + len) > CPP_BUF) {
cpp_flush();
}
bcopy(s, &cpp->storage[cpp->offset], len);
cpp->offset += len;
}
/*
* vim: tabstop=4 shiftwidth=4 expandtab:
*/