/* create_rxi.c */

/*		Copyright © 1989 by Donald T. Meyer, Stormgate Software
 *		All Rights Reserved
 */



#include "rxil.h"

#include <exec/memory.h>



/* NAME
 *		RxilCreateRxi
 *
 * SYNOPSIS
 *		rxi = RxilCreateRxi( name, type );
 *
 *		struct RxilInvocation *rxi;
 *
 *		char *name;
 *		ULONG type;
 *
 * FUNCTION
 *		Allocate a RxilInvocation structure and initialize it to
 *		the defaults which are contained in the global RxilDef
 *		structure.
 *
 * INPUTS
 *		name = character string containing the name of the ARexx
 *			program that will be launched with the structure.
 *		type = determines wether this will be used to launch commands
 *			or functions.  Must be set to either RXCOMM or RXFUNC.
 *
 * RESULT
 *		A pointer to a newly allocated and initialized RxilInvocation
 *		structure, or NULL if one could not be allocated.
 *
 * SIDES
 *
 * HISTORY
 *		01-Aug-89	Creation.
 *
 * BUGS
 *
 * SEE ALSO
 *		RxilDeleteRxi()
 */

struct RxilInvocation *RxilCreateRxi( char *name, ULONG type )
{
	struct RxilInvocation *rxi;


	/* Make call "safe" even if RxilInit() failed */
	if( global_rdef == NULL )
	{
		return( NULL );
	}

	rxi = AllocMem( sizeof(struct RxilInvocation),
		MEMF_PUBLIC | MEMF_CLEAR );
	if( rxi == NULL )
	{
		return( NULL );
	}


	/* A little sanity check, combined with deciding which
	 * command address to default to.  (not anymore).
	 */
	if( type == RXCOMM )
	{
		/* Command type macros default to having the secret port as
		 * their initial host address (if the secret port is open).
		 */
		rxi->CommAddr = global_rdef->SecretPort ?
			global_rdef->SecretPortName : global_rdef->PortName;
	}
	else
	{
		if( type == RXFUNC )
		{
			/* Functions default to returning results */
			rxi->ActionFlags = RXFF_RESULT;

			/* Functions should have REXX as their initial host it
			 * seems to me.  Commands are the ones which would
			 * need to send commands back to the application. (???)
			 */
/*			cad = "REXX"; */
			/* On the other hand, Bill disagrees.  He's probably
			 * right...
			 */
			rxi->CommAddr = global_rdef->SecretPort ?
				global_rdef->SecretPortName : global_rdef->PortName;
		}
		else
		{
			/* Invalid type flag bits */
			FreeMem( rxi, sizeof(struct RxilInvocation) );
			return( NULL );
		}
	}


	/* Init the struct members */

	rxi->Type = type;
	rxi->Name = name;

	/* Set these members to the values in the global default vars. */
	rxi->FileExt = global_rdef->Extension;
	rxi->IHostPort = global_rdef->HostPort;
	rxi->Console = global_rdef->Console;


	/* Add to our linked list for tracking purposes. */
	rxi->Next = global_rdef->Invocations;
	global_rdef->Invocations = rxi;

	return( rxi );
}



/* NAME
 *		RxilDeleteRxi
 *
 * SYNOPSIS
 *		RxilDeleteRxi( rxi );
 *
 *		struct RxilInvocation *rxi;
 *
 * FUNCTION
 *		Remove the structure allocated by a call to RxilCreateRxi() from
 *		the linked list, and then frees it's memory.
 *
 * INPUTS
 *		rxi = a pointer to the RxilInvocation structure to delete.
 *			This must have been allocated by a call to RxilCreateRxi().
 *
 * RESULT
 *		None
 *
 * SIDES
 *
 * HISTORY
 *		01-Aug-89	Creation.
 *
 * BUGS
 *
 * SEE ALSO
 *		RxilCreateRxi()
 */

void RxilDeleteRxi( struct RxilInvocation *rxi )
{
 	struct RxilInvocation **pred, *rxl;


	/* Make call "safe" even if RxilInit() failed */
	if( global_rdef == NULL )
	{
		return;
	}


	/* remove from the linked list */
	pred = &(global_rdef->Invocations);

	for( rxl=global_rdef->Invocations; rxl; rxl=rxl->Next )
	{
		if( rxl == rxi )
		{
			/* Found the one we want to remove from the list */
			*pred = rxi->Next;
			break;
		}
 
		pred = &rxi->Next;
	}

	/* And delete. */
	FreeMem( rxi, sizeof(struct RxilInvocation) );
}

