home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 488.lha / csh_v5.0 / src / csh500src.lzh / run.c < prev    next >
C/C++ Source or Header  |  1991-02-24  |  5KB  |  226 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.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  11.  * Version 5.00L by Urban Mueller 17-Feb-91
  12.  *
  13.  */
  14.  
  15. #include "shell.h"
  16.  
  17. char *rindex();
  18. int MySyncRun( char *com, char *args, BPTR in, BPTR out );
  19. int echofunc(void);
  20. long IoError;
  21.  
  22. int
  23. do_run( char *str )
  24. {
  25.     int retcode;
  26.     char buf[200]; /* enough space for 100 char cmd name + path stuff */
  27.     char *path, *argline, *trueargline, *copy, *ext, *dst, *end;
  28.  
  29.     if( !*av[0] )
  30.         return 0;
  31.  
  32.     if( (retcode=echofunc())>=0 )
  33.         return retcode;
  34.  
  35.     a0tospace( av[0] );                                 /* allow "com mand" */
  36.  
  37.     IoError=0;
  38.     if( ac==1 && (isdir(av[0]) || !strcmp( av[0],"..") || !strcmp(av[0],"~"))) {
  39.         if( !strcmp(av[0],"~") && (dst=get_var(LEVEL_SET,v_lcd)) )
  40.             sprintf(buf,"cd %s",dst);
  41.         else 
  42.             sprintf(buf,"cd %s",av[0]);
  43.         execute( buf );
  44.         return 0;
  45.     }
  46.  
  47.     if( (IoError==218 || IoError==225 || IoError==226) && index(av[0],':')) {
  48.         ierror( av[0], IoError );
  49.         return 20;
  50.     }
  51.  
  52.     argline=compile_av(av, 1, ac, ' ', 1);
  53.     trueargline= (*argline ? argline : "\n");
  54.  
  55.     if (strlen(av[0]) > 100) { ierror(NULL,509); return -1; }
  56.  
  57.     sprintf(buf,"res_%s",BaseName(av[0]));              /* delayed residents */
  58.     if (o_resident && Getenv(buf, buf+100, 90L) && loadres(av[0]))
  59.         Setenv(buf,NULL);
  60.  
  61.     if( (retcode=MySyncRun(av[0],trueargline,0L,0L))>=0 )   /* AmigaDOS path */
  62.         goto done2;
  63.  
  64.     strcpy(buf,"source ");
  65.     if (path = dofind(av[0],"",buf+7,v_path)) {             /* shell path    */
  66.         if((retcode = MySyncRun(path,trueargline,0L,0L))>=0)
  67.             goto done2;
  68.         else {
  69.             struct DPTR *dp;
  70.             int stat;
  71.             if(dp=dopen(path,&stat)) {
  72.                 stat=dp->fib->fib_Protection & FIBF_SCRIPT;
  73.                 dclose(dp);
  74.                 if( stat ) {
  75.                     execute(buf);
  76.                     return 0;
  77.                 }
  78.             }
  79.         }
  80.     }
  81.  
  82.     if(!(end=rindex(av[0],'.'))) end="";               /* automatic sourcing */
  83.     ext=strcmp(end,".sh") ? ".sh" : "";
  84.     if (path = dofind(av[0],ext,buf,v_path)) {
  85.         av[1] = buf;
  86.         copy = malloc(strlen(str)+3);
  87.         sprintf(copy,"x %s",str);
  88.         retcode = do_source(copy);
  89.         goto done;
  90.     }
  91.  
  92.     copy=malloc(strlen(av[0])+strlen(argline)+5);
  93.     sprintf(copy,"%s %s",av[0],trueargline);
  94.  
  95.     ext=strcmp(end,".rexx") ? ".rexx" : "";           /* automatic rx-ing   */
  96.     if( path = dofind(av[0], ext, buf, v_rxpath )) {
  97.         if( (retcode=MySyncRun("rx",copy,0L,0L)) >=0 ) goto done;
  98.         if (path = dofind("rx","",buf,v_path)) {
  99.             retcode = MySyncRun(path,copy,0L,0L);
  100.             goto done;
  101.         }
  102.     }
  103.  
  104.     if( !doaction(av[0],"exec",argline)) {
  105.         retcode=0;
  106.         goto done;
  107.     }
  108.  
  109.     retcode=-1;
  110.     fprintf(stderr,"Command Not Found %s\n",av[0]);
  111.  
  112. done:
  113.     free( copy );
  114. done2:
  115.     free( argline );
  116.     return retcode;
  117. }
  118.  
  119. struct Segment {
  120.     BPTR NextEntry;
  121.     LONG UseCount;
  122.     BPTR SegPtr;
  123.     BSTR SegName;
  124. };
  125.  
  126. int
  127. MySyncRun( char *com, char *args, BPTR in, BPTR out )
  128. {
  129.     struct Segment *seg;
  130.     int ret;
  131.     char buf2[84], *buf=buf2;
  132.     long oldname;
  133.  
  134. #ifdef KICK20
  135.     if( o_kick20 ) {
  136.         oldname = (long)Mycli->cli_CommandName;
  137.  
  138.         while( (long)buf & 3 ) buf++;
  139.         buf[0] = strlen( com );
  140.         strncpy(buf+1,com,80);
  141.  
  142.         Forbid();
  143.         seg=FindSegment( com, NULL, 0 );
  144.         Permit();
  145.         if( seg ) {
  146.             Mycli->cli_CommandName = (long)buf/4;
  147.             seg->UseCount++;
  148.  
  149.             ret=RunCommand(seg->SegPtr, Mycli->cli_DefaultStack,
  150.                                              args, strlen(args));
  151.             seg->UseCount--;
  152.             Mycli->cli_CommandName = (long)oldname;
  153.             return ret;
  154.         }
  155.  
  156.         if( o_internal ) {
  157.             Forbid();
  158.             seg=FindSegment( com, NULL, 1 );
  159.             Permit();
  160.             if( seg ) {
  161.                 Mycli->cli_CommandName = (long)buf/4;
  162.  
  163.                 ret=RunCommand(seg->SegPtr, Mycli->cli_DefaultStack,
  164.                                                  args, strlen(args));
  165.                 Mycli->cli_CommandName = (long)oldname;
  166.                 return ret;
  167.             }
  168.         }
  169.  
  170.     }
  171. #endif
  172.     if( (ret= SyncRun( com, args, in, out ))>=0 )
  173.         return ret;
  174.  
  175.     return ret;
  176. }
  177.  
  178. int
  179. do_which( char *str )
  180. {
  181.     char *got, *com=av[1];
  182.  
  183.     if( get_var(LEVEL_ALIAS,com) ) {
  184.         printf("Shell Alias '%s'\n",com);
  185.         return 0;
  186.     }
  187.  
  188.     if( *(got=find_internal( com ))>1 ) {
  189.         printf("Shell Internal '%s'\n",got);
  190.         return 0;
  191.     }
  192.  
  193.  
  194.  
  195.     printf( "Not found\n" );
  196.     return 20;
  197. }
  198.  
  199.  
  200. char *
  201. dofind( char *cmd, char *ext, char *buf, char *path)
  202. {
  203.     char *ptr, *s=path;
  204.  
  205.     Myprocess->pr_WindowPtr = (APTR)(-1);
  206.     sprintf(buf,"%s%s",cmd,ext);
  207.     if (exists(buf)) return buf;
  208.     if (BaseName(buf)==buf) {
  209.         if( *path=='_' )
  210.             s = get_var(LEVEL_SET, path);
  211.         while (*s) {
  212.             for (ptr=buf; *s && *s!=','; ) *ptr++ = *s++;
  213.             if( ptr[-1]!=':' && ptr[-1]!='/')
  214.                 *ptr++='/';
  215.             sprintf(ptr, "%s%s", cmd, ext);
  216.             if (exists(buf)) {
  217.                 Myprocess->pr_WindowPtr = (APTR)o_noreq;
  218.                 return buf;
  219.             }
  220.             if (*s) s++;
  221.         }
  222.     }
  223.     Myprocess->pr_WindowPtr = (APTR)o_noreq;
  224.     return NULL;
  225. }
  226.