Pristine Ack-5.5
[Ack-5.5.git] / util / opt / lookup.c
1 #ifndef NORCSID
2 static char rcsid[] = "$Id: lookup.c,v 2.7 1994/06/24 10:40:10 ceriel Exp $";
3 #endif
4
5 #include "param.h"
6 #include "types.h"
7 #include "tes.h"
8 #include "lookup.h"
9 #include "alloc.h"
10 #include "proinf.h"
11
12 /*
13  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
14  * See the copyright notice in the ACK home directory, in the file "Copyright".
15  *
16  * Author: Hans van Staveren
17  */
18
19 extern char *strncpy();
20
21 unsigned hash(string) char *string; {
22         register char *p;
23         register unsigned i,sum;
24
25         for (sum=i=0,p=string;*p;i += 3)
26                 sum ^= (*p++)<<(i&07);
27         return(sum);
28 }
29
30 sym_p symlookup(name,status,flags) char *name; int status,flags; {
31         register sym_p *spp,sp;
32         register i;
33         static short genfrag = 32767;
34
35         spp = &symhash[hash(name)%NSYMHASH];
36         while (*spp != (sym_p) 0)
37                 if (strncmp((*spp)->s_name,name,IDL)==0) {
38                         sp = *spp;
39                         if ((sp->s_flags^flags)&SYMPRO)
40                                 error("%s is both proc and datalabel",name);
41                         if (status == DEFINING) {
42                                 if (sp->s_flags&SYMDEF)
43                                         error("redefined symbol %s",name);
44                                 sp->s_flags |= SYMDEF;
45                         }
46                         return(sp);
47                 } else
48                         spp = &(*spp)->s_next;
49
50         /*
51          * symbol not found, enter in table
52          */
53
54         i = strlen(name) + 1;
55         if (i & 1)
56                 i++;
57         if (i > IDL)
58                 i = IDL;
59         *spp = sp = newsym(i);
60         strncpy(sp->s_name,name,i);
61         sp->s_flags = flags;
62         if (status == DEFINING)
63                 sp->s_flags |= SYMDEF;
64         sp->s_frag = genfrag--;
65         return(sp);
66 }
67
68 num_p numlookup(number) unsigned number; {
69         register num_p *npp, np;
70
71         npp = &curpro.numhash[number%NNUMHASH];
72         while (*npp != (num_p) 0)
73                 if ((*npp)->n_number == number)
74                         return(*npp);
75                 else
76                         npp = &(*npp)->n_next;
77
78         /*
79          * local label not found, enter in tabel
80          */
81
82         *npp = np = newnum();
83         np->n_number = number;
84         np->n_repl = np;
85         return(np);
86 }