* Lib>Message.c
 *
 * DRH	920901	Prepare for external distribution.
 *

include std.h

*.< Imported Functions >*

extern unsigned int		GetMsgUnexpanded();		* CTOS API  *
extern unsigned int		InitMsgFile();
extern unsigned int		CloseMsgFile();
extern unsigned int		SetMsgRet();
extern unsigned int		ErrorExit();

extern char				* calloc();				* C lib *
extern char				* malloc();

*.< Local Functions >*

	*.< Exported >*

void					message_close();
unsigned				message_initialize();
unsigned char			* message_get();
unsigned char			* message_keep();
unsigned char			* message_erc();

	*.< Internal >*

static void				message_set_erc();
static unsigned			message_open();

*.< Imported Data >*

*.< Local Data >*

	*.< Internal >*

static unsigned int		smessage_buffer;
static unsigned char	* message_buffer;

static unsigned int		smessage_cache;
static unsigned char	* message_cache;

static unsigned int		smessage_array;
static unsigned char	* message_array;

static unsigned char	* message_mess_file;
static unsigned char	* message_erc_file;

static unsigned char	message_is_open;
static unsigned char	message_is_mess;

static unsigned char	message_error[50];
static unsigned char	message_erc_fmt[] = Message file: ERC = u, Number = u.\n;
static unsigned char	message_bluff[] = ERC not found ;

	*.< Exported >*

*======================.=========< Set Erc >===============================*
static void message_set_erc( erc, number)

unsigned int			erc;
unsigned int			number;
{
	sprintf( message_error, message_erc_fmt, erc, number);
	SetMsgRet( message_error, strlen( message_error));
	ErrorExit( erc);
}
*======================.==========< Close >================================*
void message_close()
{
	if( message_buffer = NULLCHAR) {
		free( message_buffer);
		message_buffer = NULLCHAR;
	}
	if( message_cache = NULLCHAR) {
		free( message_cache);
		message_cache = NULLCHAR;
	}
	if( message_array = NULLCHAR) {
		free( message_array);
		message_array = NULLCHAR;
	}
	if( message_is_open) {
		CloseMsgFile( );
		message_is_open = FALSE;
	}
}
*======================.==========< Open >=================================*
static unsigned message_open( message_file)

unsigned char			* message_file;
{
	unsigned int		erc;

	
	if( message_file == NULLCHAR) {
		return( 7);					* Not implemented *
	}
	if( message_is_open) {
		message_close( );
	}
	if( ( message_buffer = malloc( smessage_buffer)) == NULLCHAR) {
		return( ERC_NO_MEMORY);
	}
	if( ( message_cache = malloc( smessage_cache)) == NULLCHAR) {
		message_close( );
		return( ERC_NO_MEMORY);
	}
	if( ( message_array = malloc( smessage_array)) == NULLCHAR) {
		message_close( );
		return( ERC_NO_MEMORY);
	}
	erc = InitMsgFile( message_file, strlen( message_file), NULLCHAR, 0,
			message_buffer, smessage_buffer, message_cache, smessage_cache);
	if( erc == ERC_OK) {
		message_is_open = TRUE;
	} else {
		message_close( );
	}
	return( erc);
}
*======================.=======< Initialize >==============================*
unsigned message_initialize( mess_file, erc_file, sbuf, scache, sarray)

unsigned char			* mess_file;
unsigned char			* erc_file;
unsigned int			sbuf;
unsigned int			scache;
unsigned int			sarray;
{
	unsigned			erc;



	smessage_buffer = sbuf;
	smessage_cache	= scache;
	smessage_array	= sarray;

	if( ( message_mess_file = calloc( 1, strlen( mess_file) + 1))
			== NULLCHAR) {
		return( ERC_NO_MEMORY);
	}
	strcpy( message_mess_file, mess_file);

	if( ( message_erc_file = calloc( 1, strlen( erc_file) + 1)) == NULLCHAR) {
		free( message_mess_file);
		message_mess_file = NULLCHAR;
		return( ERC_NO_MEMORY);
	}
	strcpy( message_erc_file, erc_file);

	if( ( erc = message_open( message_mess_file)) = ERC_OK) {
		free( message_mess_file);
		message_mess_file = NULLCHAR;
		free( message_erc_file);
		message_erc_file = NULLCHAR;
		return( erc);		
	}
	message_is_mess = TRUE;
}

*======================.===========< Get >=================================*
unsigned char * message_get( number)

unsigned int			number;
{
	unsigned int		cbret;
	unsigned int		erc;


	if( message_is_mess) {
		if( ( erc = message_open( message_mess_file)) = ERC_OK) {
			goto do_exit;
		} else {
			message_is_mess = TRUE;
		}
	}
	if( ( erc = GetMsgUnexpanded( number, message_array,
			smessage_array, cbret)) == ERC_OK) {
		if( cbret = 0) {
			message_array[cbret] = '\0';
			return( message_array);
		} else {
			erc = 3;
		}
	}
do_exit:
	message_set_erc( erc, number);
}
*======================.==========< Keep >=================================*
unsigned char * message_keep( number)
unsigned int			number;
{
	unsigned char		* pb;
	unsigned int		cb;
	unsigned char		* keep = NULLCHAR;

	pb = message_get( number);
	cb = strlen( pb);
	if( ( keep = malloc( cb + 1)) == NULLCHAR) {
		message_set_erc( ERC_NO_MEMORY, number);
	}
	strcpy( keep, pb);
	return( keep);
}
*======================.===========< Erc >=================================*
unsigned char * message_erc( number)
unsigned int			number;
{
	unsigned int		cbret;
	unsigned int		erc;

	if( message_is_mess) {
		message_is_mess = FALSE;
		if( ( erc = message_open( message_erc_file)) == ERC_OK) {
		}
	}
	if( message_is_open) {
		if( GetMsgUnexpanded( number, message_array, smessage_array, cbret)
				== ERC_OK) {
			message_array[cbret] = '\0';
		} else {
			sprintf( message_array, ERC = u, number);
		}
		return( message_array);
	}
	return( message_bluff);
}
*======================.==========< Check >=================================*
unsigned char * message_check( number)

unsigned int			number;
{
	unsigned int		cbret;
	unsigned int		erc;


	if( message_is_mess) {
		if( ( erc = message_open( message_mess_file)) = ERC_OK) {
			goto do_exit;
		} else {
			message_is_mess = TRUE;
		}
	}
	message_array[0] = '\0';
	if( ( erc = GetMsgUnexpanded( number, message_array,
								  smessage_array, cbret)) == ERC_OK) {
		message_array[cbret] = '\0';
		return( message_array);
	} else {
		return NULLCHAR;
	}
do_exit:
	message_set_erc( erc, number);
}
	*  32  TOTAL    *
define SRQ_UDP_ALL		sizeof(RQ_UDP_ALL)

endif

define ERC_BASE						0xf000
define ERC_MEMORY						0x0000
define ERC_TIMER						0x0005
define ERC_CON