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