;<FOONEX>LOG2JS.MAC;32 18-Mar-81 21:00:19, Edit by MMCM
; ERJMP/ERCAL
;DSK:<FOONEX>LOG2JS.MAC;37 11-Aug-80 03:03:21, Edit by LOWE
; disgusting crock to hangup foo dls lines, pending full modem control
;DSK:<134-TENEX>LOG2JS.MAC;30  2-Jun-80 18:22:44, Edit by PETERS
; More LOGTOT bug fixes
;DSK:<134-TENEX>LOG2JS.MAC;29  2-Jun-80 16:09:59, Edit by PETERS
; Re-re-fix LOGTOT calling garbage
;DSK:<134-TENEX>LOG2JS.MAC;28 13-May-80 09:37:12, Edit by RKNIGHT
; No-oped 2nd call to LOGTOT in logout code.
;<134-TENEX>LOG2JS.MAC;27     5-Apr-80 07:41:57    EDIT BY LYNCH
; FIXED UP ACCOUNT PRINTOUT ON LOGOUT
;DSK:<134-TENEX>LOG2JS.MAC;26  4-Apr-80 22:01:19, Edit by RKNIGHT
; Brain damage in LOGACC fixed.
;DSK:<134-TENEX>LOG2JS.MAC;25  3-Apr-80 16:46:04, Edit by RKNIGHT
; Made account string world wonderful for CACCT and LOGIN.
;DSK:<134-TENEX>LOG2JS.MAC;24  1-Apr-80 20:16:10, Edit by RKNIGHT
; Fixed pie-group data update on logout.
;DSK:<134-TENEX>LOG2JS.MAC;22 31-Mar-80 13:29:30, Edit by RKNIGHT
; Made logout update ACCTPT, ACCTSR, ACCTSL ala SETACT for LOGJOB.
;DSK:<134-TENEX>LOG2JS.MAC;21 31-Mar-80 12:29:26, Edit by RKNIGHT
; Made login and change account update LLORCA table.
;DSK:<134-TENEX>LOG2JS.MAC;20 30-Mar-80 15:04:42, Edit by RKNIGHT
; Fixed bad check for numeric account in LGCJM0.
;DSK:<134-TENEX>LOG2JS.MAC;19 27-Mar-80 09:42:28, Edit by RKNIGHT
; Made node pickup at login 12 bits instead of losing 9.
;DSK:<134-TENEX>LOG2JS.MAC;18 26-Mar-80 16:50:44, Edit by RKNIGHT
; Made connect time calculation in LGCJM0 correct.
;DSK:<134-TENEX>LOG2JS.MAC;17 24-Mar-80 17:20:36, Edit by RKNIGHT
; Put in code to EFACT node number at login time.
;DSK:<134-TENEX>LOG2JS.MAC;16 14-Mar-80 16:28:50, Edit by PETERS
; Fix extern problems with NWDGRP and TTYCHS
;DSK:<134-TENEX>LOG2JS.MAC;15  5-Mar-80 18:16:45, Edit by RKNIGHT
; Made new FACT file changes.
;<134-TENEX>LOG2JS.MAC;12    12-Nov-79 19:00:39    EDIT BY LYNCH
; FIXED TERRIBLE CROCK (OR INVENTED ONE?) IN DATAC1 FOR DETECTING
; IF JOB IS STILL AROUND.
;<134-TENEX>LOG2JS.MAC;11    25-Sep-79 09:47:35    EDIT BY LYNCH
; TOOK OUT ZEROING OF DSHARE AT LOGOUT TIME.   HELPS GET JOBS OUT FASTER.
;<134-TENEX>LOG2JS.MAC;10    13-Aug-78 14:41:37    EDIT BY PETERS
;<134-TENEX>LOG2JS.MAC;9    10-Jul-78 13:20:35    EDIT BY PETERS
;<134-TENEX>LOG2JS.MAC;8    14-SEP-76 20:57:37    EDIT BY GEOFF
; Added some lower caseing.
;<134-TENEX>LOG2JS.MAC;7    18-APR-76 19:27:20    EDIT BY LYNCH
; CHANGED LOGOUT MESSAGE FROM "KILLED" TO "LOGOUT"
;<134-TENEX>LOG2JS.MAC;3    17-FEB-76 11:00:33    EDIT BY UNTULIS
;ELIMINATED HANGUP CODE BECAUSE WE DON'T HAVE THAT CAPABILITY
;<134-TENEX>LOG2JS.MAC;2     9-FEB-76 15:12:27    EDIT BY UNTULIS
;ADDED LOCKPG,UNLKPG CODE
;<135-TENEX>LOG2JS.MAC;17    12-DEC-75 10:52:50    EDIT BY PLUMMER
; DELETE UNUSED EXTERN OF CPUSED
;<134-TENEX>LOG2JS.MAC;16    16-SEP-75 18:47:20    EDIT BY CLEMENTS
; FIX TO PREVIOUS FIX OF DATAC1
;<134-TENEX>LOG2JS.MAC;15    16-SEP-75 09:29:56    EDIT BY ALLEN
;<134-TENEX>LOG2JS.MAC;14    15-SEP-75 15:45:28    EDIT BY CALVIN
; FIXED OVER-POPPING STACK PROBLEM IN DATAC1
;<134-TENEX>LOG2JS.MAC;13    28-AUG-75 13:00:19    EDIT BY ALLEN
; CORRECTION FOR NEW PIE-SLICE CPU TIME UPDATE SCHEME
;<134-TENEX>LOG2JS.MAC;12    28-AUG-75 11:12:13    EDIT BY PLUMMER
; BREAK ADVICE LINKS ALONG WITH NORMAL LINKS
;<134-TENEX>LOG2JS.MAC;11    27-AUG-75 10:48:43    EDIT BY ALLEN
; REPAIR INCORRECT MAINTENANCE OF PIE-SLICE GROUP CPU TIME
;<134-TENEX>LOG2JS.MAC;10     8-AUG-75 19:15:12    EDIT BY CLEMENTS
; Fix typo in call to CPYFU1 for password in .crjob
;<134-TENEX>LOG2JS.MAC;9     4-JUN-75 12:01:39    EDIT BY CLEMENTS
; ADD WORD LEFT OUT OF EFACT ON PROXY ATTACH
;<134-TENEX>LOG2JS.MAC;8    29-MAY-75 11:20:29    EDIT BY CLEMENTS
; ADD CODE FOR EFACT AND LOGTTY ON EXPANDED ATACH JSYS
;<134-TENEX>LOG2JS.MAC;7    28-MAY-75 12:10:00    EDIT BY CLEMENTS
; MOVE CALLS TO TTHNGU OUT OF NOSKED CODE (BUG - TTHNGU CAN DISMISS)
;<134-TENEX>LOG2JS.MAC;6    16-MAY-75 17:12:42    EDIT BY CLEMENTS
; BUG FIX IN ATACH AND PROXY ATT/DET
;<134-TENEX>LOG2JS.MAC;5     5-MAY-75 16:26:30    EDIT BY CLEMENTS
; ADD B12 COMMENT ON CRJOB JSYS. CODE ACTUALLY IN JOBINI.
;<134-TENEX>LOG2JS.MAC;4     2-MAY-75 17:58:50    EDIT BY CLEMENTS
;ADDING PROXY ATTACH AND DETACH. ALSO MAKE MESSAGES BE SWAPPABLE, NOT
; LITERALS WHERE POSSIBLE
;<134-TENEX>LOG2JS.MAC;3    28-APR-75 12:12:13    EDIT BY CLEMENTS
;<134-TENEX>LOG2JS.MAC;2    24-APR-75 12:36:27    EDIT BY CLEMENTS
;<134-TENEX>LOG2JS.MAC;1    23-APR-75 15:54:48    EDIT BY CLEMENTS
;SEPARATING SWPMON INTO FINITE SIZED PIECES. THIS IS LOG2JS.MAC

	SEARCH PROLOG
	TITLE LOG2JS
	SWAPCD

INTERNAL	.LGOUT,LOGO,LOGTOT,FLOGO,XEFACT,LOGCJM
INTERNAL	.DTACH,.ATACH,.CRJOB,LOGONM
INTERNAL	LOGFCT,LOCKPG,UNLKPG

EXTERNAL	BHC,MENTR,MRETN,MRETNE,R,MSTKOV
EXTERNAL	ACCIFG,BUGCHK,BUGHLT,CAPENB,CAPMSK,CLRLFK
EXTERNAL	CRJONJ,CRJAC1,CRJOJC,CRJUSR,CRJPSW,CRJACT,CRJFIL
EXTERNAL	CRJEVO,CRJFAC,CRJTTY,CRJEXF,CRJPJF
EXTERNAL	DTIALL,FKDIR,FKINT,FKPGS,FLGLCK,FORKX,HALTF1
EXTERNAL	HLTJB,JB0TT,JOBDIR,JOBONT,JOBPT,JOBRT,LLGLCK
EXTERNAL	LOGDES,LOGLCK,PASSWC,PSICH,PSILOB,PSIR4
EXTERNAL	SETMPG,SKMRTN,TTDPSI,TTFORK,TTPSI,TTPSRQ
IFN PIESLC,<
EXTERNAL	NJBGRP,PIEGRP,DSHARE,GRPLOK,ASGDSH
>

;LOGOUT

.LGOUT:	JSYS MENTR
	NOINT
	CAME 1,MINUS1##		;SELF?
	JRST ELOGO		;NO
LOGO::	SKIPE FORKN		;TOP FORK?
	JRST [	MOVSI 1,LOG
		TDNN 1,CAPMSK	;LOG PERMITTED?
		JRST LOGOE1	;NO, ERROR RETURN
		CALL SKTGRP	;EXECUTING FORK IN TOP GROUP
		RETERR FRKHX7	;NO, ILLEGAL LGOUT ATTEMPT
		HLRZ 1,FORKN
		HRRZ 2,SYSFK(1)	;2=SYS FK INDEX OF TOP FORK IN JOB
		NOSKED
		MOVSI 1,400000+PSILOB
		IORM 1,FKINT(2)	;REQUEST LOGOUT
		CALL PSIR4
		OKSKED
		RESKD1
		OKINT
		JRST HALTF1]	;HALT THIS ONE
FLOGO1:				;LOGOUT PSI REQUEST ENTERS HERE
IFDEF DSPCHN,<
	SETO 1,
	RELDP
	RELDC>
IFDEF IMPCHN,<
	SETO 1,
	JSYS 753>
	MOVEI 1,-4
	KFORK			;DELETE FORKS
	SETO 1,			;CLEAR USER MAP OF TOP FORK
	MOVSI 2,400000
	PMAP
	ADDI 2,1
	TRNN 2,1000
	JRST .-3
	SKIPL CTRLTT
	RESET			;RESET TTY MODES, ETC.
	MOVEI 1,400000		;THIS FORK
	HRLI 1,(1B5)		;INSIST ON CLOSING
	CLZFF
;FALL THRU

;FALLS THRU
	MOVE 1,JOBNO
	HLRZ 1,JOBDIR(1)	;CONNECTED DIRECTORY
	DELDF			;DELETE DELETED FILES
	MOVEI 1,400000
	SETO 2,
	DIC			;DEACTIVATE INTERRUPTS AND KEYS
	CALL DTIALL
	MOVSI 3,-NJOBS		;LOG OUT ANY JOBS WHICH I OWN
SJLGO1:	HRRZ 2,JOBONT(3)	;GET AN OWNER
	CAME 2,JOBNO		;IS IT ME?
	JRST SJLGO2		;NO
	MOVEI 1,(3)		;YES. NOW LOG IT OUT
	LGOUT			; ..
	  JFCL			;COULDN'T?!?
	SETOM JOBONT(3)		;AND MAKE ME NOT OWN IT
SJLGO2:	AOBJN 3,SJLGO1		;CHECK ALL JOBS
SJLGO3:	MOVE 1,JOBNO		;NOW SEE IF I AM OWNED
	MOVE 2,JOBONT(1)	; ..
	CAMN 2,MINUS1		; ..
	JRST SJLGO4		;NO, I CAN LOG OUT OK
	MOVSI 3,(1B1)		;I HAVE AN OWNER. SET BIT SO HE KNOWS
	IORM 3,JOBONT(1)	; I AM TRYING TO LOG OUT
	JRST SJLGO5		;GO TO RESIDENT CODE

RESCD

SJLGO5:	MOVEI 3,^D31		;CAUSE A CODE 31 TERMINAL INTERRUPT
	HRRE 2,JOBPT(2)		;IN TOP FORK OF OWNING JOB
	JUMPGE 2,[NOSKED	;IF THERE IS STILL ONE
		CALL TTPSR1##	;TTY PSI ROUTINE IN SCHEDULER
		OKSKED		; ..
		JRST .+1]	; ..
	MOVEI 1,^D5000		;WAIT FOR OWNER TO FREE ME
	DISMS
	JRST SJLGO3		;SEE IF I CAN GO YET
SWAPCD

SJLGO4:	MOVE 2,JOBNO
	HRRZ 1,JOBDIR(2)
;;;	JUMPE 1,LOG1		;IF JOB NOT LOGGED IN

;;;	CALL GTLGTT		;GET THE LOGGING TTY
;;;	CALL LOGTOT		;SUMMARY TO LOG TTY
;;;	CALL RLLGTT		;RELEASE LOG TTY
;;;	CALL LGOFCT		;MAKE LOGOUT FACT FILE ENTRY
LOG1:
IFN PIESLC,<
	MOVE 1,JOBNO
	MOVE 2,PIEGRP(1)
	SOS NJBGRP(2)
	CALL UPDPIE##		;UPDATE PIE-SLICE CPU TIME
	CALL ASGDSH		;RECOMPUTE DSHARE VALUES

>;END PIE-SLICE SCHEDULER CONDITIONAL

	HRRE 1,CTRLTT
	JUMPL 1,LOG2		;CONTROL TTY?
	MOVSI 1,(1B0)		;YES. BREAK ADVICE LINKS
	ADVIZ
	 JFCL
	HRLOI 1,(1B0+1B1+1B4+1B5)	;AND BREAK LINKS
	MOVEI 2,-1
	TLINK			;BREAK LINKS, SET ACCEPT
	JFCL
	HRRZ 1,CTRLTT
	MOVEI 1,400000(1)	;NOW PRINT LOGOUT MESSAGE
	SKIPA			;WE HAVE A REAL TTY TO OUTPUT TO
LOG2:	MOVEI 1,377777		;NIL DESIGNATOR IF DETACHED
	CALL LOGTOT
	CALL LOGCR
	MOVE 1,JOBNO		;GET JOB NUMBER
	HRRZ 1,JOBDIR(1)	;GET LOGIN DIRECTORY NUMBER
	JUMPE 1,HLTJB		;NO FACT ENTRY IF NOT LOGGED IN
	CALL LGOFCT		;ELSE MAKE LOGOUT FACT ENTRY
	CALL FDLSHU		;hangup foodls
	JRST HLTJB

repeat 1,<
fdlshu:				;PURGE!!! this crock is for hanging up FooDLS
	push p,1
	push p,2
	move 2,ctrltt		; get tty line number
	jumpl 2,fdlsh1		; if detached, then ignore
	caile 2,nttys-1		; if not hardware tty
	 jrst fdlsh1		;  then ignore
	hrlz 2,2		; get tty line number
	cono 374,2	
	movei 1,1000		; wait
	disms
	hrri 2,247		; form line number,,247
	cono 374,2		; reactivate the line
fdlsh1:
	pop p,2
	pop p,1
	popj p,>

ELOGO:	MOVE 2,JOBNO		;GIVEN JOB NUMBER TO LOGOUT
	CAIN 2,0(1)		;SELF?
LOGOE1:	RETERR LOUTX1		;YES, CAN'T SAY IT THAT WAY
	CAIGE 1,NJOBS		;REASONABLE NUMBER?
	CAIGE 1,0
LOGOE2:	RETERR LOUTX2		;NO
	SKIPGE JOBRT(1)		;EXISTS?
	JRST LOGOE2
	HRRZ 3,JOBDIR(1)	;LOGIN DIRECTORY OF OBJECT JOB
	XOR 3,JOBDIR(2)
	TRNN 3,-1		;SAME AS THIS JOB?
	JRST ELOGO1		;YES, THAT'S OK
	HRRZ 3,JOBONT(1)	;OWNER OF OBJECT JOB
	CAMN 2,3		;IS IT ME?
	JRST ELOGO1		;YES. I CAN LOG IT OUT.
	MOVEI 3,WHEEL+OPER+MAINT	;OTHERWISE MUST BE WHEEL
	TDNN 3,CAPENB
	JRST LOGOE1		;ILLEGAL
ELOGO1:	SETOM JOBONT(1)		;MAKE IT UN-OWNED, SO IT CAN GO AWAY
	HRRZ 2,JOBPT(1)		;TOP FORK OF OBJECT JOB
	NOSKED
	MOVSI 1,400000+PSILOB
	IORM 1,FKINT(2)		;REQUEST LOGOUT
	CALL PSIR4
	OKSKED
	RESKD1
	AOS 0(P)		;RETURN GOOD
	JRST MRETN

FLOGO:	SKIPGE SLOWF		;ALREADY IN JSYS?
	JSYS MENTR		;NO, DUMMY UP ENTRY
	NOINT
	MOVE P,UPP		;MAKE SURE WE HAVE EMPTY STACK
	ADD P,BHC+2
	JRST FLOGO1

;LOGGING FUNCTIONS

;TYPE LOGOUT MESSAGE, DESTINATION DESIGNATOR IN 1

LOGTOT:	HRROI 2,LGTMS1		;TAG THIS CONSTANT - MAKE SWAPPABLE
	CALL LOGMES
	CALL LOGJOB		;PRINTS JOB NO, TTY NO, TIME
	HRROI 2,LGTMS2
	CALL LOGMES
	PUSH P,1
	Move	1,Jobno		;Get TTY character count.
	Move	1,TTYCHS##(1)
	Movem	1,5(7)		;Stash in Fact entry.

	MOVNI 1,5		;FOR WHOLE JOB
	RUNTM
	MOVEM 1,3(7)		;RUNTIME FOR EFACT
	MOVEM 3,4(7)		;CONSOLE TIME FOR EFACT

	Move	2,Jobno		; Update group data.
	Move	2,Piegrp##(2)
	Imuli	2,Nwdgrp
	Skipe	Primet##
	Addm	3,Ctunpt(2)
	Skipn	Primet
	Addm	3,Ctused(2)

	MOVE 2,1
	POP P,1
	PUSH P,3
	CALL LOGTIM		;PRINT CPU TIME
	HRROI 2,LGTMS3
	CALL LOGMES
	POP P,2
	CALL LOGTIM		;PRINT CONSOLE TIME
	RET
LGTMS1:	ASCIZ /Logout/
LGTMS2:	ASCIZ /
  Used /
LGTMS3:	ASCIZ / in /

;POINTERS TO FACT FILE HEADER
LFCTBP:	POINT 9,0(7),8		;POINTER TO TYPE CODE OF EFACT BUFFER
LFCTP2:	POINT 9,0(7),17		;POINTER TO JOB NUMBER IN EFACT BUFFER
LFCTP3:	POINT 12,0(7),29	;POINTER TO TTY NUM

; MAKE LOGOUT FACT ENTRY
LGOFCT:	MOVEI 2,141		;LOGOUT CODE
	DPB 2,LFCTBP
	MOVSI 1,-6		;SIZE OF LOGOUT BLOCK
; FALL INTO LOGFCT

;FALLS THRU FROM ABOVE

; MAKE LOG TYPE FACT FILE ENTRY (LOGOUT, LOGIN, ATACH, DETACH ...)

LOGFCT:	HRRI 1,0(7)		;LOC OF LOGOUT BLOCK
	NOINT
	PUSH P,CAPENB
	MOVEI 2,OPER		;TEMP CHANGED FROM MOVSI 2,LOG
	IORM 2,CAPENB		;BE SURE WE HAVE LOG CAPABILITY
	EFACT
	 JSP 2,XEFACT		;CHECK EFACT FAILURE (ALWAYS SKIPS)
	  SETOM ACCIFG		; IF SUCCESS, ASSUME SUCCESS HENCEFORTH
	POP P,CAPENB		;RESTORE CAPS
	OKINT
	RET

;CHECK EFACT FAILURE

XEFACT:	SKIPE ACCIFG		;ACCOUNTS SUPPOSEDLY INITIALIZED?
	BUG(CHK,<EFACT FAILURE>)
	JRST 1(2)		;NO, ALLOW EFACT FAILURE

;LOGON MESSAGE, CALLED FROM .LOGIN

LOGONM:	CALL GTLGTT		; GET LOGGING TTY
	HRROI 2,LGNMS1
	CALL LOGMES
	CALL LOGJOB
	CALL RLLGTT		; RELEASE LOGGING TTY

	Push	P,2
	Move	1,Ctrltt##
	Setzm	2
	Skipl	1
	Move	2,Tynods##(1)	
	Tdz	2,[-1,,770000]
	Movem	2,3(7)
	Pop	P,2

	MOVEI 1,501		;LOGIN CODE
LOGCJ1:	DPB 1,LFCTBP
	MOVE 1,4(7)		;ACCOUNT WORD
	JUMPL 1,LOGCJ2		;JUMP IF STRING ACCOUNT
	MOVSI 1,-5		;WORD COUND INCLUDES BLOCK ONLY
	JRST LOGFCT
LOGCJ2:	SUBI 1,4		;STRING ACCOUNT. ROUND CHAR COUNT
	IDIVI 1,5		;COMPUTE WORD COUNT
	MOVSI 1,-5(1)		;ADD WORD COUNTS FOR STRING AND BLOCK
	JRST LOGFCT

LGNMS1:	ASCIZ /Login/

;CHANGED ACCOUNT NUMBER MESSAGE

LOGCJM:	CALL GTLGTT		; GET LOGGING TTY
	HRROI 2,LGCMS1
	CALL LOGMES
	CALL LOGJOB
	HRROI 2,LGTMS2
	CALL LOGMES
	PUSH P,1
	MOVNI 1,5
	RUNTM			;RUNTIME OF JOB
	MOVEM 1,3(7)		;SAVE FOR FACT FILE
	MOVE 2,1
	POP P,1
	CALL LOGTIM		;PRINT ON LOG TTY
	CALL RLLGTT		; RELEASE LOGGING TTY
	Popj	P,

LGCMS1:	ASCIZ /Changed/

LGCJM0::
	Umove	B,2		;Get user's 2.
	Tlnn	B,200000	;Did he want messages?
	Jrst	Messages	;Yes.
	Move	B,Capenb	;Is he privied?
	Trnn	B,Wheel!Oper	;Hmmm?
Messages:
	Call	Logcjm		;Nope...give him messages.

	MOVEI 7,LOGBUF			; PLACE FOR EFACT ENTRY
	MOVEI 2,502			; CHANGE ACCOUNT ENTRY
	DPB 2,LFCTBP		;STORE TYPE CODE
	MOVE 2,JOBNO
	DPB 2,LFCTP2
	MOVE 2,CTRLTT			; CONTROLLING TTY
	DPB 2,LFCTP3
	MOVE 2,JOBNO
	HRRZ 2,JOBDIR(2)		; DIRECTORY #
	HRRM 2,1(7)
	GTAD
	MOVEM 1,2(7)
	Move	B,Jobno
	Movem	A,LLORCA##(B)	; Save this.
	MOVNI 1,5
	RUNTM				; GET JOB'S RUNTIM
	MOVEM 1,3(7)
	Time			;Get time since last reload.
	Sub	1,Consto	;Convert to connect time.
	Push	P,3		;Save it.
	Idivi	B,^D1000	;Set divisor for ms.
	Idiv	A,B		;Convert connect time to ms.
	Movem	1,4(7)		;Stash in Fact file.
	Move	2,Jobno		;Get TTY chars.
	Move	3,Piegrp##(2)	;Get pie slice group.
	Imuli	3,Nwdgrp	;Make it an index.
	Skipe	Primet##	;Prime time?
	Addm	1,Ctunpt##(3)	;No...stash non-prime figure.
	Skipn	Primet		;Prime time?
	Addm	1,Ctused##(3)	;Yes.
	Pop	P,3		;Reincarnate 3.
	Move	1,TTYCHS(2)
	Movem	1,5(7)		;Stash in Fact file.
	MOVE 2,ACCTPT			; GET ACCOUNT DESIGNATOR
	HLRZ 3,2			; SEE IF NUMERIC ACCOUNT
	ANDI 3,(7B2)			; ..
	CAIE 3,500000			; NUMERIC ACCOUNT?
	 JRST LGCJM1			; NO, A STRNG ONE
	TLZ 2,700000			; YES, KILL BITS 0,1,2
	MOVEM 2,6(7)			; INTO EFACT BLOCK
	MOVSI 1,-7			; SIZE OF THIS ENTRY
	JRST LOGFCT			; LOG IT
LGCJM1:	SETZ 4,				; FOR COUNTING
	Move	1,[Point 7,7(7)] 	; Where string goes.
Lgcjm2:
	ILDB 3,2
	JUMPE 3,Lgcjm3			; IF DONE
	Idpb	3,1
	SOJA 4,Lgcjm2			; IF NOT
Lgcjm3:
	MOVEM 4,6(7)			; - # OF CHARS IN STRING
	SUBI 4,4			; ROUND UP(?) TO NEXT WORD
	IDIVI 4,5			; # OF WORDS
	MOVSI 1,-7(4)			; TOTAL SIZE OF BLOCK
	JRST LOGFCT			; LOG THE ENTRY

;PRINT TIME FROM 2

LOGTIM:	JUMPL 1,R
	IDIVI 2,^D1000		;MS TO SECS
	IDIVI 2,^D60		;PEEL OFF SECONDS
	PUSH P,3
	IDIVI 2,^D60		;PEEL OFF MINUTES
	PUSH P,3
	CALL LOGNUM		;PRINT HOURS
	MOVEI 2,":"
	BOUT
	POP P,2
	CALL LOGNUM		;PRINT MINUTES
	MOVEI 2,":"
	BOUT
	POP P,2
	CALL LOGNUM		;PRINT SECONDS
	RET

;ATACH AND DETACH MESSAGES

DETMSG:	CALL GTLGTT		; GET LOGGING TTY
	HRROI 2,ATAMS3		;TEXT FOR LOG TTY
	CALL LOGMES
	CALL LOGJOB
	CALL RLLGTT		; RELEASE LOGGING TTY
	MOVEI 1,143		;DETACH CODE
ATAMS2:	DPB 1,LFCTBP
	MOVSI 1,-3		;SIZE OF BLOCK
	JRST LOGFCT

ATAMS3:	ASCIZ /Detached/
ATAMS4:	ASCIZ /Attached job /
ATAMS5:	ASCIZ / to TTY /

ATAMSG:	MOVEI 7,LOGBUF
	CALL GTLGTT		; GET LOGGING TTY
	HRROI 2,ATAMS4
	CALL LOGMES
	HLRZ 2,0(7)		;GET JOB NUMBER
	CALL LOGNUM		;PRINT IT
	HRROI 2,ATAMS5
	CALL LOGMES
	LDB 2,LFCTP3		;TTY NUMBER
	MOVEI 3,^D8		;PRINT IN OCTAL
	CALL LGNOUT
	CALL LOGJ2		;PRINT AND STORE TOD
	CALL RLLGTT		; RELEASE LOGGING TTY
	MOVEI 1,142		;ATACH CODE
	JRST ATAMS2

;PRINTS  JOB 1, TTY 40, AT 12:34:56
;  OR    JOB 1, DETACHED, AT 12:34:56

LOGJOB:	MOVEI 7,LOGBUF		;BLOCK FOR EFACT
	HRROI 2,LGJMS1
	CALL LOGMES
	MOVE 2,JOBNO
	DPB 2,LFCTP2		;JOB NUMBER FOR EFACT
	CALL LOGNUM
	MOVE 2,JOBNO
	HRRZ 2,JOBDIR(2)
	JUMPE 2,LOGJ3		;LOGGED IN NOW?
	HRROI 2,LGJMS2
	CALL LOGMES
	MOVE 2,JOBNO
	HRRZ 2,JOBDIR(2)
	HRRM 2,1(7)		;DIRECT NUMBER FOR EFACT
	CAIL 1,0
	DIRST			;DIRECTORY NAME
	  JFCL
	HRROI 2,LGJMS3
	CALL LOGMES
	MOVE 2,ACCTPT
	CALL LOGACC
LOGJ3:	MOVE 2,CTRLTT
	DPB 2,LFCTP3		;TTY NUMBER TO FACT FILE
	JUMPL 2,LOGJ1		;IF NO CTRL TTY (DETACHED JOB)
	HRROI 2,LGJMS4
	CALL LOGMES
	MOVE 2,CTRLTT
	MOVEI 3,^D8		;PRINT TTY NUMBER IN OCTAL
	CALL LGNOUT
LOGJ2:	HRROI 2,LGJMS5
	CALL LOGMES
	PUSH P,1		;SAVE DESIGNATOR
	GTAD
	MOVEM 1,2(7)		;TOD FOR EFACT
	MOVE 2,1
	POP P,1			;RESTORE DESTINATION
	JUMPL 1,R
	MOVSI 3,045240		;MOST ABBREVIATED DATE AND TIME
	CAIL 2,0
	ODTIM
	RET

LOGJ1:	HRROI 2,LGJMS6
	CALL LOGMES
	JRST LOGJ2

LGJMS1:	ASCIZ / Job /
LGJMS2:	ASCIZ /, User /
LGJMS3:	ASCIZ /, Acct /
LGJMS4:	ASCIZ /, TTY /
LGJMS5:	ASCIZ /, at /
LGJMS6:	ASCIZ /, detached/

LOGACC:	HLRZ 3,2
	ANDI 3,(7B2)
	CAIE 3,500000		;STRING OR NUMBER?
	JRST LOGAC1		;STRING
	TLZ 2,700000
	MOVEM 2,4(7)		;ACCOUNT NUMBER FOR EFACT
	JRST LOGNUM		;PRINT NUMBER

LOGAC1:	SETZ 4,			;TO COUNT CHARS
	Push	P,1
	Move	A,[Point 7,5(7)]
	MOVE 3,2		;STRING PTR
LOGAC2:	ILDB 2,3		;CHAR OF ACCOUNT STRING
	JUMPE 2,LOGAC3		;NULL IS END OF STRING
	Idpb	2,1		; Stash char in fact file.
	EXCH 1,(P)		; GET LOG JFN BACK
	CAIL 1,0		; DETACHED, MAYBE?
	 BOUT			; NO, SO PRINT IT ALSO
	EXCH 1,(P)		; GET BACK STRING POINTER...
	AOJA 4,LOGAC2		;COUNT IT

Logac3:
	Pop	P,1

	MOVNM 4,4(7)		;LEAVE NEG OF WORD COUNT FOR EFACT
	RET

LOGMES:	SETZ 3,
	JUMPL 1,R
	SOUT
	RET

LOGNUM:	MOVEI 3,^D10		;DECIMAL FOR TIMES, ETC.
LGNOUT:	JUMPL 1,R
	NOUT
	BUG(CHK,<LGNOUT - NOUT FAILED>)
	RET

LOGCR:	MOVEI 2,15
	JUMPL 1,R
	BOUT
	MOVEI 2,12
	BOUT
	RET

; LOCK UP LOGGING TTY TO PREVENT INTERMIXED MSGS

GTLGTT:	MOVE 1,LOGDES		; TRY TO SEPARATE TYPEOUT IF JOB0 AND
	CAMN 1,JB0TT		; LOGGING INFO, IF ON SAME TTY
	CAIN 1,377777		; DON'T DOBE ON NIL DEVICE - NOT NEEDED
	SKIPA			; NIL OR NOT SAME
	DOBE			; SAME AND NOT NIL. WAIT.
	NOINT
GTLGT1:	AOSN LOGLCK		; LOCK THE LOCK
	JRST GTLGT2		; OK
	MOVEI 1,LOGLCK
	PUSHJ P,DISL##
	JRST GTLGT1

GTLGT2:	MOVE 1,0(P)
	MOVEM 1,LLGLCK
	MOVE 1,FORKX
	MOVEM 1,FLGLCK
	MOVE 1,LOGDES
	PUSH P,2
	PUSH P,3
	MOVE 2,[BYTE (2)0,0,0,0,0,0,0,1,2,2,2,0,1,2,0,0,0,0]
	MOVE 3,[BYTE (2)0,0,0,0,0,0,0,0,0,0,0,0,0,2]
	SFCOC			; MAKE SURE NOBODY'S BEEN MUCKING WITH
	RFMOD
	TRO 2,100
	SFMOD			; MODE WORDS
	POP P,3
	POP P,2
	RET

; RELEASE LOGGING TTY

RLLGTT:	CALL LOGCR
	SETOM LOGLCK
	OKINT
	RET


;DETACH FROM CONTROLLING TTY

.DTACH:	JSYS MENTR
	NOINT
	SKIPGE CTRLTT		;IS ONE?
	JRST MRTNE1##		;NO, DO NOTHING
	CALL SKTGRP		;EX FORK IN TOP GROUP IN JOB?
	JRST MRTNE1		;NO, DTACH=NOOP
	MOVSI 1,(1B0)		;BREAK ALL LINKS
	ADVIZ
	 JFCL
	HRLOI 1,(1B0+1B1+1B4+1B5)
	MOVEI 2,-1
	TLINK			;BREAK LINKS
	JFCL
	MOVE 4,JOBNO
	HRRE 2,JOBDIR(4)
	CAIE 2,0		;LOGGED IN?
	CALL DETMSG		;YES, DO MESSAGE AND EFACT
	MOVE 2,CTRLTT
	call fdlshu
	SETOM CTRLTT
	SETOM TTFORK(2)
	SETZM TTPSI(2)
	MOVE 1,JOBNO
	HRROS JOBPT(1)
	cail 2,tymttl		;tymnet line?
	caile 2,tymtth		;true if in this range
	jrst mretn		;not tymnet - done
	call tyhngu##		;tymnet - call hang-up sequence
	JRST MRETN

;ATTACH JOB TO TTY

	EXTERN ATACX4

.ATACH:	JSYS MENTR
	NOINT
	HRRZS 1			;JOB NUMBER
	CAIL 1,NJOBS		;POSSIBLE NUMBER?
	RETERR ATACX1		;NO.
	TLNE 2,(1B2)		;REQUEST TO DETACH SOMEONE?
	JRST DATACH		;YES.
	TLNE 2,(1B1)		;REQUEST FOR PROXY ATTACH?
	JRST PATACH		;YES.
	PUSH P,1		;SAVE JOB NO
	CALL SKTGRP		;EXECUTING FORK IN TOP GROUP IN JOB?
	 RETERR FRKHX7
	UMOVE 2,2		; GET USER NUMBER
	MOVEI 1,WHEEL+OPER
	TDNE 1,CAPENB		;PRIVILEGED?
	JRST ATACH3		;YES, NO PASSWORD CHECK
	PUSH P,2		; SAVE USER NUMBER
	HRRZ 1,2
	UMOVE 2,3		; GET PASSWORD STRING POINTER
	CALL PASSWC		;CHECK PASSWORD
	 JRST [	MOVEI 1,ATACX4
		JRST MRETNE]
	POP P,2
ATACH3:	HRRZ 1,0(P)
	HRRZ 3,JOBDIR(1)	;DIRECTORY OF  OTHER JOB
	PUSH P,3		;SAVE FOR FACT FILE
	CAIE 3,0(2)		;SAME AS SPECIFIED?
	RETERR ATACX3		;NO
	SKIPGE CTRLTT		;THIS JOB NOT ATTACHED? OR
	RETERR ATACX2		;YES
	HRRZ 1,JOBPT(1)		;TOP FORK OF OTHER JOB
	HRRZ 1,FKPGS(1)		;GET PSB OF IT
	MOVE 2,[XWD RWX,FPBPGA]
	CALL SETMPG		;MAP IT
	LDB 1,[POINT 13,FPBPGA+JSBPG,26]	;GET JSB NO
	CALL SETMPG
	MOVEI 6,FPBPGA-JSB
	SKIPL 4,CTRLTT(6)	;OTHER JOB NOW UNATACHED?
	JRST [	NOSKED		;NO, MUST DETACH IT FIRST
		CALL DETPSI	;CAUSE PSI, DETACH AND SLOW HANGUP
		MOVE 1,-1(P)	;OTHER JOB NUMBER
		HRROS JOBPT(1)
		OKSKED
		JRST ATACH2]

ATACH2:	PUSH P,CTRLTT
	NOSKED
	SETOM CTRLTT
	MOVE 1,JOBNO
	HRROS JOBPT(1)		;DO THE DETACH PART
	POP P,5			;TTY
	DPB 5,[POINT 12,LOGBUF,29] ;TTY NUMBER FOR EFACT
	POP P,4			;OTHER JOB'S DIRECTORY NUMBER
	HRRM 4,LOGBUF+1		;PUT IN EFACT BLOCK
	POP P,4			;DESTINATION JOB NO
	HRLM 4,LOGBUF		;PUT IN EFACT BLOCK
	MOVEM 5,CTRLTT(6)	;SET CTRLTT IN DEST JOB
	HRLM 5,JOBPT(4)
	HRLM 4,TTFORK(5)
	MOVE 1,TTSPSI(6)
	MOVEM 1,TTPSI(5)
	MOVE 1,TTSDPS(6)	;RESTORE INT CODE WORDS TO TTY LINE
	MOVEM 1,TTDPSI(5)
	CALL CLRLFK
	OKSKED
	CALL ATAMSG		;DO LOGGING STUFF
	MOVE 1,JOBNO
	HRRE 1,JOBDIR(1)
	JUMPE 1,LOGO		;VANISH IF NOT LOGGED IN
	JRST SKMRTN		;RETURN SKIP


;COMMON PART OF DETACH CODE. CAUSE ^C PSI, HANGUP, DETACH.
; LINE IN 4, A JSB INDEXED BY 6

DETPSI:	MOVEI 2,0(4)		;COPY TTY NUMBER
	MOVE 3,PSICH+3		;SIMULATE A CONTROL C
	PUSH P,6		;SAVE CRUCIAL AC'S
	PUSH P,4		; ..
	CALL TTPSRQ##		;CAUSE THE PSI
	POP P,4			;RESTORE TTY NUMBER
	POP P,6			;RESTORE JSB INDEX
	SETZM TTPSI(4)		;CLEAR TTY WORDS
	SETZM TTDPSI(4)		; ..
	SETOM TTFORK(4)
	SETOM CTRLTT(6)
	POPJ P,0

;SKIP IF EXECUTING FORK IS IN TOP GROUP IN JOB
;CLOBBERS 1 AND 2

SKTGRP:	MOVE 1,FORKX		;CHECK THAT FORK IN TOP GROUP IN JOB
	SKIPGE FKDIR(1)
	HRRZ 1,FKDIR(1)		;1=SYS INDEX OF TOP FORK IN GROUP
	MOVE 2,JOBNO
	HRRZ 2,JOBPT(2)		;2=SYS INDEX OF TOP FORK IN JOB
	CAIN 1,0(2)		;SAME?
	AOS 0(P)		;YES.
	RET

;ROUTINE TO DETACH ANOTHER JOB. CALLED IF B2 ON IN AC2 OF CALLER

DATACH:	MOVE 3,JOBNO		;MY JOB NUMBER
	CAIN 3,(1)		;REQUEST TO DETACH MYSELF?
	JRST DATAC0		;YES. OK IF TO FORK GRP
	HRRZ 4,JOBONT(1)	;SEE IF I OWN THE JOB POINTED TO
	MOVEI 2,WHEEL+OPER	;OR IF I AM ENABLED
	CAIE 3,(4)		; ..
	TRNE 2,CAPENB		; ..
	JRST DATC0A		;OK TO DO IT
	RETERR CAPX1		;NO. ERROR.

DATAC0:	CALL SKTGRP		;AM I IN TOP GROUP?
	  RETERR FRKHX7		;NO.
	HRRZ 1,JOBNO		;GET BACK MY JOB NUMBER
DATC0A:	CALL DATAC1		;DETACH THE JOB IN 1
	  JRST ATX1R		;NO SUCH JOB
;NOW GENERATE THE EFACT AND LOG TTY INFO
	PUSH P,1		;JOB NUMBER WHICH WAS DETACHED
	PUSH P,4		;ITS OLD CTTY
	ADD P,BHC+3		;SPACE FOR THE EFACT ENTRY
	JUMPGE P,MSTKOV		;STANDARD DEFENSE MECHANISM
	MOVEI 7,-2(P)		;7 POINTS TO THE EFACT BLOCK AS USUAL
	HRLZM 1,0(7)		;STORE THE JOB NUMBER IN EFACT BLK
	HRRZ 2,JOBDIR(1)	;WHO IT WAS LOGGED IN AS
	MOVEM 2,1(7)		;TO EFACT
	DPB 4,LFCTP3		;STORE THE OLD CTTY IN EFACT BLK
	GTAD			;LOG THE TIME
	MOVEM 1,2(7)		; ..
	MOVEI 1,143		;CODE FOR DETACH
	DPB 1,LFCTBP		; TO HEADER WORD OF FACT BLK
	MOVSI 1,-3		;LENGTH OF BLK
	CALL LOGFCT		;INTO THE FACT FILE
	CALL GTLGTT		;GET THE LOGGING TTY
	HRROI 2,ATAMS3		;COMMENT DETACHED
	CALL LOGMES		; ..
	HRROI 2,LGJMS1		;COMMENT " JOB "
	CALL LOGMES
	HRRZ 2,-4(P)		;THE JOB NUMBER
	CALL LOGNUM
	CALL LOGJ2		;OUTPUT TAD, PUT IT IN EFACT BLK AGAIN
	CALL RLLGTT		;CRLF AND RELEASE THE TTY
	JRST SKMRTN		;SUCCESS RETURN

;ROUTINE TO DETACH JOB IN 1. B0 ON SAYS KEEP JSB MAPPED
;RETURNS OLD CTRLTT IN 4, PRESERVES 1.

DATAC1:	PUSH P,1		;SAVE JOB NUMBER AND FLAG
	PUSH P,ZERO##		; DUMMY ON THE STACK
	MOVE 2,[RWX,,FPBPGA]	;MAP IN THE JSB
	MOVEI 6,FPBPGA-JSB	;USE THIS AS INDEX TO MAPPED JSB
	NOSKED
	SKIPGE JOBRT(1)		;JOB STILL THERE?
	JRST DATAC3		;NO.
	AOS -2(P)		;YES. SKIP RETURN
	HRRZ 1,JOBPT(1)		;FORKX OF THE TOP FORK IN JOB
	HRRZ 1,FKJOB##(1)	;JSB OF THE JOB
	CALL SETMPG		;MAP IN THE JSB
	MOVE 1,CTRLTT(6)
	MOVEM 1,0(P)		; SAVE THE OLD CTRLTT
	SKIPGE 4,0(P)		;WAS IT ATTACHED?
	JRST DATAC3		;NO.
DATAC2:	CALL DETPSI		;CAUSE PSI, DETACH AND SLOW HANGUP
	MOVE 1,-1(P)		;RECOVER JOB NUMBER
	HRROS JOBPT(1)		;NO CTTY HERE EITHER
DATAC3:	SKIPGE -1(P)		;WANT TO UNMAP JSB?
	JRST DATAC4		;NO
	OKSKED			;YES.
	CALL CLRLFK		;UNMAP IT, GO OKINT
DATAC4:	POP P,4			;RETURN THE OLD CTRLTT IN AC4
	POP P,1			;RESTORE ARG TO AC 1
	POPJ P,0		;AND RETURN TO CALLER OF DATAC1

;HERE FROM .ATACH IF B1 ON IN AC2. TTY IN 4, JOB IN 1.
;DO A PROXY ATTACH. LEGAL IF I OWN TTY AND JOB, OR I'M WHOPR

PATACH:	MOVE 3,JOBNO		;MY JOB NUMBER
	HRRZ 4,JOBONT(1)	;DO I OWNED THAT JOB?
	MOVEI 2,WHEEL+OPER	;OR AM I ENABLED?
	CAIE 3,(4)		; ..
	TDNE 2,CAPENB		; ..
	SKIPA			; OK
	RETERR CAPX1		;NO GOOD.
	UMOVE 2,4		;OK. GET THE TTY NUMBER
	HRRZS 2			; ..
	CAIL 2,NLINES		;VALID LINE?
	RETERR ATACX1		;NO.
	NOSKED			;COVER TTFORK
	HLRZ 3,TTFORK(2)	;OK, WHO HAS IT?
	CAIN 3,-1		;FREE?
	JRST PATAC1		;YES. OK TO DO
	CAMN 3,JOBNO		;NOT FREE. MINE?
	CAMN 2,CTRLTT		;BUT NOT MY CTTY?
	JRST PATACX		;NOT MINE TO USE, OR IS MY CTTY. FAIL.
PATAC1:	OKSKED			;SEEMS OK.
	PUSH P,2		;SAVE THE TTY NUMBER
	HRLI 1,(1B0)		;FLAG TO KEEP JSB MAPPED
	CALL DATAC1		;DETACH THE JOB
	  JRST PATACX		;IT ISN'T THERE ANY MORE?
	POP P,2			;GET BACK LINE NUMBER
	HRRZM 2,CTRLTT(6)	;ATTACH THE JOB TO IT
	HRLM 2,JOBPT(1)		; ..
	HRLM 1,TTFORK(2)	; ..
	MOVE 3,TTSPSI(6)	; SET ITS PSI WORDS UP
	MOVEM 3,TTPSI(2)
	MOVE 3,TTSDPS(6)	; ..
	MOVEM 3,TTDPSI(2)
	OKSKED
;NOW BUILD AN EFACT ENTRY AND LOG ON THE LOGGING TTY
	PUSH P,1		;OBJECT JOB
	PUSH P,2		;NEW TTY
	PUSH P,4		;OLD TTY
	ADD P,BHC+3		;SPACE FOR AN EFACT BLOCK
	JUMPGE P,MSTKOV		;CHECK IT DIDN'T OVERFLOW
	MOVEI 7,-2(P)		;POINT AT THE EFACT BLOCK IN 7
	HRLZM 1,0(7)		;PUT THE JOB NUMBER IN HEADER
	HRRZ 2,JOBDIR(1)	;PUT IN THE LOGGED IN DIR NUM
	MOVEM 2,1(7)		; ..
	GTAD			;AND CURRENT TAD
	MOVEM 1,2(7)		; ..
	MOVEI 1,142		;CODE FOR ATTACH
	DPB 1,LFCTBP		;TO HEADER WORD
	MOVE 2,-4(P)		;NEW CTTY
	DPB 2,LFCTP3		;TO WORD 0
	MOVSI 1,-3		;LENGTH OF EFACT BLOCK
	CALL LOGFCT		;PUT IT INTO THE FACT FILE
;FALL THRU

;FALLEN INTO FROM ABOVE
PATCLG:	CALL GTLGTT		;GET THE LOGGING TTY
	HRROI 2,ATAMS4		;COMMENT ATTACHED JOB
	CALL LOGMES
	HRRZ 2,-5(P)		;GET THE JOB NUMBER
	CALL LOGNUM		;JOB NUMBER IN DECIMAL TO LOGDES
	HRROI 2,ATAMS5		;COMMENT TO TTY
	CALL LOGMES
	LDB 2,LFCTP3		;GET THE TTY NUMBER BACK
	MOVEI 3,^D8		;IN OCTAL
	CALL LGNOUT
	CALL LOGJ2		;OUTPUT DATE AND TIME
	SUB P,BHC+6		;DISCARD FACT BLOCK AND ARGS
	CALL RLLGTT		;OUTPUT CRLF, LET GO OF LOG TTY.
	CALL CLRLFK		;LET GO OF JSB
	JRST SKMRTN

PATACX:	OKSKED
ATX1R:	RETERR ATACX1

;CREATE JOB JSYS -- NOTE SPEC DOES NOT AGREE WITH JSYS MANUALS
;BEFORE 1975.
;
;CALLING SEQUENCE:
;	MOVE 1,FLAG BITS
;	MOVEI 2,ADDR OF ARGUMENT BLOCK
;	CRJOB
;	  FAIL, ERROR IN AC1
;	SUCCESS
;
;BITS IN AC1:
;B0: ON MEANS LOG THE NEW JOB IN
;B1: ON MEANS USE THE NAME AND PASSWORD IN ARGUMENT BLOCK
;    OFF MEANS LOG IN SAME AS JOB EXECUTING CRJOB
;B2: ON MEANS USE ACCOUNT IN ARGUMENT BLOCK
;    OFF MEANS USE CURRENT ACCT OF JOB EXECUTING CRJOB
;B3: ON MEANS RUN THE FILE WHOSE NAME IS SPECIFIED IN ARG BLOCK
;    ELSE JUST PUT IN AN EXEC
;B4: PUT AN EXEC IN ABOVE THE SPECIFIED FILE 
;B5: IF FILE IS TO BE RUN, SET ITS AC'S AS IN ARG BLK
;B6: DISOWN THE NEW JOB
;B7: HANG UNTIL ATTACHED BEFORE STARTING NEW JOB.
;B8: DON'T CHECK PSWD ON LOGIN. IGNORED UNLESS B1 OFF OR ENABLED WHOPR.
;B9: ON LOGIN, USE DEFAULT ACCT OF USER.
;B10: ON LOGIN, DON'T UPDATE LAST-LOGIN-DATE IN DDB
;B11: DO SPJFN BEFORE STARTING UP NEW JOB
;B12: SET NEW JOB'S CAPMSK RH TO MY CURRENT CAPENB RH, UNTIL IT LOGS IN.
;B13: (SYMBLX) DISABLE WHEEL LOGINS FOR THIS JOB
;B17: JOB IN 3. DISOWN IT.
;
;IN ARG BLOCK:
;WD 0: LH - ADDR OF NAME STRING (ASCIZ)
;      RH - SAME FOR PASSWORD STRING (ASCIZ)
;WD 1: 5B2+N FOR NUMERIC ACCOUNT, ELSE 0,,ADDR OF STRING ACCT
;WD 2: LH OFFSET FOR SFRKV FOR FORK TO RUN
;      RH ADDR OF FILE'S NAME STRING TO BE RUN
;WD 3: TERMINAL DESIGNATOR FOR JOB'S CONTROLLING TTY, OR 377777
;      FOR A DETACHED JOB
;WD 4: CPU LIMIT FOR NEW JOB BEFORE FORCED LOGOUT (NOT YET IMPL)
;WD 5: CONNECT TIME LIMIT FOR NEW JOB BEFORE FORCED LOGOUT (NOT YET IMP)
;WD 6: ADDR OF 20 AC'S TO BE STUFFED INTO FORK BEFORE STARTING IT
;WD 7: FLAGS FOR EXEC'S AC 1
;WD 8: PRIMARY JFN'S TO DO SPJFN ON NEW JOB


INTERN .CRJOB

.CRJOB:	JSYS MENTR		;ENTER NORMAL MONITOR CONTEXT
CRJOB1:	NOINT			;DON'T INTERRUPT OF OF CRJLCK RESOURCE
	LOCK (CRJLCK##,<JRST CRJLKF>)
	SETZM CRJANS##		;CLEAR RESULT WORD
	MOVE A,JOBNO		;STORE MY JOB NUMBER
	MOVEM A,CRJONJ		;IN COMMON STORAGE
	MOVE A,CAPENB		;AND MY CURRENT CAPABILITIES
	MOVEM A,CRJOJC		; ..
	UMOVE 10,1		;JSYS REQUEST BITS
IFDEF SYMBLX,<
	TRNN A,WHEEL+OPER
	 TLO 10,(1B13)		;NON WHEELS CANNOT MAKE WHEEL JOBS
>
	MOVEM 10,CRJAC1		; ..
	TLNE 10,(1B17)		;DISOWN A JOB?
	JRST CRJDSN		;YES.
	TRNN A,WHEEL+OPER	;IS IT A WHEEL OR OPER?
	TLNE 10,(1B0+1B4)	;NO. MUST HAVE LOGIN OR EXEC BITS ON
	SKIPA			;OK
	JRST CRJILG		;ILLEGAL REQUEST.
	UMOVE 7,2		;POINTER TO PARAMETER BLOCK
	SETOM CRJTTY		;ASSUME DETACHED NEW JOB
	UMOVE A,3(7)		;SEE WHAT TTY HE WANTS THIS ON
	CAIN A,377777		;NIL?
	JRST CRJB1A		;YES. OK.
	CAIL A,400000		;RANGE CHECK
	CAIL A,400000+NLINES	; ..
	JRST CRJTTX		;NOT A GOOD TTY NUMBER
	SUBI A,400000		;OK, SEE IF HE CAN HAVE IT
	NOSKED			;STABLE STATE OF TTFORK
	HLRZ B,TTFORK(A)	;WHO OWNS IT?
	CAMN B,JOBNO		;ME?
	CAMN A,CTRLTT		;OK. MUST ALSO NOT BE MY CTTY ALREADY
CRJOTX:	JRST [OKSKED		;NOT MINE, OR IS CTTY
		JRST CRJTTX]	; ..
	TRO A,400000		;SEE IF IT'S ASSIGNED TO ME
	PUSHJ P,CHKDEV##	; ..
	  JRST CRJOTX		;SHOULDN'T HAVE FAILED HERE...
	TLNN C,(1B6)		;ASSIGNED (TO ME)?
	JRST CRJOTX		;NO.
	UMOVE 10,1		;RESTORE AC'S
	UMOVE 7,2
	UMOVE A,3(7)		;AND THE SPECIFIED TTY NUMBER
	ANDI A,377777
	MOVEM A,CRJTTY		;STORE THE TTY TO START JOB ON
	OKSKED
	TRO A,400000
	RELD			;RELEASE THE TTY
	  JFCL

CRJB1A:	JUMPGE 10,CRJOB4	;IF NOT LOGGING IN, SKIP NAME, PSWD
	TLNN 10,(1B1)		;USE SUPPLIED, OR MINE?
	JRST CRJOB2		;MINE.
	UMOVE B,0(7)		;SUPPLIED. GET NAME STRING
	HLRZ B,B		;FROM USER SPACE
	HRLI B,440700		;AND IS AN ASCIZ STRING
	MOVEI A,CRJUSR-1	;WHERE TO STORE IT
	PUSHJ P,CPYFU1##	;COPY IT.
	  JRST CRJCPX		;COPY FAILED
	UMOVE B,0(7)		;NOW SAME FOR PASSWORD
	HRLI B,440700		; ..
	MOVEI A,CRJPSW-1	;STORE PASSWORD HERE
	PUSHJ P,CPYFU1
	  JRST CRJCPX		;COPY FAILED
	JRST CRJOB4		;GO GET AN ACCOUNT NUMBER/STRING

;HERE'S THE FAIL PATH ON LOCK MACRO ABOVE...
CRJLKF:	OKINT
	MOVEI 1,CRJLCK
	PUSHJ P,DISL		;WAIT, INTERRUPTABLE, FOR LOCK.
	JRST CRJOB1		;TRY TO GET IT AGAIN.

;HERE TO COPY MY OWN NAME AND PASSWORD FOR NEW JOB
;ACTUALLY, JUST PUT NAME IN, CAUSE LOGIN JSYS TO NOT CHECK PSWD
CRJOB2:	MOVE A,JOBNO
	HRRZ A,JOBDIR(A)	;HERE IS MY IDENTITY
	MOVEM A,CRJUSR		;EXEC0 WILL TAKE IT FROM HERE.

;NOW GET ACCOUNT FOR NEW JOB
CRJOB4:	SETZM CRJACT		;DEFENSIVE CHECK ON ACCOUNT.
	TLNE 10,(1B9)		;WANT DEFAULT ACCOUNT?
	JRST CRJB5Z		;YES. NOTHING TO SET UP
	TLNE 10,(1B2)		;USE CURRENT ACCOUNT, OR SUPPLIED.
	JRST CRJOB5		;SUPPLIED.
	SKIPL A,ACCTPT		;LOGGED IN NUMERICALLY?
	JRST CRJB4A		;NO. GO GET STRING
	MOVEM A,CRJACT		;YES. STORE IT HERE
	JRST CRJB5Z		;DONE ACCOUNT
;GET STRING ACCOUNT FOR CURRENT JOB
CRJB4A:	MOVE A,[XWD ACCTSR,CRJACT+1]
	BLT A,CRJACT+10		;COPY THE ACCOUNT STRING
	JRST CRJB5Z		;DONE ACCOUNT.
;HERE IF ACCOUNT WAS SUPPLIED BY USER IN ARG BLOCK
CRJOB5:	UMOVE B,1(7)		;GET THE ACCOUNT NUMBER OR POINTER
	MOVE A,B
	TLC A,(5B2)		;SEE IF NUMERIC OR STRING
	TLNE A,(7B2)		; ..
	JRST CRJB5A		;STRING
	MOVEM B,CRJACT		;STORE NUMERIC ACCOUNT
	JRST CRJB5Z		;DONE ACCOUNT
CRJB5A:	HRLI B,440700		;STRING POINTER
	MOVEI A,CRJACT		;COPY TO HERE + 1
	PUSHJ P,CPYFU1		;COPY IT.
	  JRST CRJCPX		;COPY FAILED?
CRJB5S:	MOVE A,[440700,,CRJACT+1]	;STRING POINTER
	MOVEM A,CRJACT		;FOR STRING COPIED HERE.
CRJB5Z:				;DONE PUTTING ACCOUNT INTO SWAPPABLE STG
	UMOVE B,7(7)		;SET UP REQUESTOR'S FLAGS TO EXEC
	MOVEM B,CRJEXF		;JUST IN CASE WANTED.
	UMOVE B,10(7)		;AND PRIMARY JFN'S IF USED.
	MOVEM B,CRJPJF		; ..
	SETZM CRJEVO		;ASSUME NO FILE TO RUN
	TLNN 10,(1B3)		;WANT TO RUN A FILE?
	JRST CRJB5Y		;NO
	MOVEI A,CRJFIL-1	;YES. STORE THE FILE NAME HERE
	UMOVE B,2(7)		;GET THE NAME FROM HERE
	TLZ B,777000		;9 BITS OF ENT VEC OFFSET
	HLRM B,CRJEVO		;TO SHARED STORAGE
	HRLI B,440700		;MAKE A BYTE POINTER TO NAME
	MOVEI C,<5*40>-1	;MAX STRING LENGTH
	PUSHJ P,CPYFU2##	;COPY THE STRING
	  JRST CRJCPX		;COPY FAILED
	TLNN 10,(1B5)		;SET THE AC'S?
	JRST CRJB5C		;NO
	UMOVE B,6(7)		;YES. WHERE THEY ARE IN USER AREA
	HRLZS B			;FORM A BLT POINTER
	HRRI B,CRJFAC		;AND HERE THE COMMON TEMP AREA FOR THEM
	XCTUM [BLT B,CRJFAC+17]	;COPY THEM, USER TO MONITOR SPACE.
CRJB5C:
CRJB5Y:	;END OF CODE DEALING WITH INFERIOR TO RUN

;HERE TO ACTUALLY PUT IN THE REQUEST FOR SCHEDULER TO START JOB

	MOVSI A,-2		;FLAG OF -2 FOR JOBSRT MEANS CRJOB CALL
	HRRI A,JOBSRT##		;SCHEDULER JOB START ROUTINE
	CALL SCDRQ7##		;CALL IT
	MOVEI A,CRJANS		;AND WAIT FOR ANSWER TO COME BACK
	CALL DISN##		; ..
	MOVE A,CRJANS		;WHEN IT DOES, IT'S A -1 FOR
	JUMPGE A,CRJXXX		;SUCCESS, OR +N FOR AN ERROR CODE.
	HLRZ A,CRJONJ		;NEW JOB NUMBER IS IN LH HERE
	UMOVEM A,1		;RETURN IT TO CALLER
CRJXIT:	UNLOCK CRJLCK		;RELEASE THE JSYS LOCK
	JRST SKMRTN		;AND RETURN TO CALLER

CRJDSN:	UMOVE A,3		;DISOWN A JOB. HERE'S THE NUMBER
	CAIL A,0		;RANGE CHECK
	CAIL A,NJOBS		; ..
	JRST CRJILG		;FOO. BAD JOB NUMBER
	NOSKED			;LOCK THE OWNERSHIP TABLE
	HRRZ B,JOBONT(A)	;GET THE JOB'S OWNER
	CAMN B,JOBNO		;DO I OWN IT?
	SETOM JOBONT(A)		;YES. I HEREBY DISOWN IT.
	OKSKED
	CAME B,JOBNO		;WAS IT MINE?
	JRST CRJILG		;NO. ERROR RETURN.
	JRST CRJXIT		;YES. SUCCESS RETURN.

CRJCPX:				;COPY OF USER STUFF FAILED.
CRJILG:	MOVEI A,CRJBX1##	;ILLEGAL PARAMETER OR BIT COMBINATION
	JRST CRJXXX
CRJTTX:	MOVEI A,CRJBX4##	;BAD TTY REQUESTED
CRJXXX:	UNLOCK CRJLCK		;FREE THE CREATE-JOB LOCK
	OKINT
	JRST MRETNE		;RETURN ERROR NUMBER IN 1

;		LOCK SWPMON PAGE FOR PATCHING

LOCKPG:	ANDI	1,777		;FORCE PAGE TO BE WITHIN LIMITS
	HRLZI	2,40000		;LOCK BIT
	IORM	2,MMAP(1)	;LOCK PAGE
	RET

;		UNLOCK SWPMON PAGE

UNLKPG:	ANDI	1,777		;FORCE PAGE TO BE WITHIN LIMITS
	HRLZI	2,40000		;LOCK BIT
	ANDCAM	2,MMAP(1)	;UNLOCK PAGE
	RET
	END ; OF LOG2JS.MAC

