Annotation of src/usr.bin/m4/look.c, Revision 1.22
1.22 ! marco 1: /* $OpenBSD: look.c,v 1.21 2009/10/14 17:23:17 sthen Exp $ */
1.2 deraadt 2:
1.1 deraadt 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.
1.11 millert 18: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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>
1.18 espie 43: #include <stdint.h>
1.3 espie 44: #include <stddef.h>
1.1 deraadt 45: #include <string.h>
1.13 espie 46: #include <ohash.h>
1.1 deraadt 47: #include "mdef.h"
48: #include "stdd.h"
49: #include "extern.h"
50:
1.15 espie 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 *);
1.22 ! marco 54: static void setup_definition(struct macro_definition *, const char *,
1.13 espie 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:
1.15 espie 61: struct ohash macros;
62:
63: /* Support routines for hash tables. */
64: void *
65: hash_alloc(s, u)
66: size_t s;
1.22 ! marco 67: void *u UNUSED;
1.15 espie 68: {
1.16 espie 69: void *storage = xalloc(s, "hash alloc");
1.15 espie 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;
1.22 ! marco 79: void *u UNUSED;
1.15 espie 80: {
81: free(p);
82: }
83:
84: void *
85: element_alloc(s, u)
86: size_t s;
1.22 ! marco 87: void *u UNUSED;
1.15 espie 88: {
1.16 espie 89: return xalloc(s, "element alloc");
1.15 espie 90: }
1.13 espie 91:
92: void
93: init_macros()
94: {
1.15 espie 95: ohash_init(¯os, 10, ¯o_info);
1.1 deraadt 96: }
97:
98: /*
99: * find name in the hash table
100: */
1.22 ! marco 101: ndptr
1.10 espie 102: lookup(const char *name)
1.1 deraadt 103: {
1.13 espie 104: return ohash_find(¯os, ohash_qlookup(¯os, name));
1.12 espie 105: }
106:
107: struct macro_definition *
108: lookup_macro_definition(const char *name)
109: {
110: ndptr p;
111:
1.15 espie 112: p = ohash_find(¯os, ohash_qlookup(¯os, name));
1.12 espie 113: if (p)
1.13 espie 114: return p->d;
1.12 espie 115: else
116: return NULL;
117: }
118:
1.22 ! marco 119: static void
1.13 espie 120: setup_definition(struct macro_definition *d, const char *defn, const char *name)
1.12 espie 121: {
1.14 espie 122: ndptr p;
1.12 espie 123:
1.13 espie 124: if (strncmp(defn, BUILTIN_MARKER, sizeof(BUILTIN_MARKER)-1) == 0 &&
1.14 espie 125: (p = macro_getbuiltin(defn+sizeof(BUILTIN_MARKER)-1)) != NULL) {
126: d->type = macro_builtin_type(p);
1.13 espie 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(¯os, name, &end);
147: n = ohash_find(¯os, i);
148: if (n == NULL) {
149: n = ohash_create_entry(¯o_info, name, &end);
150: ohash_insert(¯os, i, n);
1.15 espie 151: n->trace_flags = FLAG_NO_TRACE;
1.14 espie 152: n->builtin_type = MACRTYPE;
1.13 espie 153: n->d = NULL;
1.12 espie 154: }
1.13 espie 155: return n;
1.12 espie 156: }
157:
158: void
159: macro_define(const char *name, const char *defn)
160: {
1.13 espie 161: ndptr n = create_entry(name);
162: if (n->d != NULL) {
163: if (n->d->defn != null)
164: free(n->d->defn);
165: } else {
1.16 espie 166: n->d = xalloc(sizeof(struct macro_definition), NULL);
1.13 espie 167: n->d->next = NULL;
168: }
169: setup_definition(n->d, defn, name);
1.12 espie 170: }
171:
172: void
173: macro_pushdef(const char *name, const char *defn)
174: {
1.13 espie 175: ndptr n;
176: struct macro_definition *d;
1.22 ! marco 177:
1.13 espie 178: n = create_entry(name);
1.16 espie 179: d = xalloc(sizeof(struct macro_definition), NULL);
1.13 espie 180: d->next = n->d;
181: n->d = d;
182: setup_definition(n->d, defn, name);
1.12 espie 183: }
184:
185: void
186: macro_undefine(const char *name)
187: {
1.13 espie 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: }
1.12 espie 200: }
201:
202: void
203: macro_popdef(const char *name)
204: {
1.13 espie 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: }
1.12 espie 216: }
217:
218: void
219: macro_for_all(void (*f)(const char *, struct macro_definition *))
220: {
1.13 espie 221: ndptr n;
222: unsigned int i;
1.12 espie 223:
1.22 ! marco 224: for (n = ohash_first(¯os, &i); n != NULL;
1.13 espie 225: n = ohash_next(¯os, &i))
1.19 guenther 226: if (n->d != NULL)
227: f(n->name, n->d);
1.12 espie 228: }
229:
1.22 ! marco 230: void
1.12 espie 231: setup_builtin(const char *name, unsigned int type)
232: {
1.13 espie 233: ndptr n;
1.20 sthen 234: char *name2;
1.12 espie 235:
1.21 sthen 236: if (prefix_builtins) {
1.20 sthen 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);
1.14 espie 244: n->builtin_type = type;
1.16 espie 245: n->d = xalloc(sizeof(struct macro_definition), NULL);
1.20 sthen 246: n->d->defn = name2;
1.13 espie 247: n->d->type = type;
248: n->d->next = NULL;
1.12 espie 249: }
250:
1.15 espie 251: void
252: mark_traced(const char *name, int on)
1.12 espie 253: {
1.15 espie 254: ndptr p;
255: unsigned int i;
1.12 espie 256:
1.15 espie 257: if (name == NULL) {
258: if (on)
259: trace_flags |= TRACE_ALL;
260: else
261: trace_flags &= ~TRACE_ALL;
1.22 ! marco 262: for (p = ohash_first(¯os, &i); p != NULL;
1.15 espie 263: p = ohash_next(¯os, &i))
1.22 ! marco 264: p->trace_flags = FLAG_NO_TRACE;
1.15 espie 265: } else {
266: p = create_entry(name);
267: p->trace_flags = on;
268: }
1.1 deraadt 269: }
1.14 espie 270:
1.22 ! marco 271: ndptr
1.14 espie 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: }