/* Small library of routines to manage text messages */

#include <exec/types.h>
#include <exec/memory.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <st/textmessage.h>
#include <st/st_proto.h>

/*********************************************************/
 void TextMessageReply(struct TextMessage **txtmsg)
/*********************************************************
     Safely reply or free a text message structure
             and the associated string
 NOTE: pass a pointer to the pointer to the text message
 since this routine needs to NULLify the pointer itself
**********************************************************/
{
	if(*txtmsg != NULL) {
		SafeFreeString0(&((*txtmsg)->String));
		if(TMREPLYPORT(*txtmsg))
			ReplyMsg((struct Message *)(*txtmsg));
		else FreeMem((*txtmsg), sizeof(struct TextMessage));
		(*txtmsg) = NULL;
	}
}


/*********************************************************/
 void  TextMessageInitReply(struct TextMessage *txtmsg)
/*********************************************************
  Initialize the reply string part of the text message
**********************************************************/
{
	AllocString0(&(txtmsg->RepString),0);	/* 0 reply string */
}

/*********************************************************/
 void TextMessageCleanup(struct MsgPort *ourport)
/*********************************************************
     Cleanup any pending text messages on the port
**********************************************************/
{
struct TextMessage *currtxtmsg;
	if(!ourport) return;
	Forbid();
	while (currtxtmsg = (struct TextMessage *)GetMsg(ourport))
								TextMessageReply(&currtxtmsg);
	DeletePort(ourport);
	Permit();
}

/*********************************************************/
void TextMessageSafeCleanup(struct MsgPort **ourport)
/*********************************************************
     Cleanup any pending text messages on the port
**********************************************************/
{ TextMessageCleanup(*ourport); *ourport = NULL; }


