	SUBROUTINE ININDX(FNAME,L,M,NLD)
C	This routine allows you to enter text into a file using EDITOR,
C	KED, etc., and print it to a terminal or file with the speed
C	of a direct access file.  This greatly reduces the size of a
C	task image and also allows changes in the text without re-
C	compiling the program.
C	Routine takes a sequential text output file, FNAME and creates
C	a Direct Access text file (.DAX extension) which is then used
C	with the DAO routine.  This enables large text files to be
C	accessed quickly while still using a user-written text file
C	that can be created and changed with EDITOR, KED, etc.
C	Refernece numbers from 1 to 9999 precede any text associated with
C	them, beginning in column 1.  A '.' in column 1 indicates a dot control
C	string; the function of the string is defined by the character in
C	column 2.  Associated text is any number of lines of text or any
C	other character data to be output when DAO(nnnn,lun1,lun2) is
C	called.
C	. CONTROL FUNCTIONS.   Format:    .a comments
C	The '.' must be in column 1; "comments" is anything from column 3 on.
C	.a	function:
C	.C	clear screen before writing
C		(if LUNT=6 in DAO call, .C has no effect)
C	.B	rings bell
C	. 	(space or tab) non-printing comment (line ignored)
C	.n	skip n lines; n is from 1 to 9
C	.W	wait for user response (<RETURN> or <ENTER>)
C	The .DAX file is a direct access, temporary file.
C	L is the LUN of FNAME and M is the LUN of the .DAX file
C
C	NLD is delete status of the file; if 0 or omitted the .DAX file
C	will be deleted, if not 0 then it will be saved.
C	FNAME will be closed, freeing LUN L, but the .DAX file must be
C	closed when the main program is finished with it (CALL EXIT or
C	CALL CLOSE(M) or CLOSE(UNIT=M).
C 
	LOGICAL*1 FNAME(32),DNAME(32)
	DIMENSION IREF(40),IZERO(40)
	CHARACTER A*80,B*1,C*1
	DATA IZERO /40*0/
C	FNAME IS USER'S SEQUENTIAL TEXT FILE
C	DNAME IS NAME OF .DAX TEMPORARY, DIRECT ACCESS FILE
C	IREF CONTAINS INDICES TO USER'S REFERENCE NUMBERS
C	A IS A STRING IN FNAME; B IS A(1:1).
C	L IS LUN FOR FNAME, M IS LUN FOR .DAX FILE
C	N=0 OR OMITTED: DELETE .DAX FILE; ELSE SAVE IT
	DO 1 I=1,27
	DNAME(I)=FNAME(I)
	IF(FNAME(I).EQ.'.') GOTO 2
1	CONTINUE
5	WRITE(5,3)
3	FORMAT(' ININDX -- File initialization failure')
	GOTO 100   ! RETURN
C 
C	I IS STARING POSITION FOR ".DAX" EXTENSION
2	DNAME(I+1)='D'
	DNAME(I+2)='A'
	DNAME(I+3)='X'
	DO 4 J=I+4,32
	DNAME(J)="0
4	CONTINUE
C	CHECK SAVE STATUS OF .DAX FILE:
	J=0
	CALL GETADR(J,NLD)
	IF(J.LT."1000) NDL=0
	IF(J.GT."1000) NDL=NLD
	OPEN(UNIT=L,NAME=FNAME,STATUS='OLD',READONLY)
	CLOSE(UNIT=M)
	IF(NDL.EQ.0) OPEN(UNIT=M,ACCESS='DIRECT',ASSOCIATEVARIABLE=IREC,
     CFORM='UNFORMATTED',RECL=20,NAME=DNAME,STATUS='SCRATCH',ERR=5)
	IF(NDL.NE.0) OPEN(UNIT=M,ACCESS='DIRECT',ASSOCIATEVARIABLE=IREC,
     CFORM='UNFORMATTED',RECL=20,NAME=DNAME,STATUS='NEW',ERR=5)
C 
C	FIRST PASS:
C	FIND ALL REFERENCE NUMBERS AND STORE AT BEGINNING (INDEX) OF
C	.DAX FILE.  40 REF. NUMBERS WILL BE STORED PER LINE.
	JREC=0
	MAXREF=0
	INDEXH=0  ! HIGHEST RECORD NUMBER NEEDED FOR INDEX FILE
C	IF NEW INDEXH IS FOUND, LINES FROM OLD ONE TO NEW ONE MUST BE
C	WRITTEN TO FILE.
20	READ(L,21,END=50) A
	JREC=JREC+1
21	FORMAT(A)
	B=A(1:1)
	IF(B.GE.'0'.AND.B.LE.'9') THEN
C	HAVE A REFERENCE NUMBER; DECODE IT:
	NUM=0   ! REF NUMBER
	DO 26 I=1,4
	IF(A(I:I).LT.'0'.OR.A(I:I).GT.'9') GOTO 26
	NUM=NUM*10+ICHAR(A(I:I))-48
26	CONTINUE
C	HAVE DECODED REF. NUMBER IN NUM; STORE IN FILE:
27	N=NUM-1
	INDEXT=N/40 + 2
	IF(INDEXT.GT.INDEXH) THEN
	   DO 28 I=INDEXH+1,INDEXT
	   WRITE(M'I) IZERO
28	   CONTINUE
	   INDEXH=INDEXT
	   END IF
	NREF=N-N/40*40+1
	READ(M'INDEXT) IREF
	IREF(NREF)=JREC
	WRITE(M'INDEXT) IREF
	END IF
	IF(NUM.GT.MAXREF) MAXREF=NUM
	GOTO 20
C 
C	SECOND PASS:
C	INCREMENT ALL RECORD INDICES BY INDEXH, THEN COPY SEQUENTIAL
C	FILE TO END OF D.A. FILE.
50	IREF(1)=MAXREF
	WRITE(M'1) IREF  ! STORE INDEX SIZE IN LINE 1 OF D.A. FILE
	DO 51 I=1,INDEXH
	READ(M'I) IREF
	DO 52 J=1,40
	IF(IREF(J).GT.0) IREF(J)=IREF(J)+INDEXH
52	CONTINUE
	WRITE(M'I) IREF
51	CONTINUE
C 
	REWIND L
	IREC=INDEXH+1
55	READ(L,21,END=60) A
	IF(A(1:1).GE.'0'.AND.A(1:1).LE.'9') THEN
C	   RIGHT-JUSTIFY NUMBER IN FIELD FOR LATER COMPARISONS:
	   N=INDEX(A,' ')
	   IF(N.LT.5.AND.N.GT.0) THEN
	      A(6-N:)=A
	      DO 58 K=1,5-N
	      A(K:K)='0'
58	      CONTINUE
	      END IF
	   END IF
	WRITE(M'IREC,ERR=5) A
	GOTO 55
C 
C	WRITE 'END-OF-FILE' MARK TO .DAX FILE:
60	A='.END-OF-FILE'
	WRITE(M'IREC,ERR=5) A
C 
C	FINISHED INDEXING FILES:
90	CALL CLOSE(L)
100	RETURN
	END
