home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume12 / cake / part03 / sym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-14  |  4.4 KB  |  278 lines

  1. /*
  2. **    Symbol table module
  3. */
  4.  
  5. static    char
  6. rcs_id[] = "$Header: /mip/zs/src/sys/cake/RCS/sym.c,v 1.15 87/10/05 20:16:08 zs Exp $";
  7.  
  8. #include    "cake.h"
  9.  
  10. typedef    struct    s_out
  11. {
  12.     char    *o_cmd;
  13.     char    *o_out;
  14. } Out;
  15.  
  16. typedef    struct    s_stat
  17. {
  18.     char    *s_cmd;
  19.     int    s_stat;
  20. } Stat;
  21.  
  22. extern    int    hash();
  23. extern    Cast    name_key();    extern    bool    name_equal();
  24. extern    Cast    node_key();    extern    bool    node_equal();
  25. extern    Cast    out_key();    extern    bool    out_equal();
  26. extern    Cast    stat_key();    extern    bool    stat_equal();
  27. Table    name_tab = { SIZE, NULL, name_key, hash, name_equal };
  28. Table    node_tab = { SIZE, NULL, node_key, hash, node_equal };
  29. Table    out_tab  = { SIZE, NULL, out_key,  hash, out_equal  };
  30. Table    stat_tab = { SIZE, NULL, stat_key, hash, stat_equal };
  31.  
  32. /*
  33. **    Initialize the name and the command tables.
  34. */
  35.  
  36. init_sym()
  37. {
  38.     init_table(name_tab);
  39.     init_table(node_tab);
  40.     init_table(out_tab);
  41.     init_table(stat_tab);
  42. }
  43.  
  44. /**********************************************************************/
  45. /*    Name table                              */
  46.  
  47. /*
  48. **    Save the given string in the name table if not already there;
  49. **    return its new address. This address is unique, so comparing
  50. **    two identifiers for equality can be done by comparing their
  51. **    addresses.
  52. */
  53.  
  54. char *
  55. new_name(str)
  56. reg    char    *str;
  57. {
  58.     reg    Cast    old;
  59.     reg    char    *copy;
  60.  
  61.     if ((old = lookup_table(name_tab, str)) != NULL)
  62.         return (char *) old;
  63.  
  64.     copy = (char *) newmem(strlen(str) + 1);
  65.     strcpy(copy, str);
  66.     insert_table(name_tab, copy);
  67.  
  68.     return copy;
  69. }
  70.  
  71. Cast
  72. name_key(entry)
  73. reg    Cast    entry;
  74. {
  75.     return entry;
  76. }
  77.  
  78. bool
  79. name_equal(key1, key2)
  80. reg    Cast    key1, key2;
  81. {
  82.     return streq((char *) key1, (char *) key2);
  83. }
  84.  
  85. /**********************************************************************/
  86. /*    Node table                              */
  87.  
  88. /*
  89. **    Th insertion function for the node table is chase(),
  90. **    which is in chase.c with the rest of the chase stuff.
  91. */
  92.  
  93. /*
  94. **    This function merely reports on the results
  95. **    of past calls to chase.
  96. */
  97.  
  98. Node *
  99. chase_node(name)
  100. reg    char    *name;
  101. {
  102.     return (Node *) lookup_table(node_tab, name);
  103. }
  104.  
  105. /*
  106. **    Return a list of all the nodes of this run.
  107. */
  108.  
  109. List *
  110. get_allnodes()
  111. {
  112.     return contents_table(node_tab);
  113. }
  114.  
  115. Cast
  116. node_key(entry)
  117. reg    Cast    entry;
  118. {
  119.     return (Cast) ((Node *) entry)->n_name;
  120. }
  121.  
  122. bool
  123. node_equal(key1, key2)
  124. reg    Cast    key1, key2;
  125. {
  126. #ifdef    EXTRACHECK
  127.     if (key1 != key2 && streq((char *) key1, (char *) key2))
  128.     {
  129.         fprintf(stderr, "cake internal error: inconsistency in node_equal\n");
  130.         exit_cake(TRUE);
  131.     }
  132. #endif
  133.  
  134.     return key1 == key2;
  135. }
  136.  
  137. /**********************************************************************/
  138. /*    Command output table                          */
  139.  
  140. #ifdef    STATS_FILE
  141. int    out_tried = 0;
  142. int    out_found = 0;
  143. #endif
  144.  
  145. /*
  146. **    Save a command and its output.
  147. */
  148.  
  149. new_out(cmd, output)
  150. reg    char    *cmd;
  151. reg    char    *output;
  152. {
  153.     reg    Out    *out;
  154.  
  155.     out = make(Out);
  156.     out->o_cmd = cmd;
  157.     out->o_out = output;
  158.     insert_table(out_tab, out);
  159. }
  160.  
  161. /*
  162. **    Return the output if any associated with a given command.
  163. */
  164.  
  165. char *
  166. get_out(cmd)
  167. reg    char    *cmd;
  168. {
  169.     reg    Out    *out;
  170.  
  171. #ifdef    STATS_FILE
  172.     out_tried++;
  173. #endif
  174.  
  175.     if ((out = (Out *) lookup_table(out_tab, cmd)) == NULL)
  176.         return NULL;
  177.  
  178. #ifdef    STATS_FILE
  179.     out_found++;
  180. #endif
  181.  
  182.     return out->o_out;
  183. }
  184.  
  185. Cast
  186. out_key(entry)
  187. reg    Cast    entry;
  188. {
  189.     return (Cast) ((Out *) entry)->o_cmd;
  190. }
  191.  
  192. bool
  193. out_equal(key1, key2)
  194. reg    Cast    key1, key2;
  195. {
  196. #ifdef    EXTRACHECK
  197.     if (key1 != key2 && streq((char *) key1, (char *) key2))
  198.     {
  199.         fprintf(stderr, "cake internal error: inconsistency in out_equal\n");
  200.         exit_cake(TRUE);
  201.     }
  202. #endif
  203.  
  204.     return key1 == key2;
  205. }
  206.  
  207. /**********************************************************************/
  208. /*    Command status table                          */
  209.  
  210. #ifdef    STATS_FILE
  211. int    stat_tried = 0;
  212. int    stat_found = 0;
  213. #endif
  214.  
  215. /*
  216. **    Save a command and its status.
  217. */
  218.  
  219. new_stat(cmd, status)
  220. reg    char    *cmd;
  221. reg    int    status;
  222. {
  223.     reg    Stat    *stat;
  224.  
  225.     stat = make(Stat);
  226.     stat->s_cmd  = cmd;
  227.     stat->s_stat = status;
  228.     insert_table(stat_tab, stat);
  229. }
  230.  
  231. /*
  232. **    Return the status if any associated with a given command.
  233. */
  234.  
  235. bool
  236. get_stat(cmd, code)
  237. reg    char    *cmd;
  238. reg    int    *code;
  239. {
  240.     reg    Stat    *stat;
  241.  
  242. #ifdef    STATS_FILE
  243.     stat_tried++;
  244. #endif
  245.  
  246.     if ((stat = (Stat *) lookup_table(stat_tab, cmd)) == NULL)
  247.         return FALSE;
  248.  
  249. #ifdef    STATS_FILE
  250.     stat_found++;
  251. #endif
  252.  
  253.     *code = stat->s_stat;
  254.     return TRUE;
  255. }
  256.  
  257. Cast
  258. stat_key(entry)
  259. reg    Cast    entry;
  260. {
  261.     return (Cast) ((Stat *) entry)->s_cmd;
  262. }
  263.  
  264. bool
  265. stat_equal(key1, key2)
  266. reg    Cast    key1, key2;
  267. {
  268. #ifdef    EXTRACHECK
  269.     if (key1 != key2 && streq((char *) key1, (char *) key2))
  270.     {
  271.         fprintf(stderr, "cake internal error: inconsistency in stat_equal\n");
  272.         exit_cake(TRUE);
  273.     }
  274. #endif
  275.  
  276.     return key1 == key2;
  277. }
  278.