Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate files.c @ 77:123168887da8
Clean out old not-really-working nested comment handling.
author | David A. Holland |
---|---|
date | Mon, 10 Jun 2013 20:12:37 -0400 |
parents | 980ed7cb620a |
children | 27c9aafcaca1 |
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; |
128 | |
129 for (i=start; i<limit; i++) { | |
130 if (incomment) { | |
131 if (i+1 < limit && buf[i] == '*' && buf[i+1] == '/') { | |
132 i++; | |
133 incomment = 0; | |
134 } | |
135 } | |
136 else { | |
137 if (i+1 < limit && buf[i] == '/' && buf[i+1] == '*') { | |
138 i++; | |
139 incomment = 1; | |
140 } | |
141 else { | |
142 if (buf[i] == '\n') { | |
143 return i; | |
144 } | |
145 } | |
146 } | |
147 } | |
148 return limit; | |
149 } | |
150 | |
151 static | |
152 unsigned | |
153 countnls(const char *buf, size_t start, size_t limit) | |
154 { | |
155 size_t i; | |
156 unsigned count = 0; | |
15 | 157 |
158 for (i=start; i<limit; i++) { | |
159 if (buf[i] == '\n') { | |
75 | 160 count++; |
15 | 161 } |
162 } | |
75 | 163 return count; |
15 | 164 } |
165 | |
166 static | |
6 | 167 void |
28 | 168 file_read(const struct placefile *pf, int fd, const char *name, bool toplevel) |
15 | 169 { |
170 struct place linestartplace, nextlinestartplace, ptmp; | |
171 size_t bufend, bufmax, linestart, lineend, nextlinestart, tmp; | |
172 ssize_t result; | |
173 bool ateof = false; | |
174 char *buf; | |
175 | |
176 place_setfilestart(&linestartplace, pf); | |
177 nextlinestartplace = linestartplace; | |
178 | |
179 bufmax = 128; | |
180 bufend = 0; | |
181 linestart = 0; | |
182 lineend = 0; | |
183 buf = domalloc(bufmax); | |
184 | |
185 while (1) { | |
186 if (lineend >= bufend) { | |
187 /* do not have a whole line in the buffer; read more */ | |
75 | 188 assert(bufend >= linestart); |
15 | 189 if (linestart > 0 && bufend > linestart) { |
190 /* slide to beginning of buffer */ | |
191 memmove(buf, buf+linestart, bufend-linestart); | |
192 bufend -= linestart; | |
193 lineend -= linestart; | |
194 linestart = 0; | |
195 } | |
196 if (bufend >= bufmax) { | |
197 /* need bigger buffer */ | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
198 buf = dorealloc(buf, bufmax, bufmax*2); |
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
199 bufmax = bufmax*2; |
15 | 200 } |
201 | |
202 if (ateof) { | |
203 /* don't read again, in case it's a socket */ | |
204 result = 0; | |
205 } else { | |
206 result = read(fd, buf+bufend, bufmax - bufend); | |
207 } | |
208 | |
209 if (result == -1) { | |
210 /* read error */ | |
211 warn("%s", name); | |
212 complain_fail(); | |
213 } else if (result == 0 && bufend == linestart) { | |
214 /* eof */ | |
215 ateof = true; | |
216 break; | |
217 } else if (result == 0) { | |
218 /* eof in middle of line */ | |
219 ateof = true; | |
220 ptmp = linestartplace; | |
221 ptmp.column += bufend - linestart; | |
222 complain(&ptmp, "No newline at end of file"); | |
223 if (mode.werror) { | |
224 complain_fail(); | |
225 } | |
226 assert(bufend < bufmax); | |
227 lineend = bufend++; | |
228 buf[lineend] = '\n'; | |
229 } else { | |
230 tmp = bufend; | |
231 bufend += (size_t)result; | |
75 | 232 lineend = findeol(buf, tmp, 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; | |
75 | 260 lineend = findeol(buf, lineend, 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 } |