home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 1 / FFMCD01.bin / bbs / libdisks / d700t799 / disk793.lha / Snap / src.lha / src / minrexx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-15  |  15.1 KB  |  446 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((struct Library *)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((struct Message *)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 *RexxMsg ;
  178.    int cmdcmp() ;
  179.    register struct rexxCommandList *rcl ;
  180.    register char *p ;
  181.    register int dontreply ;
  182.  
  183. /*
  184.  *   If there's no rexx port, we're out of here.
  185.  */
  186.    if (rexxPort == NULL)
  187.       return ;
  188. /*
  189.  *   Otherwise we have our normal loop on messages.
  190.  */
  191.    while (RexxMsg = (struct RexxMsg *)GetMsg(rexxPort)) {
  192. /*
  193.  *   If we have a reply to a message we sent, we look at the second
  194.  *   argument.  If it's set, it's a function we are supposed to call
  195.  *   so we call it.  Then, we kill the argstring and the message
  196.  *   itself, decrement the outstanding count, and attempt to close
  197.  *   down the Rexx library.  Note that this call only succeeds if
  198.  *   there are no outstanding messages.  Also, it's pretty quick, so
  199.  *   don't talk to me about efficiency.
  200.  */
  201.       if (RexxMsg->rm_Node.mn_Node.ln_Type == NT_REPLYMSG) {
  202.          if (RexxMsg->rm_Args[1]) {
  203.             ((int (*)())(RexxMsg->rm_Args[1]))(RexxMsg) ;
  204.          }
  205.          DeleteArgstring(RexxMsg->rm_Args[0]) ;
  206.          DeleteRexxMsg(RexxMsg) ;
  207.          stillNeedReplies-- ;
  208.          closeRexxLib() ;
  209. /*
  210.  *   The default case is we got a message and we need to check it for
  211.  *   primitives.  We skip past any initial tabs or spaces and initialize
  212.  *   the return code fields.
  213.  */
  214.       } else {
  215.          p = (char *)RexxMsg->rm_Args[0] ;
  216.          while (*p > 0 && *p <= ' ')
  217.             p++ ;
  218.          RexxMsg->rm_Result1 = 0 ;
  219.          RexxMsg->rm_Result2 = 0 ;
  220. /*
  221.  *   If somehow the reply is already done or postponed, `dontreply' is
  222.  *   set.
  223.  */
  224.          dontreply = 0 ;
  225. /*
  226.  *   If the sky is falling, we just blow up and replymsg.
  227.  */
  228.          if (bringerdown) {
  229.             RexxMsg->rm_Result1 = RXERRORIMGONE ;
  230. /*
  231.  *   Otherwise we cdr down our association list, comparing commands,
  232.  *   until we get a match.  If we get a match, we call the dispatch
  233.  *   function with the appropriate arguments, and break out.
  234.  */
  235.          } else {
  236.             oRexxMsg = RexxMsg ;
  237.             for (rcl = globalrcl; rcl->name; rcl++) {
  238.                if (cmdcmp(rcl->name, p) == 0) {
  239.                   userdisp(RexxMsg, rcl, p+strlen(rcl->name)) ;
  240.                   break ;
  241.                }
  242.             }
  243. /*
  244.  *   If we broke out, rcl will point to the command we executed; if we
  245.  *   are at the end of the list, we didn't understand the command.  In
  246.  *   this case, if we were supplied an extension in upRexxPort, we know
  247.  *   that we should send the command out, so we do so, synchronously.
  248.  *   The synchronous send takes care of our reply.  If we were given a
  249.  *   NULL extension, we bitch that the command didn't make sense to us.
  250.  */
  251.             if (rcl->name == NULL) {
  252.                if (extension) {
  253.                   syncRexxCmd(RexxMsg->rm_Args[0], RexxMsg) ;
  254.                   dontreply = 1 ;
  255.                } else {
  256.                   RexxMsg->rm_Result1 = RXERRORNOCMD ;
  257.                }
  258.             }
  259.          }
  260. /*
  261.  *   Finally, reply if appropriate.
  262.  */
  263.          oRexxMsg = NULL ;
  264.          if (! dontreply)
  265.             ReplyMsg((struct Message *)RexxMsg) ;
  266.       }
  267.    }
  268. }
  269. /*
  270.  *   This is the function we use to see if the command matches
  271.  *   the command string.  Not case sensitive, and the real command only
  272.  *   need be a prefix of the command string.  Make sure all commands
  273.  *   are given in lower case!
  274.  */
  275. static int cmdcmp(c, m)
  276. register char *c, *m ;
  277. {
  278.    while (*c && ((*c == *m) || (*c == *m + 32 && ('a' <= *c && *c <= 'z')))) {
  279.       c++ ;
  280.       m++ ;
  281.    }
  282.    return((int)*c) ;
  283. }
  284. /*
  285.  *   Opens the Rexx library if unopened.  Returns success (1) or
  286.  *   failure (0).  This is another function that is *private* but
  287.  *   that doesn't have to be.
  288.  */
  289. static int openRexxLib() {
  290.    if (RexxSysBase)
  291.       return(1) ;
  292.    return((RexxSysBase = (struct RxsLib *)OpenLibrary(RXSNAME, 0L)) != NULL) ;
  293. }
  294. /*
  295.  *   This is the general ARexx command interface, but is not the one
  296.  *   you will use most of the time; ones defined later are easier to
  297.  *   understand and use.  But they all go through here.
  298.  */
  299. struct RexxMsg *sendRexxCmd(s, f, p1, p2, p3)
  300. char *s ;
  301. /*
  302.  *   The first parameter is the command to send to Rexx.
  303.  */
  304. int (*f)() ;
  305. /*
  306.  *   The second parameter is either NULL, indicating that the command
  307.  *   should execute asynchronously, or a function to be called when the
  308.  *   message we build up and send out here finally returns.  Please note
  309.  *   that the function supplied here could be called during cleanup after
  310.  *   a fatal error, so make sure it is `safe'.  This function always is
  311.  *   passed one argument, the RexxMsg that is being replied.
  312.  */
  313. STRPTR p1, p2, p3 ;
  314. /*
  315.  *   These are up to three arguments to be stuffed into the RexxMsg we
  316.  *   are building up, making the values available when the message is
  317.  *   finally replied to.  The values are stuffed into Args[2]..Args[4].
  318.  */
  319. {
  320.    struct RexxMsg *CreateRexxMsg() ;
  321.    STRPTR CreateArgstring() ;
  322.    register struct MsgPort *rexxport ;
  323.    register struct RexxMsg *RexxMsg ;
  324.  
  325. /*
  326.  *   If we have too many replies out there, we just return failure.
  327.  *   Note that you should check the return code to make sure your
  328.  *   message got out!  Then, we forbid, and make sure that:
  329.  *      - we have a rexx port open
  330.  *      - Rexx is out there
  331.  *      - the library is open
  332.  *      - we can create a message
  333.  *      - we can create an argstring
  334.  *
  335.  *   If all of these succeed, we stuff a few values and send the
  336.  *   message, permit, and return.
  337.  */
  338.    if (rexxPort == NULL || stillNeedReplies > MAXRXOUTSTANDING-1)
  339.       return(NULL) ;
  340.    RexxMsg = NULL ;
  341.    if (openRexxLib() && (RexxMsg =
  342.              CreateRexxMsg(rexxPort, extension, rexxPort->mp_Node.ln_Name)) &&
  343.              (RexxMsg->rm_Args[0] = CreateArgstring(s, (long)strlen(s)))) {
  344.       RexxMsg->rm_Action = RXCOMM ;
  345.       RexxMsg->rm_Args[1] = (STRPTR)f ;
  346.       RexxMsg->rm_Args[2] = p1 ;
  347.       RexxMsg->rm_Args[3] = p2 ;
  348.       RexxMsg->rm_Args[4] = p3 ;
  349.       RexxMsg->rm_Node.mn_Node.ln_Name = RXSDIR ;
  350.       Forbid() ;
  351.       if (rexxport = FindPort(RXSDIR))
  352.          PutMsg(rexxport, (struct Message *)RexxMsg) ;
  353.       Permit() ;
  354.       if (rexxport) {
  355.          stillNeedReplies++ ;
  356.          return(RexxMsg) ;
  357.       } else
  358.          DeleteArgstring(RexxMsg->rm_Args[0]) ;
  359.    }
  360.    if (RexxMsg)
  361.       DeleteRexxMsg(RexxMsg) ;
  362.    closeRexxLib() ;
  363.    return(NULL) ;
  364. }
  365. /*
  366.  *   This function is used to send out an ARexx message and return
  367.  *   immediately.  Its single parameter is the command to send.
  368.  */
  369. struct RexxMsg *asyncRexxCmd(s)
  370. char *s ;
  371. {
  372.    return(sendRexxCmd(s, NULL, NULL, NULL, NULL)) ;
  373. }
  374. /*
  375.  *   This function sets things up to reply to the message that caused
  376.  *   it when we get a reply to the message we are sending out here.
  377.  *   But first the function we pass in, which actually handles the reply.
  378.  *   Note how we get the message from the Args[2]; Args[0] is the command,
  379.  *   Args[1] is this function, and Args[2]..Args[4] are any parameters
  380.  *   passed to sendRexxCmd() as p1..p3.  We pass the result codes right
  381.  *   along.
  382.  */
  383. static void replytoit(msg)
  384. register struct RexxMsg *msg ;
  385. {
  386.    register struct RexxMsg *omsg ;
  387.  
  388.    omsg = (struct RexxMsg *)(msg->rm_Args[2]) ;
  389.    replyRexxCmd(omsg, msg->rm_Result1, msg->rm_Result2, NULL) ;
  390.    ReplyMsg((struct Message *)omsg) ;
  391. }
  392. /*
  393.  *   This function makes use of everything we've put together so far,
  394.  *   and functions as a synchronous Rexx call; as soon as the macro
  395.  *   invoked here returns, we reply to `msg', passing the return codes
  396.  *   back.
  397.  */
  398. struct RexxMsg *syncRexxCmd(s, msg)
  399. char *s ;
  400. struct RexxMsg *msg ;
  401. {
  402.    return(sendRexxCmd(s, (APTR)&replytoit, msg, NULL, NULL)) ;
  403. }
  404. /*
  405.  *   There are times when you want to pass back return codes or a
  406.  *   return string; call this function when you want to do that,
  407.  *   and return `1' from the user dispatch function so the main
  408.  *   event loop doesn't reply (because we reply here.)  This function
  409.  *   always returns 1.
  410.  */
  411. void replyRexxCmd(msg, primary, secondary, string)
  412. /*
  413.  *   The first parameter is the message we are replying to.
  414.  */
  415. register struct RexxMsg *msg ;
  416. /*
  417.  *   The next two parameters are the primary and secondary return
  418.  *   codes.
  419.  */
  420. register long primary, secondary ;
  421. /*
  422.  *   The final parameter is a return string.  This string is only
  423.  *   returned if the primary return code is 0, and a string was
  424.  *   requested.
  425.  *
  426.  *   We also note that we have replied to the message that came in.
  427.  */
  428. register char *string ;
  429. {
  430.    STRPTR CreateArgstring() ;
  431.  
  432. /*
  433.  *   Note how we make sure the Rexx Library is open before calling
  434.  *   CreateArgstring . . . and we close it down at the end, if possible.
  435.  */
  436.    if (primary == 0 && (msg->rm_Action & (1L << RXFB_RESULT))) {
  437.       if (string && openRexxLib())
  438.          secondary = (long)CreateArgstring(string, (long)strlen(string)) ;
  439.       else
  440.          secondary = 0L ;
  441.    }
  442.    msg->rm_Result1 = primary ;
  443.    msg->rm_Result2 = secondary ;
  444.    closeRexxLib() ;
  445. }
  446.