annotate files.c @ 6:0601b6e8e53d

checkpoint - can find files
author David A. Holland
date Sun, 19 Dec 2010 18:55:51 -0500
parents
children b8167949474a
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
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
187 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
188 place_setnowhere(struct place *p)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
189 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
190 p->file = NULL;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
191 p->line = NOWHERE_LINE;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
192 p->column = 0;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
193 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
194
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
195 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
196 place_setbuiltin(struct place *p, unsigned num)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
197 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
198 p->file = NULL;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
199 p->line = BUILTIN_LINE;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
200 p->column = num;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
201 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
202
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
203 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
204 place_setcommandline(struct place *p, unsigned column)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
205 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
206 p->file = NULL;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
207 p->line = COMMANDLINE_LINE;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
208 p->column = column;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
209 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
210
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
211 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
212 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
213 place_print(const struct place *p)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
214 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
215 if (place_iscommandline(p)) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
216 fprintf(stderr, "<command-line>:1:%u", p->column);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
217 } else if (place_isbuiltin(p)) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
218 fprintf(stderr, "<built-in>:%u:1", p->column);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
219 } else {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
220 fprintf(stderr, "%s:%u:%u", p->file->name, p->line, p->column);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
221 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
222 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
223
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
224 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
225 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
226 place_printfrom(const struct place *p)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
227 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
228 if (!place_isnowhere(&p->file->includedfrom)) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
229 place_printfrom(&p->file->includedfrom);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
230 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
231 fprintf(stderr, "In file included from ");
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
232 fprintf(stderr, ":\n");
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
233 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
234
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
235 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
236 complain(const struct place *p, const char *fmt, ...)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
237 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
238 va_list ap;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
239
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
240 if (!place_isnowhere(&p->file->includedfrom)) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
241 place_printfrom(&p->file->includedfrom);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
242 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
243 place_print(p);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
244 fprintf(stderr, ": ");
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
245 va_start(ap, fmt);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
246 vfprintf(stderr, fmt, ap);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
247 va_end(ap);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
248 fprintf(stderr, "\n");
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
249 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
250
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 complain_fail(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
253 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
254 overall_failure = true;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
255 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
256
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
257 bool
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
258 complain_failed(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
259 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
260 return overall_failure;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
261 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
262
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
263 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
264 // parsing
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
265
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
266 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
267 file_read(struct seenfile *sf, int fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
268
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
269 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
270 // path search
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
271
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
272 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
273 int
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
274 file_tryopen(const char *file)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
275 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
276 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
277
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
278 fd = open(file, O_RDONLY);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
279 if (fd < 0) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
280 return -1;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
281 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
282 /* XXX: do we need to do anything here or is this function pointless?*/
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
283 return fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
284 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
285
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
286 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
287 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
288 file_search(struct place *place, struct incdirarray *path, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
289 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
290 unsigned i, num;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
291 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
292 struct seenfile *sf;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
293 char *file;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
294 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
295
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
296 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
297
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
298 num = incdirarray_num(path);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
299 for (i=0; i<num; i++) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
300 id = incdirarray_get(path, i);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
301 file = dostrdup3(id->name, "/", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
302 fd = file_tryopen(file);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
303 if (fd >= 0) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
304 sf = seenfile_create(place, file, id->issystem);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
305 seenfilearray_add(&seenfiles, sf, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
306 file_read(sf, fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
307 close(fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
308 return;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
309 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
310 free(file);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
311 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
312 complain(place, "Include file %s not found", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
313 complain_fail();
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
314 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
315
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
316 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
317 file_readquote(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
318 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
319 file_search(place, &quotepath, name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
320 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
321
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
322 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
323 file_readbracket(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
324 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
325 file_search(place, &bracketpath, name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
326 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
327
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
328 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
329 file_readabsolute(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
330 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
331 struct seenfile *sf;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
332 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
333
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
334 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
335
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
336 fd = file_tryopen(name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
337 if (fd < 0) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
338 warn("%s", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
339 die();
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
340 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
341 sf = seenfile_create(place, dostrdup(name), false);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
342 seenfilearray_add(&seenfiles, sf, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
343 file_read(sf, fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
344 close(fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
345 }