home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 142.lha / DNet / dnet / window.c < prev   
C/C++ Source or Header  |  1986-11-21  |  6KB  |  285 lines

  1.  
  2. /*
  3.  * WINDOW.C
  4.  *
  5.  *    DNET interactive terminal window.
  6.  */
  7.  
  8. #include "dnet.h"
  9. #include <stdio.h>
  10.  
  11. #include "/server/servers.h"
  12.  
  13. typedef struct IOStdReq     IOCON;
  14. typedef struct TextAttr     TA;
  15. typedef struct IntuiText    ITEXT;
  16. typedef struct MenuItem     ITEM;
  17. typedef struct Menu        MENU;
  18. extern void *OpenWindow();
  19.  
  20. long    IntuitionBase;
  21. long    GfxBase;
  22.  
  23. TA Ta = { (ubyte *)"topaz", 8 };
  24.  
  25. ITEXT IText[] = {
  26.     { 0, 1, JAM2, 0, 0, &Ta, (ubyte *)"SendBreak"   },
  27.     { 0, 1, JAM2, 0, 0, &Ta, (ubyte *)"StartDNET"   },
  28.     { 0, 1, JAM2, 0, 0, &Ta, (ubyte *)"QUIT"        }
  29. };
  30.  
  31. ITEM Item[] = {
  32.     { &Item[1], 0, 0, 120, 10, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[0], NULL, 'b' },
  33.     { &Item[2], 0,10, 120, 10, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[1], NULL, 's' },
  34.     { NULL    , 0,20, 120, 10, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[2], NULL, 'q' }
  35. };
  36.  
  37. MENU Menu[] = {
  38.     { NULL    , 0, 0, 120, 20, MENUENABLED, "DnetControl", &Item[0] }
  39. };
  40.  
  41. do_dnetwindow()
  42. {
  43.     static struct NewWindow Nw = {
  44.     50, 50, 320, 100, -1, -1,
  45.     CLOSEWINDOW|MENUPICK,
  46.     WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|NOCAREREFRESH|ACTIVATE,
  47.     NULL, NULL, (UBYTE *)"DNET V1.10", NULL, NULL,
  48.     32, 32, -1, -1, WBENCHSCREEN
  49.     };
  50.     struct Window *win;
  51.     char RcvBuf[128];
  52.     char Cc;
  53.     char RSync = 0;
  54.     long imask, smask, conmask, mask, dnet_mask;
  55.     IOCON *iocr, *iocw;
  56.     char notdone = 1;
  57.     int returncode = -1;
  58.  
  59.     setparity(0xFF, 0x00, 0);
  60.     IntuitionBase = OpenLibrary("intuition.library", 0);
  61.     GfxBase = OpenLibrary("graphics.library", 0);
  62.     if (IntuitionBase == NULL || GfxBase == NULL)
  63.     goto e1;
  64.     win = OpenWindow(&Nw);
  65.     if (win == NULL)
  66.     goto e1;
  67.     SetMenuStrip(win, Menu);
  68.     OpenConsole(win, &iocr, &iocw);
  69.     if (iocr == NULL)
  70.     goto e3;
  71.     if (Wto_act) {
  72.     AbortIO(&Wto);
  73.     WaitIO(&Wto);
  74.     Wto_act = 0;
  75.     }
  76.     AbortIO(RNet);
  77.     WaitIO(RNet);
  78.     RNet->io_Data = (APTR)RcvBuf;
  79.     RNet->io_Length = 1;
  80.     SendIO(RNet);
  81.     iocr->io_Data = (APTR)&Cc;
  82.     iocr->io_Length = 1;
  83.     SendIO(iocr);
  84.  
  85.     NetWrite("\rAT\r", 4, 0);     /*  cause modem to reset baud rate */
  86.  
  87.     imask   = 1 << win->UserPort->mp_SigBit;
  88.     smask   = 1 << RNet->io_Message.mn_ReplyPort->mp_SigBit;
  89.     conmask = 1 << iocr->io_Message.mn_ReplyPort->mp_SigBit;
  90.     dnet_mask = 1 << DNetPort->mp_SigBit;
  91.  
  92.     while (notdone) {
  93.     mask = Wait(imask|smask|conmask|dnet_mask);
  94.     if (mask & dnet_mask) {
  95.         register IOR *ior;
  96.         while (ior = (IOR *)GetMsg(DNetPort)) {
  97.         switch(ior->io_Command) {
  98.         default:
  99.         case DNCMD_WRITE:
  100.         case DNCMD_EOF:
  101.         case DNCMD_IOCTL:
  102.         case DNCMD_QUIT:
  103.         case DNCMD_OPEN:
  104.             ior->io_Error = 1;
  105.             break;
  106.         case DNCMD_CLOSE:
  107.             Chan[(ulong)ior->io_Unit].state = CHAN_FREE;
  108.             break;
  109.         case DNCMD_SOPEN:
  110.             {
  111.             uword chan = (ulong)ior->io_Unit;
  112.             Chan[chan].state = CHAN_FREE;
  113.             if (!ior->io_Error) {
  114.                 Chan[chan].state = CHAN_CLOSE;
  115.                 Chan[chan].port  = (PORT *)ior->io_Offset;
  116.                 Chan[chan].flags = CHANF_RCLOSE;
  117.                 WritePort(Chan[chan].port, DNCMD_CLOSE, NULL, 0, PKT_REQ, chan);
  118.             }
  119.             if (ior->io_Length)
  120.                 FreeMem(ior->io_Data, ior->io_Length);
  121.             FreeMem(ior, sizeof(IOR));
  122.             ior = NULL;
  123.             }
  124.             break;
  125.         }
  126.         if (ior)
  127.             ReplyMsg(ior);
  128.         }
  129.     }
  130.     if (mask & imask) {
  131.         struct IntuiMessage *im;
  132.         while (im = GetMsg(win->UserPort)) {
  133.         switch(im->Class) {
  134.         case CLOSEWINDOW:
  135.             notdone = 0;
  136.             returncode = -1;
  137.             break;
  138.         case MENUPICK:
  139.             switch((uword)((MENUNUM(im->Code)<<8)|ITEMNUM(im->Code))) {
  140.             case 0x0000:    /*    menu 0 item 0    */
  141.             NetBreak();
  142.             break;
  143.             case 0x0001:    /*    menu 0 item 1    */
  144.             notdone = 0;
  145.             returncode = 1;
  146.             break;
  147.             case 0x0002:    /*    menu 0 item 2    */
  148.             notdone = 0;
  149.             returncode = -1;
  150.             break;
  151.             case 0x0100:    /*    menu 1 item 0    */
  152.             break;
  153.             }
  154.             break;
  155.         }
  156.         ReplyMsg(im);
  157.         }
  158.     }
  159.     if (mask & smask) {
  160.         if (CheckIO(RNet)) {
  161.         int n;
  162.         ubyte dummy;
  163.  
  164.         WaitIO(RNet);
  165.         if (RNet->io_Actual > 0) {
  166.             ubyte c = *(ubyte *)RNet->io_Data;
  167.             if (RSync && c == PKCMD_RESTART) {
  168.             returncode = 1;
  169.             notdone = 0;
  170.             }
  171.             RSync = (c == SYNC);
  172.             for (n = 0; n < RNet->io_Actual; ++n)
  173.             ((char *)RNet->io_Data)[n] &= 0x7F;
  174.             iocw->io_Data   = RNet->io_Data;
  175.             iocw->io_Length = RNet->io_Actual;
  176.             DoIO(iocw);
  177.         }
  178.         if ((n = NetReady(RNet, &dummy)) > sizeof(RcvBuf))
  179.             n = sizeof(RcvBuf);
  180.         if (n <= 0)
  181.             n = 1;
  182.         RNet->io_Data  = (APTR)RcvBuf;
  183.         RNet->io_Length= n;
  184.         SendIO(RNet);
  185.         }
  186.     }
  187.     if (mask & conmask) {
  188.         if (CheckIO(iocr)) {
  189.         WaitIO(iocr);
  190.         addparity(iocr->io_Data, iocr->io_Actual);
  191.         NetWrite(iocr->io_Data, iocr->io_Actual, 0);
  192.         NetWrite(NULL, 0, 0);
  193.         iocr->io_Data = (APTR)&Cc;
  194.         iocr->io_Length = 1;
  195.         SendIO(iocr);
  196.         }
  197.     }
  198.     }
  199.  
  200.     AbortIO(RNet);
  201.     WaitIO(RNet);
  202.     RNet->io_Data   = (APTR)&Raux->sync; /*  Startup the network read    */
  203.     RNet->io_Length = 3;
  204.     SendIO(RNet);
  205.  
  206.     AbortIO(iocr);
  207.     WaitIO(iocr);
  208.     CloseConsole(iocr, iocw);
  209. e3:
  210.     CloseWindow(win);
  211. e1:
  212.     if (IntuitionBase)  CloseLibrary(IntuitionBase);
  213.     if (GfxBase)        CloseLibrary(GfxBase);
  214.     if (returncode > 0)
  215.     puts("DNET RUNNING");
  216.     else
  217.     puts("DNET EXITING");
  218.     return(returncode);
  219. }
  220.  
  221. /*
  222.  *  PARITY ROUTINES
  223.  */
  224.  
  225. static ubyte Party[256/8];
  226.  
  227. setparity(pand, por, podd)
  228. {
  229.     register short i, j, k, l;
  230.     bzero(Party, sizeof(Party));
  231.     for (i = 0; i < 128; ++i) {
  232.     for (j = (~i & 127), k = 0; j; j >>= 1) {   /*  k = count # of 0's */
  233.         if (j & 1)
  234.         ++k;
  235.     }
  236.     k ^= podd;
  237.     l = (((k & 1) ? 0x01 : 0) | por) & pand;
  238.     Party[i >> 3] |= l << (i & 7);
  239.     }
  240. }
  241.  
  242. addparity(buf, bytes)
  243. register ubyte *buf;
  244. {
  245.     register short i;
  246.     for (i = bytes - 1; i >= 0; --i) {
  247.     if (Party[buf[i]>>3] & (1 << (buf[i]&7)))
  248.         buf[i] |= 0x80;
  249.     }
  250. }
  251.  
  252. OpenConsole(win, piocr, piocw)
  253. IOCON **piocr, **piocw;
  254. struct Window *win;
  255. {
  256.     PORT *port;
  257.     static IOCON iocr, iocw;
  258.     int error;
  259.  
  260.     port = CreatePort(NULL, 0);
  261.     iocr.io_Command = CMD_READ;
  262.     iocr.io_Data = (APTR)win;
  263.     iocr.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  264.     iocr.io_Message.mn_ReplyPort = port;
  265.     error = OpenDevice("console.device", 0, &iocr, 0);
  266.     if (!error) {
  267.     iocw = iocr;
  268.     iocw.io_Command = CMD_WRITE;
  269.     *piocr = &iocr;
  270.     *piocw = &iocw;
  271.     } else {
  272.     *piocr = *piocw = NULL;
  273.     }
  274. }
  275.  
  276. CloseConsole(iocr, iocw)
  277. IOCON *iocr;
  278. IOCON *iocw;
  279. {
  280.     CloseDevice(iocr);
  281.     DeletePort(iocr->io_Message.mn_ReplyPort);
  282. }
  283.  
  284.  
  285.