xref: /openbsd/usr.bin/m4/look.c (revision 404b540a)
1 /*	$OpenBSD: look.c,v 1.21 2009/10/14 17:23:17 sthen Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Ozan Yigit at York University.
9  *
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.
21  *
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.
33  */
34 
35 /*
36  * look.c
37  * Facility: m4 macro processor
38  * by: oz
39  */
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdint.h>
44 #include <stddef.h>
45 #include <string.h>
46 #include <ohash.h>
47 #include "mdef.h"
48 #include "stdd.h"
49 #include "extern.h"
50 
51 static void *hash_alloc(size_t, void *);
52 static void hash_free(void *, size_t, void *);
53 static void *element_alloc(size_t, void *);
54 static void setup_definition(struct macro_definition *, const char *,
55     const char *);
56 
57 static struct ohash_info macro_info = {
58 	offsetof(struct ndblock, name),
59 	NULL, hash_alloc, hash_free, element_alloc };
60 
61 struct ohash macros;
62 
63 /* Support routines for hash tables.  */
64 void *
65 hash_alloc(s, u)
66 	size_t s;
67 	void *u 	UNUSED;
68 {
69 	void *storage = xalloc(s, "hash alloc");
70 	if (storage)
71 		memset(storage, 0, s);
72 	return storage;
73 }
74 
75 void
76 hash_free(p, s, u)
77 	void *p;
78 	size_t s	UNUSED;
79 	void *u 	UNUSED;
80 {
81 	free(p);
82 }
83 
84 void *
85 element_alloc(s, u)
86 	size_t s;
87 	void *u 	UNUSED;
88 {
89 	return xalloc(s, "element alloc");
90 }
91 
92 void
93 init_macros()
94 {
95 	ohash_init(&macros, 10, &macro_info);
96 }
97 
98 /*
99  * find name in the hash table
100  */
101 ndptr
102 lookup(const char *name)
103 {
104 	return ohash_find(&macros, ohash_qlookup(&macros, name));
105 }
106 
107 struct macro_definition *
108 lookup_macro_definition(const char *name)
109 {
110 	ndptr p;
111 
112 	p = ohash_find(&macros, ohash_qlookup(&macros, name));
113 	if (p)
114 		return p->d;
115 	else
116 		return NULL;
117 }
118 
119 static void
120 setup_definition(struct macro_definition *d, const char *defn, const char *name)
121 {
122 	ndptr p;
123 
124 	if (strncmp(defn, BUILTIN_MARKER, sizeof(BUILTIN_MARKER)-1) == 0 &&
125 	    (p = macro_getbuiltin(defn+sizeof(BUILTIN_MARKER)-1)) != NULL) {
126 		d->type = macro_builtin_type(p);
127 		d->defn = xstrdup(defn+sizeof(BUILTIN_MARKER)-1);
128 	} else {
129 		if (!*defn)
130 			d->defn = null;
131 		else
132 			d->defn = xstrdup(defn);
133 		d->type = MACRTYPE;
134 	}
135 	if (STREQ(name, defn))
136 		d->type |= RECDEF;
137 }
138 
139 static ndptr
140 create_entry(const char *name)
141 {
142 	const char *end = NULL;
143 	unsigned int i;
144 	ndptr n;
145 
146 	i = ohash_qlookupi(&macros, name, &end);
147 	n = ohash_find(&macros, i);
148 	if (n == NULL) {
149 		n = ohash_create_entry(&macro_info, name, &end);
150 		ohash_insert(&macros, i, n);
151 		n->trace_flags = FLAG_NO_TRACE;
152 		n->builtin_type = MACRTYPE;
153 		n->d = NULL;
154 	}
155 	return n;
156 }
157 
158 void
159 macro_define(const char *name, const char *defn)
160 {
161 	ndptr n = create_entry(name);
162 	if (n->d != NULL) {
163 		if (n->d->defn != null)
164 			free(n->d->defn);
165 	} else {
166 		n->d = xalloc(sizeof(struct macro_definition), NULL);
167 		n->d->next = NULL;
168 	}
169 	setup_definition(n->d, defn, name);
170 }
171 
172 void
173 macro_pushdef(const char *name, const char *defn)
174 {
175 	ndptr n;
176 	struct macro_definition *d;
177 
178 	n = create_entry(name);
179 	d = xalloc(sizeof(struct macro_definition), NULL);
180 	d->next = n->d;
181 	n->d = d;
182 	setup_definition(n->d, defn, name);
183 }
184 
185 void
186 macro_undefine(const char *name)
187 {
188 	ndptr n = lookup(name);
189 	if (n != NULL) {
190 		struct macro_definition *r, *r2;
191 
192 		for (r = n->d; r != NULL; r = r2) {
193 			r2 = r->next;
194 			if (r->defn != null)
195 				free(r->defn);
196 			free(r);
197 		}
198 		n->d = NULL;
199 	}
200 }
201 
202 void
203 macro_popdef(const char *name)
204 {
205 	ndptr n = lookup(name);
206 
207 	if (n != NULL) {
208 		struct macro_definition *r = n->d;
209 		if (r != NULL) {
210 			n->d = r->next;
211 			if (r->defn != null)
212 				free(r->defn);
213 			free(r);
214 		}
215 	}
216 }
217 
218 void
219 macro_for_all(void (*f)(const char *, struct macro_definition *))
220 {
221 	ndptr n;
222 	unsigned int i;
223 
224 	for (n = ohash_first(&macros, &i); n != NULL;
225 	    n = ohash_next(&macros, &i))
226 		if (n->d != NULL)
227 			f(n->name, n->d);
228 }
229 
230 void
231 setup_builtin(const char *name, unsigned int type)
232 {
233 	ndptr n;
234 	char *name2;
235 
236 	if (prefix_builtins) {
237 		name2 = xalloc(strlen(name)+3+1, NULL);
238 		memcpy(name2, "m4_", 3);
239 		memcpy(name2 + 3, name, strlen(name)+1);
240 	} else
241 		name2 = xstrdup(name);
242 
243 	n = create_entry(name2);
244 	n->builtin_type = type;
245 	n->d = xalloc(sizeof(struct macro_definition), NULL);
246 	n->d->defn = name2;
247 	n->d->type = type;
248 	n->d->next = NULL;
249 }
250 
251 void
252 mark_traced(const char *name, int on)
253 {
254 	ndptr p;
255 	unsigned int i;
256 
257 	if (name == NULL) {
258 		if (on)
259 			trace_flags |= TRACE_ALL;
260 		else
261 			trace_flags &= ~TRACE_ALL;
262 		for (p = ohash_first(&macros, &i); p != NULL;
263 		    p = ohash_next(&macros, &i))
264 		    	p->trace_flags = FLAG_NO_TRACE;
265 	} else {
266 		p = create_entry(name);
267 		p->trace_flags = on;
268 	}
269 }
270 
271 ndptr
272 macro_getbuiltin(const char *name)
273 {
274 	ndptr p;
275 
276 	p = lookup(name);
277 	if (p == NULL || p->builtin_type == MACRTYPE)
278 		return NULL;
279 	else
280 		return p;
281 }
282 
283