annotate files.c @ 7:b8167949474a

make places work better
author David A. Holland
date Sun, 19 Dec 2010 19:08:24 -0500
parents 0601b6e8e53d
children 97243badae69
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 <stdarg.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
2 #include <stdbool.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
3 #include <stdio.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
4 #include <stdlib.h>
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"
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
10 #include "files.h"
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
11
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
12 struct place {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
13 struct seenfile *file;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
14 unsigned line;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
15 unsigned column;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
16 };
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
17
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
18 struct incdir {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
19 const char *name;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
20 bool issystem;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
21 };
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
22
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
23 struct seenfile {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
24 struct place includedfrom;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
25 char *name;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
26 bool fromsystemdir;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
27 };
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
28
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
29 DECLARRAY(incdir);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
30 DECLARRAY(seenfile);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
31 DEFARRAY(incdir, );
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
32 DEFARRAY(seenfile, );
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
33
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
34 static struct incdirarray quotepath, bracketpath;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
35 static struct seenfilearray seenfiles;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
36 static bool overall_failure;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
37
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
38 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
39 // management
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
40
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
41 #define DESTROYALL(T) \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
42 static \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
43 void \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
44 T##array_destroyall(struct T##array *arr) \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
45 { \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
46 unsigned i, num; \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
47 struct T *t; \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
48 \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
49 num = T##array_num(arr); \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
50 for (i=0; i<num; i++) { \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
51 t = T##array_get(arr, i); \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
52 T##_destroy(t); \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
53 } \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
54 T##array_setsize(arr, 0); \
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
55 }
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 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
73 free(id);
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 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
77 struct seenfile *
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
78 seenfile_create(const struct place *from, char *name, bool fromsystemdir)
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 seenfile *sf;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
81
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
82 sf = domalloc(sizeof(*sf));
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
83 sf->includedfrom = *from;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
84 sf->name = name;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
85 sf->fromsystemdir = fromsystemdir;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
86 return sf;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
87 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
88
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
89 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
90 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
91 seenfile_destroy(struct seenfile *sf)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
92 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
93 free(sf->name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
94 free(sf);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
95 }
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_init(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
99 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
100 incdirarray_init(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
101 incdirarray_init(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
102 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
103
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
104 DESTROYALL(incdir);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
105 DESTROYALL(seenfile);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
106
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
107 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
108 files_cleanup(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
109 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
110 seenfilearray_destroyall(&seenfiles);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
111 seenfilearray_cleanup(&seenfiles);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
112
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
113 incdirarray_destroyall(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
114 incdirarray_cleanup(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
115 incdirarray_destroyall(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
116 incdirarray_cleanup(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
117 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
118
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
119 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
120 // path setup
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
121
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
122 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
123 files_addquotepath(const char *dir, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
124 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
125 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
126
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
127 id = incdir_create(dir, issystem);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
128 incdirarray_add(&quotepath, id, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
129 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
130
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
131 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
132 files_addbracketpath(const char *dir, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
133 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
134 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
135
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
136 id = incdir_create(dir, issystem);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
137 incdirarray_add(&bracketpath, id, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
138 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
139
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
140 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
141 // places and complaints
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
142
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
143 #define NOWHERE_LINE 0
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
144 #define BUILTIN_LINE 1
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
145 #define COMMANDLINE_LINE 2
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
146
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
147 static struct place scratchplace;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
148 static bool scratchplace_inuse;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
149
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
150 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
151 bool
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
152 place_isnowhere(const struct place *p)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
153 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
154 return p->file == NULL && p->line == NOWHERE_LINE;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
155 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
156
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
157 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
158 bool
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
159 place_isbuiltin(const struct place *p)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
160 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
161 return p->file == NULL && p->line == BUILTIN_LINE;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
162 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
163
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
164 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
165 bool
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
166 place_iscommandline(const struct place *p)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
167 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
168 return p->file == NULL && p->line == COMMANDLINE_LINE;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
169 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
170
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
171 struct place *
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
172 place_gettemporary(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
173 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
174 assert(!scratchplace_inuse);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
175 scratchplace_inuse = true;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
176 return &scratchplace;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
177 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
178
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
179 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
180 place_puttemporary(struct place *p)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
181 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
182 assert(scratchplace_inuse);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
183 assert(p == &scratchplace);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
184 scratchplace_inuse = false;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
185 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
186
7
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
187 struct place *
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
188 place_create(void)
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
189 {
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
190 struct place *p;
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
191
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
192 p = domalloc(sizeof(*p));
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
193 place_setnowhere(p);
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
194 return p;
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
195 }
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
196
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
197 struct place *
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
198 place_clone(const struct place *op)
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
199 {
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
200 struct place *p;
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
201
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
202 p = domalloc(sizeof(*p));
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
203 *p = *op;
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
204 return p;
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
205 }
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
206
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
207 void
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
208 place_destroy(struct place *p)
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
209 {
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
210 free(p);
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
211 }
b8167949474a make places work better
David A. Holland
parents: 6
diff changeset
212
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
213 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
214 place_setnowhere(struct place *p)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
215 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
216 p->file = NULL;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
217 p->line = NOWHERE_LINE;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
218 p->column = 0;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
219 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
220
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
221 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
222 place_setbuiltin(struct place *p, unsigned num)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
223 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
224 p->file = NULL;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
225 p->line = BUILTIN_LINE;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
226 p->column = num;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
227 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
228
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
229 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
230 place_setcommandline(struct place *p, unsigned column)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
231 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
232 p->file = NULL;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
233 p->line = COMMANDLINE_LINE;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
234 p->column = column;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
235 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
236
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
237 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
238 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
239 place_print(const struct place *p)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
240 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
241 if (place_iscommandline(p)) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
242 fprintf(stderr, "<command-line>:1:%u", p->column);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
243 } else if (place_isbuiltin(p)) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
244 fprintf(stderr, "<built-in>:%u:1", p->column);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
245 } else {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
246 fprintf(stderr, "%s:%u:%u", p->file->name, p->line, p->column);
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
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
250 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
251 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
252 place_printfrom(const struct place *p)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
253 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
254 if (!place_isnowhere(&p->file->includedfrom)) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
255 place_printfrom(&p->file->includedfrom);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
256 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
257 fprintf(stderr, "In file included from ");
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
258 fprintf(stderr, ":\n");
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
259 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
260
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 complain(const struct place *p, const char *fmt, ...)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
263 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
264 va_list ap;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
265
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
266 if (!place_isnowhere(&p->file->includedfrom)) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
267 place_printfrom(&p->file->includedfrom);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
268 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
269 place_print(p);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
270 fprintf(stderr, ": ");
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
271 va_start(ap, fmt);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
272 vfprintf(stderr, fmt, ap);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
273 va_end(ap);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
274 fprintf(stderr, "\n");
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
275 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
276
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
277 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
278 complain_fail(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
279 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
280 overall_failure = true;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
281 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
282
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
283 bool
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
284 complain_failed(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
285 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
286 return overall_failure;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
287 }
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 // parsing
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_read(struct seenfile *sf, int fd);
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 // path search
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
297
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
298 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
299 int
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
300 file_tryopen(const char *file)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
301 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
302 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
303
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
304 fd = open(file, O_RDONLY);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
305 if (fd < 0) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
306 return -1;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
307 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
308 /* XXX: do we need to do anything here or is this function pointless?*/
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
309 return fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
310 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
311
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
312 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
313 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
314 file_search(struct place *place, struct incdirarray *path, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
315 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
316 unsigned i, num;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
317 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
318 struct seenfile *sf;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
319 char *file;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
320 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
321
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
322 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
323
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
324 num = incdirarray_num(path);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
325 for (i=0; i<num; i++) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
326 id = incdirarray_get(path, i);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
327 file = dostrdup3(id->name, "/", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
328 fd = file_tryopen(file);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
329 if (fd >= 0) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
330 sf = seenfile_create(place, file, id->issystem);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
331 seenfilearray_add(&seenfiles, sf, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
332 file_read(sf, fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
333 close(fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
334 return;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
335 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
336 free(file);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
337 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
338 complain(place, "Include file %s not found", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
339 complain_fail();
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
340 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
341
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
342 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
343 file_readquote(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
344 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
345 file_search(place, &quotepath, name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
346 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
347
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
348 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
349 file_readbracket(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
350 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
351 file_search(place, &bracketpath, name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
352 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
353
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
354 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
355 file_readabsolute(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
356 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
357 struct seenfile *sf;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
358 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
359
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
360 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
361
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
362 fd = file_tryopen(name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
363 if (fd < 0) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
364 warn("%s", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
365 die();
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
366 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
367 sf = seenfile_create(place, dostrdup(name), false);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
368 seenfilearray_add(&seenfiles, sf, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
369 file_read(sf, fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
370 close(fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
371 }