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