home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / mslang / as / cleanf.asm < prev    next >
Assembly Source File  |  1985-08-28  |  4KB  |  147 lines

  1.     name    cleanf
  2.     page    55,132
  3.     title    'CLEANF - Filter text file'
  4. ;
  5. ; CLEANF - a DOS 2.0 filter for word processing document files.
  6. ; CLEAN.ASM Originally written by Ray Duncan
  7. ; Converted to DOS 2.0 filter CLEANF.ASM by Bob Taylor.
  8. ;
  9. ; This program reads text from the standard input device and writes
  10. ; filtered and transformed text to the standard output device.
  11. ;    1.  High bit of all characters is stripped off.
  12. ;     2.  Tabs are expanded.
  13. ;       3.  Removes all control codes except for line
  14. ;           feeds, carriage returns, and form feeds.
  15. ;       4.  Appends an end-of-file mark to the text, if
  16. ;           none was present in the input stream.
  17. ;
  18. ; Can be used to make a WordStar file acceptable for 
  19. ; other screen or line editors, and vice versa.
  20. ;
  21. ;
  22. cr    equ    0dh        ; ASCII carriage return
  23. lf    equ    0ah        ; ASCII line feed
  24. ff    equ    0ch        ; ASCII form feed
  25. eof    equ    01ah        ; End-of-file marker
  26. tab    equ    09h        ; ASCII tab code
  27.  
  28. command    equ    80h        ; buffer for command tail
  29.  
  30. ; DOS 2.0 Pre-Defined Handles
  31.  
  32. stdin    equ    0000        ; standard input file
  33. stdout    equ    0001        ; standard output file
  34. stderr    equ    0002        ; standard error file
  35. stdaux    equ    0003        ; standard auxilliary file
  36. stdprn    equ    0004        ; standard printer file
  37.  
  38. cseg    segment para public 'CODE'
  39.  
  40.     assume    cs:cseg,ds:cseg
  41.  
  42.     org    100H        ; start .COM at 100H
  43.  
  44. clean    proc    far        ; entry point from PC-DOS.
  45.     push    ds        ; push a long return back
  46.     xor    ax,ax        ; to DOS onto the stack.
  47.     push    ax
  48.  
  49. clean3:    call    get_char    ; get a character from input.
  50.     and    al,7fh        ; turn off the high bit.
  51.     cmp    al,20h        ; is it a control char?
  52.     jae    clean4        ; no.  write it to output.
  53.     cmp    al,eof        ; is it end of file?
  54.     je    clean6        ; yes, go write EOF mark and exit.
  55.     cmp    al,tab        ; is it a tab?
  56.     je    clean5        ; yes, go expand it to spaces.
  57.     cmp    al,cr        ; is it a carriage return?
  58.     je    clean35        ; yes, go process it.
  59.     cmp    al,lf        ; is it a line feed?
  60.     je    clean35        ; yes, go process it.
  61.     cmp    al,ff        ; is it a form feed?
  62.     jne    clean3        ; no. discard it.  
  63. clean35:
  64.     mov    column,0    ; if it's a legit ctrl char,
  65.     jmp    clean45        ;  we should be back at column 0.
  66.  
  67. clean4:    inc    column        ; if it's a non-ctrl char, 
  68. clean45:            ;  col = col + 1.
  69.     call    put_char    ; write the char to output.
  70.     jnc    clean3        ; if OK, go back for another char.
  71.  
  72.     mov    bx,stderr    ; not OK.  Set up to show error.
  73.     mov    dx,offset err_msg
  74.     mov    cx,err_msg_len    ; error = Disk full.
  75.     mov    ah,40h        ; write the error message
  76.     int    21h        ; to the standard error device. (CON:)
  77.     ret            ; back to DOS.
  78.  
  79. clean5:    mov    ax,column    ; tab code detected, must expand 
  80.     cwd            ; expand tabs to spaces.
  81.     mov    cx,8        ; divide the current column counter
  82.     idiv    cx        ; by eight...
  83.     sub    cx,dx        ; eight minus the remainder is the 
  84.     add    column,cx    ; number of spaces to send out to
  85. clean55:            ; move to the next tab position.
  86.     push    cx
  87.     mov    al,20h
  88.     call    put_char    ; send an ASCII blank
  89.     pop    cx
  90.     loop    clean55
  91.     jmp    clean3
  92.  
  93. clean6:    call    put_char    ; write out the EOF mark,
  94.     ret            ; and return to DOS.
  95.  
  96. clean    endp
  97.  
  98.  
  99. get_char proc near
  100.     mov    bx,stdin        ; get chars from std. input
  101.     mov    cx,1            ; # of chars to get = 1
  102.     mov    dx,offset input_buffer    ; location = input_buffer
  103.     mov    ah,3fh
  104.     int    21h            ; do the function call
  105.     or    ax,ax            ; test # of chars returned
  106.     jz    get_char1        ; if none, return EOF
  107.     mov    al,input_buffer        ; else, return the char in AL
  108.     ret
  109. get_char1:
  110.     mov    al,eof            ; no chars read, return 
  111.     ret                ; an End-of-File (EOF) mark.
  112. get_char endp
  113.  
  114. put_char proc near
  115.     mov    output_buffer,al    ; put char to write in buffer.
  116.     mov    bx,stdout        ; write to std. output
  117.     mov    cx,1            ; # of chars = 1
  118.     mov    dx,offset output_buffer    ; location = output_buffer
  119.     mov    ah,40h
  120.     int    21h            ; do the function call
  121.     cmp    ax,1            ; check to see it was really done.
  122.     jne    put_char1
  123.     clc                ; really done. return carry = 0
  124.     ret                ; as success signal.
  125. put_char1:
  126.     stc                ; not really done. return carry = 1
  127.     ret                ; as error signal (device is full).
  128. put_char endp
  129.  
  130. input_buffer    db    0        
  131. output_buffer    db    0
  132.  
  133. column        dw    0
  134.  
  135. err_msg        db    cr,lf
  136.         db    'clean: Disk is full.'
  137.         db    cr,lf
  138. err_msg_len    equ    (this byte)-(offset err_msg)
  139.  
  140. cseg    ends
  141.  
  142.     end    clean
  143.  
  144.  
  145.