home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************/
- /* SPAWN_8.C - Call sub programs using the ERRORLEVEL exit code */
- /*******************************************************************/
- /* Author: Patrick Cohan PMC Systems Software (c)1991 */
- /*******************************************************************/
- /* 134-17th Ave. N.E. Calgary, Alberta, Canada T2E-1L6 */
- /*******************************************************************/
- /* (403)230-2700 Voice (403)230-8669 BBS FidoNet 1:134/38 */
- /*******************************************************************/
-
- /* This program will call a child process control program. The exit
- ERRORLEVEL of the control program will then, using a case level,
- execute another child process. The case level can be a number from
- 1 to 255 or 255 to 1. Since Dos and other applications tend to use
- from 1 to 40, it would be better to use from 255 on down. The design
- of this code however uses 1 to 8, so modify if it is necessary. All
- the spawned child programs should exit with a TRUE value, otherwise,
- the control loop is exited and drops to the dos level. By adding
- another loop, this can be further controlled if you run applications
- that you, as the programmer can not control. */
-
- /* The main routine allows command line parameters to be passed to it
- should there be a need. It will then pass these on with an address
- pointer design, adding any defaults if necessary. */
-
- /* The spawnvpe call code is only one of the many spawn routines. This
- vpe call is best, since it will look for a PATHname if required and
- it will allow the passing of the dos level environment variables or
- any static environment variables that you may set up below. */
-
- /* This C program is a parent process, which calls the oldest child or
- the control program. The oldest then calls other children. When the
- child is done, it can then reload the oldest child, by using a TRUE
- errorlevel exit. By adding a spawn to a child process, then they can
- call grand children. Cold winter nights over a keyboard can create
- many children. Grand children eat up alot of system memory, so do
- practice birth control. */
-
- /* Compile with MS C 5.1 or higher, in small or medium model */
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <process.h>
- #include <string.h>
- /*****************/
- char sp_prg_0 [64] = "StartPrg.EXE"; /* Control or Menu Program */
- char sp_prg_1 [64] = "1stSpawn.EXE"; /* All of these are known as */
- char sp_prg_2 [64] = "2ndSpawn.EXE"; /* child processes Each called */
- char sp_prg_3 [64] = "3rdSpawn.EXE"; /* with a set of parameters */
- char sp_prg_4 [64] = "4thSpawn.EXE"; /* There can be in theory */
- char sp_prg_5 [64] = "5thSpawn.EXE"; /* 255 of them */
- char sp_prg_6 [64] = "6thSpawn.EXE";
- char sp_prg_7 [64] = "7thSpawn.EXE";
- char sp_prg_8 [64] = "8thSpawn.EXE";
- /*****************/
- /* Static Passed Parameters */
- /*****************/
- char sp_val_0 [12] = "Called.Prg"; /* Always is the program name */
- char sp_val_1 [10] = "1st Param"; /* 1st parameter to program */
- char sp_val_2 [10] = "2nd Param"; /* etc...*/
- char sp_val_3 [10] = "3rd Param"; /* all these goto the command line */
- char sp_val_4 [10] = "4th Param"; /* the [10] should be set to the max */
- char sp_val_5 [10] = "5th Param"; /* size per variable but no more than */
- char sp_val_6 [10] = "6th Param"; /* 126 total including spaces between */
- /*****************/
- /* this variable is a pointer to the above values, it can be
- called a pointer array to array pointers */
- char * sp_val_ptr [7] = { sp_val_0,
- sp_val_1,
- sp_val_2,
- sp_val_3,
- sp_val_4,
- sp_val_5,
- sp_val_6 };
- /*****************/
- /* these are enviroment variables but you don't have to use the
- SET VALUE=PARAM just include it in a string, add or subtract
- whatever number you need and the [10] should be set for the
- width of each string. When a spawned program is called it passes
- the environment values. On return the created table disappears
- and these values again are passed to the next process. You could
- create a different set per child program process if desired */
-
- char sp_env_0 [10] = "VALUE0=";
- char sp_env_1 [10] = "VALUE1=";
- char sp_env_2 [10] = "VALUE2=";
- char sp_env_3 [10] = "VALUE3=";
- char sp_env_4 [10] = "VALUE4=";
- char sp_env_5 [10] = "VALUE5=";
- /*****************/
- /* this is a pointer array to the above arrays. Arrays in C
- are natural address pointers when used without braces */
- char * sp_env_ptr [7] = { sp_env_0,
- sp_env_1,
- sp_env_2,
- sp_env_3,
- sp_env_4,
- sp_env_5,
- NULL }; /* this last one must always be NULL */
- /*****************/
- int FALSE = 0; /* some control variables */
- int TRUE = 1;
- int err_level = 1;
- int count = 0;
- /*****************/
- main(argc, argv, env_ptr)
- /*****************/
- int argc; /* standard C argument count, always atleast 1 */
- char * argv[]; /* pointer to the passed parameters, always */
- /* atleast one, the current program filename */
- char * env_ptr[]; /* the regular dos enviroment, to pass this to
- the programs use NULL rather than sp_env_ptr */
- /*****************/
- {
- if (argc > TRUE)
- /* check for passed parameters if so get address */
- {
- for(count = FALSE; count < argc; count++) sp_val_ptr[count] = argv[count];
- }
- /* if not then use the defaults from sp_val_0 etc.
- when there are not enough parameters the defaults
- are passed, to include the true total */
- /**************/
- while (err_level > FALSE) /* set the loop */
- { /* call the control program */
- /************/
- sp_val_ptr[0] = sp_prg_0; /* give pointer to the filename */
- /************/
- err_level = spawnvpe(P_WAIT, sp_prg_0, sp_val_ptr, sp_env_ptr);
- /************/
- switch(err_level) /* test program exit code */
- {
- case 1: /* if 1 then spawn this one */
- /******/
- sp_val_ptr[0] = sp_prg_1; /* give pointer to the filename */
- /******/
- err_level = spawnvpe(P_WAIT, sp_prg_1, sp_val_ptr, sp_env_ptr);
- /******/
- break;
- /******/
- case 2: /* if 2 run this child */
- /******/
- sp_val_ptr[0] = sp_prg_2; /* give pointer to the filename */
- /******/
- err_level = spawnvpe(P_WAIT, sp_prg_2, sp_val_ptr, sp_env_ptr);
- /******/
- break;
- /******/
- case 3: /* run child 3 */
- /******/
- sp_val_ptr[0] = sp_prg_3; /* give pointer to the filename */
- /******/
- err_level = spawnvpe(P_WAIT, sp_prg_3, sp_val_ptr, sp_env_ptr);
- /******/
- break;
- /******/
- case 4: /* run child 4 */
- /******/
- sp_val_ptr[0] = sp_prg_4; /* give pointer to the filename */
- /******/
- err_level = spawnvpe(P_WAIT, sp_prg_4, sp_val_ptr, sp_env_ptr);
- /******/
- break;
- /******/
- case 5: /* run child 5 etc. */
- /******/
- sp_val_ptr[0] = sp_prg_5; /* give pointer to the filename */
- /******/
- err_level = spawnvpe(P_WAIT, sp_prg_5, sp_val_ptr, sp_env_ptr);
- /******/
- break;
- /******/
- case 6: /* All these should exit with a TRUE value */
- /******/
- sp_val_ptr[0] = sp_prg_6; /* give pointer to the filename */
- /******/
- err_level = spawnvpe(P_WAIT, sp_prg_6, sp_val_ptr, sp_env_ptr);
- /******/
- break;
- /******/
- case 7: /* or you pop out of the while loop if zero */
- /******/
- sp_val_ptr[0] = sp_prg_7; /* give pointer to the filename */
- /******/
- err_level = spawnvpe(P_WAIT, sp_prg_7, sp_val_ptr, sp_env_ptr);
- /******/
- break;
- /******/
- case 8: /* and now what about real error codes eh ! */
- /******/
- sp_val_ptr[0] = sp_prg_8; /* give pointer to the filename */
- /******/
- err_level = spawnvpe(P_WAIT, sp_prg_8, sp_val_ptr, sp_env_ptr);
- /******/
- break;
- /******/
- } /* end of switch/case */
- /*************/
- } /* end of while loop */
- _exit(err_level); /* drops to dos with ERRORLEVEL result */
- /**************/
- } /* end of main routine */
- /* End of File */
-