home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progc / spawn_c.arj / SPAWN_C.C
Encoding:
C/C++ Source or Header  |  1991-08-28  |  8.6 KB  |  205 lines

  1. /*******************************************************************/
  2. /* SPAWN_8.C - Call sub programs using the ERRORLEVEL exit code    */
  3. /*******************************************************************/
  4. /* Author: Patrick Cohan   PMC Systems Software   (c)1991          */
  5. /*******************************************************************/
  6. /* 134-17th Ave. N.E.    Calgary, Alberta, Canada     T2E-1L6      */
  7. /*******************************************************************/
  8. /* (403)230-2700  Voice   (403)230-8669 BBS       FidoNet 1:134/38 */
  9. /*******************************************************************/
  10.  
  11. /* This program will call a child process control program. The exit
  12.    ERRORLEVEL of the control program will then, using a case level,
  13.    execute another child process. The case level can be a number from
  14.    1 to 255 or 255 to 1. Since Dos and other applications tend to use
  15.    from 1 to 40, it would be better to use from 255 on down. The design
  16.    of this code however uses 1 to 8, so modify if it is necessary. All
  17.    the spawned child programs should exit with a TRUE value, otherwise,
  18.    the control loop is exited and drops to the dos level. By adding
  19.    another loop, this can be further controlled if you run applications
  20.    that you, as the programmer can not control. */
  21.  
  22. /* The main routine allows command line parameters to be passed to it
  23.    should there be a need. It will then pass these on with an address
  24.    pointer design, adding any defaults if necessary. */
  25.  
  26. /* The spawnvpe call code is only one of the many spawn routines. This
  27.    vpe call is best, since it will look for a PATHname if required and
  28.    it will allow the passing of the dos level environment variables or
  29.    any static environment variables that you may set up below. */
  30.  
  31. /* This C program is a parent process, which calls the oldest child or
  32.    the control program. The oldest then calls other children. When the
  33.    child is done, it can then reload the oldest child, by using a TRUE
  34.    errorlevel exit. By adding a spawn to a child process, then they can
  35.    call grand children. Cold winter nights over a keyboard can create
  36.    many children. Grand children eat up alot of system memory, so do
  37.    practice birth control. */
  38.  
  39. /* Compile with MS C 5.1 or higher, in small or medium model */
  40.  
  41. #include <stdlib.h>
  42. #include <stdio.h>
  43. #include <process.h>
  44. #include <string.h>
  45. /*****************/
  46. char sp_prg_0 [64] = "StartPrg.EXE"; /* Control or Menu Program */
  47. char sp_prg_1 [64] = "1stSpawn.EXE"; /* All of these are known as */
  48. char sp_prg_2 [64] = "2ndSpawn.EXE"; /* child processes Each called */
  49. char sp_prg_3 [64] = "3rdSpawn.EXE"; /* with a set of parameters */
  50. char sp_prg_4 [64] = "4thSpawn.EXE"; /* There can be in theory */
  51. char sp_prg_5 [64] = "5thSpawn.EXE"; /* 255 of them */
  52. char sp_prg_6 [64] = "6thSpawn.EXE";
  53. char sp_prg_7 [64] = "7thSpawn.EXE";
  54. char sp_prg_8 [64] = "8thSpawn.EXE";
  55. /*****************/
  56. /* Static Passed Parameters */
  57. /*****************/
  58. char sp_val_0 [12] = "Called.Prg"; /* Always is the program name */
  59. char sp_val_1 [10] = "1st Param"; /* 1st parameter to program */
  60. char sp_val_2 [10] = "2nd Param"; /* etc...*/
  61. char sp_val_3 [10] = "3rd Param"; /* all these goto the command line */
  62. char sp_val_4 [10] = "4th Param"; /* the [10] should be set to the max */
  63. char sp_val_5 [10] = "5th Param"; /* size per variable but no more than */
  64. char sp_val_6 [10] = "6th Param"; /* 126 total including spaces between */
  65. /*****************/
  66. /* this variable is a pointer to the above values, it can be
  67.    called a pointer array to array pointers */
  68. char * sp_val_ptr [7] = { sp_val_0,
  69.               sp_val_1,
  70.               sp_val_2,
  71.               sp_val_3,
  72.               sp_val_4,
  73.               sp_val_5,
  74.               sp_val_6 };
  75. /*****************/
  76. /* these are enviroment variables but you don't have to use the
  77.    SET VALUE=PARAM just include it in a string, add or subtract
  78.    whatever number you need and the [10] should be set for the
  79.    width of each string. When a spawned program is called it passes
  80.    the environment values. On return the created table disappears
  81.    and these values again are passed to the next process. You could
  82.    create a different set per child program process if desired */
  83.  
  84. char sp_env_0 [10] = "VALUE0=";
  85. char sp_env_1 [10] = "VALUE1=";
  86. char sp_env_2 [10] = "VALUE2=";
  87. char sp_env_3 [10] = "VALUE3=";
  88. char sp_env_4 [10] = "VALUE4=";
  89. char sp_env_5 [10] = "VALUE5=";
  90. /*****************/
  91. /* this is a pointer array to the above arrays. Arrays in C
  92.    are natural address pointers when used without braces */
  93. char * sp_env_ptr [7] = { sp_env_0,
  94.               sp_env_1,
  95.               sp_env_2,
  96.               sp_env_3,
  97.               sp_env_4,
  98.               sp_env_5,
  99.                           NULL }; /* this last one must always be NULL */
  100. /*****************/
  101. int FALSE = 0;  /* some control variables */
  102. int TRUE = 1;
  103. int err_level = 1;
  104. int count = 0;
  105. /*****************/
  106. main(argc, argv, env_ptr)
  107. /*****************/
  108. int argc;         /* standard C argument count, always atleast 1 */
  109. char * argv[];    /* pointer to the passed parameters, always */
  110.           /* atleast one, the current program filename */
  111. char * env_ptr[]; /* the regular dos enviroment, to pass this to
  112.              the programs use NULL rather than sp_env_ptr */
  113. /*****************/
  114. {
  115.  if (argc > TRUE)
  116.            /* check for passed parameters if so get address */
  117.   {
  118.   for(count = FALSE; count < argc; count++) sp_val_ptr[count] = argv[count];
  119.   }
  120.                /* if not then use the defaults from sp_val_0 etc.
  121.                   when there are not enough parameters the defaults
  122.                   are passed, to include the true total */
  123. /**************/
  124.  while (err_level > FALSE) /* set the loop */
  125.    {                 /* call the control program */
  126.      /************/
  127.      sp_val_ptr[0] = sp_prg_0; /* give pointer to the filename */
  128.      /************/
  129.      err_level = spawnvpe(P_WAIT, sp_prg_0, sp_val_ptr, sp_env_ptr);
  130.      /************/
  131.      switch(err_level) /* test program exit code */
  132.       {
  133.         case 1:     /* if 1 then spawn this one */
  134.            /******/
  135.            sp_val_ptr[0] = sp_prg_1; /* give pointer to the filename */
  136.            /******/
  137.            err_level = spawnvpe(P_WAIT, sp_prg_1, sp_val_ptr, sp_env_ptr);
  138.            /******/
  139.            break;
  140.            /******/
  141.         case 2:     /* if 2 run this child */
  142.            /******/
  143.            sp_val_ptr[0] = sp_prg_2; /* give pointer to the filename */
  144.            /******/
  145.            err_level = spawnvpe(P_WAIT, sp_prg_2, sp_val_ptr, sp_env_ptr);
  146.            /******/
  147.            break;
  148.            /******/
  149.         case 3:     /* run child 3 */
  150.            /******/
  151.            sp_val_ptr[0] = sp_prg_3; /* give pointer to the filename */
  152.            /******/
  153.            err_level = spawnvpe(P_WAIT, sp_prg_3, sp_val_ptr, sp_env_ptr);
  154.            /******/
  155.            break;
  156.            /******/
  157.         case 4:     /* run child 4 */
  158.            /******/
  159.            sp_val_ptr[0] = sp_prg_4; /* give pointer to the filename */
  160.            /******/
  161.            err_level = spawnvpe(P_WAIT, sp_prg_4, sp_val_ptr, sp_env_ptr);
  162.            /******/
  163.            break;
  164.            /******/
  165.         case 5:     /* run child 5 etc. */
  166.            /******/
  167.            sp_val_ptr[0] = sp_prg_5; /* give pointer to the filename */
  168.            /******/
  169.            err_level = spawnvpe(P_WAIT, sp_prg_5, sp_val_ptr, sp_env_ptr);
  170.            /******/
  171.            break;
  172.            /******/
  173.         case 6:     /* All these should exit with a TRUE value */
  174.            /******/
  175.            sp_val_ptr[0] = sp_prg_6; /* give pointer to the filename */
  176.            /******/
  177.            err_level = spawnvpe(P_WAIT, sp_prg_6, sp_val_ptr, sp_env_ptr);
  178.            /******/
  179.            break;
  180.            /******/
  181.         case 7:     /* or you pop out of the while loop if zero */
  182.            /******/
  183.            sp_val_ptr[0] = sp_prg_7; /* give pointer to the filename */
  184.            /******/
  185.            err_level = spawnvpe(P_WAIT, sp_prg_7, sp_val_ptr, sp_env_ptr);
  186.            /******/
  187.            break;
  188.            /******/
  189.         case 8:     /* and now what about real error codes eh ! */
  190.            /******/
  191.            sp_val_ptr[0] = sp_prg_8; /* give pointer to the filename */
  192.            /******/
  193.            err_level = spawnvpe(P_WAIT, sp_prg_8, sp_val_ptr, sp_env_ptr);
  194.            /******/
  195.            break;
  196.            /******/
  197.       }              /* end of switch/case */
  198.     /*************/
  199.    }                 /* end of while loop */
  200.    _exit(err_level); /* drops to dos with ERRORLEVEL result */
  201.    /**************/
  202. }                    /* end of main routine */
  203. /* End of File */
  204. 
  205.