/* Filename:	sendpkt.c
 * Authors:		Andy Finkel, Phil Lindsay, Commodore-Amiga
 * Date:		06/29/87
 *
 * This package was derived from "touch.c", written by Andy Finkel
 * and Phil Lindsay of Commodore-Amiga.  
 */

#include "exec/types.h"
#include "exec/ports.h"
#include "exec/io.h"
#include "exec/memory.h"
#include "libraries/dos.h"
#include "libraries/dosextens.h"

#ifdef AZTEC_C
#include <functions.h>
#endif

LONG 
sendpkt(id,type,args,nargs)
	struct MsgPort *id;				/* process indentifier ... (handler's
									   message port ) */
	LONG type,						/* packet type ... (what you want 
									   handler to do )   */
	args[],							/* a pointer to argument list */
	nargs;							/* number of arguments in list  */
{

	struct MsgPort     *replyport;
	struct StandardPacket  *packet;

	LONG count,*pargs,res1=NULL;

	if (!(replyport = (struct MsgPort   *) CreatePort(NULL,NULL)))
		return(NULL);

	packet = (struct StandardPacket *)
	           AllocMem((LONG)sizeof(*packet),MEMF_PUBLIC|MEMF_CLEAR);

	if (packet) {
		packet->sp_Msg.mn_Node.ln_Name = &(packet->sp_Pkt);/* link packet */
		packet->sp_Pkt.dp_Link = &(packet->sp_Msg);/* to message    */
		packet->sp_Pkt.dp_Port = replyport;/* set-up reply port   */
		packet->sp_Pkt.dp_Type = type;/* what to do... */

	/* move all the arguments to the packet */
		pargs = &(packet->sp_Pkt.dp_Arg1);/* address of first argument */
		for (count=0; (count < nargs) && (count < 7); count++)
			pargs[count] = args[count];

		PutMsg(id,packet);			/* send packet */
		WaitPort(replyport);		/* wait for packet to come back */
		GetMsg(replyport);			/* pull message */

		res1 = packet->sp_Pkt.dp_Res1;/* get result */
		FreeMem(packet,(LONG)sizeof(*packet));

	}
	DeletePort(replyport);
	return(res1);
}
