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

  1. /*
  2. **    Module to handle Cake's tests.
  3. */
  4.  
  5. static    char
  6. rcs_id[] = "$Header: /mip/zs/src/sys/cake/RCS/test.c,v 1.15 87/10/05 20:16:34 zs Exp $";
  7.  
  8. #include    "cake.h"
  9.  
  10. bool
  11. eval(node, test, env)
  12. reg    Node    *node;
  13. reg    Test    *test;
  14. Env        env;
  15. {
  16.     extern    char    *expand_cmds();
  17.     extern    int    cake_proc();
  18.     extern    Wait    cake_wait();
  19.     extern    Node    *chase();
  20.     extern    bool    get_stat();
  21.     extern    bool    exist();
  22.     extern    char    *ground();
  23.     char        buf[256];
  24.     Wait        status;
  25.     reg    List    *ptr;
  26.     reg    Pat    *pat;
  27.     reg    char    *text1, *text2;
  28.     reg    char    *cmd;
  29.     reg    Node    *chasenode;
  30.     reg    int    result;
  31.     reg    int    pid;
  32.  
  33.     if (test == (Test *) NULL)
  34.         return TRUE;
  35.  
  36. #ifdef    CAKEDEBUG
  37.     if (cakedebug)
  38.     {
  39.         printf("testing ");
  40.         print_test(test);
  41.         printf("\n");
  42.     }
  43. #endif
  44.  
  45.     switch (test->t_kind)
  46.     {
  47.  
  48. when t_TRUE:    return TRUE;
  49. when t_FALSE:    return FALSE;
  50. when t_AND:    return eval(node, test->t_left, env) && eval(node, test->t_right, env);
  51. when t_OR:    return eval(node, test->t_left, env) || eval(node, test->t_right, env);
  52. when t_NOT:    return ! eval(node, test->t_left, env);
  53.  
  54. when t_CMD:    if (get_stat(test->t_cmd, &status.w_status))
  55.         {
  56.             test->t_kind = (status.w_status == 0)? t_TRUE: t_FALSE;
  57.             cdebug("test cmd cache %s: %s\n", test->t_cmd,
  58.                 (status.w_status == 0)? "True": "False");
  59.             return (status.w_status == 0)? TRUE: FALSE;
  60.         }
  61.  
  62.         cmd = expand_cmds(ground(env, test->t_cmd));
  63.         pid = cake_proc(cmd, Exec, "/dev/null", (Node *) NULL,
  64.             (int (*)()) NULL, (List *) NULL);
  65.         status = cake_wait(pid);
  66.         new_stat(test->t_cmd, status.w_status);
  67.         test->t_kind = (status.w_status == 0)? t_TRUE: t_FALSE;
  68.         cdebug("test cmd %s: %s\n", test->t_cmd,
  69.             (status.w_status == 0)? "True": "False");
  70.         return (status.w_status == 0)? TRUE: FALSE;
  71.  
  72. when t_MATCH:    text1 = (char *) first(test->t_list);    /* -vX    */
  73.         text2 = (char *) last(test->t_list);    /* file    */
  74.         /* e.g.    sub -vX X.c NULL file.c */
  75.         sprintf(buf, "sub %s %s NULL %s > /dev/null",
  76.             text1, text2, test->t_pat->p_str);
  77.  
  78.         cmd = new_name(buf);
  79.         cdebug("matching command: %s\n", cmd);
  80.         if (get_stat(cmd, &status.w_status))
  81.         {
  82.             test->t_kind = (status.w_status == 0)? t_TRUE: t_FALSE;
  83.             cdebug("test cmd cache %s: %s\n", test->t_cmd,
  84.                 (status.w_status == 0)? "True": "False");
  85.             return (status.w_status == 0)? TRUE: FALSE;
  86.         }
  87.  
  88.         pid = cake_proc(cmd, Exec, "/dev/null", (Node *) NULL,
  89.             (int (*)()) NULL, (List *) NULL);
  90.         status = cake_wait(pid);
  91.         new_stat(test->t_cmd, status.w_status);
  92.         test->t_kind = (status.w_status == 0)? t_TRUE: t_FALSE;
  93.         cdebug("test cmd %s: %s\n", test->t_cmd,
  94.             (status.w_status == 0)? "True": "False");
  95.         return (status.w_status == 0)? TRUE: FALSE;
  96.  
  97. when t_LIST:    for_list (ptr, test->t_list)
  98.         {
  99.             pat = (Pat *) ldata(ptr);
  100.             if (streq(test->t_pat->p_str, pat->p_str))
  101.                 return TRUE;
  102.         }
  103.  
  104.         return FALSE;
  105.  
  106. when t_EXIST:    result = exist(test->t_pat->p_str);
  107.         cdebug("test exist %s: %s\n", test->t_pat->p_str,
  108.             result? "True": "False");
  109.         return result;
  110.  
  111. when t_CANDO:    chasenode = chase(test->t_pat->p_str, 0, (Entry *) NULL);
  112.         if (on_node(chasenode, nf_ERR))
  113.         {
  114.             sprintf(scratchbuf, "cannot evaluate 'cando %s' test for %s",
  115.                 chasenode->n_name, node->n_name);
  116.             add_error(node, new_name(scratchbuf), LNULL, TRUE);
  117.         }
  118.  
  119.         result = is_ok(chasenode) || is_cando(chasenode);
  120.         cdebug("test cando %s: %s\n", test->t_pat->p_str,
  121.             result? "True": "False");
  122.         return result;
  123.  
  124. when t_OK:    chasenode = chase(test->t_pat->p_str, 0, (Entry *) NULL);
  125.         if (on_node(chasenode, nf_ERR))
  126.         {
  127.             sprintf(scratchbuf, "cannot evaluate 'ok %s' test for %s",
  128.                 chasenode->n_name, node->n_name);
  129.             add_error(node, new_name(scratchbuf), LNULL, TRUE);
  130.         }
  131.  
  132.         result = is_ok(chasenode);
  133.         cdebug("test ok %s: %s\n", test->t_pat->p_str,
  134.             result? "True": "False");
  135.         return result;
  136.  
  137. otherwise:    fprintf(stderr, "cake internal error: invalid test type %x in eval\n",
  138.             test->t_kind);
  139.         exit_cake(TRUE);
  140.     }
  141.  
  142.     /*NOTREACHED*/
  143.     return FALSE;
  144. }
  145.