	title	TURBO Overlay Extractor
	name	('OVLEXT')
	maclib	base80

; Program extracts a single overlay file from a TURBO
; Pascal generated overlay.

; Call it:
;		OVLEXT file.ext hex_start hex_len
;
; It generates a file extsssll.COM
;
; ext	Extension from big overlay
; sss	Last three characters from start
; ll	Last two characters from len

	entry	$memry

	ext	cmdarg,hexin,hexout,tstdig,strcm0,crlfc,prfn
	ext	open,creatd,filsiz,fseek,dskred,dskwrt,close

ARGC	equ	3

	dseg
$memry:
	ds	2
start:
	ds	2
len:
	ds	2
filen:
	ds	2
ASClen:
	db	'xxxx'
ASCstrt:
	db	'xxxx'
FOVCOM:
	db	0,'extsssllCOM'
	ds	fcblen-(.fdrv+.fname+.fext)
$CPY1:
	db	'Overlay from file ',null
$CPY2:
	db	' copied to ',null
$ILLCCP:
	db	'Program extracts a single overlay file from a TURBO'
	db	cr,lf
	db	'Pascal generated overlay.'
	db	cr,lf,lf
	db	'Call it:'
	db	cr,lf
	db	tab,tab,'OVLEXT file.ext hex_start hex_len'
	db	cr,lf,lf
	db	'It generates a file extsssll.COM'
	db	cr,lf,lf
	db	'ext',tab,'Extension from big overlay'
	db	cr,lf
	db	'sss',tab,'Last three characters from start'
	db	cr,lf
	db	'll',tab,'Last two characters from len'
	db	cr,lf,null
$NOFILE:
	db	'Cannot find file ',null
$ILLFILE:
	db	'Invalid size of file ',null
$ILLSIZE:
	db	'Selected arguments out of range of file ',null
$ILLHEX:
	db	'Invalid hex character in argument ',null
$ILLEXT:
	db	'Invalid extension of file ',null
$NODIR:
	db	'Cannot create file ',null
$POSERR:
	db	'Cannot position in file ',null
$RDERR:
	db	'Early end of file ',null
$ILLWR:
	db	'Write error on file ',null
$CLOSE:
	db	'Canno close file ',null
	cseg
;
; Convert parameter to hex ASCII
; Relative parameter pointer in reg DE
;
atoi:
	ld	hl,($memry)	; Point to parameter
	add	hl,de		; Position in memory
	ld	e,(hl)		; Get argument pointer
	inc	hl
	ld	d,(hl)
	push	de
	ld	b,null		; Set end character
	call	hexin		; Get hex
	pop	de
	ret	nc		; Ok
illhex:
	push	de
	ld	de,$ILLHEX
	call	strcm0		; Tell invalid hex found
	pop	de
	call	strcm0
	call	crlfc
	jp	OS
;
; Scan command line
;
scanccp:
	ld	b,ARGC
	call	cmdarg		; Verify input parameters
	jr	c,IllCCP	; Invalid correct number
	sub	ARGC
	jr	nz,IllCCP
	ld	de,2		; Set offset to start
	call	atoi		; Convert it
	ld	(start),hl
	ld	de,4		; Set offset to len
	call	atoi		; Convert it
	ld	(len),hl
	ld	de,ASClen
	call	hexout		; Convert length back to ASCII
	ld	hl,(start)
	ld	de,ASCstrt
	call	hexout		; Convert start back to ASCII
	ret
IllCCP:
	ld	de,$ILLCCP
	call	strcm0		; Tell error
	jp	OS
;
; Open source file
;
reset:
	ld	de,FCB
	call	open		; Open file
	jr	c,NoFile	; Not found
	call	filsiz		; Get size of file
	jr	c,illFile	; Invalid
	jr	nz,IllFile
	ld	(filen),hl	; Save it
	ex	de,hl
	ld	bc,(len)
	ld	hl,(start)
	add	hl,bc		; Calculate last record
	ex	de,hl
	or	a
	sbc	hl,de		; Verify in range
	ret	nc		; Ok
illsize:
	ld	de,$ILLSIZE
	jr	SrcFerr
illFile:
	ld	de,$ILLFILE
	jr	SrcFerr
NoFile:
	ld	de,$NOFILE
SrcFerr:
	call	strcm0		; Tell error
	ld	de,FCB
xxxFerr:
	call	prfn
	call	crlfc
	jp	OS
;
; Build FCB for output
;
; It generates a file extsssll.COM
;
illext:
	ld	de,$ILLEXT
	jr	SrcFerr		; Tell invalid extension
buildfile:
	ld	hl,FCBext	; Point to extension of source
	ld	b,.fext
chkhex:
	ld	a,(hl)		; Get extension character
	call	tstdig		; Verify 0..9
	jr	c,illext	; Nope
	inc	hl
	djnz	chkhex
	ld	de,FOVCOM+.fdrv	; Point to name of destination file
	ld	hl,FCBext	; Point to extension again
	ld	bc,.fext
	ldir			; Construct name of file
	ld	hl,ASCstrt+1
	ld	bc,3
	ldir
	ld	hl,ASClen+2
	ld	bc,2
	ldir
	ret
;
; Prepare destination file
;
rewrite:
	ld	de,FOVCOM
	call	creatd		; Create file
	ret	nc		; Ok
nodir:
	ld	de,$NODIR
DstFerr:
	call	strcm0		; Tell error
	ld	de,FOVCOM
	jr	xxxFerr
;
; Position in source file
;
posfile:
	ld	de,FCB		; Point to source file
	ld	hl,(start)	; Get start record
	xor	a
	call	fseek		; Position file
	ret	nc		; Ok
poserr:
	ld	de,$POSERR
	jr	SrcFerr		; Tell error
;
; Copy file
;
illRd:
	ld	de,$RDERR
	jr	SrcFerr		; Tell error
illWr:
	ld	de,$ILLWR
	jr	DstFerr
clserr:
	ld	de,$CLOSE
	jr	DstFerr
;
copyovl:
	ld	hl,(len)	; Init length
cpyloop:
	ld	de,FCB
	call	dskred		; Read record
	jr	c,illRd		; Invalid
	ld	de,FOVCOM
	call	dskwrt		; Write record
	jr	c,illWr		; Invalid
	dec	hl
	ld	a,l
	or	h		; Test end
	jr	nz,cpyloop	; Nope
	ld	de,FOVCOM
	call	close		; Close file
	ret	nc		; Ok
	jr	clserr		; Error
;
; Give final message
;
byebye:
	ld	de,$CPY1
	call	strcm0		; Tell action
	ld	de,FCB
	call	prfn
	ld	de,$CPY2
	call	strcm0
	ld	de,FOVCOM
	call	prfn
	call	crlfc
	ret
;
; ###########################
; ##### START THE PROGRAM ###
; ###########################
;
OVLEXT:
	ld	sp,(TPATOP)	; Get stack
	ld	de,CCPlen
	ld	hl,($memry)	; Get pointer
	call	scanccp		; Scan command line
	call	buildfile	; Build FCB for output
	call	reset		; Open source file
	call	rewrite		; Prepare destination file
	call	posfile		; Position in source file
	call	copyovl		; Copy file
	call	byebye		; Give final message
	jp	OS

	end	OVLEXT

