6
|
1 #include <stdarg.h>
|
|
2 #include <stdbool.h>
|
|
3 #include <stdio.h>
|
|
4 #include <stdlib.h>
|
|
5 #include <unistd.h>
|
|
6 #include <fcntl.h>
|
|
7 #include <err.h>
|
|
8
|
|
9 #include "array.h"
|
|
10 #include "files.h"
|
|
11
|
|
12 struct place {
|
|
13 struct seenfile *file;
|
|
14 unsigned line;
|
|
15 unsigned column;
|
|
16 };
|
|
17
|
|
18 struct incdir {
|
|
19 const char *name;
|
|
20 bool issystem;
|
|
21 };
|
|
22
|
|
23 struct seenfile {
|
|
24 struct place includedfrom;
|
|
25 char *name;
|
|
26 bool fromsystemdir;
|
|
27 };
|
|
28
|
|
29 DECLARRAY(incdir);
|
|
30 DECLARRAY(seenfile);
|
|
31 DEFARRAY(incdir, );
|
|
32 DEFARRAY(seenfile, );
|
|
33
|
|
34 static struct incdirarray quotepath, bracketpath;
|
|
35 static struct seenfilearray seenfiles;
|
|
36 static bool overall_failure;
|
|
37
|
|
38 ////////////////////////////////////////////////////////////
|
|
39 // management
|
|
40
|
|
41 #define DESTROYALL(T) \
|
|
42 static \
|
|
43 void \
|
|
44 T##array_destroyall(struct T##array *arr) \
|
|
45 { \
|
|
46 unsigned i, num; \
|
|
47 struct T *t; \
|
|
48 \
|
|
49 num = T##array_num(arr); \
|
|
50 for (i=0; i<num; i++) { \
|
|
51 t = T##array_get(arr, i); \
|
|
52 T##_destroy(t); \
|
|
53 } \
|
|
54 T##array_setsize(arr, 0); \
|
|
55 }
|
|
56
|
|
57 static
|
|
58 struct incdir *
|
|
59 incdir_create(const char *name, bool issystem)
|
|
60 {
|
|
61 struct incdir *id;
|
|
62
|
|
63 id = domalloc(sizeof(*id));
|
|
64 id->name = name;
|
|
65 id->issystem = issystem;
|
|
66 return id;
|
|
67 }
|
|
68
|
|
69 static
|
|
70 void
|
|
71 incdir_destroy(struct incdir *id)
|
|
72 {
|
|
73 free(id);
|
|
74 }
|
|
75
|
|
76 static
|
|
77 struct seenfile *
|
|
78 seenfile_create(const struct place *from, char *name, bool fromsystemdir)
|
|
79 {
|
|
80 struct seenfile *sf;
|
|
81
|
|
82 sf = domalloc(sizeof(*sf));
|
|
83 sf->includedfrom = *from;
|
|
84 sf->name = name;
|
|
85 sf->fromsystemdir = fromsystemdir;
|
|
86 return sf;
|
|
87 }
|
|
88
|
|
89 static
|
|
90 void
|
|
91 seenfile_destroy(struct seenfile *sf)
|
|
92 {
|
|
93 free(sf->name);
|
|
94 free(sf);
|
|
95 }
|
|
96
|
|
97 void
|
|
98 files_init(void)
|
|
99 {
|
|
100 incdirarray_init("epath);
|
|
101 incdirarray_init(&bracketpath);
|
|
102 }
|
|
103
|
|
104 DESTROYALL(incdir);
|
|
105 DESTROYALL(seenfile);
|
|
106
|
|
107 void
|
|
108 files_cleanup(void)
|
|
109 {
|
|
110 seenfilearray_destroyall(&seenfiles);
|
|
111 seenfilearray_cleanup(&seenfiles);
|
|
112
|
|
113 incdirarray_destroyall("epath);
|
|
114 incdirarray_cleanup("epath);
|
|
115 incdirarray_destroyall(&bracketpath);
|
|
116 incdirarray_cleanup(&bracketpath);
|
|
117 }
|
|
118
|
|
119 ////////////////////////////////////////////////////////////
|
|
120 // path setup
|
|
121
|
|
122 void
|
|
123 files_addquotepath(const char *dir, bool issystem)
|
|
124 {
|
|
125 struct incdir *id;
|
|
126
|
|
127 id = incdir_create(dir, issystem);
|
|
128 incdirarray_add("epath, id, NULL);
|
|
129 }
|
|
130
|
|
131 void
|
|
132 files_addbracketpath(const char *dir, bool issystem)
|
|
133 {
|
|
134 struct incdir *id;
|
|
135
|
|
136 id = incdir_create(dir, issystem);
|
|
137 incdirarray_add(&bracketpath, id, NULL);
|
|
138 }
|
|
139
|
|
140 ////////////////////////////////////////////////////////////
|
|
141 // places and complaints
|
|
142
|
|
143 #define NOWHERE_LINE 0
|
|
144 #define BUILTIN_LINE 1
|
|
145 #define COMMANDLINE_LINE 2
|
|
146
|
|
147 static struct place scratchplace;
|
|
148 static bool scratchplace_inuse;
|
|
149
|
|
150 static
|
|
151 bool
|
|
152 place_isnowhere(const struct place *p)
|
|
153 {
|
|
154 return p->file == NULL && p->line == NOWHERE_LINE;
|
|
155 }
|
|
156
|
|
157 static
|
|
158 bool
|
|
159 place_isbuiltin(const struct place *p)
|
|
160 {
|
|
161 return p->file == NULL && p->line == BUILTIN_LINE;
|
|
162 }
|
|
163
|
|
164 static
|
|
165 bool
|
|
166 place_iscommandline(const struct place *p)
|
|
167 {
|
|
168 return p->file == NULL && p->line == COMMANDLINE_LINE;
|
|
169 }
|
|
170
|
|
171 struct place *
|
|
172 place_gettemporary(void)
|
|
173 {
|
|
174 assert(!scratchplace_inuse);
|
|
175 scratchplace_inuse = true;
|
|
176 return &scratchplace;
|
|
177 }
|
|
178
|
|
179 void
|
|
180 place_puttemporary(struct place *p)
|
|
181 {
|
|
182 assert(scratchplace_inuse);
|
|
183 assert(p == &scratchplace);
|
|
184 scratchplace_inuse = false;
|
|
185 }
|
|
186
|
7
|
187 struct place *
|
|
188 place_create(void)
|
|
189 {
|
|
190 struct place *p;
|
|
191
|
|
192 p = domalloc(sizeof(*p));
|
|
193 place_setnowhere(p);
|
|
194 return p;
|
|
195 }
|
|
196
|
|
197 struct place *
|
|
198 place_clone(const struct place *op)
|
|
199 {
|
|
200 struct place *p;
|
|
201
|
|
202 p = domalloc(sizeof(*p));
|
|
203 *p = *op;
|
|
204 return p;
|
|
205 }
|
|
206
|
|
207 void
|
|
208 place_destroy(struct place *p)
|
|
209 {
|
|
210 free(p);
|
|
211 }
|
|
212
|
6
|
213 void
|
|
214 place_setnowhere(struct place *p)
|
|
215 {
|
|
216 p->file = NULL;
|
|
217 p->line = NOWHERE_LINE;
|
|
218 p->column = 0;
|
|
219 }
|
|
220
|
|
221 void
|
|
222 place_setbuiltin(struct place *p, unsigned num)
|
|
223 {
|
|
224 p->file = NULL;
|
|
225 p->line = BUILTIN_LINE;
|
|
226 p->column = num;
|
|
227 }
|
|
228
|
|
229 void
|
|
230 place_setcommandline(struct place *p, unsigned column)
|
|
231 {
|
|
232 p->file = NULL;
|
|
233 p->line = COMMANDLINE_LINE;
|
|
234 p->column = column;
|
|
235 }
|
|
236
|
|
237 static
|
|
238 void
|
|
239 place_print(const struct place *p)
|
|
240 {
|
|
241 if (place_iscommandline(p)) {
|
|
242 fprintf(stderr, "<command-line>:1:%u", p->column);
|
|
243 } else if (place_isbuiltin(p)) {
|
|
244 fprintf(stderr, "<built-in>:%u:1", p->column);
|
|
245 } else {
|
|
246 fprintf(stderr, "%s:%u:%u", p->file->name, p->line, p->column);
|
|
247 }
|
|
248 }
|
|
249
|
|
250 static
|
|
251 void
|
|
252 place_printfrom(const struct place *p)
|
|
253 {
|
|
254 if (!place_isnowhere(&p->file->includedfrom)) {
|
|
255 place_printfrom(&p->file->includedfrom);
|
|
256 }
|
|
257 fprintf(stderr, "In file included from ");
|
|
258 fprintf(stderr, ":\n");
|
|
259 }
|
|
260
|
|
261 void
|
|
262 complain(const struct place *p, const char *fmt, ...)
|
|
263 {
|
|
264 va_list ap;
|
|
265
|
|
266 if (!place_isnowhere(&p->file->includedfrom)) {
|
|
267 place_printfrom(&p->file->includedfrom);
|
|
268 }
|
|
269 place_print(p);
|
|
270 fprintf(stderr, ": ");
|
|
271 va_start(ap, fmt);
|
|
272 vfprintf(stderr, fmt, ap);
|
|
273 va_end(ap);
|
|
274 fprintf(stderr, "\n");
|
|
275 }
|
|
276
|
|
277 void
|
|
278 complain_fail(void)
|
|
279 {
|
|
280 overall_failure = true;
|
|
281 }
|
|
282
|
|
283 bool
|
|
284 complain_failed(void)
|
|
285 {
|
|
286 return overall_failure;
|
|
287 }
|
|
288
|
|
289 ////////////////////////////////////////////////////////////
|
|
290 // parsing
|
|
291
|
|
292 void
|
|
293 file_read(struct seenfile *sf, int fd);
|
|
294
|
|
295 ////////////////////////////////////////////////////////////
|
|
296 // path search
|
|
297
|
|
298 static
|
|
299 int
|
|
300 file_tryopen(const char *file)
|
|
301 {
|
|
302 int fd;
|
|
303
|
|
304 fd = open(file, O_RDONLY);
|
|
305 if (fd < 0) {
|
|
306 return -1;
|
|
307 }
|
|
308 /* XXX: do we need to do anything here or is this function pointless?*/
|
|
309 return fd;
|
|
310 }
|
|
311
|
|
312 static
|
|
313 void
|
|
314 file_search(struct place *place, struct incdirarray *path, const char *name)
|
|
315 {
|
|
316 unsigned i, num;
|
|
317 struct incdir *id;
|
|
318 struct seenfile *sf;
|
|
319 char *file;
|
|
320 int fd;
|
|
321
|
|
322 assert(place != NULL);
|
|
323
|
|
324 num = incdirarray_num(path);
|
|
325 for (i=0; i<num; i++) {
|
|
326 id = incdirarray_get(path, i);
|
|
327 file = dostrdup3(id->name, "/", name);
|
|
328 fd = file_tryopen(file);
|
|
329 if (fd >= 0) {
|
|
330 sf = seenfile_create(place, file, id->issystem);
|
|
331 seenfilearray_add(&seenfiles, sf, NULL);
|
|
332 file_read(sf, fd);
|
|
333 close(fd);
|
|
334 return;
|
|
335 }
|
|
336 free(file);
|
|
337 }
|
|
338 complain(place, "Include file %s not found", name);
|
|
339 complain_fail();
|
|
340 }
|
|
341
|
|
342 void
|
|
343 file_readquote(struct place *place, const char *name)
|
|
344 {
|
|
345 file_search(place, "epath, name);
|
|
346 }
|
|
347
|
|
348 void
|
|
349 file_readbracket(struct place *place, const char *name)
|
|
350 {
|
|
351 file_search(place, &bracketpath, name);
|
|
352 }
|
|
353
|
|
354 void
|
|
355 file_readabsolute(struct place *place, const char *name)
|
|
356 {
|
|
357 struct seenfile *sf;
|
|
358 int fd;
|
|
359
|
|
360 assert(place != NULL);
|
|
361
|
|
362 fd = file_tryopen(name);
|
|
363 if (fd < 0) {
|
|
364 warn("%s", name);
|
|
365 die();
|
|
366 }
|
|
367 sf = seenfile_create(place, dostrdup(name), false);
|
|
368 seenfilearray_add(&seenfiles, sf, NULL);
|
|
369 file_read(sf, fd);
|
|
370 close(fd);
|
|
371 }
|