/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*---------------------------------------------------------------------------
 *		
 * MODULE NAME:	rciSetOptions
 *
 * DESCRIPTION:	Routines to set and get the various RCI application option
 *		flags.
 *
 * AUTHOR/DATE:	John Lloyd
 *
 * MODIFICATION HISTORY:
 *
 *--------------------------------------------------------------------------*/

#include "../h/cdefs.h"
#include "../h/rciOptions.h"
#include "../h/rciTask.h"

extern unsigned long rciOptionFlags;

extern int rciCpu;
extern int rciTaskId;

#define ALL_OPTIONS	(EXIT_ON_ERROR)

static int initDone = 0;
#define INIT_CHECK()	if (!initDone) doInit();
static doInit()
{
	initDone = 1;
	rciOptionFlags = 0;
	requestFlagInit (&initDone);
}

unsigned long rciPutOptions (mask)
unsigned long mask;
{
	unsigned long old;

	mask &= ALL_OPTIONS;
	
	INIT_CHECK();

	old = rciOptionFlags;
	rciOptionFlags = mask;
	return (old);
}

unsigned long rciGetOptions (mask)
unsigned long mask;
{
	mask &= ALL_OPTIONS;
	
	INIT_CHECK();

	return (rciOptionFlags & mask);
}

unsigned long rciSetOptions (mask)
unsigned long mask;
{
	unsigned long old;

	mask &= ALL_OPTIONS;
	
	INIT_CHECK();

	old = rciOptionFlags & mask;
	rciOptionFlags |= mask;
	return (old);
}

unsigned long rciClearOptions (mask)
unsigned long mask;
{
	unsigned long old;

	mask &= ALL_OPTIONS;
	
	INIT_CHECK();

	old = rciOptionFlags & mask;
	rciOptionFlags &= ~(mask);
	return (old);
}

char rciAutoAbortMask[MAX_RCI_TASKS];

rciMaskAutoAbort()
{
	extern int rciTaskId;

	/* routine whose purpose is to briefly mask the effect of the
	   RCI option EXIT_ON_ERROR. This is useful in implementing
	   packages with RCI */

	rciAutoAbortMask[rciTaskId] = 1;
}

rciUnmaskAutoAbort()
{
	extern int rciTaskId;

	rciAutoAbortMask[rciTaskId] = 0;
}

rciAutoAbortCheck()
{
	/* A system support routine, used by many RCI functions
	   to implement the EXIT_ON_ERROR option: Whenever the
	   function is about to return an error, it calls this routine. This
	   routine then checks to see if EXIT_ON_ERROR has been selected, and
	   if it has, it either prints the error stack and exits the
	   program (if we are in the planning task), or calls rciAbort.
	 */

	extern int rciTaskId;

	if ((rciOptionFlags & EXIT_ON_ERROR) && !rciAutoAbortMask[rciTaskId])
	 { if (rciTaskId == 0)
	    { printErrors();
	      exit(-1);
	    }
	   else
	    { rciAbortErrors();
	    }
	 }
}
