;<134-TENEX>PSDOTY.MAC;2     1-Feb-80 14:11:35    EDIT BY PETERS
; Changed name of REPTY JSYS to RLPTY
;<133-TENEX>PSDOTY.MAC;20    23-JUN-75 15:27:08    EDIT BY UNTULIS
;<133-TENEX>PSDOTY.MAC;18    20-JUN-75 16:18:44    EDIT BY UNTULIS
;ADD CODE TO RELEASE PTY ASSIGNED AS PIPELINE
;<133-TENEX>PSDOTY.MAC;17    17-JUN-75 10:57:46    EDIT BY UNTULIS
;<133-TENEX>PSDOTY.MAC;16    13-JUN-75 14:42:37    EDIT BY UNTULIS
;<133-TENEX>PSDOTY.MAC;15    13-JUN-75 13:48:11    EDIT BY UNTULIS
;CLEANED UP VALIDITY CHECKING IN REPTY AND ADDED NOINT
;<133-TENEX>PSDOTY.MAC;14    12-MAY-75 12:35:03    EDIT BY UNTULIS
;CORRECT COMMENTS
;<133-TENEX>PSDOTY.MAC;12    30-APR-75 10:03:19    EDIT BY LYNCH
;ADD CAPABILITY TO IGNORE SYSTEM FULL CHECK IN ASPTY
;<133-TENEX>PSDOTY.MAC;8    12-MAR-75 16:57:38    EDIT BY UNTULIS
;ADD FULL SYSTEM CHECK TO ASSPTY
;ADD INSTRUCTION TO PREPARE CHARACTER FOR STI
	SEARCH	PROLOG
	TITLE	PSDOTY


	INTERN	.ASPTY,.RLPTY,.PSTI,.PSTO
	EXTERN	EDISMS,SKMRTN,TTCIBF,TTCOBF,TTOCT,CHKTTY
	EXTERN	TTSND,TTSTI1,TTIMAX,TTICT
	EXTERN	MENTR,MRETN,TTFORK,CAPENB

IFG NPTY, <
GS	PTYFRE,1		;POINTER TO FREE PTY LIST
GS	PTYJOB,NPTY		;TABLE OF OWNING FORKS FOR PTYS
	>
;
;		INITIALIZE PSEUDO TTY QUEUE
;
PSDINI::
IFG NPTY, <
	SETZM	,PTYFRE		;INITIALIZE FREE LIST PTR
	MOVEI	1,NPTY-1
	MOVSI	2,777777	;SET END OF FREE LIST
	MOVEM	2,PTYJOB(1)	;INTO END OF PTYFRK LIST
	HRLZ	2,1		;PUT POINTERS IN LEFT HALF
	SOJGE	1,.-2
	RET
	>
	SWAPCD
;	JSYS ADDED FOR PSEUDO TTY STUFF
;
;

;
;	ASSIGN PSEUDO TTY RETURNS WITH TERMINAL DESIGNATOR
;	IF ONE AVAILABLE, OTHERWISE FRETURN.
;
.ASPTY:	JSYS	MENTR
	CALL	ASSPTY
	JRST	MRETN
	UMOVEM	2,1		;RETURN TO USER IN AC1
	JRST	SKMRTN


;
;	RELEASE PSEUDO TTY, AC#1 HAS TERMINAL DESIG AFTER CHKTTY
;	IF AC1  HAS -1 RELEASE ALL OWNED BY THIS JOB
;

.RLPTY:	JSYS	MENTR
	CAIGE	1,0	;IF AC1 >=0 JUST ONE PTTY
	JRST	.REPY0
	CALL	CHKTTY
	JRST	MRETN
	SUBI	2,PTYLO		;MAKE INTO PTTY INDEX
	CAIGE	2,NPTY
	SKIPGE	2
	JRST	MRETN
	MOVE	1,PTYJOB(2)
	CAME	1,JOBNO		;DOES ENTRY BELONG TO THIS JOB
	JRST	MRETN		;NO,BAD REQUEST
	HLRZS	A		;ISOLATE LINK FIELD
	JUMPG	A,MRETN		;CAN'T RELEASE ALREADY RELEASED SLOT
	CALL	PTYREL
	JRST	SKMRTN

.REPY0:	MOVEI	2,0
.REPY1:	MOVE	1,PTYJOB(2)	;SEE WHO OWNS
	CAME	1,JOBNO
	JRST	.REPY2
	HLRZS	A		;ISOLATE LINK FIELD
	JUMPG	A,.REPY2	;CAN'T RELEASE ALREADY RELEASED SLOT
	CALL	PTYREL		;RELEASE THIS PTTY
.REPY2:	AOJ	2,
	CAIGE	2,NPTY
	JRST	.REPY1
	JRST	SKMRTN


;
;	DO PSEUDO TTY STI FUNCTION GET AN FRET IF INPUT BUFFER
;	FULL
;
.PSTI:	JSYS	MENTR
	CALL	CHKTTY
	JRST	MRETN	;INVALID TTY# OR UNOWNED PTTY
	UMOVE	3,2	;GET CHAR IN AC3
	CALL	TTPSTI
	JRST	MRETN	;INPUT BUFFER FULL
	JRST	SKMRTN	;SUCESS.

;
;	DO PSEUDO TTY STO FUNCTION.
;	AC1 HAS TERMINAL DESIGNATOR
;	AC2 WILL HAVE CHAR IF SUCESSFUL
;	GET AN FRET ON OUTPUT BUFFER EMPTY
;	IF AC2 HAS VALUE OF -1, THEN BLOCK UNTIL OUTPUT BUFFER
;	IS NOT EMPTY AND RETURN WITH CHAR, OR UNTIL
;	PSEUDO TTY PROCESS BLOCKS FOR INPUT, THEN FRET
;
.PSTO:	JSYS	MENTR
	CALL	CHKTTY
	JRST	MRETN	;INVALID TTY# OR UNOWNED PTTY
	UMOVE	5,2	;GET BLOCK OPTION IN AC5 FOR TTPSTO
	CALL	TTPSTO
	JRST	MRETN	;OUTPUT BUFFER EMPTY
	UMOVEM	3,2	;GIVE CHAR TO USER
	JRST	SKMRTN
;	SIBF - SKIP IF INPUT BUFFER FULL
;	CLOBBERS AC1,AC4
;	RETURNS # OF CHARS IN BUFFER IN AC1
	SWAPCD
TTSIBF::LDB	4,TTIMAX	;MAX BYTES OF INPUT BUFFER.
	MOVE	1,TTICT(2)	;CURRENT # IN BUFFER
	CAML	1,4
	AOS	0(P)		;SKIP IS FULL
	RET
IFG NPTY, <
	RESCD
PTYASS::PIOFF
	SKIPGE	2,PTYFRE	;GET POINTER
	JRST	[PION
		POPJ	P,]
	HLRZ	2,2		;GET LINK FIELD TO AN INDEX VALUE
	MOVE	1,PTYJOB(2)	;GET NEXT LINK
	MOVEM	1,PTYFRE	;GET NEXT LIST ENTRY
	PION
	AOS	(P)
	POPJ	P,
;
;	RELEASE A LINE. AC2 HAS LINE# FOR PTY
;
PTYREL::PUSH	P,2
	ADDI	2,PTYLO
	HLRZ	1,TTFORK(2)
	CAIE	1,0
	CAIL	1,NJOBS
	JRST	PTYRL1
	CAME	1,JOBNO		;ASSIGNED TO THIS JOB (PIPELINE TO ANOTHER FORK)
	JRST	PTYRL0		;NOT ASSIGNED TO THIS JOB, GO LOG IT OUT
	MOVE	1,2		;GET TTY NUMBER
	TRO	1,400000	;MAKE IT A DESIGNATOR
	RELD			;RELEASE FROM THIS JOB
	JFCL			;IGNORE ERRORS
	JRST	PTYRL1		;GO RELEASE PTY
PTYRL0:	CALL	TTCOBF		;CLEAR OUTPUT FIRST
	NOINT
	PUSH	P,CAPENB
	MOVEI	3,WHEEL
	IORM	3,CAPENB
	LGOUT
	JFCL
	POP	P,CAPENB
	OKINT
PTYRL2:	MOVEI	1,^D1000
	DISMS
	CALL	TTCOBF
	SKIPL	TTFORK(2)
	JRST	PTYRL2
PTYRL1:	POP	P,2
	PIOFF
	MOVE	1,PTYFRE
	MOVEM	1,PTYJOB(2)
	HRLZM	2,PTYFRE	;MAKE INDEX FIELD A LINK VALUE
	PION
	RET
;
;
SWAPCD
;	ASSIGN A PSEUDO TTY TO A FORK..
;
;
ASSPTY::UMOVE	1,1		;GET SYSTEM FULL CHECKING FLAG
	JUMPL	1,ASSPT1	;JUMP IF NOT FOR STARTING JOB 
	MOVEI	1,0
	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.
	CAIE 1,0		;IS LOGIN ALLOWED
	POPJ	P,		;NO, SYSTEM FULL, ERROR IN 1
ASSPT1:	CALL	PTYASS		;GET A LINE IF AVAILABLE
	POPJ	P,		;NONE FREE
	MOVE	1,JOBNO		;GET INDEX OF CALLING JOB
	MOVEM	1,PTYJOB(2)	;MARK AS OWNED
	ADDI	2,PTYLO
	CALL	TTCOBF
	CALL	TTCIBF
	ADDI	2,400000	;MAKE A TTY DESIGNATOR
	AOS	(P)		;REPORT SUCCESS
	POPJ	P,
;
;
;	PTY STI ROUTINE USES ITS OWN ,LOGIC DIFFERENT FROM BBN STI
;	AC3 HAS THE CHARACTER IN IT, AC2 HAS PTTY NUMBER
;
;	PROVIDES FRET ON INPUT BUFFER FULL
;	OR IF NOT SUFFICIENT ROOM IN BIG BUFF, AS PARMD BY FNUM
TTPSTI:: 
	CALL	TTSIBF		;SEE IF ROOM IN BUFFER
	JRST	.+2
	POPJ	P,	;YES
	HRR	1,3		;PUT CHAR IN BIG BUFF WORD
	ANDI	1,377	;PREPARE FOR STI ***SRI-AIC***
	PUSH P,6
	PUSHJ P,TTSTI1
	POP P,6
	AOS	(P)
	POPJ	P,
;
;	PSEUDO TTY STO GETS A CHAR OUT OF OUTPUT BUFFER OF PTY
;	AC2 HAS PTY #
;	AC3 GETS CHARACTER IF THERE
;	AC5 HAS BLOCKING OPTION, IF <0, BLOCK UNTIL CHAR IN	>
;		OUTPUT BUFFER, OR PROCESS BLOCKS FOR INPUT
;	PROVIDES FRETURN IF OUTPUT BUFFER EMPTY
;
;
TTPSTO::CALL GETCH
	JRST	TPSTO1
	AOS	(P)	;HAVE A CHAR IN AC3, S RETURN TO CALLER IN JSYS
	POPJ	P,
TPSTO1:	CAIL	5,0	;LOOK FOR BLOCKING OPTION
	JRST	[POPJ	P,]	;NO OPTION, FRETURN
	MOVEI	1,STOTST	;TEST ADDR FOR SCHED
	HRLI	1,0(2)		;LINE # IN LEFT HALF
	JSYS	EDISMS
	CALL	GETCH		;SEE IF CHAR THERE
	JRST	[HRREI	6,-1	;FORK BLOCKED ON INPUT
		 UMOVEM	6,2	;-1 TO CALLER AC2
		 POPJ	P,	]
	AOS	(P)
	POPJ	P,
;	SCHEDULER TEST FOR STEAL OUTPUT
;	WILL WAKEUP IF OUTPUT BUFFER GETS A CHARACTER
;	OR IF PROCESS BLOCKS FOR INPUT
;
;
RESCD
STOTST::SKIPE	TTOCT(1)	;TEST OUTPUT BUFF CONTENTS
	JRST	1(4)		;NOT EMPTY, WAKEUP
	HRLZ	2,TTFORK(1)
	SKIPGE	2
	JRST	0(4)		;TRY LATER
	JRST	1(4)		;WAKE UP
;
;	GETCHARACTER SEE IF ANYTHING IN BUFFERS
;	PTY # IN AC2
;	RETURNS +1 IF BUFFERS EMPTY, +2 IF NOT EMPTY, WITH CHAR IN AC3
GETCH:	PIOFF
	JSP 4,TTSND
	CAIA 
	AOS (P)
	PION
	POPJ	P,
	>
	END
