NEW
  AUTO 3,1
*--------------------------------------
CL.Init			>LDYAI 256
				>SYSCALL GetMem
				bcs .9

				>STYA ZPCLBuf
				txa
				>STA.G CL.hBuf

.9				rts
*--------------------------------------
CL.PrintPrompt	>PUSHW L.PS1
				>PUSHW ZPCLBuf
				>SYSCALL Expand
				bcs .9

				>LDYA ZPCLBuf
				jsr IO.EscOutYA
				
				lda #0
				sta (ZPCLBuf)

.1				>LDYA L.MSG.PROMPT
				jsr IO.OutYA
.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 #IN.EscCharsCnt-1

.1				cmp IN.EscChars,x
				beq .2

				dex
				bpl .1

.8				clc
				rts						invalid \e[ sequence

.2				>LDA.G CL.MaxCnt
				bne .3

				lda IN.EscCodes,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 CL.MaxCnt
				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 CL.bSilent
				bmi .6

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

.6				>DEC.G CL.Ptr

				jmp CL.DEL
*--------------------------------------
CL.Insert		>LDA.G CL.Len
				>CMP.G CL.MaxCnt
				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 CL.bSilent
				bmi .7

				txa

				>SYSCALL PutChar
				bcs .9

				jsr CL.PrintEOL

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

				lda #$ff
				>STA.G CL.bReady

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

				cpx #C.EOF
				beq .19

				cpx #C.TAB
				bne .1

				jmp HIS.Select

.1				cpx #26					Ctrl-Z
				beq CL.SUPPR

				cpx #3					Ctrl-C
				bne .8
				
				jmp CL.CLR

.18				jsr CheckLFAfterCR		Check for any extra LF
				bcs .9

.19				lda #$ff
				>STA.G CL.bReady

.8				clc
.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						CL.Ptr

.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 CL.bSilent
				bmi CL.PrintEOL.8
*--------------------------------------
CL.PrintEOL		>LDA.G CL.Ptr

				>CMP.G CL.Len
				beq CL.PrintEOL.8

.1				pha
				tay
				lda (ZPCLBuf),y
				>SYSCALL PutChar
				pla
				inc
				>CMP.G CL.Len
				bne .1

				lda #C.SPACE
				>SYSCALL PutChar
				>LDA.G CL.Ptr

.3				>CMP.G CL.Len
				beq .4

				pha
				jsr CL.BS.OUT
				pla
				inc
				bra .3

.4				jmp CL.BS.OUT

CL.PrintEOL.8	clc
				rts
*--------------------------------------
CL.BS			>LDA.G CL.Ptr
				beq CL.BS.8

				dec
				sta (pData),y

				>LDA.G CL.bSilent
				bpl CL.BS.OUT
				
CL.BS.8			clc
				rts

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

				rts
*--------------------------------------
CL.FS			>LDA.G CL.Ptr
				>CMP.G CL.Len
				beq CL.BS.8

				inc
				>STA.G CL.Ptr

				>LDA.G CL.bSilent
				bmi CL.BS.8

CL.FS.OUT		>LDYA L.MSG.FS
				jmp IO.OutYA
*--------------------------------------
CL.CLR			>STZ.G HIS.Ptr

CL.CLR.1		lda (ZPCLBuf)
				beq CL.Reset.1

				>LDA.G CL.bSilent
				bmi CL.Reset

.1				>LDA.G CL.Ptr
				>CMP.G CL.Len
				beq .2

				inc
				>STA.G CL.Ptr

				jsr CL.FS.OUT
				bra .1

.2				ldy #0

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

				iny
				lda (ZPCLBuf),y
				bne .3

				>LDYA ZPCLBuf
				jsr IO.OutYA
*--------------------------------------
CL.Reset		lda #0
				sta (ZPCLBuf)

CL.Reset.1		lda #0

				ldy #CL.Ptr

.1				sta (pData),y
				iny
				cpy #CL.bEscMode+1
				bne .1

				rts
*--------------------------------------
CL.ReadResetV	sec						Verbose mode

CL.ReadReset	lda #$ff
				>STA.G CL.MaxCnt

				adc #0					CC = silent

				iny						CL.bSilent
				sta (pData),y
				
				rts
*--------------------------------------
MAN
SAVE usr/src/bin/sh.s.cl
LOAD usr/src/bin/sh.s
ASM
