home *** CD-ROM | disk | FTP | other *** search
- /*
- Execute DOS .EXE file with standard handles redirected.
- Accept redirection arguments of the form:
-
- e -r file command : stdout and stderr to file. ( space opt. )
- e -r #hnd command : stdout and stderr to handle hnd. ( space req.)
- e -rhnd file command : handle hnd to file.
- e -rhnd1 #hnd2 command : handle hnd1 to handle hnd2.
-
- Same as above with -a instead of -r will append.
-
- Standard handles are:
-
- 0 - standard input ( not useful here )
- 1 - standard output.
- 2 - standard error.
- 3 - standard auxillary ( usually serial port ).
- 4 - standard printer ( usually parallel port ).
-
- Examples:
-
- e -r out masm test,,test;
- -- send all standard output and standard error output
- to the file 'out'. Note that due to the
- difference in the way C stream level stdout and
- stderr are handled, 'out' may contain output in
- a different order from that seen on the screen.
-
- e -r2 errors masm test;
- -- send all standard error output to the file
- errors. Standard out still goes to the
- console.
-
- e -r1 out -r2 errs masm test;
- -- send standard output to file 'out'.
- -- send standard error to file 'errs'.
-
- e -r4 #1 masm test;
- -- send printer output to console.
-
- */
-
- #include <process.h>
- #include <io.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <sys\types.h>
- #include <sys\stat.h>
- #include <ctype.h>
- #include <string.h>
-
- /* Error return codes */
- #define NO_ARGS 1
- #define DUP 2
- #define EXEC 3
- #define OPEN 4
- #define BAD_SWITCH 5
-
- /* Known handles */
- #define STDOUT 1
- #define STDERR 2
-
- #define SWITCHARS "-/"
- #define DIGITS "0123456789"
- #define FALSE 0
- #define TRUE -1
-
- int Redirect();
-
- /***** ************ ***********************/
- main( argc, argv )
- int argc;
- char *argv[];
- {
- int argcount = 1;
-
- if ( argc < 2 )
- {
- puts( "Usage is: e [<-r|-a[hnd] filename|hnd>] <.EXE file> <arglist>" );
- exit( NO_ARGS );
- }
-
- while( strcspn( argv[argcount], SWITCHARS ) == 0 )
-
- argcount += Redirect( argv + argcount );
-
- if ( execvp( argv[argcount], argv + argcount ) == -1 )
- {
- printf( argv[argcount] );
- perror( ": Exec Error" );
- exit( EXEC );
- }
- }
-
-
- /********* ********* ********** *************/
- int Redirect( argv )
- char *argv[];
- {
- char * cur = argv[0] + 1; /* Start looking at second char */
- int handle1 = -1; /* Handle directed from */
- int handle2 = -1; /* Handle directed to */
- int append = FALSE; /* Append to output file? */
- int numargs = 2; /* Number of command line arguments used */
- int index; /* Used for distinguishing cases */
-
-
- switch ( *cur )
- {
- case 'a' : append = TRUE;
- case 'r' : cur++;
- break;
- default : printf( "Mal-formed switch: '%s'\n", cur - 1 );
- exit( BAD_SWITCH );
- }
- index = strspn( cur, DIGITS ); /* Find first non-digit */
- if ( cur[ index ] == '\0' && index ) /* If it's end-of-string, and
- string len > 1, then we're
- looking at -r# filename */
- {
- if ( !sscanf( cur, "%d" , &handle1 ) )
- {
- printf( "Bad handle in switch expression: '%s'\n",
- cur );
- exit( BAD_SWITCH );
- }
- cur = argv[1];
- }
- else { /* If first char is nul, we're looking at -r filename */
- if ( *cur == '\0' ) cur = argv[1];
- else numargs = 1; /* Else we're looking at -rfilename */
- }
-
-
- if ( *cur == '#' ) /* handle2 is in command string */
- {
- if ( !sscanf( cur + 1, "%d", &handle2 ) )
- {
- printf( "Bad handle in switch expression: '%s'",
- cur + 1 );
- exit( BAD_SWITCH );
- }
- }
- else /* handle2 must be generated with open() */
- if ( (handle2 = open( cur ,
- O_WRONLY | (append ? O_APPEND : O_TRUNC ) | O_CREAT,
- S_IWRITE )) == -1 )
- {
- printf( cur );
- perror( ": Open Error" );
- exit( OPEN );
- }
-
- if ( !(handle1 == -1) ) /* Handle to redirect is specified */
- {
- if ( dup2( handle2, handle1 ) == -1 )
- {
- perror( "Dup2 Error" );
- exit( DUP );
- }
- }
- else
- {
- if ( dup2( handle2, STDOUT ) == -1 )
- {
- perror( "Dup2 Error on Standard Out" );
- exit( DUP );
- }
- if ( dup2( handle2, STDERR ) == -1 )
- {
- perror( "Dup2 Error on Standard Error" );
- exit( DUP );
- }
- }
-
- return numargs;
- }
- MZ2