Google
 

Trailing-Edge - PDP-10 Archives - BB-4170G-SM - sources/filmsc.mac
There are 52 other files named filmsc.mac in the archive. Click here to see a list.
;<3A.MONITOR>FILMSC.MAC.4, 17-May-78 11:10:53, EDIT BY MILLER
;FIX UP HANDLING OF RSCNBP TO AVOID SYSTEM CRASHES. CURRENT RSCAN
; CODE IS STILL WRONG AND FULL OF RACES
;<3A.MONITOR>FILMSC.MAC.3, 22-Feb-78 16:33:07, EDIT BY MILLER
;RETRY IN PTYIN IF TTSTO DOESN'T SKIP
;<3A.MONITOR>FILMSC.MAC.2, 20-Feb-78 14:54:01, EDIT BY MILLER
;FIX TYPEO IN TTYOPN
;<3.SM10-RELEASE-3>FILMSC.MAC.1,  6-Dec-77 15:01:03, EDIT BY MILLER
;CHECK FOR TTY ACCESS IN CERTAIN TTY MTOPRS.
;CHANGE RETBAD TO RETERR IN RSCAN JSYS
;<3-MONITOR>FILMSC.MAC.102,  7-Nov-77 13:01:55, EDIT BY KIRSCHEN
;MORE COPYRIGHT UPDATING...
;<3-MONITOR>FILMSC.MAC.101, 12-Oct-77 13:46:36, EDIT BY KIRSCHEN
;UPDATE COPYRIGHT FOR RELEASE 3
;<3-MONITOR>FILMSC.MAC.100, 19-Sep-77 09:54:36, EDIT BY HELLIWELL
;FIX PROTECTION AND STATUS INSERTION DISPATCHES IN NILDTB
;<3-MONITOR>FILMSC.MAC.99, 17-Aug-77 16:37:14, EDIT BY HURLEY
;FIX TTY AND PTY HUNGRY MTOPR FOR LINE ACTIVATION
;<3-MONITOR>FILMSC.MAC.98, 11-Aug-77 14:33:40, EDIT BY MILLER
;CHECK FOR DETACHED JOB IN TTYIN CODE
;<3-MONITOR>FILMSC.MAC.97, 30-Jul-77 01:43:08, EDIT BY CROSSLAND
;REMOVE SPECIAL CHECKS FOR NVT'S
;<3-MONITOR>FILMSC.MAC.96, 17-Jun-77 14:45:27, EDIT BY HALL
;TCO 1740 - MAKE PTYCLZ NOSKED WHILE DEASSIGNING TTY DATA AND 
;	CLEARING PTYSTS (FIX RACE)
;<3-MONITOR>FILMSC.MAC.95, 17-Jun-77 00:24:20, EDIT BY CROSSLAND
;FIX UP NVTDET CALL
;<3-MONITOR>FILMSC.MAC.94, 24-May-77 10:34:31, EDIT BY KIRSCHEN
;TCO 1802 - ADD SUPPORT FOR .MOSIG (IGNORE INPUT) FUNCTION
;<3-MONITOR>FILMSC.MAC.93, 23-May-77 14:19:24, EDIT BY HALL
;MADE OPEN AND CLOSE OF 'TTY:' ALWAYS SUCCEED
;<3-MONITOR>FILMSC.MAC.92,  8-May-77 19:01:54, EDIT BY CROSSLAND
;<3-MONITOR>FILMSC.MAC.91,  6-May-77 11:53:06, EDIT BY HURLEY
;ADD SET INPUT/OUTPUT AND ATTRIBUTE CHECK ENTRIES IN DISPATCH TABLE
;<3-MONITOR>FILMSC.MAC.90,  3-May-77 17:40:21, EDIT BY CROSSLAND
;TCO 1742 MERGE ARPANET SOURCES
;<3-MONITOR>FILMSC.MAC.89,  2-May-77 20:32:14, EDIT BY BOSACK
;<3-MONITOR>FILMSC.MAC.88, 14-Apr-77 16:49:47, EDIT BY HALL
;TCO 1740 - MADE TTYDAS ASSUME DEVICE LOCK IS LOCKED RATHER THAN LOCKING
;<3-MONITOR>FILMSC.MAC.87, 13-Apr-77 18:34:22, EDIT BY HALL
;TCO 1740 - BUG FIX IN TTMSLN - FORGOT TO CALL ULKTTY
;<3-MONITOR>FILMSC.MAC.86,  6-Apr-77 14:31:09, EDIT BY HALL
;TCO 1740 - MAKE TTYOPN CHECK FOR 'TTY'
;<3-MONITOR>FILMSC.MAC.85,  9-Mar-77 16:06:18, EDIT BY HALL
;TCO 1740 - MADE TTYDAS RETURN FAILURE TO CALLER RATHER THAN WAIT
;<3-MONITOR>FILMSC.MAC.84, 23-Feb-77 20:11:54, EDIT BY HALL
;TCO 1740 - MASSIVE CHANGES FOR TELETYPE REORGANIZATION
;<3-MONITOR>FILMSC.MAC.83,  3-Feb-77 21:01:59, Edit by MCLEAN
;<3-MONITOR>FILMSC.MAC.82,  1-Feb-77 19:04:45, EDIT BY HURLEY
;<3-MONITOR>FILMSC.MAC.81,  1-Feb-77 18:15:56, EDIT BY HURLEY
;MAKE OUTPUT TO DEV TTY: ALWAYS GO TO CONTROLING TTY
;<3-MONITOR>FILMSC.MAC.80, 13-Jan-77 18:25:38, Edit by MCLEAN
;TCO 1669 EXTENDED ADDRESSING
;<3-MONITOR>FILMSC.MAC.79, 13-Jan-77 16:47:19, EDIT BY HALL
;COMMENTS
;<3-MONITOR>FILMSC.MAC.78, 12-Jan-77 15:50:54, Edit by MCLEAN
;<3-MONITOR>FILMSC.MAC.77, 11-Jan-77 16:04:18, Edit by MCLEAN
;<3-MONITOR>FILMSC.MAC.76, 11-Jan-77 15:20:35, EDIT BY HALL
;COMMENTS ON TTYDTB
;<3-MONITOR>FILMSC.MAC.75, 27-Dec-76 17:32:17, EDIT BY HURLEY
;<2-MONITOR>FILMSC.MAC.74,  4-Nov-76 15:13:52, EDIT BY HURLEY
;TCO 1604- ADD NEW MTOPR FCN CODE TO SUPPRESS TTY MSG
;<2-MONITOR>FILMSC.MAC.73, 22-Oct-76 23:28:35, EDIT BY OSMAN
;TCO 1618 - FIX TTY BIN TO NOT READ FROM RSCAN BUFFER IF
;	      BKJFN WAS JUST DONE (USE BACKED UP CHAR FIRST)
;<2-MONITOR>FILMSC.MAC.72, 18-Oct-76 13:54:46, EDIT BY HURLEY
;TCO 1606 - FIX NUL: TO NOT CRASH ON GDSTS OR SDSTS
;<2-MONITOR>FILMSC.MAC.71, 12-Oct-76 10:39:46, EDIT BY MILLER
;RETURN MTOPR RESULTS IN 3 INSTEAD OF 2
;<2-MONITOR>FILMSC.MAC.70, 11-Oct-76 08:42:48, EDIT BY HURLEY
;TCO 1579 - MAKE PTY'S BE IDEAL TERMINALS
;<2-MONITOR>FILMSC.MAC.69,  7-Oct-76 11:26:54, EDIT BY MILLER
;TCO 1570. ADD MTOPR'S TO SET LENGTH AND WIDTH OF TTY'S
;<2-MONITOR>FILMSC.MAC.68,  6-Oct-76 19:17:18, EDIT BY HURLEY
;<2-MONITOR>FILMSC.MAC.67,  6-Oct-76 17:02:27, EDIT BY HURLEY
;<2-MONITOR>FILMSC.MAC.66,  6-Oct-76 16:07:52, EDIT BY HURLEY
;TCO 1571 - MAKE RSCAN BUFFER BE DYNAMICALLY ASSIGNED
;<2-MONITOR>FILMSC.MAC.65,  1-Oct-76 16:44:20, EDIT BY HURLEY
;MAKE NUL DEVICE BE OPENABLE IN BOTH READ AND WRITE MODES
;<2-MONITOR>FILMSC.MAC.64, 30-Sep-76 17:31:04, EDIT BY OSMAN
;<2-MONITOR>FILMSC.MAC.63, 30-Sep-76 17:26:06, EDIT BY OSMAN
;TCOS 1551-1552 1 IN AC1 FOR RSCAN MEANS COUNT REMAINING CHARS
;<1B-MONITOR>FILMSC.MAC.62, 16-Jun-76 19:24:26, EDIT BY HURLEY
;<1B-MONITOR>FILMSC.MAC.61, 16-Jun-76 19:21:00, EDIT BY HURLEY
;MORE OF TCO 1352 - MAKE TTYIN USE JOBPT IF DEVICE IS TTY:
;<1B-MONITOR>FILMSC.MAC.60, 16-Jun-76 19:08:34, EDIT BY HURLEY
;<1B-MONITOR>FILMSC.MAC.59, 16-Jun-76 18:00:26, EDIT BY HURLEY
;TCO 1440 - ALLOW ALL OPERATIONS TO BE SUCCESSFUL TO NUL DEVICE
;<1B-MONITOR>FILMSC.MAC.58, 10-JUN-76 17:51:56, EDIT BY HURLEY
;MORE OF TCO 1352
;<1B-MONITOR>FILMSC.MAC.57, 10-JUN-76 16:44:24, EDIT BY HALL
;TCO 1397 - ADDED RANGE CHECK ON CHANNEL NUMBER IN PTENAI
;<1B-MONITOR>FILMSC.MAC.56,  7-JUN-76 14:15:38, EDIT BY HURLEY
;TCO # 1352 - MAKE DETACHED FORK TO NO LONGER WAIT ON DETACHED TTY
;<1B-MONITOR>FILMSC.MAC.55, 26-MAY-76 12:57:19, EDIT BY MILLER
;TCO 1320. SET REFUSE ADVICE AT JOB INIT
;<1MONITOR>FILMSC.MAC.54, 23-MAR-76 18:18:04, EDIT BY HURLEY
;<1MONITOR>FILMSC.MAC.53, 23-MAR-76 17:17:57, EDIT BY HURLEY
;TCO 1216 - MAKE ALL UNSUPPORTED DISPATCH ENTRIES RETURN AN ERROR CODE
;<1MONITOR>FILMSC.MAC.52,  2-MAR-76 13:14:37, EDIT BY HURLEY
;TCO #1140 - CLEAR PTYSTS BITS ON OPEN AND CLOSF
;<2MONITOR>FILMSC.MAC.51,  3-FEB-76 20:32:55, EDIT BY MILLER
;FIX MCO 42 EDIT TO INCLUDE A RET
;<2MONITOR>FILMSC.MAC.50, 29-JAN-76 18:09:13, EDIT BY MURPHY
;MCO #42
;<2MONITOR>FILMSC.MAC.49, 16-JAN-76 17:47:32, EDIT BY MURPHY
;<2MONITOR>FILMSC.MAC.48, 23-DEC-75 12:48:59, EDIT BY LEWINE


;THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY ONLY BE USED
;  OR COPIED IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE.
;
;COPYRIGHT (C) 1976, 1977, 1978 BY DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASS.

	SEARCH PROLOG
	TTITLE FILMSC		;MISC - TTY, PTY, STRING AND NIL
	SWAPCD

;SPECIAL AC DEFINITIONS USED HEREIN

DEFAC (U,Q1)			;UNIT NUMBER
DEFAC (IOS,Q2)			;DEVICE STATUS FLAGS
DEFAC (STS,P1)			;SEE GTJFN FOR FUNCTIONS
DEFAC (JFN,P2)
DEFAC (DEV,P4)
DEFAC (F1,P5)

	SUBTTL NUL DEVICE FUNCTIONS

NILDTB::IFIW!NILSET
	IFIW!NILLKP		;NAME LOOKUP
	IFIW!NILLKP		;EXT LOOKUP
	IFIW!NILVLK		;VER LOOKUP
	DTBBAD			;PROTECTION INSERTION
	DTBSKP			;ACCOUNT INSERTION
	DTBBAD			;STATUS INSERTION
	IFIW!NILOPN		;OPEN
	IFIW!NILSQI		;INPUT
	IFIW!NILSQO		;OUTPUT
	DTBSKP			;CLOSF
	DTBSKP			;RENAME
	DTBSKP			;DELETE
	DTBBAD (DUMPX6)		;DUMPI
	DTBBAD (DUMPX6)		;DUMPO
	DTBSKP			;MOUNT
	DTBSKP			;DISMOUNT
	DTBSKP			;INIT DIRECTORY
	DTBSKP			;MTOPR
	DTBBAD			;DEVICE STATUS READ
	DTBBAD			;DEVICE STATUS SET
	DTBSKP			; RECORD OUT
	IFIW!RFTADN		; READ TAD
	IFIW!SFTADN		; SET TAD
	DTBDSP (BIOINP)		;SET JFN FOR INPUT
	DTBDSP (BIOOUT)		;SET JFN FOR OUTPUT
	DTBBAD (GJFX49)		;CHECK ATTRIBUTE

	DTBLEN==:.-NILDTB	;GLOBAL LENGTH OF DISPATCH TABLE

NILOPN:	TQCE <RNDF>		;WANT APPEND?
	TQO <WRTF>		;YES. LIGHT WRITE BIT THEN
	SETZB A,B
	CAIN JFN,377777
	RET			;ALL DONE FOR THIS GUY
	TQNN <READF>		;IS IT READ?
	HRLOI A,377777		;NO. DO OUTPUT
	MOVEM A,FILCNT(JFN)	;STORE COUNT
	SETZB A,FILBYT(JFN)	;RETURN ZEROS
	RETSKP			;AND RETURN SUCCESSFULLY

NILSQO:	SETZB A,B
	CAIN JFN,377777
	 RET
	TQNN <READF>		;IS IT READ?
	HRLOI A,377777		;NO, SET UP LARGE COUNT
	MOVEM A,FILCNT(JFN)
	SETZB A,FILBYT(JFN)
	RET

NILSQI:	TQO <EOFF>		;GENERATE EOF ON INPUT
	RET

NILSET:	TQO <NNAMF,NVERF>
	JRST SK2RET


NILLKP:	JUMPE A,SK2RET		;IF FIRST TIME, ALLOW IT
	TQNN <STEPF>		;STEPPING
	JRST SK2RET		;NO, GIVE OK RETURN
	RETBAD (GNJFX1)		;NO MORE NAMES

NILVLK:	CAME A,[-2]		;FIRST TIME?
	TQNN <STEPF>		;OR NOT STEPPING
	RETSKP			;THEN SAY OK
	RETBAD (GNJFX1)		;NO MORE VERSIONS
	SUBTTL STRING DEVICE FUNCTIONS

STRDTB::DTBBAD (GJFX32)	;DIR SET
	REPEAT BIND-1,<
	DTBBAD (DESX9)>
	STRIN
	IFIW!STROUT
	REPEAT DELD-BOUTD,<
	DTBBAD (DESX9)>
	DTBBAD (DUMPX6)		;DUMPI
	DTBBAD (DUMPX6)		;DUMPO
	DTBBAD (DESX9)		;MOUNT
	DTBBAD (DESX9)		;DISMOUNT
	DTBBAD (DESX9)		;INITIALIZE DIR
	DTBBAD (MTOX1)		; MTOPR
	DTBBAD (DESX9)		;GET DEV STATUS
	DTBBAD (DESX9)		;SET DEV STATUS
	DTBSKP			;RECORD OUT
	IFIW!RFTADN		; READ TAD
	IFIW!SFTADN		; SET TAD
	DTBDSP (BIOINP)		;SET JFN FOR INPUT
	DTBDSP (BIOOUT)		;SET JFN FOR OUTPUT
	DTBBAD (GJFX49)		;CHECK ATTRIBUTE

	DTBLEN==:.-STRDTB	;GLOBAL LENGTH OF DISPATCH TABLE

STRIN:	XCTBU [ILDB A,JFN]
	RET

STROUT:	XCTBU [IDPB A,JFN]
	RET

;DECREMENT BYTE POINTER
; A/ BYTE PTR
;	CALL DBP
; RETURNS +1, CLOBBERS B AND C

DBP::
	MOVNI T2,1		;ADJUST BY -1
	ADJBP T2,T1
	MOVEM T2,T1		;RETURN RESULT
	RET			;AND DONE
	SUBTTL PTY DEVICE FUNCTIONS

;DEVICE DEPENDENT ROUTINES. TO DO CERTAIN FUNCTIONS, THE RH OF DEV
;CONTAINS THE NAME OF A TABLE SUCH AS PTYDTB, AND A ROUTINE IS CALLED
;VIA 'CALL @FOO(DEV)', WHERE FOO IS A SYMBOL FOR AN OFFSET INTO THE 
;TABLE. THE SYMBOLS ARE DEFINED IN PROLOG. IN THE COMMENTS BELOW,
;THE OFFSET IS INDICATED ALONG WITH THE PURPOSE OF THE ROUTINE

PTYDTB::IFIW!PTYSET		;DLUKD - DIRECTORY SETUP
	DTBBAD (DESX9)		;INLUKD - NAME LOOKUP
	DTBBAD (DESX9)		;ELUKD - EXTENSION LOOKUP
	DTBBAD (DESX9)		;VLUKD - VERSION LOOKUP
	DTBBAD (DESX9)		;PULKD - PROTECTION INSERTION
	DTBBAD (DESX9)		;ALUKD - ACCOUNT INSERTION
	DTBBAD (DESX9)		;SLUKD - STATUS INSERTION
	IFIW!PTYOPN		;OPEND - OPEN FILE
	IFIW!PTYIN		;BIND - BYTE INPUT
	IFIW!PTYOUT		;BOUTD - BYTE OUTPUT
	IFIW!PTYCLZ		;CLOSD - CLOSE FILE
	DTBBAD (DESX9)		;REND - RENAME
	DTBBAD (DESX9)		;DELD - DELETE FILE
	DTBBAD (DUMPX6)		;DMPID - DUMP MODE INPUT
	DTBBAD (DUMPX6)		;DMPOD - DUMP MODE OUTPUT
	DTBBAD (DESX9)		;MNTD - MOUNT DEVICE
	DTBBAD (DESX9)		;DSMD - DISMOUNT DEVICE
	DTBBAD (DESX9)		;INDD - INITIALIZE DIRECTORY OF DEVICE
	IFIW!PTMTOP		;MTPD - DO MTAPE OPERATION
	DTBBAD (DESX9)		;GDSTD - GET DEVICE STATUS
	DTBBAD (DESX9)		;SDSTD - SET DEVICE STATUS
	DTBSKP			;RECOUT - FORCE RECORD OUT (SOUTR)
	IFIW!RFTADN		;RFTADD - READ FILE TIME AND DATE
	IFIW!SFTADN		;SFTADD - SET FILE TIME AND DATE
	DTBDSP (BIOINP)		;SET JFN FOR INPUT
	DTBDSP (BIOOUT)		;SET JFN FOR OUTPUT
	DTBBAD (GJFX49)		;CHECK ATTRIBUTE

	DTBLEN==:.-PTYDTB	;GLOBAL LENGTH OF DISPATCH TABLE

	DEFSTR (PTYFRK,PTYSTS(U),35,18) ;FORK INDEX OF OWNER
	DEFSTR (PTYCHN,PTYSTS(U),17,6) ;PSI CHANNEL NUMBER

;BITS IN AC 'IOS' - SEE DEFAC AT BEGINNING OF MODULE

FLG(POPEN,L,IOS,400000)		;PTY IS OPEN
FLG(PIIEN,L,IOS,200000)		;INPUT INTERRUPT ENABLED
FLG(POIEN,L,IOS,100000)		;OUTPUT INTERRUPT ENABLED
FLG(PIIEV,L,IOS,040000)		;INPUT EVENT OCCURRED
FLG(POIEV,L,IOS,020000)		;OUTPUT EVENT OCCURRED
FLG(PTBAT,L,IOS,010000)		;PTY IS BEING CONTROLED BY BATCH
;FUNCTION DLUKD FOR PTY - DIRECTORY SETUP

PTYSET:	TQO <NNAMF,NVERF>	;SAY NO NAME, VERSION, ETC
	RETBAD (GJFX32)

;OPEND FUNCTION FOR PTY - OPEN FILE

;ACCEPTS:
;	JFN/ JOB FILE NUMBER
;	DEV/ (UNIT NUMBER,,DISPATCH ADDRESS)

;CALL PTYOPN

;RETURNS +1: FAILURE
; 	 +2: SUCCESS

PTYOPN:	STKVAR <PTYOLN>
	CALL CHKAPD		;FIX APPEND BIT
	HLRZ U,DEV		;GET UNIT NUMBER
	MOVE IOS,PTYSTS(U)
	TQOE <POPEN>		;ALREADY OPEN?
	RETBAD OPNX9		;YES
	HLRZ T2,DEV		;T2/ PTY NUMBER
	CALL PTYTTY		;GET CORRESPONDING TELETYPE NUMBER
	MOVEM T2,PTYOLN		;SAVE LINE NUMBER
	CALL STADYN		;IS LINE ALREADY ACTIVE?
	 SKIPA			;NO
	RETBAD (OPNX7)		;YES. INDICATE ASSIGNED TO ANOTHER JOB
	MOVE T2,PTYOLN		;T2/ LINE NUMBER
	CALL TTYASC		;ASSIGN DYNAMIC DATA FOR LINE
	 RETBAD			;FAILED
	MOVE T2,PTYOLN		;T2/ LINE NUMBER
	MOVEI T1,-1		;T1/ JOB NUMBER
	CALL STCJOB		;INDICATE NO CONTROLLING JOB FOR TERMINAL
	MOVE T2,PTYOLN		;T2/ LINE NUMBER
	CALL STADYN		;POINT TO DYNAMIC DATA
	 JRST PTYOP1		;SHOULDN'T HAPPEN
	CALL STPRM		;MAKE THE DATA PERMANENT UNTIL PTY IS CLOSED
	MOVX IOS,POPEN		;SET THAT PTY IS NOW OPEN
	HRR IOS,FORKX		;REMEMBER FORK DOING OPEN
PTYRET:	MOVEM IOS,PTYSTS(U)
	RETSKP

PTYOP1:	BUG(CHK,TTNAC1,<LINE NOT ACTIVE AT PTYOPN>)
	RETBAD (TTYX01)
;FUNCTION BIND FOR PTY - BYTE INPUT

;ACCEPTS:
;	DEV/ (UNIT,,DISPATCH ADDRESS)

;	CALL PTYIN

;RETURNS +1: ALWAYS,
;		T1/ A CHARACTER

;GETS A CHARACTER FROM THE ASSOCIATED TERMINAL'S OUTPUT BUFFER TO
;SERVE AS INPUT ON THE PTY.  BLOCKS IF NECESSARY. ALWAYS RETURNS WITH
;CHARACTER

PTYIN:	HLRZ 2,DEV
	CALL PTYTTY		;GET INTERNAL LINE NUMBER
	CALL LCKTTY		;GET ADDRESS OF DYNAMIC DATA AND PREVENT
				; DEALLOCATION
	 JRST [	CALL ULKTTY	;NOT FULLY ACTIVE. ALLOW DEALLOCATION
		TQO <ERRF>	;INDICATE ERROR
		RET]
	CALL TTSTO		;GET CHAR FROM LINE, BLOCK IF NONE
	 JRST PTYIN		;TRY AGAIN
	CALL ULKTTY		;ALLOW DEALLOCATION
	RET

;FUNCTION BOUTD FOR PTY - BOUT OUTPUT

;ACCEPTS:
;	T1/ A CHARACTER
;	DEV/ (UNIT,,DISPATCH ADDRESS)

;	CALL PTYOUT

;RETURNS +1: ALWAYS

;FOR THE ASSOCIATED TERMINAL, SIMULATES REMOVING THE SPECIFIED
;CHARACTER FROM TTBBUF AND PLACING IT IN THE LINE'S INPUT BUFFER
;(ISSUING AN INTERRUPT IF NECESSARY, ECHOING, ETC.)

PTYOUT:	HLRZ 2,DEV		;GET PTY NUMBER
	CALL PTYTTY		;CONVERT TO INTERNAL LINE NUMBER
	CALL TTSTI		;PUT CHAR INTO LINE
	RET
;FUNCTION CLOSD FOR PTY - CLOSE FILE

;ACCEPTS:
;	DEV/ (UNIT,,DISPATCH ADDRESS)

;	CALL PTYCLZ

;RETURNS +1: FAILURE
;	 +2: SUCCESS

;ON FAILURE, IF BLOCKING IS NECESSARY, BLKF IS SET AND T1 CONTAINS
;(LINE NUMBER,,ADDRESS OF TEST ROUTINE)

PTYCLZ:	STKVAR <PTYCLN>
	HLRZ 2,DEV
	CALL PTYTTY		;GET LINE NUMBER
	MOVEM T2,PTYCLN		;SAVE LINE NUMBER
	CALL LCKTTY		;POINT TO DYNAMIC DATA
	 JRST [	CALL ULKTTY	;NOT ACTIVE
		JRST PTYCL1]	;SKIP DEALLOCATION AND DETACH
	CALL CLRPRM		;CLEAR PERMANENT BIT SO DEASSIGN WILL HAPPEN
	CALL ULKTTY		;ALLOW DEALLOCATION
	MOVE T2,PTYCLN		;T2/ LINE NUMBER
	CALL GTCJOB		;GET OWNING JOB
	 JRST PTYCL1		;LINE NOT ACTIVE. DON'T DETACH OR DEALLOCATE
	CAIN T3,-1		;ANY JOB?
	JRST PTYCL2		;NO. GO DEALLOCATE DATA

;A JOB EXISTS ON THIS LINE. GO DETACH IT. THIS WILL CAUSE THE TERMINAL
;DATA BLOCK TO BE DEASSIGNED

	NOSKED
	CALL NTYCOF		;CAUSE CARRIER OFF ACTION
	OKSKED
	JRST PTYCL1

;NO JOB EXISTS ON THIS LINE.  DEASSIGN TERMINAL'S DATA BASE
;AND INDICATE THAT THE PTY IS CLOSED

PTYCL2:	NOSKED			;NO INTERFERENCE UNTIL TTY AND PTY AGREE
	MOVE T2,PTYCLN		;T2/ LINE NUMBER
	CALL TTYDEA		;DEALLOCATE THE DYNAMIC DATA
	 JRST [	OKSKED
		JRST PTYCL4]	;FAILED. 
	HLRZ U,DEV		;GET PTY NUMBER
	SETZM PTYSTS(U)		;INDICATE PTY CLOSED
	OKSKED
	RETSKP

;TERMINAL SIDE IS NOT ACTIVE. INDICATE PTY IS CLOSED

PTYCL1:	HLRZ U,DEV		;GET PTY NUMBER
	SETZ IOS,		;CLEAR OPEN BIT AND ALL OTHERS
	JRST PTYRET

;UNABLE TO DEASSIGN. RETURN TO CALLER, WHICH WILL WAIT

PTYCL4:	TXZN T1,1B0		;FAILURE OR WAIT?
	RETBAD			;FAILURE. RETURN ERROR CODE
	HRL T1,T2		;WAIT. T1/(LINE NUMBER,,ROUTINE ADDRESS)
	TQO BLKF		;INDICATE WAIT NECESSARY
	RETBAD			;TAKE FAILURE RETURN
;FUNCTION MTPD FOR PTY - MTOPR

;ACCEPTS:
;	USER T2/ FUNCTION CODE
;	USER T3/ ARGUMENT OR ADDRESS OF ARGUMENT BLOCK
;	DEV/ (PTY NUMBER,,PTYDTB)

;	CALL PTMTOP

;RETURNS +1: FAILURE
;	 +2: SUCCESS

PTMTOP:	HLRZ U,DEV		;GET PTY NUMBER
	UMOVE 2,2		;GET FUNCTION CODE FROM USER
	MOVEI 3,0(2)		;MTOPR OP CODE
	CAIN 3,.MOAPI		;ASSIGN PTY INTERRUPT CHANNEL?
	JRST PTENAI		;ENABLE INTERRUPTS
	CAIN 3,.MOPIH		;PTY HUNGRY?
	JRST PTSTIH		;TEST INPUT HUNGRY
	CAIE 3,.MOBAT		;SET BATCH BIT?
	RETBAD (MTOX1)		;ILLEGAL FUNCTION
	MOVX T1,PTBAT		;INITIALIZE BATCH BIT TO 0
	ANDCAM T1,PTYSTS(U)
	XCTU [SKIPE 3]		;SETTING THE BATCH BIT?
	IORM T1,PTYSTS(U)	;YES, PTY IS A BATCH PTY
	RETSKP

;MTOPR FUNCTION .MOPIH - SEE IF PTY IS HUNGRY

;RETURNS +2: ALWAYS
;	USER T2/ 0 FOR NOT HUNGRY
;		 -1 FOR HUNGRY

PTSTIH:	MOVE T2,U		;T2/ PTY NUMBER
	CALL PTYTTY		;CONVERT TO INTERNAL LINE NUMBER
	CALL LCKTTY		;GET ADDRESS OF DYNAMIC DATA, PREVENT DEALLOCATION
	 JRST [	CALL ULKTTY	;NOT ACTIVE. ALLOW DEALLOCATION
		SETZ T1,	;ASSUME NOT HUNGRY
		SKIPN T2	;UNLESS LINE IS NOT ACTIVE AT ALL
		SETO T1,	;THEN MAKE IT HUNGRY
		JRST PTSTI1]
	CALL TTSTIH		;TEST INPUT HUNGRY ON TTY
	CALL ULKTTY		;ALLOW DEALLOCATION
PTSTI1:	UMOVEM 1,2		;RETURN ANSWER TO USER
	RETSKP
;MTOPR FUNCTION .MOAPI - ASSIGN INTERRUPT CHANNELS

PTENAI:	MOVE 3,FORKX		;GET THIS FORK'S INDEX
	HRRM 3,PTYSTS(U)	;REASSIGN FORK
	LDB 3,[POINT 6,2,17]	;GET CHANNEL ASSIGNMENT
	SKIPL 3			;IS THE CHANNEL NON-NEGATIVE?
	CAIL 3,<^D36-1>		;IS IT < 35?
	RETBAD (MTOX18)		;BAD VALUE. INPUT HAS TO BE <=34
				;SO OUTPUT CAN BE <=35
	STOR 3,PTYCHN		;REMEMBER IT
	MOVX 3,PIIEN		;INPUT INTERRUPT ENABLED FLAG
	ANDCAM 3,PTYSTS(U)
	TLNE 2,(MO%WFI)		;SET IT ACCORDING TO B0 OF ARG
	IORM 3,PTYSTS(U)
	MOVX 3,POIEN		;OUTPUT INTERRUPT ENABLED FLAG
	ANDCAM 3,PTYSTS(U)
	TLNE 2,(MO%OIR)		;SET IT ACCORDING TO B1 OF ARG
	IORM 3,PTYSTS(U)
	CALL PTTINT		;SEE IF INTERRUPT NOW POSSIBLE
	RETSKP
;PTYINI - ROUTINE CALLED FROM OPENF TO INITIALIZE THE PTY PARAMETERS

;ACCEPTS:
;	DEV/ UNIT,,CODE

;	CALL PTYINI

;RETURNS +1: ALWAYS

;INITIALIZES VALUES FOR THE TERMINAL ASSOCIATED WITH THE GIVEN PTY
;THIS IS DONE BEFORE THE JOB IS CREATED ON THE LINE SO THAT THE OWNER
;OF THE PTY CAN CONTROL THE LINE.  OPENING THE PTY HAS CAUSED THE
;ASSOCIATED TERMINAL TO HAVE PERMANENT DYNAMIC DATA, WHICH WILL
;BE DEALLOCATED WHEN THE PTY IS CLOSED

PTYINI::HLRZ B,DEV		;GET PTY UNIT
	CALL PTYTTY		;CONVERT TO LINE NUMBER
	MOVEI A,.TTDES(B)	;GET DESIGNATOR (400000+N)
	MOVE B,NORMTF		;GET NORMAL SETTING
	SFMOD			;SET MODES
	STPAR			;AND THESE AS WELL
	MOVEI B,.TTIDL		;SET IT TO BE "IDEAL" TERMINAL
	STTYP			;SET THE TYPE
	DMOVE B,TTICB1		;DEFAULT CONTROL ACTIONS
	SFCOC
	HRLI A,(TL%CRO+TL%COR+TL%SAB+TL%STA) ;BREAK ALL LINKS AND SET REFUSE
	MOVEI B,-1		;ALL REMOTES
	TLINK			;GO DO IT
	 JFCL
	RET			;AND DONE

;GET FORK OF OWNER OF PTY
; 1/ TTY NUMBER
;	CALL PTYGFK
; RETURN +1: NO PTY FOR THAT TTY
; RETURN +2: SUCCESS,
;		T1/ FORKX OF OWNER

PTYGFK::CALL CHKPTY		;GET PTY NUMBER
	 RETBAD			;NO
	PUSH P,U		;YES. SAVE AC
	MOVE U,T1		;PUT PTY NUMBER IN NECESSARY AC
	LOAD 1,PTYFRK		;GET FORK (USES AC U)
	POP P,U			;RESTORE U
	RETSKP

;ROUTINE TO SEE IF A PTY IS A BATCH PTY
;ACCEPTS IN T1/	TTY NUMBER
;	CALL PTYGBB
;RETURNS +1:	NOT A PTY
;	 +2:	0 = NOT BATCH
;		-1 = BATCH PTY

PTYGBB::CALL CHKPTY		;GET PTY NUMBER
	 RETBAD			;NO. RETURN FAILURE
	MOVE T2,PTYSTS(T1)	;YES, GET STATUS WORD
	TXNN T2,PTBAT		;BATCH BIT ON?
	TDZA T1,T1		;NO
	SETO T1,		;YES
	RETSKP			;RETURN
;CHECK IF ACCESS TO TTY IS OK BECAUSE IT IS CONTROLLED BY PTY OF
; THIS JOB.
; 1/ TTY #
; RETURNS +1, 1/ 0= FALSE
;	-1 = TRUE (ACCESS OK)

PTCHKA::CALL CHKPTY		;GET PTY NUMBER
	 JRST PTCHK1		;NOT A PTY
	MOVX T2,POPEN
	TDNN T2,PTYSTS(T1)	;PTY IS OPEN?
	JRST PTCHK1		;NO
	HRRZ T2,PTYSTS(T1)	;OWNING FORK
	HLRZ T2,FKJOB(T2)	;OWNING JOB
	CAME T2,JOBNO		;IS OWNING JOB SAME AS THIS JOB?
PTCHK1:	TDZA 1,1		;NO, RETURN FALSE
	SETO 1,			;RETURN TRUE
	RET

;FIND JOB NUMBER OF JOB CONTROLLING PTY
; 1/ TTY NUMBER
; RETURN +1: 1/ JOB NUMBER, OR -1 IF NOT PTY OR PTY NOT OPENED

PTGETJ::CALL CHKPTY		;GET PTY NUMBER
	 JRST PTGJ1		;NOT A PTY
	MOVX T2,POPEN
	TDNN T2,PTYSTS(T1)	;PTY OPEN?
	JRST PTGJ1		;NO
	HRRZ T2,PTYSTS(T1)	;GET CONTROLLING FORK
	HLRZ T1,FKJOB(T2)	;GET JOB NUMBER
	RET

PTGJ1:	SETO T1,		;INDICATE NOT A PTY OR NOT OPEN
	RET
	RESCD

;FOLLOWING CODE RESIDENT BECAUSE CALLED FROM TTYSRV AT PI LEVEL

;ACCEPTS:
;	T1/ PTY NUMBER

;CALLED WHEN LINE IS GOING INTO INPUT WAIT

PTYFIN::PUSH P,IOS
	PUSH P,U
	MOVE U,T1		;GET PTY NUMBER IN CORRECT AC
	MOVX IOS,PIIEV		;INPUT EVENT FLAG
	JRST PTYF1

;ACCEPTS:
;	T1/ PTY NUMBER

;CALLED WHEN STARTING OUTPUT TO A LINE

PTYFOU::PUSH P,IOS
	PUSH P,U
	MOVE U,T1
	MOVX IOS,POIEV		;OUTPUT EVENT FLAG
PTYF1:	IORB IOS,PTYSTS(U)	;SET APPROPRIATE FLAG
	CALL PTTINT		;SEE IF INTERRUPT NOW POSSIBLE
	POP P,U
	POP P,IOS
	RET
;INITIATE INTERRUPT IF POSSIBLE

;ACCEPTS:
;	U/ PTY NUMBER

PTTINT:	PUSH P,1
	PUSH P,2
	SETCM 3,PTYSTS(U)	;GET COMPLEMENTED FLAGS
	TXNN 3,PIIEV+PIIEN	;SEE IF INPUT ENABLE AND EVENT BOTH SET
	JRST [	MOVX IOS,PIIEV	;YES, CLEAR EVENT
		ANDCAB IOS,PTYSTS(U)
		LOAD 1,PTYCHN	;DESIGNATED CHANNEL
		MOVEI 2,0(IOS)	;DESIGNATED FORK
		CALL PSIRQ	;REQUEST INTERRUPT
		JRST .+1]
	SETCM 3,PTYSTS(U)	;GET COMPLEMENTED FLAGS
	TXNN 3,POIEV+POIEN	;SEE IF OUTPUT ENABLE AND EVENT BOTH SET
	JRST [	MOVX IOS,POIEV	;YES, CLEAR EVENT
		ANDCAB IOS,PTYSTS(U)
		LOAD 1,PTYCHN	;ON DESIGNATED CHANNEL +1
		ADDI 1,1
		MOVEI 2,0(IOS)	;TO DESIGNATED FORK
		CALL PSIRQ	;REQUEST INTERRUPT
		JRST .+1]
	POP P,2
	POP P,1
	RET

;SKIP IF PTY OPEN
;GIVEN TTY LINE NUMBER IN 2

PTYSKO::PUSH P,IOS
	PUSH P,T1
	MOVE T1,T2
	CALL TTYPTY
	MOVE IOS,PTYSTS(T1)
	TQNE <POPEN>		;OPEN?
	JRST [	POP P,T1	;YES. TAKE SUCCESS RETURN
		POP P,IOS
		RETSKP]
	POP P,T1		;NO. TAKE FAILURE RETURN
	POP P,IOS
	RET
	SUBTTL RESCAN JSYS

	SWAPCD
.RSCAN::MCENT
	TLNE 1,-1		;BITS IN L.H.?
	JRST RSC0		;YES, ASSUME BYTE POINTER
	CAIL A,RFNLEN		;LEGAL FUNCTION?
	RETERR RSCNX2		;NO, "RSCAN FUNCTION CODE OUT OF RANGE"
	JRST @RFNTAB(1)		;IN RANGE, DO IT.

;DISPATCH TABLE FOR RSCAN FUNCTIONS

RFNTAB:	IFIW!REINIT		;0 MEANS MAKE BUFFER AVAILABLE FOR INPUT
	IFIW!RECNT		;1 MEANS COUNT CHARACTERS LEFT TO READ
RFNLEN==.-RFNTAB

RECNT:	MOVEI 1,0		;START WITH COUNT OF 0
	SKIPN 2,RSCNPT		;READING FROM RESCAN BUFFER?
	JRST RSC5		;NO, RETURN COUNT OF 0
	JRST RSC4		;YES, GO COUNT REMAINING CHARS

REINIT:	SKIPN 2,RSCNBP		;REINITIALIZE THE POINTER
	JRST RSC5		;NO BYTE POINTER, RETURN 0
	HRLI 2,(POINT 7,0,34)	;SET UP BYTE POINTER
	MOVEM 2,RSCNPT
	SKIPA			;SKIP ADDI FIRST TIME
RSC6:	ADDI 1,1		;COUNT A CHAR
RSC4:	ILDB 3,2		;GET NEXT CHAR
	JUMPN 3,RSC6		;END YET?
RSC5:	UMOVEM 1,1		;YES, RETURN COUNT TO USER
	SMRETN

RSC0:	NOSKED			;SETTING UP A NEW BUFFER
	NOINT			;THIS TOO
	SETZM RSCNPT		;ZERO PREVIOUS POINTER
	MOVE T2,RSCNBP		;GET PREVIOUS BUFFER
	SETZM RSCNBP		;NONE HERE NOW
	OKSKED
	JUMPE T2,RSC1		;IF NO BUFFER, GO ON
	MOVEI T1,JSBFRE		;YES, RELEASE ITS SPACE
	CALL RELFRE
RSC1:	OKINT
	UMOVE Q3,1		;GET STRING POINTER
	TLC Q3,-1		;LH = -1?
	TLCN Q3,-1
	HRLI Q3,(POINT 7,0)	;YES, MAKE IT A VALID STRING POINTER
	MOVEI T4,RSCMXC		;SET UP MAX COUNT
	MOVEI T2,^D11		;START WITH ENOUGH CHARACTERS FR HEADER
	MOVE T3,Q3		;GET STARTING POINTER
RSC2:	XCTBU [ILDB T1,T3]	;GET A CHARACTER
	SOJLE T4,[RETERR (RSCNX1)]
	SKIPE T1		;FOUND THE NULL YET?
	AOJA T2,RSC2		;NO, COUNT UP THIS CHARACTER
	MOVEI P3,-^D10(T2)	;GET COUNT OF CHARACTERS IN STRING
	IDIVI T2,5		;GET WORDS FOR BUFFER SPACE
	NOINT			;GUARD AGAINST DATA BASE LOSSAGE
	CALL ASGJFR		;GET SPACE FOR BUFFER
	 RETERR ()		;NOT ENOUGH ROOM
	HRRZM T1,RSCNBP		;SAVE THE POINTER TO THE BUFFER
	HRLI T1,(POINT 7,0,34)	;SET UP A POINTER TO IT
RSC3:	XCTBU [ILDB T2,Q3]	;GET NEXT CHARACTER
	IDPB T2,T1		;STORE IT IN THE BUFFER
	SKIPE T2		;FOUND THE NULL YET?
	SOJG P3,RSC3		;NO, LOOP BACK FOR REST
	MOVEI T2,0		;END WITH A NULL
	DPB T2,T1
	UMOVEM Q3,1		;STORE THE UPDATED STRING POINTER
	SMRETN			;DONE
;	TTY DEVICE FUNCTIONS

;DEVICE DEPENDENT ROUTINES. TO DO CERTAIN FUNCTIONS, THE RH OF DEV
;CONTAINS THE NAME OF A TABLE SUCH AS TTYDTB, AND A ROUTINE IS CALLED
;VIA 'CALL @FOO(DEV)', WHERE FOO IS A SYMBOL FOR AN OFFSET INTO THE 
;TABLE. THE SYMBOLS ARE DEFINED IN PROLOG. IN THE COMMENTS BELOW,
;THE OFFSET IS INDICATED ALONG WITH THE PURPOSE OF THE ROUTINE

TTYDTB::IFIW!TTYSET		;DLUKD - DIRECTORY SETUP
REPEAT OPEND-1,<DTBBAD (DESX9)>
	IFIW!TTYOPN		;OPEND - OPEN FILE
	IFIW!TTYIN		;BIND - BYTE INPUT
	IFIW!TTYOUT		;BOUTD - BYTE OUTPUT
	IFIW!TTYCLZ		;CLOSD - CLOSE FILE
	DTBBAD (DESX9)		;REND - RENAME
	DTBBAD (DESX9)		;DELD - DELETE FILE
	DTBBAD (DUMPX6)		;DMPID - DUMP MODE INPUT
	DTBBAD (DUMPX6)		;DMPOD - DUMP MODE OUTPUT
	DTBBAD (DESX9)		;MNTD - MOUNT DEVICE
	DTBBAD (DESX9)		;DSMD - DISMOUNT DEVICE
	DTBBAD (DESX9)		;INDD - INITIALIZE DIRECTORY OF DEVICE
	TTMTOP			;MTPD - DO MTAPE OPERATION
	DTBBAD (DESX9)		;GDSTD - GET DEVICE STATUS
	DTBBAD (DESX9)		;SDSTD - SET DEVICE STATUS
	DTBSKP			;RECOUT - FORCE RECORD OUT (SOUTR)
	IFIW!RFTADN		;RFTADD - READ FILE TIME AND DATE
	IFIW!SFTADN		;SFTADD - SET FILE TIME AND DATE
	DTBDSP (BIOINP)		;SET JFN FOR INPUT
	DTBDSP (BIOOUT)		;SET JFN FOR OUTPUT
	DTBBAD (GJFX49)		;CHECK ATTRIBUTE

	DTBLEN==:.-TTYDTB	;GLOBAL LENGTH OF DISPATCH TABLE
;DLUKD FUNCTION FOR TTY - DIRECTORY SETUP

TTYSET:	TQO <NVERF,NNAMF>
	RETBAD (GJFX32)

;OPEND FUNCTION FOR TTY - OPEN FILE

;ACCEPTS:
;	JFN/ JOB FILE NUMBER
;	DEV/ (UNIT NUMBER,,DISPATCH ADDRESS)

;	CALL TTYOPN

;RETURNS +1: FAILURE
;	 +2: SUCCESS

TTYOPN:	STKVAR <TTYOPX>
	CALL CHKAPD		;FIX APPEND BIT
TTYOP1:	CALL CKJFTT		;SEE IF JFN IS FOR 'TTY:'
	 RETSKP			;YES. THIS ALWAYS SUCCEEDS
	CALL LCKDVL		;LOCK THE DEVICE TABLES
	CALL FNDUNT		;GET INDEX INTO DEVICE TABLE FOR THIS UNIT
	MOVEM A,TTYOPX		;SAVE INDEX INTO DEVICE TABLES
	HRRZ B,A		;GET INTO CORRECT AC
	CALL DEVAV		;IS THIS DEVICE AVAILABLE TO US?
	 RETBAD (OPNX7,<UNLOCK DEVLCK
			OKINT>) ;NO. RETURN NOT AVAILABLE
	HLRZ B,DEV		; Get tty line number
	CALL TTYASO		;ASSIGN TERMINAL IF POSSIBLE
	 JRST TTYOP2		;ERROR. GO SEE WHAT TO DO
	HRRZ A,TTYOPX		;GET INDEX INTO DEVICE TABLES
	MOVE B,JOBNO		;NO. STORE JOB NUMBER IN DEVUNT
	HRLM B,DEVUNT(A)
	MOVX B,DV%OPN		;AND INDICATE OPEN IN DEVCHR
	IORM B,DEVCHR(A)
TTYOP3:	UNLOCK DEVLCK		;UNLOCK DEVICE TABLES
	OKINT
	TQO <SIZF>
	RETSKP

;FAILED. SEE IF GIVING UP OR WAITING

TTYOP2:
	UNLOCK DEVLCK		;UNLOCK DEVICE TABLES
	OKINT
	TXZN T1,1B0		;WAITING?
	RETBAD			;NO. FAIL
	HRL T1,T2		;GET LINE NUMBER
	MDISMS			;WAIT
	JRST TTYOP1		; AND GO TRY AGAIN
;BIND FUNCTION FOR TTY - BYTE INPUT

;ACCEPTS:
;	DEV/ (UNIT,,DISPATCH ADDRESS)
;	JFN/ JFN OR -1 OR 400000+LINE NUMBER

;	CALL TTYIN

;RETURNS +1: ALWAYS,
;		T1/ THE CHARACTER

;GETS ONE INPUT CHARACTER FOR THE LINE. IF NONE IS AVAILABLE, BLOCKS.

;THERE'S A BUG IN THE FOLLOWING CODE.  IF JFN CONTAINS -1, CODE
;KNOWS TO CHECK RSCAN BUFFER.  HOWEVER, NOTICE HOW WHEN JFN CONTAINS
;REAL JFN FOR "TTY", RSCAN BUFFER ISN'T CHECKED!

TTYIN:	CAIN JFN,-1		;CONTROLLING TTY?
	JRST [	MOVE A,JOBNO	;YES, SET UP LINE NUMBER
		HLL DEV,JOBPT(A)
		HLRZ B,JOBPT(A)	;GET LINE NUMBER
		CAIE B,-1	;NOW ATTACHED?
		CALL CKBJFN	;HAS A BKJFN BEEN DONE?
		 SKIPA		;NO.
		JRST .+1	;YES. GO USE THAT CHARACTER INSTEAD OF RSCAN
		SKIPN RSCNPT	;RESCAN PTR SET?
		JRST .+1	;NO
		ILDB A,RSCNPT	;YES, GET CHAR FROM RESCAN BUFFER
		JUMPN A,R	;USE IT UNLESS IT'S NULL
		NOSKED		;OWN SYSTEM
		SETZM RSCNPT	;FLUSH POINTER
		MOVE B,RSCNBP	;GET CURRENT RSCAN BUFFER POINTER
		SETZM RSCNBP	;NO POINTER NOW
		OKSKED
		JUMPE B,.+1	;IF NO BUFFER, ALL DONE
		MOVEI A,JSBFRE	;RELEASE THE OLD BUFFER
		CALL RELFRE	;RELEASE IT
		JRST .+1]
	CAIL JFN,400000		; Real jfn?
	 JRST TTYINX		;JFN HAS 400000 + LINE NUMBER
	HLRZ B,FILDDN(JFN)	;YES, GET POINTER TO DEVICE NAME
	MOVE B,1(B)		;GET STRING
	TRZ B,377		;FLUSH POSSIBLE GARBAGE
	CAMN B,[ASCIZ/TTY/]	;IS THIS THE CONTROLLING TTY?
	JRST [	MOVE A,JOBNO	;YES, GET THE LINE NUMBER
		HLL DEV,JOBPT(A)
		JRST .+1]	;USE CURRENT ATTACHED TERMINAL
	LDB B,[POINT 4,STS,35]	;GET MODE OF OPEN
	JUMPE B,[LDB B,PBYTSZ	;IF ZERO, USE NORMAL FOR THE TERMINAL
		CAIE B,8	;WAS OPENED WITH BYTE SIZE OF 8?
		JRST TTYINX	;NO. ALLOW TRANSLATION
		JRST TTYINB]	;YES. ASSUME BINARY
	CAIE B,10		;OPENED IN BINARY MODE
TTYINX:	SKIPA D,[TCI]		;CALL INPUT ROUTINE THAT ALLOWS TRANSLATION
TTYINB:	MOVEI D,TCIB		;CALL INPUT ROUTINE FOR BINARY INPUT
	HLRZ 2,DEV		;GET INTERNAL LINE NUMBER
	CAIN 2,-1		;LINE NUMBER AVAILABLE?
	CALL TTYATW		;NO, WAIT FOR ATTACH
	CALL LCKTTY		;GET ADDRESS OF DYNAMIC DATA, PREVENT DEALLOCATION
	JRST [	CALL ULKTTY	;NOT ACTIVE. ERROR
		TQO ERRF	;INDICATE FAILURE TO CALLER
		RET]
	CALL 0(D)
	 JRST TTYIN		;BLOCKED. GO TRY AGAIN (TTYSRV UNLOCKED DATA)
	CALL ULKTTY		;ALLOW DEALLOCATION
	CAIL JFN,400000		;JFN OR 400000+LINE NUMBER?
	 RET			;TERMINAL DESIGNATOR
	HLLZ B,FILBYT(JFN)	;JFN. GET BYTE SIZE FROM OPENF
	TLZ B,770000		;POINT TO END OF AC
	HRRI B,A		;FORM BYTE POINTER TO GET CHARACTER
	LDB A,B			;GET THE NUMBER OF BITS ASKED FOR
	RET			;RETURN CHARACTER OF CORRECT SIZE
;BOUTD FUNCTION FOR TTY - BOUT OUTPUT

;ACCEPTS:
;	T1/ CHARACTER
;	DEV/ (UNIT,,DISPATCH ADDRESS)

;	CALL TTYOUT

;RETURNS +1: ALWAYS

;OUTPUTS ONE CHARACTER TO THE SPECIFIED TERMINAL. 

TTYOUT:	CAIL JFN,400000		;JFN OR 400000 + LINE NUMBER?
	 JRST TTYOUX		;400000 + LINE NUMBER
	MOVE C,JOBNO		;SET UP TO CHECK FOR TTY:
	HLRZ B,FILDDN(JFN)	;GET POINTER TO DEVICE NAME
	MOVE B,1(B)		;GET THE DEVICE NAME
	TRZ B,377		;MASK OUT THE "DONT CARE" BITS
	CAMN B,[ASCIZ/TTY/]	;IS THIS THE CONTROLING TTY?
	HLL DEV,JOBPT(C)	;YES, GET THE REAL DEVICE NUMBER
	HLLZ B,FILBYT(JFN)	;GET BYTE SIZE FROM OPENF
	TLZ B,770000		;POINT TO END OF AC
	HRRI B,A		;CREATE BYTE POINTER TO CHARACTER IN AC
	LDB A,B			;GET AS MANY BITS AS OPENED FOR
	LDB B,[POINT 4,STS,35]	;GET MODE IN WHICH OPENED
	JUMPE B,[LDB B,PBYTSZ	;IF NORMAL MODE, GET BTYE SIZE OPENED FOR
		CAIE B,8	;OPENED FOR 8-BIT BYTES?
		JRST TTYOUX	;NO. ALLOW TRANSLATION
		JRST TTYOUB]	;YES. BINARY OUTPUT
	CAIE B,10		;NOT NORMAL MODE. BINARY?
TTYOUX:	SKIPA D,[TCO]		;CALL ROUTINE THAT ALLOWS TRANSLATION
TTYOUB:	MOVEI D,TCOB		;CALL ROUTINE FOR BINARY OUTPUT
	HLRZ B,DEV		;GET LINE NUMBER
	CAIN 2,-1		;LINE NUMBER AVAILABLE?
	CALL TTYATW		;NO, WAIT FOR ATTACH
	CALL LCKTTY		;GET ADDRESS OF DYNAMIC DATA, PREVENT DEALLOCATION
	JRST [	CALL ULKTTY	;NOT ACTIVE. ERROR
		TQO ERRF	;INDICATE ERROR TO CALLER
		RET]
	CALL 0(D)		;CALL OUTPUT ROUTINE IN TTYSRV
	CALL ULKTTY		;ALLOW DEALLOCATION
	RET

;TTYATW - WAIT UNTIL JOB HAS A CONTROLLING TERMINAL

;	CALL TTYATW

;RETURNS +1: ALWAYS
;		DEV/ (CONTROLLING TERMINAL,,UNCHANGED)
;		T2/ CONTROLLING TERMINAL

;PRESERVES T1

TTYATW:	PUSH P,A
	MOVE A,JOBNO
	MOVEI A,JOBPT(A)	;CONSTRUCT PTR TO JOBPT
	CALL DISGE		;DISMISS UNTIL IT'S POSITIVE
	HLL DEV,0(A)		;GET NEW CONTROLLING TTY NUMBER
	POP P,A
	HLRZ 2,DEV		;GET LINE NUMBER
	CAIN 2,-1		;BE SURE WE HAVE ONE
	JRST TTYATW		;STILL DETACHED
	RET
;CLOSD FUNCTION FOR TTY - CLOSE FILE

;ACCEPTS:
;	DEV/ (UNIT NUMBER,,DISPATCH ADDRESS)

;	CALL TTYCLZ

;RETURNS +1: FAILURE
;	 +2: SUCCESS

TTYCLZ:	STKVAR <TTYCLX>
TTYCL3:	CALL CKJFTT		;SEE IF JFN FOR 'TTY:'
	 RETSKP			;YES. THIS ALWAYS SUCCEEDS
	CALL LCKDVL		;LOCK THE DEVICE LOCK, GO NOINT
	CALL FNDUNT		;GET POINTER TO DEVICE TABLES
	MOVEM T1,TTYCLX		;SAVE IT
	HRRZ T2,T1		;T2/ POINTER TO DEVICE TABLES
	CALL DEVAV		;SEE IF DEVICE AVAILABLE TO THIS JOB
	 JRST TTYCL4		;NO. RETURN FAILURE
	HLRZ T2,DEV		;YES. T2/ LINE NUMBER
	CALL GTCJOB		;GET CONTROLLING JOB FOR THIS TERMINAL
	 JRST TTYCL4		;NONE. SHOULDN'T HAPPEN
	CAME T3,JOBNO		;IS IT THIS JOB?
	JRST TTYCL4		;NO. SHOULDN'T HAPPEN
	MOVE T1,TTYCLX		;GET INDEX TO DEVICE TABLES
	JN DV%ASN,DEVCHR(T1),TTYCL1 ;IF ALSO ASSIGNED, DON'T DEALLOCATE DATA
	HLRZ T2,DEV		;T2/ LINE NUMBER
	CALL TTYDEA		;DEALLOCATE DYNAMIC DATA FOR THIS LINE
	 JRST TTYCL2		;FAILED. GO WAIT IF NECESSARY
TTYCL0:	MOVE T1,TTYCLX		;POINT TO DEVICE TABLES
	HRROS DEVUNT(T1)	;INDICATE NO JOB OWNS THE DEVICE
TTYCL1:	SETZRO DV%OPN,DEVCHR(T1) ;INDICATE NO LONGER OPEN
	UNLOCK DEVLCK		;UNLOCK THE DEVICE LOCK
	OKINT			;LCKDVL DID A NOINT
	RETSKP			;SUCCESS

;TTYDEA FAILED. IF BIT 0 IS SET, AC 1 HAS ADDRESS OF SCHEDULER TEST

TTYCL2:	UNLOCK DEVLCK		;UNLOCK THE DEVICE LOCK
	OKINT			;LCKDVL DID A NOINT
	TXZN T1,1B0		;IS THIS A TEST ROUTINE?
	RETBAD			;NO. RETURN FAILURE
	HRL T1,T2		;T1/ (LINE NUMBER,,ROUTINE ADDRESS)
	TQO BLKF		;INDICATE BLOCK
	RETBAD

;DEVICE IS NOT AVAILABLE TO THIS JOB

TTYCL4:	UNLOCK DEVLCK		;UNLOCK THE DEVICE LOCK
	OKINT			;LCKDVL DID A NOINT
	RETBAD (DEVX2)
;MTPD FUNCTION FOR TTY - MTOPR

TTMTOP:	HLRZ 2,DEV		;GET LINE NUMBER
	XCTU [HRRZ 3,2]		;GET MTOPR OP CODE
	MOVSI T1,-TTMTTB	;FORM AOBJN POINTER
TTMTO1:	HLRZ T4,TTMTFC(T1)	;GET CODE
	CAIN T4,0(T3)		;IS THIS THE ONE?
	JRST [	HRRZ T3,TTMTFC(T1) ;GET DISPATCH ADDRESS
		JRST 0(T3)]	;GO DO IT
	AOBJN T1,TTMTO1		;DO ALL OF THEM
	RETBAD (MTOX1)		;ERROR

;MTOPR DISPATCH TABLE

TTMTFC:	.MOPIH,,TTMHNG		;TEST INPUT HUNGRY
	.MOSPD,,TTMSSP		;SET LINE SPEED
	.MORSP,,TTMRSP		;READ LINE SPEED
	.MOSLW,,TTMSWD		;SET LINE WIDTH
	.MORLW,,TTMRWD		;READ LINE WIDTH
	.MOSLL,,TTMSLN		;SET LINE LENGTH
	.MORLL,,TTMRLN		;READ LINE LENGTH
	.MOSNT,,TTMSNT		;SET TTY MSG STATUS
	.MORNT,,TTMRNT		;READ TTY MSG STATUS
	.MOSIG,,TTMSIG		;SET "IGNORE INPUT" BIT

TTMTTB==.-TTMTFC		;LENGTH OF TABLE

;FUNCTION .MOPIH OF MTOPR - SEE IF LINE IS HUNGRY

;RETURNS +2: ALWAYS
;		T1/ 0 IF NOT HUNGY
;		   -1 IF HUNGRY

TTMHNG:	CALL LCKTTY		;GET ADDRESS OF DYNAMIC DATA, PREVENT ALLOCATION
	 JRST [	CALL ULKTTY	;NOT ACTIVE. ALLOW DEALLOCATION
		SETZ T1,	;ASSUME NOT HUNGRY
		SKIPN T2	;UNLESS LINE IS NOT ACTIVE AT ALL
		SETO T1,	;THEN MAKE IT HUNGRY
		JRST TTMHN1]
	CALL TTSTIH		;SEE IF HUNGRY
	CALL ULKTTY		;ALLOW DEALLOCATION
TTMHN1:	UMOVEM T1,2		;STORE RESULT
	RETSKP			;AND DONE

;FUNCTION .MOSPD OF MTOPR - SET TERMINAL SPEED

;ACCEPTS:
;	T2/ LINE NUMBER
;	T3/ (INPUT SPEED,,OUTPUT SPEED)

TTMSSP:	CALL TTMLOK		;CHECK FOR ACCESS TO THIS TTY
	 RETBAD (MTOX7)		;CAN'T DO IT
	CALL TTSSPD		;GO SET SPEED
	RETSKP			;AND DONE

;FUNCTION .MORSP OF MTOPR - GET LINE SPEED

TTMRSP:	CALL TTRSPD		;GO READ SPEED
	RETSKP			;DONE


;TTSIG - SET "IGNORE INPUT" BIT FOR A LINE
;MUST BE WHEEL OR OPERATOR TO DO THIS

TTMSIG:	MOVX T1,SC%OPR!SC%WHL	;MUST BE WHEEL OR OPERATOR TO DO THIS
	TDNN T1,CAPENB		;IS IT?
	RETBAD (MTOX7)		;CAN'T DO IT
	UMOVE T1,T3		;GET NEW SETTING
	CALL TTSSIG		;GO SET BIT IN LINE'S STATIC DATABASE
	RETSKP			;DONE, RETURN SUCCESS

;LOCAL ROUTINE TO VERIFY ACCESS TO TTY
;CHECKS IF TTY IS "ACTIVE" OR IF CALLER IS WHEEL OR OPERATOR.
;IF TTY IS ACTIVE, THEN THIS PROCESS MUST HAVE IT ASSIGNED OR
;OPENED.

TTMLOK:	SAVET			;SAVE ALL REGS
	MOVX T1,SC%WHL!SC%OPR	;SEE IF WHEEL OR OPERATOR
	TDNE T1,CAPENB		;IS IT?
	RETSKP			;YES. ALLOW ACCESS THEN
	CALL LCKTTY		;NO. SEE IF ACTIVE
	 CALLRET ULKTTY		;NO. UNLOCK IT AND GIVE ERROR RETURN
	CALL ULKTTY		;YES. UNLOCK IT
	RETSKP			;AND ALLOW ACCESS
;FUNCTION .MOSLW OF MTOPR - SET LINE WIDTH

TTMSWD:	UMOVE T1,3		;GET WIDTH TO SET
	CALL LCKTTY		;GET ADDRESS OF DYNAMIC DATA, PREVENT DEALLOCATION
	 JRST [	CALL ULKTTY	;NOT ACTIVE. ALLOW DEALLOCATION
		RETBAD (TTYX01)] ;INDICATE NOT ACTIVE
	CALL TTSWID		;GO SET IT
	JRST [	CALL ULKTTY	;ALLOW DEALLOCATION
		RETBAD (MTOX19)] ;RETURN INVALID WIDTH
	CALL ULKTTY		;ALLOW DEALLOCATION
	RETSKP			;AND DONE

;FUNCTION .MORLW - READ LINE WIDTH

TTMRWD:	CALL LCKTTY		;GET ADDRESS OF DYNAMIC DATA, PREVENT DEALLOCATION
	 JRST [	CALL ULKTTY	;NOT ACTIVE. ALLOW DEALLOCATION
		RETBAD (TTYX01)] ;INDICATE NOT ACTIVE
	CALL TTRWID		;GO READ WIDTH
	JRST TTMTST		;GO RETURN TO USER

;FUNCTION .MOSLL OF MTOPR - SET LINE LENGTH

TTMSLN:	UMOVE T1,3		;GET LENGTH
	CALL LCKTTY		;GET ADDRESS OF DYNAMIC DATA, PREVENT DEALLOCATION
	 JRST [	CALL ULKTTY	;NOT ACTIVE. ALLOW DEALLOCATION
		RETBAD (TTYX01)] ;RETURN NOT ACTIVE
	CALL TTSLEN		;GO SET IT
	 JRST [	CALL ULKTTY	;ALLOW DEALLOCATION
		RETBAD (MTOX20)] ;RETURN INVALID LENGTH
	CALL ULKTTY		;SUCCESS. ALLOW DEALLOCATION
	RETSKP			;AND DONE

;FUNCTION .MORLL OF MTOPR - READ LINE LENGTH

TTMRLN:	CALL LCKTTY		;GET ADDRESS OF DYNAMIC DATA, PREVENT DEALLOCATION
	 JRST [	CALL ULKTTY	;NOT ACTIVE. ALLOW DEALLOCATION
		RETBAD (TTYX01)] ;RETURN NOT ACTIVE
	CALL TTRLEN		;GO GET IT
TTMTST:	UMOVEM T1,3		;STORE RESULT
	CALL ULKTTY		;ALLOW DEALLOCATION
	RETSKP			;AND DONE

;FUNCTION .MOSNT OF MTOPR

TTMSNT:	CALL TTMLOK		;VERIFY ACCESS TO TTY
	 RETBAD (MTOX7)		;CAN'T DO IT
	UMOVE C,C		;GET USER'S ARG
	CALL TTSNTS		;GO SET TTY NO MSG
	RETSKP			;DONE

;FUNCTION .MORNT OF MTOPR

TTMRNT: CALL TTRNTS		;GO READ TTY MSG STATUS
	UMOVEM C,C		;AND GIVE TO USER
	RETSKP			;DONE
;ROUTINE CALLED BY NILOPN,PTYOPN ,AND TTYOPN TO SET WRTF
;IF APPEND REQUESTED

CHKAPD:	TQZE <RNDF>		;WANT APPEND?
	TQO <WRTF>		;YES. BUT ALWAYS FAKE APPEND
	RET			;AND DONE
;TTYASN - ASSIGN TERMINAL AS CONTROLLING TERMINAL FOR A JOB

;ACCEPTS:
;	T1/ JOB NUMBER
;	T2/ INTERNAL LINE NUMBER

;	CALL TTYASN

;RETURNS +1: FAILURE
;	 +2: SUCCESS

TTYASN::
	SE1CAL
	STKVAR <TTYALN,TTYASX>
	MOVEM T2,TTYALN		;SAVE LINE NUMBER
	CALL LCKDVL		;LOCK THE DEVICE TABLES
	MOVEI T1,.TTDES(T2)	;T1/ DEVICE DESIGNATOR
	CALL CHKDES		;GET INDEX INTO DEVICE TABLES
	 RETBAD(,<UNLOCK DEVLCK ;INVALID DESIGNATOR. SHOULDN'T HAPPEN
		OKINT>)
	MOVEM B,TTYASX		;SAVE INDEX INTO DEVICE TABLES
	MOVE B,TTYALN		;2/LINE NUMBER
	CALL TTYASC		;GO ASSIGN TERMINAL IF POSSIBLE
	 RETBAD(,<UNLOCK DEVLCK ;FAILED
		OKINT>)
	HRRZ A,TTYASX		;GET INDEX INTO DEVICE TABLES
	MOVE B,JOBNO		;GET THIS JOB NUMBER
	HRLM B,DEVUNT(A)	;MARK THIS IT OWNS THE TERMINAL
	MOVX B,DV%ASN		;INDICATE ASSIGNED
	IORM B,DEVCHR(A)
	UNLOCK DEVLCK
	OKINT
	RETSKP
;TTYDAS - DEASSIGN TERMINAL

;ACCEPTS:
;	T2/ INTERNAL LINE NUMBER

;	CALL TTYDAS

;RETURNS +1: FAILED,
;		T1/ ERROR CODE
;			OR
;		    1B0 + ADDRESS OF SCHEDULER TEST ROUTINE FOR MDISMS
;	 +2: SUCCEEDED

;CALLER MUST HAVE LOCKED THE DEVICE LOCK

TTYDAS::STKVAR <TTYDLN,TTYDAX>
	MOVEM T2,TTYDLN		;SAVE LINE NUMBER
TTYDA3:	MOVEI T1,.TTDES(T2)	;T1/ DEVICE DESIGNATOR FOR TERMINAL
	CALL CHKDES		;GET INDEX TO DEVICE TABLES
	 JRST TTYDA2		;INVALID DESIGNATOR. SHOULDN'T HAPPEN
	MOVEM T2,TTYDAX		;SAVE INDEX TO DEVICE TABLES
	JN DV%OPN,DEVCHR(T2),TTYDA1 ;DON'T DEALLOCATE IF ALSO OPEN
	HLRZ T2,DEV		;T2/ LINE NUMBER
	CALL TTYDEA		;DEALLOCATE DYNAMIC DATA
	 JRST TTYDA2		;FAILED. GO WAIT IF NECESSARY
TTYDA5:	MOVE T2,TTYDAX		;GET INDEX TO DEVICE TABLES
	HRROS DEVUNT(T2)	;INDICATE NO JOB OWNS THIS DEVICE
TTYDA1:	SETZRO DV%ASN,DEVCHR(T2) ;INDICATE NO LONGER ASSIGNED
	RETSKP

;FAILED TO DEASSIGN DYNAMIC DATA. RETURN FAILURE TO CALLER WITHOUT
;CHANGING DEVICE TABLES

TTYDA2:	RETBAD
	TNXEND
	END