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

				>STYA ZPCLBuf
				txa
				>STA.G CL.hBuf

.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 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.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

				lda #C.BS
				>SYSCALL PutChar

.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 #3					Ctrl-C
				beq CL.CLR

				cpx #C.TAB
				bne .1

				jmp HIS.Select

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

.8				clc
.9				rts
*--------------------------------------
CL.CLR			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
				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.Print
*--------------------------------------
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
*--------------------------------------
CL.BS			>LDA.G CL.Ptr
				beq .9

				dec
				sta (pData),y

				>LDA.G CL.bSilent
				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 CL.bSilent
				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 CL.bSilent
				bmi CL.PrintEOL.8
*--------------------------------------
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 #C.SPACE
				>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.Print		ldy #S.PS.hStdOut
				lda (pPS),y
				>PUSHA
				>PUSHW ZPCLBuf

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