
.IIF	NDF	RSX	RSX	=	1	;Assume RSX	;01+
.IIF	NDF	VIRJOB	VIRJOB	=	0	;Assume not VJ	;k1+
.IF	NE	RSX						;k1
.IIF	NE	VIRJOB, .ERROR RSX; RSX & VIRJOB both set	;k1
.ENDC								;k1-
	.TITLE	CC000	Parse command line
	.ident	/X01.27/

	.NLIST	BEX, CND
	.ENABL	LC, GBL
	.LIST	MEB
;;;	.LIST	CND		;Debugging only

;
; C COMPILER
; PARSE COMMAND LINE
;
; VERSION X01
;
; DAVID G. CONROY	13-JAN-78
;
; Edit history
; 01  8-MAY-79 MM	More elaborate command line scan for VAX
; 02 17-JUL-79 MM	Clean up command line scan for RSTS/E
; 03 17-JUL-79 MM	Made Unix mode a run-time flag
; 04 30-JUL-79 MM	Added RT11 support
; 05 10-SEP-79 MM	.src and .int files are now .tmp and .tm1
; 06 11-SEP-79 MM	Accept lowercase switches in rt11
; 07 04-Mar-80 MM	For the New, improved C
; 08 04-Mar-80 MM	Only generate UNIX calling sequence
; 09 03-Jun-80 MM	Added -e and -f for eis and fpu generation
; 10 03-Jun-80 MM	Fixed bug in creating output file version numbers
; 11 09-Jun-80 MM	Redid RSX output file name creation, switches
; 12 26-Jun-80 MM	Redid RSX output file name creation again
; 13 08-Jul-80 MM/RBD	Typo in .fetch, also CTRL/Z exits xcc
; 14 23-Dec-80 RBD	Catch null command line as an error
; 15 28-DEC-80 RBD	Add '-a' flag to chain to AS assembler
; 16 31-Dec-80 RBD	Add exit with status on RSX ^Z. Remove useless
;			conditional code for RT-11 in RSX code. Exit with
;			'severe' status on RT-11 if error in
;			opening files. Fix junk 'no device' msg
;			from monitor if input file fails to open.
; 17 23-Feb-81 MM	Added -n switch to disable inline EIS
; 18 26-Feb-81 MM	Added #define date to save current date
; 19 29-May-81 RBD	Catch null command lines on rsx
; 20 01-Jun-81 MM	VMS V2.3 bug fix -- warning, disabled
; 21 30-Jul-81 MM	Null-trail command lines (eats edit 19)
; 22 19-Oct-81 MM	Redid RT file open to gain disk space
; u1 17-Sep-81 CCG	Added compile-time switch to elimnate pre-processor
; 23 08-Feb-82 MM	Incorporated u1 changes as a run-time switch.
;			Switch changes: -? for debug, -X for timings,
;			-D for double floating, -F for single floating.
;			C$$FLT selects floating point default.
;			Also, force .FETCH of "DK:" handler in RT11
; 24 01-Mar-82 RBD	Make -v default, '-b' stands for brief (opposite
;			of -v), '-z' now debug, '-h' gives help on all
;			of these switches (whew!).
; 25 22-Mar-82 MM	Removed -s and -i switches.  Now done by -x
; 26 23-Mar-82 MM	Added -w to supress "no reference" warnings
; 27 15-Apr-82 MM	Added -r to #define rsts
; k1 19-Feb-84 KHS	Add RT-11 XM Virtual Job Support. This is the only
;			module affected. Add new conditional VIRJOB = 1 to
;			RT11.MAC to enable the feature; Assumed off. Forces
;			allocated memory into extended memory; .FETCHes still
;			go to low memory - there may be little room here due
;			to mapping policy. Safest to LOAD all handlers, costs
;			little in XM environment.
;
	.GLOBL	CC000
.IF NE	RSX							;04
	.GLOBL	CSI
	.GLOBL	DFNB1
	.GLOBL	DFNB2
.IFF								;04
	.GLOBL	RTAREA
	.GLOBL	CSIBLK						;04+
	.GLOBL	DEFEXT
	.GLOBL	CFDB		;.C	file descriptor
	.GLOBL	SFDB		;.TMP	file descriptor
	.GLOBL	IFDB		;.TM1	file descriptor
	.GLOBL	OFDB		;.S	file descriptor
	.GLOBL	FDB$CH		;Channel number in FDB
	.GLOBL	FDB$NM		;Name pointer in FDB

.MACRO	CALL	ARG1,ARG2
.IF B	ARG2
	  JSR	PC,ARG1
.IFF
	  JSR	ARG1,ARG2
.ENDC
.ENDM	CALL

.MACRO	CALLR	ARG1
	  JMP	ARG1
.ENDM

.MACRO	RETURN	ARG1
.IF B	ARG1
	  RTS	PC
.IFF
	  RTS	ARG1
.ENDC
.ENDM	RETURN							;04-
.ENDC								;04
.IF NE	RSX							;04
	.MCALL	CALL
	.MCALL	CALLR
	.MCALL	RETURN
	.MCALL	FDOF$L						;12
	.MCALL	NBOF$L						;12
	.MCALL	FDAT$R
	.MCALL	FDOP$R
	.MCALL	FINIT$
	.MCALL	CSI$
	.MCALL	CSI$1
	.MCALL	CSI$2
	.MCALL	OPEN$A
	.MCALL	OPEN$W
	.MCALL	OPEN$R
	.MCALL	OFNB$W
	.MCALL	CLOSE$
	.MCALL	NMBLK$
	.MCALL	DIR$
	.MCALL	WTSE$S
	.MCALL	GMCR$
	.MCALL	QIO$
	.MCALL	EXST$S						;16

	CSI$
	FDOF$L
	NBOF$L
.IFF								;04 +
	.MCALL	.GTLIN, .CSISPC, .PRINT, .FETCH, .CLOSE
	.MCALL	.LOOKUP, .WAIT, .DELETE, .SETTOP		;22
.ENDC								;04 -
;
; EQUIVALENCES
;

BLANK	=	40		;ASCII BLANK
CR	=	15		;ASCII CARRIAGE RETURN
ALT	=	33		;ASCII ALTMODE
DOL	=	44		;ASCII DOLLAR SIGN

.IF EQ	RSX							;k1 +
.IF NE VIRJOB							;k1
	.ASECT			; Set Virtual Job bit		;k1
	.=44			;   in the RT-11		;k1
	.WORD	2000		;   Job Status Word		;k1
.ENDC								;k1
.ENDC								;k1 -

;
; LOCAL DATA
;

	.PSECT	LD000,OVR,GBL
.IF NE	VIRJOB							;k1 +
VJLIM:	.LIMIT							;k1
NFA	=	VJLIM + 2 	;XM Virtual Job Next Free Adr	;k1
.ENDC								;k1 -
.IF NE	RSX							;04
SFILE:	.WORD	0		;PTR TO SOURCE FILE
APPEND:	.WORD	0		;FLAG FOR '>>'
EFILE:	.WORD	TI		;PTR TO ERROR FILE
IOS:	.BLKW	2		;IO STATUS BLOCK

SRC:	.RAD50	/TMP/		;FILETYPES			;05
INT:	.RAD50	/TM1/		;				;05
ASM:	.RAD50	/S  /		;

CSI:	.BLKB	C.SIZE		;CSI WORK BLOCK
DFNB1:	NMBLK$	,,,SY		;DEFAULT FILENAME BLOCKS
DFNB2:	NMBLK$	,C,,SY		;
GMCR:	GMCR$			;GET MCR COMMAND LINE BLOCK
GMCRP:	QIO$	IO.WVB,1,1,,,,<PRM,4,44> ;PROMPT QIO DPB
GMCRR:	QIO$	IO.RVB,1,1,,IOS,,<GMCR+G.MCRB,79.> ;READ LINE

;
; ASCII DATA.
;

TI:	.ASCIZ	'TI:'
PRM:	.ASCIZ	'CC>'
MSG01:	.ASCII	'?CC-F-Usage: cc [-abdefhilmnpstvwxz] file'<15><12>
							;/07/08/15/24/25/26
	.ASCIZ	'             cc -h  gives help on switches'
MSG02:	.ASCIZ	'?CC-F-Cannot open error file'
MSG03:	.ASCIZ	'?CC-F-Cannot open input file'
MSG04:	.ASCIZ	'?CC-F-Cannot open intermediate file'
MSG05:	.ASCIZ	'?CC-F-Cannot open output file'
MSG06:	.ASCIZ	'%CC-W-Unknown switch "?" - Ignored'
SWBYTE	=	MSG06+22.
.IFF								;04 +
;
; Parameter blocks for output open routines
;
SPMTR:	.WORD	SBLK
	.WORD	SFLAG
	.RAD50	/TMP/						;05
	.WORD	SFDB+FDB$CH
	.WORD	MSG07

DKDEV:	.RAD50	/DK /						;23
PRM:	.ASCII	"CC> "
	.BYTE	200
MSG01:	.ASCII	'?CC-F-Usage: CC out=in/a/b/d/e/f/h/l/m/n/p/t/v/w/x/z'<15><12>
	.ASCIZ	'             CC /h  gives help on switches'	;24/25/26
MSG02:	.ASCIZ	'?CC-F-Handler fetch failure'			;16
MSG03:	.ASCIZ	'?CC-F-Cannot open input file'
;MSG04:	.ASCIZ	'?CC-F-Cannot open intermediate file'		;22
;MSG05:	.ASCIZ	'?CC-F-Cannot open output file'			;22
MSG06:	.ASCIZ	'%CC-W-Unknown switch "?" - ignored'
SWBYTE	=	MSG06+22.		;Stuff switch here
MSG07:	.ASCIZ	'?CC: Cannot open expanded source file'
;MSG08:	.ASCIZ	'?CC: Fatal error flushing expanded source file';22
.ENDC								;04 -
	.EVEN
SWTAB:					;Switch table
	.BYTE	'A,1			;Chain to AS		;15
	.WORD	AFLAG						;15
	.BYTE	'B,0			;Brief errors		;24
	.WORD	VFLAG
	.BYTE	'D,0			;Double fpu		;07/23
	.WORD	FFLAG			;			;07/23
	.BYTE	'E,1			;Inline EIS		;09+
	.WORD	EFLAG
	.BYTE	'F,1			;Inline FPU
	.WORD	FFLAG						;09-
	.BYTE	'H,1			;Help			;24
	.WORD	HELPFL						;24
	.BYTE	'L,1
	.WORD	LFLAG						;07
	.BYTE	'M,1			;Skip preprocessor	;07/23
	.WORD	MFLAG						;07/23
	.BYTE	'N,0			;No EIS			;17
	.WORD	EFLAG						;17
	.BYTE	'P,1
	.WORD	PFLAG
	.BYTE	'R,1			; #define rsts		;27
	.WORD	RFLAG			;			;27
	.BYTE	'T,1
	.WORD	TFLAG
	.BYTE	'V,1
	.WORD	VFLAG
	.BYTE	'W,0			; No warnings		;23/26
	.WORD	WFLAG						;26
	.BYTE	'X,1			; Meter, keep temp's	;26
	.WORD	XFLAG						;23
	.BYTE	'Z,1			;Debug			;24
	.WORD	DEBUGF						;24
	.WORD	0

	.PSECT	CC000						;04
.IF NE	RSX							;04 +
.SBTTL	CC000	Parse command line (RSX)
;+
; ** CC000 - PARSE COMMAND LINE
;
; THIS ROUTINE IS CALLED BY THE ROOT. IT READS THE MCR COMMAND LINE
; (PERHAPS GETTING IT FROM THE USER TI:, IF THE GMCR$ FAILS).  THEN
; IT PARSES IT INTO ARGUMENTS, SETTING THE OPTION FLAGS.
;
; THE ERROR STREAM IS OPENED ON THE EFDB (RSX ONLY).  A TEMP FILE IS
; OPENED FOR WRITING ON P0OUT, THE USER'S SOURCE IS OPENED ON P0IN, AND
; THE PREPROCESSOR (CCPREP) IS CALLED.
;
; THE USER'S SOURCE FILE IS THEN CLOSED. (IN RSX), P1OUT AND P2OUT WHICH
; USE THE SAME FDB'S AS P0IN AND P0TMP, ARE OPENED UP AS THE INTERMEDIATE
; CODE AND  OUTPUT FILES. CONTROL RETURNS TO THE ROOT.
; THE CORRESPONDANCE BETWEEN FILE DATA BLOCKS AND FILE DESIGNATORS
; IS DEFINED IN MODULE CC0RT.  IT IS SLIGHTLY DIFFERENT FOR RSX AND RT11
; AS RT11 OPENS ALL FILES AT THE SAME TIME.
;
; FILE	RSX FDB	  RT11 DEV	USED FOR
; P0IN	AFDB	  CFDB   3	ORIGINAL .C SOURCE FILE
; P0OUT	SFDB	  SFDB   2	.C SOURCE WITH INCLUDES, ETC.
; P0TMP	IFDB	  INCFDB 4	#INCLUDE PROCESSING
; P1IN	SFDB	  SFDB	 2	(SAME AS P0OUT)
; P1OUT	IFDB	  IFDB	 1	INTERMEDIATE CODE OUTPUT BY PARSER
; P2IN	IFDB	  IFDB   2	(SAME AS P1OUT)
; P2OUT	AFDB	  OFDB	 0	.S OUTPUT FROM COMPILER
;
; WHILE THIS IS ADMITTEDLY CONFUSING, IT SOMEWHAT SIMPLIFIES FILE
; NAMING WITHIN THE COMPILER PROPER.  NOTE THAT RT11 DOES NOT RE-USE
; FILE DATA BLOCKS, WHILE RSX DOES.  TANSTAAFL.
;-

CC000:
	FINIT$			;INITIALISE THE FSR
	CALL	DODATE		;SETUP TODAY'S DATE		;18

	CLR	R3		;ASSUME IT'S A COMMAND		;02
	DIR$	#GMCR		;GET MCR LINE
.if eq 0
	BCC	20$		;YEP
	BCS	10$		;NOPE
.iff
	BCS	10$		;NOPE				;20+
;
; In VMS V2.3, if the compiler is installed as a foreign command
; and the command line begins with "-" (likely), the last byte
; of the command is tacked onto the front of the command line.
; This code untacks the line.  Otherwise, the first byte of the
; command will be ' ' <space>.
;
	MOV	#GMCR+G.MCRB,R0	;R0 -> first command byte
	CMPB	(R0),#'A	;Is the first byte alpha?
	BLO	20$		;No problem
	CMPB	(R0)+,#'Z	;Check the other end,
	BHI	20$		;too.
	CMPB	(R0),#'-	;Yes, is it "X-FOO"?
	BNE	20$		;No, continue
	MOVB	#' ,-(R0)	;Yes, overwrite the first byte
	BR	20$		;Kampen gaar vidare		;20-
.endc
10$:	DIR$	#GMCRP		;PROMPT FOR THE LINE
	WTSE$S	#1		;
	DIR$	#GMCRR		;READ IT BACK
	WTSE$S	#1		;
	CMPB	IOS,#IE.EOF	;END OF FILE?			;13+
	BNE	15$		;NO, DO IT
	EXST$S	#EX$SUC		;YES, NORMAL EXIT		;16

15$:								;13-
	CMP	IOS,#IS.CR	;WAS THE LINE GOOD
	BNE	10$		;NO
	INC	R3		;YES, NOT A COMMAND, THOUGH	;02+
	MOV	IOS+2,R0	;ANY BYTES IN THE COMMAND LINE?	;21+
	BEQ	140$		;GIVE USAGE MESSAGE IF NOT
	CLRB	GMCR+G.MCRB(R0)	;MAKE SURE IT NULL TRAILS	;21-

20$:	MOV	#GMCR+G.MCRB,R0	;POINT TO THE LINE
	MOV	R0,R1		;GET ANOTHER COPY
22$:	MOVB	(R1)+,R2	;LOOP ON EACH BYTE
	BEQ	24$		;AT NULL END
	CMPB	R2,#CR		;NO, IS IT RETURN?
	BEQ	24$		;EXIT IF SO
	CMPB	R2,#ALT		;IF NOT ALTMODE,
	BNE	22$		;LOOK AGAIN
24$:	CLRB	-(R1)		;FORCE NULL TRAILER
	TST	R3		;OK, IS IT FROM GCMR$?		;02
	BNE	40$		;NO, FROM STANDARD READ, GO ON	;02
30$:	MOVB	(R0)+,R1	;SKIP OVER COMMAND NAME
	BEQ	10$		;PROMPT ON TI: IF NONE FOUND	;02
	CMPB	R1,#BLANK	;
	BNE	30$		;KEEP LOOKING

40$:	MOVB	(R0)+,R1	;SKIP BLANKS BETWEEN ARGUMENTS
	BEQ	130$		;EXIT AT END			;02
	CMPB	R1,#BLANK	;AT A BLANK?
	BEQ	40$		;LOOP IF SO

	CMPB	R1,#'-		;OPTIONS
	BNE	60$		;NO
50$:	MOVB	(R0)+,R1	;OPTION CHARACTER
	BEQ	130$		;DONE				;02
	CMPB	R1,#BLANK	;
	BEQ	40$		;GO GET THE NEXT ARG.
	CALL	OPT		;PROCESS OPTIONS
	BR	50$		;

60$:	CLR	R2		;CLEAR QUOTES FLAG
	CMPB	R1,#'"		;CHECK FOR QUOTES
	BEQ	70$		;
	CMPB	R1,#''		;
	BNE	80$		;BR IF NOT A QUOTE
70$:	MOV	R1,R2		;SAVE QUOTE DELIMITER
	TST	SFILE		;DO WE HAVE A FILE
	BNE	140$		;YES, ERROR
	MOV	R0,SFILE	;SAVE THE FILE POINTER
	BR	90$		;GO COLLECT NAME

80$:	CMPB	R1,#'<		;INPUT REDIRECTION
	BEQ	90$		;IS DISCARDED
	CMPB	R1,#'>		;OUTPUT REDIRECTION
	BNE	85$		;NO
	CMPB	R1,(R0)		;IS THIS '>>'
	BNE	82$		;NO
	INC	APPEND		;SET APPENDING
	INC	R0		;SKIP OVER THE SECOND '>'
82$:	MOV	R0,EFILE	;SAVE ERROR FILE NAME
	BR	90$		;GO GET NAME

85$:	TST	SFILE		;DO WE HAVE A FILE
	BNE	140$		;YES, ERROR
	MOV	R0,SFILE	;SET POINTER
	DEC	SFILE		;

90$:	MOVB	(R0)+,R1	;COLLECT THE ARG.
	BEQ	110$		;DONE AT NULL			;02
	TST	R2		;QUOTED ARGUMENT
	BNE	100$		;BR IF YES
	CMPB	R1,#BLANK	;STOP ON BLANKS
	BEQ	110$		;DONE
	BR	90$		;KEEP GOING
100$:	CMP	R1,R2		;STOP ON MATCHING QUOTE
	BNE	90$		;KEEP GOING

110$:	CLRB	-1(R0)		;MAKE ARG ASCIZ
	TSTB	R1		;CHECK FOR END OF LINE		;02
	BNE	40$		;GO GET NEXT ARG
120$:	TST	R2		;CHECK FOR MISMATCHED QUOTES
	BNE	140$		;ERROR

;									;24+
; HELP PROCESSING. JUST GIVE HELP IF -H WAS GIVEN.
; OTHERWISE, FAIL IF NO FILES GIVEN.
;
130$:	TSTB	HELPFL
	BEQ	135$
	CALLR	HELP
135$:	TST	SFILE		;DID WE GET A FILE			;24-
	BNE	200$		;BR IF YES



;
; FATAL SCAN ERROR
;

140$:	MOV	#MSG01,R0	;COMMAND LINE BOTCH
	JMP	300$		;ERROR EXIT

;
; OPEN ERROR STREAM.
;

200$:	MOV	EFILE,R0	;FILE NAME
	CALL	DSPT		;BUILD THE DSPT
	BCS	220$		;URK

	MOV	#EFDB,R0	;GET PTR TO THE ERROR FDB
	FDAT$R	,#R.VAR,#FD.CR	;SET RECORD ATTRIBUTES
	FDOP$R	,,#CSI+C.DSDS	;DATASET DESCRIPTOR
	FDOP$R	,,,#DFNB1	;DEFAULT FILENAME BLOCK
	TST	APPEND		;APPENDING
	BEQ	210$		;NO
	OPEN$A			;TRY OPEN FOR APPEND
	BCC	230$		;WORKED
	CMPB	F.ERR(R0),#IE.NSF ;IS THE ERROR NO FILE
	BNE	220$		;NO, HARD ERROR
210$:	OPEN$W			;TRY OPEN FOR WRITE
	BCC	230$		;OK

220$:	MOV	#MSG02,R0	;CANNOT CREATE ERROR FILE
	BR	300$		;

;
; PREPROCESSING.
;

230$:	TSTB	XFLAG		;"Debugging"				;25+
	BEQ	232$		;Continue if not
	INCB	SFLAG		;Yes, keep source and
	INCB	IFLAG		;Intermediate files
232$:				;					;25-
	MOV	SFILE,R0	;GET SOURCE FILE NAME
	CALL	DSPT		;
	BCS	240$		;URK

	MOV	#AFDB,R0	;OPEN FILE -- assume preprocessor	;23+
	TSTB	MFLAG		;Need preprocessor?
	BEQ	235$		;Branch if so
	MOV	#SFDB,R0	;Read as if pass 0 has completed
235$:				;Main sequence				;23-
	FDOP$R	,,#CSI+C.DSDS	;DATASET DESCRIPTOR
	FDOP$R	,,,#DFNB2	;DEFAULT FILENAME BLOCK (.C)
	OPEN$R			;
	BCC	250$		;OK

240$:	MOV	#MSG03,R0	;CANNOT OPEN INPUT FILE
	BR	300$		;

250$:
	MOV	#AFDB+F.FNAM,R0	;R0 -> .C FILE NAME BLOCK	;12+
	TSTB	MFLAG		;Really need preprocessor?	;23+
	BEQ	255$		;Br if so
	MOV	#SFDB+F.FNAM,R0	;R0 -> correct file name block
255$:				;Main sequence			;23-
	MOV	#DFNB1+N.FNAM,R1
	MOV	(R0)+,(R1)+	;SHOVE FILE NAME INTO
	MOV	(R0)+,(R1)+	;THE DEFAULT NAME BLOCK
	MOV	(R0)+,(R1)	;
	TSTB	MFLAG		;Skipping over preprocessor?	;23
	BNE	280$		;Br around the preprocessing.	;23
	MOV	SRC,DFNB1+N.FTYP
	MOV	#SFDB,R0	;(OUTPUT) FILE NAME
	CALL	FNAME		;SETUP NAME FIELD		;11
	OFNB$W			;OPEN THE FILE
	BCC	270$		;OK

	MOV	#MSG04,R0	;CANNOT CREATE TEMP. FILE
	BR	300$		;

270$:
	CALL	CCPREP		;DO PREPROCESSING

	CLOSE$	#AFDB		;CLOSE USER'S SOURCE
280$:				;Here if preprocessor not needed	;23
;
; OPEN OUTPUT FILES.
;

	MOV	INT,DFNB1+N.FTYP				;12+
	MOV	#IFDB,R0	;OUTPUT NAME,
	CALL	FNAME		;DO IT				;12-
	OFNB$W			;
	BCC	290$		;

	MOV	#MSG04,R0	;CANNOT GET TEMP FILE
	BR	300$		;

290$:
	MOV	ASM,DFNB1+N.FTYP				;12+
	MOV	#AFDB,R0	;OUTPUT NAME, TOO
	FDOP$R	,,#0		;ERASE DATASET DESCRIPTOR
	CALL	FNAME		;DO IT				;12-
	OFNB$W			;
	BCC	310$		;

	MOV	#MSG05,R0	;CANNOT OPEN OUTPUT FILE
300$:	CALL	CCTTY		;WRITE ERROR TO TI:
	INC	NERRS		;MAKE THE COMPILER EXIT

310$:
	RETURN			;

;+
; ** FNAME -- SETUP OUTPUT FILE NAME FIELD			;11+
;
; LOAD THE FILE NAME USING THE MIGHTY .PARSE
;
;	MOV	#??FDB,R0
;	CALL	FNAME
;	OFNB$W
;
; USES R1-R3
;
;-

FNAME:
	MOV	R0,R1		;R1 ->
	ADD	#F.FNB,R1	;FILE NAME BLOCK
	CLR	R2		;NO DATASET DESCRIPTOR
	MOV	#DFNB1,R3	;DEFAULT FILE NAME BLOCK
	CALL	.PARSE		;OFF WE GO
	FDAT$R	,#R.VAR,#FD.CR	;VANILLA FILE
	RETURN			;AND EXIT			;11/12-

.ENDC								;04 -

.IF NE	RSX							;04
.SBTTL	OPT - Process command options
;+
; ** OPT - PROCESS COMMAND OPTIONS
;
;	-A		CHAIN TO AS ASSEMBLER, SET IT'S -D SW	;15
;       -B		BRIEF ERRORS				;24
;	-E		HARDWARE EIS				;09
;	-F		HARDWARE FPU				;09
;	-L		PRINT CODE TREES AS A COMMENT		;07
;	-N		DISABLE EIS				;17
;	-P		PROFILE
;	-R		DEFINE RSTS				;27
;	-T		GENERATE SYMBOL TABLES
;	-V		ECHO BAD SOURCE LINES
;	-X		METER -- PRINT PASS TIMINGS		;07
;	-Z		DEBUG (BPT AT PHASE ENTRANCE)		;24
;	-H		HELP					;24
;
; INPUTS:
;	R1=OPTION CHARACTER
;-

OPT:
	MOV	R0,-(SP)	;SAVE WORKING REGISTER		;11+
	BIC	#040,R1		;MAKE UPPER CASE
	MOV	#SWTAB,R0	;R0 -> SWITCH TABLE

10$:
	TSTB	(R0)		;IS THERE ANOTHER TABLE ENTRY?
	BEQ	30$		;BR IF NOT -- ERROR
	CMPB	R1,(R0)+	;HAVE WE FOUND IT?
	BEQ	20$		;BR IF SO
	ADD	#3,R0		;NO, SKIP TO THE NEXT
	BR	10$		;AND TRY AGAIN

20$:
	MOVB	(R0)+,R1	;FOUND IT, GET THE VALUE,
	MOVB	R1,@(R0)+	;UPDATE THE GLOBAL,
	BR	40$		;AND EXIT

30$:	MOVB	R1,SWBYTE	;ILLEGAL OPTION
	MOV	#MSG06,R0	;COMPLAIN
	CALL	CCTTY		;

40$:	MOV	(SP)+,R0	;RETURN
	RETURN			;				;11-
.ENDC								;04-

.IF NE	RSX							;04 +
.SBTTL	DSPT - Build a DSPT
;+
; ** DSPT - BUILD A DSPT
;
; GIVEN AN ASCII FILE NAME STRING, THIS ROUTINE USES THE MIGHTY CSI
; TO BUILD A DSPT IN THE CSI WORK BLOCK.
; WHY FCS REQUIRES A DSPT IS BEYOND ME.
;
; INPUTS:
;	R0=PTR TO FILENAME STRING
;
; USES:
;	R0
;-

DSPT:	MOV	R0,CSI+C.CMLD+2	;SET ADDRESS OF COMMAND LINE

10$:	TSTB	(R0)+		;FIND THE LENGTH
	BNE	10$		;
	DEC	R0		;
	SUB	CSI+C.CMLD+2,R0	;
	MOV	R0,CSI+C.CMLD	;AND PUT IN THE CSI BLOCK

	CSI$1	#CSI		;DO NASTY WORK
	BCS	20$		;
	CSI$2	,OUTPUT		;
20$:	RETURN			;DONE
.ENDC								;04

.IF EQ	RSX							;04 +
.SBTTL	CC000 Mainline (RT11)
CC000:
	CALL	DODATE		;Setup today's date		;18
	.GTLIN	#CMDLIN,#PRM	;Prompt the operator
	TSTB	CMDLIN		;First byte a null?		;14
	BEQ	5$		;If so, it's an error		;14
	.CSISPC	#CSIBLK,#DEFEXT,#CMDLIN ;and parse the files
	BCC	10$		;Branch if OK
5$:	MOV	#MSG01,R0	;Nope, say so			;14
	CALL	CCTTY		;to the terminal
	BR	CC000		;and try again.

10$:				;Now, do the switches
	MOV	(SP)+,R3	;R0 := number of switches
	BEQ	CKOPN		;Branch if none

20$:				;Do the next switch
	MOV	#SWTAB,R2	;R2 -> Switch table
	MOV	(SP)+,R1	;Get switch name and other junk
	BPL	30$		;Branch if no value
	TST	(SP)+		;Trash the value
30$:	BIC	#177640,R1	;R1 := switch ascii value	;06

40$:				;Look at a switch table entry
	TSTB	(R2)		;Is there another to scan?
	BEQ	60$		;Branch if no more to look at.
	CMPB	R1,(R2)+	;Have we come to the right place?
	BEQ	50$		;Branch if so
	ADD	#3,R2		;Nope, skip to the next switch entry
	BR	40$		;And try for it

50$:				;Found a switch
	MOVB	(R2)+,R1	;Get the value to set
	MOVB	R1,@(R2)+	;Set it here.
	BR	70$		;And go for the next one

60$:				;Here on an unknown switch
	MOVB	R1,SWBYTE	;Remember it's name
	MOV	#MSG06,R0	;Don't know this switch
	CALL	CCTTY		;And inform the cretin

70$:				;Done with this switch
	DEC	R3		;Is there another?
	BGT	20$		;Go for it, if so
;								;24+
; Help processing
;
	TSTB	XFLAG		;Debugging			;25+
	BEQ	72$		;Br if not
	INCB	SFLAG		;Yes, keep expanded source
	INCB	IFLAG		;And intermediate files
72$:
	TSTB	HELPFL
	BEQ	CKOPN
	CALLR	HELP						;24-

.ENDC								;04 -

.IF EQ	RSX							;04 +
.SBTTL	CKOPN	Check for the right flavor of open files
;+
; CKOPN
;
;	Check that the right (number of) files have been opened.
;	If the source or intermediate files were explicitly specified,
;	force the appropriate "don't delete" switch on.  Open the
;	files in any case.
;-

CKOPN:
	MOV	@#50,R1		;Get top of free memory
	TST	(R1)+		;Hicore + 2 is first free
;
; Fetch all handlers before setting "free memory"		;23+
;
	TST	SBLK		;Expanded source, too
	BEQ	3$		;Gotta .fetch
	TST	IBLK		;Check intermediate file, too
	BEQ	3$		;Gotta .fetch
	TST	OBLK		;Output file, too
	BNE	4$		;All were specified, continue
3$:
	.FETCH	R1,#DKDEV	;Force DK: handler resident
	BCS	30$		;Abort on error
	MOV	R0,R1		;Set top of memory
4$:				;Main sequence
;
	.FETCH	R1,#CBLK	;Get the handler for source	;13/22+
	BCS	30$		;Fetch failed, so abort
	MOV	R0,R1		;Save top of memory
	TST	SBLK		;was there a .TMP file?
	BEQ	10$		;don't fetch if not.
	.FETCH	R1,#SBLK	;Now for the expanded source
	BCS	30$		;Urk
	MOV	R0,R1		;Top of memory
10$:
	TST	IBLK		;was there a .TM1 file?
	BEQ	20$		;dont fetch if not.
	.FETCH	R1,#IBLK	;Get the intermediate file
	BCS	30$		;Urk
	MOV	R0,R1		;Save top of memory
20$:
	TST	OBLK		;Was there a .S file?
	BEQ	40$		;don't fetch if not
	.FETCH	R1,#OBLK	;Finally, the output (.S) file
	BCC	40$		;continue mainstream
30$:
	MOV	#MSG02,R0	;Fetch failed. (Said Martin, "Urk")
	BR	50$

40$:
	TSTB	MFLAG		;Skip preprocessor?			;23+
	BNE	43$		;Br if no preprocessing
	.LOOKUP	#RTAREA,CFDB+FDB$CH,#CBLK ;Find the file
	BCC	60$		;Branch if no error
	BR	46$		;Sorry
43$:
	.LOOKUP	#RTAREA,SFDB+FDB$CH,#CBLK ;Open .SRC channel with .C file
	BCC	65$		;Continue if ok
46$:				;Can't open source file			;23-
	MOV	#MSG03,R0	;Urk
50$:
	CALL	CCTTY		;Print the bad news
	INC	NERRS		;Make the compiler exit
	MOV	#8.,XSTAT	; with 'severe' status			;16
	RETURN			;Back to mainline handler

60$:
	MOV	#SPMTR,R1	;Now create the source file		;22-
	CALL	DOOPEN
65$:				;Here if MFLAG set			;23
	MOV	@#50,R1		;Get start of free memory
	ADD	#3,R1		;Round high-water mark up to
	BIC	#1,R1		;first free location in memory
.IF EQ	VIRJOB								;k1 +
	MOV	R1,FREMEM	;Save for free-space allocation.
.IFF									;k1
	MOV	NFA,FREMEM	;Save Next Free Address as free space	;k1
				;  bottom				;k1
.ENDC									;k1 -
	.SETTOP	#-2		;Get top of free memory
	MOV	R0,FRETOP	;And save for alloc.
	TSTB	MFLAG		;Actually preprocessing?		;23
	BNE	70$		;Actually, no.				;23
	CALL	CCPREP		;Do preprocessing
	.CLOSE	P0IN+FDB$CH	;All closed
70$:				;Main sequence				;23
	RETURN			;All done
.ENDC								;04 -

;
; ** DODATE
;
; Setup today's date
;
DODATE:
	CALL	ADATE		;Get date/time
	MOV	#DATBUF+1,R1	;And copy to "our" buffer
10$:	MOVB	(R0)+,(R1)+	;all of it
	BNE	10$		;
	MOVB	#'",-1(R1)	;Trailing quote for #define
	RETURN			;That's all

;
; HELP PROCESSING
;
	.PSECT	LD000
	.ENABL	LSB
10$:	.ASCIZ	"-a: Auto-spawn AS               -n: Disable inline EIS"
20$:	.ASCIZ	"-b: Brief error messages        -p: Compile profiling code"
30$:	.ASCIZ	"-d: Double floating default     -r: #define rsts"
40$:	.ASCIZ	"-e: Enable inline EIS           -t: Generate symbol tables"
50$:	.ASCIZ	"-f: Single floating default     -w: Suppress 'no use' warning"
60$:	.ASCIZ	"-h: Help - give this display    -x: Support: display phase"
70$:	.ASCIZ	"-l: Show code trees in .S file      timings; keep temp. files"
80$:	.ASCIZ	"-m: Bypass preprocessor         -z: DEBUG, trap phase entry"
	.EVEN
110$:	.WORD	10$,20$,30$,40$,50$,60$,70$,80$,0

	.PSECT	CC000

HELP:	MOV	#110$,R1	;R1 --> CHAR *TABLE[]
120$:	MOV	(R1)+,R0	;R0 --> LINE OF TEXT
	BEQ	130$		;(DONE)
	CALL	CCTTY		;DISPLAY IT
	BR	120$		;MORE

130$:	INC	NERRS		;FORCE THE COMPILER TO EXIT.
	RETURN

	.END

