/*-- Rev Header - do NOT edit!
 *
 *  Filename : CED_Support.c
 *  Purpose  : Einige Hilfsroutinen für CED
 *
 *  Program  : -
 *  Author   : Gerhard Müller
 *  Copyright: (c) by Gerhard Müller
 *  Creation : Fri Sep 10 00:41:01 1993
 *
 *  compile  : makefile
 *
 *  Compile version  : 0.1
 *  Ext. Version     : 0.1
 *
 *  REVISION HISTORY
 *
 *  Date                     Comment
 *  ------------------------ -------------------------------------------------
 *  Fri Sep 17 00:34:04 1993 Taken from CED_Help, works only with CED 3.5 and
 *                           above
 *
 *
 *
 *-- REV_END --
 */

	/*
	 * C-Includes, C-Definitionen
	 *
	 */

#include <exec/types.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/exall.h>
#include <clib/alib_protos.h>
#include <clib/alib_stdio_protos.h>
#include <rexx/storage.h>
#include <inline/stubs.h>
#ifdef __OPTIMIZE__
#include <inline/exec.h>
#include <inline/dos.h>
#else
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#endif

#include "CED_Support.h"

struct RexxMsg	TheMessage;


/*
	This   routine   sends   an   ascii   string  command  to  CygnusEd
Professional.   It  deals with the messy business of setting up the message
etc.   It  returns  TRUE or FALSE to say whether or not the send succeeded.
Any  error  codes  from  CygnusEd  Professional  will  be  returned  in the
rm_Result1 field of the message.

*/

BOOL SendCygnusEdMessage(char *thecommand)
{
	struct MsgPort	*CedPort, *MyPort;

	TheMessage.rm_Args[0] = (STRPTR)thecommand;
	TheMessage.rm_Node.mn_Node.ln_Type = NT_MESSAGE;
	TheMessage.rm_Node.mn_Length = sizeof(struct RexxMsg);
	TheMessage.rm_Action = 0;	/* We don't want results. */
	TheMessage.rm_Result2 = 0;	/* clear out the last result. */

	Forbid();

	if (!(CedPort = FindPort((UBYTE *)"rexx_ced")))
	{
		Permit();
		return((BOOL)FALSE);
	}

	if (!(MyPort = CreateMsgPort())) {
		Permit();
		return((BOOL)FALSE);
	}

	TheMessage.rm_Node.mn_ReplyPort = MyPort;
	PutMsg(CedPort, (struct Message *)&TheMessage.rm_Node);
	WaitPort(MyPort);
	DeleteMsgPort(MyPort);
	Permit();
	return((BOOL)TRUE);
}



/*
	This  routine is used for sending commands to CygnusEd Professional
when you want a response.  Certain commands, such as the status command can
return  strings  or  numbers  telling  you  something  about the success or
failure  of the command or about the internal status of the editor.  If the
command  returns  a string, you are responsible for freeing the memory used
to  store  the  string.   The  FreeMyString  command  is  provided for this
purpose.

*/

BOOL SendCygnusEdMessageGetReply(char *thecommand)
{
	struct MsgPort	*CedPort, *MyPort;

	TheMessage.rm_Args[0] = (STRPTR)thecommand;
	TheMessage.rm_Node.mn_Node.ln_Type = NT_MESSAGE;
	TheMessage.rm_Node.mn_Length = sizeof(struct RexxMsg);
	TheMessage.rm_Action = 1L << RXFB_RESULT;	/* We do want results. */
	TheMessage.rm_Result2 = 0;	/* clear out the last result. */

	Forbid();
	if (!(CedPort = FindPort((UBYTE *)"rexx_ced")))
	{
		Permit();
		return((BOOL)FALSE);
	}

	if (!(MyPort = CreateMsgPort()))
	{
		Permit();
		return((BOOL)FALSE);
	}

	TheMessage.rm_Node.mn_ReplyPort = MyPort;
	PutMsg(CedPort, (struct Message *)&TheMessage.rm_Node);
	WaitPort(MyPort);
	DeleteMsgPort(MyPort);
	Permit();
	return((BOOL)TRUE);
}



	/*
	 * char *GetStringFromArexx();
	 *
	 * Function that gets a string from CED. Allocates the memory via AllocVec.
	 * The user is responsible for calling FreeVec !
	 *
	 */


char *GetStringFromArexx()
{
	struct RexxArg *rexxarg;
	char *mem=0;

	if(TheMessage.rm_Result2)
	{
		rexxarg=(struct RexxArg *)(TheMessage.rm_Result2 -8);
		mem=(char *)AllocVec(rexxarg->ra_Length+1,MEMF_CLEAR);
		if(mem)
		{
			CopyMem((char *)TheMessage.rm_Result2,mem,rexxarg->ra_Length);
		}
		if(rexxarg->ra_Size)
			FreeMem(rexxarg, rexxarg->ra_Size);
	}
	return mem;
}


	/*
	 * BOOL GetNumberFromArexx(LONG *number)
	 *
	 * Gets a number from CED.
	 *
	 */


BOOL GetNumberFromArexx(LONG *number)
{
	struct RexxArg *rexxarg;

	if(TheMessage.rm_Result2)
	{
		StrToLong((UBYTE *)TheMessage.rm_Result2,number);

		rexxarg=(struct RexxArg *)(TheMessage.rm_Result2 -8);
		if(rexxarg->ra_Size)
			FreeMem(rexxarg, rexxarg->ra_Size);
		return TRUE;
	}
	else return FALSE;
}



	/*
	 * void GetNothingFromArexx()
	 *
	 * Frees memory occupied via a call to CED through
	 * SendCygnusEdMessageGetReply(char *thecommand);
	 *
	 */


void GetNothingFromArexx()
{
	struct RexxArg *rexxarg;

	if(TheMessage.rm_Result2)
	{
		rexxarg=(struct RexxArg *)(TheMessage.rm_Result2 -8);
		if(rexxarg->ra_Size)
			FreeMem(rexxarg, rexxarg->ra_Size);
	}
}
