Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate files.c @ 105:600f36cd7353
don't use getprogname() in the name of portability
author | David A. Holland |
---|---|
date | Tue, 11 Jun 2013 11:24:48 -0400 |
parents | 91f600e6647b |
children | 33954a07d013 |
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 | |
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 | |
104 | 343 if (name[0] == '/') { |
344 fd = file_tryopen(name); | |
6 | 345 if (fd >= 0) { |
104 | 346 pf = place_addfile(place, name, true); |
347 file_read(pf, fd, name, false); | |
6 | 348 close(fd); |
349 return; | |
350 } | |
104 | 351 } else { |
352 num = incdirarray_num(path); | |
353 for (i=0; i<num; i++) { | |
354 id = incdirarray_get(path, i); | |
355 file = mkfilename(id->name, name); | |
356 fd = file_tryopen(file); | |
357 if (fd >= 0) { | |
358 pf = place_addfile(place, file, id->issystem); | |
359 file_read(pf, fd, file, false); | |
360 dostrfree(file); | |
361 close(fd); | |
362 return; | |
363 } | |
364 dostrfree(file); | |
365 } | |
6 | 366 } |
367 complain(place, "Include file %s not found", name); | |
368 complain_fail(); | |
369 } | |
370 | |
371 void | |
372 file_readquote(struct place *place, const char *name) | |
373 { | |
374 file_search(place, "epath, name); | |
375 } | |
376 | |
377 void | |
378 file_readbracket(struct place *place, const char *name) | |
379 { | |
380 file_search(place, &bracketpath, name); | |
381 } | |
382 | |
383 void | |
384 file_readabsolute(struct place *place, const char *name) | |
385 { | |
13 | 386 const struct placefile *pf; |
6 | 387 int fd; |
388 | |
389 assert(place != NULL); | |
390 | |
24 | 391 if (name == NULL) { |
392 fd = STDIN_FILENO; | |
393 pf = place_addfile(place, "<standard-input>", false); | |
394 } else { | |
395 fd = file_tryopen(name); | |
396 if (fd < 0) { | |
397 warn("%s", name); | |
398 die(); | |
399 } | |
400 pf = place_addfile(place, name, false); | |
6 | 401 } |
24 | 402 |
28 | 403 file_read(pf, fd, name, true); |
24 | 404 |
405 if (name != NULL) { | |
406 close(fd); | |
407 } | |
6 | 408 } |