home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 274.lha / RunBack_v5.0 / RunBack.c < prev    next >
C/C++ Source or Header  |  1989-07-24  |  5KB  |  180 lines

  1. /* RunBack.c version 5 */
  2. /* Author:  Rob Peck.  5/9/86 */
  3. /* Re-written by Greg Searle 7/14/89 */
  4.  
  5. /* Command format:  RUNBACK [[-]delay [priority]] command [argumentss...]
  6.  *
  7.  *    delay is an integer number of seconds to wait for the task to
  8.  *    load.  This may be used to eliminate "disk thrashing" when 
  9.  *    loading many tasks in sequence.  May be preceded with a dash.
  10.  *
  11.  *    priority sets the task's priority.  Recommended between -5 and
  12.  *    5, but may theoretically be between -127 and 127.  Don't precede
  13.  *    with a dash unless you wish the priority to be negative.
  14.  *
  15.  * Modified 7/3/89 by Greg Searle.  version 5.  Major Cleanup.
  16.  * Removed crash situation when program tries to input
  17.  * input or output to the CLI.
  18.  *
  19.  * Oh my!  What a little cleaning up a re-coding will do!  This had a whole lot
  20.  * of wrong turns and tangled code.  Now it works like a dream.  Even runs
  21.  * DPaint.
  22.  *
  23.  * Doesn't need to check if command exists anymore.
  24.  * Doesn't need to search path.  AmigaDOS does this automatically.
  25.  *
  26.  * Added priority setting.
  27.  *
  28.  * Parameter checking improved.  Knows when it sees a number.
  29.  * Treats everything after its own parameters as arguments.
  30.  *
  31.  * Removed obsolete comments.
  32.  */
  33.  
  34. /* Modified 5/21/88 by Dan Barrett.  version 4.
  35.  *
  36.  * [path searching removed in version 5.]
  37.  *
  38.  * A few "#ifdef AZTEC_C" lines had to be added.  It seems that
  39.  * Aztec C parses the command line differently from the way Lattice
  40.  * does with respect to arguments in quotes.  When I compiled this
  41.  * program with Aztec, all the quotes in quoted arguments were 
  42.  * disappearing.  I re-insert them around any argument that has a
  43.  * space character (' ') in it.
  44. */
  45.  
  46. /* #define DEBUG    /* Uncomment this line for debugging. */
  47.  
  48. #include "exec/types.h"
  49. #include "exec/memory.h"
  50. #include "exec/tasks.h"
  51. #include "libraries/dosextens.h"
  52.  
  53. extern struct FileHandle *Open();
  54.  
  55. main (argc, argv)
  56.   int argc;
  57.   char *argv[];
  58. {   
  59.    LONG   loaddelay = 1, fromparm = 1, priority = 0, oldpriority,
  60.           priorityset = FALSE, SetTaskPri();
  61.    UBYTE  commandstring[255];
  62.  
  63.    struct FileHandle *nilfh;   /* Don't free this.  It is passed on to the task. */
  64.    struct TaskCB     *task, *FindTask();
  65.  
  66. #ifdef AZTEC_C
  67.     int hasSpace = 0;      /* If a string have a space in it, re-quote it. */
  68. #endif
  69.     
  70.     if (argc < 2  ||  argv[1][0] == '?') Usage();
  71.  
  72.     /********* *  Check for Delay and Priority parameters.  * *********/
  73.  
  74.     if (IsInteger (argv[1])) {               /* Load Delay. */
  75.  
  76.        fromparm  = 2;
  77.        loaddelay = 50 * atol (argv[1]);
  78.        if (loaddelay < 0) loaddelay = -loaddelay;
  79.        if (argc < 3) Usage();           /* Only a delay, and no filename! */
  80.  
  81.     if (IsInteger (argv[2])) {        /* Task priority.  */
  82.  
  83.        fromparm    = 3;
  84.        priority    = atol (argv[2]);
  85.        priorityset = TRUE;
  86.        if (argc < 4) Usage();    /* Still no filename! */
  87.     }}
  88.  
  89.     nilfh = Open ("NIL:" ,MODE_NEWFILE); /* will always succeed */
  90.  
  91.    /********* *  Rebuild parameter string  * *********/
  92.  
  93.     strcpy (commandstring, "RUN ");
  94.  
  95.     while (fromparm < argc) {
  96.        strcat (commandstring, " "); /* add a blank */
  97.  
  98. #ifdef AZTEC_C
  99.        hasSpace = HasASpace(argv[fromparm]);   /* Quoted argument?     */
  100.        if (hasSpace)            /* Then quote it again! */
  101.           strcat (commandstring, "\"");
  102. #endif
  103.  
  104.        strcat (commandstring, argv[fromparm++]);
  105.  
  106. #ifdef AZTEC_C
  107.        if (hasSpace)
  108.           strcat (commandstring, "\"");
  109. #endif
  110.  
  111.     } /* while */
  112.  
  113.    /********* *  Run the program * *********/
  114.  
  115.    /* Priority is set by temporarily setting the priority
  116.     * of the current CLI.  The new task takes on this
  117.     * priority.
  118.     */
  119.  
  120. #ifdef DEBUG
  121.     printf("%s\n", commandstring);
  122.     printf("Delay %ld Priority %ld\n", loaddelay, priority);
  123. #else
  124.     task = FindTask (NULL);
  125.     if (priorityset) oldpriority = SetTaskPri (task, priority);
  126.     Execute (commandstring, nilfh, nilfh);
  127.     if (priorityset) SetTaskPri (task, oldpriority);
  128. #endif
  129.  
  130.    /* The full command passed to Execute now looks like this:
  131.     *
  132.     *   "RUN FILENAME PARAMETER(s)"
  133.     *
  134.     * There is no need to put re-direction in here, as it defaults to the
  135.     * file handle passed on to the Execute() call.  Redirection only
  136.     * confuses things, and was one of the reason for past crashes.
  137.     */
  138.  
  139.    /* RunBack won't check if the file exists, and won't say anything to the
  140.     * user about its actions.
  141.     */
  142.  
  143.    /* Execute, in this case, returns IMMEDIATELY.  The process
  144.     * that is loading the code that is to be run as a background
  145.     * process is working to get everything in and started.  
  146.     */
  147.  
  148.     /* Now, to minimize thrashing between tasks, lets put this task to 
  149.      * sleep so that the each task actually gets a chance to load.
  150.      */
  151.  
  152.     Delay (loaddelay);
  153.  
  154. }  /* main() */
  155.    
  156. IsInteger (s)  /* Return TRUE if string is an integer. */
  157.   char *s;
  158. {
  159.    if (*s == '-') ++s;
  160.    while (*s)
  161.       if (*s < '0'  ||  *(s++) > '9') return (FALSE);
  162.    return (TRUE);
  163. }
  164.  
  165. HasASpace (s)  /* Return TRUE if string has a space. */
  166.   char *s;
  167. {
  168.    while (*s)
  169.       if (*(s++) == ' ') return (TRUE);
  170.    return (FALSE);
  171.  
  172. }  /* HasASpace() */
  173.  
  174. Usage()  /* Called from many places. */
  175. {
  176.     printf("Usage: RUNBACKGROUND [[-]delay [priority]] command [arguments...]\n\n");
  177.     exit(0);
  178. }
  179.  
  180.