/*
 *	File:					test2.c
 *	Description:	Small program to show how easy EasyRexx.library makes
 *								the AREXX handling.  Shows how to handle AREXX messages
 *								calling the userdata functions in the commandTable.  Must be
 *								linked with easyrexx.lib.
 *
 *	(C) 1994, Ketil Hunn
 *
 *	Set tab to 2 for best readability
 *
 */

#include <stdio.h>

#include <libraries/easyrexx.h>
struct Library *EasyRexxBase;

BYTE done=FALSE;

#define	AREXX_CLEAR			1
#define	AREXX_OPEN			2
#define	AREXX_SAVEAS		3
#define	AREXX_HELP			4
#define	AREXX_TEXT			5
#define	AREXX_ROW				6
#define	AREXX_QUIT			7

LONG arexx_clear(struct ARexxContext *c)
{
	printf("CLEAR");
	return RC_OK;
}

LONG arexx_open(struct ARexxContext *c)
{
	printf("OPEN");
	if(ARG(c, 1))
		printf(" TEXT");
	else
		printf(" PROJECT");
	if(ARG(c, 2))
		printf(" '%s'", ARGSTRING(c,2));
	return RC_OK;
}

LONG arexx_saveas(struct ARexxContext *c)
{
	printf("SAVEAS '%s'", ARGSTRING(c,0));
	return RC_OK;
}

LONG arexx_help(struct ARexxContext *c)
{
	printf("HELP");
	if(ARG(c, 0))
		printf(" AMIGAGUIDE");
	if(ARG(c, 1))
		printf(" '%s'", ARGSTRING(c,1));
	return RC_OK;
}

LONG arexx_text(struct ARexxContext *c)
{
	printf("TEXT '%s'", ARGSTRING(c,0));
	return RC_OK;
}

LONG arexx_row(struct ARexxContext *c)
{
	printf("ROW %ld", ARGNUMBER(c,0));
	return RC_OK;
}

LONG arexx_quit(struct ARexxContext *c)
{
	printf("QUIT");
	done=TRUE;
	return RC_OK;
}

/* This application supports these AREXX commands */
struct ARexxCommandTable commandTable[]=
{
/*    ID          CMD       ARGUMENT TEMPLATE             USERDATA  */

	AREXX_CLEAR,	"CLEAR",		"FORCE/S",										arexx_clear,
	AREXX_OPEN,		"OPEN",			"PROJECT/S,TEXT/S,NAME/F",		arexx_open,
	AREXX_SAVEAS,	"SAVEAS",		"NAME/K",											arexx_saveas,
	AREXX_HELP,		"HELP",			"AMIGAGUIDE/S,TOPIC/F",				arexx_help,
	AREXX_TEXT,		"TEXT",			"TEXT/A/F",										arexx_text,
	AREXX_ROW,		"ROW",			"ROWNUMBER/A/N",							arexx_row,
	AREXX_QUIT,		"QUIT",			NULL,													arexx_quit,
	TABLE_END,
};

void main(int argc, char **argv)
{
	if(EasyRexxBase=OpenLibrary(EASYREXXNAME, EASYREXXVERSION))
	{
		struct ARexxContext	*context;

		if(context=AllocARexxContext(	ER_Portname,			"EASYREXX_TEST",
																	ER_CommandTable,	commandTable,
																	TAG_DONE))
		{
			ULONG signal;

			printf(	"Welcome to a small EasyRexx demonstration\n"
							"-----------------------------------------\n"
							"Open another shell and start the small\n"
							"AREXX script: rx test\n"
							"or doubleclick on the test.rexx icon.\n");
			while(!done)
			{
				signal=Wait(ER_SIGNAL(context));

				if(signal & ER_SIGNAL(context))
				{
					printf("Received: ");
					HandleARexxFuncs(context);
					printf("\n");
				}
			}
			FreeARexxContext(context);
		}
		CloseLibrary(EasyRexxBase);
	}
}
