home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff276.lzh / DateRequester / minrexx.c < prev    next >
C/C++ Source or Header  |  1989-11-09  |  15KB  |  449 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 int (*userdisp)() ;                 /* the user's dispatch function */
  36. static struct RexxMsg *oRexxMsg ;          /* the outstanding Rexx message */
  37. /*
  38.  *   Our library base.  Don't you dare close this!
  39.  */
  40. struct RxsLib *RexxSysBase ;
  41. /*
  42.  *   This is the main entry point into this code.
  43.  */
  44. long upRexxPort(s, rcl, exten, uf)
  45. /*
  46.  *   The first argument is the name of your port to be registered;
  47.  *   this will be used, for instance, with the `address' command of ARexx.
  48.  */
  49. char *s ;
  50. /*
  51.  *   The second argument is an association list of command-name/user-data
  52.  *   pairs.  It's an array of struct rexxCommandList, terminated by a
  53.  *   structure with a NULL in the name field. The commands are case
  54.  *   sensitive.  The user-data field can contain anything appropriate,
  55.  *   perhaps a function to call or some other data.
  56.  */
  57. struct rexxCommandList *rcl ;
  58. /*
  59.  *   The third argument is the file extension for ARexx macros invoked
  60.  *   by this program.  If you supply this argument, any `primitive' not
  61.  *   in the association list rcl will be sent out to ARexx for
  62.  *   interpretation, thus allowing macro programs to work just like
  63.  *   primitives.  If you do not want this behavior, supply a `NULL'
  64.  *   here, and those commands not understood will be replied with an
  65.  *   error value of RXERRORNOCMD.
  66.  */
  67. char *exten ;
  68. /*
  69.  *   The fourth argument is the user dispatch function.  This function
  70.  *   will *only* be called from rexxDisp(), either from the user calling
  71.  *   this function directly, or from dnRexxPort().  Anytime a command
  72.  *   match is found in the association list, this user-supplied function
  73.  *   will be called with two arguments---the Rexx message that was
  74.  *   received, and a pointer to the association pair.  This function
  75.  *   should return a `1' if the message was replied to by the function
  76.  *   and a `0' if the default success code of (0, 0) should be returned.
  77.  *   Note that the user function should never ReplyMsg() the message;
  78.  *   instead he should indicate the return values with replyRexxCmd();
  79.  *   otherwise we lose track of the messages that still lack replies.
  80.  */
  81. int (*uf)() ;
  82. /*
  83.  *   upRexxPort() returns the signal bit to wait on for Rexx messages.
  84.  *   If something goes wrong, it simply returns a `0'.  Note that this
  85.  *   function is safe to call multiple times because we check to make
  86.  *   sure we haven't opened already.  It's also a quick way to change
  87.  *   the association list or dispatch function.
  88.  */
  89. {
  90.    struct MsgPort *FindPort() ;
  91.    struct MsgPort *CreatePort() ;
  92.  
  93. /*
  94.  *   Some basic error checking.
  95.  */
  96.    if (rcl == NULL || uf == NULL)
  97.       return(0L) ;
  98. /*
  99.  *   If we aren't open, we make sure no one else has opened a port with
  100.  *   this name already.  If that works, and the createport succeeds, we
  101.  *   fill rexxPortBit with the value to return.
  102.  *
  103.  *   Note that rexxPortBit will be 0 iff rexxPort is NULL, so the check
  104.  *   for rexxPort == NULL also insures that our rexxPortBit is 0.
  105.  */
  106.    if (rexxPort == NULL) {
  107.       Forbid() ;
  108.       if (FindPort(s)==NULL)
  109.          rexxPort = CreatePort(s, 0L) ;
  110.       Permit() ;
  111.       if (rexxPort != NULL)
  112.          rexxPortBit = 1L << rexxPort->mp_SigBit ;
  113.    }
  114. /*
  115.  *   Squirrel away these values for our own internal access, and return
  116.  *   the wait bit.
  117.  */
  118.    globalrcl = rcl ;
  119.    extension = exten ;
  120.    userdisp = uf ;
  121.    return(rexxPortBit) ;
  122. }
  123. /*
  124.  *   This function closes the rexx library, but only if it is open
  125.  *   and we aren't expecting further replies from REXX.  It's
  126.  *   *private*, but it doesn't have to be; it's pretty safe to
  127.  *   call anytime.
  128.  */
  129. static void closeRexxLib() {
  130.    if (stillNeedReplies == 0 && RexxSysBase) {
  131.       CloseLibrary(RexxSysBase) ;
  132.       RexxSysBase = NULL ;
  133.    }
  134. }
  135. /*
  136.  *   This function closes down the Rexx port.  It is always safe to
  137.  *   call, and should *definitely* be made a part of your cleanup
  138.  *   routine.  No arguments and no return.  It removes the Rexx port,
  139.  *   replies to all of the messages and insures that we get replies
  140.  *   to all the ones we sent out, closes the Rexx library, deletes the
  141.  *   port, clears a few flags, and leaves.
  142.  */
  143. void dnRexxPort() {
  144.    if (rexxPort) {
  145.       RemPort(rexxPort) ;
  146.       bringerdown = 1 ;
  147. /*
  148.  *   A message still hanging around?  We kill it off.
  149.  */
  150.       if (oRexxMsg) {
  151.          oRexxMsg->rm_Result1 = RXERRORIMGONE ;
  152.          ReplyMsg(oRexxMsg) ;
  153.          oRexxMsg = NULL ;
  154.       }
  155.       while (stillNeedReplies) {
  156.          WaitPort(rexxPort) ;
  157.          dispRexxPort() ;
  158.       }
  159.       closeRexxLib() ;
  160.       DeletePort(rexxPort) ;
  161.       rexxPort = NULL ;
  162.    }
  163.    rexxPortBit = 0 ;
  164. }
  165. /*
  166.  *   Here we dispatch any REXX messages that might be outstanding.
  167.  *   This is the main routine for handling Rexx messages.
  168.  *   This function is fast if no messages are outstanding, so it's
  169.  *   pretty safe to call fairly often.
  170.  *
  171.  *   If we are bring the system down and flushing messages, we reply
  172.  *   with a pretty serious return code RXERRORIMGONE.
  173.  *
  174.  *   No arguments, no returns.
  175.  */
  176. void dispRexxPort() {
  177.    register struct RexxMsg *GetMsg() ;
  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.    struct RxsLib *OpenLibrary() ;
  292.  
  293.    if (RexxSysBase)
  294.       return(1) ;
  295.    return((RexxSysBase = OpenLibrary(RXSNAME, 0L)) != NULL) ;
  296. }
  297. /*
  298.  *   This is the general ARexx command interface, but is not the one
  299.  *   you will use most of the time; ones defined later are easier to
  300.  *   understand and use.  But they all go through here.
  301.  */
  302. struct RexxMsg *sendRexxCmd(s, f, p1, p2, p3)
  303. char *s ;
  304. /*
  305.  *   The first parameter is the command to send to Rexx.
  306.  */
  307. int (*f)() ;
  308. /*
  309.  *   The second parameter is either NULL, indicating that the command
  310.  *   should execute asynchronously, or a function to be called when the
  311.  *   message we build up and send out here finally returns.  Please note
  312.  *   that the function supplied here could be called during cleanup after
  313.  *   a fatal error, so make sure it is `safe'.  This function always is
  314.  *   passed one argument, the RexxMsg that is being replied.
  315.  */
  316. STRPTR p1, p2, p3 ;
  317. /*
  318.  *   These are up to three arguments to be stuffed into the RexxMsg we
  319.  *   are building up, making the values available when the message is
  320.  *   finally replied to.  The values are stuffed into Args[2]..Args[4].
  321.  */
  322. {
  323.    struct RexxMsg *CreateRexxMsg() ;
  324.    STRPTR CreateArgstring() ;
  325.    register struct MsgPort *rexxport ;
  326.    register struct RexxMsg *RexxMsg ;
  327.  
  328. /*
  329.  *   If we have too many replies out there, we just return failure.
  330.  *   Note that you should check the return code to make sure your
  331.  *   message got out!  Then, we forbid, and make sure that:
  332.  *      - we have a rexx port open
  333.  *      - Rexx is out there
  334.  *      - the library is open
  335.  *      - we can create a message
  336.  *      - we can create an argstring
  337.  *
  338.  *   If all of these succeed, we stuff a few values and send the
  339.  *   message, permit, and return.
  340.  */
  341.    if (rexxPort == NULL || stillNeedReplies > MAXRXOUTSTANDING-1)
  342.       return(NULL) ;
  343.    RexxMsg = NULL ;
  344.    if (openRexxLib() && (RexxMsg =
  345.              CreateRexxMsg(rexxPort, extension, rexxPort->mp_Node.ln_Name)) &&
  346.              (RexxMsg->rm_Args[0] = CreateArgstring(s, (long)strlen(s)))) {
  347.       RexxMsg->rm_Action = RXCOMM ;
  348.       RexxMsg->rm_Args[1] = (STRPTR)f ;
  349.       RexxMsg->rm_Args[2] = p1 ;
  350.       RexxMsg->rm_Args[3] = p2 ;
  351.       RexxMsg->rm_Args[4] = p3 ;
  352.       RexxMsg->rm_Node.mn_Node.ln_Name = RXSDIR ;
  353.       Forbid() ;
  354.       if (rexxport = FindPort(RXSDIR))
  355.          PutMsg(rexxport, RexxMsg) ;
  356.       Permit() ;
  357.       if (rexxport) {
  358.          stillNeedReplies++ ;
  359.          return(RexxMsg) ;
  360.       } else
  361.          DeleteArgstring(RexxMsg->rm_Args[0]) ;
  362.    }
  363.    if (RexxMsg)
  364.       DeleteRexxMsg(RexxMsg) ;
  365.    closeRexxLib() ;
  366.    return(NULL) ;
  367. }
  368. /*
  369.  *   This function is used to send out an ARexx message and return
  370.  *   immediately.  Its single parameter is the command to send.
  371.  */
  372. struct RexxMsg *asyncRexxCmd(s)
  373. char *s ;
  374. {
  375.    return(sendRexxCmd(s, NULL, NULL, NULL, NULL)) ;
  376. }
  377. /*
  378.  *   This function sets things up to reply to the message that caused
  379.  *   it when we get a reply to the message we are sending out here.
  380.  *   But first the function we pass in, which actually handles the reply.
  381.  *   Note how we get the message from the Args[2]; Args[0] is the command,
  382.  *   Args[1] is this function, and Args[2]..Args[4] are any parameters
  383.  *   passed to sendRexxCmd() as p1..p3.  We pass the result codes right
  384.  *   along.
  385.  */
  386. static void replytoit(msg)
  387. register struct RexxMsg *msg ;
  388. {
  389.    register struct RexxMsg *omsg ;
  390.  
  391.    omsg = (struct RexxMsg *)(msg->rm_Args[2]) ;
  392.    replyRexxCmd(omsg, msg->rm_Result1, msg->rm_Result2, NULL) ;
  393.    ReplyMsg(omsg) ;
  394. }
  395. /*
  396.  *   This function makes use of everything we've put together so far,
  397.  *   and functions as a synchronous Rexx call; as soon as the macro
  398.  *   invoked here returns, we reply to `msg', passing the return codes
  399.  *   back.
  400.  */
  401. struct RexxMsg *syncRexxCmd(s, msg)
  402. char *s ;
  403. struct RexxMsg *msg ;
  404. {
  405.    return(sendRexxCmd(s, (APTR)&replytoit, msg, NULL, NULL)) ;
  406. }
  407. /*
  408.  *   There are times when you want to pass back return codes or a
  409.  *   return string; call this function when you want to do that,
  410.  *   and return `1' from the user dispatch function so the main
  411.  *   event loop doesn't reply (because we reply here.)  This function
  412.  *   always returns 1.
  413.  */
  414. void replyRexxCmd(msg, primary, secondary, string)
  415. /*
  416.  *   The first parameter is the message we are replying to.
  417.  */
  418. register struct RexxMsg *msg ;
  419. /*
  420.  *   The next two parameters are the primary and secondary return
  421.  *   codes.
  422.  */
  423. register long primary, secondary ;
  424. /*
  425.  *   The final parameter is a return string.  This string is only
  426.  *   returned if the primary return code is 0, and a string was
  427.  *   requested.
  428.  *
  429.  *   We also note that we have replied to the message that came in.
  430.  */
  431. register char *string ;
  432. {
  433.    STRPTR CreateArgstring() ;
  434.  
  435. /*
  436.  *   Note how we make sure the Rexx Library is open before calling
  437.  *   CreateArgstring . . . and we close it down at the end, if possible.
  438.  */
  439.    if (primary == 0 && (msg->rm_Action & (1L << RXFB_RESULT))) {
  440.       if (string && openRexxLib())
  441.          secondary = (long)CreateArgstring(string, (long)strlen(string)) ;
  442.       else
  443.          secondary = 0L ;
  444.    }
  445.    msg->rm_Result1 = primary ;
  446.    msg->rm_Result2 = secondary ;
  447.    closeRexxLib() ;
  448. }
  449.