home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / arexx / larp11a.lha / LARP / LMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-05  |  2.9 KB  |  164 lines

  1. /* Compile me to get full executable. */
  2.  
  3. #include "defs.h"
  4.  
  5. #define MAXBUF 60
  6.  
  7. char *RexxHostName = "LARP";
  8. char *MyCommand = "PUTS";
  9. UBYTE count = 0;        /* num messages in buffer */
  10. char *Buf;            /* what a lame way - must fix this */
  11.  
  12. void main(void)
  13. {
  14.     int done=0;
  15.     ULONG class;
  16.     UWORD code;
  17.     LONG mask;
  18.     struct IntuiMessage *imsg;
  19.     if(RexxSysBase == NULL)
  20.     {
  21.         puts("Unable to open rexxsyslib.library !");
  22.         exit(20);
  23.     }
  24.     if (OpenLibs()==0)
  25.     {
  26.         Buf = AllocMem(MAXBUF*80l, MEMF_PUBLIC|MEMF_CLEAR);
  27.         if(Buf == NULL)
  28.         {
  29.             puts("Cannot allocate memory.");
  30.             CloseLibs();
  31.             exit(20);
  32.         }
  33.         if (OpenWindowWin0()==0)
  34.         {
  35.             while(done==0)
  36.             {
  37.                 mask = Wait((1l<<RexxSigBit)|(1L << Win0->UserPort->mp_SigBit));
  38.                 if(mask & (1l<<RexxSigBit))
  39.                 {
  40.                     ProcessRexxCommands(NULL);
  41.                     DrawMessages(Win0, Buf, count);
  42.                 }
  43.                 else
  44.                 {
  45.                     imsg=GT_GetIMsg(Win0->UserPort);
  46.                     while (imsg != NULL )
  47.                     {
  48.                         class=imsg->Class;
  49.                         code=imsg->Code;
  50.                         GT_ReplyIMsg(imsg);
  51.                         done = ProcessWindowWin0(class, code, imsg);
  52.                         imsg=GT_GetIMsg(Win0->UserPort);
  53.                     }
  54.                 }
  55.             }
  56.             CloseWindowWin0();
  57.         }
  58.         else
  59.         {
  60.             puts("Cannot open window.");
  61.         }
  62.         FreeMem(Buf, MAXBUF*80l);
  63.         CloseLibs();
  64.     }
  65.     else
  66.     {
  67.         puts("Cannot open libraries.\n");
  68.         exit(20);
  69.     }
  70. }
  71.  
  72. long DoRexxCommand(msg, port, arg0, pres)
  73. void *msg;
  74. struct MsgPort *port;
  75. char *arg0;
  76. char **pres;
  77. {
  78.     int rc;
  79.     if(strncmp(arg0, MyCommand, 4) == 0)
  80.     {
  81.         *pres = "success!";
  82.         rc = 0;
  83.         UpdateMessages(arg0 + 5);
  84.     }
  85.     else
  86.     {
  87.         *pres = "failure!";
  88.         rc = 5;
  89.     }
  90.     return(rc);
  91. }
  92.  
  93. void DrawMessages(struct Window *win, char *buff, UBYTE count)
  94. {
  95.     short i, howmany, start;
  96.     UWORD offx = win->BorderLeft;
  97.     UWORD offy = win->BorderTop;
  98.     int rowchar = (win->Width-32-offx)/8;
  99.     int linechar = (win->Height-16-offy)/8;
  100.     if(rowchar>80)
  101.         rowchar = 80;
  102.     SetAPen(win->RPort, 0);
  103.     RectFill(win->RPort, offx+8, offy + 5, win->Width-offx-21, win->Height-7);
  104.     SetAPen(win->RPort, 1);
  105.     start = count - linechar;
  106.     if(start<0)
  107.         start = 0;
  108.     for(i = 0; i<count; i++)
  109.     {
  110.         howmany = strlen(buff + ((i+start) * 80));
  111.         if(howmany>rowchar)
  112.             howmany = rowchar;
  113.         Move(win->RPort, offx + 8, offy+ 11 + (i*8));
  114.         Text(win->RPort, buff + ((i + start) * 80), howmany);
  115.         if(i == linechar)
  116.             break;
  117.     }
  118. }
  119.  
  120. void UpdateMessages(char *line)
  121. {
  122.     int howmany = strlen(line);
  123.     if(howmany>78)
  124.         howmany = 78;
  125.     count++;
  126.     if(count>MAXBUF)
  127.     {
  128.         count = MAXBUF;
  129.         memmove(Buf, Buf + 80, (MAXBUF-1)*80);
  130.         memcpy(Buf + ((MAXBUF-1)*80), line, howmany+1);
  131.     }
  132.     else
  133.     {
  134.         memcpy(Buf+((count-1)*80), line, howmany+1);
  135.     }
  136. }
  137.  
  138. int ProcessWindowWin0( LONG Class, UWORD Code, APTR IAddress )
  139. {
  140.     int returncode = 0;
  141.     switch ( Class )
  142.     {
  143.     case IDCMP_CLOSEWINDOW:
  144.         returncode = 1;
  145.         break;
  146.     case IDCMP_NEWSIZE:
  147.         RendWindowWin0(Win0);
  148.         DrawMessages(Win0, Buf, count);
  149.         break;
  150.     case IDCMP_REFRESHWINDOW :
  151.         GT_BeginRefresh( Win0);
  152.         RendWindowWin0( Win0);
  153.         DrawMessages(Win0, Buf, count);
  154.         GT_EndRefresh( Win0, TRUE);
  155.         break;
  156.     }
  157.     return(returncode);
  158. }
  159.  
  160. void wbmain(void)
  161. {
  162.     main();
  163. }
  164.