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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/*-
* Copyright (c) 2010, 2013 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by David A. Holland.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include "array.h"
#include "mode.h"
#include "place.h"
#include "files.h"
#include "directive.h"
struct incdir {
const char *name;
bool issystem;
};
DECLARRAY(incdir, static UNUSED);
DEFARRAY(incdir, static);
static struct incdirarray quotepath, bracketpath;
////////////////////////////////////////////////////////////
// management
static
struct incdir *
incdir_create(const char *name, bool issystem)
{
struct incdir *id;
id = domalloc(sizeof(*id));
id->name = name;
id->issystem = issystem;
return id;
}
static
void
incdir_destroy(struct incdir *id)
{
dofree(id, sizeof(*id));
}
void
files_init(void)
{
incdirarray_init("epath);
incdirarray_init(&bracketpath);
}
DESTROYALL_ARRAY(incdir, );
void
files_cleanup(void)
{
incdirarray_destroyall("epath);
incdirarray_cleanup("epath);
incdirarray_destroyall(&bracketpath);
incdirarray_cleanup(&bracketpath);
}
////////////////////////////////////////////////////////////
// path setup
void
files_addquotepath(const char *dir, bool issystem)
{
struct incdir *id;
id = incdir_create(dir, issystem);
incdirarray_add("epath, id, NULL);
}
void
files_addbracketpath(const char *dir, bool issystem)
{
struct incdir *id;
id = incdir_create(dir, issystem);
incdirarray_add(&bracketpath, id, NULL);
}
////////////////////////////////////////////////////////////
// parsing
/*
* Find the end of the logical line. End of line characters that are
* commented out do not count.
*/
static
size_t
findeol(const char *buf, size_t start, size_t limit)
{
size_t i;
int incomment = 0;
bool inquote = false;
char quote = '\0';
for (i=start; i<limit; i++) {
if (incomment) {
if (i+1 < limit && buf[i] == '*' && buf[i+1] == '/') {
i++;
incomment = 0;
}
} else if (!inquote && i+1 < limit &&
buf[i] == '/' && buf[i+1] == '*') {
i++;
incomment = 1;
} else if (i+1 < limit &&
buf[i] == '\\' && buf[i+1] != '\n') {
i++;
} else if (!inquote && (buf[i] == '"' || buf[i] == '\'')) {
inquote = true;
quote = buf[i];
} else if (inquote && buf[i] == quote) {
inquote = false;
} else if (buf[i] == '\n') {
return i;
}
}
return limit;
}
static
unsigned
countnls(const char *buf, size_t start, size_t limit)
{
size_t i;
unsigned count = 0;
for (i=start; i<limit; i++) {
if (buf[i] == '\n') {
count++;
}
}
return count;
}
static
void
file_read(const struct placefile *pf, int fd, const char *name, bool toplevel)
{
struct place linestartplace, nextlinestartplace, ptmp;
size_t bufend, bufmax, linestart, lineend, nextlinestart, tmp;
ssize_t result;
bool ateof = false;
char *buf;
place_setfilestart(&linestartplace, pf);
nextlinestartplace = linestartplace;
bufmax = 128;
bufend = 0;
linestart = 0;
lineend = 0;
buf = domalloc(bufmax);
while (1) {
if (lineend >= bufend) {
/* do not have a whole line in the buffer; read more */
assert(bufend >= linestart);
if (linestart > 0 && bufend > linestart) {
/* slide to beginning of buffer */
memmove(buf, buf+linestart, bufend-linestart);
bufend -= linestart;
lineend -= linestart;
linestart = 0;
}
if (bufend >= bufmax) {
/* need bigger buffer */
buf = dorealloc(buf, bufmax, bufmax*2);
bufmax = bufmax*2;
}
if (ateof) {
/* don't read again, in case it's a socket */
result = 0;
} else {
result = read(fd, buf+bufend, bufmax - bufend);
}
if (result == -1) {
/* read error */
complain(NULL, "%s: %s",
name, strerror(errno));
complain_fail();
} else if (result == 0 && bufend == linestart) {
/* eof */
ateof = true;
break;
} else if (result == 0) {
/* eof in middle of line */
ateof = true;
ptmp = linestartplace;
ptmp.column += bufend - linestart;
complain(&ptmp, "No newline at end of file");
if (mode.werror) {
complain_fail();
}
assert(bufend < bufmax);
lineend = bufend++;
buf[lineend] = '\n';
} else {
bufend += (size_t)result;
lineend = findeol(buf, linestart, bufend);
}
/* loop in case we still don't have a whole line */
continue;
}
/* have a line */
assert(buf[lineend] == '\n');
buf[lineend] = '\0';
nextlinestart = lineend+1;
nextlinestartplace.line++;
/* check for CR/NL */
if (lineend > 0 && buf[lineend-1] == '\r') {
buf[lineend-1] = '\0';
lineend--;
}
/* check for continuation line */
if (lineend > 0 && buf[lineend-1]=='\\') {
lineend--;
tmp = nextlinestart - lineend;
if (bufend > nextlinestart) {
memmove(buf+lineend, buf+nextlinestart,
bufend - nextlinestart);
}
bufend -= tmp;
nextlinestart -= tmp;
lineend = findeol(buf, linestart, bufend);
/* might not have a whole line, so loop */
continue;
}
/* line now goes from linestart to lineend */
assert(buf[lineend] == '\0');
/* count how many commented-out newlines we swallowed */
nextlinestartplace.line += countnls(buf, linestart, lineend);
/* if the line isn't empty, process it */
if (lineend > linestart) {
directive_gotline(&linestartplace,
buf+linestart, lineend-linestart);
}
linestart = nextlinestart;
lineend = findeol(buf, linestart, bufend);
linestartplace = nextlinestartplace;
}
if (toplevel) {
directive_goteof(&linestartplace);
}
dofree(buf, bufmax);
}
////////////////////////////////////////////////////////////
// path search
static
char *
mkfilename(struct place *place, const char *dir, const char *file)
{
size_t dlen, flen, rlen;
char *ret;
bool needslash = false;
if (dir == NULL) {
dir = place_getparsedir(place);
}
dlen = strlen(dir);
flen = strlen(file);
if (dlen > 0 && dir[dlen-1] != '/') {
needslash = true;
}
rlen = dlen + (needslash ? 1 : 0) + flen;
ret = domalloc(rlen + 1);
snprintf(ret, rlen+1, "%s%s%s", dir, needslash ? "/" : "", file);
return ret;
}
static
int
file_tryopen(const char *file)
{
int fd;
/* XXX check for non-regular files */
fd = open(file, O_RDONLY);
if (fd < 0) {
if (errno != ENOENT && errno != ENOTDIR) {
complain(NULL, "%s: %s", file, strerror(errno));
}
return -1;
}
return fd;
}
static
void
file_search(struct place *place, struct incdirarray *path, const char *name)
{
unsigned i, num;
struct incdir *id;
const struct placefile *pf;
char *file;
int fd;
assert(place != NULL);
if (name[0] == '/') {
fd = file_tryopen(name);
if (fd >= 0) {
pf = place_addfile(place, name, true);
file_read(pf, fd, name, false);
close(fd);
return;
}
} else {
num = incdirarray_num(path);
for (i=0; i<num; i++) {
id = incdirarray_get(path, i);
file = mkfilename(place, id->name, name);
fd = file_tryopen(file);
if (fd >= 0) {
pf = place_addfile(place, file, id->issystem);
file_read(pf, fd, file, false);
dostrfree(file);
close(fd);
return;
}
dostrfree(file);
}
}
complain(place, "Include file %s not found", name);
complain_fail();
}
void
file_readquote(struct place *place, const char *name)
{
file_search(place, "epath, name);
}
void
file_readbracket(struct place *place, const char *name)
{
file_search(place, &bracketpath, name);
}
void
file_readabsolute(struct place *place, const char *name)
{
const struct placefile *pf;
int fd;
assert(place != NULL);
if ((name == NULL) || !strcmp(name, "-")) {
fd = STDIN_FILENO;
pf = place_addfile(place, "<standard-input>", false);
} else {
fd = file_tryopen(name);
if (fd < 0) {
complain(NULL, "%s: %s", name, strerror(errno));
die();
}
pf = place_addfile(place, name, false);
}
file_read(pf, fd, name, true);
if (name != NULL) {
close(fd);
}
}
|