]> icculus.org git repositories - btb/d2x.git/blob - misc/error.c
added stuff to .cvsignore
[btb/d2x.git] / misc / error.c
1 /* $Id: error.c,v 1.7 2004-08-28 23:17:45 schaffner 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  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #ifdef RCS
25 static char rcsid[] = "$Id: error.c,v 1.7 2004-08-28 23:17:45 schaffner Exp $";
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <string.h>
32
33 #include "pstypes.h"
34 #include "console.h"
35 #include "mono.h"
36 #include "error.h"
37
38 #define MAX_MSG_LEN 256
39
40 //edited 05/17/99 Matt Mueller added err_ prefix to prevent conflicts with statically linking SDL
41 int err_initialized=0;
42 //end edit -MM
43
44 static void (*ErrorPrintFunc)(char *);
45
46 char exit_message[MAX_MSG_LEN]="";
47 char warn_message[MAX_MSG_LEN];
48
49 //takes string in register, calls printf with string on stack
50 void warn_printf(char *s)
51 {
52         con_printf(CON_URGENT, "%s\n",s);
53 }
54
55 void (*warn_func)(char *s)=warn_printf;
56
57 //provides a function to call with warning messages
58 void set_warn_func(void (*f)(char *s))
59 {
60         warn_func = f;
61 }
62
63 //uninstall warning function - install default printf
64 void clear_warn_func(void (*f)(char *s))
65 {
66         warn_func = warn_printf;
67 }
68
69 void set_exit_message(char *fmt,...)
70 {
71         va_list arglist;
72         int len;
73
74         va_start(arglist,fmt);
75         len = vsprintf(exit_message,fmt,arglist);
76         va_end(arglist);
77
78         if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in set_exit_message (len=%d, max=%d)",len,MAX_MSG_LEN);
79
80 }
81
82 void _Assert(int expr,char *expr_text,char *filename,int linenum)
83 {
84         Int3();
85         if (!(expr)) Error("Assertion failed: %s, file %s, line %d",expr_text,filename,linenum);
86 }
87
88 void print_exit_message(void)
89 {
90         if (*exit_message)
91         {
92                 if (ErrorPrintFunc)
93                 {
94                         (*ErrorPrintFunc)(exit_message);
95                 }
96                 else
97                 {
98 #if (defined(MACINTOSH) && defined(NDEBUG) && defined(RELEASE))
99                         c2pstr(exit_message);
100                         ShowCursor();
101                         ParamText(exit_message, "\p", "\p", "\p");
102                         StopAlert(ERROR_ALERT, nil);
103 #else
104                         con_printf(CON_CRITICAL, "%s\n",exit_message);
105 #endif
106                 }
107         }
108 }
109
110 //terminates with error code 1, printing message
111 void Error(char *fmt,...)
112 {
113         va_list arglist;
114
115 #if (defined(MACINTOSH) && defined(NDEBUG) && defined(RELEASE))
116         strcpy(exit_message,"Error: "); // don't put the new line in for dialog output
117 #else
118         strcpy(exit_message,"\nError: ");
119 #endif
120         va_start(arglist,fmt);
121         vsprintf(exit_message+strlen(exit_message),fmt,arglist);
122         va_end(arglist);
123
124         Int3();
125
126         if (!err_initialized) print_exit_message();
127
128         exit(1);
129 }
130
131 //print out warning message to user
132 void Warning(char *fmt,...)
133 {
134         va_list arglist;
135
136         if (warn_func == NULL)
137                 return;
138
139         strcpy(warn_message,"Warning: ");
140
141         va_start(arglist,fmt);
142         vsprintf(warn_message+strlen(warn_message),fmt,arglist);
143         va_end(arglist);
144
145         mprintf((0, "%s\n", warn_message));
146         (*warn_func)(warn_message);
147
148 }
149
150 //initialize error handling system, and set default message. returns 0=ok
151 int error_init(void (*func)(char *), char *fmt, ...)
152 {
153         va_list arglist;
154         int len;
155
156         atexit(print_exit_message);             //last thing at exit is print message
157
158         ErrorPrintFunc = func;          // Set Error Print Functions
159
160         if (fmt != NULL) {
161                 va_start(arglist,fmt);
162                 len = vsprintf(exit_message,fmt,arglist);
163                 va_end(arglist);
164                 if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in error_init (len=%d, max=%d)",len,MAX_MSG_LEN);
165         }
166
167         err_initialized=1;
168
169         return 0;
170 }