NEW
PREFIX
AUTO 4,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF SBIN/GETTY
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/NET.TELNET.I
*--------------------------------------
TIMEOUT.MAX		.EQ 100					10 sec.
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
ZS.END			.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #S.PS.F.EVENT		S.PS.F
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data SegmentSize
				.DA #16					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
L.MSG.USAGE		.DA MSG.USAGE
L.MSG.GREETINGS .DA MSG.GREETINGS
L.TELNETOPTS	.DA TELNETOPTS
L.TELNETOPT.TTYPE	.DA TELNETOPT.TTYPE
L.TELNETOPT.LMODE	.DA TELNETOPT.LMODE
L.ENV.TERM		.DA ENV.TERM
				.DA 0					End Of Reloc Table
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			ldy #S.PS.ARGC
				lda (pPs),y
				cmp #2

				bcc .9					

.1				>INC.G ArgIndex
				>SYSCALL ArgV
				bcs .8
				
				>STYA ZPPtr1

				jsr CkeckOption
				bcs .9
				beq .1					valid option, check next arg 
				
				>LDA.G ArgDev
				bne .2
				
				>LDA.G ArgIndex
				>STA.G ArgDev
				bra .1
				
.2				>LDA.G ArgProg
				bne .9
				
				>LDA.G ArgIndex
				>STA.G ArgProg
				bra .1
					
.9				>PUSHBI 0
				>LDYA L.MSG.USAGE
				>SYSCALL printf
				lda #E.SYN
				sec
.99				rts
				
.8				>LDA.G ArgDev
				beq .9
				>LDA.G ArgProg
				beq .9
				
				>PUSHWI 0				auxtype
				>PUSHBI 0				type
				>PUSHBI O.RDWR
				>LDA.G ArgDev
				>SYSCALL ArgV

				>SYSCALL fopen
				bcs .99

				ldy #S.PS.hStdIn
				sta (pPs),y
				iny						#S.PS.hStdOut
				sta (pPs),y
				iny						#S.PS.hStdErr
				sta (pPs),y
				
				>STA.G hFILE

CS.RUN.LOOP0 	>LDYA L.ENV.TERM
				>SYSCALL UnSetEnv

.1				>PUSHWI TELNETOPTS.LEN
				>PUSHW L.TELNETOPTS
				ldy #S.PS.hStdOut
				lda (pPs),y
				>SYSCALL fwrite
				bcs CS.RUN.EXIT			I/O error

				jsr Init.Timeout
			
CS.RUN.LOOP1	>SLEEP

				ldy #S.PS.hStdIn
				lda (pPS),y

				>SYSCALL feof
				bcs CS.RUN.9
				
				tay
				beq .1

				>SYSCALL GetChar
				bcs CS.RUN.9
				
				cmp #IAC
				beq CS.RUN.IACMODE
				>SYSCALL PutChar
				bcs CS.RUN.EXIT
				
				bra CS.RUN.LOOP1
				
.1				jsr Wait.Timeout
				bcc CS.RUN.LOOP1

				>LDA.G ArgDev
				>SYSCALL ArgV
				>PUSHYA
				>PUSHWI K.VER
				>PUSHBI 4
				>LDYA L.MSG.GREETINGS
				>SYSCALL printf
				
				bcs CS.RUN.EXIT			unable to send greetings...
				
.2				>PUSHBI S.PS.F.HOLD

				>LDA.G ArgProg
				>SYSCALL ArgV
				>SYSCALL ExecV
				
				>SLEEP
				
CS.RUN.EXIT		>LDA.G bExitOnClose
				bmi .8
				
				jmp CS.RUN.LOOP0

.8				lda #0
				sec		
CS.RUN.9		rts
*--------------------------------------
CS.RUN.IACMODE	>SYSCALL GetChar		Wait for IAC CMD (or SB)
				bcs CS.RUN.9
				
				>STA.G IAC.CMD
				
				>SYSCALL GetChar		Wait for IAC SUBCMD
				bcs CS.RUN.9
				
				>STA.G IAC.SUBCMD
				tax
				
				>LDA.G IAC.CMD
				cmp #SB
				beq CS.RUN.IAC.SB
*--------------------------------------			
CS.RUN.IAC.CMD	cmp #WILL
				bne .8
				
				cpx #TELOPT.TTYPE
				bne .2
				
				>PUSHWI TELNETOPT.TTYPE.LEN
				>PUSHW L.TELNETOPT.TTYPE
				bra .7

.2				cpx #TELOPT.LINEMODE
				bne .8
				
				>PUSHWI TELNETOPT.LMODE.LEN
				>PUSHW L.TELNETOPT.LMODE
				
.7				ldy #S.PS.hStdOut
				lda (pPs),y
				>SYSCALL fwrite
				
.8				jmp CS.RUN.LOOP1
*--------------------------------------
CS.RUN.IAC.SB  	>SYSCALL GetChar		Wait for IAC SB.IS or SEND
				bcs .9
				
				>STA.G IAC.SB.CMD
				
				>STZ.G IAC.SB.LEN
				
.2				>SYSCALL GetChar
				bcs .9

				cmp #IAC
				beq .4					end of DATA, go wait SE
				
				pha
				
				>LDA.G IAC.SB.LEN
				clc
				adc #IAC.SB.DATA
				tay
				
				pla
				sta (pData),y
				iny
				lda #0
				sta (pData),y
				
				>INC.G IAC.SB.LEN
				bra .2
				
.4				>SYSCALL GetChar
				bcs .9
				
				cmp #SE
				bne .8

				>LDA.G IAC.SUBCMD
				cmp #TELOPT.TTYPE
				bne .8
				
				>LDA.G IAC.SB.CMD
				cmp #SB.IS
				bne .8

				>PUSHEA.G IAC.SB.DATA
				>LDYA L.ENV.TERM
				>SYSCALL SetEnv
				
.8				jmp CS.RUN.LOOP1
.9				rts
*--------------------------------------			
CS.DOEVENT		lda (pEvent)
				bpl .9					is it a TIMER event?

				>LDA.G TimeOut
				beq .9
				
				dec 
				sta (pData),y
				
.9				sec						do not discard TIMER event
				rts	
*--------------------------------------
CS.QUIT			>LDA.G hFILE
				beq .8

				>SYSCALL fclose
				
.8				clc
				rts
*--------------------------------------
Init.Timeout	lda #TIMEOUT.MAX
				>STA.G TimeOut
				rts
*--------------------------------------
Wait.TimeOut	sec
				>LDA.G TimeOut
				beq .9
				clc
.9				rts
*--------------------------------------
CkeckOption		lda (ZPPtr1)
				cmp #'-'
				bne .8
				
				ldy #2 
				lda (ZPPtr1),y
				bne .8
				
				dey
				lda (ZPPtr1),y
				
				ldx OptionList
				
.2				cmp OptionList,x
				beq .3
				dex
				bne .2
				
				sec						invalid option
				rts
				
.3				ldy OptionVars-1,x
				eor #$80				toggle option
				sta (pData),y
				lda #0					exit with 0
				
.8				clc						!0 if bot an option
				rts
*--------------------------------------
CS.END
OptionList		>PSTR "Ee"
OptionVars		.DA #bExitOnClose,#bExitOnClose
MSG.USAGE		.AS "Usage : GETTY <DEV> <PROGRAM>\r\n"
				.AZ "   -E : Exit on disconnect\r\n"
MSG.GREETINGS	.AZ "\r\nA2osX-GeTTY %d.%d on %s\r\n"
*--------------------------------------
TELNETOPTS		.DA #IAC,#WILL,#TELOPT.BINARY
				.DA #IAC,#WILL,#TELOPT.ECHO
				.DA #IAC,#WILL,#TELOPT.SGA
				.DA #IAC,#DO,#TELOPT.SGA
				.DA #IAC,#DO,#TELOPT.NAWS
				.DA #IAC,#DO,#TELOPT.TTYPE
				.DA #IAC,#DO,#TELOPT.LINEMODE
TELNETOPTS.LEN	.EQ *-TELNETOPTS
*--------------------------------------
TELNETOPT.TTYPE	.DA #IAC,#SB,#TELOPT.TTYPE,#SB.SEND,#IAC,#SE
TELNETOPT.TTYPE.LEN	.EQ *-TELNETOPT.TTYPE
*--------------------------------------
TELNETOPT.LMODE	.DA #IAC,#SB,#TELOPT.LINEMODE,#SB.SEND,#0,#IAC,#SE
TELNETOPT.LMODE.LEN .EQ *-TELNETOPT.LMODE
ENV.TERM		.AZ "TERM"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
ArgIndex		.BS 1
ArgDev			.BS 1
ArgProg			.BS 1
hFILE			.BS 1
bExitOnClose	.BS 1
TimeOut			.BS 1
IAC.CMD			.BS 1
IAC.SUBCMD		.BS 1
IAC.SB.CMD		.BS 1
IAC.SB.LEN		.BS 1
IAC.SB.DATA		.BS 32
DS.END			.ED
MAN
SAVE USR/SRC/SBIN/GETTY.S
ASM
