/* A bare-bones test program to demonstrate receiving messages from ARexx.
 * Opens a public port called "MyPort" and then waits for REXX messages to 
 * arrive.  The port stays open until a "CLOSE" command is received.
 * Usage:  run rexxhost
 * Send commands from within an ARexx program to the 'MyPort' port using
 * the "address 'MyPort' command" instruction.
 */

#include "exec/types.h"

#include "rexx/storage.h"
#include "rexx/rxslib.h"

#include <stdio.h>

struct RexxLib *RexxSysBase;

main(argc,argv)
int argc;
char **argv;
{
   struct MsgPort MyPort;
   struct RexxMsg *rmptr;
   LONG           test;

   RexxSysBase = OpenLibrary("rexxsyslib.library",0L);
   if (RexxSysBase == NULL) {
      printf("Bad News -- no REXX library\n");
      return(20L);
      }

   InitPort(&MyPort,"MyPort");         /* Initialize our message port   */
   AddPort(&MyPort);                   /* Make the port public          */

   for (;;) {                          /* wait for messages             */
      Wait(1<<MyPort.mp_SigBit);
      rmptr = (struct RexxMsg *) GetMsg(&MyPort);

      /* Show what we got           */
      printf("received %s\n",rmptr->rm_Args[0]);
      if (IsRexxMsg(rmptr)) printf("valid REXX message\n");

      /* See whether it's the close command                             */
      test = strcmp(rmptr->rm_Args[0],"CLOSE");

      rmptr->rm_Result1 = 5;           /* return code                   */
      rmptr->rm_Result2 = 0;           /* secondary result              */
      ReplyMsg(rmptr);                 /* send it back                  */

      if (test == 0) break;            /* all done?                     */
      }

   RemPort(&MyPort);                   /* unlink it                     */
   FreePort(&MyPort);                  /* release the port resources    */

   return(0L);
}
