1 /* $NetBSD: look.c,v 1.12 2012/03/20 20:34:58 matt Exp $ */
2 /* $OpenBSD: look.c,v 1.21 2009/10/14 17:23:17 sthen Exp $ */
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Ozan Yigit at York University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * Facility: m4 macro processor
41 #if HAVE_NBTOOL_CONFIG_H
42 #include "nbtool_config.h"
44 #include <sys/cdefs.h>
45 __RCSID("$NetBSD: look.c,v 1.12 2012/03/20 20:34:58 matt Exp $");
56 static void *hash_alloc(size_t, void *);
57 static void hash_free(void *, size_t, void *);
58 static void *element_alloc(size_t, void *);
59 static void setup_definition(struct macro_definition
*, const char *,
62 static struct ohash_info macro_info
= {
63 offsetof(struct ndblock
, name
),
64 NULL
, hash_alloc
, hash_free
, element_alloc
};
68 /* Support routines for hash tables. */
70 hash_alloc(size_t s
, void *u UNUSED
)
72 void *storage
= xalloc(s
, "hash alloc");
74 memset(storage
, 0, s
);
79 hash_free(void *p
, size_t s UNUSED
, void *u UNUSED
)
85 element_alloc(size_t s
, void *u UNUSED
)
87 return xalloc(s
, "element alloc");
93 ohash_init(¯os
, 10, ¯o_info
);
97 * find name in the hash table
100 lookup(const char *name
)
102 return ohash_find(¯os
, ohash_qlookup(¯os
, name
));
105 struct macro_definition
*
106 lookup_macro_definition(const char *name
)
110 p
= ohash_find(¯os
, ohash_qlookup(¯os
, name
));
118 setup_definition(struct macro_definition
*d
, const char *defn
, const char *name
)
122 if (strncmp(defn
, BUILTIN_MARKER
, sizeof(BUILTIN_MARKER
)-1) == 0 &&
123 (p
= macro_getbuiltin(defn
+sizeof(BUILTIN_MARKER
)-1)) != NULL
) {
124 d
->type
= macro_builtin_type(p
);
125 d
->defn
= xstrdup(defn
+sizeof(BUILTIN_MARKER
)-1);
128 d
->defn
= xstrdup(null
);
130 d
->defn
= xstrdup(defn
);
133 if (STREQ(name
, defn
))
138 create_entry(const char *name
)
140 const char *end
= NULL
;
144 i
= ohash_qlookupi(¯os
, name
, &end
);
145 n
= ohash_find(¯os
, i
);
147 n
= ohash_create_entry(¯o_info
, name
, &end
);
148 ohash_insert(¯os
, i
, n
);
149 n
->trace_flags
= FLAG_NO_TRACE
;
150 n
->builtin_type
= MACRTYPE
;
157 macro_define(const char *name
, const char *defn
)
159 ndptr n
= create_entry(name
);
161 if (n
->d
->defn
!= null
)
164 n
->d
= xalloc(sizeof(struct macro_definition
), NULL
);
167 setup_definition(n
->d
, defn
, name
);
171 macro_pushdef(const char *name
, const char *defn
)
174 struct macro_definition
*d
;
176 n
= create_entry(name
);
177 d
= xalloc(sizeof(struct macro_definition
), NULL
);
180 setup_definition(n
->d
, defn
, name
);
184 macro_undefine(const char *name
)
186 ndptr n
= lookup(name
);
188 struct macro_definition
*r
, *r2
;
190 for (r
= n
->d
; r
!= NULL
; r
= r2
) {
201 macro_popdef(const char *name
)
203 ndptr n
= lookup(name
);
206 struct macro_definition
*r
= n
->d
;
217 macro_for_all(void (*f
)(const char *, struct macro_definition
*))
222 for (n
= ohash_first(¯os
, &i
); n
!= NULL
;
223 n
= ohash_next(¯os
, &i
))
229 setup_builtin(const char *name
, unsigned int type
)
234 if (prefix_builtins
) {
235 name2
= xalloc(strlen(name
)+3+1, NULL
);
236 memcpy(name2
, "m4_", 3);
237 memcpy(name2
+ 3, name
, strlen(name
)+1);
239 name2
= xstrdup(name
);
241 n
= create_entry(name2
);
242 n
->builtin_type
= type
;
243 n
->d
= xalloc(sizeof(struct macro_definition
), NULL
);
250 mark_traced(const char *name
, int on
)
257 trace_flags
|= TRACE_ALL
;
259 trace_flags
&= ~TRACE_ALL
;
260 for (p
= ohash_first(¯os
, &i
); p
!= NULL
;
261 p
= ohash_next(¯os
, &i
))
262 p
->trace_flags
= FLAG_NO_TRACE
;
264 p
= create_entry(name
);
270 macro_getbuiltin(const char *name
)
275 if (p
== NULL
|| p
->builtin_type
== MACRTYPE
)