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

	ZRdEI()

	This function reads an EI file.  It is passed an index for the file
data block of the open EI file,  and a buffer.  It reads the contents of the
file into the buffer,  stopping when an ESCAPE or end-of-file are encountered.

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

#include "ZPort.h"		/* define portability identifiers */
#include "ZFiles.h"		/* define file data block structures */

EXTERN	struct	IFDBST	IFiles[];
EXTERN	BOOLEAN	IsEofI[];



#ifdef UNKNOWN
DEFAULT ZRdEI()			/* read from an EI file */
{
	return(ExeNYI());
}
#endif



#ifdef vax11c
#include "DefTeco.h"		/* define general identifiers */
#include "DefChars.h"		/* define identifiers for characters */
#include "DefError.h"		/* define identifiers for error messages */
#include rmsdef			/* define RMS return status identifiers */
#include ssdef			/* define identifiers for system return codes */

int	sys$get();		/* RMS GET service */
int	sys$putmsg();		/* VAX/VMS "put message" system service*/
VOID	ZDspCh();		/* display a character on the terminal */

EXTERN	struct	message_vector msgvec;


VOID ZRdEI(IfIndx, ZBfBeg, ZBfEnd, ZBfPtr)
DEFAULT	IfIndx;			/* index into IFiles array */
char	*ZBfBeg;		/* beginning of EI file input buffer */
char	*ZBfEnd;		/* end of EI file input buffer */
char	*(*ZBfPtr);		/* returned ptr to end of EI string (+1) */
{
	char	*BufPtr;
	BOOLEAN	previous_char_was_escape;
	int	status;
	char	*TmpPtr;

#if DEBUGGING /***************************************************************/
DbgInd+=2;if(DbgLvl>=1){DbgMsg();DbgDBf("ZRdEI: called");
DbgDBf(", ZBfBeg = ");MakDBf(ZBfBeg, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf(", ZBfEnd = ");MakDBf(ZBfEnd, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf("\015\012");DbgROf();}
#endif /**********************************************************************/
	BufPtr = ZBfBeg;
	FOREVER
		{
		IFiles[IfIndx].IRab.rab$l_ubf = BufPtr;
		IFiles[IfIndx].IRab.rab$w_usz = ZBfEnd-BufPtr;
		status = sys$get(&IFiles[IfIndx].IRab);
		if (status == RMS$_EOF)			/* if end of file */
			{
			IsEofI[IfIndx] = YES;
			*ZBfPtr = BufPtr;
#if DEBUGGING /***************************************************************/
if(DbgLvl>=1){DbgMsg();
DbgDBf("ZRdEI: returning with eof = YES.\015\012");DbgROf();}DbgInd-=2;
#endif /**********************************************************************/
			return;
			}
		if (status != RMS$_NORMAL)
			{
			msgvec.msgcod = status;
			msgvec.rmsstv = IFiles[IfIndx].IRab.rab$l_stv;
			status = sys$putmsg(	&msgvec,/* message vector */
						0,	/* action routine */
						0);	/* facility name */
			if (status != SS$_NORMAL)
				lib$stop(status);
			ZDspCh(LINEFD);
			}
#if DEBUGGING /***************************************************************/
if(DbgLvl>=1){DbgMsg();DbgDBf("ZRdEI: ");
MakDBf(IFiles[IfIndx].IRab.rab$w_rsz, 10);
ZDspBf(DBfBeg, DBfPtr-DBfBeg);DbgDBf(" bytes read, string = \"");
TypBuf(BufPtr,BufPtr+IFiles[IfIndx].IRab.rab$w_rsz);
DbgDBf("\"\015\012");DbgROf();}
#endif /**********************************************************************/
		TmpPtr = BufPtr;
		BufPtr += IFiles[IfIndx].IRab.rab$w_rsz;
		previous_char_was_escape = NO;
		for (; TmpPtr<BufPtr; ++TmpPtr)
			{
			if (*TmpPtr == ESCAPE)
				if (previous_char_was_escape)
					{
					IsEofI[IfIndx] = NO;
					*ZBfPtr = BufPtr;
#if DEBUGGING /***************************************************************/
if(DbgLvl>=1){DbgMsg();
DbgDBf("ZRdEI: returning with eof = NO.\015\012");DbgROf();}DbgInd-=2;
#endif /**********************************************************************/
					return;
					}
				else
					previous_char_was_escape = YES;
			else
				previous_char_was_escape = NO;
			}
		if ((	(IFiles[IfIndx].IRab.rab$w_rsz > 0) &&
			(*(BufPtr-1L) == VRTTAB)
		    ) ||
		    (	(IFiles[IfIndx].IRab.rab$w_rsz > 2) &&
			(
			 (*(BufPtr-1L) == LINEFD) &&
		         (*(BufPtr-2L) == CRETRN) &&
		         (*(BufPtr-3L) == ESCAPE)
		        )
		    )
		   )
			;
		else
			{
			*BufPtr++ = CRETRN;	/* append a carriage return */
			*BufPtr++ = LINEFD;	/* append a line feed */
			}
		}
}
#endif



#ifdef XENIX
DEFAULT ZRdEI()			/* read from an EI file */
{
	return(ExeNYI());
}
#endif
