NEW
  AUTO 3,1
*--------------------------------------
CL.READN0A		tax
CL.READN0X		>PUSHW ZPCLBuf
				>PUSHW L.FMT.Byte
				txa
				>PUSHA
				>PUSHBI 1
				>SYSCALL SPrintF
				bcs .9
				
				lda #$ff
				sta 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				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

				lda #C.BS
				>SYSCALL PutChar

				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
				inc CL.Ptr

				>SYSCALL PutChar
				jsr CL.PrintEOL

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

				lda #$ff
				sta CL.bReady

.8				clc
				rts
*--------------------------------------
CL.CHARIN.CTRL	cpx #C.CR
				beq .8

				cpx #C.EOF
				beq .9

				cpx #3					Ctrl-C
				beq CL.CLR

				cpx #26					Ctrl-Z
				bne .8

				jmp CL.SUPPR
				
.8				jsr CheckLFAfterCR
				bcs .9

				lda #$ff
				sta CL.bReady

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

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

				inc
				sta CL.Ptr

				lda #C.FS
				>SYSCALL PutChar
				bra .1

.2				ldy #0

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

				jsr CL.PrintCLBuf
*--------------------------------------
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

				rts
*--------------------------------------
CL.BS			lda CL.Ptr
				beq .9

				dec CL.Ptr

				lda #C.BS
				>SYSCALL PutChar

.9				rts
*--------------------------------------
CL.NAK			lda CL.Ptr
				cmp CL.Len
				beq .9

				inc
				sta CL.Ptr

				lda #C.FS
				>SYSCALL PutChar		

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

				lda CL.Ptr
				cmp CL.Len
				beq CL.PrintEOL.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		lda CL.Ptr

.1				cmp CL.Len
				beq .2

				pha
				tay
				lda (ZPCLBuf),y
				>SYSCALL PutChar
				pla
				inc
				bra .1

.2				lda #C.SPACE
				>SYSCALL PutChar
				lda CL.Ptr

.3				cmp CL.Len
				beq .4

				pha
				lda #C.BS
				>SYSCALL PutChar
				pla
				inc
				bra .3
				
.4				lda #C.BS
				>SYSCALL PutChar

CL.PrintEOL.8	clc
				rts
*--------------------------------------
CL.PrintCLBuf	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
