annotate files.c @ 64:f50b4ea6cbfe

Prune single-line comments from (most) directive lines. Also, don't pass the string length to the directive processing functions, as half of them weren't honoring it. Instead, ensure that the directive line is terminated at the place the directive processing functions should stop looking at it.
author David A. Holland
date Sun, 31 Mar 2013 02:04:56 -0400
parents 2e25e55dba6b
children 980ed7cb620a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
1 /*-
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
2 * Copyright (c) 2010 The NetBSD Foundation, Inc.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
3 * All rights reserved.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
4 *
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
5 * This code is derived from software contributed to The NetBSD Foundation
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
6 * by David A. Holland.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
7 *
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
8 * Redistribution and use in source and binary forms, with or without
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
9 * modification, are permitted provided that the following conditions
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
10 * are met:
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
11 * 1. Redistributions of source code must retain the above copyright
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
12 * notice, this list of conditions and the following disclaimer.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
14 * notice, this list of conditions and the following disclaimer in the
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
15 * documentation and/or other materials provided with the distribution.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
16 *
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
27 * POSSIBILITY OF SUCH DAMAGE.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
28 */
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
29
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
30 #include <stdbool.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
31 #include <stdio.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
32 #include <stdlib.h>
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
33 #include <string.h>
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
34 #include <unistd.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
35 #include <fcntl.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
36 #include <err.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
37
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
38 #include "array.h"
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
39 #include "mode.h"
8
97243badae69 split place stuff to its own file
David A. Holland
parents: 7
diff changeset
40 #include "place.h"
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
41 #include "files.h"
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
42 #include "directive.h"
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
43
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
44 struct incdir {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
45 const char *name;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
46 bool issystem;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
47 };
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
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
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
51
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
52 static struct incdirarray quotepath, bracketpath;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
53
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
54 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
55 // management
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
56
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
57 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
58 struct incdir *
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
59 incdir_create(const char *name, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
60 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
61 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
62
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
63 id = domalloc(sizeof(*id));
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
64 id->name = name;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
65 id->issystem = issystem;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
66 return id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
67 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
68
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
69 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
70 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
71 incdir_destroy(struct incdir *id)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
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
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
74 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
75
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
76 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
77 files_init(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
78 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
79 incdirarray_init(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
80 incdirarray_init(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
81 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
82
9
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 8
diff changeset
83 DESTROYALL_ARRAY(incdir, );
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
84
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
85 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
86 files_cleanup(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
87 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
88 incdirarray_destroyall(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
89 incdirarray_cleanup(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
90 incdirarray_destroyall(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
91 incdirarray_cleanup(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
92 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
93
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
94 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
95 // path setup
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
96
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
97 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
98 files_addquotepath(const char *dir, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
99 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
100 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
101
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
102 id = incdir_create(dir, issystem);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
103 incdirarray_add(&quotepath, id, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
104 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
105
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
106 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
107 files_addbracketpath(const char *dir, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
108 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
109 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
110
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
111 id = incdir_create(dir, issystem);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
112 incdirarray_add(&bracketpath, id, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
113 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
114
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
115 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
116 // parsing
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
117
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
118 static
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
119 size_t
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
120 findnl(const char *buf, size_t start, size_t limit)
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
121 {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
122 size_t i;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
123
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
124 for (i=start; i<limit; i++) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
125 if (buf[i] == '\n') {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
126 return i;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
127 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
128 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
129 return limit;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
130 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
131
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
132 static
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
133 void
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
134 file_read(const struct placefile *pf, int fd, const char *name, bool toplevel)
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
135 {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
136 struct place linestartplace, nextlinestartplace, ptmp;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
137 size_t bufend, bufmax, linestart, lineend, nextlinestart, tmp;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
138 ssize_t result;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
139 bool ateof = false;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
140 char *buf;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
141
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
142 place_setfilestart(&linestartplace, pf);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
143 nextlinestartplace = linestartplace;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
144
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
145 bufmax = 128;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
146 bufend = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
147 linestart = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
148 lineend = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
149 buf = domalloc(bufmax);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
150
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
151 while (1) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
152 if (lineend >= bufend) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
153 /* do not have a whole line in the buffer; read more */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
154 if (linestart > 0 && bufend > linestart) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
155 /* slide to beginning of buffer */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
156 memmove(buf, buf+linestart, bufend-linestart);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
157 bufend -= linestart;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
158 lineend -= linestart;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
159 linestart = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
160 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
161 if (bufend >= bufmax) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
162 /* need bigger buffer */
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
163 buf = dorealloc(buf, bufmax, bufmax*2);
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
164 bufmax = bufmax*2;
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
165 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
166
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
167 if (ateof) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
168 /* don't read again, in case it's a socket */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
169 result = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
170 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
171 result = read(fd, buf+bufend, bufmax - bufend);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
172 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
173
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
174 if (result == -1) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
175 /* read error */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
176 warn("%s", name);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
177 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
178 } else if (result == 0 && bufend == linestart) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
179 /* eof */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
180 ateof = true;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
181 break;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
182 } else if (result == 0) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
183 /* eof in middle of line */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
184 ateof = true;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
185 ptmp = linestartplace;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
186 ptmp.column += bufend - linestart;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
187 complain(&ptmp, "No newline at end of file");
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
188 if (mode.werror) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
189 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
190 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
191 assert(bufend < bufmax);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
192 lineend = bufend++;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
193 buf[lineend] = '\n';
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
194 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
195 tmp = bufend;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
196 bufend += (size_t)result;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
197 lineend = findnl(buf, tmp, bufend);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
198 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
199 /* loop in case we still don't have a whole line */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
200 continue;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
201 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
202
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
203 /* have a line */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
204 assert(buf[lineend] == '\n');
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
205 buf[lineend] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
206 nextlinestart = lineend+1;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
207 nextlinestartplace.line++;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
208
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
209 /* check for CR/NL */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
210 if (lineend > 0 && buf[lineend-1] == '\r') {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
211 buf[lineend-1] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
212 lineend--;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
213 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
214
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
215 /* check for continuation line */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
216 if (lineend > 0 && buf[lineend-1]=='\\') {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
217 lineend--;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
218 tmp = nextlinestart - lineend;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
219 if (bufend > nextlinestart) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
220 memmove(buf+lineend, buf+nextlinestart,
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
221 bufend - nextlinestart);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
222 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
223 bufend -= tmp;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
224 nextlinestart -= tmp;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
225 lineend = findnl(buf, lineend, bufend);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
226 /* might not have a whole line, so loop */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
227 continue;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
228 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
229
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
230 /* line now goes from linestart to lineend */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
231 assert(buf[lineend] == '\0');
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
232 if (lineend > linestart) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
233 directive_gotline(&linestartplace,
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
234 buf+linestart, lineend-linestart);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
235 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
236
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
237 linestart = nextlinestart;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
238 lineend = findnl(buf, linestart, bufend);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
239 linestartplace = nextlinestartplace;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
240 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
241
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
242 if (toplevel) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
243 directive_goteof(&linestartplace);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
244 }
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
245 dofree(buf, bufmax);
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
246 }
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
247
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
248 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
249 // path search
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
250
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
251 static
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
252 char *
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
253 mkfilename(const char *dir, const char *file)
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
254 {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
255 size_t dlen, flen, rlen;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
256 char *ret;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
257 bool needslash = false;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
258
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
259 dlen = strlen(dir);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
260 flen = strlen(file);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
261 if (dlen > 0 && dir[dlen-1] != '/') {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
262 needslash = true;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
263 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
264
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
265 rlen = dlen + (needslash ? 1 : 0) + flen;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
266 ret = domalloc(rlen + 1);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
267 strcpy(ret, dir);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
268 if (needslash) {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
269 strcat(ret, "/");
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
270 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
271 strcat(ret, file);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
272 return ret;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
273 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
274
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
275 static
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
276 int
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
277 file_tryopen(const char *file)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
278 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
279 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
280
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
281 /* XXX check for non-regular files */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
282
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
283 fd = open(file, O_RDONLY);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
284 if (fd < 0) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
285 return -1;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
286 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
287
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
288 return fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
289 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
290
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
291 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
292 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
293 file_search(struct place *place, struct incdirarray *path, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
294 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
295 unsigned i, num;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
296 struct incdir *id;
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
297 const struct placefile *pf;
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
298 char *file;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
299 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
300
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
301 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
302
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
303 num = incdirarray_num(path);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
304 for (i=0; i<num; i++) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
305 id = incdirarray_get(path, i);
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
306 file = mkfilename(id->name, name);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
307 fd = file_tryopen(file);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
308 if (fd >= 0) {
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
309 pf = place_addfile(place, file, id->issystem);
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
310 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
311 dostrfree(file);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
312 close(fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
313 return;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
314 }
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
315 dostrfree(file);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
316 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
317 complain(place, "Include file %s not found", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
318 complain_fail();
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
319 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
320
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
321 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
322 file_readquote(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
323 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
324 file_search(place, &quotepath, name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
325 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
326
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
327 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
328 file_readbracket(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
329 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
330 file_search(place, &bracketpath, name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
331 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
332
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
333 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
334 file_readabsolute(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
335 {
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
336 const struct placefile *pf;
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
337 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
338
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
339 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
340
24
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
341 if (name == NULL) {
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
342 fd = STDIN_FILENO;
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
343 pf = place_addfile(place, "<standard-input>", false);
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
344 } else {
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
345 fd = file_tryopen(name);
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
346 if (fd < 0) {
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
347 warn("%s", name);
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
348 die();
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
349 }
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
350 pf = place_addfile(place, name, false);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
351 }
24
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
352
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
353 file_read(pf, fd, name, true);
24
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
354
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
355 if (name != NULL) {
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
356 close(fd);
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
357 }
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
358 }