1acc9d408SJuli Mallett /* $OpenBSD: look.c,v 1.9 2002/02/16 21:27:48 millert Exp $ */ 2acc9d408SJuli Mallett 39b50d902SRodney W. Grimes /* 49b50d902SRodney W. Grimes * Copyright (c) 1989, 1993 59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 89b50d902SRodney W. Grimes * Ozan Yigit at York University. 99b50d902SRodney W. Grimes * 109b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 119b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 129b50d902SRodney W. Grimes * are met: 139b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 159b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 169b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 179b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 189b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 199b50d902SRodney W. Grimes * must display the following acknowledgement: 209b50d902SRodney W. Grimes * This product includes software developed by the University of 219b50d902SRodney W. Grimes * California, Berkeley and its contributors. 229b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 239b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 249b50d902SRodney W. Grimes * without specific prior written permission. 259b50d902SRodney W. Grimes * 269b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 279b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 289b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 299b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 309b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 319b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 329b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 339b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 349b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 359b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 369b50d902SRodney W. Grimes * SUCH DAMAGE. 379b50d902SRodney W. Grimes */ 389b50d902SRodney W. Grimes 39acc9d408SJuli Mallett #include <sys/cdefs.h> 40acc9d408SJuli Mallett __SCCSID("@(#)look.c 8.1 (Berkeley) 6/6/93"); 41acc9d408SJuli Mallett __FBSDID("$FreeBSD$"); 429b50d902SRodney W. Grimes 439b50d902SRodney W. Grimes /* 449b50d902SRodney W. Grimes * look.c 459b50d902SRodney W. Grimes * Facility: m4 macro processor 469b50d902SRodney W. Grimes * by: oz 479b50d902SRodney W. Grimes */ 489b50d902SRodney W. Grimes 499b50d902SRodney W. Grimes #include <sys/types.h> 509b50d902SRodney W. Grimes #include <stdio.h> 519b50d902SRodney W. Grimes #include <stdlib.h> 52acc9d408SJuli Mallett #include <stddef.h> 539b50d902SRodney W. Grimes #include <string.h> 549b50d902SRodney W. Grimes #include "mdef.h" 559b50d902SRodney W. Grimes #include "stdd.h" 569b50d902SRodney W. Grimes #include "extern.h" 579b50d902SRodney W. Grimes 58acc9d408SJuli Mallett static void freent(ndptr); 59acc9d408SJuli Mallett 60acc9d408SJuli Mallett unsigned 619b50d902SRodney W. Grimes hash(name) 62acc9d408SJuli Mallett const char *name; 639b50d902SRodney W. Grimes { 64acc9d408SJuli Mallett unsigned int h = 0; 659b50d902SRodney W. Grimes while (*name) 669b50d902SRodney W. Grimes h = (h << 5) + h + *name++; 67acc9d408SJuli Mallett return (h); 689b50d902SRodney W. Grimes } 699b50d902SRodney W. Grimes 709b50d902SRodney W. Grimes /* 719b50d902SRodney W. Grimes * find name in the hash table 729b50d902SRodney W. Grimes */ 739b50d902SRodney W. Grimes ndptr 749b50d902SRodney W. Grimes lookup(name) 75acc9d408SJuli Mallett const char *name; 769b50d902SRodney W. Grimes { 77acc9d408SJuli Mallett ndptr p; 78acc9d408SJuli Mallett unsigned int h; 799b50d902SRodney W. Grimes 80acc9d408SJuli Mallett h = hash(name); 81acc9d408SJuli Mallett for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr) 82acc9d408SJuli Mallett if (h == p->hv && STREQ(name, p->name)) 839b50d902SRodney W. Grimes break; 849b50d902SRodney W. Grimes return (p); 859b50d902SRodney W. Grimes } 869b50d902SRodney W. Grimes 879b50d902SRodney W. Grimes /* 889b50d902SRodney W. Grimes * hash and create an entry in the hash table. 899b50d902SRodney W. Grimes * The new entry is added in front of a hash bucket. 909b50d902SRodney W. Grimes */ 919b50d902SRodney W. Grimes ndptr 929b50d902SRodney W. Grimes addent(name) 93acc9d408SJuli Mallett const char *name; 949b50d902SRodney W. Grimes { 95acc9d408SJuli Mallett unsigned int h; 969b50d902SRodney W. Grimes ndptr p; 979b50d902SRodney W. Grimes 989b50d902SRodney W. Grimes h = hash(name); 99acc9d408SJuli Mallett p = (ndptr) xalloc(sizeof(struct ndblock)); 100acc9d408SJuli Mallett p->nxtptr = hashtab[h % HASHSIZE]; 101acc9d408SJuli Mallett hashtab[h % HASHSIZE] = p; 102acc9d408SJuli Mallett p->name = xstrdup(name); 103acc9d408SJuli Mallett p->hv = h; 1049b50d902SRodney W. Grimes return p; 1059b50d902SRodney W. Grimes } 1069b50d902SRodney W. Grimes 1079b50d902SRodney W. Grimes static void 1089b50d902SRodney W. Grimes freent(p) 1099b50d902SRodney W. Grimes ndptr p; 1109b50d902SRodney W. Grimes { 1119b50d902SRodney W. Grimes free((char *) p->name); 1129b50d902SRodney W. Grimes if (p->defn != null) 1139b50d902SRodney W. Grimes free((char *) p->defn); 1149b50d902SRodney W. Grimes free((char *) p); 1159b50d902SRodney W. Grimes } 1169b50d902SRodney W. Grimes 1179b50d902SRodney W. Grimes /* 1189b50d902SRodney W. Grimes * remove an entry from the hashtable 1199b50d902SRodney W. Grimes */ 1209b50d902SRodney W. Grimes void 1219b50d902SRodney W. Grimes remhash(name, all) 122acc9d408SJuli Mallett const char *name; 1239b50d902SRodney W. Grimes int all; 1249b50d902SRodney W. Grimes { 125acc9d408SJuli Mallett unsigned int h; 126acc9d408SJuli Mallett ndptr xp, tp, mp; 1279b50d902SRodney W. Grimes 1289b50d902SRodney W. Grimes h = hash(name); 129acc9d408SJuli Mallett mp = hashtab[h % HASHSIZE]; 1309b50d902SRodney W. Grimes tp = nil; 1319b50d902SRodney W. Grimes while (mp != nil) { 132acc9d408SJuli Mallett if (mp->hv == h && STREQ(mp->name, name)) { 1339b50d902SRodney W. Grimes mp = mp->nxtptr; 1349b50d902SRodney W. Grimes if (tp == nil) { 135acc9d408SJuli Mallett freent(hashtab[h % HASHSIZE]); 136acc9d408SJuli Mallett hashtab[h % HASHSIZE] = mp; 1379b50d902SRodney W. Grimes } 1389b50d902SRodney W. Grimes else { 1399b50d902SRodney W. Grimes xp = tp->nxtptr; 1409b50d902SRodney W. Grimes tp->nxtptr = mp; 1419b50d902SRodney W. Grimes freent(xp); 1429b50d902SRodney W. Grimes } 1439b50d902SRodney W. Grimes if (!all) 1449b50d902SRodney W. Grimes break; 1459b50d902SRodney W. Grimes } 1469b50d902SRodney W. Grimes else { 1479b50d902SRodney W. Grimes tp = mp; 1489b50d902SRodney W. Grimes mp = mp->nxtptr; 1499b50d902SRodney W. Grimes } 1509b50d902SRodney W. Grimes } 1519b50d902SRodney W. Grimes } 152