Pristine Ack-5.5
[Ack-5.5.git] / util / ncgg / lookup.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  */
5 #ifndef NORCSID
6 static char rcsid[]= "$Id: lookup.c,v 0.3 1994/06/24 10:37:40 ceriel Exp $";
7 #endif
8
9 #include "assert.h"
10 #include "param.h"
11 #include "lookup.h"
12
13 char *myalloc();
14 char *mystrcpy();
15
16 symbol dumsym;  /* dummy to return in case of error */
17
18 symbol *lookup(name,type,style)
19 char *name;
20 symtype type;
21 lookupstyle style;
22 {
23         symbol *sy_p,**sy_pp;
24
25         for (sy_pp = &symhash[hashvalue(name)];(sy_p= *sy_pp) != 0;sy_pp= &sy_p->sy_next) {
26                 if (strcmp(sy_p->sy_name,name)!=0)
27                         continue;
28                 switch(style) {
29                 default:
30                         assert(0);
31                 case justlooking:
32                 case mustexist:
33                 case makeexist:
34                         if (type==symany || type==sy_p->sy_type)
35                                 return(sy_p);
36                         continue;
37                 case newsymbol:
38                         error("%s already defined",name);
39                         return(&dumsym);
40                 }
41         }
42         switch(style) {
43         default:
44                 assert(0);
45         case justlooking:
46                 return((symbol *) 0);
47         case mustexist:
48                 fatal("%s is unknown symbol",name);
49                 /* NOTREACHED */
50         case newsymbol:
51         case makeexist:
52                 NEW(sy_p,symbol);
53                 sy_p->sy_next = 0;
54                 sy_p->sy_name = mystrcpy(name);
55                 assert(type!=symany);
56                 sy_p->sy_type = type;
57                 *sy_pp = sy_p;
58                 return(sy_p);
59         }
60 }
61
62 hashvalue(s) register char *s; {
63         register unsigned sum=0;
64         register i;
65
66         for(i=0;*s;s++,i=(i+3)&07)
67                 sum += *s<<i;
68         return(sum%NSYMHASH);
69 }