home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d3xx / d326 / snap.lha / Snap / source / minrexx.c < prev    next >
C/C++ Source or Header  |  1990-03-05  |  15KB  |  448 lines

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