	title	Check Control Sequences in File
	name	('FILE')
	maclib	base80

; This small program simply checks an ASCII file for control
; sequences. Simply call it:

;	FILE file

; Program reports resulting control code

; Copyright (c)	Werner Cirsovius
;		Hohe Weide 44
;		2000 Hamburg 20
;		Germany
;		Phone 49-40-4223247

;		Version 1.0, April 1993

_PRG	macro
	db	'FILE'
	endm

_VER	macro
	db	'1.0'
	endm

	entry	$memry

	ext	getver,wcard,open,PrFN,dskred,string,conout
	ext	incrrn,dout32

	dseg

$ILL.CPU:
	db	'Requires Z80 CPU',eot
$ILL.OS:
	db	'Requires CP/M 3.x',eot
$HELP:
	_PRG
	db	' '
	_VER
	db	cr,lf,lf
	db	'This small program simply checks an ASCII'
	db	' file for control',cr,lf
	db	'sequences. Simply call it:',cr,lf,lf,tab
	_PRG
	db	tab,'file',cr,lf,lf
	db	'Program reports resulting control code',eot
$ILL.WLD:
	db	' : invalid wildcard',eot
$NO.FILE:
	db	' : cannot find file',eot
$memry:
	dw	0
BIN:
	db	FALSE
CTRL:
	db	FALSE
$BIN:
	db	'File seems to be binary',eot
$ASC:
	db	'File seems to be pure ASCII',eot
$CNT:
	db	' : ',eot
$TIMES:
	db	' times',cr,lf,eot
Num:
	db	0,0,0,0
@NUM:
	dw	0
$EXCEPT:
	db	tab,bs,ff,cr,lf,esc
ExLen	equ	$-$EXCEPT

	cseg

;
; Tell control found
; ENTRY	Reg IX points to number
;	Reg C  holds control character
;
TellCtrl:
	push	bc
	ld	a,'^'
	call	conout		; Indicate control
	ld	a,'@'
	add	a,c
	call	conout
	ld	de,$CNT
	call	string
	ld	a,(ix+0)	; Get HI
	ld	(Num+1),a	; .. save
	ld	l,(ix+1)
	ld	h,(ix+2)
	ld	(Num+2),hl
	ld	hl,Num
	ld	de,(@NUM)
	push	de
	ld	b,eot
	call	dout32		; .. convert
	pop	de
	call	string
	ld	de,$TIMES
	call	string
	pop	bc
	ret
;
; Fix control array
; ENTRY	Accu holds character
;
SetCtrl::
	push	bc
	push	hl
	push	af
	ld	hl,$EXCEPT
	ld	bc,ExLen
	cpir			; Test exception
	jr	z,SkpCtrl	; .. yeap
	ld	c,a
	add	a,a		; *3
	add	a,c
	ld	c,a
	ld	b,0
	ld	a,TRUE
	ld	(CTRL),a	; Set control
	ld	hl,($memry)
	add	hl,bc
	push	hl
	pop	iy		; Get address
	ld	a,(iy+0)	; Fetch value
	ld	h,(iy+1)
	ld	l,(iy+2)
	call	incrrn		; .. increment
	ld	(iy+0),a
	ld	(iy+1),h
	ld	(iy+2),l
SkpCtrl:
	pop	af
	pop	hl
	pop	bc
	ret
;
; Set binary flag
;
SetBin:
	push	af		; Save character
	ld	a,TRUE
	ld	(BIN),a		; Set binary
	pop	af
	ret
;
; Init control field
; This routine zeroes fields for characters 00..1F and 7F
;
IniCtrl::
	ld	hl,($memry)
	ld	(hl),0		; Clear
	ld	e,l
	ld	d,h
	inc	de
	ld	bc,3*(' ')-1
	ldir			; Clear 24 bit for each entry
	ld	(@NUM),de
	ret
;
; Read records from file and process control check
;
RdFile:
	ld	de,FCB
	call	dskred		; Read record
	ret	c		; .. that's all
	ld	hl,DMA
	ld	b,RecLng
RdLoop:
	ld	a,(hl)
	cp	'~'+1		; Test binary
	call	nc,SetBin
	cp	eof		; Test end of file
	jr	nz,CkCtrl
	ld	a,(BIN)		; Test binary
	or	a
	ret	z		; .. yeap
	ld	a,eof
CkCtrl:
	cp	' '		; Test control
	call	c,SetCtrl
	inc	hl
	djnz	RdLoop
	jr	RdFile
;
; Give report
;
Report::
	ld	a,(BIN)		; Test binary
	or	a
	jr	z,NoBIN
	ld	de,$BIN
	call	string
	ret
NoBIN:
	ld	c,0
	ld	b,' '
	ld	ix,($memry)	; Load base
RepLoop:
	ld	a,(ix+0)	; Test any to be reported
	or	(ix+1)
	or	(ix+2)
	call	nz,TellCtrl	; .. yeap
	ld	de,3
	add	ix,de
	inc	c
	djnz	RepLoop
	ld	a,(CTRL)	; Test control
	or	a
	ld	de,$ASC
	call	z,string	; .. pure ASCII
	ret
;
; Abort on error
; ENTRY	Reg HL points to message
;	Reg DE points to FCB
;
F.Abort:
	push	hl
	call	PrFN		; Print name of file
	pop	de		; .. and message
;
; Abort on error
; ENTRY	Reg DE points to message
;
Abort:
	ld	c,.string
	call	BDOS		; Print message
	jp	OS
;
; %%%%%%%%%%%%%%%%%%
; %% MAIN PROGRAM %%
; %%%%%%%%%%%%%%%%%%
;
FILE:
	ld	de,$ILL.CPU
	sub	a		; Test right machine
	jp	pe,Abort	; .. nope
	call	getver		; .. and OS
	ld	de,$ILL.OS
	jr	c,Abort		; .. nope
	ld	sp,(TPAtop)	; Get stack
	ld	a,(FCBnam)	; Test file here
	cp	' '
	ld	de,$HELP
	jr	z,Abort		; .. nope
	ld	de,FCB
	call	wcard		; Verify no wild card
	ld	hl,$ILL.WLD
	jr	z,F.Abort
	call	open		; Find file
	ld	hl,$NO.FILE
	jr	c,F.Abort	; .. nope
	call	IniCtrl		; Init control fields
	call	RdFile
	call	Report		; Give report
	jp	OS		; .. that's all
	
	end	FILE

