home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / System / Sysmon / src / RunBackground.c < prev    next >
C/C++ Source or Header  |  2000-05-30  |  3KB  |  127 lines

  1. /*
  2. **    RunBackground.c
  3. **    Executes a command in a background shell (like Run <>NIL:), but allows
  4. **    priority, stacksize, etc. to be specified on the command line.
  5. **    Requires V36 or higher.
  6. **
  7. **    Written on 30/08/1994 by Etienne Vogt (Public domain)
  8. */
  9.  
  10. #include <exec/alerts.h>
  11. #include <dos/dosextens.h>
  12. #include <dos/rdargs.h>
  13. #include <dos/dostags.h>
  14. #include <workbench/startup.h>
  15. #define __USE_SYSBASE
  16. #include <proto/exec.h>
  17. #include <proto/dos.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20.  
  21. struct ExecBase *SysBase;
  22. static struct RDArgs *myrda;
  23. static struct WBStartup *wbmsg;
  24.  
  25. ULONG __saveds main(void);
  26. static void clearall(void);
  27.  
  28. static UBYTE template[] = "STACK=STACKSIZE/K/N,PRI=PRIORITY/K/N,DELAY/K/N,NOREQ/S,COMMAND/F/A";
  29. static UBYTE version[] = "$VER: RunBackground 0.3 (19.11.95)";
  30.  
  31. #define OPT_STACKSIZE    0
  32. #define OPT_PRIORITY    1
  33. #define OPT_DELAY    2
  34. #define OPT_NOREQ    3
  35. #define OPT_COMMAND    4
  36. #define OPTMAX        5
  37.  
  38.  
  39. ULONG __saveds main(void)    /* No startup code */
  40. {
  41.   struct Process *myproc;
  42.   struct CommandLineInterface *myCLI;
  43.   LONG opts[OPTMAX];
  44.   ULONG rc = 0;
  45.   ULONG stacksize;
  46.   LONG priority, noreq = 0;
  47.   BPTR fhin, fhout;
  48.  
  49.   SysBase = *(struct ExecBase **)4;
  50.   DOSBase = NULL;
  51.   wbmsg = NULL;
  52.   myrda = NULL;
  53.  
  54.   myproc = (struct Process *)FindTask(NULL);
  55.   if ((DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",36)) == NULL)
  56.   { Alert(AT_Recovery|AG_OpenLib|AO_DOSLib);
  57.     clearall();
  58.     return 100;
  59.   }
  60.  
  61.   if (!(myCLI = (struct CommandLineInterface *)BADDR(myproc->pr_CLI)))
  62.   { WaitPort(&myproc->pr_MsgPort);    /* If started from WB, exit cleanly */
  63.     wbmsg = (struct WBStartup *)GetMsg(&myproc->pr_MsgPort);
  64.     clearall();
  65.     return 20;
  66.   }
  67.   else
  68.   { memset((char *)opts, 0, sizeof(opts));
  69.     if ((myrda = ReadArgs(template, opts, NULL)) == NULL)
  70.     { PrintFault(IoErr(),NULL);
  71.       clearall();
  72.       return 20;
  73.     }
  74.  
  75.     if (opts[OPT_STACKSIZE]) stacksize = *(ULONG *)opts[OPT_STACKSIZE];
  76.     else stacksize = myCLI->cli_DefaultStack << 2;
  77.  
  78.     if (opts[OPT_PRIORITY]) priority = *(LONG *)opts[OPT_PRIORITY];
  79.     else priority = myproc->pr_Task.tc_Node.ln_Pri;
  80.     if (priority < -128 || priority > 127)
  81.     { Printf("Invalid priority value %ld.\n", priority);
  82.       clearall();
  83.       return 20;
  84.     }
  85.  
  86.     if (opts[OPT_NOREQ]) noreq = -1;
  87.  
  88.     if (!(fhin = Open("NIL:", MODE_OLDFILE)))
  89.     { PrintFault(IoErr(), "Error opening input stream");
  90.       clearall();
  91.       return 20;
  92.     }
  93.     if (!(fhout = Open("NIL:", MODE_OLDFILE)))
  94.     { PrintFault(IoErr(), "Error opening output stream");
  95.       clearall();
  96.       return 20;
  97.     }
  98.  
  99.     if (SystemTags((STRPTR)opts[OPT_COMMAND],
  100.         SYS_Input,    fhin,
  101.         SYS_Output,    fhout,
  102.         SYS_Asynch,    TRUE,
  103.         SYS_UserShell,    TRUE,
  104.         NP_StackSize,    stacksize,
  105.         NP_Priority,    priority,
  106.         NP_WindowPtr,    noreq,
  107.         TAG_DONE ))
  108.     { PrintFault(IoErr(), "Error starting command");
  109.       rc = 10;
  110.     }
  111.  
  112.     if (opts[OPT_DELAY]) Delay(*(ULONG *)opts[OPT_DELAY]);
  113.   }
  114.   clearall();
  115.   return rc;
  116. }
  117.  
  118. static void clearall(void)
  119. {
  120.   if (myrda) FreeArgs(myrda);
  121.   if (DOSBase) CloseLibrary((struct Library *)DOSBase);
  122.   if (wbmsg)
  123.   { Forbid();
  124.     ReplyMsg((struct Message *)wbmsg);
  125.   }
  126. }
  127.