xref: /csrg-svn/usr.sbin/sendmail/src/stab.c (revision 4446)
14057Seric # include "sendmail.h"
24057Seric 
3*4446Seric static char SccsId[] = "@(#)stab.c	3.10	09/29/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 
244151Seric # define STABSIZE	400
254057Seric 
264151Seric static STAB	*SymTab[STABSIZE];
274151Seric 
284057Seric STAB *
294103Seric stab(name, type, op)
304057Seric 	char *name;
314103Seric 	int type;
324057Seric 	int op;
334057Seric {
344151Seric 	register STAB *s;
354151Seric 	register STAB **ps;
364058Seric 	extern bool sameword();
374151Seric 	register int hfunc;
384151Seric 	register char *p;
394319Seric 	extern char lower();
404057Seric 
414075Seric # ifdef DEBUG
424075Seric 	if (Debug > 4)
434103Seric 		printf("STAB: %s %d ", name, type);
444075Seric # endif DEBUG
454075Seric 
464151Seric 	/*
474151Seric 	**  Compute the hashing function
484151Seric 	**
494151Seric 	**	We could probably do better....
504151Seric 	*/
514151Seric 
524151Seric 	hfunc = type;
534151Seric 	for (p = name; *p != '\0'; p++)
54*4446Seric 		hfunc = (((hfunc << 7) | lower(*p)) & 077777) % STABSIZE;
554151Seric 
564151Seric # ifdef DEBUG
574439Seric 	if (Debug > 5)
584151Seric 		printf("(hfunc=%d) ", hfunc);
594151Seric # endif DEBUG
604151Seric 
614151Seric 	ps = &SymTab[hfunc];
624151Seric 	while ((s = *ps) != NULL && (!sameword(name, s->s_name) || s->s_type != type))
634057Seric 		ps = &s->s_next;
644151Seric 
654151Seric 	/*
664151Seric 	**  Dispose of the entry.
674151Seric 	*/
684151Seric 
694057Seric 	if (s != NULL || op == ST_FIND)
704075Seric 	{
714075Seric # ifdef DEBUG
724075Seric 		if (Debug > 4)
734075Seric 		{
744075Seric 			if (s == NULL)
754075Seric 				printf("not found\n");
764075Seric 			else
774151Seric 				printf("type %d val %x\n", s->s_type, s->s_class);
784075Seric 		}
794075Seric # endif DEBUG
804057Seric 		return (s);
814075Seric 	}
824057Seric 
834151Seric 	/*
844151Seric 	**  Make a new entry and link it in.
854151Seric 	*/
864151Seric 
874075Seric # ifdef DEBUG
884075Seric 	if (Debug > 4)
894075Seric 		printf("entered\n");
904075Seric # endif DEBUG
914075Seric 
924057Seric 	/* make new entry */
934057Seric 	s = (STAB *) xalloc(sizeof *s);
944103Seric 	clear((char *) s, sizeof *s);
954057Seric 	s->s_name = newstr(name);
964058Seric 	makelower(s->s_name);
974103Seric 	s->s_type = type;
984057Seric 
994151Seric 	/* link it in */
1004057Seric 	*ps = s;
1014057Seric 
1024057Seric 	return (s);
1034057Seric }
104