home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d131 / mg1b.lha / Mg1b / Source / rexx.c < prev    next >
C/C++ Source or Header  |  1988-03-14  |  5KB  |  232 lines

  1. /*
  2.  *   This file sends out REXX commands.  Now we start to get into
  3.  *   the fun stuff.  Actually, it's pretty straightforward, from
  4.  *   the TeX information.
  5.  *
  6.  *   For now, we do a no-no; we open and close the emacs port.
  7.  *   Actually, this is probably fine, as we only expect REXX to
  8.  *   send us messages in response to our messages.
  9.  *
  10.  *   The first thing we do is set up a cliplist variable called
  11.  *   mg.
  12.  */
  13. #include "exec/ports.h"
  14. #include "functions.h"
  15. #include "fcntl.h"
  16. #include "rexx/storage.h"
  17. /*
  18.  *   This is a hack.
  19.  */
  20. #undef TRUE
  21. #undef FALSE
  22. #undef VOID
  23. #include "def.h"
  24. extern BUFFER *curbp ;
  25. extern WINDOW *curwp ;
  26. struct MsgPort *mgport, *rport ;
  27. long waitbits ;
  28. struct REXXmsg {
  29.    struct Message msgheader ;
  30.    long *RexxTask, *LibBase ;
  31.    long Command, Result1, Result2 ;
  32.    char *Arg0, *Arg1, *Arg2 ; /* The rest we really don't care about */
  33.    char *Args[13] ;
  34.    void *PassPort, *CommAddr, *FileExt ;
  35.    long Stdin, Stdout, avail ;
  36. } *REXXmsg, setCmsg ;
  37. /*
  38.  *   This routine dispatches any REXX messages coming
  39.  *   in.  We simply call our brand new fancy parsing
  40.  *   routine!  (Getting some of those strings right
  41.  *   might be fun, though . . .)
  42.  */
  43. mgdisp() {
  44.    register char *p ;
  45.  
  46.    while (REXXmsg = (struct REXXmsg *)GetMsg(mgport)) {
  47.       ewprintf(REXXmsg->Arg0) ;
  48.       REXXmsg->Result1 = (excline(REXXmsg->Arg0) == TRUE ? 0 : 1) ;
  49.       REXXmsg->Result2 = 0 ;
  50.       ReplyMsg(REXXmsg) ;
  51.    }
  52. }
  53. openmsgs() {
  54.         Forbid() ;
  55.         if (FindPort("mg")==NULL)
  56.            mgport = CreatePort("mg", 0L) ;
  57.         Permit() ;
  58.         REXXmsg = NULL ;
  59.         rport = CreatePort("mg:reply", 0L) ;
  60. }
  61. closemsgs() {
  62.     if (mgport) {
  63.        FreeSignal((long)(mgport->mp_SigBit)) ;
  64.        RemPort(mgport) ;
  65.        mgdisp() ;
  66.        DeletePort(mgport) ;
  67.     }
  68.     if (rport) {
  69.        FreeSignal((long)(rport->mp_SigBit)) ;
  70.        RemPort(rport) ;
  71.        DeletePort(rport) ;
  72.     }
  73. }
  74. struct MsgPort  *rexxport ;
  75. poststring(n,s)
  76. char *n, *s ;
  77. {
  78.  
  79.     setCmsg.Arg0 = n ;
  80.         setCmsg.Arg1 = s ;
  81.     setCmsg.Arg2 = (char *)(long)strlen(s) ;
  82.         setCmsg.Command = RXADDCON ;
  83.         setCmsg.msgheader.mn_Node.ln_Type = NT_MESSAGE ;
  84.         setCmsg.msgheader.mn_ReplyPort = rport ;
  85.     Forbid() ;
  86.     rexxport = FindPort("REXX") ;
  87.     if (rexxport) {
  88.        PutMsg(rexxport, &setCmsg) ;
  89.     }
  90.     Permit() ;
  91.     if (rexxport) {
  92.        WaitPort(rport) ;
  93.        GetMsg(rport) ;
  94.     }
  95. }
  96. int pendingrexx ;
  97. rexxcommand(f, n, k) {
  98.     register BUFFER    *bp;
  99.     int        s;
  100.     char        cmd[120];
  101.  
  102.     if (pendingrexx)
  103.        return(FALSE) ;
  104.     pendingrexx = 1 ;
  105.     openmsgs() ;
  106.         if (rport == NULL || mgport == NULL)
  107.            goto failure ;
  108. /*
  109.  *   Now we tell everyone the name of the file we are editing,
  110.  *   if any.
  111.  */
  112.     poststring("mg.BufferName", ((curbp && curbp->b_fname[0]) ?
  113.                curbp->b_bname : "")) ;
  114. /*
  115.  *   Now, finally, we send out the message to the REXX port.
  116.  */
  117.     if ((s=ereply("REXX command: ", cmd, 120)) != TRUE)
  118.         goto sfailure ;
  119.         setCmsg.Arg0 = cmd ;
  120.         setCmsg.Arg1 = (char *)(long)strlen(setCmsg.Arg0) ;
  121.         setCmsg.Command = RXCOMM | RXFB_TOKEN | RXFB_NOIO ;
  122.         setCmsg.FileExt = "mg" ;
  123.         setCmsg.CommAddr = "mg" ;
  124.         setCmsg.msgheader.mn_Node.ln_Type = NT_MESSAGE ;
  125.         setCmsg.msgheader.mn_ReplyPort = rport ;
  126.     Forbid() ;
  127.     rexxport = FindPort("REXX") ;
  128.     if (rexxport) {
  129.        PutMsg(rexxport, &setCmsg) ;
  130.     }
  131.     Permit() ;
  132.     if (! rexxport)
  133.        goto failure ;
  134. /*
  135.  *   Now we iterate, waiting until we get the correct message.
  136.  */
  137.     waitbits = (1L << rport->mp_SigBit) | (1L << mgport->mp_SigBit) ;
  138.     while (1) {
  139.        Wait(waitbits) ;
  140.        mgdisp() ;
  141.        if (GetMsg(rport)) {
  142.               s = (setCmsg.Result1 ? FALSE : TRUE) ;
  143.               goto sfailure ;
  144.            }
  145.     }
  146. failure:
  147.     s = FALSE ;
  148. sfailure:
  149. /*
  150.  *   Here we clean up by closing down and deleting the ports.
  151.  */
  152.     closemsgs() ;
  153.     pendingrexx = 0 ;
  154.     return(s) ;
  155. }
  156. static char *rline(p)
  157. struct LINE *p ;
  158. {
  159.    p->l_text[p->l_used] = 0 ;
  160.    return(p->l_text) ;
  161. }
  162. static char *rint(i)
  163. short i ;
  164. {
  165.    static char temp[8] ;
  166.    char *p ;
  167.  
  168.    p = temp + 7 ;
  169.    *p-- = 0 ;
  170.    do {
  171.       *p = ( i % 10 ) + '0' ;
  172.       i = i / 10 ;
  173.       p-- ;
  174.    } while (i != 0) ;
  175.    return(p+1) ;
  176. }
  177. publishline() {
  178.     if (! curbp)
  179.         return(FALSE) ;
  180.     if (! pendingrexx)
  181.         openmsgs() ;
  182.     if (rport == NULL || mgport == NULL) {
  183.         closemsgs() ;
  184.         return(FALSE) ;
  185.     }
  186.     if (curwp->w_dotp) {
  187.         poststring("mg.DotLine", rline(curwp->w_dotp)) ;
  188.         poststring("mg.DotPos", rint(curwp->w_doto)) ;
  189.     } else {
  190.         poststring("mg.DotLine", "") ;
  191.         poststring("mg.DotPos", "") ;
  192.     }
  193. /*    if (curwp->w_markp) {
  194.         poststring("mg.MarkLine", rline(curwp->w_markp)) ;
  195.         poststring("mg.MarkPos", rint(curwp->w_marko)) ;
  196.     } else {
  197.         poststring("mg.MarkLine", "") ;
  198.         poststring("mg.MarkPos", "") ;
  199.     } */
  200.     if (! pendingrexx)
  201.         closemsgs() ;
  202.     return(TRUE) ;
  203. }
  204. extern struct Window *EmW ;
  205. windowfront() {
  206.     if (EmW)
  207.         WindowToFront(EmW) ;
  208.     return(TRUE) ;
  209. }
  210. windowback() {
  211.     if (EmW)
  212.         WindowToBack(EmW) ;
  213.     return(TRUE) ;
  214. }
  215. /*
  216.  *   Bump our priority by 2.  (Editors are important!)
  217.  */
  218. static int oprior ;
  219. bumpprior() {
  220.    struct Task *task ;
  221.  
  222.    task = FindTask(0L) ;
  223.    oprior = task->tc_Node.ln_Pri ;
  224.    SetTaskPri(task, (long)(2+oprior)) ;
  225. }
  226. unbumpprior() {
  227.    struct Task *task ;
  228.  
  229.    task = FindTask(0L) ;
  230.    SetTaskPri(task, (long)oprior) ;
  231. }
  232.