home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / t_append.zip / T_APPEND.ASM < prev    next >
Assembly Source File  |  1993-01-21  |  5KB  |  179 lines

  1.     name    tee
  2.     page    55,132
  3.     title    'TEE - tee junction for DOS pipes'
  4. ;
  5. ; TEE --- A "tee" junction for DOS 2.x pipes
  6. ;         after the fashion of the Unix function TEE  
  7. ; By A. K. Head, 6 Duffryn Place, Melbourne, Australia 3142
  8. ;    Somewhat overhauled & with error handling added by Ray Duncan
  9. ;
  10. ; 01/21/93 This is a slight modification of TEE --> T_APPEND
  11. ;            by Scott McEndree, 747 Maple Av, Newark, Ohio 43055
  12. ;
  13. ; T_APPEND reads from the standard input (redirectable) and
  14. ; outputs to both the standard output (redirectable)
  15. ; and a file, e.g.:   DIR | T_APPEND outfile.ext
  16. ;
  17. ; The output file "outfile.ext" is appended with the data.
  18. ; If it doesn't exist, it is created.
  19. ;
  20. ; This is great for creating log files where you want data to go to
  21. ; the screen and a log file.
  22. ;
  23. ; The file can be CON, LPT1, etc.  If it is CON, then
  24. ; keyboard Control-S pauses the display as usual.
  25.  
  26. command equ     80h             ; buffer for command tail
  27. buflen    equ    16384        ; buffer length, alter to taste
  28.  
  29. cr    equ    0dh        ; ASCII carriage return
  30. lf    equ    0ah        ; ASCII line feed
  31. ff    equ    0ch        ; ASCII form feed
  32. eof    equ    01ah        ; End-of-file marker
  33. tab    equ    09h        ; ASCII tab code
  34. blank   equ     20h             ; ASCII blank
  35.  
  36.                 ; DOS 2.x pre-defined handles
  37. stdin    equ    0000        ; standard input file
  38. stdout    equ    0001        ; standard output file
  39. stderr    equ    0002        ; standard error file
  40. stdaux    equ    0003        ; standard auxilliary file
  41. stdprn    equ    0004        ; standard printer file
  42.  
  43.  
  44. cseg    segment para public 'CODE'
  45.  
  46.     assume    cs:cseg,ds:cseg
  47.  
  48.     org    100H        ; start .COM at 100H
  49.  
  50. tee    proc    far
  51.  
  52.         mov     cl,ds:command   ; get length of command tail.
  53.     xor    ch,ch 
  54.                                 ; address of command string.
  55.         mov     si,offset command+1
  56.         mov     di,offset command+1
  57.  
  58. tee1:                ; squeeze out leading spaces.
  59.     mov    al,byte ptr [si]
  60.         cmp     al,blank
  61.     jbe    tee2
  62.     mov    byte ptr [di],al
  63.     inc    di
  64.  
  65. tee2:   inc     si              ; look through command tail
  66.     loop    tee1        ; until it's exhausted.
  67.     mov    byte ptr [di],0 ; make ASCIIZ string.
  68.         mov     al,02h          ; read/write access
  69.         mov     ah,3dh          ; create output file.
  70.     mov    cx,0        ; attribute=0.
  71.         mov     dx,offset command+1
  72.     int    21h
  73.         mov     handle,ax       ; save token for file.
  74.         jnc     append          ; if exist append
  75.  
  76.         mov     ah,3ch          ; create output file.
  77.     mov    cx,0        ; attribute=0.
  78.         mov     dx,offset command+1
  79.         int     21h
  80.         mov     handle,ax       ; save token for file.
  81.         jc      tee6            ; can't create file
  82.  
  83. append: mov     ah,42h
  84.         mov     al,02h          ; offset from end of file
  85.         mov     bx,handle       ; set file handle
  86.         mov     cx,00h
  87.         mov     dx,00h
  88.         int     21h             ; move file pointer
  89.  
  90.         jc      tee8            ; can't create file
  91.  
  92. tee3:    mov    ah,3fh        ; read standard input.
  93.     mov    bx,stdin
  94.     mov    cx,buflen
  95.     lea    dx,buffer
  96.     int    21h
  97.     jc    tee4        ; jump, error.
  98.     mov    nchar,ax    ; save length of read.
  99.     cmp    ax,0        ; nothing read in?
  100.     je    tee4        ; end of file, exit.
  101.     mov    ah,40h        ; write to file...
  102.     mov    bx,handle
  103.     mov    cx,nchar
  104.     int    21h
  105.     jc    tee7        ; jump, write failed.
  106.     cmp    ax,nchar
  107.     jne    tee7        ; jump, write failed.
  108.     mov    ah,40h        ; now write to standard output...
  109.     mov    bx,stdout
  110.     int    21h
  111.     jc    tee7        ; jump, write failed.
  112.     cmp    ax,nchar
  113.     jne    tee7        ; jump, write failed.
  114.     jmp     tee3        ; read again until end of file.
  115.  
  116. tee4:    mov    ah,3eh        ; close output file.
  117.     mov    bx,handle
  118.     int    21h
  119.  
  120. tee5:    mov    ax,4c00h    ; exit with return code=0.
  121.     int    21h
  122.  
  123. tee6:    mov    dx,offset err1    ; print "Can't create file".
  124.     mov    cx,err1len
  125.          mov    ah,40h        ; display error message and exit.
  126.     mov    bx,stderr
  127.     int    21h
  128.     mov    ax,4c01h    ; exit with return code=1.
  129.     int    21h
  130.  
  131. tee7:    mov    dx,offset err2    ; print "Disk is full".
  132.     mov    cx,err2len
  133.          mov    ah,40h        ; display error message and exit.
  134.     mov    bx,stderr
  135.     int    21h
  136.     mov    ah,3eh        ; close output file.
  137.     mov    bx,handle
  138.     int    21h
  139.     mov    ax,4c02h    ; exit with return code=2.
  140.     int    21h
  141.  
  142. tee8:   mov     dx,offset err3  ; print "Can't append file".
  143.         mov     cx,err3len
  144.          mov    ah,40h        ; display error message and exit.
  145.     mov    bx,stderr
  146.     int    21h
  147.     mov    ax,4c01h    ; exit with return code=1.
  148.     int    21h
  149.  
  150. tee     endp
  151.  
  152.  
  153. nchar    dw     0        ; number of chars actually input.
  154. handle    dw    0        ; token for output file.
  155.  
  156. err1       db    cr,lf
  157.     db    'tee: Cannot create file' 
  158.     db    cr,lf
  159. err1len equ    (this byte)-(offset err1) 
  160.  
  161. err2     db    cr,lf
  162.     db    'tee: Disk is full.'
  163.     db    cr,lf
  164. err2len equ    (this byte)-(offset err2) 
  165.  
  166. err3    db      cr,lf
  167.         db      'tee: Cannot append file'
  168.     db    cr,lf
  169. err3len equ     (this byte)-(offset err1)
  170.  
  171. buffer  equ     this byte       ; data is read here from
  172.                 ; standard input
  173.  
  174. cseg    ends
  175.  
  176.     end    tee
  177. 
  178.