home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
assemblr
/
asm
/
wasm
/
filt.asm
< prev
next >
Wrap
Assembly Source File
|
1988-03-07
|
15KB
|
469 lines
Title 'Wolfware Sample Program', 'Text Filter'
;******************************************************************************
; Text FILTer
; Copyright (c) 1988 Eric Tauck
; All rights reserved
;
; This program performs various text filter functions. The files FILT1.ASM and
; FILE1.INC are required on the default drive/path for assembly.
;
; Usage:
;
; FILT [options] < in_file > out_file
;
; Options:
;
; x = expand tabs to spaces e = erase all tab stops
; c = compress spaces to tabs t# = set tab stop
;
; S = strip high bits m = left margin
; C = strip control chars d = delete left margin
; H = strip high (>127) bytes l# = truncate long lines
; U = all letters to upper-case r = remove trailing spaces
; L = all letters to lower-case b# = byte to end input lines
; A = capitalize words s = save carriage returns
;
; Z = write ^Z to output i# = input buffer size (20000)
; z = ignore ^Z in input o# = output buffer size (20000)
jmp start
;--- line definition
MAXLIN EQU 600 ;maximum line length
LeftMar DW 0 ;left margin
LeftDel DW 0 ;left characters to delete
Trunc DW 0 ;truncate line length (truncate if non-zero)
;--- input control block
InpBlk LABEL WORD
DW 0 ;status
DW 20000 ;buffer size
DW ?
DW ?
DW 0 ;standard input device
DW ?
;--- output control block
OutBlk LABEL WORD
DW 1 ;status
DW 20000 ;buffer size
DW ?
DW ?
DW 1 ;standard output device
DW ?
;--- special characters
TAB EQU 9 ;tab character
LF EQU 10 ;line feed
FF EQU 12 ;form feed
CR EQU 13 ;carriage return
EOF EQU 26 ;carriage return
EOL DB LF ;end of line character
;--- character options
STR_LOB EQU 0001h ;strip low (control) bytes
STR_HIB EQU 0002h ;strip high bytes
STR_BIT EQU 0004h ;strip high bits
MAK_UPR EQU 0008h ;make upper-case
MAK_LWR EQU 0010h ;make lower-case
MAK_CAP EQU 0020h ;capitalize
REP_TAB EQU 0040h ;replace tabs with spaces
REP_SPC EQU 0080h ;replace spaces with tabs
SAV_CR EQU 0100h ;save CR's
REM_SPC EQU 0200h ;delete trailing spaces
SKP_EOF EQU 0400h ;skip input EOF
SUP_EOF EQU 0800h ;suppress output EOF
Options DW 0
;--- input/output status
INP_EOL EQU 0001H ;end of line
INP_EOF EQU 0002H ;end of file
LAS_LET EQU 0004H ;last byte was letter
INP_ERR EQU 4000H ;input error
OUT_ERR EQU 8000H ;output error
InpSta DW 0
;--- help message
Help LABEL BYTE
DB 13,10
DB 'Text Filter, Version 1.00',13,10
DB 'Copyright (c) 1988 Eric Tauck. All rights reserved',13,10
DB 13,10
DB 'Usage: FILT [options] < input_file > output_file',13,10
DB 13,10
DB 'Tab Replacement: Tab Assignment:',13,10
DB ' x = expand tabs to spaces e = erase all tab stops',13,10
DB ' c = compress spaces to tabs t# = set tab stop',13,10
DB 'Character Replacement: Line Format:',13,10
DB ' S = strip high bits m = add left margin',13,10
DB ' C = strip control chars d = delete left margin',13,10
DB ' H = strip high (>127) bytes l# = truncate long lines',13,10
DB ' U = all letters to upper-case r = remove trailing spaces',13,10
DB ' L = all letters to lower-case b# = byte to end input lines',13,10
DB ' A = capitalize words s = save carriage returns',13,10
DB 'End of File: I/O Buffer:',13,10
DB ' z = ignore ^Z in input i# = input buffer size (20000)',13,10
DB ' Z = suppress ^Z in output o# = output buffer size (20000)',13,10
DB '$'
;================================================
; External source code.
include 'FILE1.INC'
include 'FILT1.ASM'
;================================================
; Main program.
;--- set up stack and reduce memory allocation
start lea bx, End ;end of program
mov sp, bx ;set stack
mov cl, 4
shr bx, cl ;adjust to paragraph
inc bx ;round up
mov ah, 4ah ;function
int 21h ;execute
;--- initialize
mov dx, MAXLIN ;max margin spaces
lea di, Margin ;margin area
call Spaces ;store spaces
call Tab_Reset ;set default tab stops
call Parse_Cmd ;parse command line
;--- input read buffer
lea bx, InpBlk ;control block
call File_Alloc ;set up buffer
jc main3
;--- output write buffer
lea bx, OutBlk ;control block
call File_Alloc ;set up buffer
jc main3
;--- process file
call Proc_Doc ;process document
test InpSta, INP_ERR
jnz main4
test InpSta, OUT_ERR
jnz main5
;--- close input buffer
lea bx, InpBlk ;control block
call File_Free ;close buffer
;--- close output buffer
lea bx, OutBlk ;control block
call File_Flush ;flush file first
jc main5
call File_Free ;close buffer
mov ax, 4c00h
int 21h
;--- not enough memory
main3 mov al, 1
jmp Error_Exit
;--- error reading input file
main4 mov al, 2
jmp Error_Exit
;--- error writing output file
main5 mov al, 3
jmp Error_Exit
;================================================
; Parse command line.
Parse_Cmd PROC NEAR
mov si, 80h ;command tail
lodsb ;get number of bytes
mov bx, si
sub ah, ah
add bx, ax ;BX points to end
parcmd1 cmp bx, si ;check if done
je parcmd5
lodsb ;get character
mov mess1+16, al ;put in message in case of error
lea di, OptTab ;start of table
parcmd2 cmp BYTE [di], 0 ;check if end of table
je parcmd6
cmp al, [di] ;check if option matches
je parcmd3
add di, 5 ;skip to next entry
jmps parcmd2
;--- found matching option
parcmd3 mov ax, [di+1] ;load command word
or ax, ax ;check if branch location
jnz parcmd4
mov ax, [di+3] ;load option flag
xor Options, ax ;flip bits
jmp parcmd1
parcmd4 call ax ;branch to special routine
jmp parcmd1
;--- finished
parcmd5 ret
;--- bad option
parcmd6 mov al, 0 ;error number
jmp Error_Exit ;branch to error routine
ENDP ;Parse_Cmd
;------------------------------------------------
; Option table.
OptTab LABEL BYTE
DB ' ', WORD 0, WORD 0 ;option delimiter
DB ',', WORD 0, WORD 0 ;option delimiter
DB '-', WORD 0, WORD 0 ;option delimiter
DB '/', WORD 0, WORD 0 ;option delimiter
DB 'x', WORD 0, WORD REP_TAB ;expand tabs
DB 'c', WORD 0, WORD REP_SPC ;compress spaces with tabs
DB 'e', OFFSET setopt4, WORD 0 ;erase all tab stops
DB 't', OFFSET setopt5, WORD 0 ;set tab stop
DB 'S', WORD 0, WORD STR_BIT ;strip high bits
DB 'C', WORD 0, WORD STR_LOB ;strip control (<32) chars
DB 'H', WORD 0, WORD STR_HIB ;strip high (>127) bytes
DB 'U', WORD 0, WORD MAK_UPR ;all letters to upper-case
DB 'L', WORD 0, WORD MAK_LWR ;all letters to lower-case
DB 'A', WORD 0, WORD MAK_CAP ;capitalize words
DB 'm', OFFSET setopt7, WORD 0 ;left margin
DB 'd', OFFSET setopt8, WORD 0 ;delete left margin
DB 'l', OFFSET setopt6, WORD 0 ;truncate lines
DB 'r', WORD 0, WORD REM_SPC ;remove trailing spaces
DB 'b', OFFSET setopt3, WORD 0 ;byte to end input lines
DB 's', WORD 0, WORD SAV_CR ;save CR's
DB 'z', WORD 0, WORD SKP_EOF ;ignore input EOF
DB 'Z', WORD 0, WORD SUP_EOF ;suppress output EOF
DB 'i', OFFSET setopt1, WORD 0 ;input buffer size (20000)
DB 'o', OFFSET setopt2, WORD 0 ;output buffer size (20000)
DB '?', OFFSET setopt9, WORD 0 ;display help
DB 'h', OFFSET setopt9, WORD 0 ;display help
DB 0
;------------------------------------------------
; Special option setting routines.
;--- set input buffer size
setopt1 PROC NEAR
call Cmd_Num ;get size
mov InpBlk+2, cx ;save
ret
ENDP
;--- set output buffer size
setopt2 PROC NEAR
call Cmd_Num ;get size
mov OutBlk+2, cx ;save
ret
ENDP
;--- byte to end lines
setopt3 PROC NEAR
call Cmd_Num ;get byte
mov EOL, cl ;save
ret
ENDP
;--- clear all tab stops
setopt4 PROC NEAR
call Tab_Clear ;clear all stops
ret
ENDP
;--- set a tab stop
setopt5 PROC NEAR
call Cmd_Num ;get location
push bx
mov bx, cx
dec bx ;start numbering at 0
call Tab_Set ;set tab stop
pop bx
ret
ENDP
;--- truncate lines
setopt6 PROC NEAR
call Cmd_Num ;get length
mov Trunc, cx ;save
ret
ENDP
;--- left margin
setopt7 PROC NEAR
call Cmd_Num ;get size
mov LeftMar, cx ;save
ret
ENDP
;--- delete left margin
setopt8 PROC NEAR
call Cmd_Num ;get bytes to delete
mov LeftDel, cx ;save
ret
ENDP
;--- display help message
setopt9 PROC NEAR
mov ah, 9 ;display function
lea dx, Help ;load help location
int 21h ;execute
mov ax, 4c03h ;exit function and error code
int 21h ;execute
ENDP
;------------------------------------------------
; Read a command line number. Number returned
; in CX.
Cmd_Num PROC NEAR
sub cx, cx
cmdnum1 cmp bx, si ;check if done
je cmdnum2
cmp BYTE [si], '0' ;check if below zero
jb cmdnum2
cmp BYTE [si], '9' ;check if above nine
ja cmdnum2
mov ax, 10 ;base ten
mul ax, cx ;multiply
jc cmdnum3 ;jump if overflow
mov cx, ax ;back into total
lodsb ;load number
sub al, '0' ;convert to binary
sub ah, ah
add cx, ax ;add to total
jmps cmdnum1
;--- finished
cmdnum2 ret
;--- overflow
cmdnum3 mov al, 0 ;error number
jmp Error_Exit ;branch to error routine
ENDP ;Cmd_Num
;================================================
; Error routine.
;
; In: AL= local error number
Error_Exit LABEL NEAR
;--- get error table entry
sub ah, ah
mov bx, ax
shl ax
add bx, ax ;error number times three
lea si, [ErrTbl + bx] ;get location of error data
lodsb ;load return code
push ax
;--- write message
lodsw ;load offset of message
mov si, ax
lodsb ;load length of message
sub ah, ah
mov cx, ax ;length in CX
mov bx, 2 ;error device
mov dx, si ;offset in DX
mov ah, 40h ;function
int 21h ;execute
;--- exit
pop ax ;restore error code
mov ah, 4ch ;exit function
int 21h ;execute
;------------------------------------------------
; Messages and return codes.
;--- messages
mess1 DB 37, 'Option error: "?", enter ? for help',13,10
mess2 DB 21, 'Insufficient memory',13,10
mess3 DB 12, 'Read error',13,10
mess4 DB 13, 'Write error',13,10
;--- error table
ErrTbl LABEL BYTE
DB 3, OFFSET mess1 ;option error (0)
DB 4, OFFSET mess2 ;insufficient memory (1)
DB 2, OFFSET mess3 ;input error (2)
DB 1, OFFSET mess4 ;output error (3)
;================================================
; Uninitialized data.
InpBuf LABEL BYTE ;input read location
ORG +1
Margin LABEL BYTE ;margin
ORG +MAXLIN
LinBuf LABEL BYTE ;line buffer
ORG +MAXLIN+2
TabTbl LABEL BYTE ;tab table
ORG +MAXLIN
TabEndx LABEL BYTE
TabEnd EQU OFFSET TabEndx ;end of tab table
SpcCnt LABEL WORD ;space count (for compressing spaces)
ORG +2
TabOff LABEL WORD ;tab offset (decrements tab column number)
ORG +2
ORG +500
End LABEL BYTE ;end of code and data, top of stack