home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / library / asm_kit / tee.asm < prev    next >
Assembly Source File  |  1985-09-10  |  3KB  |  132 lines

  1.     name    tee
  2.     page    52,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. ;
  8. ; TEE reads from the standard input (redirectable) and
  9. ; outputs to both the standard output (redirectable)
  10. ; and a file, e.g. : DIR | TEE C:\MYDIR\FILE.EXT | SORT
  11. ;
  12. ; The file can be CON, LPT1, etc.  If it is CON, then
  13. ; keyboard Control-S pauses the display as usual.
  14.  
  15. command equ    80h    ;buffer for command tail
  16. buflen    equ    16384    ;buffer length, alter to taste
  17.  
  18. cr    equ    0dh    ;ASCII Carriage Return
  19. lf    equ    0ah    ;ASCII Line Feed
  20. ff    equ    0ch    ;ASCII Form Feed
  21. eof    equ    01ah    ;End-Of-File marker
  22. tab    equ    09h    ;ASCII Tab
  23. blank    equ    20h    ;ASCII blank
  24.  
  25.             ;DOS pre-defined handles
  26. stdin    equ    0000    ;standard input file
  27. stdout    equ    0001    ;standard output file
  28. stderr    equ    0002    ;standard error file
  29. stdaux    equ    0003    ;standard auxilliary file (i.e. COMM1)
  30. stdprn    equ    0004    ;standard printer file
  31.  
  32. cseg    segment para public 'CODE'
  33.  
  34.     assume    cs:cseg, ds:cseg
  35.  
  36.     org    100h    ;this is going to be a .COM
  37.  
  38. tee    proc    far
  39.  
  40.     mov    cl,ds:command    ;get length of command tail
  41.     xor    ch,ch
  42.                 ;address of command string
  43.     mov    si,offset command + 1
  44.     mov    di,offset command + 1
  45. tee1:                ;squeeze out leading spaces
  46.     mov    al,byte ptr[si]
  47.     cmp    al,blank
  48.     jbe    tee2
  49.     mov    byte ptr[di],al
  50.     inc    di
  51.  
  52. tee2:    inc    si        ;look through command tail
  53.     loop    tee1        ;until it's exhausted
  54.     mov    byte ptr[di],0    ;make ASCIIZ string
  55.     mov    ah,3ch        ;create output file
  56.     mov    cx,0        ;attribute=0
  57.     mov    dx,offset command+1
  58.     int    21h
  59.     jc    tee6        ;can't create file
  60.     mov    handle,ax    ;save token for file
  61.  
  62. tee3:    mov    ah,3fh        ;read standard input
  63.     mov    bx,stdin
  64.     mov    cx,buflen
  65.     lea    dx,buffer
  66.     int    21h
  67.     jc    tee4        ;jump, error
  68.     mov    nchar,ax    ;save length of read
  69.     cmp    ax,0        ;nothing read in?
  70.     je    tee4        ;end of file, exit
  71.     mov    ah,40h        ;write to file...
  72.     mov    bx,handle
  73.     mov    cx,nchar
  74.     int    21h
  75.     jc    tee7        ;jump, write failed
  76. ;    cmp    ax,nchar
  77. ;    jne    tee7        ;jump, write failed
  78.     mov    ah,40h        ;now write to standard output...
  79.     mov    bx,stdout
  80.     int    21h
  81.     jc    tee7        ;jump, write failed
  82.     cmp    ax,nchar
  83.     jne    tee7        ;jump, write failed
  84.     jmp    tee3        ;read again until end of file
  85.  
  86. tee4:    mov    ah,3eh        ;close output file
  87.     mov    bx,handle
  88.     int    21h
  89.  
  90. tee5:    mov    ax,4c00h    ;exit with retcode 0
  91.     int    21h
  92.  
  93. tee6:    mov    dx,offset err1    ;print "Can't create file"
  94.     mov    cx,err1len
  95.     mov    ah,40h        ;display error message and exit
  96.     mov    bx,stderr
  97.     int    21h
  98.     mov    ax,4c01h    ;exit with retcode 1
  99.     int    21h
  100.  
  101. tee7:    mov    dx,offset err2    ;print "Disk is full"
  102.     mov    cx,err2len
  103.     mov    ah,40h        ;display error message and exit
  104.     mov    bx,stderr
  105.     int    21h
  106.     mov    ah,3eh        ;close output file
  107.     mov    bx,handle
  108.     int    21h
  109.     mov    ax,4c02h    ;exit with retcode 2
  110.     int    21h
  111.  
  112. tee    endp
  113.  
  114. nchar    dw    0        ;number of chars actually input
  115. handle    dw    0        ;token for output file
  116.  
  117. err1    db    cr,lf
  118.     db    'tee: Cannot create file'
  119.     db    cr,lf
  120. err1len equ    (this byte)-(offset err1)
  121.  
  122. err2    db    cr,lf
  123.     db    'tee: Disk is full.'
  124.     db    cr,lf
  125. err2len equ    (this byte)-(offset err2)
  126.  
  127. buffer    equ    this byte    ;data is read here from standard input
  128.  
  129. cseg    ends
  130.  
  131.     end    tee
  132.