	.TITLE usrsb - Secure CLI USER subroutine for BYE
	.IDENT /V1.5/

.REM |
*+	usrsb - Secure CLI USER subroutine for BYE

	File:usrsb.mac
	Version: V1.5
	Author:
	Last Edit: 30-SEP-1985 16:04:04 
	Last Update: 6-Aug-1985 08:51:12 
		     Alter tt's in JMBTBL list



 This subroutine is hooked into the BYE program, and is called when
 a user logs off the system. We use it to determine if the terminal
 being logged off should have a CLI other than MCR established for it.
 If this is the case, we issue a SCLI$ directive to accomplish the 
 switch. 


*-
|

;+
; CONDITIONALS
;
; if DBGMSG is defined, a message will be returned to BYE saying
; which tt we think we are working with. 
;
;-
;;dbgmsg = 0

;+
; LOCAL DATA
;
; 	THIS DATA PSECT CONTAINS A SIMPLE TABLE OF TERMINALS AND CLIS.
; 	IT IS TERMINATED BY A ZERO ENTRY. FORMAT IS
;		.WORD NUM	-- TT NUMBER
;		.RAD50 CLI	-- CLI NAME IN RAD50 
;				   *** MUST BE TWO WORDS ***
;
;	THE TABLE IS TERMINATED BY A ZERO WORD.
;	THE TABLE MAY BE TERMINATED BY SEVERAL ZERO WORDS, TO ALLOW ZAP
;	PATCHES. ADD WORDS IN MULTIPLES OF 3 WORDS, AND MAKE SURE TO 
;	LEAVE A ZERO TERMINATOR WORD. 
;	ZAP NOTE -- TO 'REMOVE' AN ENTRY, DO NOT ZERO THE TT NUMBER!
;	AS THIS WILL TERMINATE THE TABLE. DO ZERO THE CLI WORDS. WE
;	DETECT THIS AND IGNORE ENTRIES WITH NULL CLI NAMES.

	.MCALL R50$, SCLI$, DIR$
	.PSECT BYEDAT D,RO,LCL
;
; SIMPLE MACRO TO MAKE TABLE ENTRIES
;
	.MACRO ENT NUM,NAM
	.WORD NUM
	R50$ NAM
	.ENDM

JMBTBL:: 
	ENT 6,SCLI			; TT6: BURNS HARBOR MODEM
	ENT 0
	ENT 0
	.WORD 0		; TERMINATE WITH ZERO WORD!!!
	
	.even
DMSG:	.ascii	/WHATT == terminal number is /
TTN:	.blkb 6		; space for tt number
	.word 0
	.even
SCLI:	SCLI$

	.PSECT 


;+
; ***-$USRSB - USER HOOK TO BYE
;		DETERMINE TT NUMBER, MATCH AGAINST INTERNAL TABLE
;		IF MATCH FOUND, ISSUE SCLI$ TO RESET TT CLI TO
;		THAT IN TABLE. ELSE DO NOTHING.
;
; INPUT 
; 	R3 -> UCB OF TI: TERMINAL
;
; OUTPUT
;	CC -- SUCCESS
;	CS -- SCLI$ FAILED, BYE WILL LEAVE TT SLAVED
;		THIS REQUIRES MOD TO BYE.MAC
;
;	ALL REGS USED
;
;-
 
$USRSB::CALL $WHATT	; GET TT NUMBER IN R0
	MOV #JMBTBL, R1	; POINT TO TT TABLE
10$:	TST (R1)	; AT END?
	BEQ 50$		; BR IF SO
	CMP R0,(R1)+	; MATCH? ( AND SKIP UNIT NUMBER)
	BEQ 20$		; YES - SKIP
	ADD #4, R1	; SKIP CLI NAME
	BR 10$		; AND KEEP LOOKING

; WE HAVE A MATCH, MOVE tt number, CLI NAME FROM TABLE TO DPB, 
; THEN EXECUTE IF CLI NAME IN TABLE NULL, IGNORE.
; NOTE, ERRORS ARE IGNORED, EXCEPT THAT THE TT ENDS UP SLAVED

20$:	MOV (R1), R2	; CHECK FOR NULL NAME
	BIS 2(R1), R2	; CHECK BOTH WORDS
	BEQ 50$		; NULL NAME, BR

	.if df dbgmsg
	mov	#dmsg, r0
	.iff
	clr	r0
	.endc

	MOV #SCLI, R2	; POINT TO DPB
	MOV (R1)+, S.CICN(R2)	; STUFF CLI NAME INTO DPB
	MOV (R1)+, S.CICN+2(R2)	; ..BOTH WORDS
	DIR$ #SCLI
	BR 100$		; EXIT, TAKING CARRY ALONG WITH

50$:
	.if df dbgmsg
	mov #dmsg, r0
	.iff
	clr r0
	.endc
	CLC		; SHOW SUCCESS
100$:	RETURN	


;+
; *** - $WHATT - COMPUTE TERMINAL NUMBER (TTxx:) GIVEN UCB
;
; INPUT:
;
;	R3 - ADDRESS OF UCB
;
; OUTPUT: R0 - TT number
;	R1,R2 ARE USED AND MODIFIED
;
;-
$WHATT: MOV	U.DCB(R3),R2	; GET DCB ADDRESS
	MOV	R3,R0		; GET UCB ADDRESS
	SUB	D.UCB(R2),R0	; COMPUTE RELATIVE UCB ADDRESS
	MOV	D.UCBL(R2),R1	; GET UCB LENGTH
	CALL	$DIV		; COMPUTE RELATIVE UNIT NUMBER
	ADD	D.UNIT(R2),R0	; COMPUTE ABSOLUTE UNIT NUMBER
	BIC	#177400,R0	; CLEAR HIGH BYTE
	.ifdf dbgmsg
	mov	r0, -(sp)	; save number
	mov	r0, r1		; set up for $CBOMG
	clr	r2		; ..
	mov	#ttn, r0	; ...
	call 	$CBOMG		; convert to ascii
	mov	(sp)+, r0	; restore number
	.endc
	RETURN			; RETURN
	.END
