/*
 *	hinter.c	This code constitutes the procedures
 *			used to interface any given application with the 
 *			InfoMinder process. Using these calls within an 
 *			application affords the developer the context
 *			sensitive help capabilities that are available
 *			in the InfoMinder product.
 *
 *	Jim Becker	Copyright (C) 1986  --  Terrapin Software
 *
 *	Last update:	May 30th, 1986
 */

/* include files */
#include <intuition/intuition.h>

/* error return literals */

#define IM_Success  	2
#define IM_No_File  	3
#define IM_No_IM_Prog  	4
#define IM_No_Memory	5
#define IM_Init_Error   6

#define MSGLEN		200
#define IM_PORTNAME	"InfoMinder"

/*
 * 	declare message structure we expect from processes (tasks)
 */

#define IMMSG	struct IM_Message

IMMSG	{
	struct	Message	msghead;	/* message header */
	short	sectionnum;		/* section number */
	char	filename[MSGLEN];	/* filename desired */
};

IMMSG	IM_mess;			/* message used for send */
struct  MsgPort		*IM_port;	/* destination port */
struct	MsgPort		*IM_returnport;	/* return port */
char	IM_filename[MSGLEN];		/* local copy of filename */

extern	struct	MsgPort	*FindPort();
extern  struct  MsgPort *CreatePort();
extern  struct	Task    *FindTask();

/*
 *	IM_Initialize( <file> )
 *
 *	purpose
 *		initialize interaction with the InfoMinder process,
 *		setup ports and data structures.
 */

IM_Initialize( filename )
char	*filename;
{
	/* save local copy of the filename */
	strcpy( IM_filename, filename );

	/* if the port does not exist, return failure */
	IM_port = FindPort( IM_PORTNAME );
	if( IM_port == NULL )
		return IM_No_IM_Prog;

	IM_returnport = CreatePort(0L,0L);
        if( IM_returnport == NULL )
		return IM_Init_Error;

	IM_mess.msghead.mn_ReplyPort = IM_returnport;
	IM_mess.msghead.mn_Length    = (long)sizeof(IM_mess);

	return IM_Success;
}


/*
 * 	IM_LoadInfo( <section_number> )
 *
 *	purpose
 *		This procedure is used to poke the InfoMinder
 *		process by making a request to display a section
 *		of the information that is in the currently
 *		selected document. The filename of the document
 *		and the section number specified are send via message
 *		to InfoMinder.
 */

IM_LoadInfo( sectionnum )
int	sectionnum;
{
	if( strlen( IM_filename ) == 0 ) 
		return IM_No_File;

	/* insure that InfoMinder is still there... */
	IM_port = FindPort( IM_PORTNAME );
	if( IM_port == NULL )
		return IM_No_IM_Prog;

	strcpy( IM_mess.filename, IM_filename );
	IM_mess.sectionnum = (short)sectionnum;

	/* send message then wait for reply */
	PutMsg( IM_port, &IM_mess );

	if( !GetMsg( IM_returnport ) ) {
		Wait( 1L << IM_returnport->mp_SigBit );
		GetMsg( IM_returnport );
	}

	return IM_Success;
}

/*
 *	IM_Terminate()		
 *
 *	purpose
 *		free resources and close connections if appropriate
 *		(currently just frees signal)
 */

IM_Terminate()
{
	if( IM_returnport->mp_SigBit != 0 )
		FreeSignal( IM_returnport->mp_SigBit );

	return IM_Success;
}
	
