;
; +++++++++++++++++++++++
; +++ KERMIT commands +++
; +++++++++++++++++++++++
;
; This is the BYE command.
; It tells the remote KERSRV to logout and exits.
;
bye:
	call	cfmcmd
	call	logo		; Tell the main frame to logout.
	 jp	kermit		; If it fails, don't exit.
	call	sysexit		; Clear line
	jp	exit1		; Exit Kermit.
;
; This is the CLEAR screen command
;
clrscr:
	call	ResScr		; Clear
	jp	kermit
;
; This is the DISCONNECT command
;
disconnect:
	call	DTR.Off		; Turn off DTR
	jp	kermit
;
; This is the HANGUP command
;
hangup:
	call	DTR.Drop	; Drop DTR
	jp	kermit
;
; This is the CONNECT command.
;
telnet:
	call	DTR.On		; Turn on DTR
	call	cfmcmd
	ld	de,infms7	; Output start of message
telnt1:
	call	prtstr
	call	escpr		; Print the escape char.
	ld	de,infms8	; Output the rest of the message
	call	prtstr
	ld	de,mid.w
	call	pr.window
	call	ResScr
chrlup:
	call	prtchr		; See if char at port (type to cons).
	call	conchr		; See if char at cons (type to port).
	 jp	kermit		; Go to command loop.
	jr	chrlup		; Go do it again.
prtchr:
	call	auxrdy		; Is char at auxin?
	ret	z		; No
	call	auxesc		; Read char from auxin
				; And fall into "normal" code
	and	NoMSB		; Make seven bit.
	jr	z,prtchr	; .. skip zero
	ld	e,a		; Set the char aside.
	cp	xon		; Is it an XON?
	jr	nz,prtxof	; No
	xor	a		; Yes, reset XOFF flag
	ld	(xofflg),a
	ld	a,e		; Restore character
	jr	prttst
prtxof:
	cp	xoff		; Is it an XOFF?
	jr	nz,prttst	; No
	ld	(xofflg),a	; Yes, set flag
prttst:
	ld	a,(alogfl)	; Get the log flag.
	or	a
	call	nz,logit	; Log the char if it is set.
	ld	a,(prnflg)	; Get Print parallel flag
	or	a
	call	nz,putlst	; Char to printer
	ld	a,e		; Get character back
	call	condir		; Output the char to the console.
	jr	prtchr		; Go see if there is another character.
conchr:
	call	contst		; Direct console I/O bdos call.
	and	NoMSB		; Keep only 7 bits
	jp	z,rskp		; Null means nothing there.
	ld	e,a		; Move the char for comparison.
	ld	(lstchr),a	; Save it
	ld	a,(escchr)	; Get the escape char.
	cp	e		; Is it an escape char?
	jr	z,intchr	; If so go process it.
	ld	a,e		; Get the char.
	call	setpar		; Set parity (if any).
	ld	e,a		; Restore it.
	call	prtout		; Output the char to the port.
	ld	a,(ecoflg)	; Get the echo flag.
	or	a		; Is it turned on?
	jp	z,rskp		; If not we're done here.
	ld	a,e		; Get the char.
	and	NoMSB		; Turn off the parity bit.
	call	condir		; Echo the char.
	jp	rskp
intchr:
	call	contst		; Direct console I/O.
	jr	z,intchr	; If so, go until we get a char.
	ld	b,a		; Save the actual char.
	cp	ctrlc		; Is it Control-C?
	jr	z,contc		; Yes
	call	uppcas		; Convert to upper case.
	cp	'C'		; Is it close?
	jr	nz,intch0	; If not proceed.
contc:
	ld	de,big.w
	call	pr.window
	call	ResScr
	ld	de,infms9	; Say we are back.
	call	prtstr
	ld	a,(logflg)	; Get the log flag.
	or	a		; Is it set?
	ret	z		; Return if it is zero.
	xor	a
	ld	(logflg),a	; Turn off the log flags.
	ld	(alogfl),a
	ld	de,infms6	; Tell user we are closing file.
	call	prtstr
	ld	hl,(bufpnt)
	ld	(hl),'Z'-'@'	; Put in a ^Z.
	ld	de,Log.FCB
	call	wrtseq		; Write the last buffer.
	ld	de,Log.FCB
	call	close		; Close the file.
	ret
intch0:
	cp	'S'		; Is it status?
	jr	nz,inch01	; If not, proceed.
	call	stat01		; Print out the status stuff.
	jp	rskp
inch01:
	cp	'R'-'@'		; Control-R?
	jr	z,inch02	; Yes
	cp	'R'		; (plain) R?
	jr	nz,inch03	; No
inch02:
	ld	a,(fileio)	; TRANSMIT in progress?
	or	a
	jr	z,inch03	; No,ignore
	pop	bc		; Remove return address (non-local goto)
	jp	rexmit		; Retransmit line
inch03:
	ld	a,b		; Get the char.
	cp	'?'		; Is it a help request?
	jr	nz,intch1	; If not, go to the next check.
	ld	a,(fileio)	; TRANSMIT in progress?
	or	a
	jr	z,inch04	; No
	ld	de,xmthlp	; Tell about R too
	call	prtstr
inch04:
	ld	de,inthlp	; If so, get the address of
				; The help message.
	call	prtstr
	call	escpr		; Print escape character
	ld	de,inhlp2	; Print the rest
	call	prtstr
	jp	intchr		; Get another char.
intch1:
	ld	a,b		; Get the character.
	call	uppcas		; Convert to upper case if necessary.
	cp	'P'		; Pause LOG file
	jr	z,pauseLOG
	cp	'W'		; Clear screen
	jr	z,intch.
	cp	'B'		; Send a BREAK?
	jr	z,sendbr	; Yes, go send a BREAK
	ld	a, b		; '0' to send a null.
	cp	'0'		; Is it?
	jr	nz,intch3	; No.
	ld	a,0		; Yes, send an ASCII zero.
	call	prtout
	jp	rskp
pauseLOG:
	ld	a,(alogfl)	; Toggle flag
	cpl
	ld	(alogfl),a
	jp	rskp
intch.:
	call	ResScr		; Clear screen
	jp	rskp
intch3:
	cp	'D'		; Test drop line
	jr	nz,intch4
	call	DTR.Drop	; .. hang up the modem
	jp	rskp
intch4:
	ld	e,a		; Put the char into another reg.
	ld	a,(escchr)	; Get the escape char.
	cp	e		; Is it the escape char?
	jr	nz,intchz	; If not, go send a beep to the user.
	ld	a,e		; Get the char.
	call	setpar
	call	prtout		; Output it.
	jp	rskp		; Return, we are done here.
intchz:
	ld	a,bell		; Otherwise send a beep.
	call	condir
	jp	rskp
sendbr:
	call	sysbrk		; Send break
	jp	rskp		; .. and return
;
; The COPY command
;
copy:
	ld	a,cmifi		; go parse a file name
	ld	de,cFCBs	; use the source for copy FCB (Allows copy
	call	comnd		; from a TAKE file etc)
	 jp	kermit		; if error, abort
	ld	a,cmifin	; go parse a target file name
	ld	de,cFCBd	; use destination FCB
	call	comnd		; get it
	 jp	copy0
copy0:
	ld	b,FCBnam	; we are gonna test drive, file and extention
	ld	de,cFCBs	; from source file name...
	ld	hl,cFCBd	; to destination file name
	xor	a		; clear flag for difference found
	ld	(equflg),a
copy1:
	ld	a,(de)		; get source file name character
	cp	(hl)		; test with targer file name
	jr	z,copy2		; if equal, do nothing
	ld	a,(equflg)	; else update flage (ie files are different)
	inc	a
	ld	(equflg),a
copy2:
	inc	hl
	inc	de
	djnz	copy1		; up pointers and test for next char
	ld	a,(equflg)	; if still null, then its a daft thing to do
	and	a
	jr	nz,copy3	; its not a daft thing to do
	ld	de,samems	; load up "File source and destination the same"
	jr	coperr		; tell user
copy3:
	ld	de,cFCBs	; load up source FCB
	call	open		; open file
	inc	a		; error on open?
	jr	nz,copy4
	ld	de,nofile	; assume file not found
coperr:
	call	prtstr
	jp	kermit		; and die
copy4:
	ld	de,cFCBd	; load up destination FCB
	call	delete		; destroy target name if it exists
	ld	de,cFCBd	; load up destination FCB
	call	create		; make a file
	inc	a		; make error?
	jr	nz,copy4a
	ld	de,erms12	; no directory space
	jr	copclr		; close source file
copy4a:
	ld	de,cFCBd	; load up destination FCB...
	call	open		; for open
	inc	a		; error on open?
	jr	nz,copy5	; could do with better error detection...
	ld	de,erms15	; ... but assume its a disk full
copclr:
	call	prtstr
	jr	copy7		; close source file and jmp kermit
copy5:
	call	dmaset		; set default dma address to 80h
copy6:
	ld	de,cFCBs	; copy routine proper.. get a sector
	call	redseq
	and	a		; error reading the file?
	jr	nz,copy8	; yes, then cope with it (could be EOF)
	ld	de,cFCBd	; send sector to destination
	call	wrtseq
	and	a		; error on write (disk full?)
	jr	z,copy6		; no error, so do another sector.
	ld	de,erms17	; say disk is full
	call	prtstr
	ld	de,cFCBd	; close the output file...
	call	close
	ld	de,cFCBd	; ... and then delete it
	call	delete		; ... and then drop through to
copy7:
	ld	de,cFCBs	; here to close the source FCB
	call	close
	jp	kermit
copy8:
	ld	de,cFCBd	; orderly close of destination file
	call	close
	jr	copy7		; now close the source file as well.
;
; The DIR command
; Display the directory for the requested drive
;
;	Note: This is abstracted from Keith Peterson's DIRF.ASM
;		directory print function.  Thank's again Keith.
;
dir:
	ld	de,FCB		; Where to put the data, if any.
	ld	a,cmifin
	call	comnd		; Plse a full or piece of file-spec
	 jp	dir2		; Didn't get a FULL file-spec
	jp	dir4		; Lets do it
;
; Make FCB all '?' to match any file
;
dir2:
	ld	a,(FCB)
	cp	' '		; CMIFIN leaves that as ' '
	jr	nz,dir2a	; He typed at least x:
	xor	a
	ld	(FCB),a		; Default drive
dir2a:
	ld	hl,FCB+_NAM
	ld	b,11		; FN+FT count.
dir3:
	ld	(hl),'?'	; Store '?'s in FCB.
	inc	hl
	djnz	dir3
;
; Print signon message and drive name
;
dir4:
	ld	a,(FCB)
	or	a		; Default ?
	jr	nz,dir4a
	ld	a,(curdsk)	; Get default
dir4a:
	add	a,'@'		; Asciize it
	ld	(dnam14),a	; Save it in message.
	call	crlf
;
; Initialize number of names per line counter
;
	ld	a,npl		; Nr. names per line.
	ld	(nnams),a	; Init counter.
	ld	a,FALSE		; Clear any file
	ld	(filfnd),a
	ld	(mfflg1),a
dir5:
	call	mfname		; Get some names
	jr	c,dir17		; Got none - do summary
	ld	a,(filfnd)	; Check 1st file
	cp	FALSE
	jr	nz,dir6
	ld	a,TRUE
	ld	(filfnd),a	; Set any
	ld	de,inms14	; Point to message
	call	prtstr
dir6:				; Check for console break
	call	conrdy		; Ck status of kbd.
				; Any key pressed?
	jr	z,dir6a		; Nope, keep going
	call	getcon		; Gobble key
	jr	dir17		; And print summary only
;
; Print an entry
;
dir6a:
	call	pr.file		; Print Filename
	call	dir25		; Print size
	ld	hl,nnams	; Point to names counter.
	dec	(hl)		; One less on this line.
	push	af
	call	nz,dir7		; No cr-lf needed, do fence.
	pop	af
	call	z,dir12		; Cr-lf needed.
	jr	dir5
;
; Determines free space remaining
;
dir17:
	ld	a,(filfnd)
	cp	FALSE		; Check any file
	ld	de,inms27
	call	z,prtstr	; .. none
	ld	a,(FCB)
	or	a
	jr	z,dir18
	dec	a
	call	logdrv
dir18:	
	call	dmaset		; Set DMA buffer
	call	getdrv		; Get drive
	call	spcget		; And it's free space
test::
	ld	hl,(buff)	; Fetch records
	ld	a,(buff+2)
	call	dir27		; Shift by 8 to get kBytes
	call	dir27
	call	dir27
	push	hl
	ld	de,inms15	; "Drive "
	call	prtstr
	ld	a,(FCB)		; If no drive, get
	or	a		; Logged in drive
	jr	nz,dir24
	call	getdrv
	inc	a
dir24:
	add	a,'@'
	ld	(inms16),a
	ld	de,inms16	; " has "
	call	prtstr
	pop	hl		; Get number of bytes available
	call	nout
	ld	de,inms17	; " K bytes free"
	call	prtstr
;
; Fall into EXIT
; Exit - All done, return via jmp (as for all main commands)
;
	call	crlf
	ld	a,(curdsk)
	dec	a		; Relative to 0
	call	logdrv		; Back to "logged in" disk
	jp	kermit		; ...and return to kermit.
;
; Compute the size of the file
;
dir25:
	ld	de,FCB
	call	filsiz		; Get file-size
	ld	hl,(FCBrno)	; Get value
	ld	a,(FCBrno+2)
	call	dir27		; Divide by 8 to get kBytes
	call	dir27
	call	dir27
	ld	a,(FCBrno)
	and	00000111b	; Test remainder
	jr	z,dir25x
	inc	hl		; Else fix
dir25x:
	ld	a,l
	cp	10
	jr	nc,dir25d
	call	dir8
	jr	dir25e
dir25d:
	cp	100
	call	c,dir9
dir25e:
	call	dir9		; A space
	call	nout		; ..go print it
	ld	a,'k'		; ..and follow with K size
	call	dir10
	ret
;
; Shift 24 <L,H,ACCU> bits right one place
;
dir27:
	srl	a		; .. hi
	rr	h		; .. middle
	rr	l		; .. low
	ret
;
; Print  space, fence character, then   space
;
dir7:
	call	dir9
	ld	a,':'		; Fence character.
	call	dir10
	jr	dir9
;
; Print two spaces
;
dir8:
	call	dir9
;
; Print one space
;
dir9:
	ld	a,' '
;
; Type char in A register
;
dir10:
	push	bc
	push	de
	push	hl
	call	putcon		; Write char to console function.
	pop	hl
	pop	de
	pop	bc
	ret
;
; CR-LF routine. HL=NNAMS upon entry
;
dir12:
	ld	a,cr		; CR.
	call	dir10
	ld	a,lf		; LF.
	call	dir10
	ld	(hl),npl	; Number of names per line.
	ret
;
; Print name of file
;
pr.file:
	ld	hl,FCB+_NAM	; Point to Filename
	ld	b,8		; File name length.
	call	dir11		; Type filename.
	ld	a,'.'		; Period after FN.
	call	dir10
	ld	b,3		; Get the filetype.
;
; Type (B) characters from memory (HL)
;
dir11:
	ld	a,(hl)
	and	NoMSB		; Remove CP/M attributes.
	call	dir10
	inc	hl
	djnz	dir11
	ret
;
; ERAse a CP/M file
;
era:
	ld	a,cmifi		; Parse a file-spec
	ld	de,FCB		; Into FCB
	call	comnd
	 jp	kermit		; Bad parse
	ld	a,FALSE
	ld	(mfflg1),a
	ld	(eraflg),a
era0:
	call	mfname		; Check if valid
				; Carry if FILE not found
	jr	c,era1		; Found at least one
	ld	a,(FCBt)
	and	MSB		; Check R/O
	jr	z,do.era?
	ld	de,inms28
	call	delete.it?	; Ask for deletion
	jr	z,era0		; Not erased
	ld	a,(FCBt)
	and	NoMSB
	ld	(FCBt),a	; Reset R/O bit
	ld	de,FCB
	call	attrbut		; Set attribute
do.era:
	ld	de,FCB
	call	delete
	ld	a,TRUE
	ld	(eraflg),a
	jr	era0
do.era?:
	ld	a,(erawrn)	; Test warning
	or	a
	jr	z,do.era	; .. nope
	ld	de,inms31
	call	delete.it?	; Ask for deletion
	jr	z,era0		; .. nope
	jr	do.era		; .. do it
era1:
	ld	a,(eraflg)
	cp	FALSE
	ld	de,erms15	; "unable to find file"
	jp	z,..reent
	ld	de,inms18	; " File(s) erased"
	jp	..reent
;
; Ask for file deletion
; ENTRY	Reg DE points to message
; EXIT	Zero set if no to be deleted
;
delete.it?:
	push	de
	call	crlf
	call	pr.file		; Print file name
	pop	de
	ld	ix,..N.Y
	call	get.decision	; Check delete
	ret
;
..N.Y:
IF	GERMAN
	db	'NJ'
ELSE
	db	'NY'
ENDIF;GERMAN
;
; This is the QUIT command.
; It leaves KERMIT and returns to CP/M.
;
quit:
	call	IOres		; Reset I/O
	jp	warm		; .. then return to system
;
; This is the EXIT command.
; It leaves KERMIT and returns to CP/M.
;
exit:
	call	cfmcmd
exit1:
	call	IOres		; Reset I/O
	call	sysreset	; .. reset system
	jp	warm		; Then return to system.
;
; Clean up I/O
;
IOres:
IF	IOport	EQ	_BDOS
	ld	de,scb.i.s
	call	scb		; Reset vector
	ld	de,scb.o.s
	call	scb		; Reset vector
ENDIF;IOport EQ _BDOS
	call	ResScr
	ld	de,cur.w
	call	pr.window	; Reset window to old state
	ld	de,inms29
	call	prtstr		; Tell end
	ret
;
; This is the FINISH command.
; It tells the remote KERSRV to exit
;
finish:
	call	cfmcmd
	xor	a
	ld	(numtry),a	; Inititialize count.
	ld	a,'1'		; Reset block check type
	ld	(curchk),a	; To single character
finsh1:
	ld	a,(numtry)	; How many times have we tried?
	cp	maxtry		; Too many times?
	jr	c,finsh3	; No, try it.
finsh2:
	ld	de,erms18	; Say we couldn't do it.
	jp	..reent		; Go home.
finsh3:
	inc	a		; Increment the number of tries.
	ld	(numtry),a
	xor	a
	ld	(argblk),a	; Make it packet number zero.
	ld	a,1
	ld	(argblk+1),a	; One piece of data.
	ld	hl,data
	ld	(hl),'F'	; Finish running Kermit.
	ld	a,'G'		; Generic command packet.
	call	spack
	 jp	finsh2		; Tell the user and die.
	call	rpack		; Get an acknowledgement.
	 jp	finsh1		; Go try again.
	cp	'Y'		; ACK?
	jp	z,kermit	; Yes, we are done.
	cp	'E'		; Is it an error packet?
	jr	nz,finsh1	; Try sending the packet again.
	call	error1		; Print the error message.
	jp	kermit
;
; This is the HELP command.
; It gives a list of the commands.
;
help:
	call	cfmcmd
help2:
	ld	hl,tophlp	; The address of the help message.
	call	prhelp		; .. print help
	jp	kermit
;
; This is the LOG command.
; It logs a session to a file.
;
log:
	ld	a,cmofi		; Parse an input file spec.
	ld	de,Log.FCB
	call	comnd
	 jp	kermt3
	call	cfmcmd
	ld	a,TRUE		; Set the log flags.
	ld	(logflg),a
	ld	(alogfl),a
	ld	hl,buff
	ld	(bufpnt),hl
	call	dmaset
	ld	de,Log.FCB
	call	open		; Try to find file
	jr	nz,..log
new.LOG:
	ld	de,Log.FCB
	call	setfil		; Delete the file if it exists.
				; And create new one with same name
	ld	de,erms17	; If so tell the user
	call	z,prtstr
	jp	kermit
..log:
	ld	de,inms26
	ld	ix,..O.A
	call	get.decision	; Ask for Append/Overwrite
	jr	z,new.LOG
	ld	de,Log.FCB
	call	filsiz		; Get size of LOG file
	ld	hl,(Log.FCB+FCBrno-FCB)
	ld	a,h
	or	l		; Test empty file
	jr	z,new.LOG	; .. yeap
	dec	hl
	ld	(Log.FCB+FCBrno-FCB),hl
	ld	de,Log.FCB
	call	redrnd		; Position last record
	ld	b,bufsiz
	ld	hl,buff
	ld	a,'Z'-'@'
log.src:
	cp	(hl)		; Find EOF
	jr	z,EOF.log
	inc	hl
	djnz	log.src
	ld	de,Log.FCB
	call	redseq		; Advance record
	ld	hl,buff		; .. fix for new location
EOF.log:
	ld	(bufpnt),hl	; Set buffer pointer
	jp	kermit
;
..O.A:
IF	GERMAN
	db	'LA'
ELSE
	db	'OA'
ENDIF;GERMAN
;
; This is the LOGOUT command.
; It tells the remote KERSRV to logout
;
logout:
	call	cfmcmd
	call	logo		; Send the logout packet.
	 jp	kermit		; Go get another command
	jp	kermit		; whether we succeed or not.
logo:
	xor	a
	ld	(numtry),a	; Inititialize count.
	ld	a,'1'		; Reset block check type
	ld	(curchk),a	; To single character
logo1:
	ld	a,(numtry)	; How many times have we tried?
	cp	maxtry		; Too many times?
	jr	c,logo3		; No, try it.
logo2:
	ld	de,erms19	; Say we couldn't do it.
	call	prtstr
	ret			; Finished.
logo3:
	inc	a		; Increment the number of tries.
	ld	(numtry),a
	xor	a
	ld	(argblk),a	; Make it packet number zero.
	ld	a,1
	ld	(argblk+1),a	; One piece of data.
	ld	hl,data
	ld	(hl),'L'	; Logout the remote host.
	ld	a,'G'		; Generic command packet.
	call	spack
	 jp	logo2		; Tell the user and die.
	call	rpack		; Get an acknowledgement
	 jp	logo1		; Go try again.
	cp	'Y'		; ACK?
	jp	z,rskp		; Yes, we are done.
	cp	'E'		; Is it an error packet?
	jr	nz,logo1	; Try sending the packet again.
	call	error1		; Print the error message.
	ret			; All done.
;
; This is the REMOTE command
;
remote:
	ld	de,remtab	; remote commands table
	ld	hl,remhlp	; remote help table
	call	keycmd		; get result
	ex	de,hl
	jp	(hl)		; and do it
remtab:
	db	3		; three commands so far
	db	3,'DIR',eot
	dw	remdir		; remote dir command
	db	6,'FINISH',eot
	dw	finish		; same as finish
	db	6,'LOGOUT',eot
	dw	logout		; same as logout
remhlp:
IF	GERMAN
	db	cr,lf,'DIR    Dateien des Hosts anzeigen'
	db	cr,lf,'FINISH Server beenden'
	db	cr,lf,'LOGOUT Server beenden und austragen'
ELSE
	db	cr,lf,'DIR    to list a directory on a remote system'
	db	cr,lf,'FINISH to stop a remote server'
	db	cr,lf,'LOGOUT to stop and logout a remote server'
ENDIF;GERMAN
	db	eot
;
; REMOTE DIRECTORY command. Start off with no parameters.
;
; Packets start with an I packet in place of S/R packet.  An X
;	packet is the same as an F (filename) packet except the 'file'
;	is not applicable.  Copy X packet data field to display.  Set
;	options so that no data is written to disk during D packets. 
;	(REMTXT <> 0)
;
; Packets:
;	we	we	comments
;	send	receive
;	I
;		ACK
;	GD		generic command, directory. No params yet.
;		Init
;	ACK
;		X	Dummy header.
;	ACK
;		D	directory listing from remote end
;	ACK		We got it
;	....
;	ACK		last packet received ok
;		Z
;	ACK
;		B
;	ACK		end of dir.
;
remdir:
	call	cfmcmd		; get return
	xor	a
	ld	(numtry),a	; rest retries
	ld	(czseen),a
	ld	(pktnum),a
	ld	hl,0
	ld	(numpkt),hl
	ld	(numrtr),hl	; clear some variables
	ld	a,'1'		; reset block check type
	ld	(curchk),a
	ld	a,'I'		; init state
	ld	(state),a
	call	sinit		; do sendinit with I packet
	ld	a,(state)	; now see if we are in the 'X' state
	cp	'X'
	jr	z,remdi2
	ld	de,erms29	; nope, say error
	jr	remabo
remdi1:
	ld	de,erms28	; yup, say so
remabo:
	call	prtstr
	jp	kermit		; and abort
remdi2:
	call	ClrTop		; clear the screen
	inc	a		; update retries
	ld	(numtry),a
	xor	a
	ld	(argblk),a	; set packet no zero
	ld	a,1		; one byte of data (Will change if params sent)
	ld	(argblk+1),a
	ld	hl,data
	ld	(hl),'D'	; directory requested
	ld	a,'G'		; generic command
	call	spack
	 jp	remdi2		; could not send it
	call	rpack
	 jp	remdi1		; if here, something wrong, so try again
	cp	'E'		; error packet
	jp	z,kermit	; so abort
	cp	'X'		; x packet?
	jr	nz,remd5a	; nope, try something else
	ld	hl,data		; else lets see what we got
	ld	a,(argblk+1)	; length to a
remdi5:
	and	a		; anything left?
	jr	z,remd5a
	ld	e,(hl)		; else get the next bit of data
	dec	a
	inc	hl
	push	af
	push	hl
	ld	a,e
	call	putcon		; send it to the console
	pop	hl
	pop	af
	jr	remdi5		; and do next one
remd5a:
	ld	a,TRUE		; set flag for no file (REMTXT <> 0)
	ld	(remtxt),a
	ld	a,'D'		; data state to follow
	ld	(state),a
	call	ackp		; send an OK to the other side
	jp	read2		; do the same as read a file, but echo
				; to the screen.. Dont close non-open files.
;
; This is the TRANSMIT command.
;
xmit:
	ld	a,cmofi		; Parse an input file spec (non-wild).
	ld	de,FCB		; Give the address for the FCB.
	call	comnd
	 jp	kermit		; Give up on bad parse.
	call	cfmcmd
	call	getfil		; Open file.
	 jp	kermit		; Can't happen?
	cp	TRUE		; Succeed?
	jr	nz,xmit1
	ld	de,erms15
	call	error3		; Display error msg.
	jp	kermit
xmit1:
	ld	de,inms19	; Output start message.
	call	prtstr
	call	escpr		; Print the escape character.
	ld	de,inms20	; Output 2nd part.
	call	prtstr
	call	escpr		; Print the escape character.
	ld	de,inms21	; Print the rest.
	call	prtstr
	ld	a,1		; Start file I/O.
	ld	(fileio),a
	xor	a		; Clear XOFF flag.
	ld	(xofflg),a
	ld	(chrcnt),a	; No chars yet.
xnext:
	call	prtchr		; Receive comm. line & display.
	call	contst		; Anything at console?
	jr	z,xnext		; No.
	ld	a,(eoflag)	; EOF encountered?
	or	a
	jp	nz,xend1	; Yes, finish.
	ld	hl,(bufpnt)	; Get current buffer pointer.
	ld	a,(chrcnt)	; Characters remaining in buffer?
	or	a
	jr	nz,xmit2	; Yes, proceed.
	call	xbuf		; Get next disk record (if any)
	ld	a,(chrcnt)	; Get new character counter
xmit2:
	ld	b,a		; Character counter.
	xor	a		; Reset line buffer counter.
	ld	c,a
	ld	(cmaflg),a	; Reset carriage return flag.
	ld	de,cmdbuf	; Use comnd buffer as line buffer.
xmit30:
	ld	a,(hl)		; Get a character.
	inc	hl
	and	NoMSB		; Mask 7 bits.
	cp	cr		; Carriage return?
	jr	z,xmit32
	cp	subt		; CTRL-Z (substitute)?
	jr	z,xmit38
	cp	lf		; Line feed?
	jr	z,xmit39
	ld	(de),a		; Save to buffer.
	inc	de
	ld	a,(cmaflg)	; Carriage return seen?
	or	a
	jr	nz,xmit31	; Yes, don't count this character.
	inc	c		; Count it.
xmit31:
				; Any characters in buffer?
	djnz	xmit30		; Yes, loop.
	call	xbuf		; No, get next record.
	ld	a,(chrcnt)	; Reset working counter.
	ld	b,a
	jr	xmit30
xmit32:
	ld	(cmaflg),a	; Mark return seen.
	jr	xmit31		; And continue.
xmit38:
	ld	(eoflag),a	; Mark EOF for next line.
xmit39:
	ld	(bufpnt),hl	; Save next buffer pointer.
	dec	b		; Account for the terminating character.
	ld	a,b		; Save count of remaining characters.
	ld	(chrcnt),a
	ld	a,c		; Save line length.
	ld	(filcnt),a
rexmit:
	ld	a,(filcnt)	; Set up line length.
	ld	(cmccnt),a
	ld	hl,cmdbuf	; Set up line buffer pointer.
	ld	(cmcptr),hl
xmit40:
	call	prtchr		; Receive comm. line & display.
	ld	a,(xofflg)	; XOFF received?
	or	a
	jr	nz,xmit40	; Yes, wait for XON?
	ld	a,(cmccnt)	; Any characters left?
	or	a
	jr	z,xmit49	; No, next state.
	dec	a		; Count this character.
	ld	(cmccnt),a
	ld	hl,(cmcptr)	; Get it.
	ld	a,(hl)
	inc	hl		; Bump to next character.
	ld	(cmcptr),hl
	call	setpar		; Set parity (if any).
	ld	e,a		; Restore character.
	call	prtout		; Output it to the comm. line.
	ld	a,(ecoflg)	; Local echo?
	or	a
	jr	z,xmit40	; No, continue.
	ld	a,e		; Get the character.
	and	NoMSB		; Mask out parity.
	call	condir		; Display on console.
	jr	xmit40		; Continue.
xmit49:
	xor	a		; Reset last character seen.
	ld	(lstchr),a
xmit50:
	call	prtchr		; Receive comm. line & display.
	call	conchr		; Read keyboard & send.
	jr	xendc		; CLOSE connection.
	ld	a,(lstchr)	; Check last keyboard character.
	cp	cr		; Carriage return?
	jp	z,xnext		; Yes, prepare to send next line.
	jr	xmit50		; Continue, until carriage return.
xbuf:
	ld	a,(eoflag)	; Eof encountered?
	or	a
	jr	nz,xend		; Yes, return to normal CONNECT state.
	push	bc		; Save registers.
	push	de
	ld	de,FCB
	call	redseq		; Read a record.
	pop	de		; Restore registers.
	pop	bc
	ld	a,bufsiz	; Set up counter.
	ld	(chrcnt),a
	ld	hl,buff		; Set the buffer pointer.
	ld	(bufpnt),hl
	ld	a,(filerc)	; Get the file record count.
	sub	1		; Decrement it.
	ld	(filerc),a
	ret	nz		; If not the last packet then return.
	ld	a,(fileex)	; Any more left?
	cp	0
	jr	z,xbuf1
	sub	1
	ld	(fileex),a
	ld	a,bufsiz	; Get a record length.
	ld	(filerc),a	; Start the record count over.
	ret
xbuf1:
	ld	a,TRUE
	ld	(eoflag),a	; Set the EOF flag.
	ret
xend:
	pop	bc		; Remove return address
				; (a non-local goto)
xend1:
	call	xmtoff		; Close file, etc.
	ld	de,inms22	; Tell we're doen with transmission
	jp	telnt1		; Branch to CONNECT command.
xendc:
	call	xmtoff		; Close file, etc.
	jp	kermit		; Back to command loop.
xmtoff:
	ld	de,FCB
	call	close		; Close file.
	xor	a		; Terminate file I/O.
	ld	(fileio),a
	ret
;
; PRINT a CP/M file
;
print:
	ld	a,TRUE
	jr	typprn
;
; TYPE a CP/M file
;
type:
	ld	a,FALSE
typprn:
	ld	(prflag),a
	ld	a,cmifi		; Parse a file-spec
	ld	de,FCB		; Into FCB
	call	comnd
	 jp	kermit		; Bad parse
	ld	a,FALSE
	ld	(mfflg1),a
	call	mfname		; Check if valid
				; Carry if FILE not found
	ld	de,erms15
	jp	c,..reent	; Found at least one
	xor	a
	ld	(pagecur),a
	ld	de,FCB
	call	open		; Open the file
	call	dmaset		; Set buffer
	ld	de,scb.er
	call	scb		; Get current error mode
	push	af
	ld	de,-1
	call	conmod		; Get state of console
	push	hl
	ld	a,0ffh
	call	errmod		; Set error mode
	ld	de,1
	call	conmod		; Set CTRL^C break only
	ld	de,scb.pg
	call	scb		; Check page mode
	cp	0
	jr	nz,..pag.OK
	ld	(pagemode),a	; Save current mode
	ld	de,scb.cp
	call	scb		; Get current page length
	dec	a
	ld	(pagelen),a	; .. save it
..pag.OK:
	call	crlf
	call	.type		; Now type
	pop	de
	call	conmod		; Reset console mode
	pop	af
	call	errmod		; .. and error mode
	ld	a,FALSE
	ld	(prflag),a
	jp	kermit		; Bye
;
; Print VERSION of KERMIT
;
version:
	call	crlf
	ld	de,versio
	call	prtstr		; .. print version
	jp	kermit

