annotate files.c @ 18:c08a947d8f30

deal with macro parameters
author David A. Holland
date Mon, 20 Dec 2010 01:51:47 -0500
parents f6177d3ed5c2
children daa801fe719e
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
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
105 file_read(const struct placefile *pf, int fd, const char *name)
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
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
213 directive_goteof(&linestartplace);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
214 free(buf);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
215 }
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
216
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
217 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
218 // path search
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
219
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
220 static
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
221 char *
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
222 mkfilename(const char *dir, const char *file)
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
223 {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
224 size_t dlen, flen, rlen;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
225 char *ret;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
226 bool needslash = false;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
227
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
228 dlen = strlen(dir);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
229 flen = strlen(file);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
230 if (dlen > 0 && dir[dlen-1] != '/') {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
231 needslash = true;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
232 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
233
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
234 rlen = dlen + (needslash ? 1 : 0) + flen;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
235 ret = domalloc(rlen + 1);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
236 strcpy(ret, dir);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
237 if (needslash) {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
238 strcat(ret, "/");
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
239 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
240 strcat(ret, file);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
241 return ret;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
242 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
243
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
244 static
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
245 int
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
246 file_tryopen(const char *file)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
247 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
248 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
249
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
250 /* XXX check for non-regular files */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
251
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
252 fd = open(file, O_RDONLY);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
253 if (fd < 0) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
254 return -1;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
255 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
256
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
257 return fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
258 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
259
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
260 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
261 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
262 file_search(struct place *place, struct incdirarray *path, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
263 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
264 unsigned i, num;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
265 struct incdir *id;
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
266 const struct placefile *pf;
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
267 char *file;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
268 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
269
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
270 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
271
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
272 num = incdirarray_num(path);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
273 for (i=0; i<num; i++) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
274 id = incdirarray_get(path, i);
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
275 file = mkfilename(id->name, name);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
276 fd = file_tryopen(file);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
277 if (fd >= 0) {
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
278 pf = place_addfile(place, file, id->issystem);
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
279 file_read(pf, fd, file);
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
280 free(file);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
281 close(fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
282 return;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
283 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
284 free(file);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
285 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
286 complain(place, "Include file %s not found", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
287 complain_fail();
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
288 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
289
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
290 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
291 file_readquote(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
292 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
293 file_search(place, &quotepath, 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
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
296 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
297 file_readbracket(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
298 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
299 file_search(place, &bracketpath, 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
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
302 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
303 file_readabsolute(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
304 {
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
305 const struct placefile *pf;
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
306 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
307
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
308 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
309
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
310 fd = file_tryopen(name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
311 if (fd < 0) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
312 warn("%s", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
313 die();
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
314 }
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
315 pf = place_addfile(place, name, false);
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
316 file_read(pf, fd, name);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
317 close(fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
318 }