/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_handlers[] = "@(#)handlers.c	1.33 10/20/92 05:54:38  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	7/9/90

	COLLECTION:	Exception error routines

		__ROr_raised_runtime
		__ROr_missing_op_xlate
		__ROr_missing_arg_xlate
		__ROr_exception_exit

DESCRIPTION:
	These routines are used in the exception handling feature of
	ELROS.   They are all primarily intended for internal use.


*/

#define TRUE	1
#define FALSE	0

/***********************************************************************
	ROUTINE:	__ROr_raised_runtime


PURPOSE:
	This routine fills the exception_entry field to point to the
	corresponding entry in the __RO_Messages table for the exception
	being raised.


IN:
	instance - instance record

OUT:
	instance - instance record


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>
#include <elros/typecodes.h>

int __ROr_raised_runtime(instance)

	__RO_Instance_Record *instance;

{
   unsigned long key = instance->except_state.number;
   int lo=0, hi=__RO_MESSAGES_COUNT-1, index;

   do
   {
      index = (lo+hi)/2;
      if (key == __RO_Messages[index].code)
      {

		/* We've found it, now do some stuff before returning */

         instance->except_state.entry = &__RO_Messages[index];
         if (instance->except_state.entry->rejecter != __ROm_NOREJECT)
            if (__ROr_STDsendRO_reject(instance,
				instance->except_state.entry->rejecter))
               return(EXCEPTION);
         if (instance->except_state.entry->aborter == __ROm_ABORT)
            if (__ROr_STDabort(instance))
               return(EXCEPTION);

         return(SUCCESS);
      }
      else if (key > __RO_Messages[index].code)
         lo = index+1;
      else
         hi = index-1;

      if (lo>hi)
         __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_MESSAGEUNKNOWN);
   } while (1);
}


/***********************************************************************
	ROUTINE:	__ROr_missing_op_xlate


PURPOSE:
	This routine returns a pointer to the name of an operation.
	It also returns the operations class.


IN:
	instance - instance record

OUT:
	op_class - 0-3 operation class
	op_string - operation name (max. 16 chars + '\0')


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>
#include <elros/typecodes.h>

int __ROr_missing_op_xlate(instance,op_class,op_string)

	__RO_Instance_Record *instance;
	char **op_class;
	char *op_string;

#define MO_OP	(instance->except_state.parameters.missing_op.operation)
{
   if (op_class)
      if (MO_OP > -1)
         *op_class = __ROc_ROHstrings[instance->give_table[MO_OP].operation_class];
      else
         *op_class = "not available";
   if (op_string)
      if (MO_OP > -1)
         strcpy(op_string,instance->give_table[MO_OP].text);
      else
         strcpy(op_string,"***UNKNOWN***");
   return(SUCCESS);
}


/***********************************************************************
	ROUTINE:	__ROr_missing_arg_xlate


PURPOSE:
	This routine returns a pointer to the name of an operation,
	and the name of the argument that was missing.
	It also returns the operation's class.


IN:
	instance - instance record

OUT:
	op_class - 0-3 operation class
	op_string - operation name (max. 16 chars + '\0')
	arg_string - argument name (max. 16 chars + '\0')


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>

int __ROr_missing_arg_xlate(instance,op_class,op_string,arg_string)

	__RO_Instance_Record *instance;
	char **op_class;
	char *op_string;
	char *arg_string;

#define MA_OP	(instance->except_state.parameters.missing_arg.operation)
#define MA_ARG	(instance->except_state.parameters.missing_arg.argument)
{
   if (op_class)
         *op_class = __ROc_ROHstrings[instance->give_table[MA_OP].operation_class];
   if (op_string)
      strcpy(op_string,instance->give_table[MA_OP].text);
   if (arg_string)
   {
      __RO_Argument_table_Entry *args;

      
      if (instance->give_table[MA_OP].arguments)
      {
         for (args = instance->give_table[MA_OP].arguments;
              args->internal_number == MA_OP;
              args++)
         {
            if (args->tag_value == MA_ARG)
            {
               strcpy(arg_string,args->text);
               return(SUCCESS);
            }
         }
         sprintf(arg_string,"? = 0x%8.8x",MA_ARG);
      }
   }
   return(SUCCESS);
}


/***********************************************************************
	ROUTINE:	__ROr_exception_exit


PURPOSE:
	This routine prints an error message and terminates the program.


IN:
	instance - instance record

OUT:
NONE

CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>
#include <elros/typecodes.h>
#include <stdio.h>

int __ROr_exception_exit(instance)

	__RO_Instance_Record *instance;

{
   fprintf(stderr,"\n***ELROS Exception Abort\n");
   fprintf(stderr,"Exception raised in FILE: '%s', LINE: %d\n",
		instance->except_state.location.raised_file,
		instance->except_state.location.raised_line);
   fprintf(stderr,"        called from FILE: '%s', LINE: %d\n",
		instance->except_state.location.goto_file,
		instance->except_state.location.goto_line);

   if (!instance->except_state.entry)
   {
      __ROinternal_EXCEPTION(instance,__ROx_INTERNAL_EXCEPTIONENTRY);
      __ROr_exception_exit(instance);
   }
   switch(instance->except_state.entry->exception_id)
   {
      case __ROm_NO_EXCEPTION:
         fprintf(stderr,"__ROr_exception_exit called. No exception found.\n");
         break;
      case __ROm_EXCEPTION_INTERNAL_ERROR:
         fprintf(stderr,"Exception is INTERNAL_ERROR.\n");
         fprintf(stderr,"INTERNAL_ERRORs are related to the integrity of the runtime system itself.\n");
         fprintf(stderr,"They may be caused by inadvertent corruption of the runtime system by the user,\n");
         fprintf(stderr,"or by bugs in the runtime code.  Out of memory errors are also of this type.\n");
         goto general_error;

      case __ROm_EXCEPTION_COM_ERROR:
         fprintf(stderr,"Exception is COM_ERROR.\n");
         fprintf(stderr,"COM_ERRORs are characterized by their network-oriented nature.  They are often\n");
         fprintf(stderr,"transient, or related to the proper use or operation of the underlying network.\n");
         goto general_error;

      case __ROm_EXCEPTION_PROTOCOL_ERROR:
         fprintf(stderr,"Exception is PROTOCOL_ERROR.\n");
         fprintf(stderr,"PROTOCOL_ERRORs are either violations of the communications protocol (by the\n");
         fprintf(stderr,"user or correspondent), or invalid parameterizations of runtime calls.\n");
         goto general_error;

general_error:
         fprintf(stderr,"  Status message follows:\n");
         fprintf(stderr,instance->except_state.entry->text,
			instance->except_state.customize
			? instance->except_state.customize
			: "???");
         fprintf(stderr,"\n");
         break;
      case __ROm_EXCEPTION_MISSING_OP:
      {
         char *opclass;
         char opstr[64];

         fprintf(stderr,"Exception is MISSING_OP.\n");
         if (__ROr_missing_op_xlate(instance,&opclass,opstr))
            __ROr_exception_exit(instance);
         fprintf(stderr,"\tOperation class is %s.\n",opclass);
         fprintf(stderr,"\tOperation Name:    '%s'\n",opstr);
         break;
      }
      case __ROm_EXCEPTION_MISSING_ARG:
      {
         char *opclass;
         char opstr[17];
         char argstr[17];

         fprintf(stderr,"Exception is MISSING_ARG.\n");
         if (__ROr_missing_arg_xlate(instance,&opclass,opstr,argstr))
            __ROr_exception_exit(instance);
         fprintf(stderr,"\tOperation class is %s.\n",opclass);
         fprintf(stderr,"\tOperation Name:    '%s'\n",opstr);
         fprintf(stderr,"\tArgument:          '%s'\n",argstr);
         break;
      }
      case __ROm_EXCEPTION_BAD_OP_ID:
      {
         unsigned long opid;

         fprintf(stderr,"Exception is BAD_OP_ID.\n");
         fprintf(stderr,"\tOperation identifier is %ld.\n",
		instance->except_state.parameters.bad_op_id.opid);
         break;
      }
      case __ROm_EXCEPTION_NAME_ERROR:
      {
         fprintf(stderr,"Exception is NAME_ERROR.\n");
         fprintf(stderr,"\tReturn Code: %d\n",
                        instance->except_state.entry->code);
         fprintf(stderr,"\tPosition in name string where error was encountered: %d\n",
                        instance->except_state.parameters.name_error.position);
         break;
      }
      case __ROm_EXCEPTION_DONE:
      {
         fprintf(stderr,"Exception is DONE.\n");
         break;
      }
      default:
         fprintf(stderr,"Unknown exception id.\n");
         fprintf(stderr,"\tException id was: %d\n",
		instance->except_state.entry->exception_id);
         break;
   }
   abort();
   exit(1);
}


