;-*-Midas-*-

	Title :FRETTY - Print list of FREe TTYs

A=1
B=2
C=3
D=4
E=5
F=6
G=7
H=10

L=11
U=12
I=13

T=15
TT=16
P=17

CALL=<PUSHJ P,>
RETURN=<POPJ P,>

TTYo==1
PDLEN==10

TTYSTS:	0
NTTYS:	0
TTYPAG:	0
NUMFRE:	0

PDLIST:	-PDLEN,,PDLIST
	BLOCK PDLEN

Linear:	0
LasTTY:	0
GGroup:	0

TTYLOC:	0	;Pointer to data area for a particular site

; Data table for each site starts with # of generic locations.
;   G.L.#1 starts with # tty ranges
;     T.R.#1 is <bottom tty#>,,<top tty#>

MCDATA:	4
	 1,,[ASCIZ "30cps dialups"] ? 4,,7
	 2,,[ASCIZ "120cps dialups"] ? 1,,1 ? 10,,16
	 3,,[ASCIZ "8th floor"] ? 21,,22 ? 26,,26 ? 30,,34
	 1,,[ASCIZ "Software"] ? 40,,77

AIDATA:	7
	 1,,[ASCIZ "10cps dialups"] ? 2,,2
	 2,,[ASCIZ "15cps dialups"] ? 1,,1 ? 6,,6
	 1,,[ASCIZ "30cps dialups"] ? 15,,20
	 2,,[ASCIZ "120/15cps dialups"] ? 30,,30 ? 32,,33
	 1,,[ASCIZ "3rd floor"] ? 31,,31
	 2,,[ASCIZ "8th floor"] ? 21,,22 ? 36,,36
	 4,,[ASCIZ "9th floor"] ? 3,,5 ? 7,,7 ? 34,,34 ? 37,,40

MLDATA:	6
	 1,,[ASCIZ "30cps dialups"] ? 2,,3
	 4,,[ASCIZ "120cps dialups"] ? 1,,1 ? 4,,4 ? 11,,11 ? 32,,32
	 4,,[ASCIZ "3rd floor"] ? 5,,5 ? 10,,10 ? 16,,27 ? 33,,33
	 1,,[ASCIZ "5th floor"] ? 31,,31
	 2,,[ASCIZ "8th floor"] ? 7,,7 ? 12,,15
	 1,,[ASCIZ "9th floor"] ? 6,,6

DMDATA:	 4
	 4,,[ASCIZ "2nd floor"] ? 1,,10 ? 12,,13 ? 16,,16 ? 20,,20
	 2,,[ASCIZ "2nd floor Imlac"] ?	15,,15 ? 17,,17
	 1,,[ASCIZ "8th floor"] ? 11,,11

FRETTY:	BLOCK 77	;TTY#'s of free terminals

DEFINE SYSCAL OP,ARGS
	.CALL [SETZ ? SIXBIT /OP/ ? ARGS ((SETZ))]
	TERMIN

;;;
;;;
;;;

.scalar sysnam

GO:	MOVE P,PDLIST
	SYSCAL SSTATU,[REPEAT 5,%CLOUT,,T
		       %CLOUT,,SYSNAM]
	  .LOSE %LSSYS
	move tt,sysnam
	MOVEI T,MCDATA
	CAMN TT,[SIXBIT /AI/]
	  MOVEI T,AIDATA
	CAMN TT,[SIXBIT /ML/]
	  MOVEI T,MLDATA	;Figure out which dataset to use
	CAMN TT,[SIXBIT /DM/]
	  MOVEI T,DMDATA
	HRLI T,G		;Indexed off ac G
	MOVEM T,TTYLOC

	MOVE A,[SQUOZE 0,TTYSTS]
	.EVAL A,
	  .LOSE
	MOVE B,A
	IDIVI B,2000	;Page # in B
	HRLI B,-2	;We want 2 pages starting at c(B)
	MOVEM B,TTYPAG
	ADD A,[(C)]	;Indexed by C
	MOVEM A,TTYSTS
	MOVE A,[SQUOZE 0,NCT]
	.EVAL A,
	  .LOSE
	MOVEM A,NTTYS
	SYSCAL CORBLK,[%CLIMM,,%CBNDR
		       %CLIMM,,%JSELF
		       TTYPAG
		       %CLIMM,,%JSNUM]
	  .LOSE %LSSYS
	SYSCAL OPEN,[%CLBIT,,.UAO\%TJDIS
		     %CLIMM,,TTYo
		     [SIXBIT /TTY/]]
	  .LOSE %LSFIL

	MOVEI C,1	;Start at T01
	SETZB E,B	;Pointer to freeTTYs array

GETFTY:	HRRE D,@TTYSTS		;Save all these for now so they don't change
	SKIPG D
	  JRST [AOJ E,
		MOVEM C,FRETTY(E)
		JRST .+1]
	AOJ C,
	CAME C,NTTYS
	  JRST GETFTY
	MOVEM E,NUMFRE

JCLp:	.Suset [.rOPTION,,TT]
	Tlne TT,OPTCMD
	  Jrst Other
FIND:	.Suset [.rCNSL,,I]
	Move A,@TTYLOC	;# groups.
	Movei G,1
Find2:	Hlrz C,@TTYLOC	;#ranges
	Aoj G,		;Point to 1st range.
	Call TTYinP
	  Jrst [Soje A,Other
		Add G,C
		Jrst Find2]
	Movem A,GGroup
	Skipa

Other:	Setom GGroup	;-1 means all groups.

PRTFTY:	Setz G,
	MOVE A,@TTYLOC	;# groups in ac A
	Movei G,1

GROUP:	HLRZ C,@TTYLOC	;# entries in C
	Skipl GGroup
	  Camn A,GGroup
	    Skipa
	      Aoja G,NGROUP
	.Iot TTYo,[^P]
	.Iot TTYo,["A]
	HRRZ T,@TTYLOC	;location of name in D
	CALL TYPE
	.IOT TTYo,[":]
	SETZB E,H	;# matches this group
	Setom LasTTY
	Setzm Linear
	Aoj G,		;Point at 1st pair.

SCAN:	AOJ H,
	MOVE I,FRETTY(H)
	Call TTYinP
	  Jrst NScan

Aha:	Aoj E,
	Move T,I
	Sub T,LasTTY
	Cain T,1
	  Jrst [Setom Linear
		Jrst NScan-1]
	Skipn Linear
	  Jrst PTTY
	.Iot TTYo,["-]
	Move T,LasTTY
	Call TTYprt
	Setzm Linear

PTTY:	.Iot TTYo,[" ]
	MOVE T,I
	Call TTYprt

	Movem I,LasTTY
NSCAN:	CAMG H,NUMFRE
	  JRST SCAN
	Skipn Linear
	  Jrst NSUBGR
	.Iot TTYo,["-]
	Move T,LasTTY
	Call TTYprt

NSUBGR:	JUMPE E,[MOVEI T,[ASCIZ /None/]
		 CALL TYPE
		 JRST .+1]

NGROUP:	Add G,C
	SOJN A,GROUP

	move tt,sysnam
	came tt,[sixbit \ML\]
	  jrst die
	.suset [.rcnsl,,tt]
	caie tt,7
	  jrst die
	.suset [.runame,,tt]
	hlres tt
	came tt,[-1]
	  jrst die
	movei t,[ascii /ADon't be a resource-wasting twit;  log out this terminal.A/]
	call type

DIE:	.LOGOUT 1,

TYPE:	HRLI T,440700
	ILDB TT,T
	Skipn TT
	  Return
	.IOT TTYo,TT
	JRST TYPE+1

TTYprt:	.Iot TTYo,["T]
	IDivi T,10
	Addi T,"0
	.Iot TTYo,T
	Addi TT,"0
	.Iot TTYo,TT
	Return

TTYinP:	Move T,G	;I=TTY#, G=Group^
	Move TT,C
TTP2:	Hlrz L,@TTYLOC	;lower
	Hrrz U,@TTYLOC	;upper.
	Caml I,L
	  Camle I,U
	    Jrst [Aoj G,
		  Sojn TT,TTP2
		  Jrst .+2]
	Aos (P)
	Move G,T
	Return

	END GO