c.begin.doc ************************** begin.doc
C 
C .c ;Module name:
C .c ;^&SCAN\&
C .NF	
C .X SCAN>Defined
C Source:SCAN0.MOD
C Designer           :H.BARTA
C Author             :H. BARTA, E. LAKIA
C Date of last update:25-AUG-86
C Revision level     :1.1
C 	
C .C ;Formal Parameter List
C Receives:
C 
C  D FNAME           CHAR*80    File name to be scanned for document sections
C 
C Returns:
C     
C Accesses common(s):
C 	
C .X SUCK1>Common
C         SUCK1                 Common contains statistics regarding what was or
C                               was not found during the scan. The variablea are:
C                                    
C .X SUCK2>common
C         SUCK2                 Common contains statistics regarding what was or
C                               was not found during the scan. The variablea are:
C                                    
C                                         MODDIR(1000)   CHAR*80 (1-80 File name
C                                                              81-100 Module name)
C                                         MODCNT        Number of doc's found.
C                                         
C                                         NFOUND(1000)   CHAR*80 File names that
C                                                               had no doc's.
C                                         NFDCNT        number of entries found in
C                                                       NFOUND.
C 	
C Accesses file(s):
C 	
C .x SFNAME>READ
C 	
C .x DOC.RNO>WRITE
C 	
C Other modules referenced:
C .SK
C .fill
C .SK
C Description:
C .sk    
C 	This module will scan a source file and look for a document section
C bounded by lines containing the following strings:
C .nf
c .sk 2
C b e g i n.doc
C 	.
C 	.
C 	.
C   (document section)
C 	.
C 	.
C 	.
C e n d.doc 
c .sk 2
C .fill
C The module name will be found in the fourth line of the section (hopefully!)
C and will be bracketed as follows:  
C .nf
C                   _.ce ;_^_&SCAN_\_&
C .fill
C where SCAN is the module name in this case.
C When the begin.doc string is found, all lines following it up to
C but not including the line containing end.doc will be copied to
C DOC.RNO. The line containing the source file reference will be modified to 
C represent the source currently being scanned. The first two characters of each
C line will be truncated.When the end.doc string is found, the module 
C name, if found, will be listed as a runoff comment (.co MODULE.NAME=aaaaaa)
C and the module name and source file will be added to MODDIR in common SUCKER.
C If no document section is located, the filename will be added to the list
C NFOUND in common SUCKER.
C 
C.end.doc ****************************** end.doc

C
C
	SUBROUTINE SCAN ( SFNAME )
C
C
	CHARACTER	SFNAME*80	!filename to be scanned
c
c
	CHARACTER 	INLINE*132,	!input line
	1		MODNAM*20	!module name
C
	BYTE		INBYTE(132)	!will be equivalenced to
c					!INLINE so the "Q" format works
	EQUIVALENCE	(INLINE,INBYTE)
c
c------------------------------------------------------------------------
c             common sucker
	CHARACTER*100	MODDIR(1000)	!modules and filenames found
	CHARACTER*80	NFOUND(1000)	!files where no document was found
c
	INTEGER*2	MODCNT,		!number of entries in MODCNT
	1		NFDCNT		!number of entries in NFOUND
C
	COMMON / SUCK1 / MODDIR, NFOUND
	COMMON / SUCK2 / MODCNT, NFDCNT
C
C-------------------------------------------------------------------------
C
	LOGICAL*1 	HL
	LOGICAL*1	FILOPN		!is output file "DOC.RNO" open
	DATA 		FILOPN / .FALSE. /
	DATA		HL/.FALSE./
C
C  Open Source file to be scanned
C
	FILHDR=.FALSE.	! SET FOR NEW FILE NAME
	IFILSIZE=80	! MAXIMUM FILE NAME STRING SIZE
	CALL CHAR_TRNC(SFNAME,SFNAME,IFILSIZE) ! TRUNCATE ANY TRAILING SPACES
	OPEN( 	UNIT=2,
	1	ACCESS='SEQUENTIAL',
	2	TYPE='OLD',
	3	NAME=SFNAME,
	4	ERR=999,
	5	READONLY			)
C
c
	HL=.FALSE.
	NFND=0		!initialize counter for doc's found
	TYPE *, 'SCANNING ',SFNAME

C
C-------------------- SCAN FOR BEGINNING OF DOC----------------------------
c
C  now read lines, scanning for the 'begin.doc'
 1	READ( 2, 100, ERR=998, END=99 )LINLEN, (INBYTE(I), I=1, LINLEN)
 100	FORMAT( Q, 132A1 )
	
	IF ( INDEX(INLINE(1:LINLEN), 'begin.doc').GT. 0 ) GOTO 2 
C						!cause a doc is found
C  else read the next line
C
	GOTO 1
C
C
C-------------------- EXTRACT DOCUMENT UNTIL END DOC FOUND ----------------
C
C  arrive here to copy lines to the output file "DOC.RNO" until the "end.doc"
c  is found
C is output file open?
c
 2	IF( .NOT.FILOPN ) THEN	!open it!!!
		OPEN( 	UNIT=3,
	1		TYPE='NEW',
	2		ACCESS='SEQUENTIAL',
	3		CARRIAGECONTROL='LIST',
	4		FORM='FORMATTED',
	5		DISPOSE='SAVE',
	5		NAME='DOC.RNO',
	6		ERR=996 		)
	        FILOPN = .TRUE.
	ENDIF
C 
C 		Write the runoff command .HL 1...for the filename
c
		IF( .NOT. HL)THEN
		  WRITE(3,170)SFNAME(1:IFILSIZE)	!  print file name
170		  FORMAT('.HL 1 ',A<IFILSIZE>)
		  HL=.TRUE.	! FILE NAME YET?
		ENDIF
C
C
 	LINCNT=0

C
C  ----- READ A LINE FROM THE SOURCE FILE ----------
C
 3	READ( 2, 100, ERR=998, END=997 )LINLEN,  (INBYTE(I), I=1, LINLEN)

C
C  first check to see if the line is the end.doc (any non-comment is 
c  assumed to be an end.doc)
C
	IF ( ((INDEX(INLINE(1:LINLEN), 'end.doc') .GT. 0) .OR. (
	1		INLINE(1:1) .NE. 'c' .AND.
	2		INLINE(1:1) .NE. 'C' .AND.
	3		INLINE(1:1) .NE. ';' .AND.
	4		INLINE(1:1) .NE. ' ' .AND. 
	5		INLINE(1:1) .NE. '{' .AND.
	6		INLINE(1:1) .NE. '}' )
	7		))THEN
C
C	    EITHER FOUND THE END.DOC OR ELSE NON MACRO, FORTRAN, PASCAL OR
C	    C LANGUAGE COMMENT LINE
C
	    IF(LINCNT.LT.4) MODNAM(1:20)='**  not found **'
C
	    MODCNT=MODCNT+1 			!another module found
	    NFND=NFND+1				!another mod. found in this file
	    IF (MODCNT.LE.1000) THEN
		MODDIR(MODCNT)(81:100)=MODNAM 	!save its name and source file
		MODDIR(MODCNT)(1:80)=SFNAME(1:IFILSIZE)
	    ENDIF
c
	    WRITE( 3, 300, ERR=996 ) ! trailing page so 
C						all modules start on a new page
 300	    FORMAT( '.PAGE')
	    GOTO 1			! to scan for the next begin.doc
c
	ELSE	! a document line has been read in
C
c  check first for a module name line (4), or a source file name (7)
C
	    LINCNT=LINCNT+1
	    IF (LINCNT.EQ.4) THEN	!look for module name
		ICHR1=INDEX(INLINE, '^&')+2
		ICHR2=INDEX(INLINE, '\&')-1
		IF( ICHR2-ICHR1.GT.0 ) THEN
		  MODNAM(1:20)=INLINE(ICHR1:ICHR2)	! GET MODULE NAME
		  WRITE(3,310)MODNAM	! SECOND LEVEL MODULE NAME
310		  FORMAT('.HL 2',A)
		ELSE
		  MODNAM(1:20)='**  not found **'
		ENDIF
	    ELSE IF(LINCNT.LT.10) THEN	!look for "Source:"
		IF( INDEX(INLINE, 'Source:').NE.0 ) THEN
		     ISPOS=INDEX(INLINE,':')+1
		     INLINE(ISPOS:ISPOS+IFILSIZE-1)=
	1		SFNAME(1:IFILSIZE)   ! Force true source filename 
C						from file we are scanning
		     LINLEN=ISPOS+IFILSIZE-1   ! LINE LENGTH
		ENDIF
	    ENDIF
C
	    WRITE( 3, 200, ERR=996 ) (INBYTE(I), I=3, LINLEN)	
 200	    FORMAT( 132A1 )
	ENDIF
C
C read the next line
C
	GOTO 3

c------------------  o n     E O F -----------------------------
c
c  arrive here with an EOF on the input file while scanning for a begin.doc
c  If at least 1 doc section was found, all is ok, otherwise, add an entry
c  to nfound	
c
 99	IF( NFND.EQ.0 ) THEN !no doc was found in this file
		NFDCNT=NFDCNT+1
		IF(NFDCNT.LE.1000) NFOUND(NFDCNT)=SFNAME
	ENDIF
C
	CLOSE( 	UNIT=2,
	1	DISPOSE='SAVE' )
C
	RETURN
C
c
c
C  arrive here with an EOF while scanning for an end.doc which is 
c  an abnormal condition
c
 997	IF(LINCNT.LT.4) MODNAM(1:20)='**  not found **'
C
	MODCNT=MODCNT+1 			!another module found
	MODDIR(MODCNT)(81:100)=MODNAM 	!save its name and source file
	MODDIR(MODCNT)(1:80)=SFNAME
C
	WRITE( 3, 300, ERR=996 ) MODDIR(MODCNT)(1:80)
C
C  notify operator of fault
C
	TYPE *, 'premature EOF when scanning ',FNAME 
	TYPE *, 'found "begin.doc" for ',MODNAM,' and encountered'
	TYPE *, 'EOF before finding "end.doc"'
C
	CLOSE( 	UNIT=2,
	1	DISPOSE='SAVE' )
C
	RETURN

c
c  --------------------  error handlers ---------------------- 
C
C  arrive here with an error opening file to be scanned.
c  error is already annunciated by the OTS so
 999	TYPE *, 'Error trying to open ',SFNAME
	RETURN
C
C
c arrive here with an error trying to read SFNAME
 998	TYPE *, 'error trying to read ',SFNAME
C
	WRITE(3, 9981 )SFNAME
 9981	FORMAT( '.! ** error trying to read: '
	1	,/1X,A60,/,' may effect document')
C
	CLOSE (	UNIT=2,
	1	DISPOSE='SAVE' )
C
	RETURN
C
C
c  error with output file
 996	TYPE *, 'ERROR OPENING OR WRITING OUTPUT FILE'
 	CALL EXIT
	END
