Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate files.c @ 197:f5981d0ee2b4
fix typo in error message; noted by Joerg
author | David A. Holland |
---|---|
date | Sat, 05 Dec 2015 18:07:58 -0500 |
parents | 8c7e508da6cc |
children | 1d2bad7151f9 |
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 <stdio.h> |
31 #include <stdlib.h> | |
13 | 32 #include <string.h> |
6 | 33 #include <unistd.h> |
34 #include <fcntl.h> | |
113
1e7144176a42
Print a warning if we get an unexpected error trying to open a file.
David A. Holland
parents:
112
diff
changeset
|
35 #include <errno.h> |
6 | 36 |
183
4c3375895c6e
Don't use <stdbool.h> unless __STDC__ is large enough.
David A. Holland
parents:
175
diff
changeset
|
37 #include "bool.h" |
6 | 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; |
128
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
113
diff
changeset
|
129 char quote = '\0'; |
75 | 130 |
131 for (i=start; i<limit; i++) { | |
132 if (incomment) { | |
133 if (i+1 < limit && buf[i] == '*' && buf[i+1] == '/') { | |
134 i++; | |
135 incomment = 0; | |
136 } | |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
137 } else if (!inquote && i+1 < limit && |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
138 buf[i] == '/' && buf[i+1] == '*') { |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
139 i++; |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
140 incomment = 1; |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
141 } else if (i+1 < limit && |
128
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
113
diff
changeset
|
142 buf[i] == '\\' && buf[i+1] != '\n') { |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
143 i++; |
128
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
113
diff
changeset
|
144 } else if (!inquote && (buf[i] == '"' || buf[i] == '\'')) { |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
113
diff
changeset
|
145 inquote = true; |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
113
diff
changeset
|
146 quote = buf[i]; |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
113
diff
changeset
|
147 } else if (inquote && buf[i] == quote) { |
1cda505ddc78
Don't expand macros within character constants.
David A. Holland
parents:
113
diff
changeset
|
148 inquote = false; |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
149 } else if (buf[i] == '\n') { |
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
150 return i; |
75 | 151 } |
152 } | |
153 return limit; | |
154 } | |
155 | |
156 static | |
157 unsigned | |
158 countnls(const char *buf, size_t start, size_t limit) | |
159 { | |
160 size_t i; | |
161 unsigned count = 0; | |
15 | 162 |
163 for (i=start; i<limit; i++) { | |
164 if (buf[i] == '\n') { | |
75 | 165 count++; |
15 | 166 } |
167 } | |
75 | 168 return count; |
15 | 169 } |
170 | |
171 static | |
6 | 172 void |
28 | 173 file_read(const struct placefile *pf, int fd, const char *name, bool toplevel) |
15 | 174 { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
143
diff
changeset
|
175 struct lineplace places; |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
143
diff
changeset
|
176 struct place ptmp; |
15 | 177 size_t bufend, bufmax, linestart, lineend, nextlinestart, tmp; |
178 ssize_t result; | |
179 bool ateof = false; | |
180 char *buf; | |
181 | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
143
diff
changeset
|
182 place_setfilestart(&places.current, pf); |
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
143
diff
changeset
|
183 places.nextline = places.current; |
15 | 184 |
185 bufmax = 128; | |
186 bufend = 0; | |
187 linestart = 0; | |
188 lineend = 0; | |
189 buf = domalloc(bufmax); | |
190 | |
191 while (1) { | |
192 if (lineend >= bufend) { | |
193 /* do not have a whole line in the buffer; read more */ | |
75 | 194 assert(bufend >= linestart); |
15 | 195 if (linestart > 0 && bufend > linestart) { |
196 /* slide to beginning of buffer */ | |
197 memmove(buf, buf+linestart, bufend-linestart); | |
198 bufend -= linestart; | |
199 lineend -= linestart; | |
200 linestart = 0; | |
201 } | |
202 if (bufend >= bufmax) { | |
203 /* need bigger buffer */ | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
204 buf = dorealloc(buf, bufmax, bufmax*2); |
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
205 bufmax = bufmax*2; |
15 | 206 } |
207 | |
208 if (ateof) { | |
209 /* don't read again, in case it's a socket */ | |
210 result = 0; | |
211 } else { | |
212 result = read(fd, buf+bufend, bufmax - bufend); | |
213 } | |
214 | |
215 if (result == -1) { | |
216 /* read error */ | |
143 | 217 complain(NULL, "%s: %s", |
218 name, strerror(errno)); | |
15 | 219 complain_fail(); |
220 } else if (result == 0 && bufend == linestart) { | |
221 /* eof */ | |
222 ateof = true; | |
223 break; | |
224 } else if (result == 0) { | |
225 /* eof in middle of line */ | |
226 ateof = true; | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
143
diff
changeset
|
227 ptmp = places.current; |
15 | 228 ptmp.column += bufend - linestart; |
186
9637bf434f8e
Don't report unclosed comments as "no newline at end of file".
David A. Holland
parents:
183
diff
changeset
|
229 if (buf[bufend - 1] == '\n') { |
9637bf434f8e
Don't report unclosed comments as "no newline at end of file".
David A. Holland
parents:
183
diff
changeset
|
230 complain(&ptmp, "Unclosed comment"); |
187 | 231 complain_fail(); |
186
9637bf434f8e
Don't report unclosed comments as "no newline at end of file".
David A. Holland
parents:
183
diff
changeset
|
232 } else { |
9637bf434f8e
Don't report unclosed comments as "no newline at end of file".
David A. Holland
parents:
183
diff
changeset
|
233 complain(&ptmp, |
9637bf434f8e
Don't report unclosed comments as "no newline at end of file".
David A. Holland
parents:
183
diff
changeset
|
234 "No newline at end of file"); |
9637bf434f8e
Don't report unclosed comments as "no newline at end of file".
David A. Holland
parents:
183
diff
changeset
|
235 } |
15 | 236 if (mode.werror) { |
237 complain_fail(); | |
238 } | |
239 assert(bufend < bufmax); | |
240 lineend = bufend++; | |
241 buf[lineend] = '\n'; | |
242 } else { | |
243 bufend += (size_t)result; | |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
244 lineend = findeol(buf, linestart, bufend); |
15 | 245 } |
246 /* loop in case we still don't have a whole line */ | |
247 continue; | |
248 } | |
249 | |
250 /* have a line */ | |
251 assert(buf[lineend] == '\n'); | |
252 buf[lineend] = '\0'; | |
253 nextlinestart = lineend+1; | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
143
diff
changeset
|
254 places.nextline.line++; |
15 | 255 |
256 /* check for CR/NL */ | |
257 if (lineend > 0 && buf[lineend-1] == '\r') { | |
258 buf[lineend-1] = '\0'; | |
259 lineend--; | |
260 } | |
261 | |
262 /* check for continuation line */ | |
263 if (lineend > 0 && buf[lineend-1]=='\\') { | |
264 lineend--; | |
265 tmp = nextlinestart - lineend; | |
266 if (bufend > nextlinestart) { | |
267 memmove(buf+lineend, buf+nextlinestart, | |
268 bufend - nextlinestart); | |
269 } | |
270 bufend -= tmp; | |
271 nextlinestart -= tmp; | |
81
27c9aafcaca1
Don't recognize comments within double-quote strings.
David A. Holland
parents:
75
diff
changeset
|
272 lineend = findeol(buf, linestart, bufend); |
15 | 273 /* might not have a whole line, so loop */ |
274 continue; | |
275 } | |
276 | |
277 /* line now goes from linestart to lineend */ | |
278 assert(buf[lineend] == '\0'); | |
75 | 279 |
280 /* count how many commented-out newlines we swallowed */ | |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
143
diff
changeset
|
281 places.nextline.line += countnls(buf, linestart, lineend); |
75 | 282 |
175 | 283 /* process the line (even if it's empty) */ |
284 directive_gotline(&places, buf+linestart, lineend-linestart); | |
15 | 285 |
286 linestart = nextlinestart; | |
75 | 287 lineend = findeol(buf, linestart, bufend); |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
143
diff
changeset
|
288 places.current = places.nextline; |
15 | 289 } |
290 | |
28 | 291 if (toplevel) { |
154
a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
David A. Holland
parents:
143
diff
changeset
|
292 directive_goteof(&places.current); |
28 | 293 } |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
294 dofree(buf, bufmax); |
15 | 295 } |
6 | 296 |
297 //////////////////////////////////////////////////////////// | |
298 // path search | |
299 | |
300 static | |
13 | 301 char * |
112 | 302 mkfilename(struct place *place, const char *dir, const char *file) |
13 | 303 { |
304 size_t dlen, flen, rlen; | |
305 char *ret; | |
306 bool needslash = false; | |
307 | |
112 | 308 if (dir == NULL) { |
309 dir = place_getparsedir(place); | |
310 } | |
311 | |
13 | 312 dlen = strlen(dir); |
313 flen = strlen(file); | |
314 if (dlen > 0 && dir[dlen-1] != '/') { | |
315 needslash = true; | |
316 } | |
317 | |
318 rlen = dlen + (needslash ? 1 : 0) + flen; | |
319 ret = domalloc(rlen + 1); | |
320 strcpy(ret, dir); | |
321 if (needslash) { | |
322 strcat(ret, "/"); | |
323 } | |
324 strcat(ret, file); | |
325 return ret; | |
326 } | |
327 | |
328 static | |
6 | 329 int |
330 file_tryopen(const char *file) | |
331 { | |
332 int fd; | |
333 | |
15 | 334 /* XXX check for non-regular files */ |
335 | |
6 | 336 fd = open(file, O_RDONLY); |
337 if (fd < 0) { | |
113
1e7144176a42
Print a warning if we get an unexpected error trying to open a file.
David A. Holland
parents:
112
diff
changeset
|
338 if (errno != ENOENT && errno != ENOTDIR) { |
143 | 339 complain(NULL, "%s: %s", file, strerror(errno)); |
113
1e7144176a42
Print a warning if we get an unexpected error trying to open a file.
David A. Holland
parents:
112
diff
changeset
|
340 } |
6 | 341 return -1; |
342 } | |
15 | 343 |
6 | 344 return fd; |
345 } | |
346 | |
347 static | |
348 void | |
349 file_search(struct place *place, struct incdirarray *path, const char *name) | |
350 { | |
351 unsigned i, num; | |
352 struct incdir *id; | |
13 | 353 const struct placefile *pf; |
6 | 354 char *file; |
355 int fd; | |
356 | |
357 assert(place != NULL); | |
358 | |
104 | 359 if (name[0] == '/') { |
360 fd = file_tryopen(name); | |
6 | 361 if (fd >= 0) { |
104 | 362 pf = place_addfile(place, name, true); |
363 file_read(pf, fd, name, false); | |
6 | 364 close(fd); |
365 return; | |
366 } | |
104 | 367 } else { |
368 num = incdirarray_num(path); | |
369 for (i=0; i<num; i++) { | |
370 id = incdirarray_get(path, i); | |
112 | 371 file = mkfilename(place, id->name, name); |
104 | 372 fd = file_tryopen(file); |
373 if (fd >= 0) { | |
374 pf = place_addfile(place, file, id->issystem); | |
375 file_read(pf, fd, file, false); | |
376 dostrfree(file); | |
377 close(fd); | |
378 return; | |
379 } | |
380 dostrfree(file); | |
381 } | |
6 | 382 } |
383 complain(place, "Include file %s not found", name); | |
384 complain_fail(); | |
385 } | |
386 | |
387 void | |
388 file_readquote(struct place *place, const char *name) | |
389 { | |
390 file_search(place, "epath, name); | |
391 } | |
392 | |
393 void | |
394 file_readbracket(struct place *place, const char *name) | |
395 { | |
396 file_search(place, &bracketpath, name); | |
397 } | |
398 | |
399 void | |
400 file_readabsolute(struct place *place, const char *name) | |
401 { | |
13 | 402 const struct placefile *pf; |
6 | 403 int fd; |
404 | |
405 assert(place != NULL); | |
406 | |
24 | 407 if (name == NULL) { |
408 fd = STDIN_FILENO; | |
409 pf = place_addfile(place, "<standard-input>", false); | |
410 } else { | |
411 fd = file_tryopen(name); | |
412 if (fd < 0) { | |
143 | 413 complain(NULL, "%s: %s", name, strerror(errno)); |
24 | 414 die(); |
415 } | |
416 pf = place_addfile(place, name, false); | |
6 | 417 } |
24 | 418 |
28 | 419 file_read(pf, fd, name, true); |
24 | 420 |
421 if (name != NULL) { | |
422 close(fd); | |
423 } | |
6 | 424 } |