/******************************************************************************

	ExeEI()

	This function executes an EI command.  This function does only part of
the work;  the DoEi function does the rest.  This function opens/closes the
file and allocates/deallocates an input buffer.  The code executes such that
no matter what happens,  an open file will always be closed and memory
allocated for an input buffer will always be freed.

******************************************************************************/

#include "ZPort.h"		/* define portability identifiers */
#include "DefTeco.h"		/* define general identifiers */
#include "DefError.h"		/* define identifiers for error messages */

extern	DEFAULT	BldStr();	/* build a string */
extern	DEFAULT	DoEI();		/* do the EI command */
extern	DEFAULT	ErrMsg();	/* display an error message */
extern	char	*ZAlloc();	/* like standard calloc() function */
extern	VOID	ZFree();	/* like standard free() function */
extern	DEFAULT	ZOpInp();	/* open file for input */

EXTERN	BYTE	CmdMod;		/* command modifiers flags for @, :, etc. */
EXTERN	WORD	EStBot;		/* expression stack bottom */
EXTERN	WORD	EStTop;		/* expression stack top */
EXTERN	char	FBfBeg[];	/* beginning of file specification buffer */
EXTERN	char	*FBfEnd;	/* end of file specification buffer */
EXTERN	char	*FBfPtr;	/* pointer into file specification buffer */
EXTERN	DEFAULT	EIIndx;		/* EI file nesting count */



DEFAULT ExeEI()			/* execute an EI command */
{
	auto	int	status;
	auto	char	*ZBfBeg;
	auto	char	*ZBfEnd;

#if DEBUGGING /***************************************************************/
DbgInd+=2;if(DbgLvl>=1){DbgMsg();DbgDBf("ExeEI: called.\015\012");DbgROf();}
#endif /**********************************************************************/

	if (BldStr(FBfBeg, FBfEnd, &FBfPtr) == FAILURE)
#if DEBUGGING /***************************************************************/
{if(DbgLvl>=1){DbgMsg();
DbgDBf("ExeEI: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif /**********************************************************************/
		return(FAILURE);
#if DEBUGGING /***************************************************************/
}
#endif /**********************************************************************/

	if (FBfPtr == FBfBeg)			/* if it's EI$ */
		{
		if (EIIndx >= EIFL)		/* if EI file is open */
		CmdMod = '\0';			/* clear modifiers flags */
		EStTop = EStBot;		/* clear expression stack */
#if DEBUGGING /***************************************************************/
if(DbgLvl>=1){DbgMsg();
DbgDBf("ExeEI: EI$$ command, returning SUCCESS.\015\012");DbgROf();}DbgInd-=2;
#endif /**********************************************************************/
		return(SUCCESS);
		}

	if (++EIIndx > NIFDBS)			/* If EI nesting too deep */
		{
		EIIndx--;
		ErrMsg(ERR_MEM);		/* ERR_MEM = memory overflow */
#if DEBUGGING /***************************************************************/
if(DbgLvl>=1){DbgMsg();
DbgDBf("ExeEI: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif /**********************************************************************/
		return(FAILURE);
		}

	if (ZOpInp(EIIndx, YES) == FAILURE)	/* open command file */
		status = FAILURE;
	else
		{
		ZBfBeg = ZAlloc(ZBFINIT);	/* allocate input buffer */
		if (ZBfBeg == NULL)		/* if allocation failed */
			{
			ErrMsg(ERR_MEM);	/* MEM = memory overflow */
			status = FAILURE;
			}
		else				/* else allocation ok */
			{
			ZBfEnd = ZBfBeg + ZBFINIT;
			status = DoEI(EIIndx, ZBfBeg, ZBfEnd);
			ZFree(ZBfBeg);		/* free allocated memory */
			}
		ZIClos(EIIndx);
		}
	EIIndx--;				/* decrement nesting count */
	CmdMod = '\0';				/* clear modifiers flags */
	EStTop = EStBot;			/* clear expression stack */
#if DEBUGGING /***************************************************************/
if(DbgLvl>=1){DbgMsg();
DbgDBf("ExeEI: returning SUCCESS.\015\012");DbgROf();}DbgInd-=2;
#endif /**********************************************************************/
	return(status);
}
