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 40					4 sec.
IAC.BUF.MAX		.EQ 20
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
TimeOut			.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

IAC.BUF			.BS IAC.BUF.MAX

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

				ldx ArgIndex
				lda ArgDev
				bne .2

				stx ArgDev
				bra .1

.2				lda ArgProg
				bne .9

				stx ArgProg
				bra .1

.9				>LDYA L.MSG.USAGE
				>SYSCALL PutS

				lda #E.SYN
				sec
.99				rts
*--------------------------------------
.8				lda ArgProg
				beq .9

				lda ArgDev
				beq .9

				>SYSCALL ArgV
				>PUSHYA
				>PUSHBI O.RDWR
				>PUSHBI 0				type
				>PUSHA					auxtype
				>PUSHA
				>SYSCALL FOpen
				bcs .99

				sta hFILE

				ldy #S.PS.hStdIn
				sta (pPS),y
				iny						#S.PS.hStdOut
				sta (pPS),y
				iny						#S.PS.hStdErr
				sta (pPS),y
*--------------------------------------
CS.RUN.LOOP0 	>SLEEP

				>PUSHW L.ENV.TERM
				>SYSCALL UnsetEnv

				ldy #S.PS.hStdOut
				lda (pPS),y
				>PUSHA
				>PUSHW L.TELNETOPTS
				>PUSHWI TELNETOPTS.LEN
				>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

				jsr CS.RUN.GREETINGS
				bcs CS.RUN.EXIT			unable to send greetings...

.2				lda ArgProg
				>SYSCALL ArgV
				>PUSHYA
				>PUSHBI S.PS.F.HOLD
				>SYSCALL ExecV
				>SLEEP

CS.RUN.EXIT		lda bExitOnClose
				bmi .8

				ldy #S.PS.S
				lda (pPS),y
				cmp #S.PS.S.QUIT		I've been killed :-(
				beq .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 #TN.O.TTYPE
				bne .2

				ldy #S.PS.hStdOut
				lda (pPS),y
				>PUSHA
				>PUSHW L.TELNETOPT.TTYPE
				>PUSHWI TELNETOPT.TTYPE.LEN
				bra .7

.2				cpx #TN.O.LINEMODE
				bne .8

				ldy #S.PS.hStdOut
				lda (pPS),y
				>PUSHA
				>PUSHW L.TELNETOPT.LMODE
				>PUSHWI TELNETOPT.LMODE.LEN

.7				>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 IAC.BUF,y
				iny
				lda #0
				sta IAC.BUF,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 #TN.O.TTYPE
				bne .8

				lda IAC.SB.CMD
				cmp #SB.IS
				bne .8

				>PUSHW L.ENV.TERM
				>PUSHWI IAC.BUF
				>SYSCALL SetEnv
.8				jmp CS.RUN.LOOP1

.9				rts
*--------------------------------------
CS.RUN.GREETINGS
				>PUSHW L.MSG.GREETINGS
				>PUSHW A2osX.KVER
				lda ArgDev
				>SYSCALL ArgV
				>PUSHYA
				>PUSHBI 4
				>SYSCALL PrintF
				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
CS.INIT			clc
				rts
*--------------------------------------
CkeckOption		lda (ZPPtr1)
				cmp #'-'
				bne .8

				ldy #2 
				lda (ZPPtr1),y
				bne .8

				dey
				lda (ZPPtr1),y
				ldx #OptionVars-OptionList-1

.2				cmp OptionList,x
				beq .3

				dex
				bpl .2

				sec						invalid option
				rts

.3				ldy OptionVars,x
				eor #$80				toggle option
				sta $0,y
				lda #0					exit with 0

.8				clc						!0 if not an option
				rts
*--------------------------------------
CS.END
OptionList		.AS "Ee"
OptionVars		.DA #bExitOnClose,#bExitOnClose
*--------------------------------------
MSG.GREETINGS	.CZ "\r\nA2osX-GeTTY %d.%d on %s\r\n"
MSG.USAGE		.CS "Usage : GETTY <DEV> <PROGRAM>\r\n"
				.CZ "   -E : Exit on disconnect"
*--------------------------------------
TELNETOPTS		.DA #IAC,#WILL,#TN.O.BINARY
				.DA #IAC,#WILL,#TN.O.ECHO
				.DA #IAC,#WILL,#TN.O.SGA
				.DA #IAC,#DO,#TN.O.SGA
				.DA #IAC,#DO,#TN.O.NAWS
				.DA #IAC,#DO,#TN.O.TTYPE
				.DA #IAC,#DO,#TN.O.LINEMODE
TELNETOPTS.LEN	.EQ *-TELNETOPTS
*--------------------------------------
TELNETOPT.TTYPE	.DA #IAC,#SB,#TN.O.TTYPE,#SB.SEND,#IAC,#SE
TELNETOPT.TTYPE.LEN	.EQ *-TELNETOPT.TTYPE
*--------------------------------------
TELNETOPT.LMODE	.DA #IAC,#SB,#TN.O.LINEMODE,#SB.SEND,#0,#IAC,#SE
TELNETOPT.LMODE.LEN .EQ *-TELNETOPT.LMODE
ENV.TERM		.AZ "TERM"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
DS.END			.ED
MAN
SAVE usr/src/sbin/getty.s
ASM
