xref: /csrg-svn/usr.sbin/sendmail/src/stab.c (revision 4151)
14057Seric # include "sendmail.h"
24057Seric 
3*4151Seric static char SccsId[] = "@(#)stab.c	3.7	08/17/81";
44057Seric 
54057Seric /*
64057Seric **  STAB -- manage the symbol table
74057Seric **
84057Seric **	Parameters:
94057Seric **		name -- the name to be looked up or inserted.
104103Seric **		type -- the type of symbol.
114057Seric **		op -- what to do:
124057Seric **			ST_ENTER -- enter the name if not
134057Seric **				already present.
144057Seric **			ST_FIND -- find it only.
154057Seric **
164057Seric **	Returns:
174057Seric **		pointer to a STAB entry for this name.
184057Seric **		NULL if not found and not entered.
194057Seric **
204057Seric **	Side Effects:
214057Seric **		can update the symbol table.
224057Seric */
234057Seric 
24*4151Seric # define STABSIZE	400
254057Seric 
26*4151Seric static STAB	*SymTab[STABSIZE];
27*4151Seric 
284057Seric STAB *
294103Seric stab(name, type, op)
304057Seric 	char *name;
314103Seric 	int type;
324057Seric 	int op;
334057Seric {
34*4151Seric 	register STAB *s;
35*4151Seric 	register STAB **ps;
364058Seric 	extern bool sameword();
37*4151Seric 	register int hfunc;
38*4151Seric 	register char *p;
394057Seric 
404075Seric # ifdef DEBUG
414075Seric 	if (Debug > 4)
424103Seric 		printf("STAB: %s %d ", name, type);
434075Seric # endif DEBUG
444075Seric 
45*4151Seric 	/*
46*4151Seric 	**  Compute the hashing function
47*4151Seric 	**
48*4151Seric 	**	We could probably do better....
49*4151Seric 	*/
50*4151Seric 
51*4151Seric 	hfunc = type;
52*4151Seric 	for (p = name; *p != '\0'; p++)
53*4151Seric 		hfunc = ((hfunc << 7) | lower(*p)) % STABSIZE;
54*4151Seric 
55*4151Seric # ifdef DEBUG
56*4151Seric 	if (Debug > 15)
57*4151Seric 		printf("(hfunc=%d) ", hfunc);
58*4151Seric # endif DEBUG
59*4151Seric 
60*4151Seric 	ps = &SymTab[hfunc];
61*4151Seric 	while ((s = *ps) != NULL && (!sameword(name, s->s_name) || s->s_type != type))
624057Seric 	{
634057Seric 		ps = &s->s_next;
644057Seric 		s = s->s_next;
654057Seric 	}
66*4151Seric 
67*4151Seric 	/*
68*4151Seric 	**  Dispose of the entry.
69*4151Seric 	*/
70*4151Seric 
714057Seric 	if (s != NULL || op == ST_FIND)
724075Seric 	{
734075Seric # ifdef DEBUG
744075Seric 		if (Debug > 4)
754075Seric 		{
764075Seric 			if (s == NULL)
774075Seric 				printf("not found\n");
784075Seric 			else
79*4151Seric 				printf("type %d val %x\n", s->s_type, s->s_class);
804075Seric 		}
814075Seric # endif DEBUG
824057Seric 		return (s);
834075Seric 	}
844057Seric 
85*4151Seric 	/*
86*4151Seric 	**  Make a new entry and link it in.
87*4151Seric 	*/
88*4151Seric 
894075Seric # ifdef DEBUG
904075Seric 	if (Debug > 4)
914075Seric 		printf("entered\n");
924075Seric # endif DEBUG
934075Seric 
944057Seric 	/* make new entry */
954057Seric 	s = (STAB *) xalloc(sizeof *s);
964103Seric 	clear((char *) s, sizeof *s);
974057Seric 	s->s_name = newstr(name);
984058Seric 	makelower(s->s_name);
994103Seric 	s->s_type = type;
1004057Seric 
101*4151Seric 	/* link it in */
1024057Seric 	*ps = s;
1034057Seric 
1044057Seric 	return (s);
1054057Seric }
106