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