home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / DigitalCD / !DigitalCD / Copy / PowerBars / c / Exception < prev    next >
Text File  |  1999-01-02  |  2KB  |  107 lines

  1. /*** exception.c ***/
  2. /* C exception handling system
  3.  * (c) Paul Field
  4.  */
  5.  
  6. #include "exception.h"
  7.  
  8. #include <assert.h>
  9. #include <signal.h>
  10. #include <stdarg.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14.  
  15. #include "kernel.h"
  16.  
  17. static exception current;
  18. _exception_block *_exception_list = NULL;
  19.  
  20. void throw(void)
  21. {
  22.     _exception_block* first_block;
  23.  
  24.     first_block = _exception_list;
  25.     if (first_block == NULL)
  26.     {
  27.         exit(EXIT_FAILURE);
  28.     }
  29.     else
  30.     {
  31.         _exception_list = first_block->previous;
  32.         longjmp(first_block->jmpbuf, 1);
  33.     }
  34. }
  35.  
  36. void __throw_user(unsigned id, char* file , int line)
  37. {
  38.     current.type          = exception_user;
  39.     current.error.user_id = id;
  40.     current.file          = file;
  41.     current.line          = line;
  42.     throw();
  43. }
  44.  
  45. void __throw_os(const _kernel_oserror* err, char* file , int line)
  46. {
  47.     if (err == NULL) return;
  48.  
  49.     current.type     = exception_os;
  50.     current.error.os = err;
  51.     current.file          = file;
  52.     current.line          = line;
  53.     throw();
  54. }
  55.  
  56. void __throw_last_os_error(char* file , int line)
  57. {
  58.     current.file          = file;
  59.     current.line          = line;
  60.     throw_os(_kernel_last_oserror());
  61. }
  62.  
  63. void __throw_string(char* file , int line, const char* pformat, ...)
  64. {
  65.     va_list arg;
  66.     static _kernel_oserror err;
  67.  
  68.     va_start(arg, pformat);
  69.     vsprintf(err.errmess, pformat, arg);
  70.     va_end(arg);
  71.  
  72.     err.errnum = 1;
  73.     __throw_os(&err, file, line);
  74. }
  75.  
  76. const exception* exception_current(void)
  77. {
  78.     return ¤t;
  79. }
  80.  
  81. static void throw_signal(int sig)
  82. {
  83.     current.type            = exception_signal;
  84.     current.error.signal_id = sig;
  85.     signal(sig, throw_signal);  /* reinstate signal handler */
  86.     throw();
  87. }
  88.  
  89. static void throw_last_os(int sig)
  90. {
  91.     sig=sig;
  92.     signal(SIGOSERROR, throw_last_os);  /* reinstate signal handler */
  93.     throw_os(_kernel_last_oserror());
  94. }
  95.  
  96. void exception_initialise(void)
  97. {
  98.     signal(SIGABRT, throw_signal);
  99.     signal(SIGFPE,  throw_signal);
  100.     signal(SIGILL,  throw_signal);
  101.     signal(SIGINT,  throw_signal);
  102.     signal(SIGSEGV, throw_signal);
  103.     signal(SIGTERM, throw_signal);
  104.     /*signal(SIGSTAK, throw_signal);*/
  105.     signal(SIGOSERROR, throw_last_os);
  106. }
  107.