NEW
  AUTO 3,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.
IAC.BUF.MAX		.EQ 32
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
TimeOut			.BS 1
hErr			.BS 1
ArgIndex		.BS 1
ArgDev			.BS 1
ArgProg			.BS 1
hFILE			.BS 1
bExitOnClose	.BS 1
IAC.CMD			.BS 1
IAC.SUBCMD		.BS 1
IAC.SB.CMD		.BS 1
IAC.SB.LEN		.BS 1
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 #64					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 ArgIndex
				lda ArgIndex
				>SYSCALL ArgV
				bcs .8
				
				>STYA ZPPtr1

				jsr CkeckOption
				bcs .9
				beq .1					valid option, check next arg 
				
				lda ArgDev
				bne .2
				
				lda ArgIndex
				sta ArgDev
				bra .1
				
.2				lda ArgProg
				bne .9
				
				lda ArgIndex
				sta ArgProg
				bra .1
					
.9				>PUSHBI 0
				>LDYA L.MSG.USAGE
				>SYSCALL printf
				lda #E.SYN
				sec
.99				rts
				
.8				lda ArgDev
				beq .9
				lda ArgProg
				beq .9
				
				ldy #S.PS.hStdErr
				lda (pPs),y
				sta hErr
				
				>PUSHWI 0				auxtype
				>PUSHBI 0				type
				>PUSHBI O.RDWR
				lda 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 hFILE

CS.RUN.LOOP0 	>SLEEP

				>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

				lda #TIMEOUT.MAX
				sta TimeOut
			
CS.RUN.LOOP1	>SLEEP

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

				>SYSCALL feof
				bcs CS.RUN.9			I/O error
				
				tay
				bne .1					no char

				>SYSCALL GetChar
				bcs CS.RUN.9
				
				cmp #IAC
				beq CS.RUN.IACMODE
				
				>SYSCALL PutChar
				bcs CS.RUN.EXIT
				
				bra CS.RUN.LOOP1
				
.1				lda TimeOut
				bne CS.RUN.LOOP1

				lda 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 ArgProg
				>SYSCALL ArgV
				>SYSCALL ExecV
				
				>SLEEP
				
CS.RUN.EXIT		lda 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 IAC.CMD
				
				>SYSCALL GetChar		Wait for IAC SUBCMD
				bcs CS.RUN.9
				
				sta IAC.SUBCMD
				tax
				
				lda 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 IAC.SB.CMD
				
				stz IAC.SB.LEN
				
.2				>SYSCALL GetChar
				bcs .9

				cmp #IAC
				beq .4					end of DATA, go wait SE
				
				ldy IAC.SB.LEN
				
				sta (pData),y
				iny
				lda #0
				sta (pData),y
				
				sty IAC.SB.LEN
				cpy #IAC.BUF.MAX
				bne .2
				bra .8
				
.4				>SYSCALL GetChar
				bcs .9
				
				cmp #SE
				bne .8

				lda IAC.SUBCMD
				cmp #TELOPT.TTYPE
				bne .8
				
				lda IAC.SB.CMD
				cmp #SB.IS
				bne .8

				>PUSHW pData			 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 TimeOut
				beq .9
				
				dec TimeOut
				
.9				sec						do not discard TIMER event
				rts	
*--------------------------------------
CS.QUIT			lda hFILE
				beq .8

				>SYSCALL fclose
				
.8				clc
				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 $0,y
				lda #0					exit with 0
				
.8				clc						!0 if not 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
IAC.SB.DATA		.BS IAC.BUF.MAX
DS.END			.ED
MAN
SAVE USR/SRC/SBIN/GETTY.S
ASM
