home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / misc_utils / dmake_440 / command.c < prev    next >
C/C++ Source or Header  |  1991-01-19  |  3KB  |  142 lines

  1.  
  2. /*
  3.  *  COMMAND.C
  4.  *
  5.  */
  6.  
  7. #include "defs.h"
  8. #include <exec/libraries.h>
  9. #include <libraries/dos.h>
  10. #include <libraries/dosextens.h>
  11.  
  12. #define BTOC(bptr,ctype)    ((ctype *)((long)bptr << 2))
  13. #define CTOB(ptr)           ((long)(ptr) >> 2)
  14.  
  15. typedef struct CommandLineInterface    CLI;
  16. typedef struct Process            Process;
  17.  
  18. extern struct Library *SysBase;
  19.  
  20. ExecuteCmdList(list)
  21. MLIST *list;
  22. {
  23.     register NODE *node;
  24.     static __aligned char LBuf[1024];      /*  offset 1 for BCPL presert   */
  25.  
  26.     if (XDebug)
  27.     printf("ExecuteCmdList\n");
  28.     if (list) {
  29.     for (node = GetHead(list); node; node = GetSucc(node)) {
  30.         strcpy(LBuf + 1, node->ln_Name);
  31.         MacroReplace(1, LBuf + 1, strlen(LBuf + 1));
  32.         fprintf(stderr, "%s\n", LBuf + 1);
  33.         if (!ListOnly) {
  34.         if (Execute_Command(LBuf + 1))     /*  error!  */
  35.             xexit(1);
  36.         if (SetSignal(0,0) & SIGBREAKF_CTRL_C) {
  37.             puts("^C");
  38.             xexit(1);
  39.         }
  40.         }
  41.     }
  42.     }
  43.     if (XDebug)
  44.     puts("ExecuteCmdList END");
  45. }
  46.  
  47. /*
  48.  *  cmd[-1] is valid space
  49.  */
  50.  
  51. Execute_Command(cmd)
  52. register char *cmd;
  53. {
  54.     register char *ptr;
  55.     short Ignore = 0;
  56.  
  57.     while (*cmd == ' ' || *cmd == '\t')
  58.     ++cmd;
  59.     if (*cmd == '-') {
  60.     ++cmd;
  61.     Ignore = 1;
  62.     }
  63.     for (ptr = cmd; *ptr && *ptr != ' ' && *ptr != '\t'; ++ptr);
  64.     if (ptr - cmd == 2 && (cmd[0]|0x20) == 'c' && (cmd[1]|0x20) == 'd') {
  65.     long lock;
  66.  
  67.     while (*ptr == ' ' || *ptr == '\t')
  68.         ++ptr;
  69.     if (*ptr)
  70.         lock = Lock(ptr, SHARED_LOCK);
  71.     else
  72.         lock = DupLock(SaveLock);
  73.     if (lock)
  74.         UnLock(CurrentDir(lock));
  75.     else
  76.         printf("Unable to cd %s\n", ptr);
  77.     return(0);
  78.     }
  79.  
  80.     {
  81.     register short i;
  82.     register short c;
  83.     register short err = 0;
  84.  
  85.     for (i = 0; cmd[i] && cmd[i] != ' ' && cmd[i] != 9; ++i);
  86.     c = cmd[i];
  87.     cmd[i] = 0;
  88.  
  89. #if INCLUDE_VERSION >= 36
  90.     if (SysBase->lib_Version >= 36) {
  91.         long seg;
  92.         long lock;
  93.         Process *proc = FindTask(NULL);
  94.         CLI *cli = BTOC(proc->pr_CLI, CLI);
  95.         long oldCommandName;
  96.  
  97.         if (cli) {
  98.         oldCommandName = (long)cli->cli_CommandName;
  99.         cmd[-1] = i;
  100.         cli->cli_CommandName = CTOB(cmd - 1);
  101.         }
  102.  
  103.         if (seg = FindSegment(cmd, 0L, 0)) {
  104.         err = RunCommand(((long *)seg)[2], 8192, cmd + i + 1, strlen(cmd + i + 1));
  105.         } else if ((lock = _SearchPath(cmd)) && seg = LoadSegLock(lock)) {
  106.         err = RunCommand(seg, 8192, cmd + i + 1, strlen(cmd + i + 1));
  107.         UnLoadSeg(seg);
  108.         } else {
  109.         cmd[i] = c;
  110.         err = System(cmd, NULL);
  111.         }
  112.         if (cli)
  113.         cli->cli_CommandName = (BSTR)oldCommandName;
  114.     } else
  115. #endif
  116.     {
  117.         cmd[i] = c;
  118.         Execute(cmd, NULL, NULL);
  119.         err = 0;
  120.     }
  121.     if (err)
  122.         printf("Error code %d %s\n", err, (Ignore) ? "(Ignored)":"");
  123.     if (Ignore)
  124.         return(0);
  125.     return(err);
  126.     }
  127. }
  128.  
  129. LoadSegLock(lock)
  130. long lock;
  131. {
  132.     long oldLock;
  133.     long seg;
  134.  
  135.     oldLock = CurrentDir(lock);
  136.     seg = LoadSeg("");
  137.     CurrentDir(oldLock);
  138.     return(seg);
  139. }
  140.  
  141.  
  142.