Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate place.c @ 173:6ff17ab68b16
directive.c needs limits.h
author | David A. Holland |
---|---|
date | Fri, 12 Jun 2015 02:15:43 -0400 |
parents | d6e6b3940780 |
children | a2f047301c15 |
rev | line source |
---|---|
30 | 1 /*- |
2 * Copyright (c) 2010 The NetBSD Foundation, Inc. | |
3 * All rights reserved. | |
4 * | |
5 * This code is derived from software contributed to The NetBSD Foundation | |
6 * by David A. Holland. | |
7 * | |
8 * Redistribution and use in source and binary forms, with or without | |
9 * modification, are permitted provided that the following conditions | |
10 * are met: | |
11 * 1. Redistributions of source code must retain the above copyright | |
12 * notice, this list of conditions and the following disclaimer. | |
13 * 2. Redistributions in binary form must reproduce the above copyright | |
14 * notice, this list of conditions and the following disclaimer in the | |
15 * documentation and/or other materials provided with the distribution. | |
16 * | |
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | |
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
27 * POSSIBILITY OF SUCH DAMAGE. | |
28 */ | |
29 | |
8 | 30 #include <assert.h> |
31 #include <stdarg.h> | |
32 #include <stdio.h> | |
33 #include <stdlib.h> | |
112 | 34 #include <string.h> |
8 | 35 |
36 #include "utils.h" | |
10 | 37 #include "array.h" |
8 | 38 #include "place.h" |
39 | |
13 | 40 struct placefile { |
10 | 41 struct place includedfrom; |
112 | 42 char *dir; |
10 | 43 char *name; |
28 | 44 int depth; |
10 | 45 bool fromsystemdir; |
46 }; | |
107 | 47 DECLARRAY(placefile, static UNUSED); |
47
2e25e55dba6b
Fix inline usage as per the version in dholland-make2.
David A. Holland
parents:
39
diff
changeset
|
48 DEFARRAY(placefile, static); |
10 | 49 |
13 | 50 static struct placefilearray placefiles; |
10 | 51 static bool overall_failure; |
52 | |
142 | 53 static const char *myprogname; |
54 | |
10 | 55 //////////////////////////////////////////////////////////// |
56 // seenfiles | |
57 | |
58 static | |
13 | 59 struct placefile * |
60 placefile_create(const struct place *from, const char *name, | |
61 bool fromsystemdir) | |
10 | 62 { |
13 | 63 struct placefile *pf; |
112 | 64 const char *s; |
65 size_t len; | |
10 | 66 |
13 | 67 pf = domalloc(sizeof(*pf)); |
68 pf->includedfrom = *from; | |
112 | 69 |
70 s = strrchr(name, '/'); | |
71 len = (s == NULL) ? 0 : s - name; | |
72 pf->dir = dostrndup(name, len); | |
73 | |
13 | 74 pf->name = dostrdup(name); |
75 pf->fromsystemdir = fromsystemdir; | |
112 | 76 |
28 | 77 if (from->file != NULL) { |
78 pf->depth = from->file->depth + 1; | |
79 } else { | |
80 pf->depth = 1; | |
81 } | |
13 | 82 return pf; |
10 | 83 } |
84 | |
85 static | |
86 void | |
13 | 87 placefile_destroy(struct placefile *pf) |
10 | 88 { |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
89 dostrfree(pf->name); |
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
90 dofree(pf, sizeof(*pf)); |
10 | 91 } |
92 | |
13 | 93 DESTROYALL_ARRAY(placefile, ); |
10 | 94 |
112 | 95 const char * |
96 place_getparsedir(const struct place *place) | |
97 { | |
98 if (place->file == NULL) { | |
99 return "."; | |
100 } | |
101 return place->file->dir; | |
102 } | |
103 | |
13 | 104 const struct placefile * |
105 place_addfile(const struct place *place, const char *file, bool issystem) | |
10 | 106 { |
13 | 107 struct placefile *pf; |
10 | 108 |
13 | 109 pf = placefile_create(place, file, issystem); |
110 placefilearray_add(&placefiles, pf, NULL); | |
114
05d67dd74e1f
Reduce the maximum include depth from 128 to 120.
David A. Holland
parents:
112
diff
changeset
|
111 if (pf->depth > 120) { |
28 | 112 complain(place, "Maximum include nesting depth exceeded"); |
113 die(); | |
114 } | |
13 | 115 return pf; |
10 | 116 } |
117 | |
118 //////////////////////////////////////////////////////////// | |
119 // places | |
120 | |
8 | 121 void |
122 place_setnowhere(struct place *p) | |
123 { | |
12 | 124 p->type = P_NOWHERE; |
8 | 125 p->file = NULL; |
12 | 126 p->line = 0; |
8 | 127 p->column = 0; |
128 } | |
129 | |
130 void | |
131 place_setbuiltin(struct place *p, unsigned num) | |
132 { | |
12 | 133 p->type = P_BUILTIN; |
8 | 134 p->file = NULL; |
12 | 135 p->line = num; |
136 p->column = 1; | |
8 | 137 } |
138 | |
139 void | |
14 | 140 place_setcommandline(struct place *p, unsigned line, unsigned column) |
8 | 141 { |
14 | 142 p->type = P_COMMANDLINE; |
8 | 143 p->file = NULL; |
14 | 144 p->line = line; |
8 | 145 p->column = column; |
146 } | |
147 | |
14 | 148 void |
149 place_setfilestart(struct place *p, const struct placefile *pf) | |
150 { | |
28 | 151 p->type = P_FILE; |
14 | 152 p->file = pf; |
153 p->line = 1; | |
154 p->column = 1; | |
155 } | |
156 | |
160 | 157 void |
158 place_setfile(struct place *p, const char *name) | |
159 { | |
160 assert(p->type == P_FILE); | |
161 if (strcmp(name, p->file->name) == 0) { | |
162 return; | |
163 } | |
164 p->file = placefile_create(&p->file->includedfrom, name, | |
165 p->file->fromsystemdir); | |
166 } | |
167 | |
12 | 168 const char * |
169 place_getname(const struct place *p) | |
8 | 170 { |
12 | 171 switch (p->type) { |
172 case P_NOWHERE: return "<nowhere>"; | |
173 case P_BUILTIN: return "<built-in>"; | |
174 case P_COMMANDLINE: return "<command-line>"; | |
175 case P_FILE: return p->file->name; | |
8 | 176 } |
12 | 177 assert(0); |
178 return NULL; | |
8 | 179 } |
180 | |
181 static | |
182 void | |
183 place_printfrom(const struct place *p) | |
184 { | |
185 const struct place *from; | |
186 | |
95
1c0575f7dd46
Don't crash printing the commandline place.
David A. Holland
parents:
47
diff
changeset
|
187 if (p->file == NULL) { |
1c0575f7dd46
Don't crash printing the commandline place.
David A. Holland
parents:
47
diff
changeset
|
188 return; |
1c0575f7dd46
Don't crash printing the commandline place.
David A. Holland
parents:
47
diff
changeset
|
189 } |
10 | 190 from = &p->file->includedfrom; |
12 | 191 if (from->type != P_NOWHERE) { |
8 | 192 place_printfrom(from); |
12 | 193 fprintf(stderr, "In file included from %s:%u:%u:\n", |
194 place_getname(from), from->line, from->column); | |
8 | 195 } |
196 } | |
197 | |
10 | 198 //////////////////////////////////////////////////////////// |
199 // complaints | |
200 | |
8 | 201 void |
142 | 202 complain_init(const char *pn) |
203 { | |
204 myprogname = pn; | |
205 } | |
206 | |
207 void | |
8 | 208 complain(const struct place *p, const char *fmt, ...) |
209 { | |
210 va_list ap; | |
211 | |
142 | 212 if (p != NULL) { |
213 place_printfrom(p); | |
214 fprintf(stderr, "%s:%u:%u: ", place_getname(p), | |
215 p->line, p->column); | |
216 } else { | |
217 fprintf(stderr, "%s: ", myprogname); | |
218 } | |
8 | 219 va_start(ap, fmt); |
220 vfprintf(stderr, fmt, ap); | |
221 va_end(ap); | |
222 fprintf(stderr, "\n"); | |
223 } | |
224 | |
225 void | |
226 complain_fail(void) | |
227 { | |
228 overall_failure = true; | |
229 } | |
230 | |
231 bool | |
232 complain_failed(void) | |
233 { | |
234 return overall_failure; | |
235 } | |
236 | |
10 | 237 //////////////////////////////////////////////////////////// |
238 // module init and cleanup | |
239 | |
240 void | |
241 place_init(void) | |
242 { | |
13 | 243 placefilearray_init(&placefiles); |
10 | 244 } |
245 | |
246 void | |
247 place_cleanup(void) | |
248 { | |
13 | 249 placefilearray_destroyall(&placefiles); |
250 placefilearray_cleanup(&placefiles); | |
10 | 251 } |