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

	ErrMsg()

	This function displays a TECO error message on the terminal screen.
This function is passed the number of the error message to be displayed and
possibly a text string to be inserted into the string.
	The EH flag controls how verbose the error message will be.  The two
least significant bits of the EH flag control how verbose the error message
is.  The third bit (mask is EH_COMMAND) causes the erroneous command string up
to and including the erroneous command to be displayed.
	The low two bits of EH have the following meanings:

	0	same as 2
	1	only the three letter code is output
	2	three letter code and one-line error message
	3	three letter code, one-lien message and paragraph

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

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

extern	VOID	DspChr();	/* type character on terminal */
extern	VOID	TypESt();	/* type error string on terminal */
extern	VOID	VrbErr();	/* display a verbose error message */
extern	VOID	ZAbort();	/* cleanup and exit TECO-C */
extern	VOID	ZDspCh();	/* output a character to the terminal */
extern	VOID	ZDspBf();	/* display a buffer on the terminal */

EXTERN	char	*ErrCod[];	/* three-letter error codes */
EXTERN	WORD	EtFlag;		/* ET mode control flag */
EXTERN	WORD	EhFlag;		/* EH mode control flag */
EXTERN	WORD	LstErr;		/* number of last error message */


VOID ErrMsg(ErrNum, StrArg)
WORD ErrNum;			/* the error number */
char *StrArg;			/* optional string argument */

{
	LOCAL	WORD	HlpLvl;
	LOCAL	char	*TmpPtr;
	LOCAL	char	*StartH;


	static char *LineTx[] = {
/*  0*/ "Improper arguments",
/*  1*/ "> not in iteration",
/*  2*/ "Confused use of conditionals",
/*  3*/ "Invalid device",
/*  4*/ "Delete too big",
/*  5*/ "File not found \"%s\"",
/*  6*/ "output command would have overflowed output device",
/*  7*/ "Illegal ^E command in search argument",
/*  8*/ "Illegal character \"%s\" after E",
/*  9*/ "Illegal character \"%s\" after F",
/* 10*/ "Illegal character \"%s\" in filename",
/* 11*/ "Illegal insert argument",
/* 12*/ "Illegal command \"%s\"",
/* 13*/ "Illegal number",
/* 14*/ "Negative or 0 argument to P",
/* 15*/ "Illegal \" character",
/* 16*/ "Illegal Q-register name \"%s\"",
/* 17*/ "Illegal radix argument to ^R",
/* 18*/ "Illegal search argument",
/* 19*/ "Illegal search string",
/* 20*/ "Illegal character \"%s\" following ^",
/* 21*/ "Missing '",
/* 22*/ "Memory overflow",
/* 23*/ "Missing left angle bracket",
/* 24*/ "Missing (",
/* 25*/ "Missing right angle bracket",
/* 26*/ "Missing )",
/* 27*/ "Missing start of conditional",
/* 28*/ "No argument before ^_ ",
/* 29*/ "No argument before ,",
/* 30*/ "No argument before =",
/* 31*/ "No argument before )",
/* 32*/ "No argument before \"",
/* 33*/ "No argument before ;",
/* 34*/ "No argument before U",
/* 35*/ "Negative argument to ,",
/* 36*/ "Numeric argument with Y",
/* 37*/ "No file for input",
/* 38*/ "No file for output",
/* 39*/ "Negative or 0 argument to P",
/* 40*/ "Not yet implemented",
/* 41*/ "Output file already open",
/* 42*/ "Attempt to pop an empty stack",
/* 43*/ "Push-down list overflow",
/* 44*/ "Attempt to move pointer off page with \"%s\"",
/* 45*/ "; not in iteration",
/* 46*/ "Search failure \"%s\"",
/* 47*/ "String too long",
/* 48*/ "Missing tag !%s!",
/* 49*/ "Unterminated command",
/* 50*/ "Unterminated macro",
/* 51*/ "Execution aborted",
/* 52*/ "Y command aborted",
/* 53*/ "No room for output"
	};


if (EhFlag & 3)					/* if any low bits are set */
	HlpLvl = EhFlag & 3;			/* mask low bits */
else						/* else (low bits are 0) */
	HlpLvl = 2;				/* default is 2 */

ZDspBf("\012?", 2);				/* line feed, question mark */
ZDspBf(ErrCod[ErrNum], 3);			/* three-letter error code */

if (HlpLvl >= 2)				/* if one-line message */
	{
	ZDspCh(TABCHR);
	TmpPtr = LineTx[ErrNum];
	while ((*TmpPtr != '\0') && (*TmpPtr != '%'))
		TmpPtr++;
	ZDspBf(LineTx[ErrNum], TmpPtr-LineTx[ErrNum]);
	if (*TmpPtr == '%')
		{
		while (*StrArg)			/* display %S string */
			DspChr(*StrArg++);
		TmpPtr++;			/* skip % */
		TmpPtr++;			/* skip s */
		StartH = TmpPtr;
		while (*TmpPtr)
			TmpPtr++;
		ZDspBf(StartH, TmpPtr-StartH);	/* display second half */
		}
	}

if (HlpLvl == 3)				/* if paragraph */
	VrbErr(ErrNum);				/* display paragraph */

ZDspBf("\015\012", 2);
if (EhFlag & EH_COMMAND)		/* if display of error string is on */
	TypESt();
if (EtFlag & ET_MUNG_MODE)			/* if we're in MUNG mode */
	ZAbort();
LstErr = ErrNum;
}
