PROCEDURE	<START - Runoff file command processing>,010005
;+
; Copyright (c) 1976
; Digital Equipment Corporation, Maynard, Mass.
;
; This software is furnished under  a license for use only  on  a
; single computer system and may be  copied only with  the inclu-
; sion of  the  above  copyright notice.  This software,  or  any
; other copies thereof, may not be  provided  or  otherwise  made
; available to any other person except  for  use  on  such system
; and to  one who agrees  to  these license  terms.  Title to and
; ownership of the software shall at all times remain in DEC.
;
; The information in this software  is  subject to change without
; notice and should not be construed  as  a commitment by Digital
; Equipment Corporation.
;
; DEC assumes no responsibility for the use or reliability of its
; software on equipment which is not supplied by DEC.
;
; Runoff start up initialization
;
; Written: 1-Jun-72, -0.0.0-, L. Wade
;
; Modified: 12-Jan-80, -1.0.0-, Henry R. Tumblin
;	Produced Duke supported version
;
; Verified: 12-Jan-80, -1.0.0-, Henry R. Tumblin
;
; Modified: 9-July-80, -1.0.1-, Jon Berntsen
;	Made Default DOC and TOC files NL instead of TI and nothing
;
; Modified: 29-July-80, -1.0.2-, Bruce C. Wright
;	Fixed so that default DOC and TOC files default to nothing
;	on IAS since IAS doesn't have the NL: device.
;
; Modified: 06-Oct-80, -1.0.3-, John D. Leonard
;	Force Form feed if /PA switch used.
;
; Modified: 10-Oct-80, -1.0.4-, John D. Leonard
;	Changed OPNS$R to OPEN$R so get error message if an input file
;	is opened for editing.
;
; Modified: 04-Dec-80, -1.0.5-, John D. Leonard
;	Added /PITCH switch and initialization of margins accordingly
;-

	.SBTTL	MACRO LIBRARY CALLS AND RUNOFF GLOBALS

	.MCALL	CLOSE$,CSI$,CSI$1,CSI$2,EXIT$S,GCMLD$,DIR$,OPNS$R
	.MCALL	GCML$,GPRT$S,GTSK$S,OPEN$R,OPEN$W,FINIT$,FDBF$R
	.MCALL	OPEN$R

;	RUNOFF global definitions

	.GLOBL	FFEED			; form feed
	.GLOBL	INLPG			; initial lines/page
	.GLOBL	INSW			; Input file switch table
	.GLOBL	LGO			; entry, main processing loop
	.GLOBL	LINSET			; init counts and pntrs for new line
	.GLOBL	LSTBLK			; output file fdb
	.GLOBL	LSTFIL			; output file default fnb
	.GLOBL	NLPG			; number of lines per page
	.GLOBL	OPRWAT			; operator wait message
	.GLOBL	OUSW			; Output file switch table
	.GLOBL	OUTPUT			; flush line buffer to output file
	.GLOBL	PNLPG			; permanent number of lines/page
	.GLOBL	PRMRG			; permanent right margin
	.GLOBL	RMARG			; current right margin
	.GLOBL	SPLSW			; spool file flag
	.GLOBL	SPSAV			; SP save area
	.GLOBL	SSTV			; declare sst dpb
	.GLOBL	TOCBLK			; TOC fdb
	.GLOBL	TOCSPC			; TOC file flag
	.GLOBL	TOCSW			; TOC switch table
	.GLOBL	TTIBLK			; gcml fcb
	.GLOBL	TTIBUF			; command input buffer
	.GLOBL	TTILIN			; command input buffer size
	.GLOBL	TTLDMY			; source input fdb
	.GLOBL	ULSWT			; underline flag word
	.GLOBL	$INITL			; task initialization
	.GLOBL	$INSW			; Input file switches
	.GLOBL	$OUSW			; Output file switches
	.GLOBL	$SDISW			; secondary buffer flag
	.GLOBL	$TOCSW			; TOC switches
	.GLOBL	$ULMSW			; underline - backspace flag
	.GLOBL	$ULNSW			; underline - line mode flag
	.GLOBL	$ULSSW			; underline - simulate flag

;	globals defined in this module

	.GLOBL	ENDFIL			; endfile processing
	.GLOBL	FLAGS			; task state flags
	.GLOBL	RUNOFF			; task restart entry point
	.GLOBL	TF.MOR			; More input files available
	.GLOBL	$START			; task startup point

	.SBTTL	LOCAL DATA

	DATA	STARTD

; csi control block

	CSI$			; define csi control block offsets
	GCMLD$			; define gcml offsets

CSIBLK:	.BLKB	C.SIZE		; csi control block
FLAGS:	.BLKW	1		; Local flags

;	task status flags

	TF.MOR	=	1		; More input files flag
	TF.OFO	=	2		; Output file open flag
	TF.PIN	=	4		; Pointers already initialized

	.SBTTL	$START-START UP INITIALIZATION

	CODE	START

$START::FINIT$			; Initialize FCS

	MOV	SP,SPSAV	; save initial stack pointer
	DIR$	#SSTV		; claim sst vectors
	CLR	FLAGS		; Initialize local flags
	DIAG	VERSN		; display version message
	BISB	#GE.IND!GE.CLO!GE.COM!GE.CON,TTIBLK+G.MODE ; Set up GCML


	.SBTTL	START MAINLINE

RUNOFF::MOV	SPSAV,SP	; reset stack pointer

5$:	CALL	$INITL		; initialize variables
AGAIN:
	MOV	SPSAV,SP	; recover SP again.
10$:	CLOSE$	#TTLDMY		; close input file
	CLOSE$	#LSTBLK		; close listing file
	CLOSE$	#TOCBLK		; CLose TOC file
	CLRB	TOCSPC		; Clear TOC flag
	CLR	FLAGS		; Clear local flags
;
;	Get a command line from the command input file.
;
15$:	MOV	#TTIBUF,R0	; Point to input buffer
	MOV	#TTILIN,R1	; Set length
	CALL	REACMD		; Get command line
	BCC	30$		; CC - then no errors
	CMPB	#GE.EOF,R3	; Top level EOF ?
	BNE	20$			; NE - Then command input error
	CLOSE$	#TTIBLK		; Close command input file
	DIAG	EXIT		; exit gracefully(always)

20$:	DIAG	MGCDME		; command error
	BR	10$

;	Analyze the command syntax and validate

30$:	CSI$1	#CSIBLK,R1,R2   ; check syntax
	BCS	20$		; CS - then syntax error.
	CALL	PARFIL		; parse the file spec

	.SBTTL	CHECK CSI STATUS
;
;	Now parse the output file specification. Check to see
;	if the = sign was specified. If it wasn't, then direct the
;	output to NL0:
;
60$:	BIT	#TF.OFO,FLAGS		; Output file open ?
	BNE	100$			; NE - yes
	BIS	#TF.OFO,FLAGS		; Else indicate so
	MOVB	#1,LSTSPC		; Indicate a file exists.
	BITB	#CS.EQU,CSIBLK+C.STAT	; = sign specified ?
	BNE	62$			; NE - Yes
	.IF DF	I$$AS ! R$$11D
	BIC	#TF.OFO,FLAGS		; Else indicate not
	CLRB	LSTSPC			; Indicate does not exist.
	BR	100$			; And leave.
	.IFF
	MOV	#"NL,LSTFIL+N.DVNM	; Set default device
	OPEN$W	#LSTBLK			; Open output file
	BCC	100$			; CC - then output is open
	MOV	R0,-(SP)
	DIAG	MGOFER,FCS
	JMP	AGAIN
	.ENDC	;I$$AS ! R$$11D

;	Parse the output listing file spec and switches

62$:	CSI$2	#CSIBLK,OUTPUT,#OUSW	; Parse output file
	BCC	80$			; CC - then parsed OK

70$:	DIAG	MGOFER
	JMP	AGAIN

80$:	BITB	#CS.WLD,C.STAT(R0)	; Check for wild cards
	BEQ	111$			; NE - then error
	DIAG	WILCRD			; No wildcards allowed
	JMP	AGAIN

111$:	MOV	CSIBLK+C.DSDS,-(SP)	; Sum length of filename
	ADD	CSIBLK+C.DSDS+4,(SP)	; ...
	ADD	CSIBLK+C.DSDS+10,(SP)+	; Is the length non-zero?
	BNE	112$			; J if so.
	CLRB	LSTSPC			; Show no listing file.
	BR	82$			; And continue.
112$:	OPEN$W	#LSTBLK,,#CSIBLK+C.DSDS ; Open listing file
	BCC	82$			; CC - Then opened ok
81$:	MOV	R0,-(SP)		; Else push addr(fdb)
	DIAG	MGOPER,FCS		; And tell user about it
	JMP	AGAIN			; And re-prompt

;	Check for and parse Table of Contents File Spec

82$:	BITB	#CS.MOR,CSIBLK+C.STAT	; Was a TOC specified ?
	BNE	83$			; Ne - then more specified
	.IF DF	I$$AS ! R$$11D		; If on IAS ...
	CLRB	TOCSPC			; Say we don't have a TOC file.
	BR	86$			; And leave.
	.IFF				; Otherwise ...
	MOV	#"NL,TOCFIL+N.DVNM	; Set default device
	OPEN$W	#TOCBLK			; Open TOC file
	BCC	85$			; CC - then TOC open
	BR	81$			; Not open complain
	.ENDC	;I$$AS ! R$$11D
83$:	CSI$2	#CSIBLK,OUTPUT,#TOCSW	; Parse TOC filespecs
	BCS	70$			; CS - then TOC error
	BITB	#CS.WLD,C.STAT(R0)	; Wild cards aren't allowed either
	BEQ	84$			; EQ - then OK
	DIAG	WILCRD			; No wildcards allowed
	JMP	AGAIN

84$:	OPEN$W	#TOCBLK,,#CSIBLK+C.DSDS ; Open TOC file
	BCS	81$			; CS - then file open failure
85$:	INCB	TOCSPC			; Say we've got a TOC file opened
86$:

	.SBTTL	INITIALIZE PAGE AND POINTERS

100$:	BIT	#TF.MOR,FLAGS	; more files ?
	BEQ	102$		; EQ - no
	BIT	#TF.PIN,FLAGS	; already initialized ?
	BNE	140$		; NE - yes, skip init
	BIS	#TF.PIN,FLAGS	; indicate initialized

102$:	MOV	PRMRG,	RMARG	; set right margin
	TST	PNLPG		; see if /ps::n given
	BNE	105$
	MOV	#INLPG,	PNLPG	; reinitialize
105$:	MOV	PNLPG,	NLPG	; and page length
	.IF	DF	A$$RAP

;	Initialize margins and half-spacing according to 10/12 pitch

	CMPNE	#10.,PITCH,106$	; Not 10 pitch
	MOV	#HS10,HSDEF	; Set 10 pitch half-spacing
	MOV	#ILM10,ILMRG	; Set initial 10 pitch left margin
	MOV	#IRM10,IRMRG	; Set initial 10 pitch right margin
	BR	108$		; Go init margins etc.
106$:
	CMPEQ	#12.,PITCH,107$	; 12 pitch ?
	DIAG	INVPIT		; Invalid pitch
	JMP	RUNOFF		; Continue after message
;
107$:
	MOV	#HS12,HSDEF	; Set 12 pitch half-spacing
	MOV	#ILM12,ILMRG	; Set 12 pitch left margin
	MOV	#IRM12,IRMRG	; Set 12 pitch right margin
108$:
	MOV	HSDEF,HSPACE	; Set alternating half-space template
	MOV	ILMRG,LMARG	; Left margin
	MOV	ILMRG,PLMARG	; Set permanent left margin
	MOV	IRMRG,RMARG	; Right margin
	MOV	IRMRG,PRMRG	; Permanent right margin ?
	MOV	IRMRG,PRMARG	; Permanent right margin
	SUB	$NUMLW,NLPG	; Adjust if numbering pages lower
	.ENDC
	CMPNEB	#'B,ULSWT,110$	; not backspace mode?
	COMB	$ULMSW		; set backspace mode
	BR	130$
110$:	CMPNEB	#'S,ULSWT,120$	; not simulate mode?
	COMB	$ULSSW		; set simulate switch
	BR	130$
120$:	CMPEQB	#'L,ULSWT,130$	; line mode?
	COMB	$ULNSW		; set no underline switch

130$:
	CALL	LINSET		; set up for line	
	.IF	NDF	A$$RAP
	CALL	FFEED		; start at top of new form
	.IFF
	MOV	LOWCHP,-(SP)	; Save low/high chapters and pages
	MOV	HGHCHP,-(SP)	;
	MOV	LOWPAG,-(SP)	;
	MOV	HGHPAG,-(SP)	;
	CLR	LOWCHP		; Re-init low-high
	MOV	#-1,HGHCHP	;
	CLR	LOWPAG		;
	MOV	#-1,HGHCHP	;
	CALL	FFEED		; Force the form feed and restore
	CALL	FLUSH		; Flush the buffer
	MOV	(SP)+,HGHPAG	; 	LOW/HIGH chapters and pages
	MOV	(SP)+,LOWPAG	;
	MOV	(SP)+,HGHCHP	;
	MOV	(SP)+,LOWCHP	;
	.ENDC
	CALL	OPRWAT		; wait for operator if requested to
	CLRB	$SDISW		; Clear sec. buffer flag
140$:	JMP	LGO		; and into main loop, line input

	.SBTTL	INPUT FILE PARSING AND OPEN

;	Now check the input file specs. Multiple input files
;	are handled as concatenated files. That is, they are a
;	continuation of the previous file.

PARFIL:
	BITB	#CS.EQU,CSIBLK+C.STAT	; Input or output ?
	BNE	42$			; NE - then use INPUT
	CSI$2	#CSIBLK,OUTPUT,#INSW	; Parse input filespecs
	BCS	40$			; CS - then syntax error
	BR	50$			; And go open the file

42$:	CSI$2	#CSIBLK,INPUT,#INSW	; Parse input filespecs
	BCC	50$			; CC - then no errors
40$:	DIAG	MGIFER			; INPUT FILE SPEC ERROR
	JMP	RUNOFF			; And go restart
50$:	BITEQB	#CS.WLD,C.STAT(R0),51$	; Wild card in input ?
	DIAG	WILCRD			; No wildcards allowed
	JMP	AGAIN

51$:	BIC	#TF.MOR,FLAGS		; Clear local flags
	BITB	#CS.MOR,CSIBLK+C.STAT	; More input files there ?
	BEQ	52$			; EQ - no
	BIS	#TF.MOR,FLAGS		; Set more input flag
52$:	CLOSE$	#TTLDMY			; close input file.
	FDBF$R	R0,,,#2,#FD.RAH		; Say we want read-ahead
	.IF	NDF	A$$RAP
	OPNS$R	R0,,#CSIBLK+C.DSDS	; Open source file
	.IFF
	OPEN$R	R0,,#CSIBLK+C.DSDS	; Open source file
	.ENDC
	BCC	60$			; CC - then file exists
	MOV	R0,-(SP)		; push for error call
	DIAG	MGIPER,FCS
	JMP	RUNOFF			; and loop.
60$:	RETURN				; and return to the caller.

	.SBTTL	ENDFILE PROCESSING

NXTFIL::CALL	POPFIL			; try to pop off a file.
	BCC	20$			; leave if ok
	BIT	#TF.MOR,FLAGS		; more input files available?
	BEQ	10$			; no
	CALL	PARFIL			; yes - parse file spec.
	CLC				; show success
	BR	20$			; and return.
10$:	SEC				; show error return
20$:	RETURN				; and return to the caller.

ENDFIL:	CALL	OUTPUT			; flush last line
	CALL	TOTPUT			; flush toc file
	.IF	DF	A$$RAP
	DEC	NFFCH
	MOV	NFFCH,PAGCNT		; page count = # of FF characters - 1
					; Move to PAGCNT for termination message
	.ENDC
	DIAG	TPAGES			; output total pages
	CALL	SPLFIL			; Check to see if we should spool file
	JMP	RUNOFF			; go restart

	.SBTTL	Read command input

;	This module will read a command line into the buffer specified
;	in R0 for length R1. C-bit will be set on error and R3 will
;	contain the error. On return, R0 will point to the command
;	buffer and R2 will contain the command length.

REACMD:	MOV	R0,-(SP)		; preserve buffer pointer
	MOV	R0,TTIBLK+G.CMLD+2 	; Point to input buffer
	MOV	R1,TTIBLK+G.CMLD 	; Set buffer length
	CLR	R2			; Clear accumulator
	BISB	#GE.CON,TTIBLK+G.MODE	; Set continuation lines on
	BICB	#GE.CLO,TTIBLK+G.MODE	; Don't close input file

LOOP:	GCML$	#TTIBLK			; Prompt for input.
	BCC	10$			; CC - then ok
	MOV	TTIBLK+G.CMLD,R2	; Set line length
	CLR	R3			; Leave the error
	MOVB	TTIBLK+G.ERR,R3  	; in R3
	SEC
	MOV	(SP)+,R0		; Restore R0
	RETURN

10$:	MOV	TTIBLK+G.CMLD,R2   	; Accumulate length
	BEQ	LOOP			; EQ - Then nothing here yet
	MOV	(SP)+,R0		; Restore pointer
	MOV	TTIBLK+G.CMLD+2,R1 	; get gcml pointer
	CLC
	RETURN				; Return to caller


	.SBTTL	The end

	.END
