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

	ZAbort()

	This function cleans up and terminates TECO-C.  It must:

	1.  close any open input files
	2.  close and delete any open output files
	3.  disconnect from terminal input and output device(s)
	4.  exit

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

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

extern	VOID	exit();		/* standard C library function */
extern	VOID	ZIClos();	/* close current input file */
extern	VOID	ZOClDe();	/* close and delete output file */





#ifdef UNKNOWN
VOID ClTrEx()			/* close terminal channels and exit */
{
	puts("Failing in function ZAbort.\n");
	exit(1);
}
#endif



#ifdef vax11c
#include fab		/* define RMS file access block structures */
#include rab		/* define RMS record access block structures */
#include rmsdef		/* define RMS return status identifiers */
#include ssdef		/* define system service status return identifiers */

VOID	lib$stop();		/* terminate image with stack dump */
int	sys$close();		/* VAX RMS "close file" service */
int	sys$dassgn();		/* VAX/VMS "deassign device" system service */
int	sys$put();		/* RMS PUT service */

EXTERN	short	TCChan;		/* terminal command channel */
EXTERN	short	TIChan;		/* terminal input channel */
EXTERN	struct	FAB TIFab;	/* terminal input file access block */
EXTERN	char	TOBBeg[];	/* terminal output buffer */
EXTERN	char	*TOBPtr;	/* terminal buffer pointer */
EXTERN	short	TOChan;		/* terminal output channel */
EXTERN	struct	FAB TOFab;	/* terminal output file access block */
EXTERN	struct	RAB TORab;	/* output RMS record access block */


VOID ClTrEx()			/* close terminal channels and exit */
{
	int status;

#if DEBUGGING
if(DbgLvl>=1){DbgMsg();
DbgDBf("ClTrEx: closing terminal channels and exiting.\015\012");
DbgROf();}DbgInd-=2;
#endif
	if (TIChan)				/* if it's a terminal */
		{
		status = sys$dassgn(TIChan);	/* de-assign the channel */
		if (status != SS$_NORMAL)
			lib$stop(status);
		}
	else					/* else process-perm file */
		{
		status = sys$close(&TIFab);	/* close the file */
		if (status != RMS$_NORMAL)
			lib$stop(status);
		}
	if (TOChan)				/* if it's a terminal */
		{
		status = sys$dassgn(TOChan);	/* de-assign the channel */
		if (status != SS$_NORMAL)
			lib$stop(status);
		}
	else					/* else process-perm file */
		{
		TORab.rab$w_rsz = (TOBPtr-TOBBeg) - 1L;
		status = sys$put(&TORab);	/* output the record */
		if (status != RMS$_NORMAL)	/* if it didn't work */
			lib$stop(status, TORab.rab$l_stv);
		status = sys$close(&TOFab);	/* close the file */
		if (status != RMS$_NORMAL)
			lib$stop(status);
		}
	if (TCChan)				/* if it was assigned */
		{
		status = sys$dassgn(TCChan);	/* de-assign the channel */
		if (status != SS$_NORMAL)
			lib$stop(status);
		}

	exit(SS$_NORMAL);
}
#endif




#ifdef XENIX
#include <sgtty.h>

EXTERN	struct	sgttyb tty_cb;	/* terminal characteristics block */
EXTERN	int	savd_tty_flags;	/* saved terminal flags */
EXTERN	BOOLEAN	tty_opened;	/* YES if tty_fd is valid */
EXTERN	int	tty_fd;		/* terminal file descriptor */
EXTERN	BOOLEAN tty_set;	/* YES if the terminal has been set */


VOID ClTrEx()			/* cleanup for TECO-C abort */
{
#if DEBUGGING
DbgInd+=2;if(DbgLvl>=1)
{DbgMsg();DbgDBf("ClTrEx: called.\015\012");DbgROf();}
#endif

	if (tty_opened)			/* if the terminal has been opened */
		{
		if (tty_set)		/* if characteristics were changed */
			{
			tty_cb.sg_flags = savd_tty_flags;
			if (stty(tty_fd, &tty_cb) != 0)
				{
			puts("Unable to reset terminal characteristics");
				exit(1);
				}
			}
		if (close(tty_fd) == -1)
			{
			puts("Unable to close terminal file");
			exit(1);
			}
		}
	exit(0);
}
#endif



VOID ZAbort()			/* cleanup for TECO-C abort */
{
#if DEBUGGING
if(DbgLvl>=1){DbgMsg();
DbgDBf("ZAbort: called\015\012");
DbgROf();}DbgInd-=2;
#endif

/******************************************************************************
	Close the primary and secondary input streams if they are open.  Close
and delete the primary and secondary output streams,  if they are open.
******************************************************************************/

	ZIClos(PINPFL);
	ZIClos(SINPFL);
	ZOClDe(POUTFL);
	ZOClDe(SOUTFL);

/******************************************************************************
	Close the terminal channel(s) and exit the program with a successful
completion indicator.
******************************************************************************/

	ClTrEx();
}
