Annotation of src/usr.bin/m4/look.c, Revision 1.2
1.2 ! deraadt 1: /* $OpenBSD$ */
! 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.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the University of
21: * California, Berkeley and its contributors.
22: * 4. Neither the name of the University nor the names of its contributors
23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
39: #ifndef lint
40: static char sccsid[] = "@(#)look.c 8.1 (Berkeley) 6/6/93";
41: #endif /* not lint */
42:
43: /*
44: * look.c
45: * Facility: m4 macro processor
46: * by: oz
47: */
48:
49: #include <sys/types.h>
50: #include <stdio.h>
51: #include <stdlib.h>
52: #include <string.h>
53: #include "mdef.h"
54: #include "stdd.h"
55: #include "extern.h"
56:
57: int
58: hash(name)
59: register char *name;
60: {
61: register unsigned long h = 0;
62: while (*name)
63: h = (h << 5) + h + *name++;
64: return (h % HASHSIZE);
65: }
66:
67: /*
68: * find name in the hash table
69: */
70: ndptr
71: lookup(name)
72: char *name;
73: {
74: register ndptr p;
75:
76: for (p = hashtab[hash(name)]; p != nil; p = p->nxtptr)
77: if (STREQ(name, p->name))
78: break;
79: return (p);
80: }
81:
82: /*
83: * hash and create an entry in the hash table.
84: * The new entry is added in front of a hash bucket.
85: */
86: ndptr
87: addent(name)
88: char *name;
89: {
90: register int h;
91: ndptr p;
92:
93: h = hash(name);
94: p = (ndptr) xalloc(sizeof(struct ndblock));
95: p->nxtptr = hashtab[h];
96: hashtab[h] = p;
97: p->name = xstrdup(name);
98: return p;
99: }
100:
101: static void
102: freent(p)
103: ndptr p;
104: {
105: if (!(p->type & STATIC)) {
106: free((char *) p->name);
107: if (p->defn != null)
108: free((char *) p->defn);
109: }
110: free((char *) p);
111: }
112:
113: /*
114: * remove an entry from the hashtable
115: */
116: void
117: remhash(name, all)
118: char *name;
119: int all;
120: {
121: register int h;
122: register ndptr xp, tp, mp;
123:
124: h = hash(name);
125: mp = hashtab[h];
126: tp = nil;
127: while (mp != nil) {
128: if (STREQ(mp->name, name)) {
129: mp = mp->nxtptr;
130: if (tp == nil) {
131: freent(hashtab[h]);
132: hashtab[h] = mp;
133: }
134: else {
135: xp = tp->nxtptr;
136: tp->nxtptr = mp;
137: freent(xp);
138: }
139: if (!all)
140: break;
141: }
142: else {
143: tp = mp;
144: mp = mp->nxtptr;
145: }
146: }
147: }