1 /* $OpenBSD: look.c,v 1.22 2010/09/07 19:58:09 marco 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 * $FreeBSD: src/usr.bin/m4/look.c,v 1.10 2012/11/17 01:54:24 svnexp Exp $
39 * Facility: m4 macro processor
43 #include <sys/types.h>
54 static void *hash_alloc(size_t, void *);
55 static void hash_free(void *, size_t, 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_alloc, hash_free, element_alloc };
66 /* Support routines for hash tables. */
68 hash_alloc(size_t s, __unused void *u)
70 void *storage = xalloc(s, "hash alloc");
72 memset(storage, 0, s);
77 hash_free(void *p, __unused size_t s, __unused void *u)
83 element_alloc(size_t s, __unused void *u)
85 return xalloc(s, "element alloc");
91 ohash_init(¯os, 10, ¯o_info);
95 * find name in the hash table
98 lookup(const char *name)
100 return ohash_find(¯os, ohash_qlookup(¯os, name));
103 struct macro_definition *
104 lookup_macro_definition(const char *name)
108 p = ohash_find(¯os, ohash_qlookup(¯os, name));
116 setup_definition(struct macro_definition *d, const char *defn, const char *name)
120 if (strncmp(defn, BUILTIN_MARKER, sizeof(BUILTIN_MARKER) - 1) == 0 &&
121 (p = macro_getbuiltin(defn + sizeof(BUILTIN_MARKER) - 1)) != NULL) {
122 d->type = macro_builtin_type(p);
123 d->defn = xstrdup(defn + sizeof(BUILTIN_MARKER) - 1);
126 d->defn = __DECONST(char *, null);
128 d->defn = xstrdup(defn);
131 if (STREQ(name, defn))
136 create_entry(const char *name)
138 const char *end = NULL;
142 i = ohash_qlookupi(¯os, name, &end);
143 n = ohash_find(¯os, i);
145 n = ohash_create_entry(¯o_info, name, &end);
146 ohash_insert(¯os, i, n);
147 n->trace_flags = FLAG_NO_TRACE;
148 n->builtin_type = MACRTYPE;
155 macro_define(const char *name, const char *defn)
157 ndptr n = create_entry(name);
159 if (n->d->defn != null)
162 n->d = xalloc(sizeof(struct macro_definition), NULL);
165 setup_definition(n->d, defn, name);
169 macro_pushdef(const char *name, const char *defn)
172 struct macro_definition *d;
174 n = create_entry(name);
175 d = xalloc(sizeof(struct macro_definition), NULL);
178 setup_definition(n->d, defn, name);
182 macro_undefine(const char *name)
184 ndptr n = lookup(name);
186 struct macro_definition *r, *r2;
188 for (r = n->d; r != NULL; r = r2) {
199 macro_popdef(const char *name)
201 ndptr n = lookup(name);
204 struct macro_definition *r = n->d;
215 macro_for_all(void (*f)(const char *, struct macro_definition *))
220 for (n = ohash_first(¯os, &i); n != NULL;
221 n = ohash_next(¯os, &i))
227 setup_builtin(const char *name, unsigned int type)
232 if (prefix_builtins) {
233 name2 = xalloc(strlen(name) + 3 + 1, NULL);
234 memcpy(name2, "m4_", 3);
235 memcpy(name2 + 3, name, strlen(name) + 1);
237 name2 = xstrdup(name);
239 n = create_entry(name2);
240 n->builtin_type = type;
241 n->d = xalloc(sizeof(struct macro_definition), NULL);
248 mark_traced(const char *name, int on)
255 trace_flags |= TRACE_ALL;
257 trace_flags &= ~TRACE_ALL;
258 for (p = ohash_first(¯os, &i); p != NULL;
259 p = ohash_next(¯os, &i))
260 p->trace_flags = FLAG_NO_TRACE;
262 p = create_entry(name);
268 macro_getbuiltin(const char *name)
273 if (p == NULL || p->builtin_type == MACRTYPE)