home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / cmdline / asmfix4 / e.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-12-11  |  4.2 KB  |  178 lines

  1. /*
  2.     Execute DOS .EXE file with standard handles redirected.
  3.     Accept redirection arguments of the form:
  4.  
  5.     e -r file command  : stdout and stderr to file. ( space opt. )
  6.     e -r #hnd command  : stdout and stderr to handle hnd. ( space req.)
  7.     e -rhnd file command : handle hnd to file.
  8.     e -rhnd1 #hnd2 command : handle hnd1 to handle hnd2. 
  9.  
  10.     Same as above with -a instead of -r will append.
  11.  
  12.     Standard handles are:
  13.  
  14.             0 - standard input ( not useful here )
  15.             1 - standard output.
  16.             2 - standard error.
  17.             3 - standard auxillary ( usually serial port ).
  18.             4 - standard printer ( usually parallel port ).
  19.  
  20.     Examples:
  21.  
  22.         e -r out masm test,,test;
  23.             -- send all standard output and standard error output
  24.                to the file 'out'.  Note that due to the
  25.                difference in the way C stream level stdout and
  26.                stderr are handled, 'out' may contain output in
  27.                a different order from that seen on the screen.
  28.  
  29.         e -r2 errors masm test;
  30.             -- send all standard error output to the file
  31.                errors.  Standard out still goes to the
  32.                console. 
  33.  
  34.         e -r1 out -r2 errs masm test;
  35.             -- send standard output to file 'out'.
  36.             -- send standard error to file 'errs'.
  37.  
  38.         e -r4 #1 masm test;
  39.             -- send printer output to console.
  40.  
  41. */
  42.  
  43. #include <process.h>
  44. #include <io.h>
  45. #include <stdlib.h>
  46. #include <fcntl.h>
  47. #include <sys\types.h>
  48. #include <sys\stat.h>
  49. #include <ctype.h>
  50. #include <string.h>
  51.  
  52. /*  Error return codes */
  53. #define NO_ARGS    1
  54. #define DUP    2
  55. #define EXEC    3
  56. #define OPEN    4
  57. #define BAD_SWITCH    5
  58.  
  59. /*  Known handles */
  60. #define STDOUT    1
  61. #define STDERR    2
  62.  
  63. #define SWITCHARS    "-/"
  64. #define DIGITS        "0123456789"
  65. #define FALSE    0
  66. #define TRUE    -1
  67.  
  68. int Redirect();
  69.  
  70. /*****            ************            ***********************/
  71. main( argc, argv )
  72. int argc;
  73. char *argv[];
  74. {
  75. int argcount = 1;
  76.  
  77.     if ( argc < 2 )
  78.     {
  79.         puts( "Usage is: e [<-r|-a[hnd] filename|hnd>] <.EXE file> <arglist>" );
  80.         exit( NO_ARGS );
  81.     }
  82.  
  83.     while( strcspn( argv[argcount], SWITCHARS ) == 0 )
  84.  
  85.         argcount += Redirect( argv + argcount );
  86.  
  87.     if ( execvp( argv[argcount], argv + argcount ) == -1 )
  88.     {
  89.         printf( argv[argcount] );
  90.         perror( ": Exec Error" );
  91.         exit( EXEC );
  92.     }
  93. }
  94.  
  95.  
  96. /*********    *********    **********    *************/
  97. int Redirect( argv )
  98. char *argv[];
  99. {
  100. char * cur = argv[0] + 1; /* Start looking at second char */
  101. int  handle1 = -1;    /* Handle directed from */
  102. int  handle2 = -1;    /* Handle directed to */
  103. int  append = FALSE;    /* Append to output file? */
  104. int  numargs = 2;    /* Number of command line arguments used */
  105. int  index;        /* Used for distinguishing cases */
  106.  
  107.  
  108.     switch ( *cur )
  109.     {
  110.         case 'a' : append = TRUE;
  111.         case 'r' : cur++;
  112.                break;
  113.         default  : printf( "Mal-formed switch: '%s'\n", cur - 1 );
  114.                exit( BAD_SWITCH );
  115.     }
  116.     index = strspn( cur, DIGITS ); /* Find first non-digit */
  117.     if ( cur[ index ] == '\0'  &&  index ) /* If it's end-of-string, and
  118.                           string len > 1, then we're
  119.                           looking at -r# filename */
  120.     {
  121.         if ( !sscanf( cur, "%d" , &handle1 ) )
  122.         {
  123.             printf( "Bad handle in switch expression: '%s'\n",
  124.                 cur );
  125.             exit( BAD_SWITCH );
  126.         }
  127.         cur = argv[1];
  128.     }
  129.     else { /* If first char is nul, we're looking at -r filename */
  130.         if ( *cur == '\0' ) cur = argv[1];
  131.         else    numargs = 1; /* Else we're looking at -rfilename */
  132.         }
  133.  
  134.  
  135.     if ( *cur == '#' )  /* handle2 is in command string */
  136.     {
  137.         if ( !sscanf( cur + 1, "%d", &handle2 ) )
  138.         {
  139.             printf( "Bad handle in switch expression: '%s'", 
  140.                 cur + 1 );
  141.             exit( BAD_SWITCH );
  142.         }
  143.     }
  144.     else    /* handle2 must be generated with open() */
  145.         if ( (handle2 = open( cur , 
  146.               O_WRONLY | (append ? O_APPEND : O_TRUNC ) | O_CREAT, 
  147.               S_IWRITE )) == -1 )
  148.         {
  149.             printf( cur );
  150.             perror( ": Open Error" );
  151.             exit( OPEN );
  152.         }
  153.  
  154.     if ( !(handle1 == -1) ) /* Handle to redirect is specified */
  155.     {
  156.         if ( dup2( handle2, handle1 ) == -1 )
  157.         {
  158.             perror( "Dup2 Error" );
  159.             exit( DUP );
  160.         }
  161.     }
  162.     else
  163.     {
  164.         if ( dup2( handle2, STDOUT ) == -1 )
  165.         {
  166.             perror( "Dup2 Error on Standard Out" );
  167.             exit( DUP );
  168.         }
  169.         if ( dup2( handle2, STDERR ) == -1 )
  170.         {
  171.             perror( "Dup2 Error on Standard Error" );
  172.             exit( DUP );
  173.         }
  174.     }
  175.  
  176.     return numargs;
  177. }
  178.