home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / ASM / STDERRF1.ZIP / STDERRF.ASM < prev    next >
Encoding:
Assembly Source File  |  1998-10-17  |  12.0 KB  |  404 lines

  1.     page 60,132
  2.     title    STDERRF - Main Procedure for the STDERRF Application
  3.     name    STDERRF
  4.     
  5. comment ÷
  6.     STDERRF                         V1.01
  7. ----------------------------------------------------------------------------
  8. NAME
  9.     STDERRF         Main Procedure for the STDERRF Application
  10.  
  11. SYNOPSIS
  12.     STDERRF Filename Exename ["Command Line"]
  13.  
  14.     where
  15.         Filename is the file that the STDERR from Exename program
  16.             is to be redirected.
  17.         Exename is the full drive:path/filename.ext of the program
  18.             that STDERRF is to execute and redirect the the
  19.             STDERR.
  20.         Command Line is an option arguement that is the command line
  21.             for the Exename application.  If there are any embedded
  22.             blanks, the entire string must be delimited by double
  23.             quotes.
  24.  
  25. DESCRIPTION
  26.     The basic outline of STDERRF is:
  27.     1.  Accepts three command line arguments:
  28.         a.    The full path and filename of the file into which STDERR is
  29.         to be written.
  30.         b.    The full path and filename of the program to be executed.
  31.         c.    The command line for the program to be executed (should be
  32.         delimited by double quotes to allow multiple arguments).
  33.         This  argument is optional.
  34.     2.  Releases all memory above the program using Int 21 function 4ah
  35.         so that there will be room enough to load and execute the
  36.         designated program.
  37.     3.  Opens the file from step #1.a above into which STDERR is to be
  38.         written.  If file exists, ask user to continue.  If to continue,
  39.         ask user if the file is to be truncated or output appended.
  40.         Perform the requested actions.
  41.     4.  Duplicates STDERR filehandle using Int 21h function 45h.
  42.     5.  Uses Int 21h function 46h, force STDERR filehandle to have the
  43.         filehandle of the opened file from step #2.
  44.     6.  Uses Int 21h function 4b00h to load and execute the program from
  45.         step #1.a.    Use the default environment and the command line from
  46.         step #1.c above.
  47.     7.  Upon return from the function 4b00h, closes the file opened in
  48.         step #2.
  49.     8.  Restores STDERR using Int 21h function 46h to force STDERR to
  50.         point to the filehandle saved from step #3 above.
  51.  
  52. PROGRAMMING NOTES
  53.     STDERRF was assembled using MASM 6.13 with the following settings:
  54.         Preserve Case of Names in Object File
  55.         Warning Level 3
  56.         Warnings Treated As Errors
  57.         /nologo
  58.         Defines: SIMPLE - See STARTUP1.ASM for explanation
  59.              memmod=small (use small memory model)
  60.     Link Settings:
  61.         Version:  5.31.009
  62.         /batch /nonnulls
  63.  
  64.     Procedures called:
  65.         Exit            EXIT.ASM
  66.         GetValidChoice        GETVLDC.ASM
  67.     DOS Interrupts:
  68.         Int 21h 3dh - Open File with Handle
  69.         Int 21h 3eh - Close File with Handle
  70.         Int 21h 40h - Write to file or device
  71.         Int 21h 42h - Move File Pointer
  72.         Int 21h 45h - Duplicate File Handle
  73.         Int 21h 46h - Force duplication of filehandle
  74.         Int 21h 4bh - Load and Execute Program
  75.         Int 21h 5bh - Create New File
  76.  
  77. MEMORY REQUIREMENTS
  78.     STDERRF.ASM
  79.         Stack:      4 bytes
  80.         Data:    160 bytes
  81.         _BSS:      0 bytes
  82.         Const:    187 bytes
  83.         Code:    314 bytes
  84.  
  85.     STDERRF Appliation
  86.         Stack:    512 bytes
  87.         Data:    212 bytes
  88.         _BSS:     12 bytes
  89.         Const:    762 bytes
  90.         Code:    672 bytes
  91.  
  92.     STDERRF requires 2,432 bytes of memory to exectute plus the size of
  93.     the passed environment and environment's Memory Control Block which
  94.     is 16 bytes.
  95.  
  96. RETURNS
  97.     If no errors, returns the return code of the executed program to
  98.     the Start_Up1S to be STDERRF's return code.
  99.  
  100. CAUTIONS
  101.  
  102. AUTHOR
  103.     Raymond Moon - 5 Mar 95
  104.     Copyright (c) 1995, 1998, MoonWare
  105.     ALL RIGHTS RESERVED
  106.  
  107. HISTORY
  108.     Version    - Date        - Remarks
  109.     1.00    -  5 Mar 95    - Orginal
  110.     1.01    -  3 Oct 98    - Corrected error when there are not any
  111.                   command line arguments to pass.
  112.                   CommandTail was not initialized correctly.
  113.                   Move declaration to .DATA and initialized.
  114.                 - Used new startup code
  115.     
  116.     ÷ End of Comment
  117.     
  118.     include procesor.inc
  119. %    .MODEL    small,FORTRAN
  120.     assume    es:DGROUP
  121.  
  122. ;-----------------------------
  123. ;    Include files
  124.  
  125.     include stderrf.inc
  126.  
  127. ;=========================================================================
  128. ;    Equates
  129. ;=========================================================================
  130.  
  131. ;----------------------------
  132. ;    ARGV equates. Using the SIMPLE option, ARGV is argv[0] vice a pointer
  133. ;    to them.
  134.  
  135. argvStdErrFile    equ    ARGV
  136. argvExeFilename    equ    ARGV + 2
  137. argvCommandTail equ    ARGV + 4
  138.  
  139. ;=========================================================================
  140. ;    Structures
  141. ;=========================================================================
  142.  
  143. LPtr    struc                ; Long Pointer structure
  144. lpOffset    dw    ?        ; Offset
  145. lpSeg        dw    ?        ; Segment
  146. LPtr    ends
  147.  
  148. LoadExec_S    struc
  149. leEnvironment    dw    ?        ; Environmental block segment address
  150. leCommandTail    LPtr    <>        ; Address of Command Tail
  151. leFCB_1     LPtr    <>        ; address of default FCB, #1
  152. leFCB_2     LPtr    <>        ; address of second FCB, #2
  153. LoadExec_S    ends
  154.  
  155. CommandTail_S    struc
  156. ctLength    db    ?        ; Length of command tail minus end CR
  157. ctLeadingSpace    db    ?        ; Initial space to start command tail
  158. ctText        db    126 dup (?)    ; Command Tail
  159. CommandTail_S    ends
  160.  
  161. ;=========================================================================
  162. ;    DATA
  163. ;=========================================================================
  164.     .DATA
  165.  
  166. EmptyFCB    db        11 dup (20h), 5 dup (00h)
  167. ParamBlk    LoadExec_S    {\
  168.                 0,            ; Use STDERRF's environ
  169.                 {CommandTail, DGROUP},    ; LPTR to CommandTail
  170.                 {EmptyFCB, DGROUP},    ; LPTR to 1st FCB
  171.                 {EmptyFCB, DGROUP}}    ; LPTR to 2nd FCB
  172. DupStdErr    word        0
  173.  
  174. CommandTail    CommandTail_S    <0,CR>            ; V1.01
  175.  
  176.     .CONST
  177.  
  178. LOGO    db    "STDERRF - Redirect STDERR Output To A File Utility", CR, LF,
  179.         "Copyright (c) MoonWare, 1998", CR, LF,
  180.         "ALL RIGHTS RESERVED", CR, LF
  181.  
  182. REDIR    db    LF, "WARNING:  Redirection File Exists. Continue? (y/n): ",
  183.         BELL
  184. TRUN    db    CR, LF, "Overwrite or Append? (o/a): "
  185.  
  186. ;=========================================================================
  187. ;    CODE
  188. ;=========================================================================
  189. ;    Start CODE segment with the proper relationship with external
  190. ;    procedures
  191.  
  192.     .CODE
  193. extrn    Start_Up1s:near
  194. extrn    Exit:near
  195. extrn    GetValidChoice:near
  196.  
  197. Main    proc    ARGV:ptr, ARGC:word
  198. local    FileHandle:word
  199.  
  200. ;----------------------------
  201. ;    Display LOGO
  202.  
  203.     @WRITE    LOGO, STDERR
  204.  
  205. ;----------------------------
  206. ;    See if there is anything to do.  Must have 2 args min.    If not,
  207. ;    display usage
  208.  
  209.     cmp    ARGC, 2         ; Is ARGC == 2?
  210.     jae    MN1            ; It's equal or larger, continue
  211.     mov    ax, eqUSAGE        ; Indicate USAGE error
  212.     call    Exit            ; Call Exit
  213.  
  214. ;----------------------------
  215. ;    See if DOS version is 3.0 or later
  216.  
  217. MN1:    cmp    OSMAJOR, 3        ; Is DOS version 3.0 or better?
  218.     jae    MN2            ; Yes, continue
  219.     mov    ax, eqBADDOSVER        ; Indicate error type
  220.     call    Exit            ; Call Exit
  221.  
  222. ;----------------------------
  223. ;    Duplicate STDERR Filehandle
  224.  
  225. MN2:    mov    bx, STDERR        ; BX = handle to duplicate
  226.     mov    ah, 45h         ; Request Duplicate
  227.     int    21h            ; Call DOS
  228.     jnc    MN3            ; Successful
  229.     mov    bx, ax            ; Move DOS Error into BX
  230.     mov    ax, eqDUPERR        ; Error, tell user which error
  231.     call    Exit            ; Call Exit Routine
  232. MN3:    mov    DupStdErr, ax        ; Save Duplicated STDERR Handle
  233.  
  234. ;----------------------------
  235. ;    Initialize the Command Tail, if present.  ARGC = 3, if present.
  236.  
  237.     cmp    ARGC, 2         ; See if equal to 2 and skip
  238.     jbe    MN6            ; Yes, skip
  239.  
  240.     mov    CommandTail.ctLeadingSpace, ' '
  241.                     ; Initialize the blank
  242.  
  243.     lea    di, CommandTail.ctText    ; DI => Command Tail Text
  244.     mov    si, argvCommandTail    ; SI => Passed Command Line
  245.     mov    cx, 1            ; CX = counter, starts 1 for space
  246.  
  247. MN4:    lodsb                ; Get the next char
  248.     or    al, al            ; Is it a null?
  249.     jz    MN5            ; Yes, we are finished
  250.     stosb                ; No, store it
  251.     inc    cx            ; Account for char
  252.     jmp    MN4            ; Go start the process again
  253. MN5:    mov    byte ptr [di], CR    ; Terminate with CR
  254.     mov    CommandTail.ctLength, cl; Save length
  255.  
  256. ;----------------------------
  257. ;    Open the file into which the stderr will be directed.
  258. ;    Open first to see if it already exists.
  259.  
  260. MN6:    mov    dx, argvStdErrFile    ; DS:DX => Filename
  261.     mov    ax, 3d01h        ; Open for write only
  262.     int    21h            ; Call DOS
  263.     jnc    MN8            ; File exists, see what to do
  264.  
  265. ;----------------------------
  266. ;    The file could not be opened so it does not exist.  Create the
  267. ;    file.
  268.  
  269.     mov    dx, argvStdErrFile    ; DS:DX => filename
  270.     xor    cx, cx            ; CX =    Normal Attributes
  271.     mov    ah, 5bh         ; Request Create New File
  272.     int    21h            ; Call DOS
  273.     jnc    MN7            ; Successful, skip error
  274.     mov    bx, ax            ; Move error into BX
  275.     mov    ax, eqCREATEERR     ; Identify type of error
  276.     call    Exit            ; Call Exit Routine
  277.  
  278. MN7:    mov    FileHandle, ax        ; No Error, save filehandle
  279.     jmp    MN12            ; Skip the open handling code
  280.  
  281. ;----------------------------
  282. ;    Save the Filehandle.
  283.  
  284. MN8:    mov    FileHandle, ax
  285.  
  286. ;----------------------------
  287. ;    File exists, see if the user wants to continue.
  288.  
  289.     @WRITE    REDIR,STDERR        ; Display question
  290.  
  291.     mov    ax, "ny"        ; Pass valid choices
  292.     call    GetValidChoice        ; Get response
  293.     jnc    MN9            ; Yes, user wants to continue
  294.     mov    ax, eqUSERTERM        ; Indicate user terminate
  295.     call    EXIT            ; Call Exit
  296.  
  297. ;----------------------------
  298. ;    The user wants to continue, see if the file is to be appended or
  299. ;    truncated.
  300.  
  301. MN9:    @WRITE    TRUN, STDERR        ; Display question
  302.  
  303.     mov    ax, "ao"        ; Pass valid choices
  304.     call    GetValidChoice        ; Get user response
  305.     jnc    MN10            ; User wants to truncate
  306.  
  307. ;----------------------------
  308. ;    User wants to append, set up for a move file pointer to end of file.
  309.  
  310.     mov    ax, 4202h        ; Request move filepointer to end
  311.     jmp    MN11            ; Go to the rest of int code
  312.  
  313. ;----------------------------
  314. ;    User wants to truncate, set up for a move file pointer to start
  315.  
  316. MN10:    mov    ax, 4200h        ; Request move filepointer to start
  317.  
  318. ;----------------------------
  319. ;    Move file pointer to desired starting position
  320.  
  321. MN11:    mov    bx, FileHandle        ; BX = filehandle
  322.     xor    cx, cx            ; DX:CX = Offset
  323.     mov    dx, cx            ;   Offset = 0000:0000
  324.     int    21h            ; Call DOS
  325.  
  326. ;---------------------------
  327. ;    Write zero bytes to confirm position
  328.  
  329.     mov    dx, offset TRUN     ; DS:DX => buffer to print
  330.     xor    cx, cx            ; CX = zero length to write
  331.     mov    bx, FileHandle        ; BX = Filehandle
  332.     mov    ah, 40h         ; Request write file or device
  333.     int    21h            ; Call DOS
  334.  
  335. ;----------------------------
  336. ;    Force duplication of STDERR filehandle to FileHandle
  337.  
  338. MN12:    mov    bx, FileHandle        ; BX = new filehandle for STDERR
  339.     mov    cx, STDERR        ; CX = filehandle to change
  340.     mov    ah, 46h         ; Request force dup filehandle
  341.     int    21h            ; Call DOS
  342.     jnc    MN13            ; Error, go tell user
  343.     mov    bx, ax            ; Move error into BX
  344.     mov    ax, eqFORCEDUPERR    ; Indicate type of error
  345.     call    Exit            ; Call Exit
  346.  
  347. ;----------------------------
  348. ;    Load and execute program.
  349.  
  350. MN13:    mov    dx, argvExeFilename    ; DS:DX => Filename to execute
  351.     mov    bx, offset ParamBlk    ; ES:BX => Parameter block
  352.     mov    ax, 4b00h        ; Request load and execute
  353.     int    21h            ; Call DOS
  354.     jnc    MN14            ; No error, continue
  355.  
  356.     mov    bx, ax            ; Error, move error code into BX
  357.     mov    ax, eqEXECERR        ; Indicate the type of error
  358.     call    Exit            ; Call Exit
  359.  
  360. ;----------------------------
  361. ;    Restore STDERR by force duplicating STDERR handle back to saved
  362. ;    filehandle for STDERR
  363.  
  364. MN14:    mov    cx, DupStdErr        ; CX = new handle for STDERR
  365.     mov    bx, STDERR        ; BX = handle to change
  366.     mov    ah, 46h         ; Request force dup filehandle
  367.     int    21h            ; Call DOS
  368.     jnc    MN15            ; No error, continue
  369.  
  370.     mov    DupStdErr, 0        ; Indicate not to redirect STDERR
  371.                     ;   again
  372.     mov    bx, ax            ; Error, move error code into BX
  373.     mov    ax, eqRESTOREERR    ; Indicate the type of error
  374.     call    Exit            ; Call Exit
  375.  
  376. ;----------------------------
  377. ;    Zero DupStdErr so that after any future errors the Exit procedure
  378. ;    will not attempt to restore STDERR.
  379.  
  380. MN15:    mov    DupStdErr, 0        ; Indicate not to redirect STDERR
  381.                     ;   again
  382.  
  383. ;----------------------------
  384. ;    Close redirected file.
  385.  
  386.     mov    bx, FileHandle        ; BX = Filehande to close
  387.     mov    ah, 3eh         ; Request close filehandle
  388.     int    21h            ; Call DOS
  389.     jnc    MN16            ; No error, continue
  390.  
  391.     mov    bx, ax            ; Error, move error code into BX
  392.     mov    ax, eqCLOSEERR        ; Indicate the type of error
  393.     call    Exit            ; Call Exit
  394.  
  395. ;----------------------------
  396. ;    Return to startup code with the return code of the child program.
  397.  
  398. MN16:    mov    ah, 4dh         ; Request get child return code
  399.     int    21h            ; Call DOS
  400.     ret                ; Return to startup code to exit
  401.  
  402. Main    endp
  403.     end
  404.