	.title	perror	Print Library Error Message
	.ident	/000004/
;
;+
;
; Index		Print library error message
;
; Usage
;
;	perror(text)
;	char		*text;
;
; Description
;
;	An error message is written to stderr, using the
;	current i/o library error (stored in $$ferr).
;	Text is prepended to the message.
;
; Diagnostics
;
; Bugs
;
;-
;
; Edit history:
; 000001 22-Oct-81 MM	Written
; 000002 19-Jan-82 MM	Fixed up RT11 stuff
; 000003 19-Jan-82 MM	Really fixed it
; 000004 26-Mar-82 MM	Typo's in .psect's

.iif	ndf	rsx	rsx	=	1	;Assume RSX11M

$str$	=	0

	.list	meb
	.nlist	bex
.if ne	rsx
.macro	m	r, t, str
.if nb	r
$str$	=	$str$+1
	.irp	$,<\$str$>
	.psect	c$data						;02+
	.word	r,s'$
	.psect	c$strn						;04
s'$	=	.
	.endr
	.asciz	str
.endc
.endm	m
.iff
.macro	m	r, t, str
.if nb	t							;03
$str$	=	$str$+1
	.irp	$,<\$str$>
	.psect	c$data						;02+
	.word	t,s'$						;03
	.psect	c$strn						;04
s'$	=	.
	.endr
	.asciz	str
.endc
.endm	m
.endc

;
; Define the error message table
;
	.psect	c$data
errtable:
m	,	e$$ilf,	<"Illegal file name">
m	ie.bad,	,	<"File system error">
m	ie.bnm,	,	<"Illegal file name">
m	ie.dfu,	e$$nor,	<"No room for file">
m	ie.nsf,	e$$fnf,	<"File not found">
m	ie.bdv, e$$nod,	<"Bad device name">
m	ie.aln,	e$$ilu,	<"I/O channel in use">
m	ie.nln,	e$$noo,	<"I/O channel not open">
m	ie.eof, e$$eof,	<"End of file">
m	ie.fhe,	e$$fat,	<"Fatal system I/O error">
m	ie.rer,	e$$err,	<"Device error">
m	ie.fex,	e$$fnd,	<"File already present">
m	ie.ilu,	e$$noc,	<"Too many open files">
m	ie.lck,	,	<"File locked">
m	ie.nbf,	e$$nsp,	<"No memory space available">
m	ie.pri,	,	<"No access to file">
m	ie.rbg,	,	<"Illegal record size">
	.psect	c$data							;04
	.word	0, 0		;terminate buffer

;
; Format strings
;
	.psect	c$strn							;04
normal:
	.asciz	"%s: %s"<12>
unknown:
	.asciz	"%s: Unknown error %06o %d."<12>
noerror:
	.asciz	"%s: No current error"<12>
	.even

	.psect	c$code
perror::
	jsr	r5,csv$		;Link environments
	mov	$$ferr,(sp)	;Get error code
	bne	10$		;Continue if there is one
	mov	C$PMTR+0(r5),(sp)
	mov	#noerror,-(sp)
	br	print

10$:
	mov	#errtable,r4	;start at the table
20$:
	cmp	(sp),(r4)+	;is it ours?
	beq	30$		;yes, exit loop
	tst	(r4)+		;no, is it the end
	bne	20$		;continue if not.
;
; Unknown error
;
	mov	(sp),-(sp)	;duplicate $$ferr
	mov	C$PMTR+0(r5),-(sp)
	mov	#unknown,-(sp)
	br	print
;
; Gotcha
;
30$:
	mov	(r4),(sp)	;Here's the text
	mov	C$PMTR+0(r5),-(sp) ;And the argument
	mov	#normal,-(sp)	;And the format

print:
	mov	stderr,-(sp)	;Where to output text
	call	fprintf		;Put out message
	jmp	cret$		;and try for more
	.end
