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

	ExeP()

	This function executes a P or PW command.

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

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

extern	VOID	ErrMsg();	/* display an error message */
extern	DEFAULT	ExePW();	/* execute a PW command */
extern	DEFAULT	GetNmA();	/* get numeric argument */
extern	DEFAULT	PushEx();	/* push onto expression stack */
extern	DEFAULT	SinglP();	/* execute a single P function */

EXTERN	BYTE	CmdMod;		/* command modifiers flags for @, :, etc. */
EXTERN	DEFAULT	CurInp;		/* index of current input stream in IFiles */
EXTERN	char	*CBfPtr;	/* pointer into command string */
EXTERN	char	*CStEnd;	/* pointer to last char of command string */
EXTERN	WORD	EStBot;		/* expression stack bottom */
EXTERN	WORD	EStTop;		/* expression stack top */
EXTERN	BOOLEAN	IsEofI[];
EXTERN	LONG	NArgmt;		/* numeric argument */


DEFAULT ExeP()			/* execute a P or PW command */
{
	BOOLEAN	SavEof;

#if DEBUGGING
DbgInd+=2;if(DbgLvl>=1){DbgMsg();DbgDBf("ExeP: called.\015\012");DbgROf();}
#endif
	SavEof = IsEofI[CurInp];

	if (EStTop == EStBot)			/* if no numeric argument */
		NArgmt = 1L;			/* default is 1P or 1PW */
	else
		{
		if (GetNmA() == FAILURE)	/* get numeric argument */
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeP: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
			return(FAILURE);
#if DEBUGGING
}
#endif
		if (NArgmt <= 0L)
			{
			ErrMsg(ERR_IPA);	/* negative or 0 arg to P */
#if DEBUGGING
if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeP: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
			return(FAILURE);
			}
		}

	if ((CBfPtr != CStEnd) &&
	    ((*(CBfPtr+1L) == 'W') || (*(CBfPtr+1L) == 'w')))
		{
		++CBfPtr;
#if DEBUGGING
if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeP: returning ExePW().\015\012");DbgROf();}DbgInd-=2;
#endif
		return(ExePW());		/* execute a PW command */
		}

	if (CmdMod & MARGIS)			/* if it's m,nP */
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeP: returning ExePW().\015\012");DbgROf();}DbgInd-=2;
#endif
		return(ExePW());		/* execute a PW command */
#if DEBUGGING
}
#endif

	do
		if (SinglP() == FAILURE)
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeP: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
			return(FAILURE);
#if DEBUGGING
}
#endif
	while ((--NArgmt > 0L) && !IsEofI[CurInp]);

	if (CmdMod & COLON)			/* if it's :P */
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("ExeP: returning PushEx().\015\012");DbgROf();}DbgInd-=2;
#endif
		return(PushEx(SavEof ? 0L : -1L, OPERAND));
#if DEBUGGING
}
#endif

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