/*
*
*       XCMDTools.c
*
*       Useful routines for XCMD
*
*
*/
#include <exec/types.h>
#include <exec/memory.h>
#include <exec/ports.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <stdio.h>
#include "XCMD.h"
#include "XCMDTools.h"


/*
*       CreateXCMD()
*
*       Create a XCMD. Memory is allocated. Memory will be
*       freed with a DeleteXCMD(). The port given to this
*       function is the port of external processus, that is
*       the port which will receive answered messages from
*       Term.
*
*/
struct XCMD *CreateXCMD(struct MsgPort *port)
  {
  struct XCMD *xcmd = AllocMem(sizeof(struct XCMD),MEMF_PUBLIC|MEMF_CLEAR);
  if(xcmd)
    {
    xcmd->xcmd_Message.mn_Length = sizeof(struct XCMD);
    xcmd->xcmd_Message.mn_ReplyPort = port;
    }
  return(xcmd);
  }


/*
*       DeleteXCMD()
*
*       Delete a XCMD. Free memory allocated by CreateXCMD
*
*/
void DeleteXCMD(struct XCMD *xcmd)
  {
  FreeMem(xcmd,sizeof(struct XCMD));
  }


/*
*       SendXCMD()
*
*       Send a XCMD to Term II. Return TRUE if succeded,
*       FALSE otherwise.
*
*/
BOOL SendXCMD(struct XCMD *xcmd)
  {
  BOOL sent = FALSE;
  struct MsgPort *TermPort;
  Forbid() ;
    if(TermPort = FindPort("XTERM"))
      {
      PutMsg(TermPort,xcmd);
      sent = TRUE;
      }
  Permit();
  return(sent);
  }

