1 /* $OpenBSD: look.c,v 1.24 2014/12/21 09:33:12 espie Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 1989, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Ozan Yigit at York University.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
41 * Facility: m4 macro processor
45 #include <sys/types.h>
56 static void *hash_calloc(size_t, size_t, void *);
57 static void hash_free(void *, void *);
58 static void *element_alloc(size_t, void *);
59 static void setup_definition(struct macro_definition *, const char *,
61 static void free_definition(char *);
62 static void keep(char *);
63 static int string_in_use(const char *);
65 static struct ohash_info macro_info = {
66 offsetof(struct ndblock, name),
67 NULL, hash_calloc, hash_free, element_alloc };
71 /* Support routines for hash tables. */
73 hash_calloc(size_t n, size_t s, void *u __unused)
75 void *storage = xcalloc(n, s, "hash alloc");
80 hash_free(void *p, void *u __unused)
86 element_alloc(size_t s, void *u __unused)
88 return xalloc(s, "element alloc");
94 ohash_init(¯os, 10, ¯o_info);
98 * find name in the hash table
101 lookup(const char *name)
103 return ohash_find(¯os, ohash_qlookup(¯os, name));
106 struct macro_definition *
107 lookup_macro_definition(const char *name)
111 p = ohash_find(¯os, ohash_qlookup(¯os, name));
119 setup_definition(struct macro_definition *d, const char *defn, const char *name)
123 if (strncmp(defn, BUILTIN_MARKER, sizeof(BUILTIN_MARKER)-1) == 0 &&
124 (p = macro_getbuiltin(defn+sizeof(BUILTIN_MARKER)-1)) != NULL) {
125 d->type = macro_builtin_type(p);
126 d->defn = xstrdup(defn+sizeof(BUILTIN_MARKER)-1);
129 d->defn = __DECONST(char *, null);
131 d->defn = xstrdup(defn);
134 if (STREQ(name, defn))
139 create_entry(const char *name)
141 const char *end = NULL;
145 i = ohash_qlookupi(¯os, name, &end);
146 n = ohash_find(¯os, i);
148 n = ohash_create_entry(¯o_info, name, &end);
149 ohash_insert(¯os, i, n);
150 n->trace_flags = FLAG_NO_TRACE;
151 n->builtin_type = MACRTYPE;
158 macro_define(const char *name, const char *defn)
160 ndptr n = create_entry(name);
162 if (n->d->defn != null)
163 free_definition(n->d->defn);
165 n->d = xalloc(sizeof(struct macro_definition), NULL);
168 setup_definition(n->d, defn, name);
172 macro_pushdef(const char *name, const char *defn)
175 struct macro_definition *d;
177 n = create_entry(name);
178 d = xalloc(sizeof(struct macro_definition), NULL);
181 setup_definition(n->d, defn, name);
185 macro_undefine(const char *name)
187 ndptr n = lookup(name);
189 struct macro_definition *r, *r2;
191 for (r = n->d; r != NULL; r = r2) {
202 macro_popdef(const char *name)
204 ndptr n = lookup(name);
207 struct macro_definition *r = n->d;
218 macro_for_all(void (*f)(const char *, struct macro_definition *))
223 for (n = ohash_first(¯os, &i); n != NULL;
224 n = ohash_next(¯os, &i))
230 setup_builtin(const char *name, unsigned int type)
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);
240 name2 = xstrdup(name);
242 n = create_entry(name2);
243 n->builtin_type = type;
244 n->d = xalloc(sizeof(struct macro_definition), NULL);
251 mark_traced(const char *name, int on)
258 trace_flags |= TRACE_ALL;
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;
265 p = create_entry(name);
271 macro_getbuiltin(const char *name)
276 if (p == NULL || p->builtin_type == MACRTYPE)
282 /* XXX things are slightly more complicated than they seem.
283 * a macro may actually be "live" (in the middle of an expansion
285 * So we actually may need to place it in an array for later...
288 static int kept_capacity = 0;
289 static int kept_size = 0;
290 static char **kept = NULL;
295 if (kept_capacity <= kept_size) {
300 kept = xreallocarray(kept, kept_capacity,
301 sizeof(char *), "Out of memory while saving %d strings\n",
304 kept[kept_size++] = ptr;
308 string_in_use(const char *ptr)
311 for (i = 0; i <= sp; i++) {
312 if (sstack[i] == STORAGE_MACRO && mstack[i].sstr == ptr)
320 free_definition(char *ptr)
324 /* first try to free old strings */
325 for (i = 0; i < kept_size; i++) {
326 if (!string_in_use(kept[i])) {
330 kept[i] = kept[kept_size];
335 /* then deal with us */
336 if (string_in_use(ptr))