/*
*   This program was written by Stephen W. Berry.
*
*   The concept is copyrighted by me, but the program listings
*   executable and documentation are placed in the Public Domain.
*
*   If you find a bug or enhance this program please let me know
*   at the following address:
*
*       The Checkered Ball
*       A-7 Sinai Circle
*       Chelmsford, Ma 01824
*       c/o Stephen Berry
*
*/

#include "rexx/storage.h"
#include "rexx/rxslib.h"
#include <stdio.h>

#define FATALERROR 20L

/* external routines - system */

extern void CloseLibrary();

/* external routines - Rexx */

/* external routines - Arp */

/* external routines - mine */

extern void OpenRexxPort(),DeleteRexxPort(),CleanupRexx();
extern long OpenLib(),StartRexxProg();
extern struct RexxMsg *port,*PollRexxPort(),*WaitMsgPort();

/* global variables, structures etc... */

struct RexxMsg myrexxport;

struct RexxLib *RexxSysBase;    /* the important thing here is the name */

long main()
{
    struct RexxMsg *resultmsg;
    LONG equal;

/*  I link with arp.lib so that I don't have to open:
    intuition, graphics and arp libraries. */

    RexxSysBase = OpenLib("rexxsyslib.library",(LONG)RXSVERS);
    if(RexxSysBase == NULL){
        exit();
    }

    OpenRexxPort("flport",&myrexxport);

    if(!StartRexxProg(&myrexxport)){    /* start up fl.flex as a rexx task */
        DeleteRexxPort(&myrexxport);
        CloseLibrary(RexxSysBase);
        printf("Couldn't start up fl.flex\n");
        exit();
    }

    /* this is a communication test ... */

    for(;;) {

        resultmsg = WaitRexxPort(&myrexxport);
        printf("got a message of - %s\n",resultmsg->rm_Args[0]);

        equal = strcmp(resultmsg->rm_Args[0],"end");

        resultmsg->rm_Result1 = 1;  /* shows an error code */
        resultmsg->rm_Result2 = 0;  /* no secondary        */

        ReplyMsg(resultmsg);

        if( equal == 0 ){
            CleanupRexx(&myrexxport);
            break;
        }
    }   /* end of forever */

    DeleteRexxPort(&myrexxport);
    CloseLibrary(RexxSysBase);
    exit(equal);
}

