1 |
|
|
/* $OpenBSD: look.c,v 1.24 2014/12/21 09:33:12 espie Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 1989, 1993 |
5 |
|
|
* The Regents of the University of California. All rights reserved. |
6 |
|
|
* |
7 |
|
|
* This code is derived from software contributed to Berkeley by |
8 |
|
|
* Ozan Yigit at York University. |
9 |
|
|
* |
10 |
|
|
* Redistribution and use in source and binary forms, with or without |
11 |
|
|
* modification, are permitted provided that the following conditions |
12 |
|
|
* are met: |
13 |
|
|
* 1. Redistributions of source code must retain the above copyright |
14 |
|
|
* notice, this list of conditions and the following disclaimer. |
15 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
16 |
|
|
* notice, this list of conditions and the following disclaimer in the |
17 |
|
|
* documentation and/or other materials provided with the distribution. |
18 |
|
|
* 3. Neither the name of the University nor the names of its contributors |
19 |
|
|
* may be used to endorse or promote products derived from this software |
20 |
|
|
* without specific prior written permission. |
21 |
|
|
* |
22 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
23 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
29 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
31 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 |
|
|
* SUCH DAMAGE. |
33 |
|
|
*/ |
34 |
|
|
|
35 |
|
|
/* |
36 |
|
|
* look.c |
37 |
|
|
* Facility: m4 macro processor |
38 |
|
|
* by: oz |
39 |
|
|
*/ |
40 |
|
|
|
41 |
|
|
#include <stdio.h> |
42 |
|
|
#include <stdlib.h> |
43 |
|
|
#include <stdint.h> |
44 |
|
|
#include <stddef.h> |
45 |
|
|
#include <string.h> |
46 |
|
|
#include <ohash.h> |
47 |
|
|
#include "mdef.h" |
48 |
|
|
#include "stdd.h" |
49 |
|
|
#include "extern.h" |
50 |
|
|
|
51 |
|
|
static void *hash_calloc(size_t, size_t, void *); |
52 |
|
|
static void hash_free(void *, void *); |
53 |
|
|
static void *element_alloc(size_t, void *); |
54 |
|
|
static void setup_definition(struct macro_definition *, const char *, |
55 |
|
|
const char *); |
56 |
|
|
static void free_definition(char *); |
57 |
|
|
static void keep(char *); |
58 |
|
|
static int string_in_use(const char *); |
59 |
|
|
|
60 |
|
|
static struct ohash_info macro_info = { |
61 |
|
|
offsetof(struct ndblock, name), |
62 |
|
|
NULL, hash_calloc, hash_free, element_alloc }; |
63 |
|
|
|
64 |
|
|
struct ohash macros; |
65 |
|
|
|
66 |
|
|
/* Support routines for hash tables. */ |
67 |
|
|
void * |
68 |
|
|
hash_calloc(size_t n, size_t s, void *u UNUSED) |
69 |
|
|
{ |
70 |
|
668 |
void *storage = xcalloc(n, s, "hash alloc"); |
71 |
|
334 |
return storage; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
void |
75 |
|
|
hash_free(void *p, void *u UNUSED) |
76 |
|
|
{ |
77 |
|
|
free(p); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
void * |
81 |
|
|
element_alloc(size_t s, void *u UNUSED) |
82 |
|
|
{ |
83 |
|
31530 |
return xalloc(s, "element alloc"); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
void |
87 |
|
|
init_macros() |
88 |
|
|
{ |
89 |
|
668 |
ohash_init(¯os, 10, ¯o_info); |
90 |
|
334 |
} |
91 |
|
|
|
92 |
|
|
/* |
93 |
|
|
* find name in the hash table |
94 |
|
|
*/ |
95 |
|
|
ndptr |
96 |
|
|
lookup(const char *name) |
97 |
|
|
{ |
98 |
|
234 |
return ohash_find(¯os, ohash_qlookup(¯os, name)); |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
struct macro_definition * |
102 |
|
|
lookup_macro_definition(const char *name) |
103 |
|
|
{ |
104 |
|
|
ndptr p; |
105 |
|
|
|
106 |
|
2764 |
p = ohash_find(¯os, ohash_qlookup(¯os, name)); |
107 |
✓✓ |
1382 |
if (p) |
108 |
|
505 |
return p->d; |
109 |
|
|
else |
110 |
|
877 |
return NULL; |
111 |
|
1382 |
} |
112 |
|
|
|
113 |
|
|
static void |
114 |
|
|
setup_definition(struct macro_definition *d, const char *defn, const char *name) |
115 |
|
|
{ |
116 |
|
|
ndptr p; |
117 |
|
|
|
118 |
✓✓✓✗
|
2043 |
if (strncmp(defn, BUILTIN_MARKER, sizeof(BUILTIN_MARKER)-1) == 0 && |
119 |
|
45 |
(p = macro_getbuiltin(defn+sizeof(BUILTIN_MARKER)-1)) != NULL) { |
120 |
|
45 |
d->type = macro_builtin_type(p); |
121 |
|
45 |
d->defn = xstrdup(defn+sizeof(BUILTIN_MARKER)-1); |
122 |
|
45 |
} else { |
123 |
✓✓ |
954 |
if (!*defn) |
124 |
|
151 |
d->defn = null; |
125 |
|
|
else |
126 |
|
803 |
d->defn = xstrdup(defn); |
127 |
|
954 |
d->type = MACRTYPE; |
128 |
|
|
} |
129 |
✓✓✗✓
|
1009 |
if (STREQ(name, defn)) |
130 |
|
|
d->type |= RECDEF; |
131 |
|
999 |
} |
132 |
|
|
|
133 |
|
|
static ndptr |
134 |
|
|
create_entry(const char *name) |
135 |
|
|
{ |
136 |
|
31570 |
const char *end = NULL; |
137 |
|
|
unsigned int i; |
138 |
|
|
ndptr n; |
139 |
|
|
|
140 |
|
15785 |
i = ohash_qlookupi(¯os, name, &end); |
141 |
|
15785 |
n = ohash_find(¯os, i); |
142 |
✓✓ |
15785 |
if (n == NULL) { |
143 |
|
15765 |
n = ohash_create_entry(¯o_info, name, &end); |
144 |
|
15765 |
ohash_insert(¯os, i, n); |
145 |
|
15765 |
n->trace_flags = FLAG_NO_TRACE; |
146 |
|
15765 |
n->builtin_type = MACRTYPE; |
147 |
|
15765 |
n->d = NULL; |
148 |
|
15765 |
} |
149 |
|
15785 |
return n; |
150 |
|
15785 |
} |
151 |
|
|
|
152 |
|
|
void |
153 |
|
|
macro_define(const char *name, const char *defn) |
154 |
|
|
{ |
155 |
|
1998 |
ndptr n = create_entry(name); |
156 |
✓✓ |
999 |
if (n->d != NULL) { |
157 |
✓✓ |
11 |
if (n->d->defn != null) |
158 |
|
9 |
free_definition(n->d->defn); |
159 |
|
|
} else { |
160 |
|
988 |
n->d = xalloc(sizeof(struct macro_definition), NULL); |
161 |
|
988 |
n->d->next = NULL; |
162 |
|
|
} |
163 |
|
999 |
setup_definition(n->d, defn, name); |
164 |
|
999 |
} |
165 |
|
|
|
166 |
|
|
void |
167 |
|
|
macro_pushdef(const char *name, const char *defn) |
168 |
|
|
{ |
169 |
|
|
ndptr n; |
170 |
|
|
struct macro_definition *d; |
171 |
|
|
|
172 |
|
|
n = create_entry(name); |
173 |
|
|
d = xalloc(sizeof(struct macro_definition), NULL); |
174 |
|
|
d->next = n->d; |
175 |
|
|
n->d = d; |
176 |
|
|
setup_definition(n->d, defn, name); |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
void |
180 |
|
|
macro_undefine(const char *name) |
181 |
|
|
{ |
182 |
|
108 |
ndptr n = lookup(name); |
183 |
✓✗ |
54 |
if (n != NULL) { |
184 |
|
|
struct macro_definition *r, *r2; |
185 |
|
|
|
186 |
✓✓ |
216 |
for (r = n->d; r != NULL; r = r2) { |
187 |
|
54 |
r2 = r->next; |
188 |
✓✗ |
54 |
if (r->defn != null) |
189 |
|
54 |
free(r->defn); |
190 |
|
54 |
free(r); |
191 |
|
|
} |
192 |
|
54 |
n->d = NULL; |
193 |
|
54 |
} |
194 |
|
54 |
} |
195 |
|
|
|
196 |
|
|
void |
197 |
|
|
macro_popdef(const char *name) |
198 |
|
|
{ |
199 |
|
|
ndptr n = lookup(name); |
200 |
|
|
|
201 |
|
|
if (n != NULL) { |
202 |
|
|
struct macro_definition *r = n->d; |
203 |
|
|
if (r != NULL) { |
204 |
|
|
n->d = r->next; |
205 |
|
|
if (r->defn != null) |
206 |
|
|
free(r->defn); |
207 |
|
|
free(r); |
208 |
|
|
} |
209 |
|
|
} |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
void |
213 |
|
|
macro_for_all(void (*f)(const char *, struct macro_definition *)) |
214 |
|
|
{ |
215 |
|
|
ndptr n; |
216 |
|
36 |
unsigned int i; |
217 |
|
|
|
218 |
✓✓ |
1620 |
for (n = ohash_first(¯os, &i); n != NULL; |
219 |
|
792 |
n = ohash_next(¯os, &i)) |
220 |
✓✓ |
792 |
if (n->d != NULL) |
221 |
|
783 |
f(n->name, n->d); |
222 |
|
18 |
} |
223 |
|
|
|
224 |
|
|
void |
225 |
|
|
setup_builtin(const char *name, unsigned int type) |
226 |
|
|
{ |
227 |
|
|
ndptr n; |
228 |
|
|
char *name2; |
229 |
|
|
|
230 |
✓✓ |
29572 |
if (prefix_builtins) { |
231 |
|
836 |
name2 = xalloc(strlen(name)+3+1, NULL); |
232 |
|
836 |
memcpy(name2, "m4_", 3); |
233 |
|
836 |
memcpy(name2 + 3, name, strlen(name)+1); |
234 |
|
836 |
} else |
235 |
|
13950 |
name2 = xstrdup(name); |
236 |
|
|
|
237 |
|
14786 |
n = create_entry(name2); |
238 |
|
14786 |
n->builtin_type = type; |
239 |
|
14786 |
n->d = xalloc(sizeof(struct macro_definition), NULL); |
240 |
|
14786 |
n->d->defn = name2; |
241 |
|
14786 |
n->d->type = type; |
242 |
|
14786 |
n->d->next = NULL; |
243 |
|
14786 |
} |
244 |
|
|
|
245 |
|
|
void |
246 |
|
|
mark_traced(const char *name, int on) |
247 |
|
|
{ |
248 |
|
|
ndptr p; |
249 |
|
|
unsigned int i; |
250 |
|
|
|
251 |
|
|
if (name == NULL) { |
252 |
|
|
if (on) |
253 |
|
|
trace_flags |= TRACE_ALL; |
254 |
|
|
else |
255 |
|
|
trace_flags &= ~TRACE_ALL; |
256 |
|
|
for (p = ohash_first(¯os, &i); p != NULL; |
257 |
|
|
p = ohash_next(¯os, &i)) |
258 |
|
|
p->trace_flags = FLAG_NO_TRACE; |
259 |
|
|
} else { |
260 |
|
|
p = create_entry(name); |
261 |
|
|
p->trace_flags = on; |
262 |
|
|
} |
263 |
|
|
} |
264 |
|
|
|
265 |
|
|
ndptr |
266 |
|
|
macro_getbuiltin(const char *name) |
267 |
|
|
{ |
268 |
|
|
ndptr p; |
269 |
|
|
|
270 |
|
90 |
p = lookup(name); |
271 |
✓✗✗✓
|
90 |
if (p == NULL || p->builtin_type == MACRTYPE) |
272 |
|
|
return NULL; |
273 |
|
|
else |
274 |
|
45 |
return p; |
275 |
|
45 |
} |
276 |
|
|
|
277 |
|
|
/* XXX things are slightly more complicated than they seem. |
278 |
|
|
* a macro may actually be "live" (in the middle of an expansion |
279 |
|
|
* on the stack. |
280 |
|
|
* So we actually may need to place it in an array for later... |
281 |
|
|
*/ |
282 |
|
|
|
283 |
|
|
static int kept_capacity = 0; |
284 |
|
|
static int kept_size = 0; |
285 |
|
|
static char **kept = NULL; |
286 |
|
|
|
287 |
|
|
static void |
288 |
|
|
keep(char *ptr) |
289 |
|
|
{ |
290 |
✓✗ |
18 |
if (kept_capacity <= kept_size) { |
291 |
|
18 |
if (kept_capacity) |
292 |
|
9 |
kept_capacity *= 2; |
293 |
|
|
else |
294 |
|
|
kept_capacity = 50; |
295 |
|
9 |
kept = xreallocarray(kept, kept_capacity, |
296 |
|
|
sizeof(char *), "Out of memory while saving %d strings\n", |
297 |
|
|
kept_capacity); |
298 |
|
9 |
} |
299 |
|
9 |
kept[kept_size++] = ptr; |
300 |
|
9 |
} |
301 |
|
|
|
302 |
|
|
static int |
303 |
|
|
string_in_use(const char *ptr) |
304 |
|
|
{ |
305 |
|
|
int i; |
306 |
✓✗ |
99 |
for (i = 0; i <= sp; i++) { |
307 |
✓✓✓✗
|
54 |
if (sstack[i] == STORAGE_MACRO && mstack[i].sstr == ptr) |
308 |
|
9 |
return 1; |
309 |
|
|
} |
310 |
|
|
return 0; |
311 |
|
9 |
} |
312 |
|
|
|
313 |
|
|
|
314 |
|
|
static void |
315 |
|
|
free_definition(char *ptr) |
316 |
|
|
{ |
317 |
|
|
int i; |
318 |
|
|
|
319 |
|
|
/* first try to free old strings */ |
320 |
✗✓ |
27 |
for (i = 0; i < kept_size; i++) { |
321 |
|
|
if (!string_in_use(kept[i])) { |
322 |
|
|
kept_size--; |
323 |
|
|
free(kept[i]); |
324 |
|
|
if (i != kept_size) |
325 |
|
|
kept[i] = kept[kept_size]; |
326 |
|
|
i--; |
327 |
|
|
} |
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
/* then deal with us */ |
331 |
✓✗ |
9 |
if (string_in_use(ptr)) |
332 |
|
9 |
keep(ptr); |
333 |
|
|
else |
334 |
|
|
free(ptr); |
335 |
|
9 |
} |
336 |
|
|
|