	title	Dump Terminal Setups for TURBO PASCAL Terminals
	name	('TERMFILE')
	maclib	base80

; This program simply dumps all or a selected terminal definition
; of the TURBO PASCAL 3.x data file TINST.DTA into TINST.TXT
;
; Call it:
;
;	TERMFILE {parameter}
;
; "parameter" may be:
;
; *	Print all definitions
; n	Print definition 'n'
; ?	Print names of definitions found
; ?-	Print number of definitions found
; +...	Prefix '+' prints all on console, too
; 
; If no parameter is given, a help text will be printed

; Copyright (C)	Werner Cirsovius
;		Hohe Weide 44
;		D-20253 Hamburg
;		Phone: +49/40/4223247

;		Version 1.2

	ext	conout,string,prfn,multip,modrec,decout
	ext	ascbyt,decin,open,rdfcb,rdbuf,rdbfp,dskget,dskput
	ext	fseek,setdma,wrfcb,wrbuf,creatd,closef,delete

	entry	$memry

_STRUC	equ	24

_TERM	equ	0
_LEADIN	equ	_TERM+1+21
_LEADOT	equ	_LEADIN+1+15
_POSCUR	equ	_LEADOT+1+15
_XYMODE	equ	_POSCUR+1+15
_XPOS	equ	_XYMODE+1
_YPOS	equ	_XPOS+1
_XOFF	equ	_YPOS+1
_YOFF	equ	_XOFF+1
_XYDEL	equ	_YOFF+1
_HOME	equ	_XYDEL+2
_CLR	equ	_HOME+1+5
_CLDEL	equ	_CLR+1+5
_INVON	equ	_CLDEL+2
_INVOF	equ	_INVON+1+5
_EOLDEL	equ	_INVOF+1+5
_EOLCL	equ	_EOLDEL+2
_INSLIN	equ	_EOLCL+1+5
_DELLIN	equ	_INSLIN+1+5
_COL	equ	_DELLIN+1+5
_LIN	equ	_COL+1
_DAT	equ	_LIN+1
_RECLEN	equ	_DAT+_STRUC

Dig	equ	3

do	macro	exe,msg,idx
	ld	de,msg
	ld	bc,idx
	call	exe
	endm
;
; Seek record HL in file
;
Seek:
	ld	de,_RECLEN
	call	multip		; Get pointer within file
	ld	de,4
	add	hl,de		; Fix for first two words
	call	modrec		; Get record and pointer
	ld	a,c
	ld	(rdbfp),a	; Save record pointer
	ld	a,1
	ld	(fseek-1),a	; Enable sequential read
	xor	a
	ld	de,DtaFile
	call	fseek		; Position file
	ret
;
; Dump byte in Accu as hex ASCII with prefix
;
xDump:
	push	af		; Save byte
	ld	de,$Hexpr
	call	Fstring		; Give prefix
	pop	af		; Get byte and dump it
;
; Dump byte in Accu as hex ASCII
;
Dump:
	ld	hl,HexB
	ld	(hl),a		; Save byte
	ld	de,$HexB
	push	de
	call	ascbyt		; Convert it
	pop	de
	call	Fstring		; Print it
	ret
;
; Print remaining part of record
;
PrArray:
	call	embidxstr	; Give string and get pointer
	ld	a,(FCOL)	; Get column
	ld	(xy),a
	ld	b,_STRUC	; Set length
PEloop:
	ld	a,(hl)		; Get byte
	push	bc
	push	hl
	call	Dump		; Dump it
	ld	a,(FCOL)	; Get column
	ld	l,a
	ld	a,(GetMaxX)	; Get max column
	cp	l		; Test still in line
	jr	nc,dmpnxt	; Yeap
	call	Fcrlf		; Give new line
	ld	a,(xy)
	ld	b,a		; Get length
	ld	e,' '
	call	Fconchd		; Position in file
dmpnxt:
	pop	hl
	inc	hl
	pop	bc
	djnz	PEloop
	call	Fcrlf		; Close line
	ret
;
; Decode control string ^HL with length in reg B
;
Decode:
	inc	hl
	ld	a,(hl)
	cp	' '		; Test control
	jr	nc,DecChr
	ld	a,'^'
	call	Fconout		; Indicate control
	ld	a,(hl)
	add	a,'@'
DecChr:
	bit	7,a		; Test hex
	push	af
	call	z,Fconout	; Print character
	pop	af
	call	nz,xDump	; Print hex
	djnz	Decode
	ret
;
; Print string in ^DE and control string from record, indexed by reg BC
;
Control:
	call	embidxstr	; Give string and get pointer
	ld	b,(hl)		; Test control defined
	inc	b
	dec	b
	ld	de,$NONE
	push	af
	call	z,Fstring	; Empty
	pop	af
	call	nz,Decode	; Decode control
	call	Fcrlf
	ret
;
; Print string in ^DE and control word from record, indexed by reg BC
;
PrWord:
	call	embidxstr	; Give string and get pointer
	ld	e,(hl)		; Fetch word
	inc	hl
	ld	d,(hl)
	ex	de,hl
	jr	PrWB		; Print it
;
; Print string in ^DE and control byte from record, indexed by reg BC
;
PrByte:
	call	embidxstr	; Give string and get pointer
	ld	l,(hl)		; Get byte
	ld	h,0
PrWB:
	ld	de,$NUM		; Point to buffer
	push	de
	ld	b,eot
	call	decout		; Convert number
	pop	de
	call	Fstring		; Print it
	call	Fcrlf
	ret
;
; Print string in ^DE and normal string from record, indexed by reg BC
;
RECstring:
	call	idxstr		; Give string and get pointer
	ld	b,(hl)		; Fetch length
	inc	b
	dec	b
	ret	z		; End if not defined
RECsL:
	inc	hl
	ld	a,(hl)
	call	Fconout		; Print string
	djnz	RECsL
	ret
;
; Print embedded string ^DE and get pointer ^HL indexed by reg BC
;
embidxstr:
	push	bc		; Save index
	push	de
	ld	b,Dig
	ld	e,' '
	call	Fconchd		; Give blanks
	pop	de
	pop	bc
	call	idxstr		; Print string
	ld	de,$DELIM
	call	Fstring		; Give delimiter
	ret
;
; Print string ^DE and get pointer ^HL indexed by reg BC
;
idxstr:
	call	Fstring		; Give string
	ld	hl,(heap)	; Get record pointer
	add	hl,bc		; Make real address
	ret
;
; Print Dig places of number in reg HL
;
prDec:
	ld	de,$NUM		; Point to buffer
	push	de
	push	de
	ld	b,eot
	call	decout		; Convert number
	pop	de
	or	a
	sbc	hl,de		; Get length of number
	ld	a,Dig
	sub	l		; Get filler
	ld	b,a
	ld	e,' '
	call	nz,Fconchd	; Give places
	pop	de
	call	Fstring		; Print it
	ret
;
; Tell name of terminal
;
TellName:
	ex	de,hl		; Get number
	call	prDec		; Print it
	ld	a,'/'
	call	Fconout
	ld	hl,(FLen)
	call	prDec		; Print max
	call	RdRec		; Read record
;
	ld	a,' '
	call	Fconout
	ld	de,$TERM
	ld	bc,_TERM
	call	RECstring	; Print name of terminal
	ld	a,(TERMDEL)	; Get delimiter
	call	Fconout		; Print it
	call	Fcrlf
	ret
;
; Tell data of terminal with index in reg DE and type in Accu
;
TellDefinition:
	call	TellName	; Tell name of terminal
;
; Process controls
;
	do	Control,$LEADIN,_LEADIN
	do	Control,$LEADOT,_LEADOT
	do	Control,$POSCUR,_POSCUR
	do	PrByte,$XYMODE,_XYMODE
	do	PrByte,$XPOS,_XPOS
	do	PrByte,$YPOS,_YPOS
	do	PrByte,$XOFF,_XOFF
	do	PrByte,$YOFF,_YOFF
	do	PrWord,$XYDEL,_XYDEL
	do	Control,$CLR,_CLR
	do	Control,$HOME,_HOME
	do	PrWord,$CLDEL,_CLDEL
	do	Control,$INVON,_INVON
	do	Control,$INVOF,_INVOF
	do	PrWord,$EOLDEL,_EOLDEL
	do	Control,$EOLCL,_EOLCL
	do	Control,$INSLIN,_INSLIN
	do	Control,$DELLIN,_DELLIN
	do	PrByte,$COL,_COL
	do	PrByte,$LIN,_LIN
	do	PrArray,$DAT,_DAT
;
	ret
;
; Fix column on file output
;
fixcol:
	cp	lf		; Test new line
	ret	z		; Ignore
	cp	cr		; Test start of line
	jr	z,col0		; Clear it
	push	hl
	ld	hl,FCOL
	inc	(hl)		; Advance count
	pop	hl
	ret
col0:
	xor	a
	ld	(FCOL),a	; Clear column
	ld	a,cr		; Return character
	ret
;
; Delete file ^HL
;
Delfile:
	ex	de,hl
	call	delete		; Delete it
	ex	de,hl
	ret
;
; Put new line to file
;
Fcrlf:
	ld	a,cr
	call	Fconout		; Simple new line
	ld	a,lf
	jr	Fconout
;
; Put character E B-times to file
;
Fconchd:
	ld	a,e		; Get constant
	call	Fconout		; .. to file
	djnz	Fconchd		; Count down
	ret
;
; Put string ^DE to file
;
Fstring:
	ld	a,(de)		; Get character
	cp	eot		; Test end
	ret	z		; Yeap
	call	Fconout
	inc	de
	jr	Fstring
;
; Put character in Accu to file
;
Fconout:
	call	fixcol		; Fix column
	call	Putconsole	; Put to console if enabled
	call	dskput		; Put to file
	ret	nc
	ld	de,$WRERR
	jr	..WrErr		; Tell write error
;
; Put character on console if enabled
;
Putconsole:
	push	hl
	ld	hl,CONMODE
	inc	(hl)
	dec	(hl)		; Test console enabled
	push	af
	call	nz,conout	; Yeap
	pop	af
	pop	hl
	ret
;
; End of program - close text file
;
FOS:
	call	closef		; Close file
	jp	nc,OS		; Ok
	ld	de,$CLSERR
	jr	..WrErr		; Tell close error
;
; Set up text file
;
rewrite:
	ld	de,TxtFile
	ld	(wrfcb),de	; Set FCB
	call	creatd		; Create file
	ret	nc		; Ok
WrErr:
	ld	de,$MAKERR
..WrErr:
	ld	hl,TxtFile
	call	Delfile		; Delete file
	jr	IOerr
;
; Read record from data file
;
RdRec:
	ld	hl,(heap)	; Get record pointer
	ld	a,(RecLen)	; Get length of record
	ld	b,a
RdRecLoop:
	call	dskget		; Read record
	jr	c,RdErr		; Error
	ld	(hl),a
	inc	hl
	djnz	RdRecLoop
	ret
;
; Read word from file into reg HL
;
getWord:
	call	dskget		; Read low
	ld	l,a
	call	nc,dskget	; And hi part
	ld	h,a
	ret	nc
RdErr:
	ld	de,$RDERR
..RdErr:
	ld	hl,DtaFile
IOerr:
	push	hl
	call	string		; Tell error
	pop	de
	call	prfn		; And name of file
	jp	OS
;
; ##################
; ### MAIN ENTRY ###
; ##################
;
MAIN:
	ld	sp,(tpatop)	; Get stack
	ld	de,DtaFile
	call	open		; Open file
	ld	de,$MISS
	jr	c,..RdErr	; Error
	ld	hl,DtaFile
	ld	(rdfcb),hl	; Set FCB
	ld	de,($memry)
	ld	(rdbuf),de	; Set read DMA
	ld	hl,reclng
	add	hl,de
	ld	(wrbuf),hl	; Set write DMA
	ld	bc,reclng
	add	hl,bc
	ld	(heap),hl	; Set start of data
	call	setdma		; Set buffer
	call	rewrite		; Set up TXT file
	call	getWord		; Read number of records
	ld	(FLen),hl
	ld	(CurFS),hl
	call	getWord		; Read record length
	ld	(RecLen),hl
	ld	de,$INVREC
	ld	a,h		; Verify correct record length
	or	a
	jr	nz,..RdErr
	ld	a,_RECLEN
	cp	l
	jr	nz,..RdErr
	call	CCPblnk		; No blanks in CCP line
moreCCP:
	ld	a,(hl)		; Test input parameter
	or	a
	jr	z,prHelp	; Nope, give help
	ld	a,(de)		; Get character
	cp	'*'		; Test print all definitions
	jr	nz,SelTerm
;
; =======================================
; == Input '*' - Print all definitions ==
; =======================================
;
	ld	hl,TellDefinition
	jp	Dumpterm	; Dump terminal
;
; ------------------------------------------
;
SelTerm:
	cp	'?'		; Test ask for number of definitions in file
	jr	z,TellTermnum
	cp	'+'		; Test enable console I/O
	jr	nz,TellTermdef	; Nope, must be selection
;
; ===================================================
; == Input '+' - Print definitions also on console ==
; ===================================================
;
	ld	(CONMODE),a	; Enable console
	inc	de		; Fix command line
	dec	(hl)
	jr	moreCCP		; Test more in line
;
; ======================================
; == Input 'n' - Print definition 'n' ==
; ======================================
;
TellTermdef:
	ld	b,null
	call	decin		; Get number
	ld	de,$ILLTERM
	jr	c,comstr	; Invalid
	ld	(Term),hl	; Save it
	ld	bc,(FLen)	; Get length
	or	a
	sbc	hl,bc		; Test in range
	jr	nc,comstr	; Invalid
	ld	hl,(Term)	; Get terminal selection
	dec	hl
	call	Seek		; Position in file
	ld	de,(Term)	; Get terminal selection
	call	TellDefinition	; Tell data of terminal
	jp	FOS
prHelp:
	ld	de,$HELP	; Give some help
comstr:
	ld	hl,TxtFile
	call	Delfile		; Delete file
	call	string
	jp	OS
;
; ======================================================
; == Input '?(-)' - Print number of definitions found ==
; ======================================================
;
TellTermnum:
	inc	de
	ld	a,(de)
	cp	'-'		; Test only number to be printed
	push	af
	ld	de,$TERMNUM
	call	Fstring		; Give prefix
	ld	hl,(FLen)
	call	prDec		; Print max
	call	Fcrlf
	pop	af
	jp	z,FOS		; End if no terminal requested
	ld	a,' '
	ld	(TERMDEL),a	; No delimiter
	ld	hl,TellName
	jp	Dumpterm	; Dump terminals on request
;
; Skip blanks in CCP line and fix remainding length
;
CCPblnk:
	ld	hl,ccplen	; Point to length
	ld	de,ccp		; Point to line
CCPtab:
	ld	a,(hl)		; Test any in there
	or	a
	ret	z		; Nope
	ld	a,(de)
	cp	' '		; Test blank
	jr	z,CCPskp
	cp	tab		; Test tabulator
	ret	nz		; Nope
CCPskp:
	inc	de		; Skip it
	dec	(hl)		; Fix remainder
	jr	nz,CCPtab
	ret
;
; Dump entire terminal definition or name only
;
Dumpterm:
	ld	(DmpType),hl	; Save entry address
	ld	de,1		; Init index
DmpLoop:
	ld	hl,(CurFS)
	dec	hl		; Count down terminals
	ld	(CurFS),hl
	bit	7,h		; Test end
	jp	nz,FOS
	push	de
	call	JpType		; Tell data of terminal
	pop	de
	inc	de
	jr	DmpLoop		; Try next
;
; Execute dump
;
JpType:
	ld	hl,(DmpType)	; Get address
	jp	(hl)		; Execute it

	dseg

CONMODE:
	db	0		; Non zero if console enabled
DmpType:
	ds	2
DtaFile:
	db	0,'TINST   DTA'
	ds	34
TxtFile:
	db	0,'TINST   TXT'
	ds	34
$MISS:
	db	'Missing file ',eot
$RDERR:
	db	'Error reading file ',eot
$INVREC:
	db	'Invalid record size in file ',eot
$ILLTERM:
	db	'Invalid terminal selection',eot
$MAKERR:
	db	'Cannot create file ',eot
$CLSERR:
	db	'Cannot close file ',eot
$WRERR:
	db	'Error writing file ',eot
FCOL:
	db	0
Term:
	ds	2
FLen:
	ds	2
CurFS:
	ds	2
RecLen:
	ds	2
$memry:
	ds	2
heap:
	ds	2
xy:
	ds	1+1
GetMaxX:
	db	80-6+1
HexB:
	ds	1
$HexB:
	db	'xx ',eot
$Hexpr:
	db	'0x',eot
$NUM:
	db	'65535',eot
$DELIM:
	db	' : ',eot
$NONE:
	db	'NONE',eot
$TERM:
	db	'Terminal ',eot
$LEADIN:
	db	'             Lead In',eot
$LEADOT:
	db	'            Lead Out',eot
$POSCUR:
	db	'      Cursor setting',eot
$XYMODE:
	db	'            X-Y mode',eot
$XPOS:
	db	'          X position',eot
$YPOS:
	db	'          Y position',eot
$XOFF:
	db	'          X   offset',eot
$YOFF:
	db	'          Y   offset',eot
$XYDEL:
	db	'          XY   delay',eot
$CLR:
	db	'        Clear screen',eot
$HOME:
	db	'         Home cursor',eot
$CLDEL:
	db	'        Clear  delay',eot
$INVON:
	db	'          Invers  on',eot
$INVOF:
	db	'          Invers off',eot
$EOLDEL:
	db	'           EOL delay',eot
$EOLCL:
	db	'Clear to end of line',eot
$INSLIN:
	db	'         Insert line',eot
$DELLIN:
	db	'         Delete line',eot
$COL:
	db	'      Screen columns',eot
$LIN:
	db	'      Screen   lines',eot
$DAT:
	db	'           Remainder',eot
$TERMNUM:
	db	'Number of definitions found:',eot
TERMDEL:
	db	':'
$HELP:
	db	'This program simply dumps all or a selected terminal definition'
	db	cr,lf
	db	'of the TURBO PASCAL 3.x data file TINST.DTA into TINST.TXT'
	db	cr,lf,lf
	db	'Call it:'
	db	cr,lf,lf
	db	tab,'TERMFILE {parameter}'
	db	cr,lf,lf
	db	'"parameter" may be:'
	db	cr,lf,lf
	db	'*',tab,'Print all definitions'
	db	cr,lf
	db	'n',tab,'Print definition ''n'''
	db	cr,lf
	db	'?',tab,'Print names of definitions found'
	db	cr,lf
	db	'?-',tab,'Print number of definitions found'
	db	cr,lf
	db	'+...',tab,'Prefix ''+'' prints all on console, too'
	db	cr,lf,lf
	db	'If no parameter is given, a help text will be printed'
	db	cr,lf,eot

	end	MAIN

