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

	GetAra()

	This function is used by commands which can be preceeded by two
arguments which specify an "area" of the edit buffer.  For instance,  the "T"
command can have the form "m,nT",  which will cause the characters between the
mth and nth character of the edit buffer to be typed on the terminal screen.
All commands which support m,n arguments call this function to compute the
addresses of the m and nth characters.  The resulting addresses are left
in the global variables AraBeg and AraEnd.

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

#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	char	*AraBeg;	/* beginning of text argument */
EXTERN	char	*AraEnd;	/* end of m,n area */
EXTERN	char	*EBfBeg;	/* beginning of edit buffer */
EXTERN	char	*EBfEnd;	/* end of edit buffer */
EXTERN	char	ErrTxt[];	/* holds part of error text */
EXTERN	char	*GapBeg;	/* beginning of edit buffer gap */
EXTERN	char	*GapEnd;	/* end of edit buffer gap */
EXTERN	LONG	MArgmt;		/* m part of m,n numeric arguments */
EXTERN	LONG	NArgmt;		/* numeric argument */


DEFAULT GetAra()		/* get m,n addresses */
{
	LOCAL LONG	TmpLng;

#if DEBUGGING
DbgInd+=2;if(DbgLvl>=4){DbgMsg();DbgDBf("GetAra: called");
DbgDBf(", MArgmt = ");MakDBf(MArgmt, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf(", NArgmt = ");MakDBf(NArgmt, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf("\015\012");DbgROf();}
#endif

/* check for negative n.  m can't be negative; it's checked in ExeCom */

	if (NArgmt < 0L)			/* if negative n */
		{
		ErrMsg(ERR_POP, ErrTxt);	/* POP = pointer off page */
#if DEBUGGING
if(DbgLvl>=4)
{DbgMsg();DbgDBf("GetAra: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
		return(FAILURE);
		}

	if (MArgmt > NArgmt)			/* if wrong order */
		{
		TmpLng = NArgmt;
		NArgmt = MArgmt;
		MArgmt = TmpLng;
		}

	AraBeg = EBfBeg + MArgmt;		/* compute area beginning */
	if (AraBeg > GapBeg-1L)			/* if past start of gap */
		AraBeg += (GapEnd-GapBeg) + 1L;	/* correct for gap */
	AraEnd = EBfBeg + NArgmt - 1L;		/* compute area end */
	if (AraEnd > (GapBeg-1L))		/* if before end of gap */
		AraEnd += (GapEnd-GapBeg) + 1L;	/* correct for gap */

	if ((AraBeg > EBfEnd) ||		/* if m or n too large */
	    (AraEnd > EBfEnd))
		{
		ErrMsg(ERR_POP, ErrTxt);	/* POP = pointer off page */
#if DEBUGGING
if(DbgLvl>=4)
{DbgMsg();DbgDBf("GetAra: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
		return(FAILURE);
		}

#if DEBUGGING
if(DbgLvl>=4)
{DbgMsg();DbgDBf("GetAra: returning SUCCESS");
DbgDBf(", AraBeg = ");MakDBf(AraBeg, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf(", AraEnd = ");MakDBf(AraEnd, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf("\015\012");DbgROf();}DbgInd-=2;
#endif
	return(SUCCESS);
}
