home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 182.lha / CShell_v3.0a / run.c < prev    next >
C/C++ Source or Header  |  1988-04-28  |  5KB  |  222 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.  * Version 3.00A by Carlo Borreo & Cesare Dieni 30-Oct-88
  12.  *
  13.  */
  14.  
  15. extern char *v_path;
  16. char *FindIt();
  17.  
  18. do_run(str)
  19. char *str;
  20. {
  21. int i, len, retcode;
  22. char buf[200]; /* enough space for 100 char cmd name + path stuff */
  23. char *path;
  24.  
  25. char *p = av[0];
  26. char **args = av+1;
  27.  
  28. while(*p++) *p &= 0x7F;      /* allow "com mand" */
  29.  
  30. while(*args) {                /* if any arg contains a space then */
  31.     if (index(*args,' ')) {        /* surround with quotes, since must */
  32.     i = strlen(*args);        /* of specified via "arg u ment" on */
  33.     movmem(*args,(*args)+1,i);    /* original command line.        */
  34.     args[0][0] = args[0][i+1] = '\"';    /* mpush in execom.c has    */
  35.     args[0][i+2] = '\0';        /* allowed for these 2 extra bytes. */
  36.     }
  37.     ++args;
  38.     }
  39. if ((len = strlen(av[0])) > 100) { ierror(NULL,509); return -1; }
  40. if (path = FindIt(av[0],"",buf)) retcode = myfexecv(path, av);
  41. else {
  42.     APTR original;
  43.     original = Myprocess->pr_WindowPtr;
  44.     Myprocess->pr_WindowPtr = (APTR)(-1);
  45.     /*
  46.      * manx's fexecv code only allows us 38
  47.      * chars for command name.
  48.      */
  49.     if (len > 37) av[0][37] = '\0';
  50.     retcode = myfexecv(av[0], av);
  51.     Myprocess->pr_WindowPtr = original;
  52.     }
  53. if (retcode < 0) {
  54.     char *copy;
  55.     if ((path = FindIt(av[0],".sh",buf)) == NULL) {
  56.         fprintf(stderr,"Command Not Found %s\n",av[0]);
  57.         return -1;
  58.         }
  59.     av[1] = buf;               /* particular to do_source() */
  60.     copy = malloc(strlen(str)+3);
  61.     sprintf(copy,"x %s",str);
  62.     retcode = do_source(copy);
  63.     free(copy);
  64.     }
  65. return retcode;
  66. }
  67.  
  68. char *dofind(cmd, ext, buf)
  69. char *cmd, *ext, *buf;
  70. {
  71. char *ptr, *s;
  72.  
  73. sprintf(buf,"%s%s",cmd,ext);
  74. if (exists(buf)) return buf;
  75. if (BaseName(buf)==buf) {
  76.     s = get_var(LEVEL_SET, v_path);
  77.     while (*s) {
  78.         for (ptr=buf; *s && *s!=','; ) *ptr++ = *s++;
  79.         sprintf(ptr, "%s%s", cmd, ext);
  80.         if (exists(buf)) return buf;
  81.         if (*s) s++;
  82.         }
  83.     }
  84. return NULL;
  85. }
  86.  
  87. char *FindIt(cmd,ext,buf)
  88. char *cmd, *ext, *buf;
  89. {
  90. APTR original;
  91. char *response;
  92.  
  93. original=Myprocess->pr_WindowPtr;
  94. Myprocess->pr_WindowPtr = (APTR)(-1);
  95. response=dofind(cmd,ext,buf);
  96. Myprocess->pr_WindowPtr = original;
  97. return response;
  98. }
  99.  
  100. myfexecv(cmd, argv)
  101. char *cmd, **argv;
  102. {
  103. long ret_val;
  104. struct FileHandle *fhp;
  105. APTR sav_ret;
  106. register char **ap, *cp, *arg;
  107. int i;
  108. long len, seg, sav, stksiz;
  109. char buf[40];
  110. union {
  111.     long *lp;
  112.     long ll;
  113.     } l, stk;
  114. long oldcin, oldcout;
  115. long doexec();
  116. extern long _savsp;
  117.  
  118. if (seg = LoadPrg(cmd)) goto found;
  119. l.lp = (long *) Mycli->cli_CommandDir;
  120. while (l.ll) {
  121.     l.ll <<= 2;
  122.     sav = CurrentDir(l.lp[1]);
  123.     seg = LoadPrg(cmd);
  124.     CurrentDir(sav);
  125.     if (seg) goto found;
  126.     l.ll = *l.lp;
  127.     }
  128. strcpy(buf, "c:");
  129. strcat(buf, cmd);
  130. if (seg = LoadPrg(buf)) goto found;
  131. return -1;
  132.  
  133. found:
  134.  
  135. stksiz = 4 * Mycli->cli_DefaultStack;
  136. if ((stk.lp = AllocMem(stksiz+8, 0L)) == 0) {
  137.     UnLoadPrg(seg);
  138.     return -1;
  139.     }
  140. for (len=1,ap=argv+1;*ap;ap++)
  141.     len += strlen(*ap) + 1;
  142. if ((cp = arg = AllocMem(len, 0L)) == 0) {
  143.     UnLoadPrg(seg);
  144.     FreeMem(stk.lp, stksiz+8);
  145.     return -1;
  146.     }
  147. *stk.lp = stksiz + 8;
  148. stk.ll += stksiz;
  149. stk.lp[0] = stksiz;
  150. stk.lp[1] = ((long *)_savsp)[2];
  151. sav_ret = Myprocess->pr_ReturnAddr;
  152. Myprocess->pr_ReturnAddr = (APTR) stk.lp;
  153.  
  154. sav = Mycli->cli_Module;
  155. Mycli->cli_Module = seg;
  156.  
  157. for (ap=argv+1;*ap;ap++) {
  158.     strcpy(cp, *ap);
  159.     strcat(cp, " ");
  160.     cp += strlen(cp);
  161.     }
  162. arg[len-1] = '\n';
  163.  
  164. cp = (char *)((long)Mycli->cli_CommandName << 2);
  165. movmem(cp, buf, 40);
  166. strcpy(cp+1, cmd);
  167. cp[0] = strlen(cmd);
  168.  
  169. fhp = (struct FileHandle *) (Myprocess->pr_CIS << 2);
  170. strncpy(fhp->fh_Buf<<2, arg, (int)(len < 200?len:199));
  171. fhp->fh_Pos = 0;
  172. fhp->fh_End = len < 200?len:199;
  173. oldcin = Myprocess->pr_CIS;
  174. oldcout = Myprocess->pr_COS;
  175.  
  176. ret_val = doexec(len, stksiz, stksiz+8, len, arg, (seg+1)<<2, stk.ll);
  177.  
  178. Myprocess->pr_CIS =  oldcin;
  179. Myprocess->pr_COS =  oldcout;
  180. fhp->fh_Pos = fhp->fh_End;
  181. UnLoadPrg(Mycli->cli_Module);
  182. Myprocess->pr_ReturnAddr = sav_ret;
  183. Mycli->cli_Module = sav;
  184. FreeMem(arg, len);
  185. movmem(buf, cp, 40);
  186. return ret_val;
  187. }
  188.  
  189. static long doexec()
  190. {
  191. #asm
  192.     movem.l    d3-d7/a2-a5,-(sp)        ;save registers
  193.     lea        savsp(pc),a0
  194.     move.l    sp,(a0)                    ;save our sp
  195.     movem.l    8(a5),d0/d2/d3/d4/a0/a4/a7    ;load params
  196.     move.l    4(sp),a3                ;get old sp from CLI
  197.     movem.l    4(a3),a1/a2/a5/a6        ;get BCPL environment
  198.     move.l    d0,12(a1)                ;set length
  199.     move.l    a0,d1                    ;copy to dreg
  200.     lsr.l    #2,d1                    ;convert to BPTR
  201.     move.l    d1,8(a1)                ;set ptr
  202.     move.l    a0,d1                    ;copy to d1 as well
  203.     jsr        (a4)                    ;call new program
  204.     movem.l    (sp)+,d2/d3                ;get stk siz and old sp
  205.     move.l    sp,a1                    ;save current sp
  206.     move.l    savsp(pc),sp            ;get back our sp
  207.     movem.l    (sp)+,d3-d7/a2-a5        ;get back registers
  208.     move.l    d0,-(sp)                ;save return code
  209.     sub.l    d2,a1                    ;back up a bit
  210.     sub.l    #8,a1                    ;back up over header
  211.     move.l    (a1),d0                    ;get size to free
  212.     move.l    4,a6                    ;get ExecBase
  213.     jsr        -210(a6)                ;free the memory
  214.     move.l    (sp)+,d0                ;get the return code
  215. #endasm
  216. }
  217.  
  218. #asm
  219. savsp:
  220.     dc.l    0
  221. #endasm
  222.