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

	ZIniFl()

	This function executes a user-written initialization file,  if one
exists.

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

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

extern	DEFAULT	ExeCSt();	/* execute a command string */
extern	VOID	TmpEI();	/* execute temporary EI command */
extern	char	*ZAlloc();	/* allocate memory */
extern	VOID	ZCpyBl();	/* copy a block of memory */
extern	VOID	ZFree();	/* free memory */

EXTERN	char	*CBfPtr;	/* pointer into command buffer */
EXTERN	char	*CStEnd;	/* pointer to last char of command string */
EXTERN	WORD	EStBot;		/* expression stack bottom */
EXTERN	WORD	EStTop;		/* expression stack top */
EXTERN	char	*FBfPtr;	/* pointer into file specification buffer */




#ifdef UNKNOWN
DEFAULT ZIniFl()	/* execute initialization file, if any */
{
	puts("Terminating in function ZIniFl.\n");
	exit(1);
}
#endif





#ifdef vax11c
/*****************************************************************************
	TECO performs initialization by attempting to translate the logical
name TEC$INIT.  If this name does not translate,  no special initiaization is
done.  If it translates to a string of the form $filespec (where "$" is a
dollar sign),  TECO executes the commands in the file.  If TEC$INIT translates
to any other string,  TECO executes that string as TECO commands.
*****************************************************************************/
#include descrip	/* define string descriptor definition macros */
#include ssdef		/* define system service status return identifiers */

VOID	lib$stop();	/* terminate image with stack dump */
int	sys$trnlog();	/* VAX/VMS translate logical name system service */

EXTERN	char	FBfBeg[];		/* file specification buffer */


DEFAULT ZIniFl()	/* execute initialization file, if any */
{
#define equ_str_size 1024		/* equivalence string buffer size */
	readonly $DESCRIPTOR(init_descriptor,"TEC$INIT");
	char	equ_str[equ_str_size];	/* equivalence string buffer */
	struct dsc$descriptor_s RSL_desc =
		{
		equ_str_size,		/* dsc$w_length */
		DSC$K_DTYPE_T,		/* dsc$b_dtype */
		DSC$K_CLASS_S,		/* dsc$b_class */
		equ_str			/* dsc$a_pointer */
		};
	int	i;
	short	length;
	int	status;
	char	*SavBfr;
	LONG	SavSiz;


#if DEBUGGING
DbgInd+=2;if(DbgLvl>=1){DbgMsg();DbgDBf("ZIniFl: called\015\012");DbgROf();}
#endif
	status = sys$trnlog(	&init_descriptor,	/* logical name */
				&length,	/* returned string length */
				&RSL_desc,	/* returned string buffer */
				0,		/* logical name table */
				0,		/* access mode */
				3);		/* table search mask */
	if (status == SS$_NOTRAN)		/* if logical doesn't exist */
		return;				/* we're done */
	if (status != SS$_NORMAL)		/* anything else wrong? */
		lib$stop(status);		/* then terminate */
	if (length == 0)			/* if it's there but empty */
		return;				/* we're done */
/*
  If the first character of the equivalence string is a dollar sign,  then
  the rest of the string is a file name.  Call TmpEI to build an EI command
  containing the file name and execute it.  Since TmpEI uses the file name
  that's in the FBf buffer,  we need to set the FBf buffer before calling
  TmpEI.  Since the FBf buffer might contain a file name,  we have to save
  the old name,  put in the new one,  call TmpEI,  and restore the old one.
*/
	if ((length > 1) && (equ_str[0] == '$'))/* if it's a file name */
		{
		SavSiz = FBfPtr - FBfBeg;	/* get current name size */
		SavBfr = ZAlloc(SavSiz);	/* allocate a save buffer */
		if (SavBfr == NULL)		/* if allocation failed */
			{
			ErrMsg(ERR_MEM);	/* MEM = memory overflow */
			ZAbort();
			}
		ZCpyBl(SavSiz, FBfBeg, SavBfr);	/* copy FBf to SavBfr */
		length--;
		ZCpyBl(length, &equ_str[1], FBfBeg);/* load file name buffer */
		FBfPtr = FBfBeg + length;
		TmpEI();			/* do temporary EI command */
		ZCpyBl(SavSiz, SavBfr, FBfBeg);	/* restore the file name */
		FBfPtr = FBfBeg + SavSiz;	/* restore FBfPtr */
		ZFree(SavBfr);
		}
	else
		{
		CBfPtr = equ_str;		/* command string start */
		length--;
		CStEnd = &equ_str[length];	/* command string end */
		EStTop = EStBot;		/* clear expression stack */
		ExeCSt();			/* execute command string */
		}
#if DEBUGGING
if(DbgLvl>=1)
{DbgMsg();DbgDBf("ZIniFl: returning\015\012");DbgROf();}DbgInd-=2;
#endif
}
#endif




#ifdef XENIX
DEFAULT ZIniFl()	/* execute initialization file, if any */
{
#if DEBUGGING
DbgInd+=2;if(DbgLvl>=1){DbgMsg();DbgDBf("ZIniFl: called\015\012");DbgROf();}
#endif

#if DEBUGGING
if(DbgLvl>=1)
{DbgMsg();DbgDBf("ZIniFl: returning\015\012");DbgROf();}DbgInd-=2;
#endif
}
#endif
