NEW
  AUTO 3,1
				.LIST OFF	
*--------------------------------------
CL.Init			>LDYAI 256
				>SYSCALL GetMem
				bcs .9
				>STYA ZPCLBuf
				txa
				>STA.G CL.hCLBuf
.9				rts
*--------------------------------------
CL.PrintPrompt	>PUSHW L.PS1
				>PUSHWI 0
				>SYSCALL Expand
				bcs .9

				phx
				
				>PUSHYA
				>PUSHBI 0

				>SYSCALL printf
				pla
				>SYSCALL FreeMem

.1				>PUSHW L.MSG.PROMPT
				>PUSHBI 0
				>SYSCALL printf
.9				rts
*--------------------------------------
CL.READN0A		tax
CL.READN0X		
				>PUSHW ZPCLBuf
				>PUSHW L.FMT.Byte
				txa
				>PUSHA
				>PUSHBI 1
				>SYSCALL sprintf
				bcs .9
				
				lda #$ff
				>STA.G CL.bReady
*				clc
.9				rts
*--------------------------------------
CL.CHARIN		tax

				>LDA.G CL.bEscMode		\e ?
				beq .4

				cpx #'['
				beq .8					\e[

				lda #0
				sta (pData),y			Y=bEscMode
				txa

				ldx #EscChars.Cnt-1

.1				cmp EscChars,x
				beq .2

				dex
				bpl .1

.8				rts						invalid \e[ sequence

.2				>LDA.G READ.N
				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.G CL.bEscMode
				clc
				rts

.5				>LDA.G READ.N
				beq CL.READN0X
				
				cpx #C.SPACE
				bcc CL.CHARIN.CTRL

				cpx #C.DEL
				bne CL.Insert
*--------------------------------------
* Erase char BEFORE cursor
*--------------------------------------
				>LDA.G CL.Len
				beq .8
				>LDA.G CL.Ptr
				beq .8

				>LDA.G bREAD.S
				bmi .6

				lda #C.BS
				>SYSCALL PutChar

.6				>DEC.G CL.Ptr
				jmp CL.DEL
*--------------------------------------
CL.Insert		>LDA.G CL.Len
				>CMP.G READ.N
				beq .8					Buffer full, discard...

				inc
				>STA.G CL.Len

				pha
				
.1				ply
				dey
				lda (ZPCLBuf),y			Move from Ptr To end of buffer forward...
				iny
				sta (ZPCLBuf),y
				dey
				phy
				tya
				>CMP.G CL.Ptr
				bne .1

				ply						Y=CL.Ptr

*				>LDA.G CL.Ptr

				txa
				sta (ZPCLBuf),y
				>INC.G CL.Ptr

				>LDA.G bREAD.S
				bmi .7

				txa
				>SYSCALL PutChar
				jsr CL.PrintEOL

.7				>LDA.G READ.N			READ N = 1, don't wait for CR
				dec
				bne .8

				lda #$ff
				>STA.G CL.bReady

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

				cpx #C.EOF
				beq .18

				cpx #3					Ctrl-C
				beq CL.CLR

				cpx #26					Ctrl-Z
				bne .8
				jmp CL.SUPPR
				
.18				jsr CheckLFAfterCR		Check for any extra LF
				bcs .9
				
				lda #$ff
				>STA.G CL.bReady

.8				clc
.9				rts
*--------------------------------------
CL.CLR			lda (ZPCLBuf)
				beq CL.RESET.1
				
				>LDA.G bREAD.S
				bmi CL.RESET.1

.1				>LDA.G CL.Ptr
				>CMP.G CL.Len
				beq .2
				inc
				>STA.G 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		>STA.G CL.Ptr
				>STA.G CL.Len
				>STA.G CL.bReady
				>STA.G CL.bEscMode
				rts
*--------------------------------------
CL.BS			>LDA.G CL.Ptr
				beq .9

				dec
				sta (pData),y

				>LDA.G bREAD.S
				bmi .9

				lda #C.BS
				>SYSCALL PutChar
.9				rts
*--------------------------------------
CL.NAK			>LDA.G CL.Ptr
				>CMP.G CL.Len
				beq .9
				inc
				>STA.G CL.Ptr
				>LDA.G bREAD.S
				bmi .9
				lda #C.FS
				>SYSCALL PutChar		
.9				rts
*--------------------------------------
* Erase char UNDER cursor
*--------------------------------------
CL.SUPPR		>LDA.G CL.Len
				beq CL.PrintEOL.8
				>LDA.G CL.Ptr
				>CMP.G CL.Len
				beq CL.PrintEOL.8
*--------------------------------------
CL.DEL			pha
.1				ply
				iny
				lda (ZPCLBuf),y
				dey
				sta (ZPCLBuf),y
				iny
				phy
				tya
	
				>CMP.G CL.Len
				bne .1
				pla
				>DEC.G CL.Len
				>LDA.G bREAD.S
				bpl CL.PrintEOL
				clc
				rts
*--------------------------------------
CL.PrintEOL		>LDA.G CL.Ptr
.1				>CMP.G CL.Len
				beq .2
				pha
				tay
				lda (ZPCLBuf),y
				>SYSCALL PutChar
				pla
				inc
				bra .1

.2				lda #' '
				>SYSCALL PutChar
				>LDA.G CL.Ptr
.3				>CMP.G 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	>PUSHW ZPCLBuf

				ldy #S.PS.hStdOut
				lda (pPs),y
				>SYSCALL fputs
				rts
*--------------------------------------
MAN
SAVE USR/SRC/BIN/SH.S.CL
LOAD USR/SRC/BIN/SH.S
ASM
