home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / DOS_HELP / ADVMSDOS.ZIP / CLEAN.ASM < prev    next >
Encoding:
Assembly Source File  |  1986-06-19  |  6.1 KB  |  168 lines

  1.         name    clean
  2.         page    55,132
  3.         title   'CLEAN - Filter text file'
  4. ;
  5. ; CLEAN.ASM     Filter to turn document files into
  6. ;        normal text files.
  7. ;
  8. ; Usage is:    C>CLEAN  <infile  >outfile
  9. ;
  10. ; All text characters are passed through with high bit stripped
  11. ; off.  Form feeds, carriage returns, and line feeds are passed
  12. ; through.  Tabs are expanded to spaces.  All other control codes
  13. ; are discarded.
  14. ;
  15. ; Copyright (c) 1984, 1985 by Ray Duncan
  16. ;
  17. ; To assemble and link into an EXE file for execution:
  18. ;
  19. ;         C>MASM CLEAN;
  20. ;        C>LINK CLEAN;
  21. ;
  22. tab     equ     09h                 ; ASCII tab code
  23. lf      equ     0ah                 ; ASCII line feed
  24. ff      equ     0ch                 ; ASCII form feed
  25. cr      equ     0dh                 ; ASCII carriage return
  26. eof     equ     01ah                ; End-of-file marker
  27. blank    equ    020h            ; ASCII space code
  28.  
  29. tab_wid    equ    8            ; width of one tab stop
  30.  
  31. stdin   equ     0000                ; standard input device handle
  32. stdout  equ     0001                ; standard output device handle
  33. stderr  equ     0002                ; standard error device handle
  34.  
  35. cseg    segment para public 'CODE'
  36.  
  37.         assume  cs:cseg,ds:dseg,es:dseg,ss:sseg 
  38.  
  39. clean   proc    far                 ; entry point from MS-DOS.
  40.  
  41.     mov    ax,dseg            ; make our data segment    addressable
  42.     mov    ds,ax
  43.     mov    es,ax
  44.  
  45.         mov     ah,30h            ; check version of MS-DOS
  46.         int     21h
  47.         cmp     al,2
  48.         jae     clean2            ; proceed, DOS 2.0 or greater
  49.         mov     dx,offset msg1      ; if DOS 1.x print error message
  50.     mov    ah,9            ; we must use the old MS-DOS
  51.     int    21h            ; string output function
  52.      mov    ah,0            ; exit via function 0
  53.     int    21h
  54.  
  55. clean2: call    getchar            ; get a character from input.
  56.         and     al,07fh              ; turn off the high bit.
  57.         cmp     al,blank            ; is it a control char?
  58.         jae     clean4              ; no.  write it to output.
  59.         cmp     al,eof              ; is it end of file?
  60.         je      clean9              ; yes, go write EOF mark and exit.
  61.         cmp     al,tab              ; is it a tab?
  62.         je      clean7              ; yes, go expand it to spaces.
  63.         cmp     al,cr               ; is it a carriage return?
  64.         je      clean3                 ; yes, go process it.
  65.         cmp     al,lf               ; is it a line feed?
  66.         je      clean3                 ; yes, go process it.
  67.         cmp     al,ff               ; is it a form feed?
  68.         jne     clean2              ; no. discard it.  
  69.  
  70. clean3:    mov     column,0            ; if acceptable control character,
  71.     jmp     clean5              ; we should be back at column 0.
  72.  
  73. clean4: inc     column              ; if it's a non-ctrl char, 
  74.                                     ; then increment column counter
  75.  
  76. clean5: call    putchar            ; write the char to output.
  77.         jnc     clean2              ; if OK, go back for another char.
  78.  
  79.         mov     dx,offset msg2        ; write failed, display error
  80.         mov     cx,msg2_len        ; message and exit
  81.  
  82. clean6: mov     bx,stderr           ; issue error message to
  83.         mov     ah,40h            ; standard error device
  84.         int     21h
  85.     mov    ax,4c01h        ; then return to DOS with an 
  86.     int    21h            ; error code of 1.
  87.  
  88. clean7: mov     ax,column           ; tab code detected, must expand 
  89.         cwd                         ; expand tabs to spaces.
  90.         mov     cx,tab_wid        ; divide the current column counter
  91.         idiv    cx                  ; by the desired tab_width
  92.         sub     cx,dx               ; tab_width minus the remainder is 
  93.         add     column,cx           ; number of spaces to send out 
  94.  
  95. clean8:    push    cx            ; move to the next tab position.
  96.         mov     al,blank
  97.         call    putchar            ; send an ASCII blank
  98.         pop     cx
  99.         loop    clean8            ; loop until tab stop reached
  100.         jmp     clean2            ; go get another character
  101.  
  102. clean9: call    putchar            ; write out the EOF mark,
  103.     mov    ax,4c00h        ; and exit to DOS with
  104.     int    21h            ; return code of zero
  105.  
  106. clean   endp
  107.  
  108. getchar proc     near            ; get a character from
  109.                     ; the Standard Input device...
  110.         mov     bx,stdin                ; handle for Standard Input 
  111.         mov     cx,1                    ; length to read = 1 byte
  112.         mov     dx,offset ibuff      ; address of input buffer
  113.         mov     ah,3fh            ; function 3FH = read
  114.         int     21h                     ; transfer to DOS
  115.         or      ax,ax                   ; any characters read? 
  116.         jz      getc1                   ; if none, return EOF
  117.         mov     al,ibuff             ; else, return the char in AL
  118.         ret
  119. getc1:  mov     al,eof                  ; no chars read, return 
  120.         ret                             ; an End-of-File (EOF) mark.
  121. getchar endp
  122.  
  123. putchar proc     near            ; send a character to
  124.                     ; the Standard Output device...
  125.         mov     obuff,al        ; save char. to be written    
  126.         mov     bx,stdout               ; handle for Standard Output
  127.         mov     cx,1                    ; length to write = 1 byte
  128.         mov     dx,offset obuff     ; address of output
  129.         mov     ah,40h            ; function 40H = write
  130.         int     21h                     ; transfer to DOS
  131.         cmp     ax,1                    ; was char. really written?
  132.         jne     putc1
  133.         clc                             ; yes, return carry = 0
  134.         ret                             ; as success signal.
  135. putc1:  stc                             ; write failed, return carry = 1
  136.         ret                             ; as error signal (device is full).
  137. putchar endp
  138.  
  139. cseg     ends
  140.  
  141. dseg    segment    para public 'DATA'
  142.  
  143. ibuff    db       0                   ; temporary storage for character
  144.                     ; read from input stream
  145.  
  146. obuff    db      0            ; temporary storage for character
  147.                     ; sent to output stream
  148.  
  149. column    dw      0            ; current column counter
  150.  
  151. msg1    db      cr,lf
  152.         db      'clean: need MS-DOS version 2 or greater.'
  153.           db      cr,lf,'$'
  154.  
  155. msg2    db      cr,lf
  156.         db      'clean: disk is full.'
  157.     db      cr,lf
  158. msg2_len equ     $-msg2
  159.  
  160. dseg    ends
  161.                     ; declare stack segment
  162. sseg    segment    para stack 'STACK'
  163.  
  164.     dw    64 dup (?)
  165. sseg    ends
  166.  
  167.     end     clean
  168.