Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate files.c @ 86:70d7ebeb4523
Erm. Don't throw away -D args.
author | David A. Holland |
---|---|
date | Mon, 10 Jun 2013 21:37:01 -0400 |
parents | 27c9aafcaca1 |
children | 60184aa42604 |
rev | line source |
---|---|
30 | 1 /*- |
2 * Copyright (c) 2010 The NetBSD Foundation, Inc. | |
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 | |
47
2e25e55dba6b
Fix inline usage as per the version in dholland-make2.
David A. Holland
parents:
39
diff
changeset
|
49 DECLARRAY(incdir, static __unused); |
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 * |
293 mkfilename(const char *dir, const char *file) | |
294 { | |
295 size_t dlen, flen, rlen; | |
296 char *ret; | |
297 bool needslash = false; | |
298 | |
299 dlen = strlen(dir); | |
300 flen = strlen(file); | |
301 if (dlen > 0 && dir[dlen-1] != '/') { | |
302 needslash = true; | |
303 } | |
304 | |
305 rlen = dlen + (needslash ? 1 : 0) + flen; | |
306 ret = domalloc(rlen + 1); | |
307 strcpy(ret, dir); | |
308 if (needslash) { | |
309 strcat(ret, "/"); | |
310 } | |
311 strcat(ret, file); | |
312 return ret; | |
313 } | |
314 | |
315 static | |
6 | 316 int |
317 file_tryopen(const char *file) | |
318 { | |
319 int fd; | |
320 | |
15 | 321 /* XXX check for non-regular files */ |
322 | |
6 | 323 fd = open(file, O_RDONLY); |
324 if (fd < 0) { | |
325 return -1; | |
326 } | |
15 | 327 |
6 | 328 return fd; |
329 } | |
330 | |
331 static | |
332 void | |
333 file_search(struct place *place, struct incdirarray *path, const char *name) | |
334 { | |
335 unsigned i, num; | |
336 struct incdir *id; | |
13 | 337 const struct placefile *pf; |
6 | 338 char *file; |
339 int fd; | |
340 | |
341 assert(place != NULL); | |
342 | |
343 num = incdirarray_num(path); | |
344 for (i=0; i<num; i++) { | |
345 id = incdirarray_get(path, i); | |
13 | 346 file = mkfilename(id->name, name); |
6 | 347 fd = file_tryopen(file); |
348 if (fd >= 0) { | |
13 | 349 pf = place_addfile(place, file, id->issystem); |
28 | 350 file_read(pf, fd, file, false); |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
351 dostrfree(file); |
6 | 352 close(fd); |
353 return; | |
354 } | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
355 dostrfree(file); |
6 | 356 } |
357 complain(place, "Include file %s not found", name); | |
358 complain_fail(); | |
359 } | |
360 | |
361 void | |
362 file_readquote(struct place *place, const char *name) | |
363 { | |
364 file_search(place, "epath, name); | |
365 } | |
366 | |
367 void | |
368 file_readbracket(struct place *place, const char *name) | |
369 { | |
370 file_search(place, &bracketpath, name); | |
371 } | |
372 | |
373 void | |
374 file_readabsolute(struct place *place, const char *name) | |
375 { | |
13 | 376 const struct placefile *pf; |
6 | 377 int fd; |
378 | |
379 assert(place != NULL); | |
380 | |
24 | 381 if (name == NULL) { |
382 fd = STDIN_FILENO; | |
383 pf = place_addfile(place, "<standard-input>", false); | |
384 } else { | |
385 fd = file_tryopen(name); | |
386 if (fd < 0) { | |
387 warn("%s", name); | |
388 die(); | |
389 } | |
390 pf = place_addfile(place, name, false); | |
6 | 391 } |
24 | 392 |
28 | 393 file_read(pf, fd, name, true); |
24 | 394 |
395 if (name != NULL) { | |
396 close(fd); | |
397 } | |
6 | 398 } |