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

	DoEI()

	This function performs the execution part of an EI command.  It is
called by the ExeEI function.  It is passed an index to a file data block in
the IFiles array that has been opened.  It is also passed a memory buffer.
This function reads text from the file into the buffer and calls ExeCSt to
execute the text.

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

#include "ZPort.h"		/* define portability identifiers */
#include "DefTeco.h"		/* define general identifiers */

extern	DEFAULT	ExeCSt();	/* execute command string */
extern	DEFAULT	PopMac();	/* pop macro level */
extern	DEFAULT	PshMac();	/* push macro level */
extern	DEFAULT	ZRdEI();	/* read EI file */

EXTERN	BOOLEAN	GotCtC;		/* YES if user just hit a control-C */
EXTERN	BOOLEAN	IsEofI[];



DEFAULT DoEI(IfIndx, ZBfBeg, ZBfEnd)		/* do an EI command */
DEFAULT	IfIndx;		/* index into IFiles array */
char	*ZBfBeg;	/* beginning of buffer containing commands */
char	*ZBfEnd;	/* end of buffer containing commands */
{
	short	length;
	auto	char	*ZBfPtr;

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

	do
		{
		if (ZRdEI(IfIndx, ZBfBeg, ZBfEnd, &ZBfPtr) == FAILURE)
#if DEBUGGING /***************************************************************/
{if(DbgLvl>=1){DbgMsg();
DbgDBf("DoEI: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif /**********************************************************************/
			return(FAILURE);
#if DEBUGGING /***************************************************************/
}if(DbgLvl>=1){DbgMsg();DbgDBf("DoEI: ZBf buffer = \"");TypBuf(ZBfBeg,ZBfPtr);
DbgDBf("\"\015\012");DbgMsg();DbgDBf("DoEI: before PshMac");
DbgDBf(", ZBfBeg = ");MakDBf(ZBfBeg, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf(", ZBfPtr = ");MakDBf(ZBfPtr, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf("\015\012");DbgROf();}
#endif /**********************************************************************/
		if (PshMac(ZBfBeg, ZBfPtr) == FAILURE)
#if DEBUGGING /***************************************************************/
{if(DbgLvl>=1){DbgMsg();
DbgDBf("DoEI: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif /**********************************************************************/
			return(FAILURE);
#if DEBUGGING /***************************************************************/
}
#endif /**********************************************************************/
		if (ExeCSt() == FAILURE)	/* execute command string */
#if DEBUGGING /***************************************************************/
{if(DbgLvl>=1){DbgMsg();
DbgDBf("DoEI: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif /**********************************************************************/
			return(FAILURE);
#if DEBUGGING /***************************************************************/
}
#endif /**********************************************************************/
		if (PopMac() == FAILURE)	/* pop macro level */
#if DEBUGGING /***************************************************************/
{if(DbgLvl>=1){DbgMsg();
DbgDBf("DoEI: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif /**********************************************************************/
			return(FAILURE);
#if DEBUGGING /***************************************************************/
}if(DbgLvl>=1){DbgMsg();DbgDBf("DoEI: loop bottom,  IsEofI[IfIndx] = ");
DbgDBf(IsEofI[IfIndx] ? "YES" : "NO");DbgDBf(", GotCtC = ");
DbgDBf(GotCtC ? "YES" : "NO");DbgDBf("\015\012");DbgROf();}
#endif /**********************************************************************/
		}
	while ((!IsEofI[IfIndx]) && (!GotCtC));
#if DEBUGGING /***************************************************************/
if(DbgLvl>=1){DbgMsg();
DbgDBf("DoEI: returning SUCCESS.\015\012");DbgROf();}DbgInd-=2;
#endif /**********************************************************************/
	return(SUCCESS);
}
