home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff330.lzh / Vt100 / Src.lzh / Src / rexx.c < prev    next >
C/C++ Source or Header  |  1990-03-01  |  4KB  |  173 lines

  1. static char rcsid[] = "$RCSfile: rexx.c,v $ $Revision: 1.2 $";
  2.  
  3. /*************************************************************
  4.  * vt100 terminal emulator - AREXX support
  5.  *                :ts=8
  6.  *
  7.  * $Log:    rexx.c,v $
  8.  * Revision 1.2  89/12/19  20:38:59  acs
  9.  * Added RCS id and change log.
  10.  * 
  11.  *    v2.9 ACS - New with this version.
  12.  *
  13.  *************************************************************/
  14.  
  15. #ifdef AREXX
  16.  
  17. #include "vt100.h"
  18.  
  19. int RexxReplies = 0;            /* # outstanding replies */
  20.  
  21. extern int doing_init;            /* in init.c        */
  22.  
  23. extern char *extension;            /* in vt100.c        */
  24.  
  25. #define AREXXCOMMAND 2
  26. /*   Derived from:
  27.  *
  28.  *  REXX.C
  29.  *
  30.  *    (c) Copyright 1987 by Kim DeVaughn, All Rights Reserved
  31.  *
  32.  *  ARexx interface code, etc.
  33.  *
  34.  */
  35.  
  36. do_rx(cmd, implied)
  37. char *cmd;
  38. int implied;
  39. {
  40.     int    len, ret = 0;
  41.     char *p;
  42.     register struct RexxMsg *RexxMsg;
  43.     struct MsgPort *RexxPort;
  44.  
  45.     if(cmd == NULL || *cmd == '\0')
  46.     return ret;
  47.  
  48.     if(RexxSysBase == NULL) {
  49.         if((RexxSysBase = (struct RxsLib *)OpenLibrary(RXSNAME, 0L)) == NULL)
  50.             return NORXLIB;
  51.         if( (ret = makerexxport()) != 0)
  52.         return ret;
  53.     }
  54.  
  55.     for(len = 0, p = cmd; *p != '\n' && *p != '\0'; p++, len++)
  56.     ;
  57.  
  58.     Forbid();
  59.     if((RexxPort = FindPort(RXSDIR))) {
  60.     if((RexxMsg = CreateRexxMsg(FromRexxPort, extension, HostName)) &&
  61.        (ARG0(RexxMsg) = (STRPTR)CreateArgstring(cmd, (LONG)len)) ) {
  62.         ARG1(RexxMsg) = (STRPTR)AREXXCOMMAND;
  63.         RexxMsg->rm_Action = RXCOMM;
  64.         PutMsg(RexxPort, (struct Message *)RexxMsg);
  65.         RexxReplies++;
  66.     } else
  67.         ret = NORXMSG;
  68.     } else
  69.     ret = NORXPORT;
  70.     Permit();
  71.     return ret;
  72. }
  73.  
  74. void
  75. processrexxmsg(RexxMsg)
  76. register struct RexxMsg *RexxMsg;
  77. {
  78.     extern char *next_wrd();
  79.     register char *arg0 = NULL, *arg1 = NULL;
  80.     char *command = NULL,
  81.      *message = NULL,
  82.      *errmsg = "AREXX macro not found: ",
  83.      *MacDone = "AREXX macro complete: ",
  84.      *NoMem = "processrexxmsg: Can't allocate memory for message.";
  85.     int len = 0;
  86.  
  87.     arg0 = (char *)ARG0(RexxMsg);
  88.     arg1 = (char *)ARG1(RexxMsg);
  89.     if(arg0 && *arg0)
  90.     command = next_wrd(arg0, &len);
  91.  
  92.     if(RexxMsg->rm_Node.mn_Node.ln_Type == NT_REPLYMSG) {
  93.     /*   Got some kind of reply so we'll assume that the AREXX lib is
  94.     ** available.  If it's a "command not found" then tell the user.  If
  95.     ** it's a command FROM VT100 (marked by AREXXCOMMAND in ARG1) and we
  96.     ** have no outstanding replies then it must mean that a previously
  97.     ** issued AREXX command FROM VT100 has completed, tell the user.
  98.     ** Otherwise, we can just toss the reply.
  99.     **/
  100.  
  101.     if(--RexxReplies <= 0)
  102.         RexxReplies = 0;
  103.  
  104.     if(RexxMsg->rm_Result1 == 5 && RexxMsg->rm_Result2 == 1) {
  105.         LONG memlen = strlen(errmsg) + len + 1;
  106.  
  107.         if(message = AllocMem(memlen, MEMF_PUBLIC | MEMF_CLEAR)) {
  108.         strcpy(message, errmsg);
  109.         strncat(message, command, len);
  110.         if(doing_init) {
  111.             puts("Init:");
  112.             puts(message);
  113.         } else
  114.             InfoMsg2Line("Script:", message);
  115.         FreeMem(message, memlen);
  116.         } else {
  117.         if(doing_init) {
  118.             puts("Init:");
  119.             puts(NoMem);
  120.         } else
  121.             InfoMsg2Line("Script:", NoMem);
  122.         }
  123.     }
  124.  
  125.     if(arg1 == (char *)AREXXCOMMAND) {
  126.         if(RexxReplies == 0 && command) {
  127.         LONG memlen = strlen(MacDone) + strlen(command) + 1;
  128.  
  129.         if(message = AllocMem(memlen, MEMF_PUBLIC | MEMF_CLEAR)) {
  130.             strcpy(message, MacDone);
  131.             strcat(message, command);
  132.             if(doing_init) {
  133.             puts("Init:");
  134.             puts(message);
  135.             } else
  136.             InfoMsg1Line(message);
  137.             FreeMem(message, memlen);
  138.         } else {
  139.             if(doing_init) {
  140.             puts("Init:");
  141.             puts(NoMem);
  142.             } else
  143.             InfoMsg2Line("Script:", NoMem);
  144.         }
  145.         }
  146.     }
  147.  
  148.     DeleteArgstring(arg0);
  149.     ARG0(RexxMsg) = NULL;
  150.     ARG1(RexxMsg) = NULL;
  151.     DeleteRexxMsg(RexxMsg);
  152.  
  153.     return;
  154.     } else if(RexxMsg->rm_Action == RXCOMM) {
  155.     int ret;
  156.     LONG memlen = strlen(arg0) + 1;
  157.  
  158.     CmdFromRexx = 1;    /* Tell everyone from whence it came */
  159.     command = AllocMem(memlen, MEMF_PUBLIC | MEMF_CLEAR);
  160.     strcpy(command, arg0);
  161.     next_wrd(command, &len);
  162.  
  163.     RexxMsg->rm_Result1 = exe_cmd(command, len);
  164.     CmdFromRexx = 0;
  165.     FreeMem(command, memlen);
  166.     } else {
  167.     RexxMsg->rm_Result1 = 5;
  168.     RexxMsg->rm_Result2 = 1;
  169.     }
  170.     ReplyMsg((struct Message *)RexxMsg);
  171. }
  172. #endif /* AREXX */
  173.