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

	PopMac()

	This function "pops" the current macro environment,  which was
established by an earlier call to the PshMac function.  Part of this job
is de-allocating memory consumed by local q-registers.

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

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

extern	DEFAULT	GetNmA();	/* get numeric argument */
extern	DEFAULT	PushEx();	/* push onto expression stack */
extern	VOID	ZFree();	/* free memory */

EXTERN	char	*CStBeg;	/* beginning of command string */
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	WORD	LStBot;		/* loop stack bottom */
EXTERN	WORD	LStTop;		/* loop stack top */
EXTERN	struct	MStck MStack[]; /* macro stack */
EXTERN	WORD	MStTop;		/* top of macro stack */
EXTERN	LONG	NArgmt;		/* numeric argument */


DEFAULT PopMac()		/* restore environment after macro exit */
{
	LOCAL	WORD	i;
	LOCAL	BOOLEAN	RetVal;		/* macro returning a value? */

#if DEBUGGING
DbgInd+=2;if(DbgLvl>=1){DbgMsg();
DbgDBf("PopMac: called.\015\012");DbgROf();}
#endif
	RetVal = NO;
	if (EStTop > EStBot)		/* if macro is returning a value */
		{
		if (GetNmA() == FAILURE)
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("PopMac: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
			return(FAILURE);
#if DEBUGGING
}
#endif
		RetVal = YES;
		}

	CStBeg = MStack[MStTop].BegCS;	/* restore old command string start */
	CBfPtr = MStack[MStTop].CmdPt;	/* restore old command string ptr */
	CStEnd = MStack[MStTop].EndCS;	/* restore old command string end */
	EStBot = MStack[MStTop].ESBot;	/* restore expression stack bottom */
	EStTop = MStack[MStTop].ESTop;	/* restore expression stack top */
	LStBot = MStack[MStTop].LSBot;	/* restore loop stack bottom */
	LStTop = MStack[MStTop].LSTop;	/* restore loop stack top */
	if (MStack[MStTop].LQTabl != NULL)
		for (i=0; i<36; i++)
			if (MStack[MStTop].LQTabl[i].Start != NULL)
				ZFree(MStack[MStTop].LQTabl[i].Start);
	--MStTop;
#if DEBUGGING
if(DbgLvl>=1){DbgMsg();
DbgDBf("PopMac: returning.\015\012");DbgROf();}DbgInd-=2;
#endif
	return(RetVal ? PushEx(NArgmt, OPERAND) : SUCCESS);
}
