00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 #include <stdio.h>
00083 #include <stdlib.h>
00084 #include <string.h>
00085
00086 #include "arithmetique.h"
00087
00088
00089
00090
00091
00092 unsigned int overflow_error = 1;
00093 unsigned int simplex_arithmetic_error = 2;
00094 unsigned int user_exception_error = 4;
00095 unsigned int parser_exception_error = 8;
00096 unsigned int timeout_error = 16;
00097
00098
00099 unsigned int any_exception_error = ~0;
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 #ifndef HAVE_BOOLEAN
00110 typedef int boolean;
00111 #endif
00112 #ifndef FALSE
00113 #define FALSE 0
00114 #endif
00115 #ifndef TRUE
00116 #define TRUE 1
00117 #endif
00118
00119 const char *get_exception_name(unsigned int exception)
00120 {
00121 if (exception==overflow_error)
00122 return "overflow_error exception";
00123 if (exception==simplex_arithmetic_error)
00124 return "simplex_arithmetic_error exception";
00125 if (exception==user_exception_error)
00126 return "user_exception_error exception";
00127 if (exception==parser_exception_error)
00128 return "parser_exception_error exception";
00129 if (exception==timeout_error)
00130 return "timeout_error exception";
00131 if (exception==any_exception_error)
00132 return "all exceptions mask";
00133
00134 return "unknown or mixed exception";
00135 }
00136
00137
00138
00139 unsigned int the_last_just_thrown_exception = 0;
00140
00141
00142
00143 static int linear_exception_debug_mode = FALSE;
00144 static unsigned int linear_exception_verbose = 1 | 2 | 16 ;
00145
00146
00147
00148 typedef struct
00149 {
00150
00151
00152 int what;
00153
00154
00155
00156 jmp_buf where;
00157
00158
00159
00160 const char * function;
00161 const char * file;
00162 int line;
00163 }
00164 linear_exception_holder;
00165
00166
00167
00168
00169
00170 #define MAX_STACKED_CONTEXTS 64
00171 static linear_exception_holder exception_stack[MAX_STACKED_CONTEXTS];
00172 static int exception_index = 0;
00173
00174
00175
00176 static exception_callback_t push_callback = NULL;
00177 static exception_callback_t pop_callback = NULL;
00178
00179 void set_exception_callbacks(exception_callback_t push,
00180 exception_callback_t pop)
00181 {
00182 if (push_callback!=NULL || pop_callback!=NULL)
00183 {
00184 fprintf(stderr, "exception callbacks already defined! (%p, %p)\n",
00185 push_callback, pop_callback);
00186 abort();
00187 }
00188
00189 push_callback = push;
00190 pop_callback = pop;
00191 }
00192
00193
00194
00195 int linear_number_of_exception_thrown = 0;
00196
00197
00198
00199 void dump_exception_stack_to_file(FILE * f)
00200 {
00201 int i;
00202 fprintf(f, "[dump_exception_stack_to_file] size=%d\n", exception_index);
00203 for (i=0; i<exception_index; i++)
00204 {
00205 fprintf(f,
00206 "%d: [%s:%d in %s (%d)]\n",
00207 i,
00208 exception_stack[i].file,
00209 exception_stack[i].line,
00210 exception_stack[i].function,
00211 exception_stack[i].what);
00212 }
00213 fprintf(f, "\n");
00214 }
00215
00216 void dump_exception_stack()
00217 {
00218 dump_exception_stack_to_file(stderr);
00219 }
00220
00221 #define exception_debug_message(type) \
00222 fprintf(stderr, "%s[%s:%d %s (%d)/%d]\n", \
00223 type, file, line, function, what, exception_index)
00224
00225 #define exception_debug_trace(type) \
00226 if (linear_exception_debug_mode) { exception_debug_message(type); }
00227
00228
00229
00230
00231 jmp_buf *
00232 push_exception_on_stack(
00233 int what,
00234 const char * function,
00235 const char * file,
00236 int line)
00237 {
00238 exception_debug_trace("PUSH ");
00239
00240 if (exception_index==MAX_STACKED_CONTEXTS)
00241 {
00242 exception_debug_message("push");
00243 fprintf(stderr, "exception stack overflow\n");
00244 dump_exception_stack();
00245 abort();
00246 }
00247
00248 if (push_callback) push_callback(file, function, line);
00249
00250 the_last_just_thrown_exception = 0;
00251
00252 exception_stack[exception_index].what = what;
00253 exception_stack[exception_index].function = function;
00254 exception_stack[exception_index].file = file;
00255 exception_stack[exception_index].line = line;
00256
00257 return & exception_stack[exception_index++].where;
00258 }
00259
00260 #if !defined(same_string_p)
00261 #define same_string_p(s1, s2) (strcmp((s1),(s2))==0)
00262 #endif
00263
00264
00265
00266
00267 void
00268 pop_exception_from_stack(
00269 int what,
00270 const char * function,
00271 const char * file,
00272 int line)
00273 {
00274 exception_debug_trace("POP ");
00275
00276 if (exception_index==0)
00277 {
00278 exception_debug_message("pop");
00279 fprintf(stderr, "exception stack underflow\n");
00280 dump_exception_stack();
00281 abort();
00282 }
00283
00284 if (pop_callback) pop_callback(file, function, line);
00285
00286 exception_index--;
00287 the_last_just_thrown_exception = 0;
00288
00289 if ((exception_stack[exception_index].what != what) ||
00290 !same_string_p(exception_stack[exception_index].file, file) ||
00291 !same_string_p(exception_stack[exception_index].function, function))
00292 {
00293 exception_debug_message("pop");
00294 fprintf(stderr,
00295 "exception stack mismatch at depth=%d:\n"
00296 " CATCH: %s:%d in %s (%d)\n"
00297 " UNCATCH: %s:%d in %s (%d)\n",
00298 exception_index,
00299 exception_stack[exception_index].file,
00300 exception_stack[exception_index].line,
00301 exception_stack[exception_index].function,
00302 exception_stack[exception_index].what,
00303 file, line, function, what);
00304 dump_exception_stack();
00305 abort();
00306 }
00307 }
00308
00309
00310
00311
00312 void throw_exception(
00313 int what,
00314 const char * function,
00315 const char * file,
00316 int line)
00317 {
00318 int i;
00319
00320 exception_debug_trace("THROW");
00321
00322 the_last_just_thrown_exception = what;
00323
00324 for (i=exception_index-1; i>=0; i--)
00325 {
00326 if (pop_callback)
00327
00328 pop_callback(exception_stack[i].file,
00329 exception_stack[i].function,
00330 exception_stack[i].line);
00331
00332 if (exception_stack[i].what & what)
00333 {
00334 exception_index = i;
00335 linear_number_of_exception_thrown++;
00336
00337 if (linear_exception_debug_mode)
00338 fprintf(stderr, "---->[%s:%d %s (%d)/%d]\n",
00339 exception_stack[i].file,
00340 exception_stack[i].line,
00341 exception_stack[i].function,
00342 exception_stack[i].what,
00343 i);
00344
00345
00346 if (linear_exception_verbose & what)
00347 fprintf(stderr, "exception %d/%d: %s(%s:%d) -> %s(%s:%d)\n",
00348 what, exception_stack[i].what,
00349 function, file, line,
00350 exception_stack[i].function,
00351 exception_stack[i].file,
00352 exception_stack[i].line);
00353
00354 longjmp(exception_stack[i].where, 0);
00355 }
00356 }
00357
00358
00359 exception_debug_message("throw");
00360 fprintf(stderr,
00361 "exception not found in stack:\n"
00362 "an exception was THROWN without a proper matching CATCH\n");
00363 dump_exception_stack();
00364 abort();
00365 }
00366
00367 void linear_initialize_exception_stack(
00368 unsigned int verbose_exceptions,
00369 exception_callback_t push,
00370 exception_callback_t pop)
00371 {
00372 linear_exception_verbose = verbose_exceptions;
00373 set_exception_callbacks(push, pop);
00374 }