Annotation of src/usr.bin/m4/look.c, Revision 1.11
1.11 ! millert 1: /* $OpenBSD: look.c,v 1.10 2002/04/26 16:15:16 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>
50: #include "mdef.h"
51: #include "stdd.h"
52: #include "extern.h"
53:
1.9 millert 54: static void freent(ndptr);
1.5 espie 55:
1.10 espie 56: unsigned int
57: hash(const char *name)
1.1 deraadt 58: {
1.7 millert 59: unsigned int h = 0;
1.1 deraadt 60: while (*name)
61: h = (h << 5) + h + *name++;
1.6 espie 62: return (h);
1.1 deraadt 63: }
64:
65: /*
66: * find name in the hash table
67: */
68: ndptr
1.10 espie 69: lookup(const char *name)
1.1 deraadt 70: {
1.4 espie 71: ndptr p;
1.7 millert 72: unsigned int h;
1.1 deraadt 73:
1.6 espie 74: h = hash(name);
75: for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
76: if (h == p->hv && STREQ(name, p->name))
1.1 deraadt 77: break;
78: return (p);
79: }
80:
81: /*
82: * hash and create an entry in the hash table.
83: * The new entry is added in front of a hash bucket.
84: */
85: ndptr
1.10 espie 86: addent(const char *name)
1.1 deraadt 87: {
1.7 millert 88: unsigned int h;
1.1 deraadt 89: ndptr p;
90:
91: h = hash(name);
92: p = (ndptr) xalloc(sizeof(struct ndblock));
1.6 espie 93: p->nxtptr = hashtab[h % HASHSIZE];
94: hashtab[h % HASHSIZE] = p;
1.1 deraadt 95: p->name = xstrdup(name);
1.6 espie 96: p->hv = h;
1.1 deraadt 97: return p;
98: }
99:
100: static void
1.10 espie 101: freent(ndptr p)
1.1 deraadt 102: {
1.8 espie 103: free((char *) p->name);
104: if (p->defn != null)
105: free((char *) p->defn);
1.1 deraadt 106: free((char *) p);
107: }
108:
109: /*
110: * remove an entry from the hashtable
111: */
112: void
1.10 espie 113: remhash(const char *name, int all)
1.1 deraadt 114: {
1.7 millert 115: unsigned int h;
1.4 espie 116: ndptr xp, tp, mp;
1.1 deraadt 117:
118: h = hash(name);
1.6 espie 119: mp = hashtab[h % HASHSIZE];
1.1 deraadt 120: tp = nil;
121: while (mp != nil) {
1.6 espie 122: if (mp->hv == h && STREQ(mp->name, name)) {
1.1 deraadt 123: mp = mp->nxtptr;
124: if (tp == nil) {
1.6 espie 125: freent(hashtab[h % HASHSIZE]);
126: hashtab[h % HASHSIZE] = mp;
1.1 deraadt 127: }
128: else {
129: xp = tp->nxtptr;
130: tp->nxtptr = mp;
131: freent(xp);
132: }
133: if (!all)
134: break;
135: }
136: else {
137: tp = mp;
138: mp = mp->nxtptr;
139: }
140: }
141: }