look.c revision 1.1.1.1 1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Ozan Yigit.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 static char sccsid[] = "@(#)look.c 5.3 (Berkeley) 2/26/91";
39 #endif /* not lint */
40
41 /*
42 * look.c
43 * Facility: m4 macro processor
44 * by: oz
45 */
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include "mdef.h"
51 #include "extr.h"
52
53 /*
54 * hash - compute hash value using the proverbial
55 * hashing function. Taken from K&R.
56 */
57 hash (name)
58 register char *name;
59 {
60 register int h = 0;
61 while (*name)
62 h += *name++;
63 return (h % HASHSIZE);
64 }
65
66 /*
67 * lookup - find name in the hash table
68 *
69 */
70 ndptr lookup(name)
71 char *name;
72 {
73 register ndptr p;
74
75 for (p = hashtab[hash(name)]; p != nil; p = p->nxtptr)
76 if (strcmp(name, p->name) == 0)
77 break;
78 return (p);
79 }
80
81 /*
82 * addent - hash and create an entry in the hash
83 * table. The new entry is added in front
84 * of a hash bucket.
85 */
86 ndptr addent(name)
87 char *name;
88 {
89 register int h;
90 ndptr p;
91
92 h = hash(name);
93 if ((p = (ndptr) malloc(sizeof(struct ndblock))) != NULL) {
94 p->nxtptr = hashtab[h];
95 hashtab[h] = p;
96 p->name = strdup(name);
97 }
98 else
99 error("m4: no more memory.");
100 return p;
101 }
102
103 /*
104 * remhash - remove an entry from the hashtable
105 *
106 */
107 remhash(name, all)
108 char *name;
109 int all;
110 {
111 register int h;
112 register ndptr xp, tp, mp;
113
114 h = hash(name);
115 mp = hashtab[h];
116 tp = nil;
117 while (mp != nil) {
118 if (strcmp(mp->name, name) == 0) {
119 mp = mp->nxtptr;
120 if (tp == nil) {
121 freent(hashtab[h]);
122 hashtab[h] = mp;
123 }
124 else {
125 xp = tp->nxtptr;
126 tp->nxtptr = mp;
127 freent(xp);
128 }
129 if (!all)
130 break;
131 }
132 else {
133 tp = mp;
134 mp = mp->nxtptr;
135 }
136 }
137 }
138
139 /*
140 * freent - free a hashtable information block
141 *
142 */
143 freent(p)
144 ndptr p;
145 {
146 if (!(p->type & STATIC)) {
147 free(p->name);
148 if (p->defn != null)
149 free(p->defn);
150 }
151 free(p);
152 }
153
154