Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate files.c @ 112:2b0b61fd1a36
Fix handling of relative includes.
author | David A. Holland |
---|---|
date | Tue, 11 Jun 2013 13:50:07 -0400 |
parents | 33954a07d013 |
children | 1e7144176a42 |
rev | line source |
---|---|
30 | 1 /*- |
99
60184aa42604
add 2013 to copyrights where it seems warranted
David A. Holland
parents:
81
diff
changeset
|
2 * Copyright (c) 2010, 2013 The NetBSD Foundation, Inc. |
30 | 3 * All rights reserved. |
4 * | |
5 * This code is derived from software contributed to The NetBSD Foundation | |
6 * by David A. Holland. | |
7 * | |
8 * Redistribution and use in source and binary forms, with or without | |
9 * modification, are permitted provided that the following conditions | |
10 * are met: | |
11 * 1. Redistributions of source code must retain the above copyright | |
12 * notice, this list of conditions and the following disclaimer. | |
13 * 2. Redistributions in binary form must reproduce the above copyright | |
14 * notice, this list of conditions and the following disclaimer in the | |
15 * documentation and/or other materials provided with the distribution. | |
16 * | |
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | |
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
27 * POSSIBILITY OF SUCH DAMAGE. | |
28 */ | |
29 | |
6 | 30 #include <stdbool.h> |
31 #include <stdio.h> | |
32 #include <stdlib.h> | |
13 | 33 #include <string.h> |
6 | 34 #include <unistd.h> |
35 #include <fcntl.h> | |
36 #include <err.h> | |
37 | |
38 #include "array.h" | |
15 | 39 #include "mode.h" |
8 | 40 #include "place.h" |
6 | 41 #include "files.h" |
15 | 42 #include "directive.h" |
6 | 43 |
44 struct incdir { | |
45 const char *name; | |
46 bool issystem; | |
47 }; | |
48 | |
107 | 49 DECLARRAY(incdir, static UNUSED); |
47
2e25e55dba6b
Fix inline usage as per the version in dholland-make2.
David A. Holland
parents:
39
diff
changeset
|
50 DEFARRAY(incdir, static); |
6 | 51 |
52 static struct incdirarray quotepath, bracketpath; | |
53 | |
54 //////////////////////////////////////////////////////////// | |
55 // management | |
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 { | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
73 dofree(id, sizeof(*id)); |
6 | 74 } |
75 | |
76 void | |
77 files_init(void) | |
78 { | |
79 incdirarray_init("epath); | |
80 incdirarray_init(&bracketpath); | |
81 } | |
82 | |
9 | 83 DESTROYALL_ARRAY(incdir, ); |
6 | 84 |
85 void | |
86 files_cleanup(void) | |
87 { | |
88 incdirarray_destroyall("epath); | |
89 incdirarray_cleanup("epath); | |
90 incdirarray_destroyall(&bracketpath); | |
91 incdirarray_cleanup(&bracketpath); | |
92 } | |
93 | |
94 //////////////////////////////////////////////////////////// | |
95 // path setup | |
96 | |
97 void | |
98 files_addquotepath(const char *dir, bool issystem) | |
99 { | |
100 struct incdir *id; | |
101 | |
102 id = incdir_create(dir, issystem); | |
103 incdirarray_add("epath, id, NULL); | |
104 } | |
105 | |
106 void | |
107 files_addbracketpath(const char *dir, bool issystem) | |
108 { | |
109 struct incdir *id; | |
110 | |
111 id = incdir_create(dir, issystem); | |
112 incdirarray_add(&bracketpath, id, NULL); | |
113 } | |
114 | |
115 //////////////////////////////////////////////////////////// | |
116 // parsing | |
117 | |
75 | 118 /* |
119 * Find the end of the logical line. End of line characters that are | |
120 * commented out do not count. | |
121 */ | |
15 | 122 static |
123 size_t | |
75 | 124 findeol(const char *buf, size_t start, size_t limit) |
15 | 125 { |
126 size_t i; | |
75 | 127 int incomment = 0; |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
128 bool inquote = false; |
75 | 129 |
130 for (i=start; i<limit; i++) { | |
131 if (incomment) { | |
132 if (i+1 < limit && buf[i] == '*' && buf[i+1] == '/') { | |
133 i++; | |
134 incomment = 0; | |
135 } | |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
136 } else if (!inquote && i+1 < limit && |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
137 buf[i] == '/' && buf[i+1] == '*') { |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
138 i++; |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
139 incomment = 1; |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
140 } else if (i+1 < limit && |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
141 buf[i] == '\\' && buf[i+1] == '"') { |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
142 i++; |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
143 } else if (buf[i] == '"') { |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
144 inquote = !inquote; |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
145 } else if (buf[i] == '\n') { |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
146 return i; |
75 | 147 } |
148 } | |
149 return limit; | |
150 } | |
151 | |
152 static | |
153 unsigned | |
154 countnls(const char *buf, size_t start, size_t limit) | |
155 { | |
156 size_t i; | |
157 unsigned count = 0; | |
15 | 158 |
159 for (i=start; i<limit; i++) { | |
160 if (buf[i] == '\n') { | |
75 | 161 count++; |
15 | 162 } |
163 } | |
75 | 164 return count; |
15 | 165 } |
166 | |
167 static | |
6 | 168 void |
28 | 169 file_read(const struct placefile *pf, int fd, const char *name, bool toplevel) |
15 | 170 { |
171 struct place linestartplace, nextlinestartplace, ptmp; | |
172 size_t bufend, bufmax, linestart, lineend, nextlinestart, tmp; | |
173 ssize_t result; | |
174 bool ateof = false; | |
175 char *buf; | |
176 | |
177 place_setfilestart(&linestartplace, pf); | |
178 nextlinestartplace = linestartplace; | |
179 | |
180 bufmax = 128; | |
181 bufend = 0; | |
182 linestart = 0; | |
183 lineend = 0; | |
184 buf = domalloc(bufmax); | |
185 | |
186 while (1) { | |
187 if (lineend >= bufend) { | |
188 /* do not have a whole line in the buffer; read more */ | |
75 | 189 assert(bufend >= linestart); |
15 | 190 if (linestart > 0 && bufend > linestart) { |
191 /* slide to beginning of buffer */ | |
192 memmove(buf, buf+linestart, bufend-linestart); | |
193 bufend -= linestart; | |
194 lineend -= linestart; | |
195 linestart = 0; | |
196 } | |
197 if (bufend >= bufmax) { | |
198 /* need bigger buffer */ | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
199 buf = dorealloc(buf, bufmax, bufmax*2); |
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
200 bufmax = bufmax*2; |
15 | 201 } |
202 | |
203 if (ateof) { | |
204 /* don't read again, in case it's a socket */ | |
205 result = 0; | |
206 } else { | |
207 result = read(fd, buf+bufend, bufmax - bufend); | |
208 } | |
209 | |
210 if (result == -1) { | |
211 /* read error */ | |
212 warn("%s", name); | |
213 complain_fail(); | |
214 } else if (result == 0 && bufend == linestart) { | |
215 /* eof */ | |
216 ateof = true; | |
217 break; | |
218 } else if (result == 0) { | |
219 /* eof in middle of line */ | |
220 ateof = true; | |
221 ptmp = linestartplace; | |
222 ptmp.column += bufend - linestart; | |
223 complain(&ptmp, "No newline at end of file"); | |
224 if (mode.werror) { | |
225 complain_fail(); | |
226 } | |
227 assert(bufend < bufmax); | |
228 lineend = bufend++; | |
229 buf[lineend] = '\n'; | |
230 } else { | |
231 bufend += (size_t)result; | |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
232 lineend = findeol(buf, linestart, bufend); |
15 | 233 } |
234 /* loop in case we still don't have a whole line */ | |
235 continue; | |
236 } | |
237 | |
238 /* have a line */ | |
239 assert(buf[lineend] == '\n'); | |
240 buf[lineend] = '\0'; | |
241 nextlinestart = lineend+1; | |
242 nextlinestartplace.line++; | |
243 | |
244 /* check for CR/NL */ | |
245 if (lineend > 0 && buf[lineend-1] == '\r') { | |
246 buf[lineend-1] = '\0'; | |
247 lineend--; | |
248 } | |
249 | |
250 /* check for continuation line */ | |
251 if (lineend > 0 && buf[lineend-1]=='\\') { | |
252 lineend--; | |
253 tmp = nextlinestart - lineend; | |
254 if (bufend > nextlinestart) { | |
255 memmove(buf+lineend, buf+nextlinestart, | |
256 bufend - nextlinestart); | |
257 } | |
258 bufend -= tmp; | |
259 nextlinestart -= tmp; | |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
260 lineend = findeol(buf, linestart, bufend); |
15 | 261 /* might not have a whole line, so loop */ |
262 continue; | |
263 } | |
264 | |
265 /* line now goes from linestart to lineend */ | |
266 assert(buf[lineend] == '\0'); | |
75 | 267 |
268 /* count how many commented-out newlines we swallowed */ | |
269 nextlinestartplace.line += countnls(buf, linestart, lineend); | |
270 | |
271 /* if the line isn't empty, process it */ | |
15 | 272 if (lineend > linestart) { |
273 directive_gotline(&linestartplace, | |
274 buf+linestart, lineend-linestart); | |
275 } | |
276 | |
277 linestart = nextlinestart; | |
75 | 278 lineend = findeol(buf, linestart, bufend); |
15 | 279 linestartplace = nextlinestartplace; |
280 } | |
281 | |
28 | 282 if (toplevel) { |
283 directive_goteof(&linestartplace); | |
284 } | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
285 dofree(buf, bufmax); |
15 | 286 } |
6 | 287 |
288 //////////////////////////////////////////////////////////// | |
289 // path search | |
290 | |
291 static | |
13 | 292 char * |
112 | 293 mkfilename(struct place *place, const char *dir, const char *file) |
13 | 294 { |
295 size_t dlen, flen, rlen; | |
296 char *ret; | |
297 bool needslash = false; | |
298 | |
112 | 299 if (dir == NULL) { |
300 dir = place_getparsedir(place); | |
301 } | |
302 | |
13 | 303 dlen = strlen(dir); |
304 flen = strlen(file); | |
305 if (dlen > 0 && dir[dlen-1] != '/') { | |
306 needslash = true; | |
307 } | |
308 | |
309 rlen = dlen + (needslash ? 1 : 0) + flen; | |
310 ret = domalloc(rlen + 1); | |
311 strcpy(ret, dir); | |
312 if (needslash) { | |
313 strcat(ret, "/"); | |
314 } | |
315 strcat(ret, file); | |
316 return ret; | |
317 } | |
318 | |
319 static | |
6 | 320 int |
321 file_tryopen(const char *file) | |
322 { | |
323 int fd; | |
324 | |
15 | 325 /* XXX check for non-regular files */ |
326 | |
6 | 327 fd = open(file, O_RDONLY); |
328 if (fd < 0) { | |
329 return -1; | |
330 } | |
15 | 331 |
6 | 332 return fd; |
333 } | |
334 | |
335 static | |
336 void | |
337 file_search(struct place *place, struct incdirarray *path, const char *name) | |
338 { | |
339 unsigned i, num; | |
340 struct incdir *id; | |
13 | 341 const struct placefile *pf; |
6 | 342 char *file; |
343 int fd; | |
344 | |
345 assert(place != NULL); | |
346 | |
104 | 347 if (name[0] == '/') { |
348 fd = file_tryopen(name); | |
6 | 349 if (fd >= 0) { |
104 | 350 pf = place_addfile(place, name, true); |
351 file_read(pf, fd, name, false); | |
6 | 352 close(fd); |
353 return; | |
354 } | |
104 | 355 } else { |
356 num = incdirarray_num(path); | |
357 for (i=0; i<num; i++) { | |
358 id = incdirarray_get(path, i); | |
112 | 359 file = mkfilename(place, id->name, name); |
104 | 360 fd = file_tryopen(file); |
361 if (fd >= 0) { | |
362 pf = place_addfile(place, file, id->issystem); | |
363 file_read(pf, fd, file, false); | |
364 dostrfree(file); | |
365 close(fd); | |
366 return; | |
367 } | |
368 dostrfree(file); | |
369 } | |
6 | 370 } |
371 complain(place, "Include file %s not found", name); | |
372 complain_fail(); | |
373 } | |
374 | |
375 void | |
376 file_readquote(struct place *place, const char *name) | |
377 { | |
378 file_search(place, "epath, name); | |
379 } | |
380 | |
381 void | |
382 file_readbracket(struct place *place, const char *name) | |
383 { | |
384 file_search(place, &bracketpath, name); | |
385 } | |
386 | |
387 void | |
388 file_readabsolute(struct place *place, const char *name) | |
389 { | |
13 | 390 const struct placefile *pf; |
6 | 391 int fd; |
392 | |
393 assert(place != NULL); | |
394 | |
24 | 395 if (name == NULL) { |
396 fd = STDIN_FILENO; | |
397 pf = place_addfile(place, "<standard-input>", false); | |
398 } else { | |
399 fd = file_tryopen(name); | |
400 if (fd < 0) { | |
401 warn("%s", name); | |
402 die(); | |
403 } | |
404 pf = place_addfile(place, name, false); | |
6 | 405 } |
24 | 406 |
28 | 407 file_read(pf, fd, name, true); |
24 | 408 |
409 if (name != NULL) { | |
410 close(fd); | |
411 } | |
6 | 412 } |