;
; ++++++++++++++++++++++++
; +++ KERMIT utilities +++
; ++++++++++++++++++++++++
;
; Print file name
;
prFN:
	ld	de,scrfln	; Position cursor
	call	prtstr
	call	ClrEol		; .. clear rest of line
	ld	de,data		; Print the file name
	call	prtstr
	ret
;
; Blank FCB name
;
blkfcb:
	xor	a		; Initialize counter.
blklop:
	ld	(hl),' '	; Blank the FCB.
	inc	hl
	inc	a
	cp	12		; Twelve?
	jr	c,blklop
	ret
;
; Captalize character
;
uppcas:
	cp	'a'		; Is it less than a?
	ret	m		; If so don't capitalize it.
	cp	'z'+1		; Is it more than z?
	ret	p		; If so don't capitalize it.
	and	01011111b	; Capitalize it.
	ret
;
; Print the escape char.
;
escpr:
	ld	a,(escchr)	; Get the escape char.
	cp	' '		; Is it a control char?
	jr	nc,escpr2
	ld	de,inms10	; Output Control-.
	call	prtstr
	ld	a,(escchr)
	or	'@'		; De-controlify.
escpr2:
	call	putcon		; Output the char
	ret
;
; Utility routines
;
; Jumping to this location is like retskp.  It assumes
; the instruction after the call is a jmp addr.
;
rskp:
	pop	hl		; Get the return address.
	inc	hl		; Increment by three.
	inc	hl
	inc	hl
	jp	(hl)
;
; Jumping here is the same as a ret.
;
r:
	ret
;
; This routine used to compare packet-Nr
;
compp:
	ld	a,(pktnum)	; Get the packet Nr.
	ld	b,a
	ld	a,(argblk)
	cp	b
	ret
;
; This routine up's the packet Nr. modulo 64
;
countp:
	inc	a		; Increment packet Nr.
	and	00111111b	; Turn off the two high order bits
	ld	(pktnum),a	; Save modulo 64 of number
	ld	hl,(numpkt)
	inc	hl		; Increment Nr. of packets
	ld	(numpkt),hl
	ret
;
; This routine sends an ACK-packet
;
ackp:
	xor	a
	ld	(numtry),a	; Reset number of retries
	ld	(argblk+1),a	; No data.
				; (The packet number is in argblk)
	ld	a,'Y'		; Acknowledge packet
	call	spack		; Send packet
	 jp	abort
	ret
tryagn:
	inc	a		; Increment it.
	ld	(oldtry),a	; Save the updated number of tries.
	ld	a,(pktnum)	; Get the present packet number.
	dec	a		; Decrement.
	ld	b,a
	ld	a,(argblk)	; Get the packet's number
	cp	b		; Is the packet's number
				; One less than now?
	jp	nz,nak0		; No, NAK it and try again.
	call	updrtr		; Update the number of retries.
	call	ackp
	ret
;
; This routine fetches keyword
;
keycmd:
	ld	a,cmkey
	call	comnd
	 jp	kermt2		; "Unrecognized command"
	ret
;
; This routine fetches confirm
;
cfmcmd:
	ld	a,cmcfm
	call	comnd
	 jp	kermt3		; "Not confirmed"
	ret
;
; This routine prints the number in HL on the screen in decimal.
; Saves all ACs.
;
nout:
	push	af		; Save our ACs.
	push	bc
	push	de
	push	hl
	ld	bc,-10		; Get some useful constants.
	ld	de,-1
nout2:
	add	hl,bc		; Subtract as many 10s as possible.
	inc	de		; Count them.
	jr	c,nout2		; If some left keep going.

	ld	bc,10		; Restore the last 10 we took away.
	add	hl,bc
	ex	de,hl		; Swap the remainder and the quotient.
	ld	a,h		; Get the number of 10s found.
	or	l		; * I think this checks if h equals zero
				; * If so, this should just be a CPI 0
				; * for clarity.
	call	nz,nout		; If non zero, recurse.
	ld	a,e		; Get the remainder.
	add	a,'0'		; Make the number printable.
	call	putcon		; Output the digit.
	pop	hl		; Restore the ACs.
	pop	de
	pop	bc
	pop	af
	ret
;
; This set of routines provides a user oriented way of parsing
; commands.  It is similar to that of the COMND JSYS in TOPS-20.
;
; This routine prints the prompt in DE and specifies the reparse
; address.
;
prompt:
	pop	hl		; Get the return address.
	push	hl		; Put it on the stack again.
	ld	(cmrprs),hl	; Save it as the address to go
				; To on reparse.
				; Get the present stack pointer.
	ld	(cmostp),sp	; Save for later restoral.
	ld	(cmprmp),de	; Save the pointer to the prompt.
	ld	hl,cmdbuf
	ld	(cmcptr),hl	; Initialize the command pointer.
	ld	(cmdptr),hl
	xor	a
	ld	(cmaflg),a	; Zero the flags.
	ld	(cmccnt),a
	ld	a,true		; Try it this way (Daphne.)
	ld	(cmsflg),a
	call	crlf
	ld	de,(cmprmp)	; Print the prompt.
	call	prtstr
	ret
;
; This address is jumped to on reparse.
;
repars:
	ld	sp,(cmostp)	; Get the old stack pointer.
	ld	hl,cmdbuf
	ld	(cmdptr),hl
	ld	a,true		; Try it this way (Daphne.)
	ld	(cmsflg),a
	ld	hl,(cmrprs)	; Get the reparse address.
	jp	(hl)		; Go there.
;
; This address can be jumped to on a parsing error.
;
prserr:
	ld	sp,(cmostp)	; Get the old stack pointer.
	ld	hl,cmdbuf
	ld	(cmcptr),hl	; Initialize the command pointer.
	ld	(cmdptr),hl
	xor	a
	ld	(cmaflg),a	; Zero the flags.
	ld	(cmccnt),a
	ld	a,true		; Try it this way (Daphne.)
	ld	(cmsflg),a
	call	crlf
	ld	de,(cmprmp)	; Get the prompt.
	call	prtstr
;
; * Instead return to before the prompt call.
;
	ld	hl,(cmrprs)
	jp	(hl)
;
; This routine parses the specified function in A.
; Any additional information is in DE and HL.
; Returns +1 on success
;         +4 on failure (assumes a JMP follows the call)
;
comnd:
	ld	(cmstat),a	; Save what we are presently parsing.
	call	cminbf		; Get chars until an action or
				; A erase char.
	cp	cmcfm		; Parse a confirm?
	jp	z,cmcfrm	; Go get one.
	cp	cmkey		; Parse a keyword?
	jp	z,cmkeyw	; Try and get one.
	cp	cmifi		; Parse an input file spec?
	jp	z,cmifil	; Go get one.
	cp	cmifin		; Input file-spec silent?
	jp	z,cmifil	; Do as he wishes
	cp	cmofi		; Output file spec?
	jp	z,cmofil	; Go get one.
	cp	cmtxt		; Parse arbitrary text?
	jp	z,cmtext	; Go do it.
	ld	de,cmer00	; "?Unrecognized COMND call"
	call	prtstr
	ret
;
; This routine parses arbitrary text up to a CR.
; Accepts DE:	address to put text
; Returns in A:	number of chars in text (may be 0)
;	    DE:	updated pointer
;
cmtext:
	ex	de,hl		; Put the pointer to the dest in HL.
	ld	(cmptab),hl	; Save the pointer.
	ld	b,0		; Init the char count
cmtxt1:
	call	cmgtch		; Get a char.
	or	a		; Terminator?
	jp	p,cmtxt5	; No, put in user space.
	and	NoMSB		; Turn off minus bit.
	cp	esc		; An escape?
	jr	nz,cmtxt2	; No.
	ld	a,bell		; Get a bell.
	call	putcon
	xor	a
	ld	(cmaflg),a	; Turn off the action flag.
	ld	hl,(cmcptr)	; Move the pointer to before the escape.
	dec	hl
	ld	(cmcptr),hl
	ld	(cmdptr),hl
	ld	hl,cmccnt	; Get the char count.
	dec	(hl)		; Decrement it by one.
	jr	cmtxt1		; Try again.
cmtxt2:
	cp	'?'		; Is it a question mark?
	jr	z,cmtxt3	; If so put it in the text.
	cp	ff		; Is it a formfeed?
	call	z,ClrTop	; If so blank the screen.
	ld	a,b		; Return the count.
	ld	hl,(cmptab)	; Return updated pointer in HL.
	ex	de,hl
	jp	rskp		; Return success.
cmtxt3:
	ld	hl,cmaflg	; Point to the action flag.
	ld	(hl),0		; Set it to zero.
cmtxt5:
	inc	b		; Increment the count.
	ld	hl,(cmptab)	; Get the pointer.
	ld	(hl),a		; Put the char in the array.
	inc	hl
	ld	(cmptab),hl	; Save the updated pointer.
	jp	cmtxt1		; Get another char.
;
; This routine gets a confirm.
;
cmcfrm:
	call	cmgtch		; Get a char.
	or	a		; Is it negative (a terminator;a space
				; Or a tab will not be returned here as
				; They will be seen as leading
				; White space.)
	ret	p		; If not, return failure.
	and	NoMSB		; Turn off the minus bit.
	cp	esc		; Is it an escape?
	jr	nz,cmcfr2
	ld	a,bell		; Get a bell.
	call	putcon
	xor	a
	ld	(cmaflg),a	; Turn off the action flag.
	ld	hl,(cmcptr)	; Move the pointer to before the escape.
	dec	hl
	ld	(cmcptr),hl
	ld	(cmdptr),hl
	ld	hl,cmccnt	; Get the char count.
	dec	(hl)		; Decrement it by one.
	jp	cmcfrm		; Try again.
cmcfr2:
	cp	'?'		; Curious?
	jr	nz,cmcfr3
	ld	de,cmin00	; Print something useful.
	call	prtstr
	call	crlf		; Print a crlf.
	ld	de,(cmprmp)	; Reprint the prompt.
	call	prtstr
	ld	hl,(cmdptr)	; Get the pointer into the buffer.
	ld	(hl),eot	; Put sign there for printing.
	ld	hl,(cmcptr)
	dec	hl		; Decrement and save the buffer pointer.
	ld	(cmcptr),hl
	ld	de,cmdbuf
	call	prtstr
	xor	a		; Turn off the action flag.
	ld	(cmaflg),a
	jp	repars		; Reparse everything.
cmcfr3:
	cp	ff		; Is it a form feed?
	call	z,ClrTop	; If so blank the screen.
	jp	rskp
;
; This routine parses a keyword from the table pointed
; to in DE.  The format of the table is as follows:
;
; addr:	db	n		;Where n is the # of entries
;				;in the table.
;	db	m		;M is the size of the keyword.
;	db	'string$'	;Where string is the keyword.
;	db	a,b		;Where a & b are pieces of data
;				;to be returned. (Must be two of them)
;
; The keywords MUST be in alphabetical order.
; NOTR:	The data value 'a' is returned in registers A and E.
;	The data value 'b' is returned in register D.
;	This allows the two data bytes to be stored as:
;		dw	xxx
;	and result in a correctly formatted 16-bit value
;	in register pair DE
;
cmkeyw:
	ld	(cmhlp),hl	; Save the help.
	ex	de,hl
	ld	(cmptab),hl	; Save the beginning of keyword
				; Tab for '?'.
	ld	b,(hl)		; Get the number of entries in the table
	inc	hl
	ld	(cmkptr),hl
	ld	hl,(cmdptr)	; Save the command pointer.
	ld	(cmsptr),hl
cmkey2:
	ld	a,b		; Get the number of entries left.
	or	a		; Any left?
	ret	z		; If not we failed.
	ld	hl,(cmkptr)
	ld	e,(hl)		; Get the length of the keyword.
	inc	hl
cmkey3:
	dec	e		; Decrement the number of chars left.
	ld	a,e
	cp	-1		; Have we passed the end?
	jp	m,cmkey5	; If so go to the next.
	call	cmgtch		; Get a char.
	or	a		; Is it a terminater?
	jp	p,cmkey4	; If positive, it is not.
	and	7fh		; Turn off the minus bit.
	cp	'?'
	jp	nz,cmky31
	xor	a
	ld	(cmaflg),a	; Turn off the action flag.
	ld	hl,cmccnt	; Decrement the char count.
	dec	(hl)
;
; * Must go through the keyword table and print them.
;
	ld	hl,(cmhlp)	; For now print the help text.
	ld	a,(hl)		; Test special code
	cp	eot
	jr	nz,nospchlp
	call	prhelp
	jr	keygo
nospchlp:
	ex	de,hl
	call	prtstr
keygo:
	call	crlf		; Print a crlf.
	ld	de,(cmprmp)	; Reprint the prompt.
	call	prtstr
	ld	hl,(cmdptr)	; Get the pointer into the buffer.
	ld	(hl),eot	; Put sign there for printing.
	ld	hl,(cmcptr)
	dec	hl		; Decrement and save the buffer pointer.
	ld	(cmcptr),hl
	ld	de,cmdbuf
	call	prtstr
	jp	repars		; Reparse everything.
cmky31:
	cp	esc		; Is it an escape?
	jp	nz,cmky35
	xor	a
	ld	(cmaflg),a	; Turn off the action flag.
	push	de
	push	bc
	push	hl
	call	cmambg
	 jp	cmky32		; Not ambiguous.
	ld	a,bell
	call	putcon		; Ring the bell.
	ld	hl,(cmcptr)	; Move the pointer to before the escape.
	dec	hl
	ld	(cmcptr),hl
	ld	(cmdptr),hl
	ld	hl,cmccnt	; Get the char count.
	dec	(hl)		; Decrement it by one.
	pop	hl
	pop	bc
	pop	de
	inc	e		; Increment the left to parse char count
	jp	cmkey3
cmky32:
	ld	de,(cmcptr)	; Pointer into buffer.
	dec	de		; Backup to the escape.
	pop	hl
	push	hl
cmky33:
	ld	a,(hl)		; Get the next char.
	cp	eot		; Finished?
	jr	z,cmky34
	inc	hl
	ld	(de),a		; Move it into the buffer.
	inc	de
	ld	a,(cmccnt)	; Increment the char count.
	inc	a
	ld	(cmccnt),a
	jr	cmky33
cmky34:
	ld	a,(cmccnt)	; Get the character count.
	inc	a		; Increment and save it.
	ld	(cmccnt),a
	ex	de,hl		; Put the command buffer pointer in HL.
	ld	a,' '		; Get a blank.
	ld	(hl),a		; Put it in the command buffer.
	inc	hl		; Increment the pointer
	ld	(cmcptr),hl	; Save the updated pointer.
	ld	(cmdptr),hl
	pop	de
	push	de
	call	prtstr		; Print the rest of the keyword.
	ld	a,' '
	call	putcon		; Print a blank.
	pop	hl
	pop	bc
	pop	de
	jr	cmky37
cmky35:
	push	hl
	push	de
	call	cmambg
	 jp	cmky36
	ld	de,cmer01
	call	prtstr		; Say its ambiguous.
	jp	prserr		; Give up.
cmky36:
	pop	de
	pop	hl
cmky37:
	inc	e		; Add one incase it is negative.
	ld	d,0
	add	hl,de		; Increment past the keyword.
	inc	hl		; Past the $.
	ld	e,(hl)		; Get the data.
	inc	hl
	ld	d,(hl)
	ld	a,e
	jp	rskp
cmkey4:
	call	uppcas		; Capitalize
	ld	d,(hl)		; Get the next char of the keyword.
	inc	hl
	cp	d		; Match?
	jp	z,cmkey3	; If so get the next letter.
cmkey5:
	ld	d,0
	ld	a,e		; Get the number of chars left.
	or	a		; Is it negative?
	jp	p,cmky51
	ld	d,-1		; If so, sign extend.
cmky51:
	add	hl,de		; Increment past the keyword.
	ld	de,3		; Plus the $ and data.
	add	hl,de
	ld	(cmkptr),hl
	dec	b		; Decrement the number of entries left.
	ld	hl,(cmsptr)	; Get the old	cmdptr.
	ld	(cmdptr),hl	; Restore it.
;
;* check so we don't pass it.
;
	jp	cmkey2		; Go check the next keyword.
cmambg:
	dec	b		; Decrement the number of entries left.
	ret	m		; If none left then it is not ambiguous.
	inc	e		; This is off by one;adjust.
	ld	c,e		; Save the char count.
	ld	a,e
	or	a		; Any chars left?
	ret	z		; No, it can't be ambiguous.
	ld	d,0
	add	hl,de		; Increment past the keyword.
	ld	e,3		; Plus the $ and data.
	add	hl,de
	ld	b,(hl)		; Get the length of the keyword.
	inc	hl
	ex	de,hl
	ld	hl,(cmkptr)	; Get pointer to keyword entry.
	ld	a,(hl)		; Get the length of the keyword.
	sub	c		; Subtract how many left.
	ld	c,a		; Save the count.
	cp	b
	jr	z,cmamb0
	ret	p		; If larger than the new word
				; Then not amb.
cmamb0:
	ld	hl,(cmsptr)	; Get the pointer to what parsed.
cmamb1:
	dec	c		; Decrement the count.
	jp	m,rskp		; If we are done then it is ambiguous.
	ex	de,hl		; Exchange the pointers.
	ld	b,(hl)		; Get the next char of the keyword
	inc	hl
	ex	de,hl		; Exchange the pointers.
	ld	a,(hl)		; Get the next parsed char.
	inc	hl
	call	uppcas		; Capitalize
	cp	b		; Are they equal?
	ret	nz		; If not then its not ambiguous.
	jr	cmamb1		; Check the next char.
cmofil:
	ld	a,0		; Don't allow wildcards on output.
cmifil:
	ld	(cmfwld),a	; Set wildcard flag
	ex	de,hl		; Get the fcb address.
	ld	(cmfcb),hl	; Save it.
	ld	e,0		; Initialize char count.
	ld	(hl),0		; Set the drive to default to current.
	inc	hl
	ld	(cmfcb2),hl
	call	blkfcb		; Blank the FCB
cmifi1:
	call	cmgtch		; Get another char.
	or	a		; Is it an action character.
	jp	p,cmifi2
	and	NoMSB		; Turn off the action bit.
	cp	'?'		; A question mark?
	jr	nz,cmif12
	ld	a,(cmfwld)	; [] Wildcards allowed?
	or	a		; []
	jp	z,cmif11	; [] Complain if not
	ld	hl,(cmdptr)	; [] This is actually what is decremented
	inc	hl		; []
	ld	(cmdptr),hl	; []
	ld	a,(cmcptr)	; [] Get LSB of real input pointer
	cp	l		; [] Is this the last character input?
	jr	nz,cmif1a	; [] No, let action flag in tact
	xor	a		; []
	ld	(cmaflg),a	; [] Yes, blank the action flag.
cmif1a:				; []
	ld	a,'?'		; []
	.comment		|
	ld	hl,cmaflg	; Blank the action flag.
	ld	(hl),0
	ld	hl,(cmcptr)	; Increment the buffer pointer.
	inc	hl		; (Gets decremented in 'cmgtch')
	ld	(cmcptr),hl
	ld	hl,(cmdptr)	; This is actually what is decremented
	inc	hl
	ld	(cmdptr),hl
				|
	jp	cmifi8		; Treat like any other character.
cmif12:
	cp	esc		; An escape?
	jp	nz,cmif13
;
; Try to recognize file-spec a'la TOPS-20
;
	xor	a
	ld	(cmaflg),a	; Turn off the action flag.
	ld	hl,(cmcptr)	; Move the pointer to before the escape.
	dec	hl
	ld	(cmcptr),hl
	ld	(cmdptr),hl
	ld	hl,cmccnt	; Get the char count.
	dec	(hl)		; Decrement it by one.
	ld	a,e		; Save character count up to now.
	ld	(temp1),a
	cp	9		; Past '.'?
	jr	c,cmfrec	; No.
	dec	a		; Yes, don't count point.
cmfrec:
	ld	hl,(cmfcb2)	; Fill the rest with CP/M wildcards.
cmfrc1:
	cp	11		; Done?
	jr	nc,cmfrc2	; Yes.
	ld	(hl),'?'
	inc	hl
	inc	a
	jr	cmfrc1
cmfrc2:
	call	srcfxx		; Find first matching file?
	cp	cpmerr
	jp	z,cmfrc9	; No, lose.
	ld	hl,fcbblk	; Copy first file spec.
	call	fspcop
	ld	hl,fcbblk+16	; Get another copy
				; (in case not ambiguous).
	call	fspcop
	call	srcnxt		; More matching specs?
	cp	cpmerr
	jp	z,cmfrc3	; Only one.
	ld	hl,fcbblk+16	; Copy second file spec.
	call	fspcop
cmfrc3:
	ld	de,fcbblk	; Start comparing file names.
	ld	hl,fcbblk+16
	ld	a,(temp1)	; Bypass characters typed.
	cp	9		; Past '.'?
	jr	c,cmfrc4	; No.
	dec	a		; Yes, don't count point.
cmfrc4:
	ld	c,0
cmfrl1:
	cp	c		; Bypassed?
	jr	z,cmfrl2	; Yes.
	inc	de
	inc	hl
	inc	c
	jr	cmfrl1		; Repeat.
cmfrl2:
	ld	a,c		; Get file name characters processed.
	cp	11		; All done?
	jr	z,cmfrc5	; Yes.
	cp	8		; End of file name?
	jr	nz,cmfrl3	; No.
	ld	a,(temp1)	; Exactly at point?
	cp	9
	jr	z,cmfrl3	; Yes, don't output a second point.
	ld	a,'.'		; Output separator.
	call	cmfput
cmfrl3:
	ld	a,(de)		; Get a character from first file spec.
	inc	de
	ld	b,(hl)		; Get from second file spec.
	inc	hl
	cp	b		; Compare.
	jr	nz,cmfrc5	; Ambiguous.
	inc	c		; Same, count.
	cp	' '		; Blank?
	call	nz,cmfput	; Put character into buffer.
	jr	cmfrl2		; Repeat.
cmfrc5:
	ld	a,c		; Get count of characters processed.
	ld	(temp1),a	; Save it.
	ld	a,eot		; Get terminator.
	call	cmfput		; Put it into buffer.
	ld	de,(cmdptr)	; Output recognized characters.
	call	prtstr
	ld	hl,(cmcptr)	; Remove terminator from buffer.
	dec	hl
	ld	(cmcptr),hl
	ld	hl,cmccnt
	dec	(hl)
	ld	a,(temp1)	; Characters processed.
	cp	11		; Complete file name.
	jp	z,repars	; Yes, don't beep.
cmfrc9:
	ld	a,bell
	call	putcon		; Ring the bell.
	jp	repars
;
; Continue file spec parsing.
;
cmif13:
	ld	a,e		; It must be a terminator.
	or	a		; Test the length of the file name.
	jp	z,cmifi9	; If zero complain.
	cp	13
	jp	p,cmifi9	; If too long complain.
	jp	rskp		; Otherwise we have succeeded.
cmifi2:
	cp	'.'
	jr	nz,cmifi3
	inc	e
	ld	a,e
	cp	1		; Any chars yet?
	jp	z,cmifi9	; No, give error.
	cp	10		; Tenth char?
	jp	p,cmifi9	; Past it, give an error.
	ld	c,9
	ld	b,0
	ld	hl,(cmfcb)
	add	hl,bc		; Point to file type field.
	ld	(cmfcb2),hl
	ld	e,9		; Say we've gotten nine.
	jp	cmifi1		; Get the next char.
cmifi3:
	cp	':'
	jr	nz,cmifi4
	inc	e
	ld	a,e
	cp	2		; Is it in the right place for a drive?
	jp	nz,cmifi9	; If not, complain.
	ld	hl,(cmfcb2)
	dec	hl		; Point to previous character.
	ld	a,(hl)		; Get the drive name.
	sub	'@'		; Get the drive number.
	ld	(cmfcb2),hl	; Save pointer to beginning
				; Of name field.
	ld	(hl),' '	; [] Restore space in name
	dec	hl		; Point to drive number.
	ld	(hl),a		; Put it in the fcb.
	ld	e,0		; Start character count over.
	jp	cmifi1
cmifi4:
	cp	'*'
	jr	nz,cmifi7
	ld	a,(cmfwld)	; Wildcards allowed?
	or	a
	jp	z,cmif11	; No,complain
	ld	a,e
	cp	8		; Is this in the name or type field?
	jr	z,cmifi9	; If its where the dot should be give up
	jr	nc,cmifi5	; Type.
	ld	b,8		; Eight chars.
	jr	cmifi6
cmifi5:
	ld	b,12		; Three chars.
cmifi6:
	ld	hl,(cmfcb2)	; Get a pointer into the FCB.
	ld	a,'?'
	ld	(hl),a		; Put a question mark in.
	inc	hl
	ld	(cmfcb2),hl
	inc	e
	ld	a,e
	cp	b
	jr	c,cmifi6	; Go fill in another.
	jp	cmifi1		; Get the next char.
cmifi7::
	cp	' '+1		; [] Control char or space
	jp	c,cmifi9	; [] Yes, illegal
;;	cp	'%'		; [] Check invalid ones
;;	jp	z,cmifi9	; []
	cp	'('		; []
	jp	z,cmifi9	; []
	cp	')'		; []
	jp	z,cmifi9	; []
	cp	','		; []
	jp	z,cmifi9	; []
	cp	'/'		; []
	jp	z,cmifi9	; []
	cp	'9'+1		; []
	jp	c,cmifi8	; []
	cp	'A'-1		; []
	jp	c,cmifi9	; []
	cp	'Z'+1		; []
	jp	c,cmifi8	; []
	cp	']'+1		; []
	jp	c,cmifi9	; []
;;	cp	'_'		; []
;;	jp	z,cmifi9	; []
	cp	'|'		; []
	jp	z,cmifi9	; []
	cp	'a'		; [] Test lower case
	jr	c,cmifi8	; []
	cp	'z'+1		; []
	jr	nc,cmifi8	; []
	call	uppcas		; [] Capitalize
	.comment		|
	cp	'0'
	jr	c,cmifi9
	cp	'z'+1
	jr	nc,cmifi9
	call	uppcas		; Capitalize
				|
cmifi8:
	ld	hl,(cmfcb2)	; Get the pointer into the FCB.
	ld	(hl),a		; Put the char there.
	inc	hl
	ld	(cmfcb2),hl
	inc	e
	jp	cmifi1
cmifi9:
	ld	a,(cmstat)
	cp	cmifin		; "silent"?
	jp	z,r		; Yes,let him go w/o check
	ld	de,cmer02
cmif10:
	call	prtstr
	ret
cmif11:
	ld	de,cmer03	; Complain about wildcards.
	jr	cmif10
fspcop:
	push	af		; Save A.
	ld	de,buff		; Get the right offset in the buffer.
	rlca
	rlca
	rlca
	rlca
	rlca
	add	a,e
	add	a,1		; Bypass drive spec.
	ld	e,a
	ld	b,11		; Copy file name.
fspcp1:
	ld	a,(de)
	inc	de
	ld	(hl),a
	inc	hl
	djnz	fspcp1
	pop	af		; Restore A.
	ret
cmfput:
	push	hl		; Save H.
	ld	hl,(cmcptr)	; Get buffer pointer.
	ld	(hl),a		; Store in buffer.
	inc	hl
	ld	(cmcptr),hl
	ld	hl,cmccnt	; Count it.
	inc	(hl)
	pop	hl		; Restore H.
	ret
cminbf:
	push	af
	push	de
	push	hl
	ld	a,(cmaflg)	; Is the action char flag set?
	or	a
	jp	nz,cminb9	; If so get no more chars.
cminb1:
	ld	hl,cmccnt	; Increment the char count.
	inc	(hl)
	call	getcon		; Get a char.
	ld	hl,(cmcptr)	; Get the pointer into the buffer.
	ld	(hl),a		; Put it in the buffer.
	inc	hl
	ld	(cmcptr),hl
	cp	'U'-'@'		; Is it a ^U?
	jr	z,cmnb12	; Yes.
	cp	'X'-'@'		; Is it a ^X?
	jr	nz,cminb2
cmnb12:
	call	DelLin		; Delete the line.
	ld	de,(cmprmp)	; Print the prompt.
	call	prtstr
	ld	hl,cmdbuf
	ld	(cmcptr),hl	; Reset the point to the start.
	ld	hl,cmccnt	; Zero the count.
	ld	(hl),0
	jp	repars		; Go start over.
cminb2:
	cp	bs		; Or backspace?
	jr	z,cminb3
	cp	'A'-'@'		; Left arrow?
	jr	nz,cminb33
	ld	a,bs
	call	putcon		; Move left
	jr	cminb3		; Delete from buffer
cminb33:
	cp	del		; Delete?
	jr	nz,cminb4
	ld	de,delstr	; Print the delete string.
	call	prtstr
cminb3:
	ld	a,(cmccnt)	; Decrement the char count by two.
	dec	a
	dec	a
	or	a		; Have we gone too far?
	jp	p,cmnb32	; If not proceed.
	ld	a,bell		; Ring the bell.
	call	putcon
	jp	cmnb12		; Go reprint prompt and reparse.
cmnb32:
	ld	(cmccnt),a	; Save the new char count.
	ld	de,clrspc	; Erase the character.
	call	prtstr
	ld	hl,(cmcptr)	; Get the pointer into the buffer.
	dec	hl		; Back up in the buffer.
	dec	hl
	ld	(cmcptr),hl
	jp	repars		; Go reparse everything.
cminb4:
	cp	'?'		; Is it a question mark.
	jr	z,cminb6
	cp	esc		; Is it an escape?
	jr	z,cminb6
	cp	cr		; Is it a carriage return?
	jr	z,cminb5
	cp	lf		; Is it a line feed?
	jr	z,cminb5
	cp	ff		; Is it a formfeed?
	jr	nz,cminb7
	call	ClrTop
cminb5:
	ld	a,(cmccnt)	; Have we parsed any chars yet?
	cp	1
	jp	z,prserr	; If not, just start over.
cminb6:
	ld	a,true		; Set the action flag.
	ld	(cmaflg),a
cminb9:
	pop	hl
	pop	de
	pop	af
	ret
cminb7:
	cp	' '
	jp	z,cminb1	; Get another char.
	cp	tab
	jp	z,cminb1	; Get another char.
	jp	cminb1		; Get another.
cmgtch:
	push	hl
	push	bc
cmgtc1:
	ld	a,(cmaflg)
	or	a		; Is it set.
	call	z,cminbf	; If the action char flag is
				; Not set get more.
	ld	hl,(cmdptr)	; Get a pointer into the buffer.
	ld	a,(hl)		; Get the next char.
	inc	hl
	ld	(cmdptr),hl
	cp	' '		; Is it a space?
	jr	z,cmgtc2
	cp	tab		; Or a tab?
	jr	nz,cmgtc3
cmgtc2:
	ld	a,(cmsflg)	; Get the space flag.
	or	a		; Was the last char a space?
	jr	nz,cmgtc1	; Yes, get another char.
	ld	a,true		; Set the space flag.
	ld	(cmsflg),a
	ld	a,' '
	pop	bc
	pop	hl
	jr	cmgtc5
cmgtc3:
	push	af
	xor	a
	ld	(cmsflg),a	; Zero the space flag.
	pop	af
	pop	bc
	pop	hl
	cp	esc
	jr	z,cmgtc5
	cp	'?'		; Is the user curious?
	jr	z,cmgtc4
	cp	cr
	jr	z,cmgtc4
	cp	lf
	jr	z,cmgtc4
	cp	ff
	ret	nz		; Not an action char, just return.
cmgtc4:
	push	hl
	ld	hl,(cmdptr)
	dec	hl
	ld	(cmdptr),hl
	pop	hl
cmgtc5:
	or	MSB		; Make the char negative to indicate it is
	ret			; A terminator.
;
; Tell condition and get decision
; ENTRY	Reg DE holds string to be printed
;	Reg IX points to two character decision
; EXIT	Zero set on first character match
;
get.decision:
	ld	a,(ix+0)	; Get conditions
	ld	(inms30+2),a
	ld	a,(ix+1)
	ld	(inms30+4),a
	call	prtstr		; Tell condition
	ld	de,inms30
	call	prtstr
dec.wt:
	call	contst		; Get character
	call	uppcas		; .. as UPPERCASE
	cp	(ix+0)		; Test mode
	jr	z,dec.OK
	cp	(ix+1)
	jr	nz,dec.wt
dec.OK:
	push	af
	call	condir		; Echo character
	call	crlf
	pop	af
	cp	(ix+0)		; Set condition
	ret
;
; Print window size
; ENTRY	Reg DE holds string to define window
;
pr.window:
	ld	b,6
pr..w:
	push	bc
	push	de
	ld	a,(de)
	call	condir		; Print the direct way
	pop	de
	pop	bc
	inc	de
	djnz	pr..w
	ld	a,cr
	call	putcon		; Reset BDOS
	ret
;
; Print tab on console
;
prtab:
	ld	a,tab
	call	putcon		; .. write
	ret
;
; Print version in invers video
;
prvers:
	call	prtab		; Justify a bit
	call	prtab
	call	prtab
	call	Invers		; Invers video
	ld	de,versio
	call	prtstr		; .. print version
	call	Normal		; .. normal video
;
; Give new line to console
;
crlf:
	ld	de,$CRLF
	call	prtstr		; .. give it
	ret
;
; Print help
; ENTRY	Reg HL points to two address block
;	First byte indicates mode for scanner
;
prhelp:
	inc	hl		; Skip indicator
	push	hl
	call	crlf		; New line
	call	Under		; Set underline
	pop	hl
	call	prind		; Print from reg
	push	hl
	call	Nunder		; No underline
	pop	hl
;
; Print message from pointer
; ENTRY	Reg HL points to text
; EXIT	Reg HL updated by +2
;
prind:
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	inc	hl
	push	hl
	call	prtstr
	pop	hl
	ret

