/*********** EXCEPT.H COPYRIGHT 1990 GREGORY COLVIN ************************
This program may be distributed free with this copyright notice.

REVISION HISTORY:
CH#1 modified 6/30/92 to generate __FILE__ and __LINE__ from actual
                      point of assertion failure (See except.h)
****************************************************************************/
#ifndef EXCEPT_CODE

#define EXCEPT_CODE

#include <stdio.h>
#include <string.h>

#define XDEBUG

#include "except.h"

X_TRAP *X_Trap = 0;
XCEPTION X_Error = 0;
volatile sig_atomic_t X_Signal;
/* modified 6/30/92 to generate __FILE__ and __LINE__ from actual
                    point of assertion failure (See except.h)  */
char *X_Assert,
     *source_file;
int  failed_line;

void X_TrapError( void )
{
   if (X_Error) {
      if (X_Trap)
         longjmp(X_Trap->context,X_Error);
      if (X_Error == X_ERRNO)
         fprintf(stderr,"C run-time library error %s\n",
                 strerror(errno));
      else if (X_Error == X_SIGNAL)
         fprintf(stderr,"Signal rasied: %d\n",
                 (int)X_Signal);
      else if (X_Error == X_ASSERT) {
         /* modified by John E. Boon, Jr. */
         /* modified 6/30/92 to generate __FILE__ and __LINE__ from actual
                             point of assertion failure (See except.h)  */
         fprintf(stderr,"\n\n\t\tAssertion failed:\n\t\t(%s)"
                        "\n\t\tOn line %d in file %s\n",
                         X_Assert,failed_line,source_file);
         }
      else if (X_Error == X_SYSTEM)
         fprintf(stderr,"System call failed.\n");
      /* added by John E. Boon, Jr. */
      else if (X_Error == X_USER)
         fprintf(stderr,"User trapped-error occurred.\n");
      else
         fprintf(stderr,"Exception not handled.\n");
      exit(EXIT_FAILURE);

   }
}

void X_HandleSignal( int sig )
{
   signal(sig, X_HandleSignal);
   X_Signal = sig;
   longjmp(X_Trap->context, X_Signal);
}

void X_ReturnSignal( int sig )
{
   signal(sig, X_ReturnSignal);
   X_Signal = sig;
}

#endif
/* End of File */
