home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff284.lzh / Dme / src / rexx.c < prev    next >
C/C++ Source or Header  |  1989-11-27  |  5KB  |  243 lines

  1.  
  2. /*
  3.  *  REXX.C
  4.  *
  5.  *    (c) Copyright 1987 by Kim DeVaughn, All Rights Reserved
  6.  *
  7.  *  ARexx interface code, etc.
  8.  *
  9.  */
  10.  
  11. #include "defs.h"
  12. #include "rexx.h"
  13. #include <stdio.h>
  14.  
  15. #if AREXX
  16.  
  17. int foundcmd;        /* control for implicit ARexx macro invocation   */
  18. int cmderr;        /* global command error flag for do_rexx()'s use */
  19.  
  20. __stdargs APTR CreateRexxMsg ARGS((PORT *, char *, char *));
  21. __stdargs void DeleteRexxMsg ARGS((struct RexxMsg *));
  22. __stdargs APTR CreateArgstring ARGS((char *, int));
  23. __stdargs void DeleteArgstring ARGS((struct RexxArg *));
  24. __stdargs int IsRexxMsg ARGS((struct RexxMsg *));
  25. __stdargs void InitPort ARGS((PORT *, char *));
  26. __stdargs void FreePort ARGS((PORT *));
  27.  
  28. struct RxsLib *RexxSysBase;
  29.  
  30. typedef struct Message MSG;
  31.  
  32. /*
  33.  * initialization for ARexx ... just open rexsyslib.library
  34.  */
  35.  
  36. void
  37. openrexx()
  38. {
  39.     RexxSysBase = (struct RxsLib *)OpenLibrary("rexxsyslib.library", (ULONG)RXSVERS);
  40.     return;
  41. }
  42.  
  43.  
  44.  
  45. /*
  46.  * cleanup any open ARexx stuff ...  just close rexsyslib.library for now
  47.  */
  48.  
  49. void
  50. closerexx()
  51. {
  52.     if (RexxSysBase)
  53.     CloseLibrary((struct Library *)RexxSysBase);
  54. }
  55.  
  56.  
  57.  
  58. /*
  59.  *  explicit invocation interface between do_command() and do_rexx
  60.  *  for ARexx macros having NO arguments (i.e., for the "rx" command)
  61.  */
  62.  
  63. void
  64. do_rx()
  65. {
  66.     do_rexx(av[1]);
  67. }
  68.  
  69.  
  70.  
  71. /*
  72.  *  explicit invocation interface between do_command() and do_rexx
  73.  *  for ARexx macros having ONE argument (i.e., for the "rx1" command)
  74.  */
  75.  
  76. void
  77. do_rx1()
  78. {
  79.     char macbuf[256];
  80.  
  81.     strcpy(macbuf, av[1]);
  82.     strcat(macbuf, " ");
  83.     strcat(macbuf, av[2]);
  84.     do_rexx(macbuf);
  85. }
  86.  
  87.  
  88.  
  89. /*
  90.  *  explicit invocation interface between do_command() and do_rexx
  91.  *  for ARexx macros having TWO arguments (i.e., for the "rx2" command)
  92.  */
  93.  
  94. void
  95. do_rx2()
  96. {
  97.     char macbuf[256];
  98.  
  99.     strcpy(macbuf, av[1]);
  100.     strcat(macbuf, " ");
  101.     strcat(macbuf, av[2]);
  102.     strcat(macbuf, " ");
  103.     strcat(macbuf, av[3]);
  104.     do_rexx(macbuf);
  105. }
  106.  
  107.  
  108.  
  109. /*
  110.  *  implicit invocation interface between do_command() and do_rexx
  111.  *  for ARexx macros implicitly called; arbitrary number of arguments
  112.  */
  113.  
  114. void
  115. do_rxImplied(cmd, args)
  116. char *cmd;
  117. char *args;
  118. {
  119.     char macbuf[256];
  120.  
  121.     strcpy(macbuf, cmd);
  122.     strcat(macbuf, " ");
  123.     strcat(macbuf, args);
  124.     do_rexx(macbuf);
  125. }
  126.  
  127. /*
  128.  *  issue a command to ARexx ...
  129.  */
  130.  
  131. int
  132. do_rexx(macstr)
  133. char *macstr;
  134. {
  135.     struct RexxArg *macarg;
  136.  
  137.     struct MsgPort  RexxPort;
  138.     struct MsgPort *ARexxPort;
  139.  
  140.     struct RexxMsg *macptr;
  141.     struct RexxMsg *cmdptr;
  142.  
  143.     char host[16];
  144.     char hexbuf[12];        /* should only need 9 bytes */
  145.     char errmsg[80];        /* don't build a larger error message */
  146.  
  147.     int  ret;
  148.     int  err;
  149.  
  150.  
  151.     if (RexxSysBase == 0) {
  152.     title("Unknown Command   -   No Macros:  ARexx Not Installed ");   /* no rexxsyslib */
  153.     return(0);
  154.     }
  155.  
  156.     BZero(&RexxPort, sizeof(struct MsgPort));
  157.     strcpy(host, "DME");
  158.     sprintf(hexbuf, "%08x", &RexxPort);
  159.     strcat(host, hexbuf);
  160.     InitPort(&RexxPort, host);      /* need to error check this */
  161.     AddPort(&RexxPort);
  162.     /* return here if InitPort failed */
  163.  
  164.  
  165.     if (macarg = (struct RexxArg *)CreateArgstring(macstr, strlen(macstr))) {
  166.     if (macptr = (struct RexxMsg *)CreateRexxMsg(&RexxPort, "dme", host)) {
  167.         ACTION(macptr) = RXCOMM;
  168.         ARG0(macptr)   = (STRPTR)macarg;
  169.  
  170.         Forbid();
  171.         if (ARexxPort = (struct MsgPort *)FindPort("REXX")) {
  172.         PutMsg(ARexxPort, (MSG *)macptr);
  173.         Permit();
  174.         title("Calling ARexx Macro ... ");
  175.  
  176.         for (;;) {
  177.             WaitPort(&RexxPort);
  178.             cmdptr = (struct RexxMsg *)GetMsg(&RexxPort);
  179.  
  180.             if (IsRexxMsg(cmdptr)) {
  181.  
  182.             foundcmd = 0;
  183.             cmderr = CMD_INITIAL;
  184.             ret = do_command(ARG0(cmdptr));
  185.             err = cmderr;
  186.             if (foundcmd) {
  187.                 ret = (ret == 1) ? 0 : 5;   /* cmd error:  RC = 5  */
  188.             } else {
  189.                 ret = do_rexx(ARG0(cmdptr));    /* another macro? */
  190.             }
  191.  
  192.             RESULT1(cmdptr) = ret;
  193.             RESULT2(cmdptr) = 0;
  194.             ReplyMsg((MSG *)cmdptr);
  195.             }
  196.             do_command("null");     /* a kludge to set "foundcmd" */
  197.             if (macptr == cmdptr) break;
  198.         }
  199.  
  200.  
  201.         if (ret = RESULT1(cmdptr)) {
  202.             if (RESULT2(cmdptr)) {
  203.             if (RESULT2(cmdptr) == 1) {
  204.                 title("Unknown Command ");
  205.             } else {
  206.                 sprintf(errmsg, "ARexx Macro Error:  Code = %d  Severity = %d ", RESULT2(cmdptr), ret);
  207.                 title(errmsg);
  208.             }
  209.             } else {
  210.             sprintf(errmsg, "User Specified Macro Error:  RC = %d ", ret);
  211.             title(errmsg);
  212.             }
  213.         } else {
  214.             if (err <= TITLE_THRESHHOLD) {
  215.             title("OK ");
  216.             }
  217.         }
  218.         ret = ret + err;
  219.         } else {
  220.         Permit();
  221.         title("Unknown Command   -   No Macros:  ARexx Not Active ");   /* no REXX port */
  222.  
  223.         ret = -1;
  224.         }
  225.         DeleteRexxMsg(macptr);
  226.     } else {
  227.         title("CreateRexxMsg() Failed ");   /* may be overkill, and not need to ckeck this */
  228.         ret = -1;
  229.     }
  230.     DeleteArgstring(macarg);
  231.     } else {
  232.     title("CreateArgstring() Failed ");     /* may be overkill, and not need to check this */
  233.     ret = -1;
  234.     }
  235.  
  236.     RemPort(&RexxPort);
  237.     FreePort(&RexxPort);
  238.     return(ret);
  239. }
  240.  
  241. #endif
  242.  
  243.