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

  1. /*
  2. **    Cake system interface.
  3. */
  4.  
  5. static    char
  6. rcs_id[] = "$Header: /mip/zs/src/sys/cake/RCS/sys.c,v 1.15 87/10/05 20:16:20 zs Exp $";
  7.  
  8. #include    "cake.h"
  9.  
  10. /*
  11. **    Change to the directory of the cakefile.
  12. **    Return the shortened name of the cakefile.
  13. */
  14.  
  15. char *
  16. dir_setup(name)
  17. reg    char    *name;
  18. {
  19.     char        buf[MAXSIZE];
  20.     reg    int    i, l;    /* l is the subscript of the last / */
  21.  
  22.     l = -1;
  23.     for (i = 0; name[i] != '\0'; i++)
  24.         if (name[i] == '/')
  25.             l = i;
  26.  
  27.     if (l < 0)
  28.         return name;
  29.     else
  30.     {
  31.         for (i = 0; i < l; i++)
  32.             buf[i] = name[i];
  33.         
  34.         if (i == 0)
  35.             buf[i++] = '/';
  36.  
  37.         buf[i] = '\0';
  38.         if (chdir(buf) != 0)
  39.         {
  40.             sprintf(scratchbuf, "cake system error, chdir %s", buf);
  41.             perror(scratchbuf);
  42.             exit(1);
  43.         }
  44.  
  45.         return name+l+1;
  46.     }
  47. }
  48.  
  49. /*
  50. **    Set up the arguments to shell exec's.
  51. **    pattern: execl("/bin/sh", "sh", "-c", s, 0);
  52. **    pattern: execl("/bin/csh", "csh", "-cf", s, 0);
  53. */
  54.  
  55. char    *shell_path[2];
  56. char    *shell_cmd[2];
  57. char    *shell_opt[2];
  58.  
  59. shell_setup(shell, which)
  60. reg    char    *shell;
  61. reg    int    which;
  62. {
  63.     char        buf[MAXSIZE];
  64.     reg    char    *s;
  65.     reg    int    i, l;
  66.  
  67.     /* find the shell path */
  68.     i = 0;
  69.     for (s = shell; *s != '\0' && *s != ' ' && *s != '\t'; s++)
  70.         buf[i++] = *s;
  71.     
  72.     buf[i] = '\0';
  73.     shell_path[which] = new_name(buf);
  74.  
  75.     for (; *s != '\0' && *s != '-'; s++)
  76.         ;
  77.  
  78.     /* find the options */
  79.     i = 0;
  80.     for (; *s != '\0' && *s != ' ' && *s != '\t'; s++)
  81.         buf[i++] = *s;
  82.     
  83.     buf[i] = '\0';
  84.     if (i != 0)
  85.         shell_opt[which] = new_name(buf);
  86.     else
  87.         shell_opt[which] = NULL;
  88.  
  89.     if (*s != NULL)
  90.     {
  91.         fprintf(stderr, "cake: cannot parse shell command '%s'\n", shell);
  92.         exit_cake(FALSE);
  93.     }
  94.  
  95.     /* find the shell command itself */
  96.     s = shell_path[which];
  97.     l = -1;
  98.     for (i = 0; s[i] != '\0'; i++)
  99.         if (s[i] == '/')
  100.             l = i;
  101.  
  102.     shell_cmd[which] = new_name(s+l+1);
  103.  
  104.     if (cakedebug)
  105.     {
  106.         printf("shell path%d: %s\n", which, shell_path[which]);
  107.         printf("shell cmd%d:  %s\n", which, shell_cmd[which]);
  108.         if (shell_opt[which] != NULL)
  109.             printf("shell opt%d:  %s\n", which, shell_opt[which]);
  110.         else
  111.             printf("shell opt%d:  NULL\n", which);
  112.     }
  113. }
  114.  
  115. bool    metatab[CHARSETSIZE];
  116.  
  117. /*
  118. **    Set up the metacharacter table.
  119. */
  120.  
  121. meta_setup(metachars)
  122. reg    char    *metachars;
  123. {
  124.     reg    int    i;
  125.     reg    char    *s;
  126.  
  127.     for (i = 0; i < CHARSETSIZE; i++)
  128.         metatab[i] = FALSE;
  129.     
  130.     for (s = metachars; *s != '\0'; s++)
  131.         metatab[*s] = TRUE;
  132. }
  133.  
  134. /*
  135. **    Decide whether the given string has metacharacters or not.
  136. **    The second arg tells whether to honour backslash escapes.
  137. */
  138.  
  139. bool
  140. has_meta(str, allow_esc)
  141. reg    char    *str;
  142. reg    bool    allow_esc;
  143. {
  144.     reg    char    *s;
  145.  
  146.     for (s = str; *s != '\0'; s++)
  147.         if (metatab[(unsigned) *s])
  148.             return TRUE;
  149.         or (allow_esc && (*s == '\\'))
  150.         {
  151.             if (s[1] != '\0')
  152.                 s++;
  153.         }
  154.     
  155.     return FALSE;
  156. }
  157.