home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 164.lha / ARexx / WB_ARexx_v1.1 / message.c < prev    next >
C/C++ Source or Header  |  1988-04-28  |  3KB  |  167 lines

  1. /** message.c
  2. *
  3. *                     Copyright 1988, W.G.J. Langeveld
  4. *                           All Rights Reserved
  5. *                           Freely Distributable
  6. *
  7. *   Send a message to the REXX host.
  8. *
  9. **/
  10. #include "functions.h"
  11. #include "exec/exec.h"
  12. #include "libraries/dos.h"
  13. #include "libraries/dosextens.h"
  14. #include "intuition/intuition.h"
  15. #include "stdio.h"
  16.  
  17. #include <rexx/rxslib.h>
  18. #include <rexx/rexxio.h>
  19. #include <rexx/errors.h>
  20. #include <rexx/storage.h>
  21.  
  22. static struct RexxMsg *msg;
  23. static struct MsgPort *port = NULL, *REXXrport = NULL;
  24. static struct FileHandle *fh = NULL;
  25. static char *cmdbuff = NULL;
  26.  
  27. extern char *malloc(), *free();
  28. char *ltoa();
  29.  
  30. /**
  31. *
  32. *   Send the command string "command" to the REXX host.
  33. *   if the "flag" is set, this is a STRING command, else it is a rexx script.
  34. *
  35. **/
  36. char *SendREXXMsg(command, flag, win)
  37. char *command, *win;
  38. int flag;
  39. {
  40.    static char buffer[80];
  41.  
  42.    msg = (struct RexxMsg *) AllocMem((long) sizeof(struct RexxMsg),
  43.                                      MEMF_PUBLIC|MEMF_CLEAR);
  44.    if (!msg) {
  45.       rxcleanup();
  46.       return("REXX - out of memory");
  47.    }
  48.    REXXrport = CreatePort(NULL, 0L);
  49.    if (!REXXrport) {
  50.       rxcleanup();
  51.       return("REXX - Couldn't create reply port");
  52.    }
  53.    cmdbuff = malloc(255);
  54.    if (cmdbuff == NULL) {
  55.       rxcleanup();
  56.       return("REXX - out of memory");
  57.    }
  58. /*
  59. *   Open a console window if desired (added by Steve Berry).
  60. */
  61.    if (win) fh = Open(win, MODE_NEWFILE);
  62.    else     fh = Open("nil:", ACCESS_WRITE);
  63.  
  64.    if (fh == NULL) {
  65.       rxcleanup();
  66.       strcpy(buffer, "Couldn't open ");
  67.       if (win) {
  68.          strcat(buffer, win);
  69.          return(buffer);
  70.       }
  71.       else {
  72.          strcat(buffer, "nil:");
  73.          return(buffer);
  74.       }
  75.    }
  76.  
  77.    msg->rm_Node.mn_ReplyPort = REXXrport;
  78.    msg->rm_Action = RXCOMM;
  79.    if (flag) msg->rm_Action |= RXFF_STRING;
  80.    strcpy(cmdbuff, command);
  81.    msg->rm_Args[0] = (STRPTR) cmdbuff;
  82.    msg->rm_FileExt = (STRPTR) "rexx";
  83.    msg->rm_Result1 = 0L;
  84.    msg->rm_Result2 = 0L;
  85.    msg->rm_Stdin = (long) fh;
  86.    msg->rm_Stdout = (long) fh;
  87.  
  88.    Forbid();
  89.    port = FindPort("REXX", 0L);
  90.    if (port) PutMsg(port, msg);
  91.    Permit();
  92.  
  93.    if (!port) {
  94.       rxcleanup();
  95.       return("REXX - REXX is not here");
  96.    }
  97.  
  98.    WaitPort(REXXrport);
  99.    GetMsg(REXXrport);
  100.  
  101.    if (msg->rm_Result1) {
  102.       strcpy(buffer, "REXX - macro exited with error ");
  103.       strcat(buffer, ltoa(msg->rm_Result1) );
  104.       rxcleanup();
  105.       return(buffer);
  106.    }
  107.    else {
  108.       rxcleanup();
  109.       return(NULL);
  110.    }
  111. }
  112.  
  113. /**
  114. *
  115. *   Cleanup all allocated resources
  116. *
  117. **/
  118. static rxcleanup()
  119. {
  120.    if (REXXrport) {
  121.       DeletePort(REXXrport);
  122.       REXXrport = NULL;
  123.    }
  124.    if (fh) {
  125.       Close(fh);
  126.       fh = NULL;
  127.    }
  128.    if (cmdbuff) {
  129.       free(cmdbuff);
  130.       cmdbuff = NULL;
  131.    }
  132.    if (msg) {
  133.       FreeMem(msg, (long)sizeof(struct RexxMsg));
  134.       msg = NULL;
  135.    }
  136.    return;
  137. }
  138.  
  139.  
  140. /**
  141. *
  142. *   Convert a long to an ascii string. sprintf is too much...
  143. *
  144. **/
  145. static char *ltoa(n)
  146. long n;
  147. {
  148.    static char nums[] = "0123456789", str[10], c;
  149.    int i, j;
  150.  
  151.    j = -1;
  152.    while (n) {
  153.       j++;
  154.       i = n % 10;
  155.       str[j] = nums[i];
  156.       n /= 10;
  157.    }
  158.    str[j + 1] = '\0';
  159.    for (i = 0, j = (strlen(str) - 1); i < j; i++, j--) {
  160.       c = str[i];
  161.       str[i] = str[j];
  162.       str[j] = c;
  163.    }
  164.  
  165.    return(str);
  166. }
  167.