	.title	exit	Exit from C programs
	.ident	"V02.00"

;+
; Index		Exit from C programs
;
; Usage
;
;	exit();			/* Exit, no return */
;	exits(status);		/* Exit with status */
;	$$fail();		/* Immediate exit */
;
;	extern int $$exst;	/* Exit status value */
;
;   Internal
;
;	jmp	$$exit		;Call exit routine
;
; Description
;
;	On exit, the following sequence occurs:
;	  1. The internal output buffer is flushed.
;	  2. The user-supplied wrapup() function is called.
;	  3. The program exits to the operating system.
;
;	By calling exits() with an appropriate value, the program can
;	pass an exit status code to the operating system monitor.
;
;		RSX	Meaning
;		 1.	Success -- no message is printed
;		 0.	Warning
;		 2.	Error
;		 4.	Severe error
;
;	Calling exit() is equivalent to calling exits(1). Note that
;	the program may also set a value into $$exst and exit via
;	exit():
;
;		extern int $$exst;
;
;		...
;
;		$$exst = 4;
;		exit();
;
;	If the program calls, or jumps to $$fail, it will immediately
;	exit to the operating system without calling wrapup().
;-
; Edit History
; V02.00 19-Oct-82 TTC		Rework of CS library
;
	.mcall	exit$s, exst$s

	.psect	c$data
$$exst::
	.word	1		; Presuppose "normal" exit
exitfl:	.byte	-2		; -1 during windup(), 0 during library exit
	.even

	.psect	c$code

exits::
	mov	2(sp),$$exst	; Set exit code and fall through

exit::
$$exit::
	call	flush		; Flush output buffer
	incb	exitfl		; Are we exiting?
	beq	$$fail		; Br if we come here twice.
	call	wrapup		; User wrapup routine

$$fail::			; Very hard exit
	EXST$S	$$exst		; Return status code
	EXIT$S			; Back to RSX (EXST$ not emulated)
	.end
