NEW
  AUTO 3,1
*--------------------------------------
CL.PrintPrompt	>PUSHW L.MSG.PROMPT
				>PUSHBI 0
				>SYSCALL PrintF
				rts
*--------------------------------------
CL.READN0A		tax
CL.READN0X		>PUSHW ZPCLBuf
				>PUSHW L.FMT.Byte
				txa
				>PUSHA
				>PUSHBI 1
				>SYSCALL SPrintF
				bcs .9

				dec CL.bReady
*				clc
.9				rts
*--------------------------------------
CL.CHARIN		tax

				bit CL.bEscMode			\e ?
				bpl .4

				cpx #'['
				beq .8					\e[

				stz CL.bEscMode
				txa

				ldx #EscChars.Cnt-1

.1				cmp EscChars,x
				beq .2

				dex
				bpl .1

.8				clc
				rts						invalid \e[ sequence

.2				lda CL.MaxCnt
				bne .3

				lda EscChars.Remap,x
				bra CL.READN0A

.3				txa
				asl
				tax
				jmp (J.ESC,x)
*--------------------------------------
.4				cpx #C.ESC
				bne .5

				lda #$ff
				sta CL.bEscMode

				clc
				rts

.5				lda CL.MaxCnt
				beq CL.READN0X

				cpx #C.SPACE
				bcc CL.CHARIN.CTRL

				cpx #C.DEL
				bne CL.Insert
*--------------------------------------
* Erase char BEFORE cursor
*--------------------------------------
				lda CL.Len
				beq .8

				lda CL.Ptr
				beq .8

				jsr CL.BS.OUT

				dec CL.Ptr
				jmp CL.DEL
*--------------------------------------
CL.Insert		ldy CL.Len
				cpy CL.MaxCnt
				beq .8					Buffer full, discard...

				iny
				sty CL.Len

.1				dey
				lda (ZPCLBuf),y			Move from Ptr To end of buffer forward...
				iny
				sta (ZPCLBuf),y
				dey
				cpy CL.Ptr
				bne .1

				txa
				sta (ZPCLBuf),y

				>SYSCALL PutChar
				bcs .9

				inc CL.Ptr

				jsr CL.PrintEOL
				bcs .9

				lda CL.MaxCnt
				dec						MaxCnt = 1, don't wait for CR
				bne .8

				dec CL.bReady

.8				clc

.9				rts
*--------------------------------------
CL.CHARIN.CTRL	cpx #C.CR
				beq .70

				cpx #C.EOF
				beq .71

				cpx #3					Ctrl-C
				beq CL.CLR

				cpx #C.TAB
				bne .1

				jmp HIS.Select

.1				cpx #26					Ctrl-Z
				bne .8

				jmp CL.SUPPR

.70				jsr CheckLFAfterCR
				bcs .9

.71				dec CL.bReady

.8				clc
.9				rts
*--------------------------------------
CL.CLR			lda (ZPCLBuf)
				beq CL.Reset.1

.1				lda CL.Ptr
				cmp CL.Len
				beq .2

				inc CL.Ptr

				jsr CL.FS.OUT
				bcc .1

				rts

.2				ldy #0

.3				lda #C.DEL
				sta (ZPCLBuf),y
				iny
				lda (ZPCLBuf),y
				bne .3

				jsr CL.Print
*--------------------------------------
CL.Reset		lda #0
				sta (ZPCLBuf)

CL.Reset.1		ldx #CL.Ptr

.1				stz 0,x
				inx
				cpx #CL.bEscMode+1
				bne .1

				>LDYA ZPCLBuf
				>STYA ZPCLBufPtr

CL.Reset.8		clc
				rts
*--------------------------------------
CL.BS			lda CL.Ptr
				beq CL.Reset.8

				dec CL.Ptr

CL.BS.OUT		lda #C.BS
				>SYSCALL PutChar

				rts
*--------------------------------------
CL.FS			lda CL.Ptr
				cmp CL.Len
				beq CL.Reset.8

				inc
				sta CL.Ptr

CL.FS.OUT		lda #C.ESC
				>SYSCALL PutChar

				lda #'['
				>SYSCALL PutChar

				lda #'C'
				>SYSCALL PutChar

				rts
*--------------------------------------
* Erase char UNDER cursor
*--------------------------------------
CL.SUPPR		lda CL.Len
				beq CL.Reset.8

				lda CL.Ptr
				cmp CL.Len
				beq CL.Reset.8
*--------------------------------------
CL.DEL			ldy CL.Ptr

.1				iny
				lda (ZPCLBuf),y
				dey
				sta (ZPCLBuf),y
				iny
				cpy CL.Len
				bne .1

				dec CL.Len
*--------------------------------------
CL.PrintEOL		ldy CL.Ptr

.1				cpy CL.Len
				beq .8

				phy
				lda (ZPCLBuf),y
				>SYSCALL PutChar
				ply
				bcs .9

				iny
				bra .1

.2				lda #C.SPACE
				>SYSCALL PutChar
				bcs .9

				jsr CL.BS.OUT
				bcs .9

				ldy CL.Ptr

.3				cpy CL.Len
				beq .8

				phy
				jsr CL.BS.OUT
				ply
				bcs .9

				iny
				bra .3

.8				clc
.9				rts
*--------------------------------------
CL.Print		ldy #S.PS.hStdOut
				lda (pPS),y
				>PUSHA
				>PUSHW ZPCLBuf

				>SYSCALL FPutS
				rts
*--------------------------------------
MAN
SAVE usr/src/bin/forth.s.cl
LOAD usr/src/bin/forth.s
ASM
