	.TITLE	EDI05
	.PSECT EDI05							;JUL11EL
	.IDENT	/X01/							;JUL11EL
									;JUL11EL
	.NLIST	BEX							;JUL11EL
	.ENABL	LC							;JUL11EL
									;JUL11EL
;									;**-5
; EDITOR (TEMP FILE VERSION)
;
; VERSION X01
;
; DAVID G. CONROY	10-APR-78
;
;
; as modified by Eric Levy
;
	.globl atty
	.globl dtty
	.GLOBL	EDIT							;JUL11EL
	.GLOBL	GETC							;JUL11EL
	.GLOBL	GETC1							;JUL11EL
	.GLOBL	TTYC							;**-3
	.GLOBL	TTYNL
	.GLOBL	TTYNUM
	.GLOBL	TTYSKP
	.GLOBL	TTYSTR
	.GLOBL	PUTBAK							;JUL11EL
									;JUL11EL
	.MCALL	CALL							;**-1
	.MCALL	CALLR
	.MCALL	RETURN
	.MCALL	OPNT$D
	.MCALL	EXIT$S
	.MCALL	GPRT$S
	.MCALL	WTSE$S
	.MCALL	DIR$
	.MCALL	QIOW$,qio$
	.MCALL	FINIT$
									;JUL11EL
;									;JUL11EL
; LOCAL DATA								;JUL11EL
;									;JUL11EL
	TSIZE=132.		;TTY INPUT/OUTPUT SIZE
	PBSIZE=120.		;SIZE OF THE PUTBACK BUFFER		;JUL11EL
PUTBUF:	.BLKB PBSIZE+2		;BUFFER (+2 IS FOR SAFETY)		;JUL11EL
PUTB:	.WORD -1		;PUTBACK POINTER, -1=EMPTY		;JUL11EL
BACK1:	.ASCII /-1P/<12>	;-1P AND NEWLINE			;JUL11EL
lback1=.-back1
	.EVEN								;JUL11EL
									;JUL11EL
LIM:	.LIMIT			;TO FIND OUT HOW BIG I AM		;JUL11EL
IOS:	.BLKW	2		;IO STATUS				;JUL11EL
	.BLKW	1		;ALSO FOR GPRT$S			;JUL11EL
									;JUL11EL
CT:	.WORD	-10000.		;TTYNUM CONVERSION TABLE		;**-9
	.WORD	-1000.		;
	.WORD	-100.		;
	.WORD	-10.		;
CTE:	.BLKW	0		;END
									;JUL11EL
;									;JUL11EL
; DIRECTIVE BLOCKS							;JUL11EL
;									;JUL11EL
									;JUL11EL
R:	QIOW$	IO.RVB,1,1,,IOS,,<IB,TSIZE-1.>				;**-5
O:	QIOW$	IO.WVB,1,1,,IOS,,<OB,0,TSIZE>
atty:
A:	QIOW$	IO.ATT,1,1
P:	QIOW$	IO.WVB,1,1,,IOS,,<PR,2,44>
dtty:
DET:	QIOW$	IO.DET,1,1
									;JUL11EL
;									;**-1
; TTY BUFFER CONTROLS
; THE MAGIC NUMBERS IN THE .BYTE JUST BEFORE THE INPUT BUFFER MAKE A
; GMCR$ DPB INTO THE INPUT BUFFER.
;
									;JUL11EL
IC:	.WORD	0		;NUMBER OF CHARACTERS IN TTY BUFFER	;**-1
IP:	.BLKW	1		;INPUT POINTER
OP:	.WORD	OB		;OUTPUT POINTER
GMCR:	.BYTE	127.,41.	;GMCR DIC, DPB SIZE
IB:	.BLKB	TSIZE		;TTY INPUT BUFFER
OB:	.BLKB	TSIZE		;TTY OUTPUT BUFFER

;
; MESSAGES
;

NOTEMP:	.ASCIZ	"Cannot create tempfile"<12>
PR:	.ASCII <15> 	  "*"
	.EVEN

;+
; ** EDIT - STARTUP
;
; THIS ROUTINE IS THE STARTUP CODE FOR THE EDITOR. IT GETS CONTROL
; FROM RSX.
; IT SIZES UP THE PARTITION (TO DETERMINE THE NUMBER OF LINES  THE
; USER CAN HAVE IN THE BUFFER) AND OPENS THE TEMP. FILE.
; WHEN DONE, CONTROL GOES OFF TO THE COMMAND LOOP, NEVER TO   EVER
; RETURN.
;-
									;JUL11EL
EDIT:	DIR$	#A		;ATTACH THE TERMINAL			;**-1
	WTSE$S	#1		;

	MOV	LIM+2,R0	;COMPUTE BASE OF BUFFER
	INC	R0		;ROUND UP
	BIC	#1,R0		;
	MOV	R0,ZERO		;SET NO TEXT IN BUFFER
	MOV	R0,DOT		;
	MOV	R0,DOL		;
									;JUL11EL
	GPRT$S	,#IOS		;GET SIZE OF PARTITION			;**-1
	MOV	IOS+G.PRPS,R0	;IN CLICKS
	ASL	R0		;IN BYTES
	ASL	R0		;
	ASL	R0		;
	ASL	R0		;
	ASL	R0		;
	ASL	R0		;
	ADD	$DSW,R0		;COMPUTE HIGH ADDRESS
	MOV	R0,TOP		;
									;JUL11EL
	FINIT$			;INITIALISE THE FSR.			;**-1
	OPNT$D	#TMP		;GET SCRATCH FILE.
	BCS	80$		;URK

;
; COMMAND LINE PROCESSING
;

	DIR$	#GMCR		;READ DOWN COMMAND LINE
	BCS	40$		;NOT THERE
	CMPB IB,#"EE		;INVOKED FROM ...EEE?			;JUL11EL
	BNE 5$								;JUL11EL
	DEC OE			;SET FLAG				;JUL11EL
	DEC ONUM		;SET FLAG FOR NUMBERING			;JUL11EL
	MOV S100,-(SP)							;JUL11EL
	MOV C100,S100		;SWAP C AND S COMMANDS			;JUL11EL
	MOV (SP)+,C100							;JUL11EL
5$:									;JUL11EL
	MOV	#GMCR+G.MCRB,R0	;POINT AT THE COMMAND BUFFER		;**-1
	CALL	50$		;GET FIELD
	CMP	R1,#'-		;EDIT -
	BNE	10$		;NO
	INC	OV		;YES, SET VERBOSE FLAG
	CALL	50$		;AND SCAN OVER THE -

10$:	CMP	R1,#CR		;A FILE PERHAPS
	BEQ	40$		;NO
	CMP	R1,#ESC		;
	BEQ	40$		;NO

	DEC	R0		;FABRICATE AN E COMMAND
	MOVB	#' ,-(R0)	;
	MOVB	#'E,-(R0)	;
	MOV	R0,IP		;SET UP A FAKE COMMAND LINE

20$:	CMPB	(R0),#CR	;DETERMINE THE SIZE
	BEQ	30$		;
	CMPB	(R0),#ESC	;
	BEQ	30$		;
	INC	R0		;
	BR	20$		;

30$:	MOVB	#NL,(R0)+	;MAKE DELIMITER A NEWLINE
	SUB	IP,R0		;GET THE SIZE
	MOV	R0,IC		;SAVE IT FOR TTYICH

40$:	MOV	SP,SPS		;SETEXIT
	JMP	COMMND		;OFF WE GO, INTO THE ...

;
; LOCAL ROUTINE TO SKIP OVER A FIELD.
;

50$:	MOVB	(R0)+,R1	;GET CHARACTER

	CMP	R1,#CR		;STOP ON COMMAND END
	BEQ	70$		;
	CMP	R1,#ESC		;
	BEQ	70$		;
	CMP	R1,#' 		;BUT CONTINUE UNTIL A BLANK
	BNE	50$		;

60$:	MOVB	(R0)+,R1	;SKIP OVER BLANKS
	CMP	R1,#' 		;
	BEQ	60$		;

70$:	RETURN			;DONE

;
; DISASTER, NO TEMP FILE
;

80$:	MOV	#NOTEMP,R0	;NO TEMP FILE.
	CALL	TTYSTR		;COMPLAIN, THEN
	EXIT$S			;DIE
;+									;JUL11EL
; ** PUTBAK - PUT BACK ONTO THE INPUT					;JUL11EL
;									;JUL11EL
; INPUT:								;JUL11EL
;									;JUL11EL
;  R0 = STRING ADDR (OF BEGINNING OF STRING)				;JUL11EL
;  R1 = STRING LEN							;JUL11EL
;									;JUL11EL
; THE ROUTINE WILL PUSH BACK ONTO THE INPUT THE SPECIFIED		;JUL11EL
; STRING. NOTE THAT THE PUSH BACK PUTS BACK IN REVERSE ORDER		;JUL11EL
; WHICH IS NECESSARY TO THE GETC ROUTINE.				;JUL11EL
;									;JUL11EL
;-									;JUL11EL
 									;JUL11EL
PUTBAK:: MOV R2,-(SP)		;SAVE THESE				;JUL11EL
	MOV R3,-(SP)							;JUL11EL
	MOV PUTB,R3		;GET POINTER				;JUL11EL
	BGT 10$			;ANYTHING THERE NOW			;JUL11EL
	CLR R3			;NO, INIT IT TO FIRST			;JUL11EL
	BR 15$								;JUL11EL
10$:									;JUL11EL
	INC R3								;JUL11EL
15$:	MOV R0,R2							;JUL11EL
	ADD R1,R2		;POINT AT LINE END			;JUL11EL
	TST R1			;CHECK FOR NULL PUTBACK			;JUL11EL
	BLE 30$			;AND NOP IF NULL			;JUL11EL
20$:									;JUL11EL
	MOVB -(R2),PUTBUF(R3)	;START PUTTING INTO BUFFER		;JUL11EL
	INC R3								;JUL11EL
	CMP R3,#PBSIZE		;TOO MUCH?				;JUL11EL
	BGT 50$			;YES IF GT				;JUL11EL
	SOB R1,20$		;LOOP ON STRING COUNT			;JUL11EL
	DEC R3								;JUL11EL
	MOV R3,PUTB		;SET UP POINTER'S NEW VALUE		;JUL11EL
30$:									;JUL11EL
	MOV (SP)+,R2							;JUL11EL
	MOV (SP)+,R3							;JUL11EL
	RTS PC								;JUL11EL
50$:									;JUL11EL
	CLR R1								;JUL11EL
	JMP ERROR							;JUL11EL
;+									;JUL11EL
; ** GETC - GET A CHARACTER						;JUL11EL
;									;JUL11EL
; GET A CHARACTER, EITHER FROM THE TTY , THE SAVED UP GLOBAL		;JUL11EL
; COMMAND LINE  , OR FROM THE PUTBAK BUFFER.				;JUL11EL
; RETURN EOF ON THE END OF THE GLOBAL LINE, BUT KEEP			;JUL11EL
; RETURNING IT.								;JUL11EL
;									;JUL11EL
; ** GETC1 - GET A CHAR (ALTERNATE ENTRY TO NOT CHECK PUTBAK BUFFER).	;JUL11EL
;									;JUL11EL
;									;**-7
; OUTPUTS:
;	R0=CHARACTER
;-
									;JUL11EL
GETC:	MOV	PEEK,R0		;PEEK CHARACTER				;**-1
	BMI	10$		;NONE
	MOV	#-1,PEEK	;MAKE IT GO AWAY
	BR	40$		;
									;JUL11EL
GETC1	=.								;JUL11EL
	MOV	PEEK,R0		;PEEK CHARACTER				;JUL11EL
	BMI	15$		;NONE					;JUL11EL
	MOV	#-1,PEEK	;MAKE IT GO AWAY			;JUL11EL
	BR	40$		;					;JUL11EL
									;JUL11EL
10$:									;JUL11EL
	MOV PUTB,R0		;IS THERE ANYTHING PUT BACK?		;JUL11EL
	BLT 15$			;NOT IF LT				;JUL11EL
	MOVB PUTBUF(R0),R0	;ELSE GET A CHAR			;JUL11EL
	DEC PUTB		;DECREMENT POSITION			;JUL11EL
	BR 40$			;RETURN THE CHARACTER			;JUL11EL
15$:	MOV	GUPTR,R0	;GET GLOBAL POINTER			;JUL11EL
	BEQ	30$		;NOT IN A GLOBAL			;**-2
	MOVB	(R0),R0		;GET CHARACTER
	BEQ	20$		;END, RETURN EOF
	INC	GUPTR		;BUMP POINTER
	BR	40$		;
20$:	MOV	#EOF,R0		;EOF ON GLOBAL
	BR	40$		;DONE
									;JUL11EL
30$:	CALL	TTYICH		;GET CHARACTER FROM TTY			;JUL11EL
40$:	BIC	#177600,R0	;MASK TO 7 BIT ASCII			;JUL11EL
	RETURN			;DONE					;JUL11EL
									;JUL11EL
;+									;**-5
; ** TTYICH - GET CHARACTER FROM THE TTY
;
; OUTPUTS:
;	R0=CHARACTER
;-
									;JUL11EL
TTYICH:	DEC	IC		;ANY CHARACTERS LEFT			;JUL11EL
	BPL	20$		;YES					;JUL11EL
									;JUL11EL
10$:	TST	OV		;VERBOSE				;**-4
	BEQ	15$		;NO, NO PROMPTS
	TST	EXECM		;EXECUTING A COMMAND
	BNE	15$		;YES, NO PROMPTS
	DIR$ #DET		;DETACH THE TTY (END OF CONTRL O)
	WTSE$S #1		;MIGHT NEED THIS
	DIR$	#P		;ISSUE PROMPT
	WTSE$S	#1		;
	DIR$ #A			;ATTACH THE TTY (START OF CONTRL O)
	WTSE$S #1		;MIGHT NEED THIS

15$:	DIR$	#R		;READ A LINE FROM THE TTY
	WTSE$S	#1		;
	CMPB #IE.EOF,IOS	;CONTROL Z?
	BEQ 30$			;EQ->YES
;	CMP	IOS,#IS.CR	;GOOD STATUS				;JUL11EL
;	BNE	10$		;NO, GO AGAIN				;JUL11EL
	CMPB IOS+1,#33		;IS IT AN ESCAPE			;JUL11EL
	BNE 16$			;IF NE THEN CONTINUE			;JUL11EL
	CLR IC			;ELSE WE SIMULATE			;JUL11EL
	MOV R1,-(SP)		;					;JUL11EL
	MOV #BACK1,R0		; A .-1P				;JUL11EL
	MOV #lback1,R1		;  COMMAND				;JUL11EL
	CALL PUTBAK		;   AND PUT IT BACK ONTO INPUT		;JUL11EL
	MOVB #'.,R0		;THIS IS THE FIRST CHAR			;JUL11EL
	MOV (SP)+,R1		;(RESTORE THIS)				;JUL11EL
	RETURN			;NOW GET OUT WITH THE FIRST CHAR	;JUL11EL
16$:	MOV	IOS+2,R0	;GET BYTE COUNT				;**-2
	MOVB	#NL,IB(R0)	;APPEND A NEWLINE TO THE BUFFER
	MOV	R0,IC		;RESET COUNT
	MOV	#IB,IP		;RESET POINTER
									;JUL11EL
20$:	MOVB	@IP,R0		;GET THE CHARACTER			;**-1
	INC	IP		;
	BIC	#177600,R0	;7 BIT ASCII
	BEQ	TTYICH		;EAT NULLS
	RETURN			;DONE
30$:	CALL TTYSKP		;CLEAN OUT ANYTHING IN GET BUFFERS	;JUL11EL
	MOV #IS.CR,IOS		;WE WILL				;JUL11EL
	MOV #5,IOS+2		; SIMULATE A: "X <nl> f  <NL> Q"		;**-1
	MOVB #'X,IB		;  COMMAND triple.
	MOVB #NL,IB+1		;   SO SET THE IOS TO OK, LENGTH TO 5
	movb #'F,ib+2		;
	movb #NL,ib+3		;
	MOVB #'Q,IB+4		;    AND x,f,q INTO THE TTY BUFFER.
	BR 16$			;     THEN CONTINUE AS THOUGH NOTHING HAPPENDED.
									;JUL11EL
;+									;**-1
; ** TTYSKP - DISCARD REMAINER OF TTY LINE
;
; THIS ROUTINE IS USED BY THE ERROR PROCESSOR TO DISCARD THE REST OF
; THE CURRENT LINE FROM THE TTY.
;-
									;JUL11EL
TTYSKP:	CLR	IC		;EASY					;JUL11EL
	MOV #-1,PUTB		;EASY					;JUL11EL
	MOV	#-1,PEEK	;TO DO					;JUL11EL
	RETURN			;					;JUL11EL
									;JUL11EL
;+									;**-5
; ** TTYSTR - OUTPUT A STRING TO THE TTY
;
; INPUTS:
;	R0=POINTER TO ASCIZ STRING
;
; USES:
;	R0
;-
									;JUL11EL
TTYSTR:	MOV	R1,-(SP)	;SAVE REGISTERS				;JUL11EL
	MOV	R0,R1		;GET WORKING COPY OF STRING		;JUL11EL
									;JUL11EL
10$:	MOVB	(R1)+,R0	;FROM STRING				;**-4
	BEQ	20$		;DONE
	CALL	TTYC		;OUT IT GOES
	BR	10$		;
									;JUL11EL
20$:	MOV	(SP)+,R1	;RETURN					;JUL11EL
	RETURN			;					;JUL11EL
									;JUL11EL
;+									;JUL11EL
; ** TTYNL - OUTPUT A NEWLINE TO THE TTY				;JUL11EL
;-									;JUL11EL
									;JUL11EL
TTYNL:	MOV	R0,-(SP)	;SAVE R0				;**-8
	MOV	#NL,R0		;PUT OUT THE NEWLINE
	CALL	TTYC		;
	MOV	(SP)+,R0	;RESTORE R0
	RETURN			;
									;JUL11EL
;+									;**-1
; ** TTYNUM - OUTPUT A NUMBER TO THE TTY
;
; INPUTS:
;	R1=THE NUMBER
;
; USES:
;	R0, R1
;-
									;JUL11EL
TTYNUM:	MOV	R2,-(SP)	;SAVE REGISTERS				;JUL11EL
	MOV	R3,-(SP)	;					;JUL11EL
									;JUL11EL
	TST	R1		;TEST FOR NEGATIVE NUMBERS		;**-4
	BPL	10$		;NO
	MOV	#'-,R0		;PUT OUT "-" SIGN
	CALL	TTYC		;
	BR	20$		;
10$:	NEG	R1		;FORCE POSITIVE TO NEGATIVE
									;JUL11EL
20$:	MOV	#CT,R2		;GET TABLE POINTER			;JUL11EL
	CLR	R3		;CLEAR SUPRESS FLAG			;JUL11EL
									;JUL11EL
30$:	MOV	#'0,R0		;START WITH "0"				;**-4
40$:	CMP	R1,(R2)		;GET A DIGIT
	BGT	50$		;
	SUB	(R2),R1		;
	INC	R0		;
	BR	40$		;
									;JUL11EL
50$:	CMP	R0,#'0		;IS IT A ZERO				;**-1
	BNE	60$		;NO
	TST	R3		;ARE WE SUPRESSING
	BEQ	70$		;YES
60$:	CALL	TTYC		;PUT IT OUT
	INC	R3		;END SUPRESSION
									;JUL11EL
70$:	TST	(R2)+		;GET NEXT ENTRY				;JUL11EL
	CMP	R2,#CTE		;AT THE END				;JUL11EL
	BLO	30$		;NO					;JUL11EL
									;JUL11EL
	MOV	R1,R0		;DO THE LAST DIGIT			;**-5
	NEG	R0		;
	ADD	#'0,R0		;
	CALL	TTYC		;
									;JUL11EL
	MOV	(SP)+,R3	;RETURN					;JUL11EL
	MOV	(SP)+,R2	;					;JUL11EL
	RETURN			;					;JUL11EL
									;JUL11EL
;+									;**-5
; ** TTYC - TYPE CHARACTER
;
; THE SUPPLIED CHARACTER IS TYPED ON THE CONTROL TTY. THE CHARACTERS
; ARE BUFFERED UNTIL NEWLINE, WHEN THE WHOLE MESS IS PUT OUT.		;JUL11EL
; FOR SOME UNKNOWN REASON DEC DOES NOT WRITE OUT THE SLEW BYTES ON A	;**-1
; ZERO BYTE WRITE. SPECIAL CHECKING MUST BE DONE IN THIS CASE.
;
; INPUTS:
;	R0=CHARACTER
;-
									;JUL11EL
TTYC:	MOV	R0,-(SP)	;SAVE THE CHARACTER			;**-1
	CMP	R0,#NL		;TIME TO FLUSH THE BUFFER
	BNE	20$		;NO
	MOV	OP,R0		;COMPUTE COUNT
	SUB	#OB,R0		;
	BNE	10$		;NOT A NULL BUFFER
	MOVB	#' ,OB		;PUT OUT 1 BLANK
	INC	R0		;
10$:	MOV	R0,O+Q.IOPL+2	;SET INTO THE DPB
	DIR$	#O		;OUTPUT THE LINE
	WTSE$S	#1		;
	MOV	#OB,OP		;WIND BACK THE POINTER
	BR	30$		;
									;JUL11EL
20$:
	cmp r0,#40		;control char?
	bhis 25$			;no continue
	cmp r0,#7
	beq 25$		;its an ok control char
	cmp r0,#11
	beq 25$		;its an ok control char
	cmp r0,#13
	beq 25$		;its an ok control char
	cmp r0,#14
	beq 25$		;its an ok control char
	cmp r0,#15
	beq 25$		;its an ok control char
	mov #'?,r0		;replace it then
25$:	CMP	OP,#OB+TSIZE 	;AT THE END OF THE BUFFER		;**-1
	BHIS	30$		;YES, DISCARD
	MOVB	R0,@OP		;SAVE THE CHARACTER
	INC	OP		;
									;JUL11EL
30$:	MOV	(SP)+,R0	;RETURN					;JUL11EL
	RETURN			;					;JUL11EL
									;JUL11EL
	.END	EDIT							;**-4
