sbin/*hammer: Add missing braces to conform to code style
[dragonfly.git] / usr.bin / m4 / look.c
blob64f16698894264084d478406f0d3170e27e344e3
1 /* $OpenBSD: look.c,v 1.22 2010/09/07 19:58:09 marco Exp $ */
3 /*
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
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.
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.
34 * $FreeBSD: src/usr.bin/m4/look.c,v 1.10 2012/11/17 01:54:24 svnexp Exp $
38 * look.c
39 * Facility: m4 macro processor
40 * by: oz
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"
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 *,
58 const char *);
60 static struct ohash_info macro_info = {
61 offsetof(struct ndblock, name),
62 NULL, hash_alloc, hash_free, element_alloc };
64 struct ohash macros;
66 /* Support routines for hash tables. */
67 void *
68 hash_alloc(size_t s, __unused void *u)
70 void *storage = xalloc(s, "hash alloc");
71 if (storage)
72 memset(storage, 0, s);
73 return storage;
76 void
77 hash_free(void *p, __unused size_t s, __unused void *u)
79 free(p);
82 void *
83 element_alloc(size_t s, __unused void *u)
85 return xalloc(s, "element alloc");
88 void
89 init_macros(void)
91 ohash_init(&macros, 10, &macro_info);
95 * find name in the hash table
97 ndptr
98 lookup(const char *name)
100 return ohash_find(&macros, ohash_qlookup(&macros, name));
103 struct macro_definition *
104 lookup_macro_definition(const char *name)
106 ndptr p;
108 p = ohash_find(&macros, ohash_qlookup(&macros, name));
109 if (p)
110 return p->d;
111 else
112 return NULL;
115 static void
116 setup_definition(struct macro_definition *d, const char *defn, const char *name)
118 ndptr p;
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);
124 } else {
125 if (!*defn)
126 d->defn = __DECONST(char *, null);
127 else
128 d->defn = xstrdup(defn);
129 d->type = MACRTYPE;
131 if (STREQ(name, defn))
132 d->type |= RECDEF;
135 static ndptr
136 create_entry(const char *name)
138 const char *end = NULL;
139 unsigned int i;
140 ndptr n;
142 i = ohash_qlookupi(&macros, name, &end);
143 n = ohash_find(&macros, i);
144 if (n == NULL) {
145 n = ohash_create_entry(&macro_info, name, &end);
146 ohash_insert(&macros, i, n);
147 n->trace_flags = FLAG_NO_TRACE;
148 n->builtin_type = MACRTYPE;
149 n->d = NULL;
151 return n;
154 void
155 macro_define(const char *name, const char *defn)
157 ndptr n = create_entry(name);
158 if (n->d != NULL) {
159 if (n->d->defn != null)
160 free(n->d->defn);
161 } else {
162 n->d = xalloc(sizeof(struct macro_definition), NULL);
163 n->d->next = NULL;
165 setup_definition(n->d, defn, name);
168 void
169 macro_pushdef(const char *name, const char *defn)
171 ndptr n;
172 struct macro_definition *d;
174 n = create_entry(name);
175 d = xalloc(sizeof(struct macro_definition), NULL);
176 d->next = n->d;
177 n->d = d;
178 setup_definition(n->d, defn, name);
181 void
182 macro_undefine(const char *name)
184 ndptr n = lookup(name);
185 if (n != NULL) {
186 struct macro_definition *r, *r2;
188 for (r = n->d; r != NULL; r = r2) {
189 r2 = r->next;
190 if (r->defn != null)
191 free(r->defn);
192 free(r);
194 n->d = NULL;
198 void
199 macro_popdef(const char *name)
201 ndptr n = lookup(name);
203 if (n != NULL) {
204 struct macro_definition *r = n->d;
205 if (r != NULL) {
206 n->d = r->next;
207 if (r->defn != null)
208 free(r->defn);
209 free(r);
214 void
215 macro_for_all(void (*f)(const char *, struct macro_definition *))
217 ndptr n;
218 unsigned int i;
220 for (n = ohash_first(&macros, &i); n != NULL;
221 n = ohash_next(&macros, &i))
222 if (n->d != NULL)
223 f(n->name, n->d);
226 void
227 setup_builtin(const char *name, unsigned int type)
229 ndptr n;
230 char *name2;
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);
236 } else
237 name2 = xstrdup(name);
239 n = create_entry(name2);
240 n->builtin_type = type;
241 n->d = xalloc(sizeof(struct macro_definition), NULL);
242 n->d->defn = name2;
243 n->d->type = type;
244 n->d->next = NULL;
247 void
248 mark_traced(const char *name, int on)
250 ndptr p;
251 unsigned int i;
253 if (name == NULL) {
254 if (on)
255 trace_flags |= TRACE_ALL;
256 else
257 trace_flags &= ~TRACE_ALL;
258 for (p = ohash_first(&macros, &i); p != NULL;
259 p = ohash_next(&macros, &i))
260 p->trace_flags = FLAG_NO_TRACE;
261 } else {
262 p = create_entry(name);
263 p->trace_flags = on;
267 ndptr
268 macro_getbuiltin(const char *name)
270 ndptr p;
272 p = lookup(name);
273 if (p == NULL || p->builtin_type == MACRTYPE)
274 return NULL;
275 else
276 return p;