NEW
  AUTO 3,1
				.LIST OFF
*--------------------------------------
IO.GetC			jsr IO.GetChar
				bcs .9

				tax

				ldy #S.CTX.bEscMode
				lda (ZPCtxPtr),y
				beq .20

				bmi .40					IAC mode

				iny 					S.CTX.InBufPtr
				lda (ZPCtxPtr),y

				cpx #'['				CSI ?
				bne .12

				cmp #S.CTX.InBufPtr
				bne .49					buffer not empty...bad SEQ

.12				jsr IO.GetC.ToBuf
				bcs .49					Buffer Full

				cpx #'['
				beq .18

				cpx #64
				bcs IO.GetC.ESCSEQ		if CC, not a letter...SEQ is incomplete

.18				lda #0
				clc
.9				rts
*--------------------------------------
.20				cpx #C.ESC
				bne .30

				inc						A = $01

				bra .31
*--------------------------------------
.30				cpx #IAC
				bne .38

				dec						A = $FF

.31				jsr IO.GetC.SetMode

				lda #0
				clc
				rts
*--------------------------------------
.38				txa
				clc
.39				rts
*--------------------------------------
.40				jsr IO.GetC.ToBuf
				bcs .49					Buffer Full

				cpy #S.CTX.InBuf		First char...
				beq .48

				ldy #S.CTX.InBuf
				lda (ZPCtxPtr),y
				cmp #SB					First char is SB ?
				bne IO.GetC.IACSEQ		no ...

				cpx #SE					is SE after SB ..... ?
				bne .48

				bra IO.GetC.IACSEQ		yes, we have IAC SB .....SE

.49				jsr IO.GetC.Reset

.48				lda #0
				clc
				rts
*--------------------------------------
IO.GetC.ESCSEQ	cmp #S.CTX.InBuf+2
				bcs .2

				ldy #ESC.Out-ESC.In-1
				txa

.1				cmp ESC.In,y
				beq .7

				dey
				bpl .1

				bra .6
*--------------------------------------
* \e[xxx;yyyR
*--------------------------------------
.2				cpx #'R'				Response to cursor position query?
				bne .6

				lda #S.CTX.InBuf+1		skip ESC [
				jsr IO.GetC.GetCtxEA
				>PUSHYA

				>PUSHW L.FMT.ESCCSI.R

				lda #S.CTX.H
				jsr IO.GetC.GetCtxEA
				>PUSHYA

				lda #S.CTX.W
				jsr IO.GetC.GetCtxEA
				>PUSHYA

				>PUSHBI 4				2 pointers
				>SYSCALL2 sscanf

.6				ldx #0
				bra .8

.7				ldx ESC.Out,y

.8				jsr IO.GetC.Reset

				txa
				clc
				rts
*--------------------------------------
* IAC SB TN.O.NAWS ww ww hh hh IAC SE
*--------------------------------------
IO.GetC.IACSEQ	ldy #S.CTX.InBuf
				lda (ZPCtxPtr),y
				cmp #SB
				bne .8

				iny
				lda (ZPCtxPtr),y
				cmp #TN.O.NAWS
				bne .8

				iny
				lda (ZPCtxPtr),y
				bne .8

				iny
				lda (ZPCtxPtr),y
				cmp #80
				bcs .1

				lda #80

.1				tax

				iny
				lda (ZPCtxPtr),y
				bne .8

				iny
				lda (ZPCtxPtr),y
				cmp #24
				bcs .2

				lda #24

.2				ldy #S.CTX.H

				sta (ZPCtxPtr),y

				dey						S.CTX.W

				txa
				sta (ZPCtxPtr),y

				jsr IO.GetC.Reset

				lda #EV.RESIZE
				clc
				rts

.8				jsr IO.GetC.Reset

				lda #0
				clc
				rts
*--------------------------------------
IO.GetC.Reset	lda #0

IO.GetC.SetMode
				ldy #S.CTX.bEscMode
				sta (ZPCtxPtr),y

				iny						Y = S.CTX.InBufPtr
				tya
				sta (ZPCtxPtr),y

				rts
*--------------------------------------
IO.GetC.GetCtxEA
				clc
				adc ZPCtxPtr
				tay
				lda ZPCtxPtr+1
				adc #0
.9				rts
*--------------------------------------
IO.GetC.ToBuf	ldy #S.CTX.InBufPtr
				lda (ZPCtxPtr),y

				cmp #S.CTX.EndBuf		buffer full
				bcs .9

				phy
				inc
				tay
				txa
				sta (ZPCtxPtr),y
				tya
				ply
				sta (ZPCtxPtr),y

*				clc

.9				rts
*--------------------------------------
IO.GetChar		ldx ZPCtxPtr
				phx
				ldx ZPCtxPtr+1
				phx
				ldx ZPObjPtr
				phx
				ldx ZPObjPtr+1
				phx

				>SYSCALL GetChar
				bcs .9

				cmp #C.CR
				clc
				bne .9

				ldy #S.PS.hStdIn		Check for any extra LF
				lda (pPS),y
				>SYSCALL FEOF
				bcs .9

				tay
				bne .8

				>SYSCALL GetChar
				bcs .9

.8				lda #C.CR

.9				plx
				stx ZPObjPtr+1
				plx
				stx ZPObjPtr
				plx
				stx ZPCtxPtr+1
				plx
				stx ZPCtxPtr

				rts
*--------------------------------------
IO.fputs		ldx ZPCtxPtr
				phx
				ldx ZPCtxPtr+1
				phx
				ldx ZPObjPtr
				phx
				ldx ZPObjPtr+1
				phx

				dec pStack
				>PUSHYA
				ldy #S.PS.hStdOut
				lda (pPS),y
				ldy #2
				sta (pStack),y
				>SYSCALL FPutS

				plx
				stx ZPObjPtr+1
				plx
				stx ZPObjPtr
				plx
				stx ZPCtxPtr+1
				plx
				stx ZPCtxPtr

				rts
*--------------------------------------
IO.LBWrite		ldx ZPCtxPtr
				phx
				ldx ZPCtxPtr+1
				phx
				ldx ZPObjPtr
				phx
				ldx ZPObjPtr+1
				phx

				ldy #S.PS.hStdOut
				lda (pPS),y
				>PUSHA
				>PUSHW LineBuf
				>PUSHW ZPLineBufCnt
				>SYSCALL fwrite

				plx
				stx ZPObjPtr+1
				plx
				stx ZPObjPtr
				plx
				stx ZPCtxPtr+1
				plx
				stx ZPCtxPtr

				rts
*--------------------------------------
MAN
SAVE usr/src/lib/libtui.s.io
LOAD usr/src/lib/libtui.s
ASM
