	title	Special ASCII to real ASCII converter
	name	('ASCII')
	maclib	base80

; Program converts a text file created by LocoScript (ASCII
; conversion mode) or MS-DOS with IBM character set.

; Copyright (c)	Werner Cirsovius
;		Hohe Weide 44
;		D-20253 Hamburg
;		Federal Republic of Germany
;		Tel.: +49 040/4223247
;		Version 2.2, September 1993

; To get program running type:

;		ASCII {-opt} filename.ext

; Options my be:

; -L	Convert LocoScript character set to ASCII - Default
; -I	Convert IBM character set to ASCII
; -Q	Simple exchange invalid characters to question marks ?

; Filename.ext may hold wildcards.

; A new file will not be created but the old one will be
; updated.

	entry	$memry

	ext	open,close,filsiz,rndred,rndwrt,string,getver
	ext	@parcn,@parst,wcard,parse,jmptab,curdrv
	ext	dirmax,files,prfn,crlf,combrk

ErrEx:
	ld	c,.string
	call	BDOS		; Tell error
	jp	OS
ASCII:
	sub	a		; Test right machine
	ld	de,$ILL.CPU
	jp	pe,ErrEx	; .. nope
	call	getver		; Get OS version
	ld	de,$ILL.CPM
	jr	c,ErrEx		; .. invalid
	ld	sp,LocStk	; Get local stack
	call	@parcn		; Fetch command parameters
	jp	z,Help		; .. none
	call	GetParm		; Sample parameters
	ld	de,FCB
	call	wcard		; Test wild card
	jr	nz,OneFile
	ld	a,TRUE
	ld	(Wild),a	; Set flag
OneFile:
	ld	a,(FCB)		; Get drive
	dec	a
	call	curdrv		; .. map to real
	call	dirmax		; Fetch max files on drive
	ld	c,l
	ld	b,h
	ld	hl,($memry)	; Fetch heap
	ld	(Heap),hl
	ld	de,FCB
	call	files		; Get files
	jr	c,..NoFile	; .. none found
	ld	(FilCnt),bc	; .. save count
MainLoop:
	call	ProcFile	; Process file
	ld	hl,(FilCnt)	; Count down
	dec	hl
	ld	(FilCnt),hl
	ld	a,l
	or	h
	jr	nz,MainLoop
	jp	OS
..NoFile:
	call	NoFile		; Tell nothing found
	jp	OS
;
; Process one file
;
ProcFile:
	ld	hl,(Heap)	; Get pointer
	inc	hl		; .. skip user
	ld	de,FCB+.fdrv
	ld	bc,.fname+.fext
	ldir			; Unpack name
	ld	(Heap),hl
	ld	a,(Wild)	; Test wild card
	or	a
	call	nz,TellFile	; .. tell action
	ld	de,FCB
	call	open		; Open file
	jp	c,NoFile	; .. not found
	call	filsiz		; Get size of file
	jp	c,NoSize	; .. error
	ld	b,h		; .. copy length
	ld	c,l
	ld	a,l
	or	h
	jp	z,NoSize	; .. empty file
	ld	hl,0		; Init read position
FileLoop:
	ld	(FCB+.frrn),hl	; Set position
	ld	de,FCB
	call	rndred		; Read record
	jp	c,ReadErr	; .. should not be end
	push	hl
	push	bc
	ld	b,RecLng	; Init length
	ld	c,FALSE		; .. flag
	ld	hl,DMA		; .. buffer
BuffLoop:
	ld	a,(hl)		; Get character
	cp	eof		; Test end of file
	jr	z,..EOF
	or	a		; Test special
	jp	p,NoSpecial
	ld	ix,NoSpecial
	push	ix
	push	hl
	push	bc
	ld	ix,(CnvExe)	; Get routine
	jp	(ix)		; .. go
NoSpecial:
	inc	hl
	djnz	BuffLoop	; Loop thru buffer
DoWr?:
	ld	a,c
	cp	TRUE		; Test any change
	call	z,WrBuff
	pop	bc
	pop	hl
	inc	hl		; Next record number
	dec	bc
	ld	a,b		; Test end of file
	or	c
	jp	nz,FileLoop
	call	close		; Close file
	ret	nc
	jp	CloseErr	; .. error
..EOF:
	pop	hl
	ld	hl,1
	push	hl		; Force end
	jr	DoWr?
;
; The converter loops
;
LOCO:
	ld	hl,LOCOtab+LOCOlen-1
	ld	bc,LOCOlen
	cpdr			; Test character in table
	jr	nz,SpecRet	; .. not in table
XchgChr:
	ld	hl,ASCIItab	; Point to table
	add	hl,bc
	ld	a,(hl)		; Get character
StChr:
	pop	bc
	pop	hl
	ld	(hl),a		; .. change character
	ld	c,TRUE		; .. set flag
	ret
SpecRet:
	pop	bc
	pop	hl
IBM:
	ld	hl,IBMtab+IBMlen-1
	ld	bc,IBMlen
	cpdr			; Test character in table
	jr	z,XchgChr
	jr	nz,SpecRet	; .. not in table
Quote:
	ld	a,'?'
	jr	StChr		; .. store
;
; Tell action on current file
;
TellFile:
	ld	de,$WORK
	call	string
	ld	de,FCB
	call	prfn		; .. tell file
	call	crlf
	call	combrk		; Test break
	ret	nc		; .. nope
	cp	CtrlC		; Test real break
	ret	nz		; .. nope
	ld	de,$ABORT
	call	string
	jp	OS		; .. that's it
;
; Sample parameters
; ENTRY	Reg B holds parameter count
;
GetParm:
	push	bc
	ld	de,($memry)	; Get dynamic base
	call	@parst		; .. fetch parameter
	call	GetFile		; Fetch file
	jr	opt?
..opt:
	push	bc
	ld	de,($memry)	; Get dynamic base
	call	@parst		; .. fetch parameter
	call	GetOpt		; .. get option
opt?:
	pop	bc
	djnz	..opt
	ret
;
; Get file from command argument
;
GetFile:
	ld	hl,($memry)
	ld	(PB),hl		; Set parse address
	ld	de,PB
	call	parse		; Parse file
	jr	c,IllParse	; ..error
	ld	a,l
	or	h
	ret	z
IllParse:
	ld	de,$ILL.PARSE
	call	string
	jp	OS
;
; Option execution
;
L.exe:
	ld	hl,LOCO
	jr	..exe
I.exe:
	ld	hl,IBM
	jr	..exe
Q.exe:
	ld	hl,Quote
..exe:
	ld	(CnvExe),hl
	ret
;
; Get valid option
;
GetOpt:
	ld	ix,($memry)	; Get pointer
	ld	a,(ix+0)	; Check valid prefix
	cp	'-'
	jr	nz,IllOpt
	ld	a,(ix+1)	; Get real key
	call	jmptab
	dw	@jmp
;
	dseg
@jmp:
	db	3
	db	'QIL'
	dw	L.exe,I.exe,Q.exe
	cseg
;
IllOpt:
	ld	de,$ILL.OPT
	call	string
	jp	OS
;
; Write modified buffer to file
;
WrBuff:
	ld	de,FCB
	call	rndwrt		; .. write
	ret	nc		; .. ok
;
	ld	hl,$WR.ERR
	jr	ErrStr
NoFile:
	ld	hl,$OPN.ERR
	jr	ErrStr
NoSize:
	ld	hl,$SIZ.ERR
	jr	ErrStr
ReadErr:
	ld	hl,$RD.ERR
	jr	ErrStr
CloseErr:
	ld	hl,$CLS.ERR
ErrStr:
	ld	de,$CANNOT
	call	string		; Give prefix
	ex	de,hl
	call	string		; .. message
	ld	de,$FILE
	call	string
	ld	de,FCB
	call	prfn		; Tell file
	call	crlf
	ret
Help:
	ld	de,$HELP
	call	string
	ret
;
	dseg
;
LOCOtab:
	db	0f0h,0d0h,0f3h,0d3h,0f4h,0d4h,0bah,0a6h
LOCOlen	equ	$-LOCOtab
;
IBMtab:
	db	084h,08eh,094h,099h,081h,09ah,0e1h
IBMlen	equ	$-IBMtab
;
ASCIItab:
	db	'{[|\}]~@'
;
CnvExe:
	dw	LOCO
$memry:
	dw	0
Wild:
	db	FALSE
PB:
	dw	0,FCB
FilCnt:
	dw	1
Heap:
	ds	2
$ILL.OPT:
	db	'Invalid option selected',eot
$ILL.PARSE:
	db	'Invalid file name',eot
$WORK:
	db	'Current file ->> ',eot
$ABORT:
	db	'<< Aborted >>',eot
$HELP:
	db	'Program converts a text file created by '
	db	'LocoScript (ASCII',cr,lf
	db	'conversion mode) or MS-DOS with IBM character'
	db	' set.',cr,lf,lf
	db	'To get program running type:',cr,lf,lf
	db	tab,'ASCII {-opt} filename.ext',cr,lf,lf
	db	'Options my be:',cr,lf,lf
	db	'-L',tab,'Convert LocoScript character set to '
	db	'ASCII - Default',cr,lf
	db	'-I',tab,'Convert IBM character set to ASCII'
	db	cr,lf
	db	'-Q',tab,'Simple exchange invalid characters '
	db	'to question marks ?',cr,lf,lf
	db	'Filename.ext may hold wildcards.',cr,lf,lf
	db	'A new file will not be created but the old '
	db	'one will be updated.',eot
$ILL.CPU:
	db	'Requires Z80 CPU',eot
$ILL.CPM:
	db	'Requires CP/M 3.1 or later',eot
$CANNOT:
	db	'Cannot ',eot
$FILE:
	db	' file: ',eot
$OPN.ERR:
	db	'open',eot
$SIZ.ERR:
	db	'compute size of',eot
$RD.ERR:
	db	'read record of',eot
$CLS.ERR:
	db	'close',eot
$WR.ERR:
	db	'write record to',eot
;
	ds	2*48
LocStk:

	end	ASCII

