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 >
C/C++ Source or Header  |  1991-02-17  |  4KB  |  144 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Lawrence Berkeley Laboratory,
  11.  * Berkeley, CA.  The name of the University may not be used to
  12.  * endorse or promote products derived from this software without
  13.  * specific prior written permission.
  14.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  15.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  16.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17.  */
  18. /*
  19.  * File: lexical.c
  20.  *
  21.  * String table routines for Prescript compiler.
  22.  *     Van Jacobson, January, 1988
  23.  */
  24.  
  25. /*
  26.  * Copyright (c) 1988 Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms are permitted
  30.  * provided that the above copyright notice and this paragraph are
  31.  * duplicated in all such forms and that any documentation,
  32.  * advertising materials, and other materials related to such
  33.  * distribution and use acknowledge that the software was developed
  34.  * by the University of California, Lawrence Berkeley Laboratory,
  35.  * Berkeley, CA.  The name of the University may not be used to
  36.  * endorse or promote products derived from this software without
  37.  * specific prior written permission.
  38.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  39.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  40.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  41.  */
  42.  
  43. #ifndef lint
  44. static char rcsid[] =
  45.     "@(#) $Header: readline.c,v 1.3 91/01/13 20:14:14 mccanne Locked $ (LBL)";
  46. static char copyright[] =
  47.     "Copyright (c) 1988, Regents of the University of California";
  48. #endif lint
  49.  
  50. #include <stdio.h>
  51. #include <sys/types.h>
  52.  
  53. extern char *malloc();
  54. extern char *realloc();
  55.  
  56. #define MODULUS 16001
  57.  
  58. static u_char *idTable[MODULUS];
  59. static u_char *stringTable;
  60. static u_char *stNext;
  61. static int stSize = 2048;
  62. static int stLeft = 2048;
  63.  
  64. rl_init()
  65. {
  66.     /* get the initial string table space */
  67.  
  68.     stNext = stringTable = (u_char *)malloc (stSize);
  69.     if (stringTable == 0)
  70.         error("out of memory");
  71. }
  72.  
  73. /* "hashpjw" from Aho, Sethi & Ullman, p.436 */
  74. static int
  75. hash(str, len)
  76.     register u_char *str;
  77.     register int len;
  78. {
  79.     register unsigned int h = 0, g;
  80.  
  81.     while (--len >= 0) {
  82.         h = (h << 4) + *str++;
  83.         if (g = h & 0xf0000000) {
  84.             h = h ^ (g >> 24);
  85.             h = h ^ g;
  86.         }
  87.     }
  88.     return (h % MODULUS);
  89. }
  90.  
  91. static char *
  92. EnterString(str, len)
  93.     u_char *str;
  94.     int len;
  95. {
  96.     register int h = hash (str, len);
  97.  
  98.     while (idTable[h]) {
  99.         if (bcmp(str, idTable[h], len) == 0)
  100.             return (char *)idTable[h];
  101.         h = ++h % MODULUS;
  102.     }
  103.     /* didn't find string in table - add it */
  104.     if ((stLeft -= len + 1) <= 0) {
  105.         register u_char *cp = stringTable;
  106.  
  107.         stLeft += stSize;
  108.         stSize *= 2;
  109.  
  110.         stringTable = (u_char *)realloc(cp, stSize);
  111.  
  112.         if (stringTable == 0)
  113.             error("out of memory");
  114.  
  115.         if (cp != stringTable) {
  116.             /*
  117.              * realloc moved our table -- fix up the
  118.              * pointers in idTable.
  119.              */
  120.             register int i = MODULUS;
  121.             register int j = stringTable - cp;
  122.  
  123.             stNext += j;
  124.             for (i = 0; i < MODULUS; i++)
  125.                 if (idTable[i])
  126.                     idTable[i] += j;
  127.         }
  128.     }
  129.     idTable[h] = stNext;
  130.     strncpy(stNext, str, len);
  131.     stNext += len;
  132.     *stNext++ = 0;
  133.     return (char *)idTable[h];
  134.  
  135. }
  136.  
  137. char *
  138. intern(s)
  139.     char *s;
  140. {
  141.     return EnterString(s, strlen(s));
  142. }
  143.  
  144.