; -*- Midas -*-

TITLE DIR

A=1
B=2
C=3
D=4
E=5
F=6

LINES=7				; Lines on screen
LLINES=10			; Lines on last page
OPTS=11				; .OPTION bits

ERR=14
IN=15
OUT=16
DIR=17

NPTRS==:200.

GO:	.OPEN OUT,[.UAO\%TJDIS,,'TTY ? SETZ ? SETZ]
	 .LOSE
	.OPEN IN,[.UAI,,'TTY ? SETZ ? SETZ]
	 .LOSE
	.CALL [	SETZ
		SIXBIT /TTYVAR/
		MOVEI OUT
		MOVE [SIXBIT /HEIGHT/]
		SETZM LINES]
	 .lose %lssys
	SUBI LINES,1
	CAILE LINES,NPTRS
	 MOVEI LINES,NPTRS
	.suset [.rsname,,dirdir]
	.suset [.roption,,opts]
	tlne opts,%opbrk
	 .break 12,[..rpfi,,dirblk]
	tlnn opts,%opcmd
	 jrst godoit
	.BREAK 12,[..RJCL,,BUF]
	SKIPN BUF
	 JRST GODOIT
	movsi a,(sixbit /DSK/)	; If there is JCL, device defaults to DSK.
	movem a,dirdev
	MOVE A,[440700,,BUF]
GETNAM:	MOVE B,[440600,,C]
	SETZ C,
GETNM1:	ILDB D,A
	JUMPE D,JDONE
	CAIN D,^M
	 JRST JDONE
	CAIE D,^_
	 CAIN D,^C
	  JRST JDONE
	CAIN D,":
	 JRST COLON
	CAIN D,";
	 JRST SEMI
	CAIN D,40
	 JRST SEMI
	CAIL D,140
	 SUBI D,40
	SUBI D,40
	TLNE B,770000
	 IDPB D,B
	JRST GETNM1

COLON:	JUMPE C,GETNAM
	movem c,dirdev
	JRST GETNAM

SEMI:	JUMPE C,GETNAM
	movem c,dirdir
	JRST GETNAM

JDONE:	JUMPE C,GODOIT
	movem c,dirdir
GODOIT:	.call [	setz ? sixbit /OPEN/
		movsi .uai
		movei dir
		move dirdev
		move [sixbit /.FILE./]
		move [sixbit /(DIR)/]
		setz dirdir ]
	 JRST LOSE
	tlne opts,%opbrk
	 .break 12,[..spfi,,dirblk]
	setzm incnt		; No characters available
FILL:	MOVE B,[440700,,BUF]
	MOVN C,LINES
	HRLZ C,C		; C: AOBJN into PTRS
	SETZM PTRS		; 0 => blank
	MOVE A,[PTRS,,PTRS+1]	; -1 => printed
	BLT A,PTRS-1(LINES)	; else BP to string
FILL1:	MOVEM B,PTRS(C)
FILL2:	sosge incnt
	 jrst infill
	ildb a,inbp
	IDPB A,B
	CAIN A,^L
	 JRST ENDDIR
	CAIE A,^J
	 JRST FILL2
	AOBJN C,FILL1
	JRST ENDPAG

.vector inbuf(linbuf==:1000.)
.scalar incnt
.scalar inbp

infill:	move d,[440700,,inbuf]
	movem d,inbp
	movei e,5*linbuf
	.call [ setz ? sixbit /SIOT/
		movei dir
		move d
		setz e ]
	 .lose %lssys
	movei d,5*linbuf
	subi d,(e)
	movem d,incnt
	jumpn d,fill2
	MOVEI A,^L
	IDPB A,B
ENDDIR:	SETZM PTRS(C)
	HRRZM C,LLINES
ENDPAG:	MOVE C,LINES
	.RDTIM D,
FIND:	IMULI D,317421
	ADDI D,512642
	MOVE E,D
	IDIV E,LINES
	MOVNI B,1
	MOVE E,F
FIND1:	CAME B,PTRS(F)		; Printed?
	 JRST FOUND		; No
	SOJGE F,FIND1		; Yes: search backwards
	MOVEI F,-1(LINES)
	JRST FIND1

FOUND:	ADDI F,10
	.IOT OUT,[^P]
	.IOT OUT,["H]
	.IOT OUT,[10]
	.IOT OUT,[^P]
	.IOT OUT,["V]
	.IOT OUT,F
	.IOT OUT,[^P]
	.IOT OUT,["L]
	SKIPN E,PTRS-10(F)	; Blank?
	 JRST LINEND		; Yes
LINE:	ILDB B,E		; No: print it
	CAIN B,^M
	 JRST LINEND
	.IOT OUT,B
	JRST LINE

LINEND:	SETOM PTRS-10(F)
	SOJG C,FIND
	CAIN A,^L
	 JRST BYEBYE
	.IOT OUT,[^P]
	.IOT OUT,["H]
	.IOT OUT,[10]
	.IOT OUT,[^P]
	.IOT OUT,["V]
	MOVEI F,10(LINES) 
	.IOT OUT,F
	.IOT OUT,[^P]
	.IOT OUT,["L]
	SKIPA B,[440700,,[ASCIZ \--MORE--\]]
MORE:	 .IOT OUT,A
	ILDB A,B
	JUMPN A,MORE
	.IOT IN,A
	CAIE A,40
	 JRST FLUSH
	.IOT OUT,[^P]
	.IOT OUT,["H]
	.IOT OUT,[10]
	.IOT OUT,[^P]
	.IOT OUT,["L]
	JRST FILL

FLUSH:	SKIPA B,[440700,,[ASCIZ \FLUSHED\]]
FLUSH1:	 .IOT OUT,A
	ILDB A,B
	JUMPN A,FLUSH1
BYEBYE:	.IOT OUT,[^P]
	.IOT OUT,["H]
	.IOT OUT,[10]
	.IOT OUT,[^P]
	.IOT OUT,["V]
	MOVEI F,10(LLINES)
	.IOT OUT,F
	.IOT OUT,[^P]
	.IOT OUT,["L]
	.BREAK 16,164000	; 2.3 => Don't crlf, close open location, etc.

LOSE:	.OPEN ERR,[.UAI,,'ERR ? 1 ? 0]
	 .LOSE
LOSE1:	.IOT ERR,A
	CAIN A,^L
	.BREAK 16,164000	; 2.3 => Don't crlf, close open location, etc.
	.IOT OUT,A
	JRST LOSE1

dirblk::
dirdev:	sixbit /DSK/
dirdir:	-1
	0
	0

PTRS:	BLOCK NPTRS

BUF:	BLOCK NPTRS*40.		; 200. character lines

END GO
