1 /* $OpenBSD: look.c,v 1.24 2014/12/21 09:33:12 espie 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 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
39 * Facility: m4 macro processor
43 #include <sys/types.h>
54 static void *hash_calloc(size_t, size_t, void *);
55 static void hash_free(void *, void *);
56 static void *element_alloc(size_t, void *);
57 static void setup_definition(struct macro_definition
*, const char *,
59 static void free_definition(char *);
60 static void keep(char *);
61 static int string_in_use(const char *);
63 static struct ohash_info macro_info
= {
64 offsetof(struct ndblock
, name
),
65 NULL
, hash_calloc
, hash_free
, element_alloc
};
69 /* Support routines for hash tables. */
71 hash_calloc(size_t n
, size_t s
, void *u __unused
)
73 void *storage
= xcalloc(n
, s
, "hash alloc");
78 hash_free(void *p
, void *u __unused
)
84 element_alloc(size_t s
, void *u __unused
)
86 return xalloc(s
, "element alloc");
92 ohash_init(¯os
, 10, ¯o_info
);
96 * find name in the hash table
99 lookup(const char *name
)
101 return ohash_find(¯os
, ohash_qlookup(¯os
, name
));
104 struct macro_definition
*
105 lookup_macro_definition(const char *name
)
109 p
= ohash_find(¯os
, ohash_qlookup(¯os
, name
));
117 setup_definition(struct macro_definition
*d
, const char *defn
, const char *name
)
121 if (strncmp(defn
, BUILTIN_MARKER
, sizeof(BUILTIN_MARKER
)-1) == 0 &&
122 (p
= macro_getbuiltin(defn
+sizeof(BUILTIN_MARKER
)-1)) != NULL
) {
123 d
->type
= macro_builtin_type(p
);
124 d
->defn
= xstrdup(defn
+sizeof(BUILTIN_MARKER
)-1);
127 d
->defn
= __DECONST(char *, null
);
129 d
->defn
= xstrdup(defn
);
132 if (STREQ(name
, defn
))
137 create_entry(const char *name
)
139 const char *end
= NULL
;
143 i
= ohash_qlookupi(¯os
, name
, &end
);
144 n
= ohash_find(¯os
, i
);
146 n
= ohash_create_entry(¯o_info
, name
, &end
);
147 ohash_insert(¯os
, i
, n
);
148 n
->trace_flags
= FLAG_NO_TRACE
;
149 n
->builtin_type
= MACRTYPE
;
156 macro_define(const char *name
, const char *defn
)
158 ndptr n
= create_entry(name
);
160 if (n
->d
->defn
!= null
)
161 free_definition(n
->d
->defn
);
163 n
->d
= xalloc(sizeof(struct macro_definition
), NULL
);
166 setup_definition(n
->d
, defn
, name
);
170 macro_pushdef(const char *name
, const char *defn
)
173 struct macro_definition
*d
;
175 n
= create_entry(name
);
176 d
= xalloc(sizeof(struct macro_definition
), NULL
);
179 setup_definition(n
->d
, defn
, name
);
183 macro_undefine(const char *name
)
185 ndptr n
= lookup(name
);
187 struct macro_definition
*r
, *r2
;
189 for (r
= n
->d
; r
!= NULL
; r
= r2
) {
200 macro_popdef(const char *name
)
202 ndptr n
= lookup(name
);
205 struct macro_definition
*r
= n
->d
;
216 macro_for_all(void (*f
)(const char *, struct macro_definition
*))
221 for (n
= ohash_first(¯os
, &i
); n
!= NULL
;
222 n
= ohash_next(¯os
, &i
))
228 setup_builtin(const char *name
, unsigned int type
)
233 if (prefix_builtins
) {
234 name2
= xalloc(strlen(name
)+3+1, NULL
);
235 memcpy(name2
, "m4_", 3);
236 memcpy(name2
+ 3, name
, strlen(name
)+1);
238 name2
= xstrdup(name
);
240 n
= create_entry(name2
);
241 n
->builtin_type
= type
;
242 n
->d
= xalloc(sizeof(struct macro_definition
), NULL
);
249 mark_traced(const char *name
, int on
)
256 trace_flags
|= TRACE_ALL
;
258 trace_flags
&= ~TRACE_ALL
;
259 for (p
= ohash_first(¯os
, &i
); p
!= NULL
;
260 p
= ohash_next(¯os
, &i
))
261 p
->trace_flags
= FLAG_NO_TRACE
;
263 p
= create_entry(name
);
269 macro_getbuiltin(const char *name
)
274 if (p
== NULL
|| p
->builtin_type
== MACRTYPE
)
280 /* XXX things are slightly more complicated than they seem.
281 * a macro may actually be "live" (in the middle of an expansion
283 * So we actually may need to place it in an array for later...
286 static int kept_capacity
= 0;
287 static int kept_size
= 0;
288 static char **kept
= NULL
;
293 if (kept_capacity
<= kept_size
) {
298 kept
= xreallocarray(kept
, kept_capacity
,
299 sizeof(char *), "Out of memory while saving %d strings\n",
302 kept
[kept_size
++] = ptr
;
306 string_in_use(const char *ptr
)
309 for (i
= 0; i
<= sp
; i
++) {
310 if (sstack
[i
] == STORAGE_MACRO
&& mstack
[i
].sstr
== ptr
)
318 free_definition(char *ptr
)
322 /* first try to free old strings */
323 for (i
= 0; i
< kept_size
; i
++) {
324 if (!string_in_use(kept
[i
])) {
328 kept
[i
] = kept
[kept_size
];
333 /* then deal with us */
334 if (string_in_use(ptr
))