annotate files.c @ 28:8a955e3dda2c posted-20101220

two more tests, more fixes
author David A. Holland
date Mon, 20 Dec 2010 05:42:15 -0500
parents daa801fe719e
children 76c114899f63
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
1 #include <stdbool.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
2 #include <stdio.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
3 #include <stdlib.h>
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
4 #include <string.h>
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
5 #include <unistd.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
6 #include <fcntl.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
7 #include <err.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
8
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
9 #include "array.h"
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
10 #include "mode.h"
8
97243badae69 split place stuff to its own file
David A. Holland
parents: 7
diff changeset
11 #include "place.h"
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
12 #include "files.h"
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
13 #include "directive.h"
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
14
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
15 struct incdir {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
16 const char *name;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
17 bool issystem;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
18 };
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
19
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
20 DECLARRAY(incdir);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
21 DEFARRAY(incdir, );
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
22
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
23 static struct incdirarray quotepath, bracketpath;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
24
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
25 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
26 // management
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
27
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
28 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
29 struct incdir *
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
30 incdir_create(const char *name, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
31 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
32 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
33
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
34 id = domalloc(sizeof(*id));
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
35 id->name = name;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
36 id->issystem = issystem;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
37 return id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
38 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
39
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
40 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
41 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
42 incdir_destroy(struct incdir *id)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
43 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
44 free(id);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
45 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
46
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
47 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
48 files_init(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
49 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
50 incdirarray_init(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
51 incdirarray_init(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
52 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
53
9
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 8
diff changeset
54 DESTROYALL_ARRAY(incdir, );
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
55
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
56 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
57 files_cleanup(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
58 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
59 incdirarray_destroyall(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
60 incdirarray_cleanup(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
61 incdirarray_destroyall(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
62 incdirarray_cleanup(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
63 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
64
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
65 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
66 // path setup
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
67
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
68 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
69 files_addquotepath(const char *dir, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
70 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
71 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
72
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
73 id = incdir_create(dir, issystem);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
74 incdirarray_add(&quotepath, id, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
75 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
76
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
77 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
78 files_addbracketpath(const char *dir, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
79 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
80 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
81
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
82 id = incdir_create(dir, issystem);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
83 incdirarray_add(&bracketpath, id, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
84 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
85
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
86 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
87 // parsing
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
88
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
89 static
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
90 size_t
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
91 findnl(const char *buf, size_t start, size_t limit)
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
92 {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
93 size_t i;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
94
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
95 for (i=start; i<limit; i++) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
96 if (buf[i] == '\n') {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
97 return i;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
98 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
99 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
100 return limit;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
101 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
102
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
103 static
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
104 void
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
105 file_read(const struct placefile *pf, int fd, const char *name, bool toplevel)
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
106 {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
107 struct place linestartplace, nextlinestartplace, ptmp;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
108 size_t bufend, bufmax, linestart, lineend, nextlinestart, tmp;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
109 ssize_t result;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
110 bool ateof = false;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
111 char *buf;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
112
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
113 place_setfilestart(&linestartplace, pf);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
114 nextlinestartplace = linestartplace;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
115
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
116 bufmax = 128;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
117 bufend = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
118 linestart = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
119 lineend = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
120 buf = domalloc(bufmax);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
121
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
122 while (1) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
123 if (lineend >= bufend) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
124 /* do not have a whole line in the buffer; read more */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
125 if (linestart > 0 && bufend > linestart) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
126 /* slide to beginning of buffer */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
127 memmove(buf, buf+linestart, bufend-linestart);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
128 bufend -= linestart;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
129 lineend -= linestart;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
130 linestart = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
131 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
132 if (bufend >= bufmax) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
133 /* need bigger buffer */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
134 bufmax *= 2;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
135 buf = dorealloc(buf, bufmax);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
136 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
137
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
138 if (ateof) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
139 /* don't read again, in case it's a socket */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
140 result = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
141 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
142 result = read(fd, buf+bufend, bufmax - bufend);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
143 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
144
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
145 if (result == -1) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
146 /* read error */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
147 warn("%s", name);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
148 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
149 } else if (result == 0 && bufend == linestart) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
150 /* eof */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
151 ateof = true;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
152 break;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
153 } else if (result == 0) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
154 /* eof in middle of line */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
155 ateof = true;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
156 ptmp = linestartplace;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
157 ptmp.column += bufend - linestart;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
158 complain(&ptmp, "No newline at end of file");
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
159 if (mode.werror) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
160 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
161 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
162 assert(bufend < bufmax);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
163 lineend = bufend++;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
164 buf[lineend] = '\n';
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
165 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
166 tmp = bufend;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
167 bufend += (size_t)result;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
168 lineend = findnl(buf, tmp, bufend);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
169 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
170 /* loop in case we still don't have a whole line */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
171 continue;
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 /* have a line */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
175 assert(buf[lineend] == '\n');
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
176 buf[lineend] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
177 nextlinestart = lineend+1;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
178 nextlinestartplace.line++;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
179
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
180 /* check for CR/NL */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
181 if (lineend > 0 && buf[lineend-1] == '\r') {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
182 buf[lineend-1] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
183 lineend--;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
184 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
185
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
186 /* check for continuation line */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
187 if (lineend > 0 && buf[lineend-1]=='\\') {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
188 lineend--;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
189 tmp = nextlinestart - lineend;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
190 if (bufend > nextlinestart) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
191 memmove(buf+lineend, buf+nextlinestart,
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
192 bufend - nextlinestart);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
193 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
194 bufend -= tmp;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
195 nextlinestart -= tmp;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
196 lineend = findnl(buf, lineend, bufend);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
197 /* might not have a whole line, so loop */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
198 continue;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
199 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
200
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
201 /* line now goes from linestart to lineend */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
202 assert(buf[lineend] == '\0');
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
203 if (lineend > linestart) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
204 directive_gotline(&linestartplace,
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
205 buf+linestart, lineend-linestart);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
206 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
207
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
208 linestart = nextlinestart;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
209 lineend = findnl(buf, linestart, bufend);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
210 linestartplace = nextlinestartplace;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
211 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
212
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
213 if (toplevel) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
214 directive_goteof(&linestartplace);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
215 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
216 free(buf);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
217 }
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
218
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
219 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
220 // path search
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
221
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
222 static
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
223 char *
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
224 mkfilename(const char *dir, const char *file)
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
225 {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
226 size_t dlen, flen, rlen;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
227 char *ret;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
228 bool needslash = false;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
229
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
230 dlen = strlen(dir);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
231 flen = strlen(file);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
232 if (dlen > 0 && dir[dlen-1] != '/') {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
233 needslash = true;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
234 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
235
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
236 rlen = dlen + (needslash ? 1 : 0) + flen;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
237 ret = domalloc(rlen + 1);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
238 strcpy(ret, dir);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
239 if (needslash) {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
240 strcat(ret, "/");
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
241 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
242 strcat(ret, file);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
243 return ret;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
244 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
245
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
246 static
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
247 int
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
248 file_tryopen(const char *file)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
249 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
250 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
251
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
252 /* XXX check for non-regular files */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
253
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
254 fd = open(file, O_RDONLY);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
255 if (fd < 0) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
256 return -1;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
257 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
258
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
259 return fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
260 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
261
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
262 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
263 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
264 file_search(struct place *place, struct incdirarray *path, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
265 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
266 unsigned i, num;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
267 struct incdir *id;
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
268 const struct placefile *pf;
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
269 char *file;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
270 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
271
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
272 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
273
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
274 num = incdirarray_num(path);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
275 for (i=0; i<num; i++) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
276 id = incdirarray_get(path, i);
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
277 file = mkfilename(id->name, name);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
278 fd = file_tryopen(file);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
279 if (fd >= 0) {
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
280 pf = place_addfile(place, file, id->issystem);
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
281 file_read(pf, fd, file, false);
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
282 free(file);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
283 close(fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
284 return;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
285 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
286 free(file);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
287 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
288 complain(place, "Include file %s not found", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
289 complain_fail();
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
290 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
291
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_readquote(struct place *place, 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 file_search(place, &quotepath, name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
296 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
297
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
298 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
299 file_readbracket(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
300 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
301 file_search(place, &bracketpath, name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
302 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
303
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
304 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
305 file_readabsolute(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
306 {
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
307 const struct placefile *pf;
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
308 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
309
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
310 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
311
24
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
312 if (name == NULL) {
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
313 fd = STDIN_FILENO;
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
314 pf = place_addfile(place, "<standard-input>", false);
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
315 } else {
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
316 fd = file_tryopen(name);
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
317 if (fd < 0) {
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
318 warn("%s", name);
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
319 die();
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
320 }
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
321 pf = place_addfile(place, name, false);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
322 }
24
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
323
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
324 file_read(pf, fd, name, true);
24
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
325
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
326 if (name != NULL) {
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
327 close(fd);
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
328 }
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
329 }