;
; ++++++++++++++++++++++++++++
; +++ KERMIT File routines +++
; ++++++++++++++++++++++++++++
;
; Output the chars in a packet to file or screen.
;
ptchr:
	ld	(temp1),a	; Save the size.
	ld	hl,data		; Beginning of received packet data.
	ld	(outpnt),hl	; Remember where we are.
	ld	a,(rquote)
	ld	b,a		; Keep the quote char in b.
	ld	c,0		; Assume no 8-bit quote char
	ld	a,(quot8)	; Doing 8-bit quoting?
	or	a
	jr	z,ptchr1	; No, keep going
	ld	a,(qbchr)	; Else get 8-bit quote char
	ld	c,a		; Keep this in c
ptchr1:
	ld	hl,temp1
	dec	(hl)		; Decrement # of chars in packet.
	jp	m,rskp		; Return successfully if done.
	ld	a,(remtxt)	; Test destination
	and	a
	jr	nz,ptchr2	; .. skip file
	ld	hl,chrcnt	; Number of chars remaining in dma.
	dec	(hl)		; Decrement.
	jp	p,ptchr2	; Continue if space left.
	call	outbuf		; Output it if full.
	 jp	r		; Error return if disk is full.
ptchr2:
	ld	hl,(outpnt)	; Get position in output buffer.
	ld	a,(hl)		; Grab a char.
	inc	hl
	ld	(outpnt),hl	; And bump pointer.
	ld	e,0		; Assume nothing to OR in.
	cp	c		; Is it the binary quote char?
	jr	nz,ptch2a	; No, keep going
	ld	e,MSB		; Include parity bit
	ld	a,(temp1)
	dec	a
	ld	(temp1),a	; Decrement character count
	ld	a,(hl)		; Get next character
	inc	hl
	ld	(outpnt),hl
ptch2a:
	cp	b		; Is it the quote char?
	jr	nz,ptchr3	; Changed to ptchr3
				; So includes parity
	ld	a,(hl)		; Get the quoted character
	inc	hl
	ld	(outpnt),hl	; And bump pointer.
	ld	hl,temp1
	dec	(hl)		; Decrement # of chars in packet.
	ld	d,a		; Save the char.
	and	MSB		; Turn off all but the parity bit.
	or	e		; Let parity come from either
	ld	e,a		; Save the parity bit.
	ld	a,d		; Get the char.
	and	NoMSB		; Turn off the parity bit.
	cp	b		; Is it the quote char?
	jr	z,ptchr3	; If so just go write it out.
	cp	c		; Maybe it's the 8-bit
				; Prefix character?
	jr	z,ptchr3	; Then don't controllify.
	ld	a,d		; Get the char.
	add	a,'@'		; Make the character a control
				; Char again.
	and	NoMSB		; Modulo 128.
ptchr3:
	or	e		; Or in the parity bit.
	ld	(temp3),a	; .. save
	ld	a,(remtxt)
	and	a		; Test where to go
	ld	a,(temp3)
	jr	z,ptch31	; .. to disk
	push	hl
	push	de
	call	condir		; .. to screen
	pop	de
	pop	hl
	jr	ptchr1
ptch31:
	ld	hl,(bufpnt)	; Destination buffer.
	ld	(hl),a		; Store it.
	inc	hl
	ld	(bufpnt),hl	; Update the pointer
	jr	ptchr1		; And loop to next char.
;
; output the buffer, reset bufpnt and chrcnt
;
outbuf:
	push	bc
	ld	de,FCB
	call	wrtseq		; Write the record.
	pop	bc
	ld	de,erms11
	jp	nz,error3	; Not successfull.
	ld	hl,buff		; Addr for beginning.
	ld	(bufpnt),hl	; Store addr for beginning.
	ld	a,bufsiz-1	; Buffer size.
	ld	(chrcnt),a	; Number of chars left.
	jp	rskp
;
; Get the chars from the file.
;
gtchr:
	ld	a,(squote)	; Get the quote char.
	ld	c,a		; Keep quote char in c.
	ld	a,(filflg)	; Get the file flag.
	or	a		; Is there anything in the DMA?
	jr	z,gtchr0	; Yup, proceed.
	ld	b,0		; No chars yet.
	call	inbuf
	 jp	gtceof		; No more chars, go return EOF.
gtchr0:
	ld	a,(curchk)	; Get current block check type
	sub	'1'		; Get the extra overhead
	ld	b,a		; Get a copy
	ld	a,(spsiz)	; Get the maximum packet size.
	sub	5		; Subtract the overhead.
	sub	b		; Determine max packet length
	ld	(temp1),a	; This is the number of chars
				; We are to get.
	ld	hl,filbuf	; Where to put the data.
	ld	(cbfptr),hl	; Remember where we are.
	ld	b,0		; No chars.
gtchr1:
	ld	a,(temp1)
	dec	a		; Decrement the number of chars left.
	jp	p,gtchr2	; Go on if there is more than one left.
	ld	a,b		; Return the count in A.
	jp	rskp
gtchr2:
	ld	(temp1),a
	ld	a,(chrcnt)	; Space left in the DMA.
	dec	a
	jp	m,gtchr3
	ld	(chrcnt),a
	jr	gtchr4
gtchr3:
	call	inbuf		; Get another buffer full.
	 jp	gtch30		; If no more return what we got.
	jr	gtchr4		; If we got some, proceed.
gtch30:
	ld	a,b		; Return the count in A.
	or	a		; Get any chars?
	jp	z,gtceof	; If not, say we found
				; The end of the file.
	jp	rskp		; If so return them.
gtchr4:
	ld	hl,(bufpnt)	; Position in DMA.
	ld	a,(hl)		; Get a char from the file.
	inc	hl
	ld	(bufpnt),hl
	ld	d,a		; Save the char.
	and	MSB		; Turn off all but parity.
	ld	e,a		; Save the parity bit.
	jr	z,gtch4a	; No parity, skip this check...
	ld	a,(quot8)	; Doing eighth-bit quoting?
	or	a
	jr	z,gtch4a	; No, just proceed normally
	ld	a,(temp1)	; Get space remaining
	cp	2		; 3 chrs left (one cnted already)?
	jp	m,gtchr9	; No, skip this
	dec	a		; Decrement space remaining
	ld	(temp1),a	; Put back.
	ld	hl,(cbfptr)	; Position in character buffer.
	ld	a,(qbchr)	; Get quote character
	ld	(hl),a		; Put the quote char in the buffer
	inc	hl		; Increment destination
				; Buffer pointer
	ld	(cbfptr),hl	; Store the pointer back
	inc	b		; Increment the char count.
	ld	e,0		; No parity bit to OR in.
gtch4a:
	ld	a,d		; Restore the char.
	and	NoMSB		; Turn off the parity.
	ld	d,a		; Save here for later...
	cp	' '		; Compare to a space.
	jr	c,gtchr5	; If less then its a control char,
				; Handle it.
	cp	del		; Is the char a delete?
	jr	z,gtchr5	; Go quote it.
	ld	a,(quot8)	; Are we doing 8th-bit quoting?
	or	a
	jr	z,gtch4c	; If not, skip this test and restore
				; Character.
	ld	a,(qbchr)	; Get 8th-bit quote character
	cp	d		; Same as current character?
	jr	z,gtch4b	; Yes, have to quote it...
gtch4c:
	ld	a,d		; No. get character back again.
	cp	c		; Is it the quote char?
	jp	nz,gtchr8	; If not proceed.
gtch4b:
	ld	hl,temp1	; Point to char count
	dec	(hl)		; Decrement
				; (know room for at least one)
	ld	hl,(cbfptr)	; Position in character buffer.
	ld	(hl),c		; Put the (quote) char in the buffer.
	inc	hl
	ld	(cbfptr),hl
	inc	b		; Increment the char count.
	ld	a,d		; Restore character again
	jp	gtchr8
gtchr5:
	or	e		; Turn on the parity bit.
	cp	'Z'-'@'		; Is it a ^Z?
	jr	nz,gtchr7	; If not just proceed.
	ld	a,(cpmflg)	; Was the file created by CPM?
	cp	1		; In ASCII-mode ?
	jr	z,gtch52	; First Control-Z stops text
	ld	a,(eoflag)	; EOF flag set?
	or	a
	jr	z,gtchr6	; If not just go on.
	ld	a,(cpmflg)	; Was the file created by CPM?
	cp	2		; In BINARY (same as default)
	jr	z,gtchr6	; Yes
				; (this code currently does the same)
	ld	hl,(bufpnt)	; Since CHRCNT is ZERO at EOF-time
	ld	a,(chrcnt)	; (set by INBUF5 B.G.E)
	ld	d,a		; Get the number of chars left
				; In the DMA.
gtch51:
	dec	d
	ld	a,d
	jp	p,gtch53	; Any chars left?
gtch52:
	xor	a		; If not, get a zero.
	ld	(chrcnt),a	; Say no more chars in buffer.
	ld	a,b		; Return the count in A.
	jp	rskp
gtch53:
	ld	a,(hl)		; Get the next char.
	inc	hl		; Move the pointer.
	cp	'Z'-'@'		; Is it a ^Z?
	jr	z,gtch51	; If so see if they rest are.
gtchr6:
	ld	a,'Z'-'@'	; Restore the ^Z.
gtchr7:
	ld	(temp2),a	; Save the char.
	ld	hl,temp1	; Point to the char total remaining.
	dec	(hl)		; Decrement it.
	ld	hl,(cbfptr)	; Position in character buffer.
	ld	(hl),c		; Put the quote in the buffer.
	inc	hl
	ld	(cbfptr),hl
	inc	b		; Increment the char count.
	ld	a,(temp2)	; Get the control char back.
	add	a,'@'		; Make the non-control.
	and	NoMSB		; Modulo 128
gtchr8:
	ld	hl,(cbfptr)	; Position in character buffer.
	or	e		; Or in the parity bit.
	ld	(hl),a		; Put the char in the buffer.
	inc	hl
	ld	(cbfptr),hl
	inc	b		; Increment the char count.
	jp	gtchr1		; Go around again.
gtchr9:
	ld	hl,(bufpnt)	; Not enough room left in buffer...
	dec	hl
	ld	(bufpnt),hl	; Back up over last character
	ld	hl,chrcnt	; Point to character count
	inc	(hl)		; Increment it
	ld	a,b		; Count of chars transferred
	jp	rskp		; Return it
gtceof:
	ld	a,TRUE		; Get a minus one.
	ret
inbuf:
	ld	a,(eoflag)	; Have we reached the end?
	or	a
	jr	z,inbufa	; No
	ld	b,0
	ret			; Return if set.
inbufa:
	push	bc
	push	de
	push	hl
	ld	hl,chrcnt	; Reset character count.
	ld	a,(filflg)	; Get the file flag.
	or	a		; Has there ever been anything
				; In the DMA?
	jr	z,inbuf1	; If so just set it.
	ld	(hl),bufsiz	; If not set it the buffer size.
	xor	a		; Zero the flag.
	ld	(filflg),a
	jr	inbuf2
inbuf1:
	ld	(hl),bufsiz-1	; ELse set it to one less than
				; The buffer size.
inbuf2:
	ld	hl,buff		; Set the buffer pointer.
	ld	(bufpnt),hl
	ld	de,FCB
	call	redseq		; Read a record
	pop	hl
	pop	de
	pop	bc
	jp	z,rskp		; 00H => read O.K.
	xor	a
	ld	(chrcnt),a	; Say no more chars in buffer
	ld	a,TRUE
	ld	(eoflag),a	; Set the EOF flag.
	ret			; Either EOF or ERROR => finish
				; With NON-skip return
;
; Multi-file access subroutine. Allows processing of multiple
; files (i.e., *.ASM) from disk. This routine builds the proper
; name in the FCB each time it is called. This command would be
; used in such programs such as modem transfer, tape save, etc.
; in which you want to process single or multiple files.
;
; The FCB will be set up with the next name, ready to do normal
; processing (OPEN, READ, etc.) when routine is called.
;
; MFFLG1 is count/switch
;	[FALSE for first time thru, TRUE for all others]
; MFFPTR is the current file pointer
;
; EXIT	Carry is set if no more names can be found
;
mfname:
	push	bc		; Save registers
	push	de
	push	hl
	ld	hl,mfflg1	; Check any file
	ld	a,(hl)
	cp	FALSE
	jr	nz,mfn03
	ld	(hl),TRUE	; Set any here
	call	dmaset		; Init DMA addr
	xor	a		; A = 0
	ld	(FCB+_EX),a	; Clear extension
	ld	hl,@top
	push	hl
	call	srcfrs		; Search first
mfn01:
	pop	de
	inc	a		; Check end
	jr	z,mfn02
	add	a,a
	add	a,a
	add	a,a
	add	a,a
	add	a,a		; * 32
	add	a,buff+1-32
	ld	l,a
	ld	h,0
	ld	bc,11
	ld	a,(fcb)
	ld	(de),a		; Set FCB
	inc	de
	ldir			; Unpack
	push	de
	call	srcnxt		; Search next
	jr	mfn01
mfn02:
	xor	a
	ld	(de),a		; Clear node
	inc	de
	ld	(de),a
	ld	hl,@top
	ld	(mffptr),hl	; Init start
mfn03:
	ld	hl,(mffptr)
	ld	a,(hl)
	inc	hl
	or	(hl)		; Check empty
	scf
	jr	z,mffix1
	dec	hl
	ld	de,fcb
	ld	bc,12
	ldir			; Get file
	ld	(mffptr),hl
	or	a		; .. reset carry
mffix1:
	pop	hl		; Restore registers
	pop	de
	pop	bc
	ret			; And return
getfil:
	ld	a,TRUE
	ld	(filflg),a	; Nothing in the DMA.
	xor	a
	ld	(eoflag),a	; Not the end of file.
	ld	de,FCB
	call	open		; Open the file
	jp	rskp
;
; Get the file name (including host to micro translation)
;
gofil:
	ld	hl,data		; Get the address of the file name.
	ld	(datptr),hl	; Store the address.
	ld	hl,FCB+_NAM	; Address of the FCB.
	ld	(fcbptr),hl	; Save it.
	xor	a
	ld	(temp1),a	; Initialize the char count.
	ld	(temp2),a
	ld	(fcb),a		; Set the drive to default to current.
	call	blkfcb		; Balnk the FCB
gofil2:
	ld	hl,(datptr)	; Get the NAME field.
	ld	a,(hl)
	call	uppcas		; Force upper case
	inc	hl
	cp	'.'		; Seperator?
	jr	nz,gofil3
	ld	(datptr),hl	; Update ptr (moved from above)
	ld	hl,fcbt
	ld	(fcbptr),hl
	ld	a,(temp1)
	ld	(temp2),a
	ld	a,9
	ld	(temp1),a
	jr	gofil6
gofil3:
	or	a		; Trailing null?
	jr	z,gofil7	; Then we're done.
	ld	(datptr),hl	; No, can update ptr now.
	ld	hl,(fcbptr)
	ld	(hl),a
	inc	hl
	ld	(fcbptr),hl
	ld	a,(temp1)	; Get the char count.
	inc	a
	ld	(temp1),a
	cp	8		; Are we finished with this field?
	jr	c,gofil2
gofil4:
	ld	(temp2),a
	ld	hl,(datptr)
	ld	a,(hl)
	inc	hl
	ld	(datptr),hl
	or	a
	jr	z,gofil7
	cp	'.'		; Is this the terminator?
	jr	nz,gofil4	; Go until we find it.
gofil6:
	ld	hl,(datptr)	; Get the TYPE field.
	ld	a,(hl)
	call	uppcas		; Force upper case
	or	a		; Trailing null?
	jr	z,gofil7	; Then we're done.
;
; Move above two lines so we don't increment pointer
; if char is null
;
	inc	hl
	ld	(datptr),hl
	ld	hl,(fcbptr)
	ld	(hl),a
	inc	hl
	ld	(fcbptr),hl
	ld	a,(temp1)	; Get the char count.
	inc	a
	ld	(temp1),a
	cp	12		; Are we finished with this field?
	jr	c,gofil6
gofil7:
	ld	hl,(datptr)
	ld	(hl),eot	; Put in sign for printing.
	call	prFN		; Print the file name
	ld	a,(flwflg)	; Is file warning on?
	or	a
	jp	z,gofil9	; If not, just proceed.
	ld	de,FCB
	call	open		; See if the file exists.
				; Does it exist?
	jp	z,gofil9	; If not create it.
	ld	de,infms5
	call	error3
	ld	a,(temp2)	; Get the number of chars
				; In the file name.
	or	a
	jr	nz,gofil8
	ld	a,(temp1)
	ld	(temp2),a
gofil8:
	ld	b,0
	ld	d,b		; Zero d for dad index into filename
	inc	a		; Replace next character after filename
	cp	tab		; Is the first field full?
	jr	nz,gofl80
	ld	b,TRUE		; Set a flag saying so.
	dec	a
gofl80:
	ld	e,a		; Keep current, replace index in d,e.
gofl81:
	ld	hl,fcb		; Get the FCB.
	add	hl,de		; Add in the character number.
	ld	(hl),'&'	; Replace the char with an ampersand.
	push	bc
	push	de
	ld	hl,fcb		; Trim off any CP/M attribute bits
	ld	b,1+8+3		; So they do not affect the new file
gofl82:
	ld	a,(hl)
	and	NoMSB
	ld	(hl),a
	inc	hl
	djnz	gofl82
	ld	de,FCB
	call	open		; See if the file exists.
	pop	de
	pop	bc
				; Does it exist?
	jr	z,gofl89	; If not create it.
	ld	a,b		; Get the field-full flag.
	or	a		; Incr. or decr. ?
	jr	z,gofl83	; Jump if increment
	dec	e		; Decrement the number of chars.
	ld	a,e
	or	a
	jr	z,gofl88	; If no more, die.
	jr	gofl81
gofl83:
	inc	e		; Increment the number of chars.
	ld	a,e
	cp	tab		; Are we to the end?
	jr	c,gofl81	; If not try again.
	ld	a,(temp2)	; Get the original size.
	ld	e,a
	ld	b,TRUE		; Set flag saying field-full, decrement
	jr	gofl81
gofl88:
	ld	de,erms16	; Tell user that we can't rename it.
	call	prtstr
	ret
gofl89:
	ld	c,8		; # of chars in name
	ld	de,fnbuf	; Point to destination
	ld	hl,FCB+_NAM	; Source of name
	ld	b,0		; First-time-thru flag
gof89a:
	ld	a,(hl)		; Get a char from the name
	inc	hl		; Pass it
	cp	' '		; End of this part of name?
	jr	z,gof89b	; Yes, skip rest...
	ld	(de),a		; Else drop char off
	inc	de		; Increment dest ptr
	dec	c		; Decrement count
	jr	nz,gof89a	; And continue if more to go
gof89b:
	ld	a,b
	or	a		; First time thru?
	jr	nz,gof89c	; No, no period
	ld	a,'.'		; Period between parts
	ld	(de),a
	inc	de
	ld	b,TRUE		; Not first time thru anymore
	ld	c,3		; Length of this part
	ld	hl,fcbt		; Start of extension
	jr	gof89a		; Keep copying
gof89c:
	ld	a,eot
	ld	(de),a		; End the name string
	ld	de,fnbuf	; Print the file name
	call	prtstr
gofil9:
	ld	de,FCB
	call	setfil		; Delete the file if it exists.
				; And create new one
	jp	nz,rskp
	ld	de,erms11
	call	error3
	ret
;
; Delete existing file and create new one with same name
; ENTRY	Reg DE points to FCB
; EXIT	Zero Flag set on Disk full
;
setfil:	
	push	de
	call	delete		; Delete the file if it exists.
	pop	de
	call	create		; Now create it.
	ret			; Is the disk full?
;
; Put character to log file
; ENTRY	Reg E holds character
;
logit:
	ld	hl,(bufpnt)
	ld	(hl),e		; Store the char.
	inc	hl
	ld	(bufpnt),hl
	ld	a,l
	or	a		; Buffer full?
	ret	nz
	push	de
	ld	a,xoff		; ^S to stop the
	call	prtout		; Host until we
	ld	de,Log.FCB
	call	wrtseq		; Write out the buffer.
	jr	z,logit2
	ld	de,erms11
	call	prtstr
logit2:
	ld	a,xon		; ^Q to restart
	call	prtout		; The host.
	ld	hl,buff
	ld	(bufpnt),hl	; Reset the buffer pointer.
	pop	de
	ret
;
; Type a file
;
.type:
	ld	de,FCB
	call	redseq		; Read record
	ret	nz		; .. EOF
	ld	hl,buff		; Set buffer
	ld	b,bufsiz
	jr	type..
..type:
	pop	bc
	pop	hl
	inc	hl
	dec	b
	jr	z,.type
type..:
	ld	a,(hl)		; Get character
	cp	'Z'-'@'		; Check EOF
	ret	z
	push	hl
	push	bc
	push	af
	call	putcon		; Type it
	pop	af
	cp	lf		; Test new line
	jr	nz,..type
	ld	a,(pagemode)	; Test page hold
	cp	0
	jr	nz,..type
	ld	a,(pagelen)
	ld	e,a
	ld	a,(pagecur)	; Test page filled
	cp	e
	jr	z,newpage
	inc	a
	ld	(pagecur),a	; Bump page count
	jr	..type
newpage:
	ld	a,0
	ld	(pagecur),a	; Reset page count
	ld	de,giv.cr
	call	prtstr
wt.type:
	call	dircon		; Get character
	cp	ctrlc		; Test CTRL^C
	jr	z,stop.type
	cp	cr
	jr	nz,wt.type	; Wait for CR
	call	ClrLin
	jr	..type
stop.type:
	pop	af
	pop	af
	ret

