TITLE TTY OUTPUT SPY

.MLLIT==1

A=1
B=2
C=3
D=4
N=5
OBP=6
CH=7
P=17

TTYO=10
TTYI=11
DIRI=12

DEFINE UTYI AC
	CAMN OBP,@TOOP
	 .HANG
	CAMN OBP,@TOBEP
	 JRST [	MOVE OBP,@TOBBP
		JRST .-3]
	ILDB AC,OBP
	ANDI AC,377
TERMIN

DEFINE SYSCAL A,B
	.CALL [SETZ ? SIXBIT \A\ ? B ((SETZ)) ]
TERMIN

GO:

LOC 42
	JSR TSINT
LOC GO
	MOVEI P,PDL
	.SUSET [.RSNAME,,SNAME]
	.CALL [SETZ
		SIXBIT /OPEN/
		MOVSI .UAO
		MOVEI TTYO
		[SIXBIT /DSK/]
		[SIXBIT /DIRT/]
		[SIXBIT />/]
		SETZ SNAME]
	 .VALUE
	SKIPE RUNFLG
	 JRST SETDON
	SETOM RUNFLG
	MOVE A,[-NSYMS,,SYMS]
SYMLP:	MOVE B,(A)
	.EVAL B,
	 .VALUE
	MOVEM B,(A)
	AOBJN A,SYMLP
	MOVE A,SYSCN
	MOVEI B,1
	PUSHJ P,MAKPAG
	MOVE A,TOIP	;MAKE SURE WE HAVE THE PAGES THE BUFFER POINTERS ARE IN
	MOVE B,NCT
	PUSHJ P,MAKPAG
	MOVE A,TOOP
	PUSHJ P,MAKPAG
	MOVE A,TOBEP
	PUSHJ P,MAKPAG
	MOVE A,TOBBP
	PUSHJ P,MAKPAG
SETDON:
GO1:	.SUSET [.SMASK,,[%PIIOC+%PITYI]]
	SETZM VPOS
	SETZM JCL
	MOVE A,[JCL,,JCL+1]
	BLT A,JCL+20
	.BREAK 12,[5,,JCL]
	SKIPN JCL
	 JRST [	SKIPE DEBUG
		 .VALUE
		.VALUE [ASCIZ \:USE JCLKILL \]]
	.VALUE [ASCIZ /:GENJOB
:PROCED
/]
	PUSHJ P,GETNUM
	ANDI N,77
	MOVSI A,N
	IORM A,TOIP	;SET UP INDEX FIELD FOR INDIRECTION
	IORM A,TOOP
	IORM A,TOBEP
	IORM A,TOBBP
	MOVE A,@TOBBP
	MOVE B,@TOBEP
	SUB B,A
	PUSHJ P,MAKPAG	;MAKE SURE WE HAVE THE PAGE(S) THE BUFFER ITSELF IS IN
	.IOT TTYO,[^P]	;BOY, IF WE'RE NOT A DISPLAY TERMINAL,
	.IOT TTYO,["C]	; WILL WE LOSE!
	MOVE A,TOIP
	MOVE B,FTCTYP	;TV'S HAVE NO INFO IN TOOP, SO MUST USE TOIP.
	CAIN B,%TNTV
	 MOVEM A,TOOP
	MOVE OBP,@TOOP	;HAVE TO START SOMEWHERE
	CAMN OBP,@TOBEP
	 MOVE OBP,@TOBBP
	IBP OBP
LPE:
LP:	UTYI A
	TRZE A,%TXDIS
	 JRST TYPD	;DISPLAY OR CURSOR POS, DISPATCH
	.IOT TTYO,A
	JRST LP

TYPD:	CAIL A,TYPDMX
	 JRST LPE
	JRST @TYPDTB(A)
DTB::
TYPDTB:	TYMOV	;MOVE CURSOR
	TYMOV1	;DUMMY FOR ABOVE
	TYEEOF	;CLEAR EOF
	TYEEOL	;CLEAR EOL
	TYDELF	;DELETE FWD
	LPE	;TERMINET MOTOR ON
	LPE	;   "       "   OFF
	TYECRL	;CRLF TO DATAPOINTS & IMLACS
	LPE	;"NOP FOR SUPERDUPER IMAGE MODE"
	TYEBS	;BS
	TYELF	;LF
	TYECRL	;CARRET
	LPE	;OUTPUT RESET
	LPE	;QUOTE
	TYEFS	;FWD SPACE
	TYMOV1	;MOVE CURSOR NEW STYLE
	TYECLR	;CLEAR SCREEN
TYPDMX==.-TYPDTB

TYMOV:	UTYI C	;OLD VPOS
	UTYI B	;OLD HPOS OR 201
	UTYI A	;NEW VPOS
	UTYI B	;NEW HPOS
	SKIPE FTCTYP
	 JRST TYMOV0
	SUB A,C
	ADD A,VPOS
	EXCH A,B
	IDIV B,VSZ
	MOVE B,C
	EXCH A,B
TYMOV0:	MOVEM A,VPOS
	MOVEI A,10(A)
	MOVEI B,10(B)
	.IOT TTYO,[^P]
	.IOT TTYO,["V]
	.IOT TTYO,A
	.IOT TTYO,[^P]
	.IOT TTYO,["H]
	.IOT TTYO,B
	JRST LPE
TYMOV1:	UTYI A	;NEW VPOS
	UTYI B	;NEW HPOS
	SKIPE FTCTYP
	 JRST TYMOV0
	EXCH A,B
	IDIV B,VSZ
	MOVE B,C
	EXCH A,B
	JRST TYMOV0

TYEFS:	MOVEI A,"F
	JRST PTYO

TYECLR:	MOVEI A,"C
	JRST PTYO

TYEEOF:	MOVEI A,"E
PTYO:	.IOT TTYO,[^P]
	.IOT TTYO,A
	JRST LPE
TYDELF:	MOVEI A,"F
	JRST PTYO
TYEEOL:	MOVEI A,"L
	JRST PTYO
TYECRL:	.IOT TTYO,[^M]
	JRST LPE
TYEBS:	.IOT TTYO,[^H]
	JRST LPE
TYELF:	.IOT TTYO,[^J]
	AOS VPOS
	JRST LPE


GETNUM:	MOVE C,[440700,,JCL]	;TRY OCTAL FIRST
	SETZ N,
GETNLP:	ILDB A,C
	CAIN A,^M
	 POPJ P,
	CAIG A,"7
	CAIGE A,"0
	 JRST NAM	;MUST BE A UNAME
	SUBI A,"0
	LSH N,3
	IORI N,(A)
	JRST GETNLP

NAM:	MOVNI D,6  ;SET UP FOR 6 CHARS, INCL SPACES IF NECESS
	MOVE C,[440700,,JCL]
NAM1:	ILDB A,C
	CAIN A,^M
	 JRST NAM2
	CAIN A,40
	 JRST NAM2
	CAIL A,140
	 SUBI A,40
	DPB A,C
	AOJL D,NAM1
	JRST DIR1
NAM2:	MOVEI A,40
	DPB A,C
	AOJGE D,DIR1
	IDPB A,C
	AOJL D,.-1

DIR1:	MOVE D,JCL
	CAMN D,[ASCII /SYS  /]
	 JRST [	SKIPL N,@SYSCN
		 POPJ P,
		JRST .+1]
	.OPEN DIRI,[0,,SIXBIT /   TTY.FILE.(DIR)/]
	 .VALUE
	MOVNI B,2
DIRLP1:	.IOT DIRI,A
	CAIE A,^J
	 JRST DIRLP1
	AOJL B,DIRLP1
DIRLP2:	MOVNI D,6	;NO. CHARS IN UNAME
	MOVE C,[440700,,JCL]
	.IOT DIRI,A	;"T" AT START OF LINE
	CAIN A,"D
	 JRST FLSLIN
	CAIE A,"T
	 JRST [	SKIPE DEBUG
		 .VALUE
		.VALUE [ASCIZ \:NOT LOGGED IN?KILL \]]
	.IOT DIRI,TMN
	.IOT DIRI,TMN+1
	.IOT DIRI,A	;SPACE
DIRLP3:	.IOT DIRI,A	;1ST CHAR OF UNAME
	ILDB B,C
	CAIE B,(A)
	 JRST FLSLIN	;NOT ON THIS LINE OF THE DIR
	AOJL D,DIRLP3
	MOVE N,TMN	;WE FOUND HIM
	SUBI N,"0
	LSH N,3
	MOVE A,TMN+1
	SUBI A,"0
	ADDI N,(A)
	.CLOSE DIRI,
	POPJ P,
FLSLIN:	.IOT DIRI,A
	CAIE A,^J
	 JRST FLSLIN
	JRST DIRLP2

7TYPE:	HRLI A,440700
7TYLP:	ILDB B,A
	JUMPE B,CPOPJ
	.IOT TTYO,B
	JRST 7TYLP
CPOPJ:	POPJ P,

;GET THE SAME PAGE THE SYS JOB HAS IN ITS PAGE WHOSE ADDRESS IS IN A, # WORDS IN B
MAKPAG:	ANDI A,-1
	PUSH P,A
	ASH A,-10.
	MOVEM A,CBLK1	;GET FIRST PAGE
	.CALL CBLK
	 .VALUE
	POP P,A
	ADDI A,-1(B)	;ADDRESS OF LAST WORD
	ASH A,-10.
	CAMN A,CBLK1
	 POPJ P,
	MOVEM A,CBLK1
	.CALL CBLK	;CROSSES PAGE BOUNDARY, GET SECOND PAGE
	 .VALUE
	POPJ P,

TSINT:	0
	0
	EXCH A,TSINT
TSIN1:	TRZE A,%PIIOC	;TYPICALLY ILLEGAL CHAR AFTER ^P
	 JRST [	AOS TSINT+1	;SO FORGET IT
		JRST INTRET]
	TRZE A,%PITYI
	 JRST [	MOVEI CH,TTYI
		.ITYIC CH,
		JRST INTRET
		CAIE CH,^Z
		 JRST VALRT
		JRST INTRET]
	.VALUE [ASCIZ \:UNKNOWN INTERRUPT?î\]
INTRET:	JUMPN A,TSIN1
	MOVE A,TSINT
	.DISMIS TSINT+1

VALRT:	CAIN CH,40
	 .IOT TTYI,CH		; EAT UP SPACE
	CAIN CH,^L
	 JRST VAL1
	.IOT TTYO,[^M]
	JRST BYEBYE
VAL1:	.IOT TTYI,CH		; EAT UP ^L
	.IOT TTYO,[^P]
	.IOT TTYO,["C]
BYEBYE:	.BREAK 16,160000


CBLK:	SETZ
	SIXBIT \CORBLK\
	1000,,210000	;READ ACCESS, FAIL IF CAN'T
	[-1]	;PUT PAGE IN SELF
	CBLK1
	SETZI 400000	;GET FROM SYS JOB
CBLK1:	0	;GETS -#PAGES,,PAGE # TO START

JCL:	BLOCK 20
TMN:	0 ? 0	;HOLDS 2 DIGIT TTY NO. FOR DIRI
SNAME:	0
LTCTYP:	0	;LOCAL TCTYP
FTCTYP:	0	;FOREIGN TCTYP
VSZ:	0	;LOCAL VERTICAL SCREEN SIZE
HSZ:	0	;  "   HORIZONTAL  "    "
VPOS:	0	;CURRENT (HOPEFULLY) LOCAL VERTICAL CURSOR POS
RUNFLG:	0	;NONZERO IF SETUP DONE ALREADY
DEBUG:	-1	;-1 => INHIBIT :KILLS.
SYMS:
IRPS X,,[NCT TOBEP TOBBP TOIP TOOP SYSCN]
X:	SQUOZE 0,X
TERMIN
NSYMS==.-SYMS
CONSTA ? VARIAB
LPDL==20
PDL:	BLOCK 20
PATCH":	BLOCK 100

END GO

