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

	ExeEPr()

	This function executes an E% command.

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

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

extern	DEFAULT	BldStr();	/* build a string */
extern	DEFAULT	FindQR();	/* find q-register index */
extern	DEFAULT	IncCBP();	/* increment CBfPtr */
extern	VOID	ZOClos();	/* close output file */
extern	DEFAULT	ZOpOut();	/* open output file */
extern	DEFAULT	WrBufr();	/* write a buffer to a file */

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	char	*(*QBfBeg);	/* beginning of q-register text area */
EXTERN	char	*(*QBfPtr);	/* end of q-register text area, plus 1 */


DEFAULT ExeEPr()			/* execute an E% command */
{

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

	if (IncCBP() == FAILURE)		/* move to char after % */
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeEPr: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
		return(FAILURE);
#if DEBUGGING
}
#endif

	if (FindQR() == FAILURE)
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeEPr: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
		return(FAILURE);
#if DEBUGGING
}
#endif

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

	if (FBfPtr == FBfBeg)			/* if it's E%q$ */
		{
		ErrMsg(ERR_UTC);		/* unterminated command */
#if DEBUGGING
if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeEPr: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
		return(FAILURE);
		}

	if (ZOpOut(EPRCFL) == FAILURE)		/* open the file */
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeEPr: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
		return(FAILURE);
#if DEBUGGING
}
#endif

	if (*QBfBeg != NULL)			/* if it's not empty */
		if (WrBufr(EPRCFL, *QBfBeg, *QBfPtr-1L) == FAILURE)
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeEPr: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
			return(FAILURE);
#if DEBUGGING
}
#endif

	ZOClos(EPRCFL);				/* close the file */

	CmdMod = '\0';				/* clear modifiers flags */
	EStTop = EStBot;			/* clear expression stack */

#if DEBUGGING
if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeEPr: returning SUCCESS.\015\012");DbgROf();}DbgInd-=2;
#endif
	return(SUCCESS);
}
