
;-*-MIDAS-*-

TITLE REATTACH

.INSRT RMS;MACROS >

TYIC==1
TYOC==2
USRI==3
DSKIC==4

BUSRC==100000	;USER-CONTROLLED BIT IN .USTP WORD.

IFNDEF LPDL,LPDL==40
IFNDEF JCLBSZ,JCLBSZ==10

BEG:	MOVE P,[-LPDL,,PDL]
	MOVEI A,ITEMTB		;INIT ALL VARIABLES
	MOVEM A,ITEMTP
	.SUSET [.RUNAM,,UNAME]
	SETZM JNAME
	SETOM TTYNUM
	.SUSET [.ROPTIO,,A]
	TLNN A,OPTCMD		;NO COMMAND STRING => REATTACH OUR OWN DETACHED TREE.
	 JRST OLDTREE
	SETZM JCLBUF		;NOW GET THE COMMAND STRING
	MOVE A,[JCLBUF,,JCLBUF+1]
	BLT A,JCLBUF+JCLBSZ-1
	.BREAK 12,[5,,JCLBUF]
	SKIPN JCLBUF
	 JRST OLDTREE
	MOVE D,[440700,,JCLBUF]	;NOW PROCESS THE COMMAND STRING.
ITEMLP:	SAVE D
	CALL RNUMO		;TRY TO READ A TTY NUMBER.
	 JRST ITEMWD		 ;FAILED; READ A WORD INSTEAD.
	SUB P,[1,,1]
	MOVEM A,TTYNUM		;GOT A TTY NUMBER; LOOK FOR NEXT THING.
	JRST ITEMX

ITEMWD:	REST D			;START OVER (MAYBE RNUMO SKIPPED SOME CHARS)
	CALL RDSIX		;READ A WORD.
	JUMPE B,ITEMW1
	XCT @ITEMTP		;STORE IT AS UNAME OR JNAME.
	AOS ITEMTP
ITEMW1:	CAIN C,"*		;"*" MEANS "DON'T SPECIFY THIS ITEM".
	 AOS ITEMTP
ITEMX:	CAIE C,^C		;^C AND ^M END THE COMMAND STRING.
	 CAIN C,^M
	  JRST CHECK
	CAIE C,"/		;SLASH STARTS A SWITCH.
	 JRST ITEMLP
	ILDB C,D
	CAIL C,140		;CONVER THE SWITCH NAME TO UPPER CASE
	 SUBI C,40
	SETZ B,
	CAIN C,"F		;/F => IF TTY IS IN USE, FREE IT BY DETACH.
	 SETOB B,FORCE
	CAIN C,"K		;/K => KILL THE TREE DETACHED FROM THE SPEC'D TTY,
	 SETOB B,KILL		 ;IF THERE WAS ONE.
	CAIN C,"X		;/X => EXCHANGE SPECIFIED JOB WITH THAT ON SPECIFIED TTY.
	 SETOB B,EXCHNG
	JUMPN B,ITEMLP
	JRST NOSWIT		;ANYTHING ELSE AFTER SLASH IS ILLEGAL.

ITEMTB:	MOVEM B,UNAME
	MOVEM B,JNAME
	SOS ITEMTP

;READ AN OCTAL NUMBER, OPTIONALLY PRECEDED BY "T", INTO A.
;THE "T" MAKES NO DIFFERENCE. A COLON MAY ALSO APPEAR AS A NOISE CHARACTER
;AT THE END.
;CLOBBERS B,C. SKIPS IF SUCCESSFUL (NUMBER IS NON-NULL AND
;HAS A PROPER TERMINATING CHARACTER).
RNUMO:	SETZB A,B	;A ACCUMULATES NUMBER, B COUNTS DIGITS.
	ILDB C,D
	CAIE C,"T
	 CAIN C,"T+40
RNUMO1:	  ILDB C,D
	CAIL C,"0
	 CAILE C,"9
	  JRST RNUMOX	;NON-DIGIT => IS THIS A LEGITIMATE TERMINATOR?
	LSH A,3
	ADDI A,-"0(C)	;DIGIT => MERGE IT IN.
	AOJA B,RNUMO1

RNUMOX:	JUMPE B,CPOPJ	;FAIL IF # OF DIGITS IS 0.
	CAIN C,":
	 ILDB C,D
RDTEST:	CAIE C,",	;SKIP IF CHAR IN C IS A LEGITIMATE TERMINATOR.
	 CAIG C,40
	  JRST POPJ1
	CAIE C,"*
	 CAIN C,"/
	  JRST POPJ1
	CAIN C,"?
	 JRST HELP
CPOPJ:	RET

POPJ1:	AOS (P)
	RET

;READ A WORD, AS A SIXBIT WORD IN B. CLOBBERS A, C.
;ENDS WITH FIRST LEGITIMATE TERMINATOR (BUT ^Q CAN QUOTE).
RDSIX:	SETZ B,
	MOVE A,[440600,,B]
RDSIXL:	ILDB C,D
	CALL RDTEST	;IS CHAR A TERMINATOR?
	 JRST RDSIX1	;NO.
	CAIE C,^Q	;YES, BUT ^Q ISN'T LIKE OTHER TERMINATORS.
	 RET
	ILDB C,D
	CAIGE C,40
	 RET		;DON'T LET ^M, ^C BE QUOTED AT ALL.
RDSIX1:	CAIL C,140
	 SUBI C,40	;CONVERT CHAR TO UPPER CASE.
	SUBI C,40
	TLNE A,770000
	 IDPB C,A
	JRST RDSIXL

;ASK THE USER FOR CONFIRMATION ON REATTACHING HIS DETACHED TREE.
CONFRM:.OPEN TYOC,TTYOF
	 .LOSE %LSSYS
	.OPEN TYIC,TTYIF
	 .LOSE %LSSYS
	MOVEI A,[ASCIZ /This will kill any jobs you have in this tree.
/]
	CALL NO3
CONFR1:	MOVEI A,[ASCIZ /Should I go ahead (Y or N)? /]
	CALL NO3
	.IOT TYIC,C
	CAIL C,140
	 SUBI C,40
	MOVEI A,[ASCIZ /
/]
	CALL NO3
	CAIN C,"N
	 JRST NO2
	CAIE C,"Y
	 JRST CONFR1
	SETOM KILL		;KILL THE USER'S NEW TREE.
	POPJ P,

;HERE IF NO ARGUMENTS.  ATTACH OUR OWN OLD DETACHED TREE, ASKING FOR CONFIRMATION.
OLDTREE:SETOM SELF

;COME HERE AFTER PROCESSING THE COMMAND STRING.
;DEFAULT THE JNAME IF NECESSARY, AND CHECK ARGS FOR LEGALITY.
CHECK:	SKIPE A,JNAME	;IF JNAME IS UNSPECIFIED, TRY DETACHED TREES, THEN HACTRN.
	 JRST CHECK1
	MOVE B,[SIXBIT/HELP/]
	CAMN B,UNAME		;IF UNAME IS "HELP" AND NO JNAME, COMMAND STRING WAS
	 JRST HELP		;JUST "HELP", SO GIVE USER HELP.
	MOVE A,['HACTRR] 	;USE HACTRR IF IT EXISTS,
	.CALL TRYOPN
	 SKIPA A,['HACTRQ]	;ELSE USE HACTRQ IF IT EXISTS,
	  JRST CHECK1
	.CALL TRYOPN
	 SKIPA A,['HACTRP]	;ELSE USE HACTRP IF IT EXISTS,
	  JRST CHECK1
	.CALL TRYOPN
	 SKIPA A,['HACTRO]	;ELSE USE HACTRO IF IT EXISTS,
	  JRST CHECK1
	.CALL TRYOPN
	 SKIPA A,['HACTRN]	;IT DOESN'T; TRY HACTRN.
	  JRST CHECK1
	SKIPE SELF		;DON'T TRY HACTRN IN CASE OF :REATTA<CR>
	 JRST NODET
CHECK1:	.CALL TRYOPN		;DOES SPEC'D JOB EXIST?
	 JRST NOJOB		;NO; ERROR.
	.USET USRI,[.RUSTP,,B]
	TLNE B,BUSRC		;IT IS AN ERROR TO TRY TO ATTACH A STOPPED JOB.
	 JRST NORUN
	SKIPE SELF		;IF :REATTA<CR>, ASK USER FOR CONFIRMATION.
	 CALL CONFRM
	MOVEM A,JNAME
	MOVE B,TTYNUM
	JUMPGE B,CHECK2		;IF USING OUR OWN TTY,
	.SUSET [.RCNSL,,B]	;FIND OUT ITS NUMBER.
	.SUSET [.RUNAME,,C]	;ALSO, IF WE ARE NOT LOGGED IN YET, ASSUME /K
	HLLES C			;SO THAT SOMEONE DOING :REATTA <SELF> WITHOUT
	AOJN C,CHECK2		;LOGGING IN DOESN'T LEAVE A NON-LOGGED-IN DETACHED TREE.
	SETOM KILL
	SKIPE EXCHNG		;/X ISN'T ALLOWED IF YOU AREN'T LOGGED IN.
	 JRST NOLOGX
CHECK2:	SKIPE KILL
	 SKIPN EXCHNG		;/X AND /K ARE INCONSISTENT.
	  CAIA
	   JRST NOXK
	TRO B,400000		;MAKE A TTY SPEC OUT OF THE TTY NUMBER.
	MOVEM B,RTTYNM
	SYSCAL STYGET,[B ? MOVEM C ? MOVEM A ? MOVEM C]
	 .LOSE %LSFIL
	HRRES C			;C HAS -1, OR IDX OF TOP OF TREE OF SPEC'D TTY.
	.USET USRI,[.RUIND,,D]
	CAIN D,(C)		;SPEC'D TREE IS ALREADY ON THAT TTY =>
	 JRST NOOP		;WE HAVE NOTHING TO DO.
	.USET USRI,[.RSUPPR,,D]
	JUMPGE D,NOTTOP		;SPEC'D JOB MUST BE TOP LEVEL
	JUMPL C,CHECK3		;NOW, IF TTY IS IN USE,
	SKIPE EXCHNG
	 SETOM FORCE
	SKIPL TTYNUM		;AND IT'S OUR OWN, DETACH US.
	 SKIPE FORCE		;ELSE GIVE UP UNLESS /F OR /X WAS SPEC'D.
	  SYSCAL DETACH,[%CLIMM,,400000(C) ? %CLBIT,,30]
	   JRST NOTFREE		;THIS DETACH OMITS CNSL FREE MSG, AND MAKES JOB
	JRST ACT		;DIE AFTER AN HOUR IF NOT ATTACHED.

CHECK3:	HRRES A
	JUMPGE A,NOTFREE	;CAN'T DO ANYTHING WITH TTY IN USE AS DEVICE.

;NOW THAT ALL IS CHECKED, DO THE DIRTY WORK.
ACT:	.USET USRI,[.RCNSL,,A]	;REMEMBER TTY THAT SPEC'D JOB CAME FROM IN A.
	.USET USRI,[.RAPRC,,D]
	JUMPL D,ACT1		;IF SPEC'D TREE IS DISOWNED, ALL IS WELL.
	JUMPL A,ACT2		;ELSE IF THE SPEC'D TREE HAD A TTY AND THE SPEC'D TTY
	JUMPL C,ACT2		;HAD A TREE, MAYBE (IF /X, "EXCHANGE", SPECIFIED)
	SKIPN EXCHNG
	 JRST ACT2
	SYSCAL DETACH,[1000,,USRI ? 5000,,30]	;KILL IN 1 HOUR, NO CONSOLE FREE MESSAGE.
	 .LOSE %LSSYS		;FREE THE SPEC'D TREE FROM ITS TTY
	SYSCAL ATTACH,[%CLIMM,,400000(C) ? %CLIMM,,400000(A) ? %CLBIT,,400000]
	 .LOSE %LSSYS		;AND PUT THE SPEC'D TTY'S TREE THERE INSTEAD.
	JRST ACT1

;HERE TO DETACH SPECIFIED TREE IF WE ARE NOT EXCHANGING IT.  IN THAT CASE,
;WE SHOULD ALLOW A CONSOLE-FREE MESSAGE ON ITS TERMINAL.
ACT2:	DPAUSE
	SYSCAL DETACH,[1000,,USRI ? 5000,,10]	;CTL BIT 10 INHIBITS CNSL FREE MSG
	 .LOSE %LSSYS
ACT1:	DPAUSE
	SYSCAL ATTACH,[%CLIMM,,USRI ? RTTYNM ? %CLBIT,,400000]
	 .LOSE %LSSYS
	SKIPN KILL		;AND MAYBE KILL THE TREE WE DETACHED FROM THE TTY.
	 JRST RETURN
	SKIPL C
	 .GUN C,
RETURN:	DPAUSE
	.LOGOUT
	.SUSET [.RTTY,,A]
	ANDI A,-1
	SKIPL TTYNUM
	 CAMN A,TTYNUM		;IF WE JUST REATTACHED OUR OWN TREE TO NEW TTY,
	  .VALUE [ASCIZ /:KILL :NEWTTY :VERSIO /] ;TELL DDT TO LOOK AGAIN AT TTY CHARACTERISTICS
	.BREAK 16,340000

;ERROR MESSAGE HANDLERS.

NOTFREE:JSP A,NO
	ASCIZ /Specified TTY not free/

NOTTOP:	JSP A,NO
	ASCIZ /Specified job not top level/

NOLOGX:	JSP A,NO
	ASCIZ */X is not allowed unless you are logged in*

NOXK:	JSP A,NO
	ASCIZ */X and /K together make no sense*

NOJOB:	JSP A,NO
	ASCIZ /No such job/

NORUN:	JSP A,NO
	ASCIZ /Specified job not running/

NOOP:	JSP A,NO
	ASCIZ /Specified job and TTY already connected/

NODET:	JSP A,NO
	ASCIZ /You have no detached tree/

NOSWIT:	JSP A,NO
	ASCIZ /Unrecognized switch. Legal ones are K, F, and X/

NO:	.OPEN TYOC,TTYOF
	 .VALUE
	CALL NO3		;PRINT THE STRING A POINTS TO,
	MOVEI A,[ASCIZ /.
For help, do :REATTA ?<cr> or print .INFO.;REATTA ORDER./]
	CALL NO3		;AND SOME GENERAL ADVICE.
NO2:	DPAUSE			;.VALUE IF DEBUGGING.
	.LOGOUT
	.BREAK 16,140000

NO3:	HRLI A,440700
NO1:	ILDB B,A
	JUMPE B,CPOPJ
	.IOT TYOC,B
	JRST NO1

;COME HERE IF COMMAND STRING IS "HELP" OR CONTAINS A "?".
HELP:	.OPEN TYOC,TTYOF
	 .LOSE %LSSYS
	SYSCAL OPEN,[1000,,DSKIC	;OPEN THE HELP FILE.
		['DSK,,] ? ['REATTA] ? [SIXBIT/ORDER/] ? ['.INFO.]]
	 .LOSE %LSFIL
	SETOM TTYNUM		;PREVENT VALRETTING :NEWTTY AT RETURN:.
HELP1:	.IOT DSKIC,A
	ANDI A,-1
	CAIE A,^L
	 CAIN A,^C
	  JRST RETURN
	.IOT TYOC,A
	JRST HELP1

;CONSTANTS
TRYOPN:	SETZ ? SIXBIT/OPEN/
	[10,,USRI]
	['USR,,]
	UNAME
	SETZ A

TTYIF:	'TTY
	0

TTYOF:	1,,'TTY
	0

;VARIABLES
FORCE:	0	;-1 => EVEN IF SPEC'D TTY IS IN USE AND NOT OURS,
		;GO AHEAD BY DETACHING WHOEVER IS ON IT.
KILL:	0	;-1 => IF MUST DETACH A TREE FROM THE TTY TO FREE IT,
		;GUN THE TREE AFTER ATTACHING THE TTY.
EXCHNG:	0	;-1 => TAKE THE TREE WE DETACH FROM THE SPECIFIED TTY
		;AND ATTACH IT TO THE TTY THE SPECIFIED JOB CAME FROM,
		;THUS EXCHANGING THE TWO TREES.
SELF:	0	;-1 => :REATTA<CR>, ATTACH OWN DETACHED TREE.
		;IN THIS CASE, WE LOOK ONLY FOR DETACHED TREES, NOT FOR HACTRN.
UNAME:	0	;UNAME OF JOB TO ATTACH
JNAME:	0	;JNAME OF JOB TO ATTACH
		;0 => TRY HACTRO, THEN TRY HACTRN IF HACTRO DOESN'T EXIST.
TTYNUM:	-1	;-1 => USE OUR TTY; ELSE, # OF TTY TO USE.
RTTYNM:	0	;A TTY SPEC, GOOD FOR SPECIFYING THE TTY TO BE USED.
ITEMTP:	ITEMTB	;FILENAME-READER COUNTER FOR SELECTING UNAME OR JNAME.

PDL:	BLOCK LPDL

JCLBUF:	BLOCK JCLBSZ
	-1

DEBUG:	0	;-1 => DEBUGGING.
PAT:
PATCH:	BLOCK 100
	-1

	END BEG
