;***************************************************************************
; SYSMSG.M68	-	Displays contenst of SYSMSG, optional file output
;
; Initial algorithm by Steve Lawson of Alpha Micro
;
; Modified for file option and other stuff by Dave Heyliger - AMUS Staff
;***************************************************************************

	SEARCH	SYS			;search the normals
	SEARCH	SYSSYM
	SEARCH	TRM
	SEARCH	AAA			;from DSK2:[100,133]

DEFINE	DIM=PRTTAB -1,11.
DEFINE	BRIGHT=PRTTAB -1,12.

	PHDR	-1,0,PH$REE!PH$REU	;define program header

	.OFINI				;define variables
	.OFDEF	BUFFER,80.		;one called BUFFER
	.OFDEF	FILE,D.DDB		;one called file	
	.OFSIZ	IMPSIZ

	GETIMP	IMPSIZ,A3		;A3 will always point to BUFFER

	;Ask user if they would like a screen or file display
	;
	DIM
	TYPE	<Output >
	BRIGHT	
	TYPE	<SYSMSG.USA >
	DIM
	TYPE	<to a >
	BRIGHT	
	TYPE	<F>
	DIM
	TYPE	<ile or >
	BRIGHT
	TYPE	<S>
	DIM
	TYPE	<creen? >
	BRIGHT
	KBD
	CTRLC	DONE			;quit on ^C
	CMPB	@A2,#'F			;look for File
	BNE	SYSMSG			;not file option
	MOV	#123,D3			;"flag" set to "file option"
	CALL	FILINI			;and initialize file

	;Setup initial loop, initialize
	;
SYSMSG:	CLR	D1			;clear message counter
LOOP:	CTRLC	DONE			;quit on ^C
	INC	D1			;increment message counter
	CMP	D1,#260.		;all messages typed?
	JEQ	DONE			;yup

	;If file option, output message to file, else output message to screen
	;
	CMP	D3,#123			;File option selected?
	BNE	10$			;nope, screen
	CALL	FILOPT			;yup, output message to file
	BR	LOOP			;and loop
10$:	CALL	TYPEEM			;else type to screen
	BR	LOOP			;and loop



	;SUBROUTINE to type out each message of SYSMSG.USA to screen
	;
TYPEEM:	TYPE	<Msg >			;start message
	DCVT	3,OT$TRM!OT$ZER		;output message number
	TYPE	<: >			;and fancy ":"
	LEA	A2,BUFFER(A3)		;point A2 to BUFFER variable
	PUSH	A2			;save this pointer on stack
	MOV	A2,A1			;copy pointer to A1
	SMSG	D1,OT$MEM		;"type" message into BUFFER
	CLRB	@A2			;end message with null

	;After message in A2, parse message to get rid of "_Message #.."
	;
10$:	CMPB	@A1,#0			;at end of message?	
	BEQ	20$			;yup, type it out to the screen	
	CMPB	(A1)+,#40		;nope, look for "_Message.."
	BNE	10$			;didn't even find the "_"
	CMPB	@A1,#'M			;found "_", how about "_M"
	BNE	10$			;nope
	CLRB	@A1			;yup, end the message right here

	;Message parsed and ready to be displayed
	;
20$:	POP	A2			;restore pointer to start of BUFFER
	TTYL	@A2			;type message out
	CRLF				;nice fancy return
	RTN				;done with message



	;SUBROUTINE to output one message from SYSMSG.USA to SYSMSG.LST
	;
FILOPT:	PUSH	D1			;save message counter
	CLR	D1			;clear character register
	MOVB	#'M,D1			;get "Msg_" into file
	FILOTB	@A4
	MOVB	#'s,D1
	FILOTB	@A4
	MOVB	#'g,D1
	FILOTB	@A4
	MOVB	#40,D1
	FILOTB	@A4
	MOV	@SP,D1			;get message number
	MOV	A4,A2			;point A2 to DDB	
	DCVT	3,OT$DDB!OT$ZER		;output message number to file
	MOVB	#':,D1
	FILOTB	@A4			;"Msg #:" created

	;get message into BUFFER varaible
	;
	LEA	A2,BUFFER(A3)		;point A2 to BUFFER variable
	PUSH	A2			;save this pointer on stack
	MOV	A2,A1			;copy pointer to A1
	MOV	4(SP),D1		;get the message number into D1
	SMSG	D1,OT$MEM		;"type" message into BUFFER
	CLRB	@A2			;end message with null

	;After message in A2, parse message to get rid of "_Message #.."
	;
10$:	CMPB	@A1,#0			;at end of message?	
	BEQ	20$			;yup, move message to file
	CMPB	(A1)+,#40		;nope, look for "_Message.."
	BNE	10$			;didn't even find the "_"
	CMPB	@A1,#'M			;found "_", how about "_M"
	BNE	10$			;nope
	CLRB	@A1			;yup, end the message right here

	;Message parsed and ready to be displayed
	;
20$:	POP	A2			;restore pointer to start of BUFFER
30$:	MOVB	(A2)+,D1		;get character into D1
	CMPB	D1,#0			;end of message?
	BEQ	40$			;yup, output a RETURN
	FILOTB	@A4			;nope, output message character
	BR	30$

	;Message complete, output a CRLF
	;
40$:	MOVB	#15,D1			;here is the CR
	FILOTB	@A4
	MOVB	#12,D1			;and here is the LF
	FILOTB	@A4
	POP	D1			;restore message number
	TYPE	<.>			;one dot for each message to screen
	RTN				;done with message




	;SUBROUTINE to initialize SYSMSG.LST file. Called only once
	;
FILINI:	LEA	A4,FILE(A3)		;point to FILE with A4
	LEA	A2,SYSFIL		;point A2 to "input" filename
	FSPEC	@A4			;fill in DDB with filespec
	INIT	@A4			;initialize DDB
	LOOKUP	@A4			;there?
	BNE	10$			;nope, all ok
	DSKDEL	@A4			;yup, get rid of old file
10$:	OPENO	@A4			;and open file for output
	RTN				;done with FILINI

SYSFIL:	ASCII	/SYSMSG.LST/
	BYTE	15,12,0
	EVEN



	;Everyone comes here when the work is done
	;
DONE:	CMP	D3,#123			;files open?
	BNE	10$			;nope
	CLOSE	FILE(A3)		;yup, close the file
	TYPECR	<!>			;final screen output
10$:	EXIT				;bye-bye

	END