00001
00055 #include <string.h>
00056 #include "exception.h"
00057 #include "output.h"
00058
00059 EXCEPTIONHANDLER *handlers = NULL;
00060
00064 EXCEPTIONHANDLER *create_exception_handler(void)
00065 {
00066 EXCEPTIONHANDLER *ptr = malloc(sizeof(EXCEPTIONHANDLER));
00067 if(ptr == NULL){
00068 output_fatal("create_exception_handler(): malloc failure");
00069 return NULL;
00070 }
00071 ptr->next = handlers;
00072 ptr->id = (handlers==NULL?0:handlers->id)+1;
00073 memset(ptr->msg,0,sizeof(ptr->msg));
00074 handlers = ptr;
00075 return ptr;
00076 }
00077
00080 void delete_exception_handler(EXCEPTIONHANDLER *ptr)
00081 {
00082 EXCEPTIONHANDLER *target;
00083 if(ptr == NULL){
00084 output_fatal("delete_exception_handler(): ending an exception handler block where no exception handler was present");
00085 return;
00086 }
00087 target = ptr->next;
00088 while (handlers!=target)
00089 {
00090 ptr = handlers;
00091 handlers=ptr->next;
00092 free(ptr);
00093
00094 }
00095 }
00096
00099 void throw_exception(char *format,
00100 ...)
00101 {
00102 char buffer[1024];
00103 va_list ptr;
00104 va_start(ptr,format);
00105 vsprintf(buffer,format,ptr);
00106 va_end(ptr);
00107
00108 if (handlers)
00109 {
00110 strncpy(handlers->msg,buffer,sizeof(handlers->msg));
00111 longjmp(handlers->buf,handlers->id);
00112 }
00113 else
00114 {
00115 output_fatal("unhandled exception: %s", buffer);
00116
00117
00118
00119
00120
00121
00122 exit(-1);
00123 }
00124 }
00125
00129 char *exception_msg(void)
00130 {
00131 return handlers->msg;
00132 }
00133