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

	ExeEC()

	This function executes an EC command.

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

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

extern	VOID	ZDspBf();	/* display a buffer on the terminal */
extern	DEFAULT	RdPage();	/* read a page */
extern	DEFAULT	GetNmA();	/* get numeric argument */
extern	VOID	ZIClos();	/* close input file */
extern	VOID	ZOClos();	/* close output file */
extern	DEFAULT	WrPage();	/* write to output file */

EXTERN	BYTE	CmdMod;		/* command modifiers flags for @, :, etc. */
EXTERN	DEFAULT	CurInp;		/* index of current input stream in IFiles */
EXTERN	DEFAULT	CurOut;		/* index of current output stream in OFiles */
EXTERN	char	*EBfBeg;	/* beginning of edit buffer */
EXTERN	char	*EBfEnd;	/* end of edit buffer */
EXTERN	WORD	EStBot;		/* expression stack bottom */
EXTERN	WORD	EStTop;		/* expression stack top */
EXTERN	LONG	FFPage;		/* form feed flag (returned by ^E command) */
EXTERN	char	*GapBeg;	/* beginning of edit buffer gap */
EXTERN	char	*GapEnd;	/* end of edit buffer gap */
EXTERN	BOOLEAN	IsOpnI[];
EXTERN	BOOLEAN	IsEofI[];


DEFAULT ExeEC()			/* execute an EC command */
{

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

	if (EStTop > EStBot)			/* if numeric argument */
		{
		if (GetNmA() == FAILURE)	/* get numeric argument */
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeEC: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
			return(FAILURE);
#if DEBUGGING
}
#endif
/* ??? */	ZDspBf("memory expand as per page 84\015\012", 29);
		CmdMod = '\0';			/* clear modifiers flags */
#if DEBUGGING
if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeEC: returning SUCCESS.\015\012");DbgROf();}DbgInd-=2;
#endif
		return(SUCCESS);
		}


/*
  if there's text in the edit buffer,  or if the form feed flag indicates
  that the current page needs to be output,  call WrPage to do it.
*/

	if ((GapBeg > EBfBeg) || (GapEnd < EBfEnd) || FFPage)
		if (WrPage(CurOut, EBfBeg, EBfEnd, FFPage) == FAILURE)
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeEC: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
			return(FAILURE);
#if DEBUGGING
}
#endif

	if (IsOpnI[CurInp])			/* if input file is open */
	while (!IsEofI[CurInp])			/* while not end-of-file */
		{

		GapBeg = EBfBeg;		/* clear the... */
		GapEnd = EBfEnd;		/*   ...edit buffer */

		if (RdPage() == FAILURE)	/* read a page */
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeEC: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
			return(FAILURE);
#if DEBUGGING
}
#endif

		if (WrPage(CurOut, EBfBeg, EBfEnd, FFPage) == FAILURE)
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeEC: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
			return(FAILURE);
#if DEBUGGING
}
#endif
		}


	ZIClos(PINPFL);		/* close primary input file */
	ZIClos(SINPFL);		/* close secondary input file */
	ZOClos(POUTFL);		/* close primary output file */
	ZOClos(SOUTFL);		/* close secondary output file */

	CmdMod = '\0';				/* clear modifiers flag */
#if DEBUGGING
if(DbgLvl>=1){DbgMsg();DbgDBf("ExeEC: returning SUCCESS.\015\012");DbgROf();}
DbgInd-=2;
#endif
	return(SUCCESS);
}
