home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d107 / csh.lha / Csh / run.c < prev    next >
C/C++ Source or Header  |  1987-10-31  |  3KB  |  131 lines

  1.  
  2. /*
  3.  * RUN.C
  4.  *
  5.  * (c)1986 Matthew Dillon     9 October 1986
  6.  *
  7.  *    RUN   handles running of external commands.
  8.  *
  9.  * Version 2.07M by Steve Drew 10-Sep-87
  10.  *
  11.  */
  12.  
  13. #include "shell.h"
  14.  
  15. char *FindIt();
  16.  
  17. do_run(str)
  18. char *str;
  19. {
  20.    int i, len, try = -1;
  21.    int run = 0;
  22.    char buf[200]; /* enough space for 100 char cmd name + path stuff */
  23.    char runcmd[200];
  24.    char *save, *path, *index();
  25.  
  26.    char *p = av[0];
  27.    char **args = av+1;
  28.    
  29.    while(*p++) *p &= 0x7F;    /* allow "com mand" */
  30.  
  31.    while(*args) {                  /* if any arg contains a space then */
  32.       if (index(*args,' ')) {      /* surround with quotes, since must */
  33.          i = strlen(*args);        /* of specified via "arg u ment" on */
  34.      movmem(*args,(*args)+1,i);/* original command line.           */
  35.          args[0][0] = args[0][i+1] = '\"';  /* mpush in execom.c has   */
  36.          args[0][i+2] = '\0';      /* allowed for these 2 extra bytes. */
  37.       }
  38.       ++args;
  39.    }
  40.    if ((len = strlen(av[0])) > 100) {
  41.        ierror(NULL,509);
  42.     return(-1);
  43.    }   
  44.    if (path = FindIt(av[0],"",buf)) {
  45.       if (!strcmp(av[0],"run")) {          /* was a run */
  46.      if (FindIt(av[1],"",runcmd)) {
  47.         run = 1;
  48.         save = av[1];
  49.         av[1] = runcmd;
  50.      }
  51.       }
  52.       if ((try = fexecv(path, av)) == 0)
  53.      i = wait();
  54.       if (run) av[1] = save;
  55.    }
  56.    else {
  57.        
  58.       APTR original;
  59.       original = Myprocess->pr_WindowPtr;
  60.       Myprocess->pr_WindowPtr = (APTR)(-1);
  61.       /*
  62.        * manx's fexev code only allows us 38
  63.        * chars for command name. 
  64.        */
  65.       if (len > 37) av[0][37] = '\0'; 
  66.       if ((try = fexecv(av[0], av)) == 0)
  67.      i = wait();
  68.      
  69.       Myprocess->pr_WindowPtr = original;
  70.    }
  71.    if (try) {
  72.       char *copy;
  73.     
  74.       if ((path = FindIt(av[0],".sh",buf)) == NULL) {
  75.      fprintf(stderr,"Command Not Found %s\n",av[0]);
  76.      return (-1);
  77.       }
  78.       av[1] = buf;         /* particular to do_source() */
  79.       copy = malloc(strlen(str)+3);
  80.       strcpy(copy+2,str);
  81.       copy[0] = 'x';
  82.       copy[1] = ' ';
  83.       i = do_source(copy);
  84.       free(copy);
  85.    }
  86.    return (i);
  87. }
  88.  
  89.  
  90. char *
  91. FindIt(cmd, ext, buf)
  92. char *cmd;
  93. char *ext;
  94. char *buf;
  95. {
  96.    long lock = 0;
  97.    char hasprefix = 0;
  98.    APTR original;
  99.    char *ptr, *s = NULL;
  100.  
  101.    original = Myprocess->pr_WindowPtr;
  102.  
  103.    for (ptr = cmd; *ptr; ++ptr) {
  104.       if (*ptr == '/' || *ptr == ':')
  105.      hasprefix = 1;
  106.    }
  107.  
  108.    if (!hasprefix) {
  109.     Myprocess->pr_WindowPtr = (APTR)(-1);
  110.     s = get_var(LEVEL_SET, V_PATH);
  111.    }
  112.  
  113.    strcpy(buf, cmd);
  114.    strcat(buf, ext);
  115.    while ((lock = (long)Lock(buf, ACCESS_READ)) == 0) {
  116.       if (*s == NULL || hasprefix) break;
  117.       for(ptr = s; *s && *s != ','; s++) ;
  118.       strcpy(buf, ptr);
  119.       buf[s-ptr] = '\0';
  120.       strcat(buf, cmd);
  121.       strcat(buf, ext);
  122.       if (*s) s++;
  123.    }
  124.    Myprocess->pr_WindowPtr = original;
  125.    if (lock) {
  126.       UnLock(lock);
  127.       return(buf);
  128.    }
  129.    return(NULL);
  130. }
  131.