home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / misc / MUser17src.lha / MultiUser / src / Support / RunCommand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-07  |  2.1 KB  |  82 lines

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * RunCommand - Run a Pogram using the Current Process            *
  5. * ---------------------------------------------------------    *
  6. * © Copyright 1993-1994 Geert Uytterhoeven                        *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <exec/execbase.h>
  14. #include <dos/dos.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. #include "string.h"
  18.  
  19. #include "RunCommand_rev.h"
  20.  
  21.  
  22. char __VersTag__[] = VERSTAG;
  23.  
  24.  
  25. int __saveds Start(char *arg)
  26. {
  27.     struct ExecBase *SysBase;
  28.     struct DosLibrary *DOSBase;
  29.     struct RDArgs *args;
  30.     LONG argarray[] = {
  31. #define argCOMMAND    0
  32. #define argARGS        1
  33.         NULL, NULL
  34.     };
  35.     int rc = RETURN_ERROR;
  36.     BPTR seglist;
  37.     ULONG stacksize;
  38.     STRPTR argptr;
  39.     ULONG argsize;
  40.     struct Process *proc;
  41.     struct CommandLineInterface *cli;
  42.  
  43.     SysBase = *(struct ExecBase **)4;
  44.  
  45.     if (!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) {
  46.         rc = ERROR_INVALID_RESIDENT_LIBRARY;
  47.         goto Exit;
  48.     }
  49.  
  50.     args = ReadArgs("COMMAND/A,ARGS/F", argarray, NULL);
  51.     if (!args || (!(seglist = NewLoadSeg((STRPTR)argarray[argCOMMAND], NULL))))
  52.         PrintFault(IoErr(), NULL);
  53.     else {
  54.         proc = (struct Process *)SysBase->ThisTask;
  55.         if (cli = (struct CommandLineInterface *)BADDR(proc->pr_CLI)) {
  56.             stacksize = cli->cli_DefaultStack*4;
  57.             if (argarray[argARGS]) {
  58.                 argsize = strlen((char *)argarray[argARGS])+1;
  59.                 if (argptr = AllocVec(argsize+1, MEMF_PUBLIC|MEMF_CLEAR)) {
  60.                     strcpy(argptr, (char *)argarray[argARGS]);
  61.                     strcat(argptr, "\n");
  62.                     rc = RunCommand(seglist, stacksize, argptr, argsize);
  63.                     FreeVec(argptr);
  64.                 } else
  65.                         PrintFault(IoErr(), NULL);
  66.             } else
  67.                 rc = RunCommand(seglist, stacksize, "\n", 1);
  68.             if (rc == -1) {
  69.                 rc = RETURN_FAIL;
  70.                 PutStr("Not enough memory for stack\n");
  71.             }
  72.             UnLoadSeg(seglist);
  73.         } else
  74.             PutStr("RunCommand must be used from a CLI\n");
  75.     }
  76.  
  77. Exit:
  78.     CloseLibrary((struct Library *)DOSBase);
  79.  
  80.     return(rc);
  81. }
  82.