;<134-TENEX>JOBINI.MAC;17    12-Dec-79 20:57:39    EDIT BY FRENCH
;ADDED CALL TO TYMQUE JUST BEFORE GETTING EXEC INCASE TALKING
;H2H FOR AUX CIRCUIT INITIATED LINES
;<134-FRENCH>JOBINI.MAC;1    21-Nov-79 23:03:13    EDIT BY FRENCH
;DON'T UNDO WHAT TYMX0 DID FOR TYMNET LINES IN EXEC0T
;<134-TENEX>JOBINI.MAC;15    16-Sep-79 18:05:07    EDIT BY PETERS
;<134-TENEX>JOBINI.MAC;14     6-Jul-78 19:10:04    EDIT BY PETERS
;<134-TENEX>JOBINI.MAC;13    27-NOV-77 16:35:45    EDIT BY PETERS
;<134-TENEX>JOBINI.MAC;12    16-FEB-77 15:14:59    EDIT BY PETERS
;<135-TENEX>JOBINI.MAC;11    12-DEC-75 10:51:55    EDIT BY PLUMMER
; CHANGE IFDEF PIESLC TO IFN (AROUND EXTERNS)
;<134-TENEX>JOBINI.MAC;10    28-AUG-75 12:55:04    EDIT BY ALLEN
; NEW SCHEME FOR UPDATING PIE-SLICE CPU TIME
;<134-TENEX>JOBINI.MAC;9    28-AUG-75 11:13:54    EDIT BY PLUMMER
; BREAK ADVICE LINKS ALONG WITH NORMAL LINKS
;<134-TENEX>JOBINI.MAC;8    27-AUG-75 14:16:04    EDIT BY CLEMENTS
; FIX TO CRJOB -- IF LOGIN FAILS IN NEW JOB, SETOM JOBONT BEFORE LOGO
;<134-TENEX>JOBINI.MAC;7    10-JUL-75 10:02:27    EDIT BY PLUMMER
; CORRECT SETUP OF FKDIR AT EXEC0A+
;<134-TENEX>JOBINI.MAC;6    12-JUN-75 15:11:47    EDIT BY ALLEN
; PROPER CAPABILITIES FOR AUTO LOGGED IN SYSTEM JOB ON CTY
; WHEN DBUGSW=2
;<134-TENEX>JOBINI.MAC;5    29-MAY-75 16:52:43    EDIT BY CLEMENTS
; INITIALIZE JOBPMF IN JSB TO -1 VERY EARLY IN EXEC0 IN CASE OF
;  ITRAP OR OTHER FORCED LOGOUT BEFORE IT IS SET UP FOR REAL.
; IN PARTICULAR, CRJOB FAIL ON SYSTEM FULL DOES THIS.
;<134-TENEX>JOBINI.MAC;4    29-MAY-75 11:24:44    EDIT BY CLEMENTS
; SET PRIMARY JFNS IN INFERIOR ONLY IF BOTH EXEC AND INFERIOR
; EXIST ON A CRJOB. FOR AUTOSTART JOBS.
;<134-TENEX>JOBINI.MAC;3     5-MAY-75 16:30:15    EDIT BY CLEMENTS
; ADD CODE FOR B12 OPTION OF CRJOB - PASSING RH CAPS TO
;  NEW JOB TIL LOGIN
;<134-TENEX>JOBINI.MAC;2    24-APR-75 12:32:02    EDIT BY CLEMENTS
;<134-TENEX>JOBINI.MAC;1    23-APR-75 15:48:03    EDIT BY CLEMENTS
;SEPARATING SWPMON.MAC INTO PIECES. THIS IS JOBINI.MAC

	SEARCH PROLOG
	TITLE JOBINI

INTERNAL	EXEC0

EXTERNAL	ACCIFG,AUTONX,BHC,BUGCHK,BUGHLT,CAPENB,CAPMSK
EXTERNAL	CHKRI,CRJAC1,CRJACT,CRJEVO,CRJEXF,CRJFAC,CRJFIL
EXTERNAL	CRJOJC,CRJONJ,CRJPJF,CRJPSW,CRJTTY,CRJUSR
EXTERNAL	DBUGSW,DDMPFK,DDTIME,DDTSYM,DISKP,DRMFRE,DSKRBS
EXTERNAL	DTIALL,EDISMS,EXECSI,FACTSW,FKDIR,FORKX,FREFK
EXTERNAL	GETDDT,GETMST,GETSWM,ILIST,ITRAP
EXTERNAL	JB0FLG,JBFINI,JOBDIR,JOBONT
EXTERNAL	JOBPT,LGNPAR,LGNSYS,LOGO,MENTR,MRETN,NORMTF,NSPECJ
EXTERNAL	NXTDMP,OTYLIN,PRIMRY,SPECJB,SPECJT,SPTC,SWCEND,SWPSTP
EXTERNAL	SYSIFG,TAB81,TAB82,TTFORK,TTICB1,TTICB2
EXTERNAL	WHEELX,WMST
external	xttflg
EXTERNAL	TYMQUE

IFN PIESLC,<
EXTERNAL	ASGDSH,NAPROC,NJBGRP,PIEGRP
>
DEFINE TMSG <	PUSHJ P,TMSGQ>

; ALL JOBS START HERE, INCLUDING THE BEGINNING OF JOB 0 WHICH DOES
;  LOTS OF INITIALIZATION INCLUDING GETTING IN THE SWAPPABLE MONITOR FROM
;  DISK OR DECTAPE. THE MECHANISM FOR GETTING HERE IS AS FOLLOWS:
; EITHER A ^C ON AN IDLE LINE OR THE EXPLICIT CALL AT SYSGO IN PISRV
;  FOR THE FIRST JOB, OR AUTO-JOBS AT SPCSTJ, CALL SCDRQ WITH A REQUEST
;  FOR THE SCHEDULER TO RUN JOBSRT (WITH A TTY LINE NUMBER AS ARGUMENT).
;  JOBSRT GRABS A JOB AND INITIATES A FORK-CREATION PSI ON IT. WHEN
;  THE FORK INITIALIZES ITSELF, IT SEES THAT IT IS THE TOP FORK IN
;  A JOB, AND SETS ITS PI PC TO EXEC0. THEN IT DEBREAKS, AND LO AND
;  BEHOLD IT IS RUNNING HERE AT PROCESS LEVEL.

RESCD

EXEC0:	MOVSI 1,UMODF		;SIMULATE CALL FROM USER
	MOVEM 1,FPC
	JSYS MENTR
	SETOM JOBPMF		;IN CASE GET TO LOGO BEFORE REAL JFN HR
	SKIPN SYSIFG		;NEW SYSTEM?
	CALL GETSWM		;YES, GET SWAPPABLE MONITOR
	CALL RESLCK##		;LOCK DOWN SOME RESIDENT FREE SPACE

GOTSWM:	MOVEI 1,FKPTRS
	MOVEI 2,NUFKS
	CALL ILIST
	MOVE 1,0(1)		;GRAB FIRST SLOT FOR TOP FORK
	MOVEM 1,FREJFK
	SETZM FKPTRS
	MOVE 1,FORKX
	HRROM 1,SYSFK		;SYSFK=JOB CTTY,,FORK INDEX
IFN PIESLC,<
	MOVSI 2,NOCNT##
	ANDCAM 2,FKFLGS##(1)	;FORK NOW IN SYSFK, OK TO MAINTAIN NAPROC
	MOVE 2,JOBNO
	SETZM JOBORT##(2)		; FORGET OLD JOB RUNTIME
	MOVE 2,PIEGRP(2)
	MOVSI 1,(1.0)
	FADRM 1,NAPROC(2)	;DO THIS NOW BECAUSE IT WASN'T DONE
				;WHEN THIS FORK ENTERED BALSET
> ;END PIE-SLICE SCHEDULER CONDITIONAL
	MOVE 1,[XWD SYSFK+1,SYSFK+2]
	SETOM -1(1)		;INIT REMAINDER OF TABLE
	BLT 1,SYSFK+NUFKS-1
	SETOM TTJTIW
	MOVSI 1,<JFCL>B53
	MOVEM 1,MONINT		;FOR DDT BREAKPOINT
	MOVE 1,[XWD ITFPC,EXECI]
	MOVEM 1,MONBK
	SETZ 1,
	MOVEI 2,3		;ENABLE CHANS 34 AND 35 FOR ^P
	MOVEM 2,MONCHN		;AND GRAB THEM IF THEY HAPPEN
	AIC
	CALL JBFINI		;INIT FILE SYS FOR THIS JOB
	SKIPN SYSIFG		;SYSTEM INITIALIZED?
	CALL EXECSI		;CALL SYSTEM INITIALIZATION PACKAGE
	JRST SYSINE		;OK, NOW GO TO SWAPPABLE CODE

	SWAPCD

SYSINE:	MOVEI 1,0		;ASSUME WILL BE ABLE TO LOG IN
	MOVE 2,SPTC		;CHECK FOR FULL SYSTEM
	CAIL 2,SSPT-NOFN-100	;ROOM IN SPT?
	MOVEI 1,LGINX3##	;NO. (THESE FAILURES SHOULD BE SEPARATE)
	MOVE 2,DRMFRE		;DRUM SPACE?
	CAMG 2,DRMIN0##
	MOVEI 1,LGINX3
	SKIPE FREJOB##
	SKIPN FREFK		;ANOTHER JOB AND FORK LEFT?
	MOVEI 1,LGINX3		;NO.
	MOVEM 1,LGNPAR		;SAVE ZERO OR FAIL CODE FOR LOGIN JSYS
	MOVE 1,JOBNO
IFN PIESLC,<
	MOVE 2,PIEGRP(1)
	AOS NJBGRP(2)
	CALL ASGDSH		;RECOMPUTE DSHARES
> ;END PIE-SLICE SCHEDULER CONDITIONAL
	HLRZ 2,JOBPT(1)		;GET TTY NUMBER TO START ON
	CAIE 2,-2		;SPECIAL FLAG THAT THIS IS CRJOB START?
	JRST EX0NCJ		;NOT CREATE-JOB
	SKIPL 2,CRJTTY		;YES. SEE WHAT TTY REALLY IS
	HRLM 1,TTFORK(2)	; ..
	HRLM 2,JOBPT(1)		;REASSIGN THE TTY TO NEW JOB
	MOVEM 2,CTRLTT		; ..
	SKIPE LGNPAR		;WILL IT SUCCEED?
	JRST [	MOVEI 1,CRJBX6##;NO, SAY SYSTEM FULL TO CALLER
		MOVEM 1,CRJANS##
		JRST LOGO]	;AND KILL OFF THE NEW JOB
	SETOM LGNPAR		;FLAG IN LGNPAR THAT THIS IS A CRJOB
EX0NCJ:	NOINT
	HRROI 2,[ASCIZ /<PMFDIR0>JOBPMF/]
	MOVE 1,JOBNO
	CAIGE 1,10
	HRROI 2,[ASCIZ /<SYSTEM>JOBPMF/] ;SYSTEM DIR FOR FIRST JOBS
	MOVSI 1,(1B5+1B8+1B17)	;TEMP FILE,IGN DEL AND SHORT FORM GTJFN
	GTJFN
	JRST PMFBAD
	HRRZM 1,JOBPMF
	MOVEI 2,1B19+1B20+1B21+1B25	;IN, OUT, XCT, THAWED
	OPENF			;OPEN PMF
	 JRST PMFBAD
	OKINT

pmfb1:	hrrz 2,ctrltt		;get tty for this job if any
	cail 2,tymttl		;is this a tymnet line?
	caile 2,tymtth		;true if in this range
	jrst exec0a		;not tymnet, or detached
	call tymx0##		;tymnet, go do login sequence

EXEC0A:	TIME
	MOVEM 1,CONSTO		;SET CONSOLE TIME ON
	MOVE 1,JOBNO		;FIRST FEW JOBS ARE SPECIAL,
	CAIL 1,NSPECJ		;IS THIS ONE OF THEM?
	JRST EXEC0T		;NO. GO DO TTY SETUP
IFE PIESLC,<
	MOVE 2,BHC+1		;YES, FAKE A LOGIN AS SYSTEM
	MOVEM 2,JOBDIR(1)
	MOVE 3,FORKX
	MOVEM 2,FKDIR(3)
>
IFN PIESLC,<
	CALL LGNSYS		;LOG IN AS SYSTEM
	MOVE 1,CAPMSK		;TURN ON CAPS
	MOVEM 1,CAPENB		; ..
	MOVE 1,JOBNO
>
	HRRZ 2,SPECJT(1)
	JRST 0(2)		;GO DO THE SPECIAL FUNCTION

EXEC0T:	SKIPGE 2,CTRLTT		;ATTACHED TO A TERMINAL?
	JRST EXEC0B		;NO -- SKIP TTY INITIALIZATION
IFDEF SYMBLX,<
	MOVE 3,XTTFLG(2)	;ENABLES ALLOWED ON THIS LINE?
	MOVSI 1,(1B7)
	TLNE 3,(1B2)
	 IORM 1,CAPMSK		;YES, SET THAT FLAG
>
	MOVEI 1,100
	CAIL 2,TYMTTL		;TYMNET LINE?
	CAILE 2,TYMTTH
	 CAIA
	JRST EXEC0C		;YES-DID THIS IN TYMX0
	MOVEI 2,3B33		;SET TO HDX HERE, SO SETTING TO FDX
	STPAR			;BELOW WILL FORCE OUT TELNET CONTROL
	MOVE 2,NORMTF		;GET TTY TO STANDARD STATE
	SFMOD
	STPAR			;SET DEVICE PARAMETERS TOO
	hrrz 3,ctrltt		;get controling tty number
	hrrz 2,xttflg(3)	;set up reasonable terminal type
	STTYP			;INIT TERM TYPE TO 0
EXEC0C:	MOVE 2,TTICB1
	MOVE 3,TTICB2
	SFCOC
	MOVE 2,TAB81		;GET STANDARD TAB STOPS
	MOVE 4,2
	MOVE 3,TAB82
	STABS			;SET CONTROLLING TTY
	MOVSI 1,(1B0)		;BREAK ADVICE LINKS
	ADVIZ
	 JFCL
	HRLOI 1,(1B0+1B1+1B4+1B5)
	MOVEI 2,-1		;ALL REMOTE
	TLINK			;BREAK LINKS, SET ACCEPT
	JFCL

EXEC0B:	SKIPGE LGNPAR		;CRJOB STARTUP?
	JRST STCRJB		;YES.
	MOVE 1,[CHKDSE]		;CHKDSK ERRORS?
	TDNE 1,FACTSW
	JRST	[HRRZ 3,CTRLTT		;YES, ALLOW CTY & DIALUP TO FIX
		CAME 3,OTYLIN		;DIALUP LINE?
		CAIN 3,CTYLIN		;OR OPERATORS CONSOLE
		JRST STEX		;START THE EXEC
		MOVEI 1,SYSINT		;HAVE AUTOJOBS WAIT FOR DISK OK
		JSYS EDISMS
		JRST .+1]
	SETO 3,
	EXCH 3,AUTONX		; PREVENT FURTHER AUTO START UP
	JUMPGE 3,SPECJB		; IF ANY MORE AUTOSTARTUP, DO IT
	MOVE 1,DBUGSW		; IF STARTING IN DEBUG MODE, AND
	HRRZ 2,CTRLTT		; ON CTY,
	CAIN 1,2		; ..
	CAIE 2,CTYLIN		; ..
	 JRST STEX0
	PUSHJ P,LGNSYS		; DO A SYSTEM LOGIN, SINCE JOBDIR ISN'T
				;  GOOD ENOUGH WITH HASH AND PIES.
	MOVE 2,CAPMSK
	MOVEM 2,CAPENB

	JRST STEX		;GO START EXEC

	RESCD

SYSINT:	MOVE 1,[CHKDSE]	;DISK ERRORS FIXED?
	TDNE 1,FACTSW
	JRST 0(4)		;NO
	JRST 1(4)		;YES
 SWAPCD

PMFBAD:	BUG(CHK,<PMF FAILURE>)
	MOVE 1,JOBPMF
	RLJFN
	JFCL
	SETOM JOBPMF
	SETZM PSIBW		;FLUSH ANY BREAKS, E.G. DATA ERROR
	OKINT
	JRST PMFB1		;ALLOW JOB WITH NO PMF

;HERE IS THE START OF THE MINIEXEC. FIRST CHECK TO SEE IF USER
; BELONGS HERE (MAYBE LOG HIM OFF IF NOT), AND THEN GO TO THE
; MINIEXEC COMMAND INTERPRETER.

EXEC2:	SETOM PRIMRY		; IN CASE ERROR INTERUPT OR HALTF
	SKIPGE LGNPAR		;CRJOB FAILED TO GET GOING?
	JRST CRJMEX		;YES. KEEP IT FROM MINIEXEC!
	MOVEI 1,100		;ESTABLISH CORRECT TTY MODES
	MOVE 2,NORMTF
	SFMOD
	MOVE 2,TTICB1
	MOVE 3,TTICB2
	SFCOC
	CALL DTIALL
	MOVE 1,[XWD 20,^D34]
	ATI			;ASSIGN CONTROL-P FOR INTERRUPTS
	MOVE 2,JOBNO
	HRRZ 1,JOBDIR(2)
	SKIPE ACCIFG		;IF NOT REFRESHING,
	JUMPE 1,[SETZM CAPMSK	;AND NOT LOGGED, FLUSH ALL CAPS
		SETZM CAPENB
		MOVE 3,CTRLTT
		CAIE 3,CTYLIN	;ALLOW MINI-EXEC ONLY ON CTY
		JRST .+1
		HRLOI 3,777000	;GIVE ALL CAPS
		MOVEM 3,CAPMSK
		MOVEM 3,CAPENB
		MOVE 3,BHC+1	;FAKE LOGIN AS SYSTEM
		MOVEM 3,JOBDIR(2)
		JRST .+1]
	MOVE 1,CAPMSK		;LOOK AT CAPS, ALLOW MINI-EXEC
	TRNN 1,WHEEL+OPER	;ONLY IF WHEEL OR OPER
	JRST [	SKIPE FORKN	;OTHERWISE,
		HALTF		;HALTF IF BELOW EXEC
		JRST LOGO]	;LOGOUT IF TOP FORK
	MOVEI 1,"."
	PBOUT
;FALL THRU

;FALLS THRU FROM ABOVE

EXECT1:	PBIN			;AWAIT USER INPUT
	CAIL 1,"A"+40
	CAILE A,"Z"+40
	SKIPA
	SUBI A,40
	CAIN 1,"E"
	JRST GEX
	CAIN 1,"B"
	JRST EBLT		;BLT SWP MON TO USER SPACE
	CAIN 1,"M"
	JRST EMNT		;MOUNT DTA-
	CAIN 1,"R"
	JRST EREST		;RESET USER MAP AND FILES
	CAIN 1,"G"		;G - GET FILE
	JRST EGET
	CAIN 1,"S"		;S - START
	JRST EST
	CAIN 1,"D"		;D - DUMP ON FILE
	JRST EDMP
	CAIN 1,"I"
	JRST EINI
	CAIN 1,"H"
	JRST EHLT		;HALT T.S.
	CAIN 1,"W"
	JRST EWMST
	CAIN 1,"^"
	JRST MRETN
	CAIN 1,"/"
	JRST TODDT
	CAIN 1,"L"		;O-1 MAGTAPE SPECIAL??
	JRST EMTLOD		;YUP
	JRST EXERR		;NOTA

CRJMEX:	MOVEI 1,CRJBX2##	;FAILURE CODE
	MOVEM 1,CRJANS##	; ..
	JRST LOGO		;AND KILL NEW JOB

TODDT:	HRROI 1,[ASCIZ /
/]
	PSOUT
	CALL GETDDT		;GET PROTOTYPE DDT
	SKIPN SWPSTP		;NON RES SYMTAB HERE?
	CALL GETMST		;NO, TRY TO GET IT
	SKIPN 1,SWPSTP		;USE NON-RES SYMTAB IF PRESENT
	MOVE 1,36		;MOVE MONITOR SYMBOL POINTER
	SKIPN @DDTSYM		;HAVE SYMTAB PTR ALREADY?
	MOVEM 1,@DDTSYM		;NO, SET ONE UP
	JRST MDDT		;GO TO DDT

;BLT SWAPPABLE CODE TO USER SPACE.  SHOULD DO RESET FIRST,
;THEN DUMP ON DTA--

EBLT:	MOVEI 1,[SIXBIT 'LT SWP MON/']
	TMSG
	CALL OKGO
	MOVE 2,SWCEND		;LST PAGE
	LSH 2,^D9
	MOVEI 1,SWPMP0		;FIRST PAGE
	LSH 1,^D9
	HRLI 1,0(1)
	XCTMU [BLT 1,777(2)]
	JRST EXCRR

EWMST:	MOVEI 1,[SIXBIT 'RITE MON SYM TAB/']
	TMSG
	CALL OKGO
	SKIPE 1,@DDTSYM		;DDT HAS CURRENT POINTER?
	MOVEM 1,SWPSTP		;YES, UPDATE IT
	JSP 4,WMST		;WRITE TABLE ONTO FILE
	JRST EXCRR

;THIS ROUTINE "GETS" A ZERO COMPRESSED SAVE FILE FROM MAG TAPE

EMTLOD:	MOVEI 1,[SIXBIT 'OAD MAG TAPE FROM: /']
	TMSG
	CALL ASGPAG##	;GRAB A BUFFER PAGE
	JRST EXERR	;NO PAGE AVAIL, ERROR RETURN
	MOVES (1)	;DIRTY PAGE
	MOVE 6,1	;SAVE PAGE ADDRESS IN 6
	MOVEI 7,EXERR	;SETUP RETURN ADDRESS FOR ERROR
	MOVSI 1,60003	;GET JFN FOR MTAN:
 	MOVE 2,[XWD 100,101]
	GTJFN
	JRST EMTRET	;ERROR RETURN
	MOVE 2,[XWD 7400,200000]	;DUMP MODE, READ ACCESS
	OPENF
	JRST EMTRET	;FAILED
	GDSTS		;GET DEVICE STATUS WORD
	TRZ 2,1B28
	TRO 2,1B27
	SDSTS		;SET TO 556 DENSITY
	PUSH P,1	;SAVE JFN 
	MOVEI 1,[SIXBIT 'FILE NUMBER: /']
	TMSG
	MOVEI 1,100
	MOVEI 3,^D10	;RADIX DECIMAL
	NIN
	JRST	[MOVE 1,(P)
		JRST EMTLD1]	;REMAIN AT CURRENT POSITION OF TAPE
	MOVE 3,2	;SAVE FILE NUMBER IN 3
	MOVE 1,(P)	;JFN TO 1
	MOVEI 2,1
	MTOPR		;REWIND TAPE
	JUMPLE 3,EMTLD1	;STAY AT FILE 0 IF FILE NUMBER <= 0
	MOVEI 2,16
	MTOPR		;MOVE FORWARD ONE FILE
	SOJN 3,.-1	;POSITION TO FILE NUMBER
EMTLD1:	CALL EMTGNW	;READ A POINTER OR JRST WORD
	SKIPL 5,4	;JRST WORD IF POSITIVE
	JRST EMTEOF	;DONE, READ EOF AND RETURN
EMTLD2:	CALL EMTGNW	;READ A DATA WORD
	UMOVEM 4,1(5)	;STORE IN USER CORE
	AOBJN 5,EMTLD2	;COUNT CORE POINTER
	JRST EMTLD1	;GET ANOTHER CORE POINTER
EMTGN1:	MOVE 3,6	;SET COUNT AND LOC FOR DATA WORDS
	HRLI 3,-200
EMTGNW:	JUMPGE 3,EMTRNB	;NEED ANOTHER BLOCK?
	MOVE 4,(3)	;NO, GET A WORD
	AOBJN 3,.+1	;COUNT IT
	RET
EMTRNB: MOVEI 2,3	;READ NEXT BLOCK FROM TAPE TO BUFFER
	MOVE 3,6	;SET UP FOR DUMPI
	SUBI 3,1
	HRLI 3,-200
	SETZ 4,		;END OF COMMAND LIST
	DUMPI
	JRST [SUB P,[XWD 1,1]	;RESTORE STACK
	     JRST EMTRET]	;ERROR RETURN
	JRST EMTGN1	;SUCCESS, CONTINUE
EMTEOF:	MOVEI 2,16	;READ EOF
	MTOPR
	MOVEI 1,400000
	CALL SETLFK##	;MAP PSB
	MOVEM 5,ENTVEC(1)	;STORE STRT LOC IN ENTRY VECTOR
	SETZ 1,
	MOVEI 2,FTPG1A
	CALL SETMPG##	;RELEASE PSB
	MOVEI 7,EXCRR	;SETUP SUCCESSFUL RETURN
EMTRET:	MOVE 1,6	;ADDRESS OF PAGE BUFFER
	CALL RELPAG##	;HAND PAGE BACK TO SYSTEM
	POP P,1		;UNSTACK JFN
	CLOSF		;CLOSE AND RELEASE JFN
	JRST EXERR	;CLOSE FAILED
	JRST (7)	;RETURN

EREST:	MOVEI 1,[SIXBIT 'ESET/']
	TMSG
	CALL OKGO
	MOVEI 1,-4
	KFORK			;KILL ALL FORKS
	MOVSI 7,-1000
	MOVNI 1,1
	MOVSI 2,400000
	SETZ 3,
ERES1:	HRRI 2,0(7)
	SKIPE UPTA(7)		;DON'T WASTE TIME IF ALREADY EMPTY
	PMAP
	AOBJN 7,ERES1
	MOVNI 1,1		;CLOSE ALL FILES
	CLOSF
	JFCL
	JRST EXCRR

OKGO:	PBIN
	CAIN 1,"."
	RET			;PERIOD MEANS GO
NODD1::	POP P,1			;ANYTHING ELSE MEANS ABORT
	JRST EXERR

GEX:	MOVEI 1,[SIXBIT 'XEC$/']
	TMSG
	JRST STEX

STEX0:	MOVE 2,CTRLTT
	CAIL 2,TYMTTL		;LINE IN TYMNET RANGE?
	CAILE 2,TYMTTH
	 JRST STEX		;NO-PASS THRU TO GET EXEC
	CALL TYMQUE		;YES-SEE IF TALKING H2H PROTOCOL
				;IF RETURNS MEANS GET EXEC
STEX:	MOVSI 1,(1B2+1B17)	;OLD FILE+SHORT FORM
	HRROI 2,[ASCIZ /<SYSTEM>EXEC.SAV/]
	GTJFN
	JRST [	HRROI 1,[ASCIZ /NO EXEC/]
		PSOUT
		JRST EXCRR]
	HRLI 1,400000
	GET
	JRST GEX1

;HERE AT STARTUP OF JOB BY CRJOB JSYS

STCRJB:	MOVE 7,JOBNO		;CARRY AROUND MY JOB NUMBER
	HRLM 7,CRJONJ		;RETURN THE ANSWER
	MOVE 10,CRJAC1		;GET THE FLAGS IN CREATOR'S AC1
	MOVE A,CRJONJ		;GET CREATOR'S JOB NUMBER
	TLNN 10,(1B6)		;IF NOT DISOWNED,
	MOVEM A,JOBONT(7)	;STORE BACK-LINK TO OWNER
	JUMPL 10,STCJB1		;JUMP IF WANT TO DO LOGIN
	HLLZ A,JOBDIR(A)	;NO LOGIN. CONNECT TO CREATOR'S CONN DIR
	HLLZM A,JOBDIR(7)	;I AM CONNECTED THERE, NOT LOGGED IN.
	MOVE 6,FORKX
	HLLZM A,FKDIR(6)
	SETZM FKGRPS##(6)	;I HAVE NO GROUP CAPABILITIES
	SETZM CAPENB		;MAKE SURE I'M NOT PRIVILEGED
	HLLZS CAPMSK		; ..
IFDEF SYMBLX,<
	MOVSI A,(1B7)		;WHEEL ALLOWED BIT
	TLNN 10,(1B13)		;REQUESTED OFF?
	 IORM A,CAPMSK		;NO, MARK IT
>
	MOVE A,CRJOJC		;GET CREATOR'S CAPS
	TLNE 10,(1B12)		;DOES HE WANT TO BESTOW THEM ON NEW JOB?
	HRRM A,CAPMSK		;OK, DO SO. (ONLY UNTIL LOGIN JSYS)
	JRST STCJ1A		;DONE WITH LOGIN STUFF

STCJB1:	MOVE A,CRJUSR		;WANT TO LOG IN. SEE IF NAME GIVEN
	TLNN A,-1		;TEXT OR NUMBER?
	JRST STCJ1B		;NUMBER.
	MOVEI A,0		;DO NOT RECOGNIZE
	HRROI B,CRJUSR		;NAME TO LOG IN AS
	STDIR			;GET DIRECTORY NUMBER
	  JFCL
	  JRST SCJXX1		;NO GOOD NAME.
	TLNE 1,(1B0)		;NOT FILES-ONLY, I HOPE
	JRST SCJXX1		;YES. YOU CAN'T DO THAT
	HRRZM 1,CRJUSR		;STORE THE NUMBER
STCJ1B:	MOVE 11,CRJOJC		;SEE IF OWNING JOB IS WHEEL/OPER
	HRRZ 1,CRJONJ		; OR SAME USER AS NEW JOB
	HRRZ 1,JOBDIR(1)	; ..
	TRNN 11,WHEEL+OPER	; ..
	CAMN 1,CRJUSR		; ..
	SKIPA			;YES. CAN BYPASS PASSWD AND/OR UPDATING
				; OF LOGIN DATE.
	TLZ 10,(1B8+1B10)	;NO. REQUIRE PASSWORD, UPDATE LOGIN DATE
	TLNN 10,(1B9)		;USE DEFAULT ACCOUNT?
	JRST STCJ1C		;NO. ONE HAS BEEN SET UP.
	MOVEI A,CRJACT+1	;YES. STRING AREA.
	HRRZ B,CRJUSR		;USER TO GET ACCT FOR
	GDACC			;GET HIS DEFAULT ACCOUNT
	  JRST SCJXXX		;HE HAS NONE, BUT IT WAS REQUESTED. ERR.
	MOVEM A,CRJACT		;STORE DEFAULT FOR LOGIN JSYS
;FALLS THRU

;FALLS THRU FROM ABOVE
STCJ1C:	MOVEI A,0		;SET UP FLAGS FOR LOGIN JSYS
	TLNE 10,(1B8)		;SUPPRESS PASSWD CHECK?
	TRO A,1			;YES.
	TLNE 10,(1B10)		;SUPPRESS LOGIN DATE UPDATE?
	TRO A,2			;YES.
	HRRM A,LGNPAR		;STORE THESE FLAGS FOR THE LOGIN JSYS.
	HRRZ A,CRJUSR		;THE DIRECTORY NUMBER
	HRROI 2,CRJPSW		;PASSWORD
	MOVE 3,CRJACT		;ACCOUNT
	LOGIN
	  JRST SCJXXX		;LOGIN FAILED.
STCJ1A:	MOVEI 1,400000		;SET UP FOR SPJFN, IN CASE WANTED.
	MOVE 2,CRJPJF		;GET THE PRIMARY JFN WORD
	TLNE 10,(1B3)		;IS THERE BOTH AN EXEC AND AN INFERIOR?
	TLNN 10,(1B4)		; ..
	SKIPA			;NO. DO THE SPJFN HERE.
	JRST STCJ1D		;YES. ONLY DO SPJFN ON THE INFERIOR
	TLNE 10,(1B11)		;REQUESTED?
	SPJFN			;YES, DO IT.
STCJ1D:	SETZM LGNPAR		;NOW LOGIN IS DONE IF WANTED.
	TLNE 10,(1B3)		;WANT TO RUN A FILE?
	JRST STCJB2		;YES.
	SETOM CRJANS		;NO. LET'S ASSUME GETTING AN EXEC WINS
	PUSHJ P,SCJWTA		;WAIT FOR ATTACH IF REQUESTED
	JRST STEX		;SET -1 TO ANSWER AND DO EXEC STARTUP

STCJB2:	TLNE 10,(1B4)		;FILE WANTED. EXEC TOO?
	JRST STCJB3		;YES.
	MOVSI A,(1B2+1B17)	;NO, JUST THE FILE.
	HRROI B,CRJFIL		;THIS FILE NAME
	GTJFN
	  JRST SCJXXX		;CAN'T GET IT.
	HRLI A,400000		;INTO THIS FORK
	GET
	MOVSI A,CRJFAC		;ACS FROM STORAGE TO USER 0
	TLNE 10,(1B5)		;IF WANTED
	XCTMU [BLT A,17]	;STORE THEM
	MOVEI A,400000		;GET THE ENTRY VECTOR
	GEVEC			; ..
	TLNN B,777000		;DEC STYLE ENTRY?
	JRST STCJ2A		;NO.
	HRRZ C,CRJEVO		;GET ENTRY VECTOR OFFSET
	CAILE C,1		;GOOD FOR DEC STYLE?
	JRST SCJXX3		;NO
	UMOVE B,120		;JOBSA
	TRNE C,-1		;IF ZERO OFFSET
	UMOVE B,124		;ELSE JOBREN
	SKIPA
STCJ2A:	ADD B,CRJEVO		;ADD OFFSET TO VECTOR
	HRRM B,0(P)		;STORE FOR RETURN
	SETOM CRJANS		;SAY CRJOB HAS SUCCEEDED
	PUSHJ P,SCJWTA		;WAIT FOR ATTACH IF REQUESTED
	JRST MRETN		;AND GO DO IT

STCJB3:	MOVSI A,(1B2+1B17)	;HERE IF WANT FILE AND EXEC BOTH
	HRROI B,CRJFIL		;GET A JFN FOR THE DESIRED FILE
	GTJFN
	  JRST SCJXXX		;RETURN GTJFN ERROR
	PUSH P,A
	DVCHR			;SEE IF IT'S THE DISK
	TLNE B,777		; ..
	JRST STCJ3A		;NO. JUST PASS JFN TO GET JSYS
	MOVE A,0(P)		;RESTORE JFN
	MOVE B,[44B5+5B21+1B25]	;OPEN RD, EX, THAWED, 36BIT
	OPENF			; ..
	  JRST SCJXX2		;OPENF FAILED. RETURN THE ERROR CODE.
STCJ3A:	MOVSI A,(1B1)		;JUST INFERIOR WITH CAPS LIKE MINE
	TLNE 10,(1B5)		;UNLESS LOAD AC'S
	TLO A,(1B3)		;THEN LOAD AC'S
	MOVEI B,CRJFAC		;FROM HERE
	CFORK
	  JRST SCJXX2		;CAN'T GET A FORK
	EXCH A,0(P)		;GET THE JFN
	SETNM			;SET THE JOB NAME FROM JFN
	HRL A,0(P)		;FORK,,FILE
	GET
	MOVSI A,(1B2+1B17)	;GET THE EXEC
	HRROI B,[ASCIZ /<SYSTEM>EXEC.SAV/]
	GTJFN			;JFN OF THE EXEC FILE
	  JRST SCJXX2		;NO EXEC EXISTS??
	HRLI A,400000		;GET IT INTO THIS FORK
	GET			; ..
	MOVE A,0(P)		;INFERIOR FORK HANDLE
	MOVE B,CRJPJF		;PRIMARY I/O REQUESTED
	TLNE 10,(1B11)		;WAS IT REQUESTED?
	SPJFN			;YES. SET IT IN INFERIOR FORK.
	POP P,A			;INFERIOR FORK HANDLE
	HRLZS A			; FORK TO RUN,,0
	HRR A,CRJEVO		;ENTRY VECTOR OFFSET OF INFERIOR
	UMOVEM A,2		;PUT THIS WHERE EXEC EXPECTS IT
	HRRZ A,ENTVEC		;THE ENTRY VECTOR OF THE EXEC ITSELF
	SKIPN A
	UMOVE A,120		;OR JOBSA
	ADDI A,3		;MAGIC ENTRY TO EXEC FOR CRJOB
	HRRM A,0(P)		;MRETN WILL RETURN HERE
	MOVE A,CRJEXF		;REQUESTOR'S FLAGS TO EXEC
	TLO A,(1B1)		;FORCE IT TO KNOW ABOUT THE FORK
	UMOVEM A,1		; ..
	SETOM CRJANS		;ALL HAS GONE WELL, ANSWER GOOD,
	PUSHJ P,SCJWTA		;WAIT FOR ATTACH IF REQUESTED
	JRST MRETN		;RETURN TO THE EXEC AT SPECIAL ENTRY

SCJWTA:	TLNN 10,(1B7)		;REQUESTED TO WAIT FOR ATTACH?
	POPJ P,0		; NO. PROCEED
	MOVE A,JOBNO		;YES. GET MY JOB NUMBER
	MOVEI A,JOBPT(A)	;CELL WHICH IS NEG WHILE DETACHED
	PUSHJ P,DISGE##		;WAIT FOR .GE. 0
	POPJ P,0		;AND RETURN

SCJXX3:	MOVEI A,SFRVX1##
	JRST SCJXXX
SCJXX2:	POP P,(P)		;CLEAR STACK
	JRST SCJXXX		;AND GIVE CFORK'S ERROR RETURN
SCJXX1:	MOVEI A,CRJBX5##	;BAD DIRECTORY NUMBER, UNKNOWN NAME
SCJXXX:	MOVEM A,CRJANS		;STORE ERROR CODE
	MOVE A,JOBNO		;MAKE SURE SELF-LOGOUT WILL GO THRU
	SETOM JOBONT(A)		;BY MAKING THIS JOB UNOWNED
	JRST LOGO		;AND KILL OFF THE ATTEMPTED JOB

;INIT DISK BIT TABLE, BAD SPOTS, ETC

EINI:	MOVEI 1,[SIXBIT 'NIT BIT TABLE/']
	TMSG
	CALL OKGO
	CALL DSKRBS		;READ BAD SPOTS
	JRST EXCRR

;HALT TENEX

EHLT:	MOVEI 1,[SIXBIT 'ALT TENEX/']
	TMSG
	CALL OKGO
	JSR CRSMSG##		;TELL THE USERS
	SETZM NXTDMP		;REQUEST DDMP ACTION
	SETZM DDTIME		;FORCE DDMP RUN
	AOS JB0FLG		;REQUEST JOB 0
	MOVEI 1,^D1000
	DISMS			;WAIT A SECOND
	SKIPL NXTDMP		;DDMP FINISHED?
	JRST .-4		;NO
	MOVSI 1,400000
	MOVEM 1,20		;HALT SCHED
	JRST EXCRR

;MOUNT DECTAPE

EMNT:	MOVEI 1,[SIXBIT 'OUNT DTA/']
	TMSG
	PBIN
	CAIL 1,"0"		;USER TYPES DIGIT
	CAILE 1,"7"		;FROM 0 TO 7
	JRST EXERR		;BAD CHAR
	MOVE 10,[ASCIZ /DTA0/]
	DPB 1,[POINT 7,10,27]	;CONSTRUCT STRING WITH UNIT NUMBER
	HRROI 1,10
	STDEV
	JRST EXERR		;NO SUCH DEVICE
	MOVE 1,2
	MOUNT
	JRST EXERR
	JRST EXCRR

EGET:	MOVEI 1,[SIXBIT 'ET FILE /']
	TMSG
	MOVSI 1,(1B2+1B4+1B16+1B17)	;OLD FILE,TERMINATION,IFOF,SHORT
	MOVE 2,[XWD 100,101]
	GTJFN
	JRST EXERR
	HRLI 1,400000		;THIS FORK
	GET
	JRST EXCRR

EST:	MOVEI 1,[SIXBIT 'TART/']
	TMSG
	CALL OKGO
	HRROI 1,[ASCIZ /
/]
	PSOUT
GEX1:	HRRZ 1,ENTVEC
	JUMPN 1,.+2
	UMOVE 1,120		;USE JOBSA
	HRRM 1,0(P)
	JRST MRETN

EDMP:	MOVEI 1,[SIXBIT 'UMP ON FILE /']
	TMSG
	MOVSI 1,(1B0+1B3+1B4+3B17)	;NEW V,PNT O/N,TERM,IFOF,SHRT
	MOVE 2,[XWD 100,101]
	GTJFN
	JRST EXERR
	HRLI 1,400000		;THIS FORK
	MOVE 2,[XWD 777760,20]	;ALL EXCEPT AC'S
	SAVE
	JRST EXCRR

EXERR:	HRROI A,[ASCIZ //]
	ESOUT
EXCRR:	HRROI 1,[ASCIZ /
/]
	PSOUT
	JRST EXEC2

;PSI TRAPPED TO HERE

EXECI:	XCT MONINT		;FOR DDT BREAKPOINT
	SETOM PRIMRY		;IN CASE WE WERE REDIRECTED FOR PRI I/O
	EXCH 1,ITFPC		;SAVE 1, GET TRAP PC
	TLNN 1,UMODF		;USER?
	JRST EXECI1		;NO
	MOVEM 1,FPC		;YES, ENTER MONITOR AT THAT LOC
	MOVE 1,ITFPC		;RESTORE AC1
EXECI2:	JSYS MENTR
	SETOM PRIMRY
	SKIPGE LGNPAR		;FAILURE IN START OF CREATED JOB?
	JRST CRJMEX		;YES. KEEP OUT OF MINIEXEC
	MOVEI 1,-4
	FFORK			;FREEZE FORKS
	MOVEI 1,100
	CFIBF			;CLEAR INPUT BUFFER
	MOVEI 1,400000
	SETO 2,
	DIC
	CALL DTIALL		;IN CASE PROGRAM TURNED ANY ON
	MOVE 2,PSIBW
	CIS
	TRNN 2,2		;CHANNEL 34?
	JRST EXECI3
	MOVEI 1,101
	CFOBF			;CLEAR OUTPUT BUFFER
	MOVEI 1,EM2
	TMSG
	JRST EXEC2

EXECI1:	MOVE P,UPP		;RESTORE TOP OF PDL
	MOVE 1,1(P)		;GET USER PC
	HRLI 1,UMODF
	MOVEM 1,FPC
	HRLZ P,ACBAS1		;RECOVER USER AC'S
	LSH P,4
	BLT P,17
	JRST EXECI2

EXECI3:	MOVEI 1,EM3
	TMSG
	MOVEI 1,101
	HRRZ 2,UPP
	HRRZ 2,1(2)		;GET TOP PC
	MOVEI 3,10		;RADIX
	NOUT			;PRINT ADDRESS
	JFCL
	JRST EXCRR		;TO MINI-EXEC IF WHEEL OR OPERATOR

EM2:	SIXBIT '$ABORT$/'
EM3:	SIXBIT '$INTERRUPT AT /'

;JSYS TO ENTER MINI-EXEC

.EXEC::	JSYS MENTR
	MOVEI 1,WHEEL+OPER
	TDNN 1,CAPENB
	JRST WHEELX		;ITRAP WITH WHEEL ERROR
	JRST EXEC2

EXBUGH::MOVE 1,UPDL		;RESET STACK, ETC.
	HRLI 1,UMODF
	MOVEM 1,FPC
	JSYS MENTR
	SETOM TRAPC
	SETZM NSKED
	SETZM INTDF
	MOVE 1,FORKX
	CAMN 1,DDMPFK		; DDMP FORK?
	JRST CHKRI		;YES, INTERRUPT
	HRRZ 1,PRIMRY
	HRROI 2,[ASCIZ /
BUGHLT AT /]
	SETZ 3,
	SOUT
	HRRZ 2,BUGHLT
	MOVEI 3,^D8
	NOUT
	JFCL
	MOVEI 2,15
	BOUT
	MOVEI 2,12
	BOUT
	JRST ITRAP

;EXEC UTILITY ROUTINES

;MESSAGE TYPER - ADR OF SIXBIT TEXT IN 1

TMSGQ::	HRLI 1,440600		;CONSTRUCT BYTE POINTER
	PUSH P,1
TMSG1:	ILDB 1,0(P)
	ADDI 1,40		;CONVERT TO ASCII
	CAIN 1,"/"		;SLASH IS TERMINATOR
	JRST TMSG2
	CAIN 1,"$"		;DOLLARS MEANS EOL
	JRST [HRROI 1,[ASCIZ /
/]
		PSOUT
		JRST TMSG1]
	PBOUT
	JRST TMSG1

TMSG2:	POP P,1			;FLUSH TEMP
	RET

	END ; OF JOBINI.MAC

