	title	'Backspace fix program'
	name	('FIXMAIL')
	maclib	base80

; -->> File :	FIXMAIL.MAC
;		===========

; Copyright (c)	Werner Cirsovius
;		Hohe Weide 44
;		D-2000 Hamburg 20
;		Federal Republic of Germany
;		Tel.: 040/4223247

;		Version 1.0, February 1988

; Program called by :

;		FIXMAIL filename.ext

; Which fixes all backspaces, e.g. delete them and
; corresponding characters. Original file will be renamed to
; filename.BAK

; Program may be useful in MAILBOX Systems catpuring data from
; MAILBOX without backspace filter

; ===== Externals =====

; From LIB BASELIB

	extrn	string,crlf,indexa,wrbuf,wrfcb,puteof
	extrn	crebak,swpnam,filchk,delete,close,emplin,fillin

; ===== Start the code =====


	.z80
	cseg

	ld	hl,(BDOS+1)
	ld	sp,hl		; Get stack
	ld	hl,erlis1
	call	filchk		; Check file there
	jp	c,comstr	; Oops, error
	ld	hl,FCB
	ld	de,myFCB
	ld	a,(hl)
	ld	(de),a		; Set same drive
	call	crebak		; Create .BAK file
	push	af
	ld	hl,erlis2
	call	indexa
	pop	af
	jp	c,comstr	; Cannot do so
	ld	hl,myDMA
	ld	(wrbuf),hl	; Init output file
	ld	hl,myFCB
	ld	(wrfcb),hl
lopfil:
	ld	b,0
	ld	de,inbuf
	call	fillin		; Fill a line from file
	push	af		; Save result
	ld	hl,inbuf+1
	ld	de,outbuf
fixlop:
	ld	a,(hl)
	ld	(de),a
	inc	de
	inc	hl
	or	a		; Check zero, that's the end
	jp	z,fixend
	cp	bs		; Test BS
	jp	nz,fixlop
	dec	de		; Else fix it
	dec	de
	jp	fixlop
fixend:
	ld	b,0
	ld	de,outbuf
	call	emplin		; Write to file
	jp	c,wrtper	; Oooh, cannot do it
	pop	af		; Get result
	jp	nc,lopfil	; Get result
	call	puteof		; Fill last record
	jp	c,wrterr
	ld	de,myFCB
	call	close		; Close the file
	jp	c,wrterr	; Impossible
	ld	de,myFCB
	ld	hl,FCB
	call	swpnam		; Change names
	ld	de,rdy
comstr:
	call	string		; Tell any result
	call	crlf
	jp	warm
wrtper:
	pop	af		; Clean stack
wrterr:
	ld	de,myFCB
	call	delete		; Delete invalid file
	ld	de,err
	jp	comstr

	dseg

erlis1:
	dw	reqfil,ilfil
erlis2:
	dw	nofile,creerr,exist
reqfil:
	db	'File name required',eot
ilfil:
	db	'Illegal start of file name',eot
nofile:
	db	'Cannot find file',eot
creerr:
	db	'Cannot create .BAK file',eot
exist:
	db	'.BAK file already exists',eot
rdy:
	db	'Completed.',eot
err:
	db	'File write error',eot
myFCB:
	ds	FCBlen
myDMA:
	ds	reclng
inbuf:
	db	255
	ds	255
outbuf:
	ds	255


	end

