1 static char Sccsid[] = "@(#)fatal.c	1.4	12/22/88";
2 
3 # include	"../hdr/macros.h"
4 # include	"../hdr/fatal.h"
5 #include <sys/syscall.h>
6 #define	syswrite(a,b,c)	syscall(SYS_write,a,b,c)
7 
8 /*
9 	General purpose error handler.
10 	Typically, low level subroutines which detect error conditions
11 	(an open or create routine, for example) return the
12 	value of calling fatal with an appropriate error message string.
13 	E.g.,	return(fatal("can't do it"));
14 	Higher level routines control the execution of fatal
15 	via the global word Fflags.
16 	The macros FSAVE and FRSTR in <fatal.h> can be used by higher
17 	level subroutines to save and restore the Fflags word.
18 
19 	The argument to fatal is a pointer to an error message string.
20 	The action of this routine is driven completely from
21 	the "Fflags" global word (see <fatal.h>).
22 	The following discusses the interpretation of the various bits
23 	of Fflags.
24 
25 	The FTLMSG bit controls the writing of the error
26 	message on file descriptor 2.  The message is preceded
27 	by the string "ERROR: ", unless the global character pointer
28 	"Ffile" is non-zero, in which case the message is preceded
29 	by the string "ERROR [<Ffile>]: ".  A newline is written
30 	after the user supplied message.
31 
32 	If the FTLCLN bit is on, clean_up is called with an
33 	argument of 0 (see clean.c).
34 
35 	If the FTLFUNC bit is on, the function pointed to by the global
36 	function pointer "Ffunc" is called with the user supplied
37 	error message pointer as argument.
38 	(This feature can be used to log error messages).
39 
40 	The FTLACT bits determine how fatal should return.
41 	If the FTLJMP bit is on longjmp(Fjmp) is
42 	called (Fjmp is a global vector of n words, see
43 	setjmp, longjmp documentation).
44 
45 	If the FTLEXIT bit is on the value of userexit(1) is
46 	passed as an argument to exit(II)
47 	(see userexit.c).
48 
49 	If none of the FTLACT bits are on
50 	(the default value for Fflags is 0), the global word
51 	"Fvalue" (initialized to -1) is returned.
52 
53 	If all fatal globals have their default values, fatal simply
54 	returns -1.
55 */
56 
57 int	Fcnt;
58 int	Fflags;
59 char	*Ffile;
60 int	Fvalue = -1;
61 int	(*Ffunc)();
62 jmp_buf	Fjmp;
63 
64 
65 fatal(msg)
66 char *msg;
67 {
68 	++Fcnt;
69 	if (Fflags & FTLMSG) {
70 		syswrite(2,"ERROR",5);
71 		if (Ffile) {
72 			syswrite(2," [",2);
73 			syswrite(2,Ffile,length(Ffile));
74 			syswrite(2,"]",1);
75 		}
76 		syswrite(2,": ",2);
77 		syswrite(2,msg,length(msg));
78 		syswrite(2,"\n",1);
79 	}
80 	if (Fflags & FTLCLN)
81 		clean_up(0);
82 	if (Fflags & FTLFUNC)
83 		(*Ffunc)(msg);
84 	switch (Fflags & FTLACT) {
85 	case FTLJMP:
86 		longjmp(Fjmp, 1);
87 	case FTLEXIT:
88 		exit(userexit(1));
89 	case FTLRET:
90 		return(Fvalue);
91 	}
92 }
93