home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume17 / ease2 / part01 / src / symtab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-08  |  5.9 KB  |  244 lines

  1. /*    $Header: symtab.c,v 2.0 88/06/15 14:43:04 root Exp $    */
  2.  
  3. /*
  4.  * $Log:    symtab.c,v $
  5.  * Revision 2.0  88/06/15  14:43:04  root
  6.  * Baseline release for net posting. ADR.
  7.  * 
  8.  */
  9.  
  10. /*
  11.  *      symtab.c   -- Contains Ease Translator symbol table routines.
  12.  *
  13.  *      author     -- James S. Schoner, Purdue University Computing Center,
  14.  *                        West Lafayette, Indiana  47907
  15.  *
  16.  *      date       -- July 9, 1985
  17.  *
  18.  *    Copyright (c) 1985 by Purdue Research Foundation
  19.  *
  20.  *    All rights reserved.
  21.  *
  22.  */
  23.  
  24. #include "fixstrings.h"
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include "symtab.h"
  28.  
  29. #define ERRORMAILER "error"        /* predefined error mailer name */
  30.  
  31. extern void FatalError (),
  32.         PrintWarning ();
  33.  
  34. struct he *LookupSymbol ();
  35.  
  36. struct Defmac {                /* predefined macro struct def  */
  37.     char *macname;
  38.     char  macrep;
  39. };
  40.  
  41. static struct he *SymTab[SST];        /* hash table base array        */
  42. static struct Defmac MacDefs[] = {    /* predefined macros            */
  43.             {"m_smtp",    'e'},
  44.             {"m_oname",    'j'},
  45.             {"m_ufrom",    'l'},
  46.             {"m_daemon",    'n'},
  47.             {"m_addrops",    'o'},
  48.             {"m_defaddr",    'q'},
  49.             {"m_sitename",    'w'},
  50.             {"m_odate",    'a'},
  51.             {"m_adate",    'b'},
  52.             {"m_hops",    'c'},
  53.             {"m_udate",    'd'},
  54.             {"m_saddr",    'f'},
  55.             {"m_sreladdr",    'g'},
  56.             {"m_rhost",    'h'},
  57.             {"m_qid",    'i'},
  58.             {"m_pid",    'p'},
  59.             {"m_protocol",    'r'},
  60.             {"m_shostname", 's'},
  61.             {"m_ctime",    't'},
  62.             {"m_ruser",    'u'},
  63.             {"m_version",    'v'},
  64.             {"m_sname",    'x'},
  65.             {"m_stty",    'y'},
  66.             {"m_rhdir",    'z'},
  67.             {"sentinel",    '\0'}
  68. };
  69.  
  70. /* FLUKE jps 28-apr-86 - Install some wired-in class names */
  71. static struct Defmac ClassDefs[] = {    /* predefined classes */
  72.             {"c_myname",    'w'},
  73.             {"class_sentinel",    '\0'}
  74. };
  75.  
  76. /*
  77.  *    DefScan () -- Scan symbol table to find macros, classes, mailers, 
  78.  *              and rulesets which have been referenced or declared, but
  79.  *              not defined.  A warning is printed for each such 
  80.  *              occurence.  This routine is usually called at the end
  81.  *              of a successful Ease translation.
  82.  *
  83.  */
  84. void
  85. DefScan ()
  86. {
  87.     register int stindex;        /* symbol table hash index   */
  88.     register struct he *hcsearch;    /* hash chain search pointer */
  89.  
  90.     for (stindex = 0; stindex < SST; stindex++) {
  91.         if ((hcsearch = SymTab[stindex]) != NULL)
  92.             while (hcsearch != NULL) {
  93.                 if ((ISMACRO(hcsearch->idtype) && 
  94.                      isupper(hcsearch->idval.idc)) &&
  95.                      !ISMACRO(hcsearch->idd))
  96.                     PrintWarning ("Macro not defined: %s\n", hcsearch->psb);
  97. #ifdef notdef
  98.                 if (ISCLASS(hcsearch->idtype) && !ISCLASS(hcsearch->idd))
  99. #else
  100.                 /* FLUKE jps 28-apr-86 */
  101.                 /* print warnings for UPPER CASE names only */
  102.                 if (ISCLASS(hcsearch->idtype) &&
  103.                     isupper(hcsearch->idval.idc) &&
  104.                     !ISCLASS(hcsearch->idd))
  105. #endif
  106.                     PrintWarning ("Class not defined: %s\n", hcsearch->psb);
  107.                 if (ISMAILER(hcsearch->idtype) && !ISMAILER(hcsearch->idd))
  108.                     PrintWarning ("Mailer not defined: %s\n", hcsearch->psb);
  109.                 if (ISRULESET(hcsearch->idtype) && !ISRULESET(hcsearch->idd))
  110.                     PrintWarning ("Ruleset not defined: %s\n", hcsearch->psb);
  111.                 hcsearch = hcsearch->phe;
  112.             }
  113.     }
  114. }
  115.                      
  116.  
  117. /*
  118.  *    InitSymbolTable () -- Invoked by main () to initialize the symbol table.
  119.  *
  120.  */
  121. void
  122. InitSymbolTable ()
  123. {
  124.     int i;
  125.  
  126.     for (i = 0; i < SST; i++)        /* initialize base array */
  127.         SymTab[i] = NULL;
  128. }
  129.  
  130.  
  131. /*
  132.  *    PreLoad () -- Invoked by main () to preload special macro names 
  133.  *              and mailer declarations.
  134.  *
  135.  */
  136. void
  137. PreLoad ()
  138. {
  139.     struct Defmac *macptr;
  140.     struct he     *symptr;
  141.  
  142.     /* preload special (lower-case) macros */
  143.     for (macptr = &MacDefs[0]; (*macptr).macrep != '\0'; macptr++) {
  144.         symptr = LookupSymbol ((*macptr).macname);
  145.         symptr->idtype |= ID_MACRO;
  146.         symptr->idval.idc = (*macptr).macrep;
  147.     }
  148.  
  149.     /* preload special (lower-case) classes */
  150.     for (macptr = &ClassDefs[0]; (*macptr).macrep != '\0'; macptr++) {
  151.         symptr = LookupSymbol ((*macptr).macname);
  152.         symptr->idtype |= ID_CLASS;
  153.         symptr->idval.idc = (*macptr).macrep;
  154.     }
  155.  
  156.     /* preload error mailer declaration */
  157.     symptr = LookupSymbol (ERRORMAILER);
  158.     symptr->idtype |= ID_MAILER;
  159.     symptr->idd |= ID_MAILER;
  160. }
  161.     
  162.  
  163. /*
  164.  *    LookupSymbol () -- Returns a pointer to the hash entry already 
  165.  *               existing, or newly created, which corresponds 
  166.  *               to string sb.
  167.  *
  168.  */
  169. struct he *
  170. LookupSymbol (sb)
  171. char sb[];            /* string buffer containing identifier */
  172. {
  173.     struct he *phe;        /* hash entry search pointer  */
  174.     int      hc;        /* hash code of sb identifier */
  175.     extern char *malloc ();
  176.  
  177.     phe = SymTab[hc = HashCode (sb)];
  178.     while (phe != NULL)            /* find hash entry for sb */
  179.         if (!strcmp (phe->psb, sb))
  180.             return (phe);
  181.         else
  182.             phe = phe->phe;
  183.     /* make new symbol table entry */
  184.     if ((phe = (struct he *) malloc (sizeof (struct he))) == NULL)
  185.         FatalError ("System out of space in LookupSymbol ()", (char *) NULL);
  186.     if ((phe->psb = (char *) malloc (strlen (sb) + 1)) == NULL)
  187.         FatalError ("System out of space in LookupSymbol ()", (char *) NULL);
  188.     strcpy (phe->psb, sb);
  189.     phe->idval.idc = '\0';
  190.     phe->idtype = ID_UNTYPED;
  191.     phe->idd = ID_UNTYPED;
  192.     phe->phe = SymTab[hc];
  193.     return (SymTab[hc] = phe);
  194. }
  195.  
  196.  
  197. /*
  198.  *    RemoveSymbol () -- Removes the symbol table entry phe from the 
  199.  *               symbol table.
  200.  *
  201.  */
  202. void
  203. RemoveSymbol (phe)
  204. struct he *phe;       /* pointer to hash entry to be removed from symbol table */
  205. {
  206.     int hc;               /* hash code of entry phe       */
  207.     struct he *sphe;    /* search pointer for entry phe */
  208.  
  209.     if (phe == NULL)
  210.         return;
  211.     else {            /* search and remove entry phe  */
  212.         sphe = SymTab[hc = HashCode (phe->psb)];
  213.         free (phe->psb);
  214.         if (sphe == phe)
  215.             SymTab[hc] = phe->phe;
  216.         else
  217.             while (sphe != NULL)
  218.                 if (sphe->phe == phe) {
  219.                     sphe->phe = phe->phe;
  220.                     return;
  221.                 } else
  222.                     sphe = sphe->phe;
  223.     }
  224. }
  225.  
  226.  
  227. /*
  228.  *    HashCode () -- Returns the hash code of the string in sb by adding 
  229.  *               the character values and applying mod by the hash 
  230.  *               table size.
  231.  *
  232.  */
  233. int
  234. HashCode (sb)
  235. char sb[];
  236. {
  237.     int ccSum = 0;            /* sum of char values in string sb */
  238.     int i;
  239.  
  240.     for (i = 0; sb[i]; i++)        /* add char codes for sb chars     */
  241.         ccSum += sb[i];
  242.     return (ccSum % SST);        /* return sum mod table size       */
  243. }
  244.