xref: /freebsd/usr.bin/m4/look.c (revision cb166ce422ac2bc81f42c2a2e2cd68625c11478d)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Ozan Yigit at York University.
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	8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40 
41 /*
42  * look.c
43  * Facility: m4 macro processor
44  * by: oz
45  */
46 
47 #include <sys/types.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include "mdef.h"
52 #include "stdd.h"
53 #include "extern.h"
54 
55 int
56 hash(name)
57 register char *name;
58 {
59 	register unsigned long h = 0;
60 	while (*name)
61 		h = (h << 5) + h + *name++;
62 	return (h % HASHSIZE);
63 }
64 
65 /*
66  * find name in the hash table
67  */
68 ndptr
69 lookup(name)
70 char *name;
71 {
72 	register ndptr p;
73 
74 	for (p = hashtab[hash(name)]; p != nil; p = p->nxtptr)
75 		if (STREQ(name, p->name))
76 			break;
77 	return (p);
78 }
79 
80 /*
81  * hash and create an entry in the hash table.
82  * The new entry is added in front of a hash bucket.
83  */
84 ndptr
85 addent(name)
86 char *name;
87 {
88 	register int h;
89 	ndptr p;
90 
91 	h = hash(name);
92 	p = (ndptr) xalloc(sizeof(struct ndblock));
93 	p->nxtptr = hashtab[h];
94 	hashtab[h] = p;
95 	p->name = xstrdup(name);
96 	return p;
97 }
98 
99 static void
100 freent(p)
101 ndptr p;
102 {
103 	if (!(p->type & STATIC)) {
104 		free((char *) p->name);
105 		if (p->defn != null)
106 			free((char *) p->defn);
107 	}
108 	free((char *) p);
109 }
110 
111 /*
112  * remove an entry from the hashtable
113  */
114 void
115 remhash(name, all)
116 char *name;
117 int all;
118 {
119 	register int h;
120 	register ndptr xp, tp, mp;
121 
122 	h = hash(name);
123 	mp = hashtab[h];
124 	tp = nil;
125 	while (mp != nil) {
126 		if (STREQ(mp->name, name)) {
127 			mp = mp->nxtptr;
128 			if (tp == nil) {
129 				freent(hashtab[h]);
130 				hashtab[h] = mp;
131 			}
132 			else {
133 				xp = tp->nxtptr;
134 				tp->nxtptr = mp;
135 				freent(xp);
136 			}
137 			if (!all)
138 				break;
139 		}
140 		else {
141 			tp = mp;
142 			mp = mp->nxtptr;
143 		}
144 	}
145 }
146