(****************************************************************************
 *             Benchmark Modula-2 from Avant-Garde Software                 *
 ****************************************************************************
 * Name: RexxHost.MOD                      Version: Amiga.00.00             *
 * Created: 04/03/88   Updated: 04/03/88   Author: Leon Frenkel             *
 * Description: Demo program for Benchmark Modula-2.                        *
 * A 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                                                     *
 * Then send commands from within an ARexx by "address 'MyPort' command"    *
 ****************************************************************************)

MODULE RexxHost;

FROM SYSTEM IMPORT
	ADR;
FROM ARexx IMPORT
	RXSName, RexxSysBase, RexxMsgPtr,
	InitPort, FreePort, IsRexxMsg;
FROM CPrintTerminal IMPORT
	printf;
FROM CStrings IMPORT
	strcmp;
FROM FormatString IMPORT
	FormatArg;
FROM Libraries IMPORT
	OpenLibrary, CloseLibrary;
FROM Ports IMPORT
	MsgPort,
	GetMsg, ReplyMsg, RemPort, AddPort;
FROM Tasks IMPORT
	SignalSet,
	Wait;

VAR
  MyPort: MsgPort;

  argo: ARRAY [0..9] OF FormatArg;

PROCEDURE Demo;
VAR
  rmptr: RexxMsgPtr;
  sigs: SignalSet;
  test: INTEGER;
BEGIN
  RexxSysBase := OpenLibrary(ADR(RXSName), 0D);
  IF (RexxSysBase # NIL) THEN

    (* Initialize our message port *)
    IF InitPort(MyPort, ADR("MyPort")) # -1D THEN

      (* Make the port public *)
      AddPort(MyPort);

      LOOP
        sigs := Wait(SignalSet{CARDINAL(MyPort.mpSigBit)});
        rmptr := GetMsg(MyPort);

        (* Show what we got *)
        IF (IsRexxMsg(rmptr)) THEN
          argo[0].L := rmptr^.rmArgs[0];
          printf("received %s\n", argo);
        END;

        (* See whether it's the close command *)
        test := strcmp(rmptr^.rmArgs[0], ADR("CLOSE"));

        rmptr^.rmResult1 := 5; (* return code *)
        rmptr^.rmResult2 := 0; (* secondary result *)
        ReplyMsg(rmptr);       (* send it back *)

        IF (test = 0) THEN EXIT; END;
      END; (* LOOP *)

      RemPort(MyPort);
      FreePort(MyPort);
    END;
    CloseLibrary(RexxSysBase^);
  END;
END Demo;

BEGIN
  Demo();
END RexxHost.
