/*
Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
*/
#ifndef lint
static char RCSid[] = "$Header: /usr/users/mjr/hacks/umud/RCS/sym.c,v 1.1 91/07/04 17:32:31 mjr Rel $";
#endif
/* configure all options BEFORE including system stuff. */
#include "config.h"
#include "mud.h"
#define SYSSYMWIDTH 31
typedef struct syssym {
char *nam;
char *def;
struct syssym *next;
} SysSym;
static SysSym *systable[SYSSYMWIDTH];
static int sym_initted = 0;
void
syminit()
{
int x;
if(sym_initted)
return;
for(x = 0; x < SYSSYMWIDTH; x++)
systable[x] = (SysSym *)0;
sym_initted = 1;
}
static unsigned
symhash(nam,hw)
register char *nam;
int hw;
{
register unsigned int n = 0;
while(*nam != '\0')
n = *nam++ + 65599 * n;
return(n % hw);
}
char *
symlook(nam)
char *nam;
{
SysSym *sp;
if(!sym_initted)
return((char *)0);
sp = systable[symhash(nam,SYSSYMWIDTH)];
while(sp != (SysSym *)0) {
if(sp->nam != (char *)0 && !strcmp(nam,sp->nam))
return(sp->def);
sp = sp->next;
}
return((char *)0);
}
symdef(nam,def)
char *nam;
char *def;
{
SysSym *sp;
int hval;
if(!sym_initted)
return(1);
hval = symhash(nam,SYSSYMWIDTH);
sp = systable[hval];
while(sp != (SysSym *)0) {
if(sp->nam != (char *)0 && !strcmp(nam,sp->nam))
break;
sp = sp->next;
}
if(sp != (SysSym *)0) {
char *op;
op = (char *)malloc((unsigned)strlen(def) + 1);
if(op == (char *)0)
return(1);
(void)free((mall_t)sp->def);
sp->def = op;
(void)strcpy(op,def);
return(0);
}
if((sp = (SysSym *)malloc(sizeof(SysSym))) == (SysSym *)0)
return(1);
sp->nam = (char *)malloc((unsigned)strlen(nam) + 1);
if(sp->nam == (char *)0)
return(1);
sp->def = (char *)malloc((unsigned)strlen(def) + 1);
if(sp->def == (char *)0)
return(1);
(void)strcpy(sp->nam,nam);
(void)strcpy(sp->def,def);
/* link in */
sp->next = systable[hval];
systable[hval] = sp;
return(0);
}
symundef(nam)
char *nam;
{
SysSym *sp;
SysSym *psp;
int hval;
if(!sym_initted)
return(1);
psp = sp = systable[(hval = symhash(nam,SYSSYMWIDTH))];
while(sp != (SysSym *)0) {
if(sp->nam != (char *)0 && !strcmp(nam,sp->nam)) {
if(sp == systable[hval])
systable[hval] = sp->next;
else
psp->next = sp->next;
break;
}
psp = sp;
sp = sp->next;
}
if(sp != (SysSym *)0) {
free((mall_t)sp->nam);
free((mall_t)sp->def);
free((mall_t)sp);
return(0);
}
return(1);
}