; delete all the audit shit	g/;jul..el$/c///
; and the ;**-n stuff too 	g/;\*\*-[0123456789].*$/c///
; kill all trailing blanks and tabs	g/[ 	][ 	]*$/c///
	.TITLE	EDI05
	.PSECT EDI05
	.IDENT	/X01/

	.NLIST	BEX
	.ENABL	LC
mutask=1
;
; EDITOR (TEMP FILE VERSION)
;
; VERSION X01
;
; DAVID G. CONROY	10-APR-78
;
;
; as modified by Eric Levy
; feb-2-80 fix lurking bug in putbak routine (wasn't saving regs correctly)
;
			.if df mutask
				.macro pure
                                .psect pur,ro
				.endm

				.macro impure
                                .psect ipur,rw
				.endm
			
			.iff
			
				.macro pure
				.endm
				.macro impure
				.endm
			
			.endc
	.globl newtop
	.globl ttflsh
	.globl ttfls2
	.globl atty
	.globl dtty
	.GLOBL	EDIT
	.GLOBL	GETC
	.GLOBL	GETC1
	.GLOBL	TTYC
	.GLOBL	TTYCC
	.GLOBL	TTYNL
	.GLOBL	TTYNUM
	.GLOBL	TTYSKP
	.GLOBL	TTYSTR
	.GLOBL	PUTBAK

	.MCALL	CALL
	.MCALL	CALLR
	.MCALL	RETURN
	.MCALL	OPNT$D
        .MCALL  open$w
	.MCALL	EXIT$S
	.MCALL	GPRT$S
	.MCALL	WTSE$S
	.MCALL	DIR$
	.MCALL	QIOW$,qio$
	.MCALL	FINIT$
	.mcall gtsk$s

;
; LOCAL DATA
;
	TSIZE=132.		;TTY INPUT/OUTPUT SIZE
	PBSIZE=120.		;SIZE OF THE PUTBACK BUFFER
PUTBUF:	.BLKB PBSIZE+2		;BUFFER (+2 IS FOR SAFETY)
PUTB:	.WORD -1		;PUTBACK POINTER, -1=EMPTY
BACK1:	.ASCII /-1P/<12>	;-1P AND NEWLINE
lback1=.-back1
	.EVEN
TFLAG:	.WORD 0 	;USED BY TTYC AND TTYCC

tsklim:
LIM:	.LIMIT			;TO FIND OUT HOW BIG I AM
himem:	.blkw
tmpbuf:	.blkw 16.
IOS:	.BLKW	2		;IO STATUS
	.BLKW	1		;ALSO FOR GPRT$S

CT:	.WORD	-10000.		;TTYNUM CONVERSION TABLE
	.WORD	-1000.		;
	.WORD	-100.		;
	.WORD	-10.		;
CTE:	.BLKW	0		;END

;
; DIRECTIVE BLOCKS
;

R:	QIOW$	IO.RVB,1,1,,IOS,,<IB,TSIZE-1.>
O:	QIOW$	IO.WVB,1,1,,IOS,,<OB,0,40>
;oscr:	QIOW$	IO.wvb!tf.wal,1,1,,IOS,,<OB,0,0>
oscr:	QIOW$	io.wal,1,1,,IOS,,<OB,0,0>
oscr1:	QIO$	io.wal,1,5,,IOS,,<OB,0,0>
atty:
A:	QIOW$	IO.ATT,1,1
;P:	QIOW$	IO.WVB,1,1,,IOS,,<PR,2,44> old way
P:	QIOW$	io.cco,1,1,,IOS,,<PR,2,44>	;new way, cancel control o
dtty:
DET:	QIOW$	IO.DET,1,1

;
; TTY BUFFER CONTROLS
; THE MAGIC NUMBERS IN THE .BYTE JUST BEFORE THE INPUT BUFFER MAKE A
; GMCR$ DPB INTO THE INPUT BUFFER.
;

IC:	.WORD	0		;NUMBER OF CHARACTERS IN TTY BUFFER
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.
;-

EDIT:	DIR$	#A		;ATTACH THE TERMINAL
	WTSE$S	#1		;
        jmp edit2               ;don't know what would happen if startup in pure

	pure



edit2:	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		;
	call newtop		;set the top pointer (now called from elsewhere)
	br $2			;so just call it here too (but skip over it).

;;;;;newtop:	GPRT$S	,#IOS		;GET SIZE OF PARTITION
;;;;;	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		;
;;;;;	return
newtop:			;now using Ron Schaefer's routine (multi-tasker p89)
			; setmem:
	mov r0,-(sp)
	gtsk$s #tmpbuf		;get task parmaeters
	bcs 111$
	mov tmpbuf+26.,r0	;task dynamic size in bytes
	gprt$s	,#tmpbuf	;gprt$ returns
	bcs 111$		;0 in 11d (virt base addr)
	add $dsw,r0		;base addr under 11m (real/virt)
	bic #1,r0		;round down and even align
	mov r0,top		;highest addr+1 (even-aligned)
	sub #2,top		;this little addition is for safety.
	cmp tsklim+2,top
	bhis 111$		;some finite amount is avail
	clc			;sucessful
	mov (sp)+,r0
	rts pc

111$:	mov (sp)+,r0
	sec
	rts pc

$2:	FINIT$			;INITIALISE THE FSR.
        cmp #debuga,#1          ;is debugging enabled
        bne 3$                  ;not if debuga <> 1
        open$w  #tmp            ;open but name it (for debugging)
        br 4$                   ;
3$:     OPNT$D  #TMP            ;GET SCRATCH FILE.
4$:     BCS     80$             ;URK

;
; COMMAND LINE PROCESSING
;

	DIR$	#GMCR		;READ DOWN COMMAND LINE
	BCS	40$		;NOT THERE
	CMPB IB,#"EE		;INVOKED FROM ...EEE?
	BNE 5$
	DEC OE			;SET FLAG
	DEC ONUM		;SET FLAG FOR NUMBERING
	MOV S100,-(SP)
	MOV C100,S100		;SWAP C AND S COMMANDS
	MOV (SP)+,C100
5$:
	MOV	#GMCR+G.MCRB,R0	;POINT AT THE COMMAND BUFFER
	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
;+
; ** PUTBAK - PUT BACK ONTO THE INPUT
;
; INPUT:
;
;  R0 = STRING ADDR (OF BEGINNING OF STRING)
;  R1 = STRING LEN
;
; THE ROUTINE WILL PUSH BACK ONTO THE INPUT THE SPECIFIED
; STRING. NOTE THAT THE PUSH BACK PUTS BACK IN REVERSE ORDER
; WHICH IS NECESSARY TO THE GETC ROUTINE.
;
;-

PUTBAK::
	MOV R3,-(SP)		;SAVE THESE
	MOV R2,-(SP)		;SAVE THESE
	MOV R1,-(SP)
	MOV PUTB,R3		;GET POINTER
	BGE 10$			;ANYTHING THERE NOW
	CLR R3			;NO, INIT IT TO FIRST
	BR 15$
10$:
	INC R3
15$:	MOV R0,R2
	ADD R1,R2		;POINT AT LINE END
	TST R1			;CHECK FOR NULL PUTBACK
	BLE 30$			;AND NOP IF NULL
20$:
	MOVB -(R2),PUTBUF(R3)	;START PUTTING INTO BUFFER
	INC R3
	CMP R3,#PBSIZE		;TOO MUCH?
	BGT 50$			;YES IF GT
	SOB R1,20$		;LOOP ON STRING COUNT
	DEC R3
	MOV R3,PUTB		;SET UP POINTER'S NEW VALUE
30$:
	MOV (SP)+,R1
	MOV (SP)+,R2
	MOV (SP)+,R3
	RTS PC
50$:
	CLR R1
	JMP ERROR
;+
; ** GETC - GET A CHARACTER
;
; GET A CHARACTER, EITHER FROM THE TTY , THE SAVED UP GLOBAL
; COMMAND LINE  , OR FROM THE PUTBAK BUFFER.
; RETURN EOF ON THE END OF THE GLOBAL LINE, BUT KEEP
; RETURNING IT.
;
; ** GETC1 - GET A CHAR (ALTERNATE ENTRY TO NOT CHECK PUTBAK BUFFER).
;
;
; OUTPUTS:
;	R0=CHARACTER
;-

GETC:	MOV	PEEK,R0		;PEEK CHARACTER
	BMI	10$		;NONE
	MOV	#-1,PEEK	;MAKE IT GO AWAY
	BR	40$		;

GETC1	=.
	MOV	PEEK,R0		;PEEK CHARACTER
	BMI	15$		;NONE
	MOV	#-1,PEEK	;MAKE IT GO AWAY
	BR	40$		;

10$:
	MOV PUTB,R0		;IS THERE ANYTHING PUT BACK?
	BLT 15$			;NOT IF LT
	MOVB PUTBUF(R0),R0	;ELSE GET A CHAR
	DEC PUTB		;DECREMENT POSITION
	BR 40$			;RETURN THE CHARACTER
15$:	MOV	GUPTR,R0	;GET GLOBAL POINTER
	BEQ	30$		;NOT IN A GLOBAL
	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

30$:	CALL	TTYICH		;GET CHARACTER FROM TTY
40$:	BIC	#177600,R0	;MASK TO 7 BIT ASCII
	RETURN			;DONE

;+
; ** TTYICH - GET CHARACTER FROM THE TTY
;
; OUTPUTS:
;	R0=CHARACTER
;-

TTYICH:	DEC	IC		;ANY CHARACTERS LEFT
	BPL	20$		;YES

10$:	TST	OV		;VERBOSE
	BEQ	15$		;NO, NO PROMPTS
	TST	EXECM		;EXECUTING A COMMAND
	BNE	15$		;YES, NO PROMPTS
;;;;	DIR$ #DET		;DETACH THE TTY (END OF CONTRL O) no longer
;;;;	WTSE$S #1		;MIGHT NEED THIS
	DIR$	#P		;ISSUE PROMPT and cancel control
	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
;	BNE	10$		;NO, GO AGAIN
	CMPB IOS+1,#33		;IS IT AN ESCAPE
	BNE 16$			;IF NE THEN CONTINUE
	tst ios+2		;check count, non-zero cnt<esc>=control u
	beq 150$		;zero length is .-1p
	call lkill		;clean up the screen at the bottom
	br 15$			;re-read the line
150$:
	CLR IC			;ELSE WE SIMULATE
	MOV R1,-(SP)		;
	MOV #BACK1,R0		; A .-1P
	MOV #lback1,R1		;  COMMAND
	CALL PUTBAK		;   AND PUT IT BACK ONTO INPUT
	MOVB #'.,R0		;THIS IS THE FIRST CHAR
	MOV (SP)+,R1		;(RESTORE THIS)
	RETURN			;NOW GET OUT WITH THE FIRST CHAR
16$:	MOV	IOS+2,R0	;GET BYTE COUNT
	MOVB	#NL,IB(R0)	;APPEND A NEWLINE TO THE BUFFER
	MOV	R0,IC		;RESET COUNT
	MOV	#IB,IP		;RESET POINTER

20$:	MOVB	@IP,R0		;GET THE CHARACTER
	INC	IP		;
	BIC	#177600,R0	;7 BIT ASCII
	BEQ	TTYICH		;EAT NULLS
	RETURN			;DONE
30$:	CALL TTYSKP		;CLEAN OUT ANYTHING IN GET BUFFERS
	MOV #IS.CR,IOS		;WE WILL
	MOV #5,IOS+2		; SIMULATE A: "X <nl> f  <NL> Q"
	tst execm		;executing a command (like a or i)
	beq 35$			;not if eq, so its ok
	movb #'.,ib
	movb #nl,ib+1		;change the control z to a .<cr>
	mov #1,ios+2		;and set length to 1 char.
	br 16$
35$:	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.
	clr smode		;out of screen mode for the exit
        mov #-1,pflg            ;so we can set protection on next write
	BR 16$			;     THEN CONTINUE AS THOUGH NOTHING HAPPENDED.

;+
; ** 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.
;-

TTYSKP:	CLR	IC		;EASY
	MOV #-1,PUTB		;EASY
	MOV	#-1,PEEK	;TO DO
	RETURN			;

;+
; ** TTYSTR - OUTPUT A STRING TO THE TTY
;
; INPUTS:
;	R0=POINTER TO ASCIZ STRING
;
; USES:
;	R0
;-

TTYSTR:	MOV	R1,-(SP)	;SAVE REGISTERS
	MOV	R0,R1		;GET WORKING COPY OF STRING

10$:	MOVB	(R1)+,R0	;FROM STRING
	BEQ	20$		;DONE
	CALL	TTYC		;OUT IT GOES
	BR	10$		;

20$:	MOV	(SP)+,R1	;RETURN
	RETURN			;

;+
; ** TTYNL - OUTPUT A NEWLINE TO THE TTY
;-

TTYNL:	MOV	R0,-(SP)	;SAVE R0
	MOV	#NL,R0		;PUT OUT THE NEWLINE
	CALL	TTYC		;
	MOV	(SP)+,R0	;RESTORE R0
	RETURN			;

;+
; ** TTYNUM - OUTPUT A NUMBER TO THE TTY
;
; INPUTS:
;	R1=THE NUMBER
;
; USES:
;	R0, R1
;-

TTYNUM:	MOV	R2,-(SP)	;SAVE REGISTERS
	MOV	R3,-(SP)	;

	TST	R1		;TEST FOR NEGATIVE NUMBERS
	BPL	10$		;NO
	MOV	#'-,R0		;PUT OUT "-" SIGN
	CALL	TTYC		;
	BR	20$		;
10$:	NEG	R1		;FORCE POSITIVE TO NEGATIVE

20$:	MOV	#CT,R2		;GET TABLE POINTER
	CLR	R3		;CLEAR SUPRESS FLAG

30$:	MOV	#'0,R0		;START WITH "0"
40$:	CMP	R1,(R2)		;GET A DIGIT
	BGT	50$		;
	SUB	(R2),R1		;
	INC	R0		;
	BR	40$		;

50$:	CMP	R0,#'0		;IS IT A ZERO
	BNE	60$		;NO
	TST	R3		;ARE WE SUPRESSING
	BEQ	70$		;YES
60$:	CALL	TTYC		;PUT IT OUT
	INC	R3		;END SUPRESSION

70$:	TST	(R2)+		;GET NEXT ENTRY
	CMP	R2,#CTE		;AT THE END
	BLO	30$		;NO

	MOV	R1,R0		;DO THE LAST DIGIT
	NEG	R0		;
	ADD	#'0,R0		;
	CALL	TTYC		;

	MOV	(SP)+,R3	;RETURN
	MOV	(SP)+,R2	;
	RETURN			;

;+
; ** 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.
; FOR SOME UNKNOWN REASON DEC DOES NOT WRITE OUT THE SLEW BYTES ON A
; ZERO BYTE WRITE. SPECIAL CHECKING MUST BE DONE IN THIS CASE.
;
; INPUTS:
;	R0=CHARACTER
;-

TTYCC:	CLR TFLAG		;DONT CHECK CONTROL CHARS
	BR 2$
TTYC=.
	MOV #1,TFLAG
2$:
	MOV	R0,-(SP)	;SAVE THE CHARACTER
	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
	tst smode		;screen mode?
	beq 15$			;not if eq
	mov o+q.iopl+2,oscr+q.iopl+2	;move in the count
	add #ob,r0		;get to end of line
	inc oscr+q.iopl+2	;one more char
	movb #15,(r0)		;add a cr
	dir$ #oscr		;output the line null carr control
	br 17$
15$:	DIR$	#O		;OUTPUT THE LINE
17$:	WTSE$S	#1		;
	MOV	#OB,OP		;WIND BACK THE POINTER
	BR	30$		;

20$:
	TST TFLAG
	BEQ 25$		;DONT CHECK IF ENTRY AT TTYCC
	cmp r0,#177	;is it a null converted to 177
        bhis 23$        ;yes if eq, so replace with ?  (or greater)
	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	;we don't want to echo form-feed either
;	beq 25$		;its an ok control char
	cmp r0,#15
	beq 25$		;its an ok control char
23$:	mov #'?,r0		;replace it then
25$:	CMP	OP,#OB+TSIZE 	;AT THE END OF THE BUFFER
	BHIS	30$		;YES, DISCARD
	MOVB	R0,@OP		;SAVE THE CHARACTER
	INC	OP		;

30$:	MOV	(SP)+,R0	;RETURN
	RETURN			;

;+
; ttflsh - flush the tty buffer with null carriage control
; ttfls2 - flush the tty buffer with null carriage control no wait, ef 5
;
;-

ttflsh:	
	mov	op,r0		;compute count
	sub	#ob,r0		;
	mov r0,oscr+q.iopl+2	;set into the dpb
	dir$ #oscr		;output the line null carr control
	MOV	#OB,OP		;WIND BACK THE POINTER
	rts	pc
ttfls2:	
	mov	op,r0		;compute count
	sub	#ob,r0		;
	mov r0,oscr1+q.iopl+2	;set into the dpb
	dir$ #oscr1		;output the line null carr control
	MOV	#OB,OP		;WIND BACK THE POINTER
	rts	pc
	.END	EDIT
