home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 206.lha / Flist_v1.2 / Sources / rexxport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-28  |  6.6 KB  |  288 lines

  1. /*
  2.  
  3.     This module is responsible for communicating with Rexx.
  4.     Specifically it opens a message port and tells rexx to execute
  5.     a program. Rexx should signal host of errors or death.
  6.  
  7. */
  8.  
  9. #include <rexx/storage.h>
  10. #include <rexx/rxslib.h>
  11. #include <rexx/errors.h>
  12. #include "flist.h"
  13.  
  14. /* system types */
  15.  
  16. extern void ClearMem(),AddPort(),FreePort(),InitPort(),RemPort(),Forbid();
  17. extern void Permit();
  18. extern LONG OpenLibrary();
  19. extern struct RexxMsg *GetMsg();
  20. extern struct RexxMsg myrexxport;
  21. extern struct Library *RexxSysBase;
  22. extern void control_keys();
  23.  
  24. /* My stuff */
  25.  
  26. struct FileHandle *fh;
  27. struct RexxMsg *rxmsg;
  28. extern char strbuf[256];
  29. extern long collum, row, line, first, top;
  30.  
  31. #define PUBFAST MEMF_FAST+MEMF_PUBLIC+MEMF_CLEAR
  32.  
  33. void OpenRexxPort(string,port)
  34. char *string;
  35. struct MsgPort *port;
  36. {
  37.  
  38.     ClearMem(port,(LONG)sizeof(struct MsgPort));
  39.     InitPort(port,string);
  40.     AddPort(port);
  41. }
  42.  
  43. void DeleteRexxPort(port)
  44. struct MsgPort *port;
  45. {
  46.     if(port != NULL){
  47.         RemPort(port);
  48.         FreePort(port);
  49.     }
  50. }
  51.  
  52. struct RexxMsg *PollRexxPort(port)
  53. struct MsgPort *port;
  54. {
  55.     struct RexxMsg *rxport;
  56.  
  57.     rxport = GetMsg(port);
  58.     return(rxport);
  59.  
  60. }
  61.  
  62. struct RexxMsg *WaitRexxPort(port)
  63. struct MsgPort *port;
  64. {
  65.  
  66.     WaitPort(port);
  67.     return(PollRexxPort(port));
  68. }
  69.  
  70. void CleanupRexx()
  71. {
  72.  
  73. /*    Close(fh); */
  74.     DeleteArgstring(rxmsg->rm_Args[0]);
  75.     DeleteRexxMsg(rxmsg);
  76. }
  77.  
  78. long StartRexxProg(rexport)
  79. struct MsgPort *rexport;
  80. {
  81.     struct MsgPort *port;
  82.     struct Window *strptr;
  83.  
  84.     strbuf[0] = '\0';
  85.     strptr = make_gadget("Enter the REXX macro name ");
  86.  
  87.     if (strptr == NULL) {
  88.         auto_req("Couldn't open Requestor!");
  89.         return FALSE;
  90.     }
  91.  
  92.     wait_for_event(strptr); 
  93.  
  94.     if (strbuf[0] == '\0') {
  95.         return FALSE;
  96.     }
  97.  
  98.     rxmsg = CreateRexxMsg(rexport,"flex","flport");
  99.     rxmsg->rm_Args[0] = strbuf;
  100.     rxmsg->rm_Action = RXCOMM;
  101. /*    rxmsg->rm_Stdout = Output(); */
  102.  
  103.     if(!FillRexxMsg(rxmsg, 1L, 0L)) {
  104.         DeleteRexxMsg(rxmsg);
  105.         return FALSE;
  106.     }
  107.  
  108.     Forbid();
  109.     port = (struct RexxMsg *)FindPort("REXX");
  110.     if(port == NULL){
  111.         Permit();
  112.         auto_req("REXX is not here!");
  113.         CleanupRexx();
  114.         return FALSE;
  115.     }
  116.  
  117.     PutMsg(port,rxmsg);         /* email in it's truest form */
  118.     Permit();
  119.     return TRUE;
  120. }
  121.  
  122. char *rexx_strings[] = {
  123.     "end",
  124.     "iconify",
  125.     "sort s",
  126.     "sort z",
  127.     "sort t",
  128.     "sort o",
  129.     "parent",
  130.     "redraw",
  131.     "reget",
  132.     "request",
  133.     "makedir",
  134.     "changedir",
  135.     "sort y"
  136. };
  137.  
  138. dorexx()
  139. {
  140.     struct RexxMsg *resultmsg;
  141.     char buf[256];
  142.     int equal, i;
  143.                 
  144.     if(RexxSysBase == NULL){
  145.         auto_req(" Rexx library not Available");
  146.         return;
  147.     }
  148.  
  149.     if(!StartRexxProg(&myrexxport)){    /* start up fl.flex as a rexx task */
  150.         if (strbuf[0] != '\0')
  151.             auto_req(" PANIC - couldn't start up rexx program!");
  152.         return;
  153.     }
  154.  
  155.     /* this is a communication test ... */
  156.  
  157.     for(;;) {
  158.  
  159.         resultmsg = WaitRexxPort(&myrexxport);
  160.  
  161. #ifdef DEBUG
  162.         sprintf(buf,"Return of -%d- , -%s-",resultmsg->rm_Result1,resultmsg->rm_Args[0]);
  163.         auto_req(buf);
  164. #endif
  165.  
  166.         if (resultmsg->rm_Result1 > 0) {
  167.  
  168.             if (Strcmp(strbuf, resultmsg->rm_Args[0]) == 0) {
  169.                 sprintf(buf,"Error %d in -%s.flex-.",
  170.                     resultmsg->rm_Result1,strbuf);
  171.                 auto_req(buf);
  172.                 resultmsg->rm_Result1 = 5; 
  173.                 resultmsg->rm_Result2 = 0; 
  174.                 CleanupRexx();
  175.                 return;
  176.             }
  177.         }
  178.  
  179.         resultmsg->rm_Result1 = 0;  /* no error  */
  180.         resultmsg->rm_Result2 = 0;  /* no secondary */
  181.  
  182.         for(i=0;i<13;i++){
  183.             equal = Strncmp(resultmsg->rm_Args[0],rexx_strings[i],
  184.                         strlen(rexx_strings[i]));
  185.             if (equal == 0)
  186.                 break;
  187.         }
  188.  
  189. #ifdef DEBUG
  190.         sprintf(buf,"I = %d, string is %s",i,rexx_strings[i]);
  191.         auto_req(buf);
  192. #endif
  193.  
  194.         blankcur();
  195.         switch (i) {
  196.         
  197.             case 0:
  198.                 ReplyMsg(resultmsg);        /* send back 'end' message */
  199.                 WaitRexxPort(&myrexxport);  /* wait for original Msg to ret */
  200.                 CleanupRexx();
  201.                 return;
  202.  
  203.             case 1:         /* Iconify */
  204.                 tticon();
  205.                 break;
  206.  
  207.             case 2:         /* sort by name */
  208.                 sort(0);
  209.                 refresh();
  210.                 break;
  211.  
  212.             case 3:         /* sort by size */
  213.                 sort(1);
  214.                 refresh();
  215.                 break;
  216.  
  217.             case 4:         /* sort by time */
  218.                 sort(2);
  219.                 refresh();
  220.                 break;
  221.  
  222.             case 5:         /* sort by pattern */
  223.                 sort(3);
  224.                 refresh();
  225.                 break;
  226.  
  227.             case 6:         /* get parent dir */
  228.                 parent();
  229.                 refresh();
  230.                 break;
  231.  
  232.             case 7:         /* redraw screen */
  233.                 control_keys(0x0c);
  234.                 break;
  235.  
  236.             case 8:         /* reget directory */
  237.                 getdir();
  238.                 refresh();
  239.                 break;
  240.  
  241.             case 9:
  242.                 control_keys(0x01); /* ARP requestor */
  243.                 break;
  244.  
  245.             case 10:
  246.                 control_keys(0x0e); /* Makedir */
  247.                 break;
  248.  
  249.             case 11:                /* changedir 'string' */
  250.                 if (strlen(resultmsg->rm_Args[0]) > 10) {
  251. #ifdef DEBUG
  252.                     auto_req(&resultmsg->rm_Args[0][10]);
  253. #endif
  254.                     strcpy(buf,&resultmsg->rm_Args[0][10]);
  255.                     changedir(buf);
  256.                     refresh();
  257.                 } else
  258.                     auto_req("Bad arguments to CHANGEDIR.");
  259.                 break;
  260.  
  261.             case 12:         /* sort by day */
  262.                 sort(4);
  263.                 refresh();
  264.                 break;
  265.  
  266.             default:
  267.                 resultmsg->rm_Result1 = 5;  /* shows an error code */
  268.                 resultmsg->rm_Result2 = 0;  /* no secondary        */
  269.  
  270.                 if (Strcmp(strbuf, resultmsg->rm_Args[0]) == 0) {
  271.                     sprintf(buf,"End keyword not found in -%s.flex-.",
  272.                         strbuf);
  273.                     auto_req(buf);
  274.                     ReplyMsg(resultmsg);
  275.                     WaitRexxPort(&myrexxport);
  276.                     CleanupRexx();
  277.                     return;
  278.                 }
  279.                 sprintf(buf,"Bad command -%s- from macro %s.flex",
  280.                     resultmsg->rm_Args[0],strbuf);
  281.                 auto_req(buf);
  282.                 break;
  283.         }
  284.         ReplyMsg(resultmsg);
  285.         drawcur();
  286.     }   /* end of forever */
  287. }
  288.