home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.ee.lbl.gov
/
2014.05.ftp.ee.lbl.gov.tar
/
ftp.ee.lbl.gov
/
bmd-1.0beta.tar.Z
/
bmd-1.0beta.tar
/
bmd-1.0beta
/
app
/
omtd
/
st.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-02-17
|
4KB
|
144 lines
/*
* Copyright (c) 1990 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Lawrence Berkeley Laboratory,
* Berkeley, CA. The name of the University may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
* File: lexical.c
*
* String table routines for Prescript compiler.
* Van Jacobson, January, 1988
*/
/*
* Copyright (c) 1988 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Lawrence Berkeley Laboratory,
* Berkeley, CA. The name of the University may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef lint
static char rcsid[] =
"@(#) $Header: readline.c,v 1.3 91/01/13 20:14:14 mccanne Locked $ (LBL)";
static char copyright[] =
"Copyright (c) 1988, Regents of the University of California";
#endif lint
#include <stdio.h>
#include <sys/types.h>
extern char *malloc();
extern char *realloc();
#define MODULUS 16001
static u_char *idTable[MODULUS];
static u_char *stringTable;
static u_char *stNext;
static int stSize = 2048;
static int stLeft = 2048;
rl_init()
{
/* get the initial string table space */
stNext = stringTable = (u_char *)malloc (stSize);
if (stringTable == 0)
error("out of memory");
}
/* "hashpjw" from Aho, Sethi & Ullman, p.436 */
static int
hash(str, len)
register u_char *str;
register int len;
{
register unsigned int h = 0, g;
while (--len >= 0) {
h = (h << 4) + *str++;
if (g = h & 0xf0000000) {
h = h ^ (g >> 24);
h = h ^ g;
}
}
return (h % MODULUS);
}
static char *
EnterString(str, len)
u_char *str;
int len;
{
register int h = hash (str, len);
while (idTable[h]) {
if (bcmp(str, idTable[h], len) == 0)
return (char *)idTable[h];
h = ++h % MODULUS;
}
/* didn't find string in table - add it */
if ((stLeft -= len + 1) <= 0) {
register u_char *cp = stringTable;
stLeft += stSize;
stSize *= 2;
stringTable = (u_char *)realloc(cp, stSize);
if (stringTable == 0)
error("out of memory");
if (cp != stringTable) {
/*
* realloc moved our table -- fix up the
* pointers in idTable.
*/
register int i = MODULUS;
register int j = stringTable - cp;
stNext += j;
for (i = 0; i < MODULUS; i++)
if (idTable[i])
idTable[i] += j;
}
}
idTable[h] = stNext;
strncpy(stNext, str, len);
stNext += len;
*stNext++ = 0;
return (char *)idTable[h];
}
char *
intern(s)
char *s;
{
return EnterString(s, strlen(s));
}