/*
** Source machine generated by the AREXX Interface Designer for EasyREXX
** which is (c) copyrighted 1995 to Ketil Hunn
**
*/

#ifndef AREXX_INTERFACE
#define AREXX_INTERFACE

#define  NO_RECORDPOINTER

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

UBYTE helloworld[]  ="Hello World! :)",
      errormessage[]="Error: Because you asked for it ;)";

/* EasyRexx IDs begins at 1 */
#define AREXX_CLEAR       1
#define AREXX_GETVAR      2
#define AREXX_HELP        3
#define AREXX_OPEN        4
#define AREXX_QUIT        5
#define AREXX_ROW         6
#define AREXX_SAVE        7
#define AREXX_TEXT        8
#define AREXX_RX          9
#define AREXX_CAUSEERROR  10

BYTE arexxfuncCLEAR(struct ARexxContext *c);
BYTE arexxfuncGETVAR(struct ARexxContext *c);
BYTE arexxfuncHELP(struct ARexxContext *c);
BYTE arexxfuncOPEN(struct ARexxContext *c);
BYTE arexxfuncQUIT(struct ARexxContext *c);
BYTE arexxfuncROW(struct ARexxContext *c);
BYTE arexxfuncSAVE(struct ARexxContext *c);
BYTE arexxfuncTEXT(struct ARexxContext *c);
BYTE arexxfuncRX(struct ARexxContext *c);
BYTE arexxfuncCAUSEERROR(struct ARexxContext *c);


struct ARexxCommandTable commandTable[]=
{
  AREXX_CLEAR,      "CLEAR",      "FORCE/S",                 (APTR)arexxfuncCLEAR,
  AREXX_GETVAR,     "GETVAR",     "HELLOWORLD/S",            (APTR)arexxfuncGETVAR,
  AREXX_HELP,       "HELP",       "AMIGAGUIDE/S,TOPIC/F",    (APTR)arexxfuncHELP,
  AREXX_OPEN,       "OPEN",       "PROJECT/S,TEXT/S,NAME/F", (APTR)arexxfuncOPEN,
  AREXX_QUIT,       "QUIT",       "",                        (APTR)arexxfuncQUIT,
  AREXX_ROW,        "ROW",        "NUMBER/A/N",              (APTR)arexxfuncROW,
  AREXX_SAVE,       "SAVE",       "AS/S,NAME/F",             (APTR)arexxfuncSAVE,
  AREXX_TEXT,       "TEXT",       "TEXT/A/F",                (APTR)arexxfuncTEXT,
  AREXX_RX,         "RX",         "COMMAND/A/F",             (APTR)arexxfuncRX,
  AREXX_CAUSEERROR, "CAUSEERROR", "",                        (APTR)arexxfuncCAUSEERROR,
  TABLE_END,
};

struct ARexxRetValues
{
  LONG  retsult,
        resultlong;
  UBYTE *resultstring,
        *error;
} myreturn;

BYTE arexxfuncCLEAR(struct ARexxContext *c)
{
  printf("CLEAR");
  if(ARG(c, 0))
    printf(" FORCE=on");
  return RC_OK;
}

BYTE arexxfuncGETVAR(struct ARexxContext *c)
{
  printf("GETVAR");
  if(ARG(c, 0))
  {
    printf(" HELLOWORLD\n* string returned");
    myreturn.retstring=helloworld;
  }
  return RC_OK;
}

BYTE arexxfuncHELP(struct ARexxContext *c)
{
  printf("HELP");
  if(ARG(c, 0))
    printf(" AMIGAGUIDE");
  if(ARG(c, 1))
    printf(" TOPIC=%s", ARGSTRING(c,1));
  return RC_OK;
}

BYTE arexxfuncOPEN(struct ARexxContext *c)
{
  printf("OPEN");
  if(ARG(c, 1))
    printf(" TEXT");
  else
    printf(" PROJECT");  /* defaults to PROJECT */
  if(ARG(c, 2))
    printf(" '%s'", ARGSTRING(c,2));
  return RC_OK;
}

BYTE arexxfuncQUIT(struct ARexxContext *c)
{
  printf("QUIT");
  return -1;
}

BYTE arexxfuncROW(struct ARexxContext *c)
{
  printf("ROW");
  if(ARG(c, 0))
    printf(" %ld", ARGNUMBER(c,0));
  return RC_OK;
}

BYTE arexxfuncSAVE(struct ARexxContext *c)
{
  printf("SAVE");
  if(ARG(c, 0))
    printf(" AS");
  if(ARG(c, 1))
    printf(" '%s'", ARGSTRING(c,1));
  return RC_OK;
}

BYTE arexxfuncTEXT(struct ARexxContext *c)
{
  printf("TEXT");
  if(ARG(c, 0))
    printf(" '%s'", ARGSTRING(c,0));
  return RC_OK;
}

BYTE arexxfuncRX(struct ARexxContext *c)
{
  printf(  "RX '%s'\n"
          "* Sending command asynchronously: '%s' to the '%s' port",
          ARGSTRING(c,0),
          ARGSTRING(c,0),
          c->portname);
  SendARexxCommand(ARGSTRING(c,0),
                    ER_Port,    c->port,
                    ER_Context,  c,
                    ER_Asynch,  TRUE,
                    TAG_DONE);
  return RC_OK;
}

BYTE arexxfuncCAUSEERROR(struct ARexxContext *c)
{
  printf(  "CAUSEERROR");
  myreturn.error=errormessage;
  return RC_ERROR;
}

BYTE myHandleARexx(struct ARexxContext *c)
{
  myreturn.result       =RC_OK;
  myreturn.resultlong   =NULL;
  myreturn.resultstring =NULL;
  myreturn.error        =NULL;

  if(GetARexxMsg(c))
  {
    register ULONG i=0L;

    printf("Received: ");
    while(c->table[i].command)
    {
      if(c->table[i].id==c->id)
      {
        BYTE (*func)(struct ARexxContext *c);

        if(func=(void *)(c->table[i].userdata))
          myreturn.result=func(c);
        break;
      }
      else
        ++i;
    }
    ReplyARexxMsg(c,
                  ER_ReturnCode, myreturn.retvalue,
                  (myreturn.error          ? ER_ErrorMessage:TAG_IGNORE),  myreturn.error,
                  (myreturn.resultlong==~0 ? ER_ResultLong  :TAG_IGNORE),  myreturn.resultlong,
                  (myreturn.resultstring   ? ER_ResultString:TAG_IGNORE),  myreturn.resultstring,
                  TAG_DONE);
    printf("\n");
  }
  return (BYTE)(myreturn.result==-1);
}


void main(void)
{
  BYTE done=FALSE;
  struct ARexxContext *context=NULL;

  if(EasyRexxBase=OpenLibrary(EASYREXXNAME, EASYREXXVERSION))
    context=AllocARexxContext(ER_CommandTable,  commandTable,
                              ER_Author,        "Ketil Hunn",
                              ER_Copyright,      "© 1995 Ketil Hunn",
                              ER_Version,        "2.0",
                              ER_Portname,      "EASYREXX_TEST",
                              TAG_DONE);
  else
    printf("easyrexx.library not found\n");

  /* We only check the context here because this example doesn't do
  ** anything else.  You should always continue without the port and
  ** just discard the ARexx stuff.
  */
  if(context)
  {
    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==FALSE | ER_SAFETOQUIT(context)==FALSE)
    {
      ULONG signals=Wait(ER_SIGNAL(context));
      ER_SETSIGNALS(context, signals);

      if(signals & ER_SIGNAL(context))
        done=myHandleARexx(context);
    }
  }
  if(EasyRexxBase)
  {
    FreeARexxContext(context);
    CloseLibrary(EasyRexxBase);
  }
}

#endif
