NEW
  AUTO 3,1
				.LIST OFF	
*--------------------------------------
CL.Init			>LDYAI 256
				>SYSCALL GetMem
				bcs .9
				>STYA ZPCLBuf
				txa
				>STA.G CL.hCLBuf
				
.9				rts				
*--------------------------------------
CL.Quit			>LDA.G CL.hCLBuf
				beq .9
				>SYSCALL FreeMem

.9				rts	
*--------------------------------------
CL.PrintPrompt	>LDYA L.PS1

				>SYSCALL ExpandStr
				bcs .9
				
				phx
				pha
				>PUSHBI 0
				pla
				>SYSCALL printf
				pla
				>SYSCALL FreeMem
				
.1				>PUSHBI 0
				>LDYA L.MSG.PROMPT
				>SYSCALL printf
.9				rts
*--------------------------------------
CL.CHARIN		tax

				>LDA.G bEscMode
				beq .13

				cpx #'['
				beq .9

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

				ldx #EscChars.Cnt-1

.17				cmp EscChars,x
				beq .18
				dex
				bpl .17
				clc
				rts

.18				txa
				asl
				tax
				jmp (J.ESC,x)
*--------------------------------------
.13				cpx #C.SPACE
				bcc CL.CHARIN.CTRL

.1				cpx #C.DEL
				bne .2
				jmp CL.DEL

.2				jmp CL.Insert

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

				cpx #C.EOF
				beq .18
				
.10				cpx #C.ESC
				bne .11
				
				lda #$ff
				>STA.G bEscMode
				clc
				rts

.11				cpx #3					Ctrl-C
				bne .12

				jmp CL.CLR
								
.12				cpx #26					Ctrl-Z
				bne .13
				
				jmp CL.SUPPR
				
.18				lda #$ff
				>STA.G CL.bReady
				
.13				clc				
				rts
*--------------------------------------
CL.Insert		>LDA.G CL.Len
				inc
				beq .9					Buffer full, discard...
				sta (pData),y

				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
				
				>LDA.G bREAD.S
				bpl .2
				
				>INC.G CL.Ptr
				clc
				rts

.2				txa				
				>SYSCALL PutChar
				
				>INC.G CL.Ptr
				
				jmp CL.PrintEOL
				
.9				clc
				rts
*--------------------------------------
CL.CLR			lda (ZPCLBuf)
				beq 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.bReady
				>STA.G CL.Ptr
				>STA.G CL.Len
				rts
*--------------------------------------
CL.BS			>LDA.G CL.Ptr
				beq .9
				
				dec
				sta (pData),y
				
				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 #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

				bra CL.DEL.1
*--------------------------------------
* Erase char BEFORE cursor
*--------------------------------------
CL.DEL			>LDA.G CL.Len
				beq CL.PrintEOL.8
				
				>LDA.G CL.Ptr
				beq CL.PrintEOL.8

				lda #C.BS
				>SYSCALL PutChar

				>DEC.G CL.Ptr
				
CL.DEL.1		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
*--------------------------------------
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	>LDYA ZPCLBuf
				>PUSHYA
				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
