	.title	k11eco	terminal emu, RSTS/E Only
	.ident	/8.0.01/
	.psect
	.enabl	gbl


;	Brian Nelson  29-Dec-83  16:00:09  initial version
;	Brian Nelson  10-Feb-86  11:45:44  Removed from K11CON.MAC



	.include	/IN:K11MAC.MAC/
	.iif ndf	,k11inc	,.error	; missing INCLUDE for K11MAC.MAC

	R$RT11	=	0

	locmod	=	ter$cc ! ter$bi	; for the 11's console (RSTS/E)
	remmod	=	ter$cc ! ter$bi ; for remote (RSTS/E)

;	note:	vt125's are a problem as they don't send xon when you type one

	.psect	$code

	.sbttl	connect code for kermit
	.enabl	gbl
	.enabl	lsb



doconn::tstb	ttname			; insure that a device name is set
	bne	1$			; ok
	message	<You must first define an output port:>,cr	;jfp 840607
	message	<Use the SET LINE command for this.>,cr
	return

	
1$:	message	<Connecting to line >	; Print the line and speed out
	print	#ttname			; the link name
	calls	ttspee	,<#ttname>	; get the speed now
	tst	r0			; not if we can't get it
	ble	5$			; yes, do not print 0 or -1 baud
	message	< at >			; and the speed
	sub	#10	,sp		; try to format the response a little
	mov	sp	,r1		; allocate a buffer and point to it
	deccvt	r0,r1,#4		; convert to decimal, 4 wide
	print	r1	,#4		; print it
	add	#10	,sp		; pop the buffer
	message	< baud>,cr		; and finish off the message
5$:	message	< Type CTRL >
	clr	-(sp)			; allocate a text buffer
	bisb	conesc	,(sp)		; get it
	add	#100	,(sp)		; convert it back to printable
	mov	sp	,r0		; point to it now
	print	r0	,#1		; at last
	tst	(sp)+			; pop the buffer
	message	< and then C to return to command level>,cr
	call	dorsts			; yes, call the correct routine

100$:	return

	.dsabl	lsb

	global	<conesc	,getsys	,ttcons	,ttname	,lun.co	,lun.ti>


	.sbttl	do the connect command for RSTS/E

;	 This is the  module that handles the CONNECT command for a Kermit
;	running in local mode on a RSTS or RT11 system. The method used is
;	simply to try a read  without stall on the remote's line,  echo to
;	the user's  terminal if there was data,  read without stall on the
;	users local terminal and send that data to the remote if there was
;	data to be read. If neither terminal  had data to get, then Kermit
;	will suspend itself  for 1 second.  For RT11, we will never stall.
;	Also,  for RT11,  whenever we get 20 (8) characters,  we will send
;	the remote  system an  XOFF so we can catch up on the input due to
;	the RT MT service overhead for single character i/o.

	chcount	=	0
	exectyp	=	2
	needxon	=	4
	locsize	=	10

dort11::
dorsts::sub	#locsiz	,sp		; allocate local r/w data
	mov	sp	,r4		; and a pointer to it please
	clr	-(sp)			; allocate single character buffer
	mov	sp	,r3		; and point to it please
	call	getsys			; get system type
	movb	r0	,exectyp(r4)	; save system type in exectype(r4)
	mov	#10	,chcount(r4)	; count down for rt11 to do an XOFF
	clr	needxon(r4)		; don't need to send xon yet
	calls	noecho	,<#ttname>	; don't want echoing please
	calls	gttnam	,<#ttcons>	; get the 11's terminal name
	calls	ttyini	,<#ttcons,#lun.co,#locmod>
	tst	r0			; did it work
	bne	5$			; no, exit with error
	calls	ttyini	,<#ttname,#lun.ti,#remmod>
	tst	r0			; did the initialization work ?
	beq	10$			; no, exit
5$:	jmp	90$

10$:	.iif ne,r$rt11	,call contrp	; special ^C things for RT11

20$:	clr	r2			; assume a short nap is needed
	calls	xbinrea	,<#lun.ti,#-1>	; try to get a charater from remote
	call	errchk			; insure error is correct one
	tst	r0			; did it work ?
	bne	30$			; no, send an XON to the remote system
	clrpar	r1			; yes, drop any aprity bits please
	movb	r1	,@r3		; yes, get the character and print
	cmpb	r1	,#'S&37		; /48/ Is this an XOFF?
	beq	21$			; /48/ Yes, ignore it please
	calls	binwri	,<r3,#1,#lun.co>; it out on the local terminal
21$:	call	dumplo			; dump to a log file perhaps
	mov	sp	,r2		; and say we don't want to sleep
	dec	chcount(r4)		; is it time to do a RT11 XOFF ?
	bne	40$			; no, wait a while then
	cmpb	exectype(r4),#sy$rt	; if not rt11, then skip the xoff
	bne	25$
	mov	#2	,needxon(r4)	; will need to send xon later
	call	sxoff			; yes, send an xoff please
25$:	mov	#10	,chcount(r4)	; and reset the character counter
	br	40$			; and look for local terminal input
30$:	cmpb	exectype(r4),#sy$rt	; if not rt, then skip the xon
	bne	40$			; not rt
	tst	needxon(r4)		; really send an xon today?
	beq	40$			; no
	dec	needxon(r4)		; don't overdo sending xon's please
	call	sxon			; no data, send an xon please

40$:	calls	xbinrea	,<#lun.co,#-1>	; see if any input is pending from
	tst	r0			; the local terminal
	bne	70$			; no
	movb	r1	,@r3		; yes, get it and send it to the

	bic	#^C177	,r1		; insure no high bits are set.
	cmpb	r1	,conesc		; other system unless it's the escape
	bne	50$
	calls	xbinrea	,<#lun.co,#20>	; char. get the next one please
	tst	r0			; but wait for it. did it work  ?
	bne	70$			; no
	bic	#^C177	,r1		; insure no high bits are set.
	cmpb	r1	,conesc		; really send the escape ?
	beq	50$			; yes
	call	concmd			; yes, do the command
	tst	r0			; exit ?
	bgt	100$			; yes, bye
	br	70$			; no, next please

50$:	setpar	@r3	,@r3		; insure correct outgoing parity now
	calls	binwri	,<r3,#1,#lun.ti>; the data is for the remote system
	tst	duplex			; full or half duplex connection?
	beq	60$			; full
	calls	binwri	,<r3,#1,#lun.co>; half, send it back please
60$:	mov	sp	,r2		; flag that no wait is needed
70$:	tst	r2			; should we take a short nap ?
	bne	80$			; no
	cmpb	exectype(r4),#sy$rt	; don't wait if rt11
	beq	80$			; yep, it's rt11 today
	calls	suspend	,<#1,#0>	; yes, sleep 1 second for RSTS/E
80$:	jmp	20$			; and back for more


		
90$:	direrr	r0			; problems in getting the connection

100$:	tst	(sp)+			; pop the one byte buffer and exit
	calls	ttyfin	,<#ttcons,#lun.co>	
	calls	ttyfin	,<#ttname,#lun.ti>
	.iif ne,r$rt11	,call	conrst	; for RT11 control C junk
	add	#locsiz	,sp		; pop local r/w data
	return				; bye




	.sbttl	errchk	check for allowable errors on the link



errchk:	mov	r0	,-(sp)		; save the error code
	cmpb	r0	,#nodata	; check error codes out please
	beq	210$			; that is what we wanted (no data)
	tst	r0			; any other kind of error ?
	beq	210$			; no
	calls	syserr	,<r0,#errtxt>	; yes, get the error text please
	strlen	#errtxt			; ok
	add	#errtxt	,r0		; get the length, point to the end
	movb	#cr	,(r0)+		; and stuff a carriage return and
	movb	#lf	,(r0)+		; a line feed in
	clrb	@r0			; insure .asciz please
	strlen	#errtxt			; the length once again
	calls	binwri	,<#errtxt,r0,#lun.co> ; try to print error on TI
	tst	r0			; did that also fail ?
	beq	210$			; no
	mov	@sp	,r0		; yes
	direrr	r0			; yes, print on default TI: lun
210$:	mov	(sp)+	,r0		; pop saved r0 and exit
	return				; bye

	global	<errtxt	,lun.co	,nodata>




	.sbttl	concmd	terminal emulation escape commands

concmd::save	<r1>
	bicb	#^C177	,r1
	scan	r1,#200$		; look for a match here
	tst	r0			; if no match, return 0
	beq	100$			; ok
	asl	r0			; word offsets
	jsr	pc	,@210$(r0)	; dispatch to the correct routine
	tst	r0			; if not set then set to -1
	bne	100$			; already set
	dec	r0			; set to -1
100$:	unsave	<r1>			; pop r1 and exit
	return


200$:	.byte	'C&137	,'c!40		; drop connection ctrl \ C
	.byte	'I&137	,'i!40		; init the line
	.byte	'Q&137	,'q!40		; quit logging but leave file open
	.byte	'R&137	,'r!40		; resume logging if file is open
	.byte	'X&137	,'X!40		; control Q and then thats all
	.byte	'B&137	,'b!40
	.byte	'?	,177		; help, rub for send break
	.byte	'H&137	,'h!40
	.byte	0
	.even

210$:	.word	con.$			; unknown escape command
	.word	con.c	,con.c		; drop connection
	.word	con.i	,con.i		; get modems attention
	.word	con.q	,con.q		; turn console logging off
	.word	con.r	,con.r		; turn it back on please
	.word	con.x	,con.x		; send XON
	.word	con.br	,con.br		; break
	.word	con.hl	,con.br		; print out commands
	.word	con.hl	,con.hl		; help



con.$:	calls	binwri	,<#200$,#1,#lun.co>	; beep at user
	clr	r0
	return

200$:	.byte	'G&37			; [BEL]
	.even


con.c:	mov	#1	,r0		; set flag to exit connect code
	return				; simple


con.i:	calls	ttydtr	,<#ttname>	; try to force DTR up on the line
	clr	r0
	return


con.q:	bic	#log$co	,trace		; turn off console logging
	clr	r0
	return


con.r:	bit	#log$op	,trace		; if the file is open do it
	beq	100$			; no
	bis	#log$co	,trace		; yes, enable this
100$:	clr	r0
	return


con.x:	calls	binwri	,<#200$,#1,#lun.ti>; insure a control Q goes down
	calls	ttxon	,<#ttname>
	clr	r0
	return

200$:	.byte	'Q&37
	.even


con.br:	calls	senbrk	,<#ttname,#lun.ti>
	clr	r0
	return



con.hl::strlen	#200$
	calls	binwri	,<#200$,r0,#lun.co>
	clr	r0
	return

200$:	.ascii	/B	Try to send a  break to the  remote/<cr><lf>
	.ascii	/C	Connect back to the local Kermit-11/<cr><lf>
	.ascii	/I	Drop  and raise DTR (for RSTS only)/<cr><lf>
	.ascii	/Q	Quit console logging.   See SET LOG/<cr><lf>
	.ascii	/R	Resume console logging. See SET LOG/<cr><lf>
	.ascii	/X	Send XON and cancel any active XONs/<cr><lf>
	.asciz	/RUBOUT	Try to fake a break to the remote/<cr><lf>
	.ascii	/?	Print this message/<cr><lf>
	.byte	0
	.even




	.sbttl	dump i/o to a log file ?


dumplo:	bit	#log$co	,trace		; is this enabled ?
	beq	100$			; no
	bit	#log$op	,trace		; is it open
	beq	100$			; no
	save	<r0,r1>			; yes, save temps please
	mov	r1	,r0		; call simple version
	mov	#lun.lo	,r1		; unit number
	call	putcr0			; thats it folks
	unsave	<r1,r0>
100$:	return



sxon:	tst	conflow
	beq	100$
	calls	binwri	,<#xon,#1,#lun.ti>
100$:	return

sxoff:	tst	conflow
	beq	100$
	calls	binwri	,<#xoff,#1,#lun.ti>
100$:	return


	.save
	.psect	$pdata
xon:	.byte	'Q&37
xoff:	.byte	'S&37
	.even
	.restore



	global	<lun.lo	,putcr0	,trace	,conflow>



	.end
