/*
 * Source generated with ARexxBox 1.12 (May 18 1993)
 * which is Copyright (c) 1992,1993 Michael Balzer
 */

#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <rexx/storage.h>
#include <rexx/rxslib.h>

#ifdef __GNUC__
/* GCC needs all struct defs */
#include <dos/exall.h>
#include <graphics/graphint.h>
#include <intuition/classes.h>
#include <devices/keymap.h>
#include <exec/semaphores.h>
#endif

#include <clib/alib_protos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/rexxsyslib_protos.h>

#ifndef __NO_PRAGMAS

#ifdef AZTEC_C
#include <pragmas/exec_lib.h>
#include <pragmas/dos_lib.h>
#include <pragmas/rexxsyslib_lib.h>
#endif

#ifdef LATTICE
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/rexxsyslib_pragmas.h>
#endif

#endif /* __NO_PRAGMAS */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#ifdef LATTICE
#undef toupper
#define inline __inline
#endif

#ifdef __GNUC__
#undef toupper
static inline char toupper( char c )
{
	return( islower(c) ? c - 'a' + 'A' : c );
}
#endif

#ifdef AZTEC_C
#define inline
#endif

#include "TrackRexx.h"
#include "TrackRexx_rxif.h"

extern struct ExecBase *SysBase;
extern struct DosLibrary *DOSBase;
extern struct RxsLib *RexxSysBase;


/* $ARB: I 848528372 */


/* $ARB: B 8 REFRESH */
void rx_refresh( struct RexxHost *host, struct rxd_refresh **rxd, long action, struct RexxMsg *rexxmsg )
{
	struct rxd_refresh *rd = *rxd;

	switch( action )
	{
		case RXIF_INIT:
			*rxd = AllocVec( sizeof *rd, MEMF_CLEAR );
			break;
			
		case RXIF_ACTION:
			/* Insert your CODE here */
			SetTrackFlag(CODE_REFRESH);
			rd->rc = BSendGoodbye ? RETURN_OK : RETURN_WARN;
			break;
		
		case RXIF_FREE:
			/* FREE your local data here */
			FreeVec( rd );
			break;
	}
	return;
}
/* $ARB: E 8 REFRESH */

/* $ARB: B 9 PING */
void rx_ping( struct RexxHost *host, struct rxd_ping **rxd, long action, struct RexxMsg *rexxmsg )
{
	struct rxd_ping *rd = *rxd;

	switch( action )
	{
		case RXIF_INIT:
			*rxd = AllocVec( sizeof *rd, MEMF_CLEAR );
			break;
			
		case RXIF_ACTION:
			/* Insert your CODE here */
			SetTrackFlag(CODE_PING);
			rd->rc = BSendGoodbye ? RETURN_OK : RETURN_WARN;
			break;
		
		case RXIF_FREE:
			/* FREE your local data here */
			FreeVec( rd );
			break;
	}
	return;
}
/* $ARB: E 9 PING */

/* $ARB: B 17 QUIT */
void rx_quit( struct RexxHost *host, struct rxd_quit **rxd, long action, struct RexxMsg *rexxmsg )
{
	struct rxd_quit *rd = *rxd;

	switch( action )
	{
		case RXIF_INIT:
			*rxd = AllocVec( sizeof *rd, MEMF_CLEAR );
			break;
			
		case RXIF_ACTION:
			/* Insert your CODE here */
			SetTrackFlag(CODE_QUIT);
			rd->rc = RETURN_OK;
			break;
		
		case RXIF_FREE:
			/* FREE your local data here */
			FreeVec( rd );
			break;
	}
	return;
}
/* $ARB: E 17 QUIT */

/* $ARB: B 24 GET */
void rx_get( struct RexxHost *host, struct rxd_get **rxd, long action, struct RexxMsg *rexxmsg )
{
	struct rxd_get *rd = *rxd;
    int o = 0, i;
    BOOL BMemErr = FALSE;
    
	switch( action )
	{
		case RXIF_INIT:
			*rxd = AllocVec( sizeof *rd, MEMF_CLEAR );
			if( rd = *rxd )
			{
				/* set your DEFAULTS here */
			}
			break;
			
		case RXIF_ACTION:
#define SetNextPtr(VAR, ARRAY) {rd->res.VAR = &rd->res.ARRAY[o++];}
			/* Insert your CODE here */
			rd->res.server     = strdup(szCurrentHostname);
			rd->res.comment    = strdup(szCurrentComment);
			rd->res.myhostname = strdup(szMyHostName); 
			if (rd->res.port = malloc(20*sizeof(long)))
			{
			  rd->res.port[o] = nPort;               SetNextPtr(port,port);
			  rd->res.port[o] = BConfirmAppLaunch;   SetNextPtr(confirm,port);
			  rd->res.port[o] = BSendGoodbye;        SetNextPtr(connected,port);
			  rd->res.port[o] = clientList ? clientList->nListLength : 0; SetNextPtr(numclients,port);
			  rd->res.port[o] = BEnabled;            SetNextPtr(enabled,port);
			  rd->res.port[o] = nNumActions;         SetNextPtr(numactions,port); 
			  rd->res.port[o] = nPingInterval;       SetNextPtr(pinginterval,port);
			  rd->res.port[o] = nRefreshInterval;    SetNextPtr(refreshinterval,port);
			  rd->res.port[o] = nPingCount;          SetNextPtr(minutestillping,port);
			  rd->res.port[o] = nRefreshCount;       SetNextPtr(minutestillrefresh,port);
			  rd->res.port[o] = (TrackWindow==NULL); SetNextPtr(hidden,port);
			  rd->res.port[o] = lLastChangeAt;       SetNextPtr(lastchangeat,port);
			  rd->res.port[o] = nLastEntryClicked;   SetNextPtr(lastentryclicked,port);
			  rd->res.port[o] = currentAction;       SetNextPtr(currentaction, port);
			}

			/* Fill optional fields */
			if (rd->arg.clients)
			{
			  if ((clientList)&&(clientList->nListLength > 0))
			  {
			   /* allocate & fill null-terminated array for client names */
			   if ((rd->res.clients  = malloc(sizeof(char *)*(clientList->nListLength+1))) &&
			       (rd->res.comments = malloc(sizeof(char *)*(clientList->nListLength+1))))
			   {
			    struct Node * current = clientList->list.lh_Head;
			    for(i=0; ((BMemErr==FALSE)&&(i<clientList->nListLength)); i++)
			    {
			      struct Client * c = (struct Client *) current;
			      if (NULL==(rd->res.clients[i]  = strdup(c->hostname))) BMemErr = TRUE;
			      if (NULL==(rd->res.comments[i] = strdup(c->comment)))  BMemErr = TRUE;
			      current = current->ln_Succ;
			    }
			    rd->res.clients[i] = rd->res.comments[i] = NULL; /* terminate the array! */
			   }
			  }
			  else 
			  {
			    /* Make an empty array */
			    if (rd->res.clients = malloc(sizeof(char *))) rd->res.clients[0]  = NULL;
			    if (rd->res.comments= malloc(sizeof(char *))) rd->res.comments[0] = NULL;
			  }
			}
			if (rd->arg.actions)
			{
			  /* allocate & fill null-terminated array for action names */
			  /* allocate & fill null-terminated array for client names */
			  if ((rd->res.actions  = malloc(sizeof(char *)*(nNumActions+1))) &&
			      (rd->res.commands = malloc(sizeof(char *)*(nNumActions+1))))
			  {
			    for(i=0; ((BMemErr==FALSE)&&(i<nNumActions)); i++)
			    {
			      if (NULL==(rd->res.actions[i]  = strdup(ActionLabels[i])))  BMemErr = TRUE;
			      if (NULL==(rd->res.commands[i] = strdup(Actions[i])))       BMemErr = TRUE;
			    }
			    rd->res.actions[i] = rd->res.commands[i] = NULL; /* terminate the array! */
			  }
			}			
			BMemErr |= (rd->res.server  == NULL)||(rd->res.port       == NULL) || 
			           (rd->res.comment == NULL)||(rd->res.myhostname == NULL);
            if (rd->arg.clients) BMemErr |= 
                       (rd->res.clients == NULL)||(rd->res.comments   == NULL);
            if (rd->arg.actions) BMemErr |= 
                       (rd->res.actions == NULL)||(rd->res.commands   == NULL);
		    rd->rc  = BMemErr ? RETURN_FAIL : RETURN_OK;
		    rd->rc2 = BMemErr ? ERROR_NO_FREE_STORE : RETURN_OK;
			break;
		
		case RXIF_FREE:
			/* FREE your local data here */
			if (rd->res.clients)  FreeStringArray(rd->res.clients);
			if (rd->res.comments) FreeStringArray(rd->res.comments);
			if (rd->res.actions)  FreeStringArray(rd->res.actions);
			if (rd->res.commands) FreeStringArray(rd->res.commands);
			if (rd->res.myhostname) free(rd->res.myhostname);
			if (rd->res.server)     free(rd->res.server);
			if (rd->res.comment)    free(rd->res.comment);
			if (rd->res.port)       free(rd->res.port);
			FreeVec( rd );
			break;
	}
	return;
}
/* $ARB: E 24 GET */

/* $ARB: B 25 SET */
void rx_set( struct RexxHost *host, struct rxd_set **rxd, long action, struct RexxMsg *rexxmsg )
{
	struct rxd_set *rd = *rxd;
    int flags = 0;
    
	switch( action )
	{
		case RXIF_INIT:
			*rxd = AllocVec( sizeof *rd, MEMF_CLEAR );
			if( rd = *rxd )
			{
				/* set your DEFAULTS here */
			}
			break;
			
		case RXIF_ACTION:
			/* Insert your CODE here */
			rd->rc = 0;
			if (rd->arg.enabled) SetTrackFlag(ParseBool(rd->arg.enabled) ? CODE_ENABLE : CODE_DISABLE);
			if (rd->arg.hidden)  SetTrackFlag(ParseBool(rd->arg.hidden)  ? CODE_HIDE   : CODE_SHOW);
			if (rd->arg.confirm) {BConfirmAppLaunch = ParseBool(rd->arg.confirm); flags |= 0x01;}
			if (rd->arg.left)    {nWinLeft   = *rd->arg.left;   flags |= 0x02;}
			if (rd->arg.top)     {nWinTop    = *rd->arg.top;    flags |= 0x02;}
			if (rd->arg.width)   {nWinWidth  = *rd->arg.width;  flags |= 0x02;}
			if (rd->arg.height)  {nWinHeight = *rd->arg.height; flags |= 0x02;}
			if (rd->arg.comment) SetCommentString(TrackWindow, rd->arg.comment);
			if (rd->arg.pingdelay) SetPingInterval(TrackWindow, *rd->arg.pingdelay);
			if (rd->arg.refreshdelay) SetUpdateInterval(TrackWindow, *rd->arg.refreshdelay);
			if (rd->arg.server) SetServerString(TrackWindow, rd->arg.server);
			if (rd->arg.port) SetPortNumber(TrackWindow, *rd->arg.port);
			if (rd->arg.windowtitle) StatMessage(rd->arg.windowtitle);
			if (rd->arg.currentactionindex) SetCurrentActionIndex(TrackWindow,*rd->arg.currentactionindex);
            if (rd->arg.currentaction) SetCurrentAction(TrackWindow, rd->arg.currentaction);
			if ((rd->arg.action)&&(rd->res.actionindex=malloc(sizeof(long))))
			  *rd->res.actionindex = UpdateActions(TrackWindow, rd->arg.action);
			if (flags & 0x01) SetMenuValues();
			if ((flags & 0x02)&&(TrackWindow)) ChangeWindowBox(TrackWindow->win, nWinLeft, nWinTop, nWinWidth, nWinHeight);
			break;
		
		case RXIF_FREE:
			/* FREE your local data here */
			if (rd->res.actionindex) free(rd->res.actionindex);
			FreeVec( rd );
			break;
	}
	return;
}
/* $ARB: E 25 SET */

/* $ARB: B 27 REQUEST */
void rx_request( struct RexxHost *host, struct rxd_request **rxd, long action, struct RexxMsg *rexxmsg )
{
	struct rxd_request *rd = *rxd;

	switch( action )
	{
		case RXIF_INIT:
			*rxd = AllocVec( sizeof *rd, MEMF_CLEAR );
			if( rd = *rxd )
			{
				/* set your DEFAULTS here */
			}
			break;
			
		case RXIF_ACTION:
			/* Insert your CODE here */
			if (rd->res.clicked = malloc(sizeof(long)))
			{
			  *rd->res.clicked = MakeReq(
			         rd->arg.title   ? rd->arg.title   : "AmiTrack Rexx Request",
			         rd->arg.message ? rd->arg.message : "An ARexx script called my REQUEST function",
			         rd->arg.buttons ? rd->arg.buttons : "Okay");
			  rd->rc = 0;
			}
			else 
			{
			  rd->rc  = RETURN_ERROR;
			  rd->rc2 = ERROR_NO_FREE_STORE;
			}
			break;
		
		case RXIF_FREE:
			/* FREE your local data here */
			FreeVec( rd );
			break;
	}
	return;
}
/* $ARB: E 27 REQUEST */

/* $ARB: B 28 CONNECT */
void rx_connect( struct RexxHost *host, struct rxd_connect **rxd, long action, struct RexxMsg *rexxmsg )
{
	struct rxd_connect *rd = *rxd;
    int connectto = -1, connectwith = -1;
    
	switch( action )
	{
		case RXIF_INIT:
			*rxd = AllocVec( sizeof *rd, MEMF_CLEAR );
			if( rd = *rxd )
			{
				/* set your DEFAULTS here */
			}
			break;
			
		case RXIF_ACTION:
			/* Insert your CODE here */
			if ((rd->arg.actionindex)||(rd->arg.action))
			{
			 if (rd->arg.actionindex) connectwith = *rd->arg.actionindex;
			 if (rd->arg.action)      connectwith = GetTrackActionByName(rd->arg.action);
			}
			else connectwith = currentAction;
			
			if ((rd->arg.hostindex)||(rd->arg.host))
			{
			 if (rd->arg.hostindex)   connectto   = *rd->arg.hostindex;
			 if (rd->arg.host)        connectto   = GetTrackHostByName(rd->arg.host);
			}
			else connectto = nLastEntryClicked;
			
			if ((connectwith >= 0)&&(connectto >= 0)) 
			{
			  rd->rc = DoAction(connectto, connectwith, 
			             (rd->arg.confirm) ? ParseBool(rd->arg.confirm) : BConfirmAppLaunch);
			  if (rd->rc == -2) rd->rc = RETURN_WARN;
			  if (rd->rc == -1) rd->rc = RETURN_ERROR;
			}
			else rd->rc = RETURN_WARN;
			break;
		
		case RXIF_FREE:
			/* FREE your local data here */
			FreeVec( rd );
			break;
	}
	return;
}
/* $ARB: E 28 CONNECT */


#ifndef RX_ALIAS_C
char *ExpandRXCommand( struct RexxHost *host, char *command )
{
	/* Insert your ALIAS-HANDLER here */
	return( NULL );
}
#endif

