Commit | Line | Data |
---|---|---|
4afad3d8 PA |
1 | /* $OpenBSD: look.c,v 1.22 2010/09/07 19:58:09 marco Exp $ */ |
2 | ||
984263bc MD |
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. | |
4afad3d8 | 18 | * 3. Neither the name of the University nor the names of its contributors |
984263bc MD |
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. | |
1de703da | 33 | * |
4afad3d8 | 34 | * $FreeBSD: src/usr.bin/m4/look.c,v 1.10 2012/11/17 01:54:24 svnexp Exp $ |
984263bc MD |
35 | */ |
36 | ||
984263bc MD |
37 | /* |
38 | * look.c | |
39 | * Facility: m4 macro processor | |
40 | * by: oz | |
41 | */ | |
42 | ||
43 | #include <sys/types.h> | |
44 | #include <stdio.h> | |
45 | #include <stdlib.h> | |
4afad3d8 | 46 | #include <stdint.h> |
984263bc MD |
47 | #include <stddef.h> |
48 | #include <string.h> | |
4afad3d8 | 49 | #include <ohash.h> |
984263bc MD |
50 | #include "mdef.h" |
51 | #include "stdd.h" | |
52 | #include "extern.h" | |
53 | ||
4afad3d8 PA |
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 *); | |
59 | ||
60 | static struct ohash_info macro_info = { | |
61 | offsetof(struct ndblock, name), | |
62 | NULL, hash_alloc, hash_free, element_alloc }; | |
63 | ||
64 | struct ohash macros; | |
65 | ||
66 | /* Support routines for hash tables. */ | |
67 | void * | |
68 | hash_alloc(size_t s, __unused void *u) | |
69 | { | |
70 | void *storage = xalloc(s, "hash alloc"); | |
71 | if (storage) | |
72 | memset(storage, 0, s); | |
73 | return storage; | |
74 | } | |
75 | ||
76 | void | |
77 | hash_free(void *p, __unused size_t s, __unused void *u) | |
78 | { | |
79 | free(p); | |
80 | } | |
81 | ||
82 | void * | |
83 | element_alloc(size_t s, __unused void *u) | |
84 | { | |
85 | return xalloc(s, "element alloc"); | |
86 | } | |
984263bc | 87 | |
4afad3d8 PA |
88 | void |
89 | init_macros(void) | |
984263bc | 90 | { |
4afad3d8 | 91 | ohash_init(¯os, 10, ¯o_info); |
984263bc MD |
92 | } |
93 | ||
94 | /* | |
95 | * find name in the hash table | |
96 | */ | |
10c3f15b | 97 | ndptr |
984263bc | 98 | lookup(const char *name) |
4afad3d8 PA |
99 | { |
100 | return ohash_find(¯os, ohash_qlookup(¯os, name)); | |
101 | } | |
102 | ||
103 | struct macro_definition * | |
104 | lookup_macro_definition(const char *name) | |
984263bc MD |
105 | { |
106 | ndptr p; | |
984263bc | 107 | |
4afad3d8 PA |
108 | p = ohash_find(¯os, ohash_qlookup(¯os, name)); |
109 | if (p) | |
110 | return p->d; | |
111 | else | |
112 | return NULL; | |
984263bc MD |
113 | } |
114 | ||
4afad3d8 PA |
115 | static void |
116 | setup_definition(struct macro_definition *d, const char *defn, const char *name) | |
984263bc | 117 | { |
984263bc MD |
118 | ndptr p; |
119 | ||
4afad3d8 PA |
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; | |
130 | } | |
131 | if (STREQ(name, defn)) | |
132 | d->type |= RECDEF; | |
984263bc MD |
133 | } |
134 | ||
4afad3d8 PA |
135 | static ndptr |
136 | create_entry(const char *name) | |
984263bc | 137 | { |
4afad3d8 PA |
138 | const char *end = NULL; |
139 | unsigned int i; | |
140 | ndptr n; | |
141 | ||
142 | i = ohash_qlookupi(¯os, name, &end); | |
143 | n = ohash_find(¯os, i); | |
144 | if (n == NULL) { | |
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; | |
149 | n->d = NULL; | |
150 | } | |
151 | return n; | |
984263bc MD |
152 | } |
153 | ||
984263bc | 154 | void |
4afad3d8 PA |
155 | macro_define(const char *name, const char *defn) |
156 | { | |
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; | |
164 | } | |
165 | setup_definition(n->d, defn, name); | |
166 | } | |
167 | ||
168 | void | |
169 | macro_pushdef(const char *name, const char *defn) | |
170 | { | |
171 | ndptr n; | |
172 | struct macro_definition *d; | |
173 | ||
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); | |
179 | } | |
180 | ||
181 | void | |
182 | macro_undefine(const char *name) | |
183 | { | |
184 | ndptr n = lookup(name); | |
185 | if (n != NULL) { | |
186 | struct macro_definition *r, *r2; | |
187 | ||
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); | |
984263bc | 193 | } |
4afad3d8 PA |
194 | n->d = NULL; |
195 | } | |
196 | } | |
197 | ||
198 | void | |
199 | macro_popdef(const char *name) | |
200 | { | |
201 | ndptr n = lookup(name); | |
202 | ||
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); | |
984263bc MD |
210 | } |
211 | } | |
212 | } | |
4afad3d8 PA |
213 | |
214 | void | |
215 | macro_for_all(void (*f)(const char *, struct macro_definition *)) | |
216 | { | |
217 | ndptr n; | |
218 | unsigned int i; | |
219 | ||
220 | for (n = ohash_first(¯os, &i); n != NULL; | |
221 | n = ohash_next(¯os, &i)) | |
222 | if (n->d != NULL) | |
223 | f(n->name, n->d); | |
224 | } | |
225 | ||
226 | void | |
227 | setup_builtin(const char *name, unsigned int type) | |
228 | { | |
229 | ndptr n; | |
230 | char *name2; | |
231 | ||
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); | |
238 | ||
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; | |
245 | } | |
246 | ||
247 | void | |
248 | mark_traced(const char *name, int on) | |
249 | { | |
250 | ndptr p; | |
251 | unsigned int i; | |
252 | ||
253 | if (name == NULL) { | |
254 | if (on) | |
255 | trace_flags |= TRACE_ALL; | |
256 | else | |
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; | |
261 | } else { | |
262 | p = create_entry(name); | |
263 | p->trace_flags = on; | |
264 | } | |
265 | } | |
266 | ||
267 | ndptr | |
268 | macro_getbuiltin(const char *name) | |
269 | { | |
270 | ndptr p; | |
271 | ||
272 | p = lookup(name); | |
273 | if (p == NULL || p->builtin_type == MACRTYPE) | |
274 | return NULL; | |
275 | else | |
276 | return p; | |
277 | } |