Annotation of src/usr.bin/m4/look.c, Revision 1.13
1.13 ! espie 1: /* $OpenBSD: look.c,v 1.12 2003/06/30 21:42:50 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 */
! 56: struct macro_definition *d;
! 57: char name[1]; /* entry name.. */
1.12 espie 58: };
59:
1.13 ! espie 60: extern void *hash_alloc(size_t, void *);
! 61: extern void hash_free(void *, size_t, void *);
! 62: extern void *element_alloc(size_t, void *);
! 63: static void setup_definition(struct macro_definition *, const char *,
! 64: const char *);
! 65:
! 66: static struct ohash_info macro_info = {
! 67: offsetof(struct ndblock, name),
! 68: NULL, hash_alloc, hash_free, element_alloc };
! 69:
! 70: static struct ohash macros;
! 71:
! 72: void
! 73: init_macros()
! 74: {
! 75: ohash_init(¯os, 7, ¯o_info);
1.1 deraadt 76: }
77:
78: /*
79: * find name in the hash table
80: */
81: ndptr
1.10 espie 82: lookup(const char *name)
1.1 deraadt 83: {
1.13 ! espie 84: return ohash_find(¯os, ohash_qlookup(¯os, name));
1.12 espie 85: }
86:
87: struct macro_definition *
88: lookup_macro_definition(const char *name)
89: {
90: ndptr p;
91:
92: p = lookup(name);
93: if (p)
1.13 ! espie 94: return p->d;
1.12 espie 95: else
96: return NULL;
97: }
98:
99: static void
1.13 ! espie 100: setup_definition(struct macro_definition *d, const char *defn, const char *name)
1.12 espie 101: {
102: int n;
103:
1.13 ! espie 104: if (strncmp(defn, BUILTIN_MARKER, sizeof(BUILTIN_MARKER)-1) == 0 &&
! 105: (n = builtin_type(defn+sizeof(BUILTIN_MARKER)-1)) != -1) {
! 106: d->type = n & TYPEMASK;
! 107: if ((n & NOARGS) == 0)
! 108: d->type |= NEEDARGS;
! 109: d->defn = xstrdup(defn+sizeof(BUILTIN_MARKER)-1);
! 110: } else {
! 111: if (!*defn)
! 112: d->defn = null;
! 113: else
! 114: d->defn = xstrdup(defn);
! 115: d->type = MACRTYPE;
! 116: }
! 117: if (STREQ(name, defn))
! 118: d->type |= RECDEF;
! 119: }
! 120:
! 121: static ndptr
! 122: create_entry(const char *name)
! 123: {
! 124: const char *end = NULL;
! 125: unsigned int i;
! 126: ndptr n;
! 127:
! 128: i = ohash_qlookupi(¯os, name, &end);
! 129: n = ohash_find(¯os, i);
! 130: if (n == NULL) {
! 131: n = ohash_create_entry(¯o_info, name, &end);
! 132: ohash_insert(¯os, i, n);
! 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);
! 215: n->d = xalloc(sizeof(struct macro_definition));
! 216: n->d->defn = xstrdup(name);
! 217: n->d->type = type;
! 218: n->d->next = NULL;
1.12 espie 219: }
220:
221: const char *
222: macro_name(ndptr p)
223: {
224: return p->name;
225: }
226:
227: struct macro_definition *
228: macro_getdef(ndptr p)
229: {
1.13 ! espie 230: return p->d;
1.1 deraadt 231: }