home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / minrexx_450.lzh / MinRexx / minrexx.c < prev    next >
C/C++ Source or Header  |  1991-02-03  |  16KB  |  437 lines

  1. /*
  2.  *   This is an example of how REXX messages might be handled.  This is
  3.  *   a `minimum' example that both accepts asynchronous REXX messages and
  4.  *   can request REXX service.
  5.  *
  6.  *   Read this entire file!  It's short enough.
  7.  *
  8.  *   It is written in such a fashion that it can be attached to a program
  9.  *   with a minimum of fuss.  The only external symbols it makes available
  10.  *   are the seven functions and RexxSysBase.
  11.  *
  12.  *   This code is by Radical Eye Software, but it is put in the public
  13.  *   domain.  I would appreciate it if the following string was left in
  14.  *   both as a version check and as thanks from you for the use of this
  15.  *   code.
  16.  *
  17.  *   If you modify this file for your own use, don't bump the version
  18.  *   number; add a suffix, such as 1.0a or 1.0.3 or something, so we
  19.  *   don't have fake `versions' floating around.
  20.  */
  21. static char *blurb = "Radical Eye MinRexx 0.4" ;
  22. /*
  23.  *   We read in our own personal little include.
  24.  */
  25. #include "minrexx.h"
  26. /*
  27.  *   All of our local globals, hidden from sight.
  28.  */
  29. static struct MsgPort *rexxPort ;          /* this is *our* rexx port */
  30. static int bringerdown ;                   /* are we trying to shut down? */
  31. static struct rexxCommandList *globalrcl ; /* our command association list */
  32. static long stillNeedReplies ;             /* how many replies are pending? */
  33. static long rexxPortBit ;                  /* what bit to wait on for Rexx? */
  34. static char *extension ;                   /* the extension for macros */
  35. static void (*userdisp)(struct RexxMsg *, struct rexxCommandList *, char *) ;
  36.                                            /* the user's dispatch function */
  37. static struct RexxMsg *oRexxMsg ;          /* the outstanding Rexx message */
  38. /*
  39.  *   Our library base.  Don't you dare close this!
  40.  */
  41. struct RxsLib *RexxSysBase ;
  42. /*
  43.  *   This is the main entry point into this code.
  44.  */
  45. long upRexxPort(
  46. /*
  47.  *   The first argument is the name of your port to be registered;
  48.  *   this will be used, for instance, with the `address' command of ARexx.
  49.  */
  50. char *s,
  51. /*
  52.  *   The second argument is an association list of command-name/user-data
  53.  *   pairs.  It's an array of struct rexxCommandList, terminated by a
  54.  *   structure with a NULL in the name field. The commands are case
  55.  *   sensitive.  The user-data field can contain anything appropriate,
  56.  *   perhaps a function to call or some other data.
  57.  */
  58. struct rexxCommandList *rcl,
  59. /*
  60.  *   The third argument is the file extension for ARexx macros invoked
  61.  *   by this program.  If you supply this argument, any `primitive' not
  62.  *   in the association list rcl will be sent out to ARexx for
  63.  *   interpretation, thus allowing macro programs to work just like
  64.  *   primitives.  If you do not want this behavior, supply a `NULL'
  65.  *   here, and those commands not understood will be replied with an
  66.  *   error value of RXERRORNOCMD.
  67.  */
  68. char *exten,
  69. /*
  70.  *   The fourth argument is the user dispatch function.  This function
  71.  *   will *only* be called from rexxDisp(), either from the user calling
  72.  *   this function directly, or from dnRexxPort().  Anytime a command
  73.  *   match is found in the association list, this user-supplied function
  74.  *   will be called with two arguments---the Rexx message that was
  75.  *   received, and a pointer to the association pair.  This function
  76.  *   should return a `1' if the message was replied to by the function
  77.  *   and a `0' if the default success code of (0, 0) should be returned.
  78.  *   Note that the user function should never ReplyMsg() the message;
  79.  *   instead he should indicate the return values with replyRexxCmd();
  80.  *   otherwise we lose track of the messages that still lack replies.
  81.  */
  82. void (*uf)(struct RexxMsg *, struct rexxCommandList *, char *))
  83. /*
  84.  *   upRexxPort() returns the signal bit to wait on for Rexx messages.
  85.  *   If something goes wrong, it simply returns a `0'.  Note that this
  86.  *   function is safe to call multiple times because we check to make
  87.  *   sure we haven't opened already.  It's also a quick way to change
  88.  *   the association list or dispatch function.
  89.  */
  90. {
  91. /*
  92.  *   Some basic error checking.
  93.  */
  94.    if (rcl == NULL || uf == NULL)
  95.       return(0L) ;
  96. /*
  97.  *   If we aren't open, we make sure no one else has opened a port with
  98.  *   this name already.  If that works, and the createport succeeds, we
  99.  *   fill rexxPortBit with the value to return.
  100.  *
  101.  *   Note that rexxPortBit will be 0 iff rexxPort is NULL, so the check
  102.  *   for rexxPort == NULL also insures that our rexxPortBit is 0.
  103.  */
  104.    if (rexxPort == NULL) {
  105.       Forbid() ;
  106.       if (FindPort(s)==NULL)
  107.          rexxPort = CreatePort(s, 0L) ;
  108.       Permit() ;
  109.       if (rexxPort != NULL)
  110.          rexxPortBit = 1L << rexxPort->mp_SigBit ;
  111.    }
  112. /*
  113.  *   Squirrel away these values for our own internal access, and return
  114.  *   the wait bit.
  115.  */
  116.    globalrcl = rcl ;
  117.    extension = exten ;
  118.    userdisp = uf ;
  119.    return(rexxPortBit) ;
  120. }
  121. /*
  122.  *   This function closes the rexx library, but only if it is open
  123.  *   and we aren't expecting further replies from REXX.  It's
  124.  *   *private*, but it doesn't have to be; it's pretty safe to
  125.  *   call anytime.
  126.  */
  127. static void closeRexxLib(void) {
  128.    if (stillNeedReplies == 0 && RexxSysBase) {
  129.       CloseLibrary((struct Library *)RexxSysBase) ;
  130.       RexxSysBase = NULL ;
  131.    }
  132. }
  133. /*
  134.  *   This function closes down the Rexx port.  It is always safe to
  135.  *   call, and should *definitely* be made a part of your cleanup
  136.  *   routine.  No arguments and no return.  It removes the Rexx port,
  137.  *   replies to all of the messages and insures that we get replies
  138.  *   to all the ones we sent out, closes the Rexx library, deletes the
  139.  *   port, clears a few flags, and leaves.
  140.  */
  141. void dnRexxPort(void) {
  142.    if (rexxPort) {
  143.       RemPort(rexxPort) ;
  144.       bringerdown = 1 ;
  145. /*
  146.  *   A message still hanging around?  We kill it off.
  147.  */
  148.       if (oRexxMsg) {
  149.          oRexxMsg->rm_Result1 = RXERRORIMGONE ;
  150.          ReplyMsg((struct Message *)oRexxMsg) ;
  151.          oRexxMsg = NULL ;
  152.       }
  153.       while (stillNeedReplies) {
  154.          WaitPort(rexxPort) ;
  155.          dispRexxPort() ;
  156.       }
  157.       closeRexxLib() ;
  158.       DeletePort(rexxPort) ;
  159.       rexxPort = NULL ;
  160.    }
  161.    rexxPortBit = 0 ;
  162. }
  163. /*
  164.  *   Here we dispatch any REXX messages that might be outstanding.
  165.  *   This is the main routine for handling Rexx messages.
  166.  *   This function is fast if no messages are outstanding, so it's
  167.  *   pretty safe to call fairly often.
  168.  *
  169.  *   If we are bring the system down and flushing messages, we reply
  170.  *   with a pretty serious return code RXERRORIMGONE.
  171.  *
  172.  *   No arguments, no returns.
  173.  */
  174. static int cmdcmp(char *, char *) ;
  175. static int dontreply ;
  176. void dispRexxPort(void) {
  177.    register struct RexxMsg *RexxMsg ;
  178.    register struct rexxCommandList *rcl ;
  179.    register char *p ;
  180.  
  181. /*
  182.  *   If there's no rexx port, we're out of here.
  183.  */
  184.    if (rexxPort == NULL)
  185.       return ;
  186. /*
  187.  *   Otherwise we have our normal loop on messages.
  188.  */
  189.    while (RexxMsg = (struct RexxMsg *)GetMsg(rexxPort)) {
  190. /*
  191.  *   If we have a reply to a message we sent, we look at the second
  192.  *   argument.  If it's set, it's a function we are supposed to call
  193.  *   so we call it.  Then, we kill the argstring and the message
  194.  *   itself, decrement the outstanding count, and attempt to close
  195.  *   down the Rexx library.  Note that this call only succeeds if
  196.  *   there are no outstanding messages.  Also, it's pretty quick, so
  197.  *   don't talk to me about efficiency.
  198.  */
  199.       if (RexxMsg->rm_Node.mn_Node.ln_Type == NT_REPLYMSG) {
  200.          if (RexxMsg->rm_Args[1])
  201.             ((int (*)(struct RexxMessage *))(RexxMsg->rm_Args[1]))(RexxMsg) ;
  202.          DeleteArgstring(RexxMsg->rm_Args[0]) ;
  203.          DeleteRexxMsg(RexxMsg) ;
  204.          stillNeedReplies-- ;
  205.          closeRexxLib() ;
  206. /*
  207.  *   The default case is we got a message and we need to check it for
  208.  *   primitives.  We skip past any initial tabs or spaces and initialize
  209.  *   the return code fields.
  210.  */
  211.       } else {
  212.          p = (char *)RexxMsg->rm_Args[0] ;
  213.          while (*p > 0 && *p <= ' ')
  214.             p++ ;
  215.          RexxMsg->rm_Result1 = 0 ;
  216.          RexxMsg->rm_Result2 = 0 ;
  217. /*
  218.  *   If somehow the reply is already done or postponed, `dontreply' is
  219.  *   set.
  220.  */
  221.          dontreply = 0 ;
  222. /*
  223.  *   If the sky is falling, we just blow up and replymsg.
  224.  */
  225.          if (bringerdown) {
  226.             RexxMsg->rm_Result1 = RXERRORIMGONE ;
  227. /*
  228.  *   Otherwise we cdr down our association list, comparing commands,
  229.  *   until we get a match.  If we get a match, we call the dispatch
  230.  *   function with the appropriate arguments, and break out.
  231.  */
  232.          } else {
  233.             oRexxMsg = RexxMsg ;
  234.             for (rcl = globalrcl; rcl->name; rcl++) {
  235.                if (cmdcmp(rcl->name, p) == 0) {
  236.                   userdisp(RexxMsg, rcl, p+strlen(rcl->name)) ;
  237.                   break ;
  238.                }
  239.             }
  240. /*
  241.  *   If we broke out, rcl will point to the command we executed; if we
  242.  *   are at the end of the list, we didn't understand the command.  In
  243.  *   this case, if we were supplied an extension in upRexxPort, we know
  244.  *   that we should send the command out, so we do so, synchronously.
  245.  *   The synchronous send takes care of our reply.  If we were given a
  246.  *   NULL extension, we bitch that the command didn't make sense to us.
  247.  */
  248.             if (rcl->name == NULL) {
  249.                if (extension) {
  250.                   syncRexxCmd(RexxMsg->rm_Args[0], RexxMsg) ;
  251.                   dontreply = 1 ;
  252.                } else {
  253.                   RexxMsg->rm_Result1 = RXERRORNOCMD ;
  254.                }
  255.             }
  256.          }
  257. /*
  258.  *   Finally, reply if appropriate.
  259.  */
  260.          oRexxMsg = NULL ;
  261.          if (! dontreply)
  262.             ReplyMsg((struct Message *)RexxMsg) ;
  263.       }
  264.    }
  265. }
  266. /*
  267.  *   This is the function we use to see if the command matches
  268.  *   the command string.  Not case sensitive, and the real command only
  269.  *   need be a prefix of the command string.  Make sure all commands
  270.  *   are given in lower case!
  271.  */
  272. static int cmdcmp(char *c, char *m) {
  273.    while (*c && ((*c == *m) || (*c == *m + 32 && ('a' <= *c && *c <= 'z')))) {
  274.       c++ ;
  275.       m++ ;
  276.    }
  277.    return((int)*c) ;
  278. }
  279. /*
  280.  *   Opens the Rexx library if unopened.  Returns success (1) or
  281.  *   failure (0).  This is another function that is *private* but
  282.  *   that doesn't have to be.
  283.  */
  284. static int openRexxLib(void) {
  285.    if (RexxSysBase)
  286.       return(1) ;
  287.    return((RexxSysBase = (struct RxsLib *)OpenLibrary(RXSNAME, 0L)) != NULL) ;
  288. }
  289. /*
  290.  *   This is the general ARexx command interface, but is not the one
  291.  *   you will use most of the time; ones defined later are easier to
  292.  *   understand and use.  But they all go through here.
  293.  */
  294. struct RexxMsg *sendRexxCmd(
  295. char *s,
  296. /*
  297.  *   The first parameter is the command to send to Rexx.
  298.  */
  299. void (*f)(struct RexxMsg *),
  300. /*
  301.  *   The second parameter is either NULL, indicating that the command
  302.  *   should execute asynchronously, or a function to be called when the
  303.  *   message we build up and send out here finally returns.  Please note
  304.  *   that the function supplied here could be called during cleanup after
  305.  *   a fatal error, so make sure it is `safe'.  This function always is
  306.  *   passed one argument, the RexxMsg that is being replied.
  307.  */
  308. STRPTR p1, STRPTR p2, STRPTR p3)
  309. /*
  310.  *   These are up to three arguments to be stuffed into the RexxMsg we
  311.  *   are building up, making the values available when the message is
  312.  *   finally replied to.  The values are stuffed into Args[2]..Args[4].
  313.  */
  314. {
  315.    struct RexxMsg *CreateRexxMsg() ;
  316.    STRPTR CreateArgstring() ;
  317.    register struct MsgPort *rexxport ;
  318.    register struct RexxMsg *RexxMsg ;
  319.  
  320. /*
  321.  *   If we have too many replies out there, we just return failure.
  322.  *   Note that you should check the return code to make sure your
  323.  *   message got out!  Then, we forbid, and make sure that:
  324.  *      - we have a rexx port open
  325.  *      - Rexx is out there
  326.  *      - the library is open
  327.  *      - we can create a message
  328.  *      - we can create an argstring
  329.  *
  330.  *   If all of these succeed, we stuff a few values and send the
  331.  *   message, permit, and return.
  332.  */
  333.    if (rexxPort == NULL || stillNeedReplies > MAXRXOUTSTANDING-1)
  334.       return(NULL) ;
  335.    RexxMsg = NULL ;
  336.    if (openRexxLib() && (RexxMsg =
  337.              CreateRexxMsg(rexxPort, extension, rexxPort->mp_Node.ln_Name)) &&
  338.              (RexxMsg->rm_Args[0] = CreateArgstring(s, (long)strlen(s)))) {
  339.       RexxMsg->rm_Action = RXCOMM ;
  340.       RexxMsg->rm_Args[1] = (STRPTR)f ;
  341.       RexxMsg->rm_Args[2] = p1 ;
  342.       RexxMsg->rm_Args[3] = p2 ;
  343.       RexxMsg->rm_Args[4] = p3 ;
  344.       RexxMsg->rm_Node.mn_Node.ln_Name = RXSDIR ;
  345.       Forbid() ;
  346.       if (rexxport = FindPort(RXSDIR))
  347.          PutMsg(rexxport, (struct Message *)RexxMsg) ;
  348.       Permit() ;
  349.       if (rexxport) {
  350.          stillNeedReplies++ ;
  351.          return(RexxMsg) ;
  352.       } else
  353.          DeleteArgstring(RexxMsg->rm_Args[0]) ;
  354.    }
  355.    if (RexxMsg)
  356.       DeleteRexxMsg(RexxMsg) ;
  357.    closeRexxLib() ;
  358.    return(NULL) ;
  359. }
  360. /*
  361.  *   This function is used to send out an ARexx message and return
  362.  *   immediately.  Its single parameter is the command to send.
  363.  */
  364. struct RexxMsg *asyncRexxCmd(char *s) {
  365.    return(sendRexxCmd(s, NULL, NULL, NULL, NULL)) ;
  366. }
  367. /*
  368.  *   This function sets things up to reply to the message that caused
  369.  *   it when we get a reply to the message we are sending out here.
  370.  *   But first the function we pass in, which actually handles the reply.
  371.  *   Note how we get the message from the Args[2]; Args[0] is the command,
  372.  *   Args[1] is this function, and Args[2]..Args[4] are any parameters
  373.  *   passed to sendRexxCmd() as p1..p3.  We pass the result codes right
  374.  *   along.
  375.  */
  376. static void replytoit(struct RexxMsg *msg) {
  377.    register struct RexxMsg *omsg ;
  378.  
  379.    omsg = (struct RexxMsg *)(msg->rm_Args[2]) ;
  380.    replyRexxCmd(omsg, msg->rm_Result1, msg->rm_Result2, NULL) ;
  381.    ReplyMsg((struct Message *)omsg) ;
  382. }
  383. /*
  384.  *   This function makes use of everything we've put together so far,
  385.  *   and functions as a synchronous Rexx call; as soon as the macro
  386.  *   invoked here returns, we reply to `msg', passing the return codes
  387.  *   back.
  388.  */
  389. struct RexxMsg *syncRexxCmd(char *s, struct RexxMsg *msg) {
  390.    return(sendRexxCmd(s, replytoit, (STRPTR)msg, NULL, NULL)) ;
  391. }
  392. /*
  393.  *   There are times when you want to pass back return codes or a
  394.  *   return string; call this function when you want to do that,
  395.  *   and return `1' from the user dispatch function so the main
  396.  *   event loop doesn't reply (because we reply here.)  This function
  397.  *   always returns 1.
  398.  */
  399. void replyRexxCmd(
  400. /*
  401.  *   The first parameter is the message we are replying to.
  402.  */
  403. register struct RexxMsg *msg,
  404. /*
  405.  *   The next two parameters are the primary and secondary return
  406.  *   codes.
  407.  */
  408. register long primary, long secondary,
  409. /*
  410.  *   The final parameter is a return string.  This string is only
  411.  *   returned if the primary return code is 0, and a string was
  412.  *   requested.
  413.  *
  414.  *   We also note that we have replied to the message that came in.
  415.  */
  416. register char *string) {
  417. /*
  418.  *   Note how we make sure the Rexx Library is open before calling
  419.  *   CreateArgstring . . . and we close it down at the end, if possible.
  420.  */
  421.    if (primary == 0 && (msg->rm_Action & (1L << RXFB_RESULT))) {
  422.       if (string && openRexxLib())
  423.          secondary = (long)CreateArgstring(string, (long)strlen(string)) ;
  424.       else
  425.          secondary = 0L ;
  426.    }
  427.    msg->rm_Result1 = primary ;
  428.    msg->rm_Result2 = secondary ;
  429.    closeRexxLib() ;
  430. }
  431. /*
  432.  *   If we don't want to reply, we call this.
  433.  */
  434. void DontReply(void) {
  435.    dontreply = 1 ;
  436. }
  437.