home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 144.lha / WB_Rexx / message.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-21  |  3.0 KB  |  154 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 *nilfh = 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)
  37. char *command;
  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.    nilfh = Open("nil:", ACCESS_WRITE);
  59.    if (nilfh == NULL) {
  60.       rxcleanup();
  61.       return("Couldn't open nil:");
  62.    }
  63.  
  64.    msg->rm_Node.mn_ReplyPort = REXXrport;
  65.    msg->rm_Action = RXCOMM;
  66.    if (flag) msg->rm_Action |= RXFF_STRING;
  67.    strcpy(cmdbuff, command);
  68.    msg->rm_Args[0] = (STRPTR) cmdbuff;
  69.    msg->rm_FileExt = (STRPTR) "rexx";
  70.    msg->rm_Result1 = 0L;
  71.    msg->rm_Result2 = 0L;
  72.    msg->rm_Stdin = (long) nilfh;
  73.    msg->rm_Stdout = (long) nilfh;
  74.  
  75.    Forbid();
  76.    port = FindPort("REXX", 0L);
  77.    if (port) PutMsg(port, msg);
  78.    Permit();
  79.  
  80.    if (!port) {
  81.       rxcleanup();
  82.       return("REXX - REXX is not here");
  83.    }
  84.  
  85.    WaitPort(REXXrport);
  86.    GetMsg(REXXrport);
  87.  
  88.    if (msg->rm_Result1) {
  89.       strcpy(buffer, "REXX - macro exited with error ");
  90.       strcat(buffer, ltoa(msg->rm_Result1) );
  91.       rxcleanup();
  92.       return(buffer);
  93.    }
  94.    else {
  95.       rxcleanup();
  96.       return(NULL);
  97.    }
  98. }
  99.  
  100. /**
  101. *
  102. *   Cleanup all allocated resources
  103. *
  104. **/
  105. static rxcleanup()
  106. {
  107.    if (REXXrport) {
  108.       DeletePort(REXXrport);
  109.       REXXrport = NULL;
  110.    }
  111.    if (nilfh) {
  112.       Close(nilfh);
  113.       nilfh = NULL;
  114.    }
  115.    if (cmdbuff) {
  116.       free(cmdbuff);
  117.       cmdbuff = NULL;
  118.    }
  119.    if (msg) {
  120.       FreeMem(msg, (long)sizeof(struct RexxMsg));
  121.       msg = NULL;
  122.    }
  123.    return;
  124. }
  125.  
  126.  
  127. /**
  128. *
  129. *   Convert a long to an ascii string. sprintf is too much...
  130. *
  131. **/
  132. static char *ltoa(n)
  133. long n;
  134. {
  135.    static char nums[] = "0123456789", str[10], c;
  136.    int i, j;
  137.  
  138.    j = -1;
  139.    while (n) {
  140.       j++;
  141.       i = n % 10;
  142.       str[j] = nums[i];
  143.       n /= 10;
  144.    }
  145.    str[j + 1] = '\0';
  146.    for (i = 0, j = (strlen(str) - 1); i < j; i++, j--) {
  147.       c = str[i];
  148.       str[i] = str[j];
  149.       str[j] = c;
  150.    }
  151.  
  152.    return(str);
  153. }
  154.