home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d02xx / d0214.lha / RunBack / runback.c < prev   
C/C++ Source or Header  |  1989-05-30  |  7KB  |  212 lines

  1. /* runbackground.c */
  2. /* Author:  Rob Peck.  5/9/86 */
  3.  
  4. /* Modified 5/21/88 by Dan Barrett to include PATH searching; added
  5.  * the functions FullPathOf() and FindIt(), largely taken from C.
  6.  * Scheppner's "which.c" program.
  7.  *
  8.  * Also, a few "#ifdef AZTEC_C" lines had to be added.  It seems that
  9.  * Aztec C parses the command line differently from the way Lattice
  10.  * does with respect to arguments in quotes.  When I compiled this
  11.  * program with Aztec, all the quotes in quoted arguments were
  12.  * disappearing.  I re-insert them around any argument that has a
  13.  * space character (' ') in it.
  14. */
  15. /* Modified 5/12/89 by Tim Maffett to use Gunnar Nordmark's NULL:
  16.  * device.  RunBack also Mounts the NULL: device before using.
  17. */
  18.  
  19. /*#define DEBUG*/       /* Uncomment this line for debugging. */
  20.  
  21. #define EQUAL(a,b)      !strcmp(a,b)
  22.  
  23. #include "exec/types.h"
  24. #include "exec/memory.h"
  25. #include "libraries/dosextens.h"
  26.  
  27. extern struct FileHandle *Open();
  28. extern struct FileLock *Lock();
  29. extern VOID *AllocMem();
  30.  
  31. main(argc, argv)
  32. int argc;
  33. char *argv[];
  34. {
  35.     LONG success, delaywillbe;
  36.     UBYTE commandstring[255];
  37.     char *test, *filename;
  38.     LONG fromparm;
  39.     LONG *port;
  40.     struct FileInfoBlock *fib;
  41.     struct FileHandle *nilfh;   /* NOTE: will hang around until next reset */
  42.     struct FileLock *lock, *FullPathOf();
  43. #ifdef AZTEC_C
  44.     int hasSpace=0;             /* Does an string have a space in it. */
  45. #endif
  46.  
  47.     fib = NULL;                 /* No file info block so far. */
  48.     delaywillbe = 1;
  49.  
  50.     if(argc < 2 || EQUAL(argv[1],"?")) {
  51. usage:
  52.         printf("Usage: RUNBACKGROUND [ -<loaddelay>] <name> [<parm(s)>]\n");
  53.         printf("          where optional loaddelay is 0-9,\n");
  54.         printf("          specified in seconds for the CLI\n");
  55.         printf("          to sleep, waiting for task to load\n");
  56.         printf("          (minimizes inter-task disk-thrashing)\n");
  57.         if(fib)
  58.                 FreeMem(fib, sizeof(struct FileInfoBlock));
  59.         exit(0);
  60.     }
  61.  
  62.     /* See if there is a delay parameter present */
  63.  
  64.     test = argv[1];
  65.  
  66.     if(*test++ == '-') {
  67.         filename = argv[2];     /* argv[1] is delay so argv[2] is file  */
  68.         fromparm = 3;           /* Copy parms from 3 to end             */
  69.  
  70.         if(*test >= '0' && *test <= '9')
  71.             delaywillbe = 1 + (50 * (*test - '0'));
  72.  
  73.         if (argc < 3)
  74.                 goto usage; /* Only a delay, and no filename!! */
  75.  
  76.         argc--;         /* one less parm to copy */
  77.     }
  78.     else {
  79.         filename = argv[1];
  80.         fromparm = 2;           /* Copy parms from 2 to end             */
  81.     }
  82.  
  83.     /* Now see if the file exists!  If not, it can crash the background
  84.      * CLI and take the system along with it.
  85.      */
  86.  
  87.     lock = FullPathOf(filename);
  88.     if(!lock) {
  89.         printf("%ls: Command not found\n",filename);
  90.         goto usage;
  91.     }
  92.     else {
  93.         /* If file exists, it better be a file and not a directory */
  94.  
  95. /*DJB*/ /* With my (Dan's) changes, a file that is not found at all
  96.          * is falsely identified as a directory.  Irritating, but not
  97.          * fatal.
  98.          */
  99.  
  100.         /* Unfortunately, it is difficult to tell if it is an executable
  101.          * file.  If not executable, we'll still get blown out of the
  102.          * water, but that is up to the user to do it right!
  103.          */
  104.  
  105.         fib =  (struct FileInfoBlock *)
  106.                 AllocMem(sizeof(struct FileInfoBlock),MEMF_CLEAR);
  107.         if(!fib) {
  108.             UnLock(lock);
  109.             printf("Ran out of memory!\n");
  110.             exit(0);
  111.         }
  112.         else {
  113.             success = Examine(lock,fib);
  114.             if(fib->fib_DirEntryType > 0) {
  115. /*              printf("%ls is a directory, not a file!\n",filename); */
  116. /*DJB*/         printf("Cannot open %ls... maybe a directory?\n",filename);
  117.                 goto usage;
  118.             }
  119.         }
  120.         FreeMem(fib, sizeof(struct FileInfoBlock));
  121.         UnLock(lock);
  122.     }
  123.  
  124.     nilfh = Open("NIL:",MODE_NEWFILE); /* will always succeed */
  125.  
  126. /*TMM*/    /* Mount NULL: device
  127.               execute "Mount >nil: <nil: null:" command
  128.               this will ensure that null: is mounted
  129.               who cares if we try to mount it again ? (KLUDGE)
  130.            */
  131.  
  132.     success = Execute( "mount >nil: <nil: null:", nilfh, nilfh );
  133.  
  134. /*TMM*/ /* Run now redirected to >NULL: <NULL: ( instead of NIL: ) */
  135.     strcpy( &commandstring[0], "RUN >NULL: <NULL: " );
  136.     strcat( &commandstring[0], filename);
  137.  
  138.     /* REMOVE THIS NEXT LINE IF YOU WANT TO INCLUDE REDIRECTION IN
  139.      * THE COMMAND LINE FOR RUNBACKGROUND.   (The line was installed
  140.      * to assure that something like "RUNBACKGROUND date ?" would
  141.      * not crash the system.  "Date ?" is expecting to have an interactive
  142.      * CLI, and unless it is specifically told to direct its output to nil:
  143.      * it causes a crash.  If the next line is removed, and you are careful
  144.      * about putting only NON-interactive commands in the command line,
  145.      * everything should be ok.  Notice that if you do not specify a
  146.      * non-interactive file handle (named_disk_file or NIL:) for your
  147.      * program, it may still prevent the originating CLI from going away
  148.      * until your program ends.  Also note that specifying two instances
  149.      * of the same redirection (">somewhere" or "<somewhere") for a
  150.      * background task crashes.
  151.      */
  152.  
  153. /*TMM*/ /* Run now redirected to >NULL: <NULL: ( instead of NIL: ) */
  154.     strcat( &commandstring[0], " >NULL: <NULL: ");
  155.  
  156.     argc--;
  157.  
  158.     while(--argc > 0) {     /* Rebuild parameter string for passing it on */
  159.         strcat( &commandstring[0], " ");        /* add a blank */
  160.  
  161. #ifdef AZTEC_C
  162.         hasSpace = HasASpace(argv[fromparm]);   /* Quoted argument?     */
  163.         if (hasSpace)                           /* Then quote it again! */
  164.                 strcat( &commandstring[0], "\"");
  165. #endif
  166.  
  167.         strcat( &commandstring[0], argv[fromparm++]);
  168.  
  169. #ifdef AZTEC_C
  170.         if (hasSpace)
  171.                 strcat( &commandstring[0], "\"");
  172. #endif
  173.  
  174.     }
  175.  
  176. #ifdef DEBUG
  177.     printf("Execute %s\n", &commandstring[0]);
  178. #else
  179.     success = Execute( &commandstring[0] , nilfh, nilfh);
  180. #endif
  181.  
  182.         /* The full command passed to Execute now looks like this: */
  183.  
  184. /*TMM*/ /*  "RUN >NULL: <NULL: FILENAME >NULL: <NULL: PARAMETER(s)" */
  185.  
  186.  
  187.     if(success) {
  188.         printf("Started %ls as a background task\n",filename);
  189.  
  190.         /* Execute, in this case, returns IMMEDIATELY.  The process
  191.          * that is loading the code that is to be run as a background
  192.          * process is working to get everything in and started.
  193.          */
  194.     }
  195.     /* Now, to minimize thrashing between tasks, lets put this task to
  196.      * sleep so that the each task actually gets a chance to load.
  197.      */
  198.     Delay(delaywillbe);
  199. }
  200.  
  201.  
  202. struct FileLock *FullPathOf(filename)
  203. char *filename;
  204. {
  205.         struct FileLock *lock;
  206.         char realname[256], *FindIt();
  207.  
  208.         strcpy(realname, FindIt(filename));
  209.         lock = Lock(realname,ACCESS_READ);
  210.         return(lock);
  211. }
  212.