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

	FlowEL()

	This function exits the current loop.  It is called when a loop
command (<) is executed with a numeric argument less than or equal to zero,
when a smei-colon command is executed with a numeric argument greater than
zero,  or when a search command within a loop fails.

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

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

extern	VOID	EchoIt();	/* echo a character to the terminal */
extern	VOID	ErrMsg();	/* display an error message */
extern	DEFAULT	SkpCmd();	/* skip a single command */

EXTERN	char	*CBfPtr;	/* pointer into command string */
EXTERN	char	*CStEnd;	/* pointer to last char of command string */
EXTERN	WORD	LStTop;		/* top of loop stack */
EXTERN	BOOLEAN	TraceM;		/* trace mode flag */


DEFAULT FlowEL()		/* flow to end of loop */
{
	LOCAL WORD TmpNst;	/* temporary loop next count */

#if DEBUGGING
DbgInd+=2;if(DbgLvl>=3){DbgMsg();DbgDBf("FlowEL: called.\015\012");DbgROf();}
#endif
	TmpNst = 1;
	do {
		if (CBfPtr == CStEnd)		/* if end of command string */
			{
			ErrMsg(ERR_UTC);	/* unterminated command */
#if DEBUGGING
if(DbgLvl>=3)
{DbgMsg();DbgDBf("FlowEL: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
			return(FAILURE);
			}
		++CBfPtr;			/* move to next command */
		if (*CBfPtr == '<')		/* if loop start character */
			++TmpNst;		/* increment nesting count */
		else if (*CBfPtr == '>')	/* else if loop end char */
			--TmpNst;		/* decrement nesting count */
		else
			if (SkpCmd() == FAILURE)
#if DEBUGGING
{if(DbgLvl>=3)
{DbgMsg();DbgDBf("FlowEL: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
				return(FAILURE);
#if DEBUGGING
}
#endif
	} while (TmpNst > 0);

	if (TraceM)				/* if tracing is on */
		EchoIt(*CBfPtr);		/* echo the character */

	--LStTop;				/* decrement nest count */
#if DEBUGGING
if(DbgLvl>=3)
{DbgMsg();DbgDBf("FlowEL: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
	return(SUCCESS);
}
