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

	ZDspCh()

	This function outputs a single character to the terminal.

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

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



#ifdef UNKNOWN
VOID ZDspCh(Charac)		/* output a character to terminal */
char Charac;
{
	puts("Terminating in function ZDspCh.\n");
	exit(1);
}
#endif



#ifdef vax11c
#include "DefTeco.h"		/* define general identifiers */
#include "ChrMacs.h"		/* define character processing macros */
#include "DefChars.h"		/* define identifiers for characters */
#include iodef			/* define i/o code identifiers */
#include rab			/* define RMS record access block structures */
#include rmsdef			/* define RMS return status identifiers */
#include ssdef			/* define system return code identifiers */

VOID	lib$stop();		/* terminate image with stack dump */
int	sys$qiow();		/* VAX/VMS queue i/o request system service */
int	sys$put();		/* RMS PUT service */

EXTERN	char	TOBBeg[];	/* terminal output buffer */
EXTERN	char	*TOBEnd;	/* terminal buffer end (+1) */
EXTERN	char	*TOBPtr;	/* terminal buffer pointer */
EXTERN	short	TOChan;		/* terminal channel */
EXTERN	struct	RAB TORab;	/* output RMS record access block */


VOID ZDspCh(Charac)		/* output a character to the terminal */
char Charac;
{
	int	status;
	struct	io_status_block iosb;



	if (TOChan)				/* if it's a terminal */
		{
		status = sys$qiow(
				TERM_OUT_EFN,	/* event flag number */
				TOChan,		/* channel */
				IO$_WRITEVBLK,	/* I/O func */
				&iosb,		/* I/O status block */
				0,		/* ast routine address */
				0,		/* ast parameter */
				&Charac,	/* p1 */
				1,		/* p2 */
				0,		/* p3 */
				0,		/* p4 */
				0,		/* p5 */
				0);		/* p6 */

		if (status != SS$_NORMAL)
			lib$stop(status);
		if ((iosb.io_status != SS$_NORMAL) &&
		     (iosb.io_status != SS$_CONTROLO))
			lib$stop(iosb.io_status);
		}
	else				/* else it's not a terminal */
		if (IsEOL(Charac))
			{
			if (Charac == LINEFD)
				if (TOBPtr > TOBBeg)
					if (*(TOBPtr-1L) == CRETRN)
						--TOBPtr;
			else
				{
				*TOBPtr = Charac;
				if (++TOBPtr > TOBEnd)
					lib$stop(SS$_NORMAL);
				}
			TORab.rab$w_rsz = TOBPtr - TOBBeg;
			status = sys$put(&TORab);	/* output the record */
			if (status != RMS$_NORMAL)	/* if it didn't work */
				lib$stop(status, TORab.rab$l_stv);
			TOBPtr = TOBBeg;
			}
		else
			{
			*TOBPtr = Charac;
			if (++TOBPtr > TOBEnd)
				lib$stop(SS$_NORMAL);
			}
}
#endif



#ifdef XENIX

EXTERN	int	tty_fd;

extern	VOID	ZAbort();	/* clean up and terminate TECOC */

VOID ZDspCh(Charac)		/* output a character to terminal */
char Charac;
{
	if (write(tty_fd, &Charac, 1) == -1)
		{
		puts("Unable to write to terminalin function ZDspCh");
		ZAbort();
		}
}
#endif
