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

	IniOpn()

	This function opens input and output files based on the contents of
the command line used to invoke TECO-C.
*****************************************************************************/

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

extern	DEFAULT	ExeEY();	/* execute an EY command */
extern	VOID	ZAbort();	/* cleanup and exit */
extern	VOID	ZDspBf();	/* display a buffer on the terminal */
extern	DEFAULT	ZMkOFN();	/* make output file name */
extern	DEFAULT	ZOpInp();	/* open an input file */
extern	DEFAULT	ZOpOut();	/* open output file */
extern	VOID	ZStoFl();	/* store last-file-edited file name */

EXTERN	DEFAULT	CurInp;		/* index of current input stream in IFiles */
EXTERN	DEFAULT	CurOut;		/* index of current output stream in OFiles */
EXTERN	char	FBfBeg[];	/* file specification buffer */
EXTERN	char	*FBfPtr;	/* pointer into file specification buffer */
EXTERN	BOOLEAN	FileDf;		/* was a file specification parsed? */
EXTERN	char	LBfBeg[];	/* last-file-editer file name buffer */
EXTERN	char	*LBfPtr;	/* pointer into LBfBeg */
EXTERN	BOOLEAN	MemDf;		/* remembered filespec used? */
EXTERN	BOOLEAN	Swit_C;		/* was a C command line switch parsed? */
EXTERN	BOOLEAN	Swit_M;		/* was an M command line switch parsed? */
EXTERN	BOOLEAN	Swit_R;		/* was an R command line switch parsed? */



DEFAULT IniOpn()		/* initial file opening */
{
	LONG	TmpSiz;

/*****************************************************************************
	handle -r (readonly) command line switch
*****************************************************************************/

#if DEBUGGING
DbgInd+=2;if(DbgLvl>=1){DbgMsg();DbgDBf("IniOpn: called\015\012");DbgROf();}
#endif

	if (Swit_R)				/* if "readonly" */
		{
		if (!FileDf && !MemDf)
			{
			ZDspBf("How can I open nothing for", 26);
			ZDspBf(" reading?\015\012", 11);
			ZAbort();
			}
		if (ZOpInp(CurInp, NO) == FAILURE)
			ZAbort();
		if (ExeEY() == FAILURE)
			ZAbort();
		if (MemDf)
			{
			ZDspBf("Inspecting file \"", 17);
			ZDspBf(LBfBeg, LBfPtr-LBfBeg);
			ZDspBf("\"\015\012", 3);
			}
		}

/*****************************************************************************
	handle the default command line stuff
*****************************************************************************/

	else if (FileDf || MemDf)
		{
		TmpSiz = FBfPtr - FBfBeg;	/* size of file name */
		ZCpyBl(TmpSiz, FBfBeg, LBfBeg);	/* copy FBf to LBf */
		LBfPtr = LBfBeg + TmpSiz;	/* set end-of-string */
		if (MemDf)
			{
			ZDspBf("Editing file \"", 14);
			ZDspBf(FBfBeg, FBfPtr-FBfBeg);
			ZDspBf("\"\015\012", 3);
			}
		if (ZOpInp(CurInp, NO) == FAILURE)
			{
			if (Swit_C)		/* if don't auto-create */
				ZAbort();
			ZDspBf("Creating new file\015\012", 19);
			if (ZOpOut(CurOut) == FAILURE)
				ZAbort();
			}
		else
			{
			if (ZMkOFN() == FAILURE)
				ZAbort();
			if (ZOpOut(CurOut) == FAILURE)
				ZAbort();
			if (ExeEY() == FAILURE)
				ZAbort();
			}
		if (!Swit_M)			/* if ok to use memory */
			ZStoFl(LBfBeg, LBfPtr);	/* remember file name */
		}
#if DEBUGGING
if(DbgLvl>=1)
{DbgMsg();DbgDBf("IniOpn: returning\015\012");DbgROf();}DbgInd-=2;
#endif
}
