home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / cmdline / asmfix4 / b next >
Encoding:
Text File  |  1980-01-01  |  12.7 KB  |  312 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. MZ2 ─  wù/▒╓╙▓t*╞*Uï∞╕ΦdV╟F■â~}(╕PPΦpâ─╕PΦNâ─δïF■╤αFPΦcâ─F■╕ÄPï^■╤πïv 0Φ3â─ └t╓ïF■╤αFPï^■╤πïv 0Φìâ─@u$ï^■╤πïv 0Φ└â─╕æPΦ¡â─╕PΦΓâ─^ïσ]├Uï∞╕ Φ╩Vï^ï@ëF■╟F÷  ╟F⌠  ╟Fⁿ╟F·ï╪èÿ=at=rtï├HP╕₧PΦcâ─╕PΦÅâ─δ╟Fⁿ   F■╕╖P v■Φ╧â─ëF°ï╪ïv■Ç8u9 └t5ìF÷P╕┬PVΦìâ─ └u v■╕┼PΦâ─╕PΦ=â─ï^ïGëF■δï^■Ç?tφ╟F·ï^■Ç?#u*ìF⌠P╕∞Pï├@PΦ@â─ └uVïF■@P╕∩PΦ┬â─╕δ;╕ÇPâ~ⁿt╕δ╕P v■Φ@â─ëF⌠@u v■ΦÅâ─╕PΦ|â─╕PΦ▒â─â~÷ t v÷ v⌠Φtâ─@uM╕"δ7╕P v⌠Φ_â─@u╕-PΦ>â─╕PΦsâ─╕P v⌠Φ;â─@u╕HPΦ#include <stdio.h>
  179.  
  180. main()
  181. {
  182.     fprintf( stdout, "Hello, standard out\n" );
  183.     fprintf( stderr, "Hello, standard error\n" );
  184.     fprintf( stdaux, "Hello, standard aux\n" );
  185.     fprintf( stdprn, "Hello, standard prn\n" );
  186. }
  187. MZä  ├  ¢▄qe45f|╬Uï∞3└Φ9╕\P╕║PΦïâ─╕qP╕┬PΦ}â─╕êP╕╩PΦoâ─╕¥P╕╥PΦaïσ]├Yï▄+╪r
  188. ;|rïπ ßΘ"┐0ï6+≈ü■r╛·Ä╫ü─«√sΘüΣ■ 6ë&é6ë&Çï╞▒╙αH6ú~┤0═!<s*3└P║│┤    ═!╦DOS 2.0 or later required
  189. $≈ë6î├+▐≈█┤J═!6îεⁿ┐ä╣░+╧3└≤¬ΦbΦΦn
  190. Φn ╣+ß╛÷ïⁿ≤ñΦâ3φΦ≈■PΦz
  191. error 2000: Stack overflow
  192.  
  193. error 2003: Integer divide by 0
  194.  
  195. error 2002: Floating point not loaded
  196. ▒║2φ╗┤@═!╕ PΦE▒#║;δσ▒)║^δ▐Uï∞â∞WV vΦ_â─ï≡ìFP v vΦ┘â─ï° vVΦ⌡â─ï╟^_ïσ]├Uï∞Φ≡ Φn
  197. Σt
  198. Ç~u╞F■┼Γ╕%═!ïpπ╗ nïF┤L═!Uï∞Vïv @ü■║u6÷D u0èDÿï╪╤π÷çRu!╟D░èDÿï╪╤π╞çR╟DïDë╕δhü■┬tü■╥uZ÷DuTèDÿï╪╤π÷çRuEü>╛░t╟D░èDÿúäèDÿï╪╤π╞çRÇd√δ⌐╕PΦ╪ â─ëD └tèDÿúäÇLδ▄3└^ïσ]├Uï∞Vïvâ~uí╛9Du VΦ â─3└δhâ~tbü■║uèDÿPΦX â─ └t    VΦΓ
  199. â─δ0ü■┬tü■╥u8VΦ═
  200. â─áä$D÷Dt tΦA â─Çd≈δèDÿï╪╤π╞çR3└ëëD^ïσ]├Uï∞V3÷╣52Σⁿ¼2αΓ√Ç⌠U■5uê&4δ
  201. Σt║6╗╣%┤@═!2└ó5^ïσ]├Uï∞â∞WVï6·δ╕ P╕╘P 4Φ6 â─ └tâ╞â<uΣâ<t9ï<â╟ ╟F■èGÿëF·δèÿ= u2└δèï^■ F■êç N·Gâ~·u▐╟^_ïσ]├╗zü√ÇsS [CCδ≥├╗╕D═!r
  202. ÷┬ÇtÇÅ@Ky∞├┤0═!ú⌠├Äε&ï6,╕5═!ëΓîΣ╕%║á═!6ïpπ"6┼rî┌3█6 nsΘ6²6┼vî┌╗6 n├╣3█÷çt┤>═!CΓ≥├Uï∞Φ,     vΦB²ïσ]├Uï∞╕bΦí√WVïvìåó■úÆïFúÄïFúè╟á╟₧Ç<uΘIÇ<%tΘ
  203. ╟ÿ3└úûúåúÜúîúÉúêúñúª╟ö δ2Ç<-u ªδ'Ç<+u  û╟êδÇ< uâ>ûu
  204.  êδ ñFèÿPΦâ─ └u┐V╕£PΦëâ─ï≡Ç<.u ÉFV╕ÿPΦrâ─ï≡Ç<lu╟îFÇ<uΘáèÿëå₧■=Et
  205. =Gt=Xu     åâå₧■ ïå₧■-c=w>└ô. º≥ Ü╟ñ╕
  206. PΦçâ─δP╕δ≥╕δφ3└PΦáδΘ╕δ⌡ ╢₧■Φδ█ï■δAα╝σσσεεεεεεε╬εεε╪ε╕εε╙â>átí₧δ FΘª■Ç=%tGÇ=u⌡ï╟+╞PVΦâ─ï≈Θè■^_ïσ]├Uï∞╕Φ°∙WVâ~
  207. t Üâ>îtïÄïïWëF°ëV·âÄδ)â>ÜtïÄïëF°╟F·δïÄïÖëF°ëV·âÄâ>ñtïF° F·tïFδ3└úóï6Æâ>Üu*â~·}$â~
  208. u╞-FïF°ïV·≈╪â╥≈┌ëF°ëV·╟F÷δ╟F÷ï²â∩ vW v· v°Φâ─â>Ét WΦ▒â─ïÿ+╚ëN■δ╞0FïF■ N■ └≥èêâ>åt<a|Ç, FGÇ} uµâ>Üuíû êt â~÷u╕δ3└PΦUâ─^_ïσ]├Uï∞╕Φ╩°WV╟ö â~t╛íÄâÄëFⁿδ3ïÄïëFⁿâÄ └u╟Fⁿ0 vⁿΦâ─ï≡â>Ét
  209. 9ÿsï6ÿï>£+■â>ªuWΦâ─V vⁿΦmâ─â>ªtWΦ÷â─^_ïσ]├Uï∞╕Φ>°íÄëF■â>Éu╟ÿ 6å 6ÿ v 6Æ v■Φ▓â─
  210. â~gtâ~Guâ>ñuâ>ÿt
  211.  6ÆΦÄâ─â>ñtâ>ÿu
  212.  6ÆΦvâ─âÄ╟óíû êt v■ΦYâ─ └t╕δ3└PΦ$ïσ]├Uï∞3└Φƒ≈Vâ>áu8ïè Oâ|èFïèï7 ê*Σδ 6è vΦδâ─@u áδ ₧^ïσ]├Uï∞╕ΦQ≈WVâ>áuQïv ÷~Jδ3ïè Oâ|áöïèï? ê*Σδ 6è 6öΦÆâ─@u áï╞N └╞â>áuïF₧^_ïσ]├Uï∞╕ΦΦ÷WVïvï~â>áuLδ5ïè Oâ|èïèï ï┘ê*Σδ 6èèÿPΦ)â─@u áFï╟O └u─â>áuïF₧^_ïσ]├Uï∞╕
  213. Φ~÷WVï6Æ3└ëFⁿëF°ï>£VΦµâ─ëF·+°+~â>ªuÇ<-uâ>ö0u    ¼ÿPΦ¥■â─â>ö0t  ~â>ªtâ~t F°Φ^â>ót FⁿΦpâ>ªu&WΦ▒■â─â~t    â~°uΦ4â>ót    â~ⁿuΦC v·VΦ±■â─â>ªt╟ö WΦt■â─^_ïσ]├Uï∞3└Φ╜⌡â>ût╕+δ╕ PΦ■ïσ]├Uï∞3└Φ₧⌡╕0PΦ≡²â─â>óuâ>åt╕Xδ╕xPΦ╙²â─ïσ]├Uï∞╕Φj⌡WVïvÇ<*uïÄâÄï?Fδ:3 Ç<0|3Ç<9.9>Éu Ç<0u╟ö0¼ÿï╧╤ß╤ß╧╤ß╚âΘ0ï∙Ç<0|Ç<9~πï^ë?ï╞^_ïσ]├Uï∞╕Φ⌡V╛7δè8Fu╕δFÇ<uε3└^ïσ]├Å<Äε╛Ǽÿ3╥&Ç>⌠rePÄ,3└ï╚≈╤ï°≥«&8u∙â╟ï╚≈╤ï≈&èGA:αt<"t<    t< uδX╛üδ+O+■  t±ï╧B[ï├┴%■ +αïⁿ≤ñ░ ¬ï╦╛üδï╚$■+αïⁿ╕C ½≤ñï┴¬ï⌠Pï▄ï■¼
  214. └t Φ=t⌠NV ÷¼Φ8¬
  215. └t Φ)u≥╞E δ┌¬ï⌠KK;≤s¡çëD■δ≥ï▄ ╥u ë&° &<<    t< ├<"u¼
  216. └t╧<"u Ç} \uOδ¼u¬δΦ├Å>Äε3╔ï┴ïΘï∙Iï6, ÷tÄ╞≥«E«u·Eù@$■ï²╤σ┼Φ»≤ï╧ïⁿ²ï∞Ä▐3÷Iπë~EE¼¬
  217. └u·Γ≤ëNë&· &>Uï∞â∞WVïvèDÿ⌐ât÷D@t
  218. ÇL ╕  Θ╥÷Du≡ÇLÇd∩3└ëDï°ë~■÷DuèDÿï╪╤π÷çRt0ï<+|  ~W tèDÿPΦèâ─ëF■ïD@ë╟D ï\èFêδh÷DuLü■║u+èDÿPΦ∙â─ └u3 @╟D░èDÿï╪╤π╞çR╟▒δ║╕PΦ²â─ëD └tÇLδ¥ÇL┐WìFPèDÿPΦâ─ëF■9~■tΘ) èF*Σ^_ïσ]├╗Çü√ésS [CCδ≥├╗éü√äsS [CCδ≥├Φ╖≤Uï∞â∞WVïv3 VΦ|â─èD$<u<÷DuèDÿï╪╤π÷çRt'ï+DëFⁿ └~P tèDÿPΦââ─;FⁿtÇL ┐  ïDë╟Dï╟^_ïσ]├Uï∞ï^â√}â√| ÷ç@t╕δ3└]├Uï∞ï^ÇO■ïσ]├Uï∞VW╗Bâ?u)╕Φ└u3└Öδ$@$■úBúDû╟â╞╟D■■ ë6HïNî╪Ä└Φj_^ïσ]├Uï∞Wï~3└╣  ≥«ï┴@@≈╪_]├Uï∞WVïNπ.ï┘ï~ï≈3└╣  ≥«A≈┘;╦vï╦ï■ïv≤ªèD 3╔:E wtII≈╤ï┴^_]├Uï∞│Θ`Θ╦â∙εs°AÇß■ïwⁿ¡ï■¿tBH;┴sï╨≡¡¿t4┬ï≈ëD■δµï■t ∙ëL■+┴Hëδ∙■L■ï╞î┌ü·0t&îPë├&╞T=■ t%ï■≡¡¿t≥ï■H;┴s╜ï╨≡¡¿tΓ┬ï≈ëD■δµïG └tÄ╪δ&■Ttî╪=0t&ÄLï7δ╜ïw3└ΦY;╞t$@@ÿΦMt■M■Φ tûNNδÜ3└Ö├QïE■¿t+╚IAA║ &;Rv╤Ωu⌡ï┴╞r┬r≈╥#┬+╞Φ u≈╥╤Ωuσ3└Y├RQΦtWï■ï≡≥╟D■■ ëwï╓+╫JëU■XYZ├SP3╥RRP╕PΦ~â─â· Z[t ╥├Uï∞ï^Çg╧]├Uï∞â∞WV╛▓3 δèDÿ⌐ât VΦK²â─@tGâ╞96zsπï╟^_ïσ]├Uï∞ï^÷ç t╕B3╔ï╤═!sΘß÷çÇuΘüïNïV3└ⁿWVï≡ï·πe╕
  219. @≥«u2Qï╧+╩Iπ═!£≡¥s┤    δI └t/F╣║V┤@═!s┤    δ4 └t        Microsoft Macro Assembler 4.00 and Utilities
  220.                     Correction Notice
  221.  
  222.                  Utility for Redirection
  223.                     December 11, 1985
  224.  
  225.  
  226.  
  227. With pre-4.0 versions of MASM, it is possible to redirect
  228. the Assembler's output.  For example, 
  229.  
  230.        MASM example; >errors.dat
  231.  
  232. will send the Assembler's error messages to 'errors.dat',
  233. instead of to the console.  However, the above command will
  234. not redirect error messages with MASM 4.0.
  235.  
  236. MASM 4.0 sends error messages to standard error, one of the
  237. MS-DOS standard device handles.  It is not possible to
  238. redirect output from MASM 4.0 because MS-DOS does not allow
  239. redirection of standard error from the console.  This disk
  240. contains a utility which allows you to run MASM with
  241. standard handles redirected.  The files on this disk are:
  242.  
  243. WRITE.C
  244. WRITE.EXE
  245. E.C
  246. E.EXE
  247. README
  248.  
  249.  
  250. The following is a description of the files and their
  251. purpose:
  252.  
  253. WRITE.EXE
  254.  
  255. Allows you to determine where information for standard
  256. output, standard error, standard auxiliary (usually serial
  257. port), and standard printer (usually parallel port) will be
  258. directed.  The source code for the program is WRITE.C.
  259.  
  260. E.EXE
  261.  
  262. Executes a program after redirection of standard handles has
  263. taken place.  The program accepts arguments of the form:
  264.  
  265. e -r file command         stdout and stderr to file
  266. e -r #hnd command         stdout and stderr to handle hnd
  267. e -rhnd file command    handle hnd to file
  268. e -rhnd1 #hnd2 command    handle hnd1 to handle hnd2
  269.  
  270. To append in addition to redirecting, use -a instead of -r.
  271.  
  272. The standard handles are:
  273.  
  274. 0 - standard input (not useful here)
  275. 1 - standard output
  276. 2 - standard error
  277. 3 - standard auxiliary
  278. 4 - standard printer
  279.  
  280.  
  281. The following are examples of command lines:
  282.  
  283. e -r out.dat masm test,,test;
  284.  
  285. send all standard output and standard error output to the
  286. file 'out.dat'.  Note that due to the difference in the way
  287. C stream level stdout and stderr are handled, 'out.dat' may
  288. contain output in a different order from that seen on the
  289. screen.
  290.  
  291. e -r #4 masm test,,test;
  292.  
  293. send all standard output and standard error output to the
  294. standard printer
  295.  
  296. e -r1 out.dat -r2 errs.dat masm test;
  297.  
  298. -- send standard output to file 'out.dat'
  299. -- send standard error to file 'errs.dat'
  300.  
  301. e -r4 #1 masm test;
  302.  
  303. send standard printer data to standard output (console)
  304.  
  305. e -r1 nul -a2 errlog.dat -r4 out.dat masm test;
  306.  
  307. -- throw away standard output
  308. -- append standard error to errlog.dat
  309. -- send standard printer into out.dat
  310.  
  311. The source code for the program is E.C.
  312.