1 /* $OpenBSD: look.c,v 1.23 2014/05/12 19:11:19 espie Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Ozan Yigit at York University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
39 * Facility: m4 macro processor
43 #include <sys/types.h>
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 *,
60 static struct ohash_info macro_info = {
61 offsetof(struct ndblock, name),
62 NULL, hash_calloc, hash_free, element_alloc };
66 /* Support routines for hash tables. */
68 hash_calloc(size_t n, size_t s, void *u __unused)
70 void *storage = xcalloc(n, s, "hash alloc");
75 hash_free(void *p, void *u __unused)
81 element_alloc(size_t s, void *u __unused)
83 return xalloc(s, "element alloc");
89 ohash_init(¯os, 10, ¯o_info);
93 * find name in the hash table
96 lookup(const char *name)
98 return ohash_find(¯os, ohash_qlookup(¯os, name));
101 struct macro_definition *
102 lookup_macro_definition(const char *name)
106 p = ohash_find(¯os, ohash_qlookup(¯os, name));
114 setup_definition(struct macro_definition *d, const char *defn, const char *name)
118 if (strncmp(defn, BUILTIN_MARKER, sizeof(BUILTIN_MARKER)-1) == 0 &&
119 (p = macro_getbuiltin(defn+sizeof(BUILTIN_MARKER)-1)) != NULL) {
120 d->type = macro_builtin_type(p);
121 d->defn = xstrdup(defn+sizeof(BUILTIN_MARKER)-1);
124 d->defn = __DECONST(char *, null);
126 d->defn = xstrdup(defn);
129 if (STREQ(name, defn))
134 create_entry(const char *name)
136 const char *end = NULL;
140 i = ohash_qlookupi(¯os, name, &end);
141 n = ohash_find(¯os, i);
143 n = ohash_create_entry(¯o_info, name, &end);
144 ohash_insert(¯os, i, n);
145 n->trace_flags = FLAG_NO_TRACE;
146 n->builtin_type = MACRTYPE;
153 macro_define(const char *name, const char *defn)
155 ndptr n = create_entry(name);
157 if (n->d->defn != null)
160 n->d = xalloc(sizeof(struct macro_definition), NULL);
163 setup_definition(n->d, defn, name);
167 macro_pushdef(const char *name, const char *defn)
170 struct macro_definition *d;
172 n = create_entry(name);
173 d = xalloc(sizeof(struct macro_definition), NULL);
176 setup_definition(n->d, defn, name);
180 macro_undefine(const char *name)
182 ndptr n = lookup(name);
184 struct macro_definition *r, *r2;
186 for (r = n->d; r != NULL; r = r2) {
197 macro_popdef(const char *name)
199 ndptr n = lookup(name);
202 struct macro_definition *r = n->d;
213 macro_for_all(void (*f)(const char *, struct macro_definition *))
218 for (n = ohash_first(¯os, &i); n != NULL;
219 n = ohash_next(¯os, &i))
225 setup_builtin(const char *name, unsigned int type)
230 if (prefix_builtins) {
231 name2 = xalloc(strlen(name)+3+1, NULL);
232 memcpy(name2, "m4_", 3);
233 memcpy(name2 + 3, name, strlen(name)+1);
235 name2 = xstrdup(name);
237 n = create_entry(name2);
238 n->builtin_type = type;
239 n->d = xalloc(sizeof(struct macro_definition), NULL);
246 mark_traced(const char *name, int on)
253 trace_flags |= TRACE_ALL;
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;
260 p = create_entry(name);
266 macro_getbuiltin(const char *name)
271 if (p == NULL || p->builtin_type == MACRTYPE)