	title	Stream editor
	name	('SSED')
	maclib	base80

; Stream editor for result of differential file compare program
; (Small/Stupid Stream EDitor)
; It bases upon the C program SSED22.C by Chuck Forsberg, 1982
;
; CP/M 3.x Z80 assembler version by Werner Cirsovius, 10'94
;
; Version 2.0
;
; Call it:
;   SSED {-V} {d{u}:}infile {<{d{u}:}cmdfile} {>{d{u}:}outfile}
;
; If unambiguous, either file may hold wildcards.
; Infile and outfile must be different or on different drives.
;
; With option:
; -V	Be verbose
;
; Utility edits a text file on the base of a command format
; generated by the -S option of the DFC utility.
; DFC generates the following formats:
;
; n1,n2d	Delete lines n1 to n2 in infile
; n1,n2cm1,m2	exChange lines n1 to n2 in infile against
;		lines m1 to m2 defined in cmdfile
; n1am1,m2	Append m1 to m2 lines from cmdfile after line
;		n1 in infile
;
; If n1=n2 or m1=m2, only one number will be generated

	entry	$memry

	extrn	getver,cmdarg,indexa,parse,getdu,wcard
	extrn	tstdig,decin,getlin,crlf
	extrn	open,delete,opnapp,setdma,creatd,close
	extrn	srcfrs,srcnxt,srfbuf
	extrn	getlnf,strngf,usrget,usrset
	extrn	strcn0,printf,prfn,prdufn,defio
	extrn	ugteof,udskpt,uclose,wrpblk,rdpblk


;@GERM	equ	TRUE		; Enable German
@GERM	equ	FALSE		; Enable English

_PRG	macro
	db	'SSED'
	endm

_VER	macro
	db	'2.1'
	endm

argmax	equ	2
argF	equ	1

MAXLINE	equ	256

	dseg
IF	@GERM
$ILL.CPU:
	db	'Ben|tigt Z80 CPU',eot
$ILL.OS:
	db	'Ben|tigt CP/M 3.x',eot
$HELP:
	db	'Differenz-Editor '
	_PRG
	db	' v'
	_VER
	db	cr,lf,lf
	db	'Aufruf: '
	_PRG
	db	' (-V) (d(u):)Eingabe (<(d(u):)'
	db	'Kommando) (>(d(u):)Ausgabe)'
	db	cr,lf,lf
	db	'Wenn eindeutig, k|nnen die Dateien '
	db	'Jokerzeichen enthalten.',cr,lf
	db	'Eingabe und Ausgabe Datei m}ssen '
	db	'verschieden sein oder',cr,lf
	db	'sich auf unterschiedlichen '
	db	'Laufwerken befinden.',cr,lf,lf
	db	'Mit der Option:',cr,lf
	db	'-V',tab,'Protokoll ausgeben',cr,lf,lf
	db	'Das Programm bearbeitet eine Text Datei '
	db	'basierend auf Kommandos,',cr,lf
	db	'die mit der Option -S des Programms '
	db	'DFC erzeugt wurden.',cr,lf
	db	'DFC erzeugt die folgenden Formate:'
	db	cr,lf,lf
	db	'n1,n2d',tab,tab,'L|schen (delete) der Zeilen'
	db	' n1 bis n2 aus Datei1',cr,lf
	db	'n1,n2cm1,m2',tab,'[ndern (Change) der Zeilen'
	db	' n1 bis n2 aus Datei1 in',cr,lf
	db	tab,tab,'Zeilen m1 bis m2 aus Dateie2',cr,lf
	db	'n1am1,m2',tab,'Anh{ngen (Append) der Zeilen'
	db	' m1 bis m2 aus Datei2',cr,lf
	db	tab,tab,'hinter Zeile n1 aus Datei1',cr,lf,lf
	db	'Wenn n1=n2 oder m1=m2 gilt, dann wird nur '
	db	'eine Zahl ausgegeben',cr,lf,null
$ILL.PARSE:
	db	'Ung}ltiger Dateiname in ',null
$INV.DU:
	db	'Ung}ltiges Laufwerk oder Nutzer ',null
$ILL.WILD:
	db	'Ung}ltiger Joker in Datei ',null
$NOT.DIFF:
	db	'Dateien m}ssen verschieden sein',null
$ILL.OPEN:
	db	'Datei kann nicht ge|ffnet werden ',null
$WRT.ERR:
	db	'Schreibfehler auf ',null
$SSED.END:
	_PRG
	db	' fertig',cr,lf,null
$ILL.LINE:
	db	'In Zeile %d: Geht nicht auf Zeile %d\n',null
LineTab:
	dw	linno,op1
$ILL.COMM:
	db	'Falsches Kommando %s\n',null
$ILL.DIFF:
	db	'Differenz-Datei verst}mmelt oder '
	db	'nicht mit DFC -S erzeugt'
	db	cr,lf,null
$LIN.NO:
	db	'Zeile %d :: ',null
NoTab:
	dw	linno
$EOF.CMD:
	db	'EOF in Kommando Datei',cr,lf,null
$EOF.IN:
	db	'EOF in Eingabe Datei',cr,lf,null
$UNEX.EOF:
	db	'Unerwartetes EOF in Kommando Datei',cr,lf,null
$CMD.LINE:
	db	'%s op1=%d op2=%d cmd=%c\n',null
$XCMD.LINE:
	db	'%s op1=%d op2=%d cmd=%c op3=%d op4=%d\n',null
ELSE
$ILL.CPU:
	db	'Requires Z80 CPU',eot
$ILL.OS:
	db	'Requires CP/M 3.x',eot
$HELP:
	db	'Stream editor '
	_PRG
	db	' v'
	_VER
	db	cr,lf,lf
	db	tab,'Usage: '
	_PRG
	db	' {-V} {d{u}:}infile {<{d{u}:}cmdfile} '
	db	'{>{d{u}:}outfile}'
	db	cr,lf,lf
	db	'If unambiguous, either file may '
	db	'hold wildcards.',cr,lf
	db	'Infile and outfile must be different or '
	db	'on different drives.',cr,lf,lf
	db	'With option:',cr,lf
	db	'-V',tab,'Be verbose',cr,lf,lf
	db	'Utility edits a text file on the base of a '
	db	'command format',cr,lf
	db	'generated by the -S option of the '
	db	'DFC utility.',cr,lf
	db	'DFC generates the following formats:',cr,lf,lf
	db	'n1,n2(D)',tab,'Delete lines n1 to n2 '
	db	'in infile',cr,lf
	db	'n1,n2(C)m1,m2',tab,'exChange lines n1 to n2 '
	db	'in infile against',cr,lf
	db	tab,tab,'lines m1 to m2 defined in cmdfile'
	db	cr,lf
	db	'n1(A)m1,m2',tab,'Append m1 to m2 lines from '
	db	'cmdfile after line',cr,lf
	db	tab,tab,'n1 in infile',cr,lf,lf
	db	'If n1=n2 or m1=m2, only one number '
	db	'will be generated',cr,lf,null
$ILL.PARSE:
	db	'Invalid parsing of ',null
$INV.DU:
	db	'Invalid drive or user ',null
$ILL.WILD:
	db	'Invalid wildcard in file ',null
$NOT.DIFF:
	db	'Files should be different',null
$ILL.OPEN:
	db	'Cannot open file ',null
$WRT.ERR:
	db	'Write error on ',null
$SSED.END:
	_PRG
	db	' finished',cr,lf,null
$ILL.LINE:
	db	'At line %d: Can''t back up to %d\n',null
LineTab:
	dw	linno,op1
$ILL.COMM:
	db	'Illegal command %s\n',null
$ILL.DIFF:
	db	'Difference file garbled or not made by DFC -S'
	db	cr,lf,null
$LIN.NO:
	db	'Line %d :: ',null
NoTab:
	dw	linno
$EOF.CMD:
	db	'EOF in cmdfile',cr,lf,null
$EOF.IN:
	db	'EOF in infile',cr,lf,null
$UNEX.EOF:
	db	'Unexpected EOF in cmdfile',cr,lf,null
$CMD.LINE:
	db	'%s op1=%d op2=%d cmd=%c\n',null
$XCMD.LINE:
	db	'%s op1=%d op2=%d cmd=%c op3=%d op4=%d\n',null
ENDIF	;@GERM
;
CmlTab:
CommTab:
cmdline:
	dw	$-$
	dw	op1,op2,cmd,op3,op4
;
CnvMode:
	db	0b
$OPT:
	db	'V'
OptLen	equ	$-$OPT
OptTab:
	dw	VERB.opt
;
; File control structures for user area setting
;
fp.FIN	equ	$		; \
DMAin:				; |
	dw	$-$		; |
	db	reclng		; |
FCBin:				; Infile
	dw	$-$		; |
	db	-1		; |
EOFin:				; |
	db	FALSE		; |
	db	0		; /
;
fp.FOUT	equ	$		; \
DMAout:				; |
	dw	$-$		; |
BPout:
	db	0		; |
FCBout:				; Outfile
	dw	$-$		; |
USRout:				; |
	db	-1		; |
EOFout:				; |
	db	FALSE		; /
;
fp.FCMD	equ	$		; \
DMAcmd:				; |
	dw	$-$		; |
	db	reclng		; |
FCBcmd:				; |
	dw	$-$		; Cmdfile
USRcmd:				; |
	db	-1		; |
EOFcmd:				; |
	db	FALSE		; |
FILE?:				; |
	db	-1		; /
;
$memry:
	dw	0
linno:
	dw	1
Heap:
	ds	2
Root:
	ds	2
insline:
	ds	2
curusr:
	ds	1
ARGV:
	ds	2*argmax
ARGC:
	ds	1
cmd:
	ds	1
op1:				; \
	ds	2		; |
op2:				; |
	ds	2		; /
op3:				; \
	ds	2		; |
op4:				; |
	ds	2		; /
$PB:
	ds	2*2
R$PB:
	ds	2*2
_APP_:
	db	' '
OFiles:
	db	FALSE

	cseg

;
; Parse options
; ENTRY	Accu holds option count
;
GetOpt:
	ld	iy,CnvMode	; Init status pointer
	ld	b,a		; Set count
	ld	c,0		; Init index
..getopt:
	ld	hl,ARGV
	ld	a,c
	call	indexa		; Fetch pointer
	push	bc
	call	parse.opt	; Parse option
	pop	bc
	inc	c
	djnz	..getopt
	ret
;
; Parse option
; ENTRY	Reg DE points to option string
;
parse.opt:
	ld	a,(de)		; Verify option prefix
	cp	'-'
	jp	nz,Help		; .. invalid
	inc	de
parse.more:
	ld	hl,$OPT+OptLen-1
	ld	bc,OptLen
	ld	a,(de)
	cpdr			; Find option key
	jp	nz,Help		; .. error
	inc	de		; Skip option
	push	de
	ld	hl,OptTab
	ld	a,c
	call	indexa		; Fetch address
	ex	de,hl
	pop	de
	ld	bc,parse.loop
	push	bc		; Set return vector
	jp	(hl)		; .. execute option
parse.loop:
	ld	a,(de)		; Test more in this item
	or	a
	jr	nz,parse.more
	ret
;
; Option -V : Be verbose
;
VERB.opt:
	set	0,(iy)
	ret
;
; Convert FCB to file name
; ENTRY	Reg DE points to FCB
;	Reg HL points to memory
;
CnvFtN:
	ld	a,(defio)	; Save I/O state
	push	af
	ld	a,1000b		; Set memory bit
	ld	(defio),a
	call	prfn		; Store into memory
	ld	(hl),null	; .. close
	pop	af
	ld	(defio),a	; Reset I/O
	ret
;
; Get unambiguous file name
; ENTRY	Reg DE points to FCB
; EXIT	Zero set if ambiguous
;
UnAmbiguous?:
	call	wcard		; Test wildcard
	ret	nz		; .. nope
	ld	hl,(Heap)
	ld	bc,reclng
	add	hl,bc
	ld	(srfbuf),hl	; Set first search buffer
	call	execDU
	dw	srcfrs		; Find file
	jr	c,Ambiguous	; .. not here
	push	hl		; Save file pointer
	ld	hl,(Heap)
	ld	(srfbuf),hl	; Set next search buffer
	call	srcnxt		; Find next file
	pop	hl
	jr	nc,Ambiguous	; .. any here
	push	de
	inc	de
	ld	bc,.fname+.fext
	ldir			; Unpack new file name
	pop	de
	push	de
	ld	hl,(Heap)
	ld	($PB),hl	; .. change string
	call	CnvFtN
	pop	de
	xor	a
	inc	a
	ret
Ambiguous:
	xor	a		; Set error
	ret
;
; Parse file - Verify valid delimiter
; ENTRY	Reg DE points to control block
; EXIT	Carry set on invalid parse
;
_parse::
	call	parse		; Parse thru system
	ret	c		; .. error
	ld	a,l
	or	h		; Test end
	ret	z		; .. got it
	ld	a,(hl)		; Maybe a blank
	cp	' '
	ret	z		; .. yeap, it's valid
	scf			; Set invalid parse
	ret
; 
; Set user on request
; ENTRY	Reg <DE>-1 holds user
;	Next word following call will be jumped to
;
execDU:
	push	hl
	push	af
	ld	hl,curusr
	dec	de
	ld	a,(de)		; Get user
	cp	(hl)		; .. test same
	ld	(hl),a
	call	nz,usrset
	inc	de		; .. fix FCB
	pop	af
	pop	hl
	ex	(sp),hl
	ld	c,(hl)		; Get execution address
	inc	hl
	ld	b,(hl)
	inc	hl
	ex	(sp),hl
	push	bc		; .. set it
	ret			; .. jump
;
; Assign input file
; ENTRY	Reg HL points to file string
;	Reg IX points to FIB
;
Fassign:
	ld	($PB),hl	; Save name for error
	call	getdu		; Fetch drive and user
	ld	de,$INV.DU
	jr	c,ErrAss	; .. error
	ld	($PB),hl	; Save new pointer
	push	bc		; Save DU
	ld	l,(ix+3)
	ld	h,(ix+3+1)
	ld	($PB+2),hl	; Save FCB
	ld	de,$PB
	call	_parse		; Parse file
	ld	de,$ILL.PARSE
	jr	c,ErrAss	; .. error
	ld	de,($PB+2)	; Get FCB
	push	de
	pop	iy
	pop	bc
	ld	(iy+0),b	; Set drive
	ld	(iy-1),c	; .. and user
	ld	(ix+5),c
	call	UnAmbiguous?	; Get unambiguous file name
	ld	de,$ILL.WILD
	ret	nz		; .. got it
ErrAss:
	call	strcn0		; Tell error
	ld	de,($PB)	; Get file name
	call	strcn0		; .. print
	jp	R.D.OS		; .. and stop
;
; Verify different input and output file
;
sameFiles?:
	ld	a,(OFiles)	; Test output file
	cp	TRUE
	ret	nz		; .. nope
	ld	de,(FCBin)
	ld	hl,(FCBout)
	dec	de		; Remember user
	dec	hl
	ld	b,1+.fdrv+.fname+.fext
..same?:
	ld	a,(de)		; Get name
	cp	(hl)		; Compare
	ret	nz		; .. ok, not same
	inc	de
	inc	hl
	djnz	..same?
not.diff:
	ld	de,$NOT.DIFF
	call	strcn0		; Tell error
	jp	R.D.OS
;
; Open source file
; ENTRY	Reg DE points to FCB
;
Freset:
	call	execDU
	dw	open		; Open file
	ret	nc		; .. got it
	push	de
	ld	de,$ILL.OPEN
F.io.err:
	call	strcn0		; Tell error
	pop	de		; Get FCB
	call	prdufn		; .. print
	jp	R.D.OS		; .. and stop
;
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; Test Verbose option selected
; EXIT	Carry set if so
;
Verbose?:
	ld	a,(CnvMode)
	rra			; Test verbose mode
	ret
;
; Read number pair from command line
; ENTRY	Reg DE points to command line
;	Reg IY points to number pair
;
num.pair:
	call	getnum		; Get 1st number
	ld	(iy+0),l	; .. save
	ld	(iy+1),h
	call	num.2		; .. preset 2nd number
	ld	a,(de)
	cp	','		; Test delimiter
	ret	nz		; .. nope
	inc	de
	call	getnum		; Get 2nd number
num.2:
	ld	(iy+2),l	; Save 2nd number
	ld	(iy+3),h
	ret
;
; Get command and parameters from cmdfile
; EXIT	Carry set on end of commands
;
getcmd:
	call	Verbose?	; Test verbose
	ld	hl,$LIN.NO
	ld	de,NoTab
	call	c,_printf	; .. print line number if so
	ld	a,(EOFcmd)	; Test end of cmdfile
	cp	TRUE
	jr	z,_eofcmd
..getcmd:
	ld	hl,(cmdline)
	ld	ix,fp.FCMD
	call	lgets		; Get line from file
	jr	c,_eofcmd	; .. end of file
	sub	2		; Test \n only
	jr	z,..getcmd	; .. yeap, skip it
	ld	de,(cmdline)	; Init base line
	ld	iy,op1
	call	num.pair	; Get number pair
	ld	a,(de)		; Get command character
	ld	(cmd),a		; .. save it
	inc	de
	ld	a,(de)
	call	tstdig		; Test digit
	ld	hl,$CMD.LINE
	jr	c,one.num	; .. nope
	ld	iy,op3
	call	num.pair	; Get 2nd number pair
	ld	hl,$XCMD.LINE
one.num:
	ld	de,CmlTab
	call	Verbose?	; Test verbose
	call	c,lprintf	; .. print line number if so
	or	a
	ret
_eofcmd:
	call	Verbose?	; Test verbose
	ld	de,$EOF.CMD
	call	c,strcn0	; .. give message
	scf			; Set end of file
	ret
;
; Get number from string
; ENTRY	Reg DE points to string
; EXIT	Reg HL holds number
;
getnum:
	ld	b,-1
	call	decin		; Get number
	ret
;
; Copy line from infile to outfile
;
copyin:
	call	chuckline	; Get line
	ret	c		; .. end of file
;
; Put line to file
;
puts:
	ld	de,(insline)	; Get line
.puts:
	ld	b,null
	call	O.LINE		; .. to file
	ret	nc		; .. ok, got it
	ld	hl,(FCBout)
	push	hl		; .. save file
	ld	de,$WRT.ERR
	jp	F.io.err	; .. give error
;
; Put line to file - less header characters
;
hputs:
	ld	hl,(insline)	; Get line
	ld	b,(hl)		; Get header
	inc	hl
	ld	c,(hl)
	inc	hl
	ex	de,hl
	ld	hl,'>'*256+' '
	or	a
	sbc	hl,bc		; Test ok
	jr	z,.puts
	jp	fubar		; .. error
;
; Copy line from cmdfile to outfile
;
insert:
	ld	hl,(insline)
	ld	ix,fp.FCMD
	call	lgets		; Get line from file
	jr	nc,hputs	; .. write line to file
	ld	de,$UNEX.EOF
proc.eof:
	ld	(ix+6),TRUE	; Set end of file
	call	strcn0		; Give message
	ret
;
; Read line from file
;
chuckline:
	ld	hl,(linno)
	inc	hl		; Bump line number
	ld	(linno),hl
	ld	hl,(insline)
	ld	ix,fp.FIN
	call	lgets		; Get line from file
	ret	nc		; .. ok
	call	Verbose?	; Test verbose
	ld	de,$EOF.IN
	call	proc.eof	; Process end of file
	scf			; .. set it
	ret
;
; Read line from cmdfile
; ENTRY	Reg HL points to line
;	Reg IX points to FIB
; EXIT	Carry set on end of file
;
lgets:
	ld	(rdpblk),ix	; Set FIB
	dec	hl
	dec	hl		; Set pointer
	ld	(hl),MAXLINE-1	; Set length
	ex	de,hl
	ld	b,null
	call	fgets		; Get line
	ret
;
; Print formatted string on console - less \n on original line
; ENTRY	Reg HL points to line to be formatted
;	Reg DE points to optional control pointer block
;
lprintf:
	ld	bc,(cmdline)	; Get line pointer
.lprintf:
	ld	a,(bc)
	or	a		; .. test end of line
	jr	z,cr.print
	cp	cr
	jr	z,cr.print
	cp	lf
	jr	z,cr.print
	inc	bc
	jr	.lprintf
cr.print:
	push	bc
	push	af
	xor	a
	ld	(bc),a
	call	_printf		; .. print
	pop	af
	pop	bc
	ld	(bc),a		; .. reset character
	ret
;
; Print formatted string on console
; ENTRY	Reg HL points to line to be formatted
;	Reg DE points to optional control pointer block
;
_printf:
	ld	bc,(Heap)	; Get dynamic pointer
	push	bc
	ld	a,255
	ld	(bc),a
	call	printf		; Format line
	pop	de
	inc	de
	call	strcn0		; .. print it
	ret
;
; The main stream loop
;
doSSED:
	call	getcmd		; Get next command
	jp	c,end.SCMD	; .. no more commands
	ld	hl,(op1)	; Get first number
	ld	de,(linno)
	or	a
	sbc	hl,de		; Test valid range
	jr	nc,_ssed1	; .. yeap
	ld	hl,$ILL.LINE
	ld	de,LineTab
	call	_printf		; .. print error line
	jr	fubar
_ssed1:
	ld	a,l
	or	h		; Test line reached
	jr	z,_ssed2	; .. yeap
	ld	a,(EOFin)	; Test end of file
	cp	TRUE		; .. test end
	jr	z,_ssed2	; .. yeap
	push	hl
	call	copyin		; .. copy file
	pop	hl
	dec	hl
	jr	_ssed1		; .. loop
_ssed2:
	ld	a,(cmd)		; Get command
	cp	'a'		; Test append
	jr	z,_ssed4	; .. yeap
	ld	de,(op1)
	ld	hl,(op2)
	inc	hl
	or	a
	sbc	hl,de		; Get length
_ssed7:
	ld	a,l		; Test done
	or	h
	jr	z,_ssed4
	ld	a,(EOFin)
	cp	TRUE		; .. test end of file
	jr	z,_ssed4
	push	hl
	call	chuckline
	pop	hl
	dec	hl
	jr	_ssed7
_ssed4:
	ld	a,(cmd)		; Get command
	cp	'c'		; Validate it
	jr	z,_ssed5
	cp	'a'
	jr	nz,_ssed6
_ssed5:
	ld	de,(op3)
	ld	hl,(op4)
	inc	hl
	or	a
	sbc	hl,de		; Get length
_ssed8:
	ld	a,l		; Test done
	or	h
	jr	z,doSSED
	ld	a,(EOFcmd)
	cp	TRUE		; .. test end of file
	jr	z,doSSED
	push	hl
	call	insert		; .. insert line
	pop	hl
	dec	hl
	jr	_ssed8
_ssed6:
	cp	'd'
	jp	z,doSSED
	ld	hl,$ILL.COMM
	ld	de,CommTab
	call	_printf		; .. print error line
fubar:
	ld	de,$ILL.DIFF
	call	strcn0		; .. print error line
	jp	R.D.OS
end.SCMD:
	ld	a,(EOFin)
	cp	TRUE		; .. test end
	jr	z,end.SSED	; .. yeap
	call	copyin		; .. copy remaining lines
	jr	end.SCMD
end.SSED:
	ld	de,$SSED.END
	call	strcn0		; Tell finished
	ld	a,(OFiles)
	cp	TRUE
	call	z,uclose	; Close output file
	ret
;
; Test >> in redirection
;
REDIR.append?:
	ld	a,(hl)		; Get previous character
	ld	(_APP_),a	; .. save
	cp	'>'		; Test append
REDIR.set:
	call	nz,inc.r	; .. fix pointer if so
	ld	(hl),null	; Clear character
	ex	af,af'
	ld	a,l
	sub	CCPBUF+1
	ld	(CCPBUF),a	; Set new length
	ex	af,af'
	call	z,inc.r		; Skip 2nd >
inc.r:
	inc	hl
	ret
;
; Parse file for redirection
; ENTRY	Reg HL points to string
;	Reg DE points to file pointer
; EXIT	Carry set on parse error
;
std.parse:
	push	bc
	ld	(R$PB),hl	; Set into block
	ld	(R$PB+2),de
	ld	de,R$PB
	call	_parse		; Parse file
	pop	bc
	ret
;
; Process redirection
;
REDIR?::
	call	stdout		; Process output
	call	stdin		; .. and input
	ret
;
; Process redirection of standard output
;
stdout::
	ld	hl,CCPBUF	; Point to input
	ld	a,(hl)		; At least we need ..
	cp	3		; .. blank.>.c
	ret	c		; Ignore empty line
	ld	c,a
	ld	b,0
	add	hl,bc
	ld	a,'>'
	cpdr			; Find redirection symbol
	ret	nz		; .. nope
	call	REDIR.append?	; Test >>
	call	getdu		; Get disk/user
	ld	de,(FCBout)
	call	nc,std.parse	; Parse file
	ret	c		; .. error
	ld	a,c
	ld	(USRout),a	; Set user
	ld	hl,(FCBout)
	dec	hl
	ld	(hl),c		; Set user
	inc	hl
	ld	(hl),b		; .. and drive
	ex	de,hl
	call	wcard		; Verify no wildcard
	ret	z
	ld	a,(_APP_)
	cp	'>'		; Test append
	jr	z,REDIR.file?
REDIR.crec:
	call	execDU
	dw	creatd		; Create file
	ret	c		; .. bad
	jr	REDIR.vec
REDIR.file?:
	call	execDU
	dw	open		; Test file on board
	jr	c,REDIR.crec	; .. nope, create it
	ld	hl,(DMAout)
	ex	de,hl
	call	setdma		; Set disk buffer
	ex	de,hl
	call	opnapp		; Open for append
	ret	c		; .. oops, something wrong
	ld	(BPout),a	; Save offset
REDIR.vec:
	ld	hl,fp.FOUT
	ld	(wrpblk),hl	; Init write block
	ld	de,O.LINE
	ld	hl,.O.LINE
	ld	bc,O.len
	ldir			; Unpack vectors
	ld	a,TRUE
	ld	(OFiles),a	; Indicate file in progress
	ret
;
; Process redirection of standard input
;
stdin::
	ld	hl,CCPBUF	; Point to input
	ld	a,(hl)		; At least we need ..
	cp	3		; .. blank.<.c
	ret	c		; Ignore empty line
	ld	c,a
	ld	b,0
	add	hl,bc
	ld	a,'<'
	cpdr			; Find redirection symbol
	ret	nz		; .. nope
	xor	a
	inc	a
	call	REDIR.set	; Fix command line
	call	getdu		; Get disk/user
	ld	de,(FCBcmd)
	call	nc,std.parse	; Parse file
	ret	c		; .. error
	ld	a,c
	ld	(USRcmd),a	; Set user
	ld	hl,(FCBcmd)
	dec	hl
	ld	(hl),c		; Set user
	inc	hl
	ld	(hl),b		; .. and drive
	ex	de,hl
	call	wcard		; Verify no wildcard
	ret	z
	call	execDU
	dw	open		; Open file
	ret	c		; .. bad
	xor	a
	ld	(FILE?),a	; Set file mode
	ret
;
; Redirection vectors
;
; ===>> STDOUT
;
O.LINE:
	jp	O.strcn0
R.OS:
	jp	OS
R.D.OS:
	jp	OS
;
.O.LINE:
	jp	strngf
	jp	O.close
	jp	O.delete
O.len	equ	$-.O.LINE
;
; Get line from file or standard input
;
fgets::
	ld	a,(ix+7)	; Test file
	or	a
	jp	z,getlnf	; .. yeap
;
; Get line from keyboard - fix for cr/lf
;
	ex	de,hl
	dec	(hl)		; .. fix max
	dec	(hl)
	dec	(hl)
	ex	de,hl
	push	bc
	push	de
	call	getlin		; Get line
	pop	hl		; Get back pointer
	pop	bc
	push	af
	inc	hl
	ld	e,(hl)		; Fetch length
	ld	d,0
	inc	(hl)		; .. fix it
	inc	(hl)
	ld	c,(hl)		; Save
	add	hl,de		; Point to end
	inc	hl
	ld	(hl),cr		; .. close line
	inc	hl
	ld	(hl),lf
	inc	hl
	ld	(hl),b		; Set delimiter
	call	crlf		; Close input
	pop	af
	ld	a,c		; Get back length
	ret
;
; Print string on terminal - clear carry
;
O.strcn0:
	call	strcn0		; .. print
	or	a		; Set write ok
	ret
;
; End of program -> Close file
;
O.close:
	ld	a,(EOFout)	; Test write in progress
	or	a
	jp	nz,OS		; .. nope
	call	uclose		; .. close file
	jp	OS
;
; End of program in case of error -> Delete file
;
O.delete:
	ld	de,(FCBout)
	call	execDU
	dw	close		; Close file
	call	delete		; .. and delete it
	jp	OS
;
;
; Initialize file buffers
;
ini$mem:
	ld	hl,($memry)	; Get start of data
;
; Step 1 : Allocate record buffers
; Each buffer requires 128 bytes
;
	ld	de,reclng
	ld	(DMAcmd),hl	; Command buffer
	add	hl,de
	ld	(DMAin),hl	; .. infile
	add	hl,de
	ld	(DMAout),hl	; .. outfile
	add	hl,de
;
; Step 2 : Allocate file control blocks
; Each FCB requires 36 bytes plus one extra byte for user
;
	ld	de,FCBlen
	inc	hl
	ld	(FCBcmd),hl	; Command FCB
	add	hl,de
	inc	hl
	ld	(FCBin),hl	; .. infile
	add	hl,de
	inc	hl
	ld	(FCBout),hl	; .. outfile
	add	hl,de
;
; Step 3 : Allocate line buffers
; Each line requres MAXLINE bytes plus two for management
;
	ld	de,MAXLINE
	inc	hl		; 'getlnf' specific
	inc	hl
	ld	(cmdline),hl
	add	hl,de
	inc	hl
	inc	hl
	ld	(insline),hl
	add	hl,de
;
; Final setting
;
	ld	(Heap),hl	; .. set start of heap
	ld	(Root),hl
	ex	de,hl
	call	setdma		; Change from CCP buffer
	ld	hl,ugteof
	ld	(getlnf-2),hl	; Init 'fgetc' vector
	ld	hl,udskpt
	ld	(strngf-2),hl	; Init 'fputc' vector
	ret
;
; -->> Simple error routine on cold start
;
Error:
	ld	c,.string
	call	BDOS		; Give message
	jp	OS		; .. abort
;
; %%%%%%%%%%%%%%%%%%
; %%% ENTER SSED %%%
; %%%%%%%%%%%%%%%%%%
;
MAIN:
	sub	a		; Verify right machine
	ld	de,$ILL.CPU
	jp	pe,Error
	call	getver		; Get OS version
	ld	de,$ILL.OS
	jp	c,Error
	ld	sp,(TPAtop)	; Get stack
	call	ini$mem		; Initialize memory buffers
	call	usrget		; Get current user
	ld	(curusr),a	; .. save
	push	af
	call	REDIR?		; Process redirection
	pop	af
	ld	(curusr),a	; Save user
	call	usrset		; .. and log again
	ld	hl,ARGV
	ld	de,CCPBUF
	ld	b,argmax
	call	cmdarg		; Get arguments
	jr	c,Help		; .. invalid
	sub	argF		; Delete file offset
	jr	c,Help		; .. invalid
	ld	(ARGC),a	; Save argument index
	call	nz,GetOpt	; .. get options
	ld	a,(ARGC)	; Get file index
	ld	hl,ARGV
	call	indexa		; Get pointer
	ex	de,hl
	ld	ix,fp.FIN
	call	Fassign		; Assign infile
	call	sameFiles?	; Verify different files
	ld	de,(FCBin)
	call	Freset		; Open infile
	call	doSSED		; Do the main task
	jp	R.OS		; .. that's all
Help:
	ld	de,$HELP
	call	strcn0		; Give help
	jp	R.D.OS

	end	MAIN

