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

	SSerch()

	This routine does a "simple" search.  It is passed a direction
and the starting and ending addresses of the contiguous area to be searched.
It sets Matchd, which indicates whether the search was successful or not.
If it was successful, EBPtr2 is left pointing to the last character of the
found string.  Note that if the direction is backwards, the address of the
beginning of the search area will be greater than the address of the end of
the search area.

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

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

extern	DEFAULT	BakSrc();	/* backwards search to match 1st character */
extern	DEFAULT	CMatch();	/* match a character */
extern	DEFAULT	ZFrSrc();	/* forward search to match 1st character */

EXTERN	char	*EBPtr1;
EXTERN	char	*EBPtr2;
EXTERN	char	*EndSAr;	/* end of search area */
EXTERN	BOOLEAN	Matchd;		/* indicates successful search */
EXTERN	LONG	RefLen;		/* value returned by ^S command */
EXTERN	char	*RhtSid;	/* right-hand end of area to be searched */
EXTERN	char	*SBfBeg;	/* beginning of search buffer */
EXTERN	char	*SBfPtr;	/* end of search string */
EXTERN	LONG	SIncrm;		/* 1 means forward, -1 means backward */
EXTERN	char	*SStPtr;	/* pointer into search string */


DEFAULT SSerch()		/* search for 1 occurrence of a string */
{
	LOCAL	BOOLEAN	SamChr;		/* same character indicator */

#if DEBUGGING
DbgInd+=2;if(DbgLvl>=3){DbgMsg();DbgDBf("SSerch1: called");
DbgDBf(", SIncrm = ");MakDBf(SIncrm, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf(", EBPtr1 = ");MakDBf(EBPtr1, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf(", EndSAr = ");MakDBf(EndSAr, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf("\015\012");DbgROf();}
if(DbgLvl>=3){DbgMsg();DbgDBf("SSerch2: search string = \"");
TypBuf(SBfBeg, SBfPtr);DbgDBf("\"\015\012");DbgROf();}
#endif

	if (SBfPtr == SBfBeg)			/* if null search string */
		{
		Matchd = NO;
#if DEBUGGING
if(DbgLvl>=3){DbgMsg();
DbgDBf("SSerch3: returning SUCCESS, Matchd = NO\015\012");DbgROf();}DbgInd-=2;
#endif
		return(SUCCESS);
		}

	FOREVER
		{
		SStPtr = SBfBeg;

/* find a character which matches the first character of the search string */

		if (SIncrm == 1L)		/* if forward search */
			{
			if (ZFrSrc() == FAILURE)
#if DEBUGGING
{if(DbgLvl>=3){DbgMsg();DbgDBf("SSerch4: returning FAILURE\015\012");
DbgROf();}DbgInd-=2;
#endif
				return(FAILURE);
#if DEBUGGING
}
#endif
			if (EBPtr1 > EndSAr)		/* if not found */
				{
				Matchd = NO;
#if DEBUGGING
if(DbgLvl>=3){DbgMsg();
DbgDBf("SSerch5: returning SUCCESS, Matchd = NO\015\012");DbgROf();}DbgInd-=2;
#endif
				return(SUCCESS);
				}
			}
		else
			{
			if (BakSrc() == FAILURE)
#if DEBUGGING
{if(DbgLvl>=3){DbgMsg();DbgDBf("SSerch6: returning FAILURE\015\012");
DbgROf();}DbgInd-=2;
#endif
				return(FAILURE);
#if DEBUGGING
}
#endif
			if (EBPtr1 < EndSAr)		/* if not found */
				{
				Matchd = NO;
#if DEBUGGING
if(DbgLvl>=3){DbgMsg();
DbgDBf("SSerch7: returning SUCCESS, Matchd = NO\015\012");DbgROf();}DbgInd-=2;
#endif
				return(SUCCESS);
				}
			}

/* first char is matched,  now check rest of search string */

		do
			{
			if (++SStPtr == SBfPtr)	/* if no more to check */
				{
				RefLen = EBPtr1 - EBPtr2;
				RefLen--;
				Matchd = YES;
#if DEBUGGING
if(DbgLvl>=3){DbgMsg();
DbgDBf("SSerch8: returning SUCCESS, Matchd = YES\015\012");DbgROf();}DbgInd-=2;
#endif
				return(SUCCESS);
				}
			if (++EBPtr2 > RhtSid)	/* inc and check for eob */
				{
				Matchd = NO;
#if DEBUGGING
if(DbgLvl>=3){DbgMsg();
DbgDBf("SSerch9: returning SUCCESS, Matchd = NO\015\012");DbgROf();}DbgInd-=2;
#endif
				return(SUCCESS);
				}
			if (CMatch(&SamChr) == FAILURE)
#if DEBUGGING
{if(DbgLvl>=3){DbgMsg();DbgDBf("SSerch10: returning FAILURE\015\012");
DbgROf();}DbgInd-=2;
#endif
				return(FAILURE);
#if DEBUGGING
}
#endif
			}
		while (SamChr);

/* string only partially matched,  so keep looking */

		if (SIncrm == 1L)		/* if forward search */
			{
			if (++EBPtr1 > EndSAr)
				{
				Matchd = NO;
#if DEBUGGING
if(DbgLvl>=3){DbgMsg();
DbgDBf("SSerch11: returning SUCCESS, Matchd = NO\015\012");DbgROf();}DbgInd-=2;
#endif
				return(SUCCESS);
				}
			}
		else
			if (--EBPtr1 < EndSAr)
				{
				Matchd = NO;
#if DEBUGGING
if(DbgLvl>=3){DbgMsg();
DbgDBf("SSerch12: returning SUCCESS, Matchd = NO\015\012");DbgROf();}DbgInd-=2;
#endif
				return(SUCCESS);
				}
		}				/* end of FOREVER loop */
}
