/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: e_trap.c,v 1.2 1997/09/25 17:16:54 ae59 Exp $ */
/*
$Log: e_trap.c,v $
 * Revision 1.2  1997/09/25  17:16:54  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:22:18  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : e_trap.c                              */
/*                                                              */
/*      Entry           : void e_trap(code,e_argc,e_args)       */
/*                        a_btyp code;                          */
/*                        int e_argc;                           */
/*                        e_list                                */
/*                                                              */
/*      Arguments       : code - error code                     */
/*                        e_argc - number of arguments in list  */
/*                        e_args - variable argument list       */
/*                                                              */
/*      Description     : Trap handler.                         */
/*                                                              */
/*      Date            : 1991-01-22                            */
/*      1991-01-22 : VAX_VMS_C considered                       */
/*      1990-12-04 : E_EQIE,E_EARG                              */
/*      1990-12-03 : default used if E_EMSG not set             */
/*      1990-11-28 : handle E_EXIT,E_EMSG,E_ETBC,E_ECNT         */
/*                   remove e_done                              */
/*      1990-10-31 : force linkage of global variables          */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
extern aentry *e_astk;
extern aentry e_anor;
extern char *e_head;
extern f_text f_errr;
#endif

#ifdef LINT_ARGS
#if C_P_7
local void e_trap(a_btyp code,int e_argc,...)
#else
local void e_trap(code,e_argc,e_args)
a_btyp code;
int e_argc;
e_list
#endif
#else
local void e_trap(code,e_argc,e_args)
a_btyp code;
int e_argc;
e_list
#endif

        {
        va_list e_argv;
        a_btyp op;
        aentry *ent;
        a_bool found = FALSE;

        e_open(e_argc);

        /* split code value                                     */
        op = code & E_MASK;

        /* Initialize exception handler stack                   */
        if ((ent = e_astk)==(aentry *)NULL)
           {
#if VAX_VMS_C
           e_data();
#endif
           ent = e_astk = &e_anor;
           }

        /* search for exception handler                         */
        while (ent!=(aentry *)NULL)
        if ((ent->code & E_MASK)==op)
           {

           /* active exception handler found                    */
           if (ent->active)
              {

              /* default setting for message/traceback          */
              if (!(code & E_EMSG))
                 {
                 code |= (ent->back) ? E_ETBC+E_EMSG+E_EARG : E_EMSG;
                 }

              /* default setting for exit/continue              */
              if (!(code & (E_ECNT+E_EXIT)))
                 {
                 code |= (ent->cont) ? E_ECNT : E_EXIT;
                 }

              /* invoke  exception handler                      */
              if (ent->action!=NO_TRAP)
                 {
                 (*ent->action)(code,e_argc,e_argv);
                 found = TRUE;
                 }
              else
                 {
                 fprintf(f_errr.fp,"%se_trap : No exception ",e_head);
                 fprintf(f_errr.fp,"handler defined ");
                 fprintf(f_errr.fp,"for trap code %08.8lx\n",code);
                 }

              /* inactivate special exception handler           */
              if (!(ent->def)) ent->active = FALSE;

              break;
              }
           ent = ent->succ;
           }
        else if ((ent->code & E_MASK)<op)
           ent = ent->succ;
        else
           break;
        /* end of while                                         */

        if (NOT(found))
           {
           e_back(f_errr.fp);
           fprintf(f_errr.fp,"%se_trap : No active exception ",e_head);
           fprintf(f_errr.fp,"handler found ");
           fprintf(f_errr.fp,"for error code %08.8lx\n",code);
           }

        e_shut;
        }
