comparison files.c @ 8:97243badae69

split place stuff to its own file
author David A. Holland
date Sun, 19 Dec 2010 19:15:55 -0500
parents b8167949474a
children 1fbcbd58742e
comparison
equal deleted inserted replaced
7:b8167949474a 8:97243badae69
1 #include <stdarg.h>
2 #include <stdbool.h> 1 #include <stdbool.h>
3 #include <stdio.h> 2 #include <stdio.h>
4 #include <stdlib.h> 3 #include <stdlib.h>
5 #include <unistd.h> 4 #include <unistd.h>
6 #include <fcntl.h> 5 #include <fcntl.h>
7 #include <err.h> 6 #include <err.h>
8 7
9 #include "array.h" 8 #include "array.h"
9 #include "place.h"
10 #include "files.h" 10 #include "files.h"
11
12 struct place {
13 struct seenfile *file;
14 unsigned line;
15 unsigned column;
16 };
17 11
18 struct incdir { 12 struct incdir {
19 const char *name; 13 const char *name;
20 bool issystem; 14 bool issystem;
21 }; 15 };
31 DEFARRAY(incdir, ); 25 DEFARRAY(incdir, );
32 DEFARRAY(seenfile, ); 26 DEFARRAY(seenfile, );
33 27
34 static struct incdirarray quotepath, bracketpath; 28 static struct incdirarray quotepath, bracketpath;
35 static struct seenfilearray seenfiles; 29 static struct seenfilearray seenfiles;
36 static bool overall_failure;
37 30
38 //////////////////////////////////////////////////////////// 31 ////////////////////////////////////////////////////////////
39 // management 32 // management
40 33
41 #define DESTROYALL(T) \ 34 #define DESTROYALL(T) \
136 id = incdir_create(dir, issystem); 129 id = incdir_create(dir, issystem);
137 incdirarray_add(&bracketpath, id, NULL); 130 incdirarray_add(&bracketpath, id, NULL);
138 } 131 }
139 132
140 //////////////////////////////////////////////////////////// 133 ////////////////////////////////////////////////////////////
141 // places and complaints 134 // seenfile functions exposed for places.c
142 135
143 #define NOWHERE_LINE 0 136 const char *
144 #define BUILTIN_LINE 1 137 seenfile_getname(const struct seenfile *file)
145 #define COMMANDLINE_LINE 2 138 {
146 139 return file->name;
147 static struct place scratchplace; 140 }
148 static bool scratchplace_inuse; 141
149 142 const struct place *
150 static 143 seenfile_getincludeplace(const struct seenfile *file)
151 bool 144 {
152 place_isnowhere(const struct place *p) 145 return &file->includedfrom;
153 {
154 return p->file == NULL && p->line == NOWHERE_LINE;
155 }
156
157 static
158 bool
159 place_isbuiltin(const struct place *p)
160 {
161 return p->file == NULL && p->line == BUILTIN_LINE;
162 }
163
164 static
165 bool
166 place_iscommandline(const struct place *p)
167 {
168 return p->file == NULL && p->line == COMMANDLINE_LINE;
169 }
170
171 struct place *
172 place_gettemporary(void)
173 {
174 assert(!scratchplace_inuse);
175 scratchplace_inuse = true;
176 return &scratchplace;
177 }
178
179 void
180 place_puttemporary(struct place *p)
181 {
182 assert(scratchplace_inuse);
183 assert(p == &scratchplace);
184 scratchplace_inuse = false;
185 }
186
187 struct place *
188 place_create(void)
189 {
190 struct place *p;
191
192 p = domalloc(sizeof(*p));
193 place_setnowhere(p);
194 return p;
195 }
196
197 struct place *
198 place_clone(const struct place *op)
199 {
200 struct place *p;
201
202 p = domalloc(sizeof(*p));
203 *p = *op;
204 return p;
205 }
206
207 void
208 place_destroy(struct place *p)
209 {
210 free(p);
211 }
212
213 void
214 place_setnowhere(struct place *p)
215 {
216 p->file = NULL;
217 p->line = NOWHERE_LINE;
218 p->column = 0;
219 }
220
221 void
222 place_setbuiltin(struct place *p, unsigned num)
223 {
224 p->file = NULL;
225 p->line = BUILTIN_LINE;
226 p->column = num;
227 }
228
229 void
230 place_setcommandline(struct place *p, unsigned column)
231 {
232 p->file = NULL;
233 p->line = COMMANDLINE_LINE;
234 p->column = column;
235 }
236
237 static
238 void
239 place_print(const struct place *p)
240 {
241 if (place_iscommandline(p)) {
242 fprintf(stderr, "<command-line>:1:%u", p->column);
243 } else if (place_isbuiltin(p)) {
244 fprintf(stderr, "<built-in>:%u:1", p->column);
245 } else {
246 fprintf(stderr, "%s:%u:%u", p->file->name, p->line, p->column);
247 }
248 }
249
250 static
251 void
252 place_printfrom(const struct place *p)
253 {
254 if (!place_isnowhere(&p->file->includedfrom)) {
255 place_printfrom(&p->file->includedfrom);
256 }
257 fprintf(stderr, "In file included from ");
258 fprintf(stderr, ":\n");
259 }
260
261 void
262 complain(const struct place *p, const char *fmt, ...)
263 {
264 va_list ap;
265
266 if (!place_isnowhere(&p->file->includedfrom)) {
267 place_printfrom(&p->file->includedfrom);
268 }
269 place_print(p);
270 fprintf(stderr, ": ");
271 va_start(ap, fmt);
272 vfprintf(stderr, fmt, ap);
273 va_end(ap);
274 fprintf(stderr, "\n");
275 }
276
277 void
278 complain_fail(void)
279 {
280 overall_failure = true;
281 }
282
283 bool
284 complain_failed(void)
285 {
286 return overall_failure;
287 } 146 }
288 147
289 //////////////////////////////////////////////////////////// 148 ////////////////////////////////////////////////////////////
290 // parsing 149 // parsing
291 150