Annotation of src/usr.bin/m4/look.c, Revision 1.14
1.14 ! espie 1: /* $OpenBSD: look.c,v 1.13 2003/06/30 22:10:21 espie 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: #ifndef lint
36: static char sccsid[] = "@(#)look.c 8.1 (Berkeley) 6/6/93";
37: #endif /* not lint */
38:
39: /*
40: * look.c
41: * Facility: m4 macro processor
42: * by: oz
43: */
44:
45: #include <sys/types.h>
46: #include <stdio.h>
47: #include <stdlib.h>
1.3 espie 48: #include <stddef.h>
1.1 deraadt 49: #include <string.h>
1.13 espie 50: #include <ohash.h>
1.1 deraadt 51: #include "mdef.h"
52: #include "stdd.h"
53: #include "extern.h"
54:
1.13 espie 55: struct ndblock { /* hashtable structure */
1.14 ! espie 56: unsigned int builtin_type;
1.13 espie 57: struct macro_definition *d;
58: char name[1]; /* entry name.. */
1.12 espie 59: };
60:
1.13 espie 61: extern void *hash_alloc(size_t, void *);
62: extern void hash_free(void *, size_t, void *);
63: extern void *element_alloc(size_t, void *);
64: static void setup_definition(struct macro_definition *, const char *,
65: const char *);
66:
67: static struct ohash_info macro_info = {
68: offsetof(struct ndblock, name),
69: NULL, hash_alloc, hash_free, element_alloc };
70:
71: static struct ohash macros;
72:
73: void
74: init_macros()
75: {
76: ohash_init(¯os, 7, ¯o_info);
1.1 deraadt 77: }
78:
79: /*
80: * find name in the hash table
81: */
82: ndptr
1.10 espie 83: lookup(const char *name)
1.1 deraadt 84: {
1.13 espie 85: return ohash_find(¯os, ohash_qlookup(¯os, name));
1.12 espie 86: }
87:
88: struct macro_definition *
89: lookup_macro_definition(const char *name)
90: {
91: ndptr p;
92:
93: p = lookup(name);
94: if (p)
1.13 espie 95: return p->d;
1.12 espie 96: else
97: return NULL;
98: }
99:
100: static void
1.13 espie 101: setup_definition(struct macro_definition *d, const char *defn, const char *name)
1.12 espie 102: {
1.14 ! espie 103: ndptr p;
1.12 espie 104:
1.13 espie 105: if (strncmp(defn, BUILTIN_MARKER, sizeof(BUILTIN_MARKER)-1) == 0 &&
1.14 ! espie 106: (p = macro_getbuiltin(defn+sizeof(BUILTIN_MARKER)-1)) != NULL) {
! 107: d->type = macro_builtin_type(p);
1.13 espie 108: d->defn = xstrdup(defn+sizeof(BUILTIN_MARKER)-1);
109: } else {
110: if (!*defn)
111: d->defn = null;
112: else
113: d->defn = xstrdup(defn);
114: d->type = MACRTYPE;
115: }
116: if (STREQ(name, defn))
117: d->type |= RECDEF;
118: }
119:
120: static ndptr
121: create_entry(const char *name)
122: {
123: const char *end = NULL;
124: unsigned int i;
125: ndptr n;
126:
127: i = ohash_qlookupi(¯os, name, &end);
128: n = ohash_find(¯os, i);
129: if (n == NULL) {
130: n = ohash_create_entry(¯o_info, name, &end);
131: ohash_insert(¯os, i, n);
1.14 ! espie 132: n->builtin_type = MACRTYPE;
1.13 espie 133: n->d = NULL;
1.12 espie 134: }
1.13 espie 135: return n;
1.12 espie 136: }
137:
138: void
139: macro_define(const char *name, const char *defn)
140: {
1.13 espie 141: ndptr n = create_entry(name);
142: if (n->d != NULL) {
143: if (n->d->defn != null)
144: free(n->d->defn);
145: } else {
146: n->d = xalloc(sizeof(struct macro_definition));
147: n->d->next = NULL;
148: }
149: setup_definition(n->d, defn, name);
1.12 espie 150: }
151:
152: void
153: macro_pushdef(const char *name, const char *defn)
154: {
1.13 espie 155: ndptr n;
156: struct macro_definition *d;
157:
158: n = create_entry(name);
159: d = xalloc(sizeof(struct macro_definition));
160: d->next = n->d;
161: n->d = d;
162: setup_definition(n->d, defn, name);
1.12 espie 163: }
164:
165: void
166: macro_undefine(const char *name)
167: {
1.13 espie 168: ndptr n = lookup(name);
169: if (n != NULL) {
170: struct macro_definition *r, *r2;
171:
172: for (r = n->d; r != NULL; r = r2) {
173: r2 = r->next;
174: if (r->defn != null)
175: free(r->defn);
176: free(r);
177: }
178: n->d = NULL;
179: }
1.12 espie 180: }
181:
182: void
183: macro_popdef(const char *name)
184: {
1.13 espie 185: ndptr n = lookup(name);
186:
187: if (n != NULL) {
188: struct macro_definition *r = n->d;
189: if (r != NULL) {
190: n->d = r->next;
191: if (r->defn != null)
192: free(r->defn);
193: free(r);
194: }
195: }
1.12 espie 196: }
197:
198: void
199: macro_for_all(void (*f)(const char *, struct macro_definition *))
200: {
1.13 espie 201: ndptr n;
202: unsigned int i;
1.12 espie 203:
1.13 espie 204: for (n = ohash_first(¯os, &i); n != NULL;
205: n = ohash_next(¯os, &i))
206: f(n->name, n->d);
1.12 espie 207: }
208:
209: void
210: setup_builtin(const char *name, unsigned int type)
211: {
1.13 espie 212: ndptr n;
1.12 espie 213:
1.13 espie 214: n = create_entry(name);
1.14 ! espie 215: n->builtin_type = type;
1.13 espie 216: n->d = xalloc(sizeof(struct macro_definition));
217: n->d->defn = xstrdup(name);
218: n->d->type = type;
219: n->d->next = NULL;
1.12 espie 220: }
221:
222: const char *
223: macro_name(ndptr p)
224: {
225: return p->name;
226: }
227:
228: struct macro_definition *
229: macro_getdef(ndptr p)
230: {
1.13 espie 231: return p->d;
1.1 deraadt 232: }
1.14 ! espie 233:
! 234: ndptr
! 235: macro_getbuiltin(const char *name)
! 236: {
! 237: ndptr p;
! 238:
! 239: p = lookup(name);
! 240: if (p == NULL || p->builtin_type == MACRTYPE)
! 241: return NULL;
! 242: else
! 243: return p;
! 244: }
! 245:
! 246: int
! 247: macro_builtin_type(ndptr p)
! 248: {
! 249: return p->builtin_type;
! 250: }
! 251: