home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD1.img / d2xx / d240 / runback / runback.c < prev   
C/C++ Source or Header  |  1989-08-28  |  5KB  |  181 lines

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