]> icculus.org git repositories - btb/d2x.git/blob - misc/error.c
allow specifying file to extract
[btb/d2x.git] / misc / error.c
1 /* $Id: error.c,v 1.3 2002-08-02 11:05:26 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14 /*
15  *
16  * Error handling/printing/exiting code
17  *
18  * Old Log:
19  * Revision 1.12  1994/12/07  18:49:39  matt
20  * error_init() can now take NULL as parm
21  * 
22  * Revision 1.11  1994/11/29  15:42:07  matt
23  * Added newline before error message
24  * 
25  * Revision 1.10  1994/11/27  23:20:39  matt
26  * Made changes for new mprintf calling convention
27  * 
28  * Revision 1.9  1994/06/20  21:20:56  matt
29  * Allow NULL for warn func, to kill warnings
30  * 
31  * Revision 1.8  1994/05/20  15:11:35  mike
32  * mprintf Warning message so you can actually see it.
33  * 
34  * Revision 1.7  1994/02/10  18:02:38  matt
35  * Changed 'if DEBUG_ON' to 'ifndef NDEBUG'
36  * 
37  * Revision 1.6  1993/10/17  18:19:10  matt
38  * If error_init() not called, Error() now prints the error message before
39  * calling exit()
40  * 
41  * Revision 1.5  1993/10/14  15:29:11  matt
42  * Added new function clear_warn_func()
43  * 
44  * Revision 1.4  1993/10/08  16:17:19  matt
45  * Made Assert() call function _Assert(), rather to do 'if...' inline.
46  * 
47  * Revision 1.3  1993/09/28  12:45:25  matt
48  * Fixed wrong print call, and made Warning() not append a CR to string
49  * 
50  * Revision 1.2  1993/09/27  11:46:35  matt
51  * Added function set_warn_func()
52  * 
53  * Revision 1.1  1993/09/23  20:17:33  matt
54  * Initial revision
55  * 
56  *
57  */
58
59 #ifdef HAVE_CONFIG_H
60 #include <conf.h>
61 #endif
62
63 #ifdef RCS
64 static char rcsid[] = "$Id: error.c,v 1.3 2002-08-02 11:05:26 btb Exp $";
65 #endif
66
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <stdarg.h>
70 #include <string.h>
71
72 #include "pstypes.h"
73 #include "console.h"
74 #include "error.h"
75
76 #define MAX_MSG_LEN 256
77
78 //edited 05/17/99 Matt Mueller added err_ prefix to prevent conflicts with statically linking SDL
79 int err_initialized=0;
80 //end edit -MM
81
82 char exit_message[MAX_MSG_LEN]="";
83 char warn_message[MAX_MSG_LEN];
84
85 //takes string in register, calls printf with string on stack
86 void warn_printf(char *s)
87 {
88         printf("%s\n",s);
89 }
90
91 void (*warn_func)(char *s)=warn_printf;
92
93 //provides a function to call with warning messages
94 void set_warn_func(void (*f)(char *s))
95 {
96         warn_func = f;
97 }
98
99 //uninstall warning function - install default printf
100 void clear_warn_func(void (*f)(char *s))
101 {
102         warn_func = warn_printf;
103 }
104
105 void set_exit_message(char *fmt,...)
106 {
107         va_list arglist;
108         int len;
109
110         va_start(arglist,fmt);
111         len = vsprintf(exit_message,fmt,arglist);
112         va_end(arglist);
113
114         if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in set_exit_message (len=%d, max=%d)",len,MAX_MSG_LEN);
115
116 }
117
118 void _Assert(int expr,char *expr_text,char *filename,int linenum)
119 {
120         if (!(expr)) Error("Assertion failed: %s, file %s, line %d",expr_text,filename,linenum);
121
122 }
123
124 void print_exit_message(void)
125 {
126         if (*exit_message)
127                 con_printf(CON_CRITICAL, "%s\n",exit_message);
128 }
129
130 //terminates with error code 1, printing message
131 void Error(char *fmt,...)
132 {
133         va_list arglist;
134
135         strcpy(exit_message,"\nError: ");
136
137         va_start(arglist,fmt);
138         vsprintf(exit_message+strlen(exit_message),fmt,arglist);
139         va_end(arglist);
140
141         con_printf(CON_CRITICAL, exit_message);
142
143         exit(1);
144 }
145
146 //print out warning message to user
147 void Warning(char *fmt,...)
148 {
149         va_list arglist;
150
151         if (warn_func == NULL)
152                 return;
153
154         strcpy(warn_message,"Warning: ");
155
156         va_start(arglist,fmt);
157         vsprintf(warn_message+strlen(warn_message),fmt,arglist);
158         va_end(arglist);
159
160         con_printf(CON_URGENT, warn_message);
161
162 }
163
164 //initialize error handling system, and set default message. returns 0=ok
165 int error_init(char *fmt,...)
166 {
167         va_list arglist;
168         int len;
169
170         atexit(print_exit_message);             //last thing at exit is print message
171
172         if (fmt != NULL) {
173                 va_start(arglist,fmt);
174                 len = vsprintf(exit_message,fmt,arglist);
175                 va_end(arglist);
176                 if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in error_init (len=%d, max=%d)",len,MAX_MSG_LEN);
177         }
178
179         err_initialized=1;
180
181         return 0;
182 }