home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff294.lzh / DNet / amiga / dnet / window.c < prev   
C/C++ Source or Header  |  1989-12-11  |  11KB  |  532 lines

  1.  
  2. /*
  3.  * WINDOW.C
  4.  *
  5.  *    DNET interactive terminal window.
  6.  */
  7.  
  8. #include "dnet.h"
  9.  
  10. #include "/server/servers.h"
  11. #include "/dnet/channel.h"
  12. #include "/lib/dnetlib.h"
  13.  
  14. void setparity();
  15. void addparity();
  16. void OpenConsole();
  17. void CloseConsole();
  18. void InitDeemuNW();
  19. void LoadConnectList();
  20. void UnLoadConnectList();
  21.  
  22. IOSER *NetReadReady();
  23. IOSER *NetAbortRead();
  24.  
  25. typedef struct TextAttr TA;
  26. typedef struct IntuiText ITEXT;
  27. typedef struct IntuiMessage IMESS;
  28. typedef struct Screen SCR;
  29. typedef struct Library LIB;
  30. typedef struct Menu  MENU;
  31. typedef struct MenuItem ITEM;
  32.  
  33. struct IntuitionBase *IntuitionBase;
  34. struct GfxBase *GfxBase;
  35.  
  36. TA Ta = { (ubyte *)"topaz", 8 };
  37.  
  38. ITEXT IText[] = {
  39.     { 0, 1, JAM2, 0, 0, &Ta, (ubyte *)"SendBreak"   },
  40.     { 0, 1, JAM2, 0, 0, &Ta, (ubyte *)"StartDNET"   },
  41.     { 0, 1, JAM2, 0, 0, &Ta, (ubyte *)"QUIT"        }
  42. };
  43.  
  44. ITEM Item[] = {
  45.     { &Item[1], 0, 0, 120, 10, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[0], NULL, 'b' },
  46.     { &Item[2], 0,10, 120, 10, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[1], NULL, 's' },
  47.     { NULL    , 0,20, 120, 10, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[2], NULL, 'q' }
  48. };
  49.  
  50. MENU Menu[] = {
  51.     { NULL    , 0, 0, 120, 20, MENUENABLED, "DnetControl", &Item[0] }
  52. };
  53.  
  54. ubyte Title[80];
  55.  
  56. void
  57. do_dnetwindow(oldbaud)
  58. {
  59.     static struct NewWindow Nw = {
  60.     50, 50, 320, 100, -1, -1,
  61.     CLOSEWINDOW|MENUPICK,
  62.     WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|NOCAREREFRESH|ACTIVATE,
  63.     NULL, NULL, Title, NULL, NULL,
  64.     32, 32, -1, -1, WBENCHSCREEN
  65.     };
  66.     struct Window *win;
  67.     char RcvBuf[128];
  68.     char Cc;
  69.     char RSync = 0;
  70.     long imask, smask, conmask, mask, dnet_mask;
  71. #ifdef NOTDEF
  72.     long ipc_mask;
  73. #endif
  74.     IOCON *iocr, *iocw;
  75.     char notdone = 1;
  76.  
  77.     sprintf(Title, "DNET V%s%s", VERSION, DNET_VERSION);
  78.     setparity(0xFF, 0x00, 1);
  79.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
  80.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0);
  81.     if (IntuitionBase == NULL || GfxBase == NULL)
  82.     goto e1;
  83.     InitDeemuNW(Deemu+DMNWOFF, &Nw);
  84.     win = OpenWindow(&Nw);
  85.     if (win == NULL)
  86.     goto e1;
  87.     SetMenuStrip(win, Menu);
  88.     OpenConsole(win, &iocr, &iocw);
  89.     if (iocr == NULL)
  90.     goto e3;
  91.     if (Cto_act) {
  92.     AbortIO((IOR *)&Cto);
  93.     WaitIO((IOR *)&Cto);
  94.     Cto_act = 0;
  95.     }
  96.     if (Wto_act) {
  97.     AbortIO((IOR *)&Wto);
  98.     WaitIO((IOR *)&Wto);
  99.     Wto_act = 0;
  100.     }
  101.     SetBaudRate(oldbaud);
  102.  
  103.     if (OpenCfgFile()) {    /*  Automatic enviroment reset.     */
  104.     char *env;
  105.     char *get = (AutoAnswer) ? "ENVA" : ((DialOut) ? "ENVO" : "ENVM");
  106.  
  107.     while (env = GetCfgLine(get)) {
  108.         char *ptr;
  109.         for (ptr = env; *ptr && *ptr != ' ' && *ptr != 9; ++ptr);
  110.         if (*ptr) {
  111.         *ptr = 0;
  112.         for (++ptr; *ptr == ' ' || *ptr == 9; ++ptr);
  113.         SetDEnv(env, ptr);
  114.         }
  115.     }
  116.     }
  117.  
  118.     if (Cd == 0 && AutoAnswer) {
  119.     if (OpenCfgFile()) {
  120.         char *str;
  121.         while (str = GetCfgLine("RESM")) {  /* RESM MODEM-CMD   */
  122.         short len = strlen(str);
  123.         str[len++] = '\r';
  124.         NetWrite(str, len, 0);
  125.         Cto.tr_time.tv_secs = 1;
  126.         Cto.tr_time.tv_micro= 0;
  127.         DoIO((IOR *)&Cto);
  128.         }
  129.         CloseCfgFile();
  130.     } else {
  131.         NetWrite("AT\r", 3, 0);   /*  cause modem to reset baud rate */
  132.         Cto.tr_time.tv_secs = 1;
  133.         Cto.tr_time.tv_micro= 0;
  134.         DoIO((IOR *)&Cto);
  135.         NetWrite("AT\r", 3, 0);     /*  cause modem to reset baud rate */
  136.         Cto.tr_time.tv_secs = 1;
  137.         Cto.tr_time.tv_micro= 0;
  138.         DoIO((IOR *)&Cto);
  139.         NetWrite("ATS0=1\r", 7, 0); /*  auto answer on */
  140.     }
  141.     }
  142.  
  143.     iocr->io_Data = (APTR)&Cc;
  144.     iocr->io_Length = 1;
  145.     SendIO((IOR *)iocr);
  146.  
  147.     imask   = 1 << win->UserPort->mp_SigBit;
  148.     smask   = 1 << IOSink->mp_SigBit;
  149.     conmask = 1 << iocr->io_Message.mn_ReplyPort->mp_SigBit;
  150.     dnet_mask = 1 << DNetPort->mp_SigBit;
  151. #ifdef NOTDEF
  152.     ipc_mask= 1 << IPCPort->mp_SigBit;
  153. #endif
  154.  
  155.     LoadConnectList();
  156.  
  157.     Signal(FindTask(NULL), imask|smask|conmask|dnet_mask/*|ipc_mask*/);
  158.  
  159.     StartWriteTimeout(1);
  160.  
  161.     while (!Quit && OnLine == 0) {
  162.     mask = Wait(imask|smask|conmask|dnet_mask/*|ipc_mask*/);
  163. #ifdef NOTDEF
  164.     if (mask & ipc_mask)
  165.         handle_ipc();
  166. #endif
  167.     if (mask & dnet_mask) {
  168.         IOSTD *ior;
  169.         while (ior = (IOSTD *)GetMsg(DNetPort)) {
  170.         switch(ior->io_Command) {
  171.         default:
  172.         case DNCMD_WRITE:
  173.         case DNCMD_EOF:
  174.         case DNCMD_IOCTL:
  175.         case DNCMD_QUIT:
  176.         case DNCMD_OPEN:
  177.             ior->io_Error = 1;
  178.             break;
  179.         case DNCMD_CLOSE:
  180.             Chan[(ulong)ior->io_Unit].state = CHAN_FREE;
  181.             break;
  182.         case DNCMD_SOPEN:
  183.             {
  184.             uword chan = (ulong)ior->io_Unit;
  185.             Chan[chan].state = CHAN_FREE;
  186.             if (!ior->io_Error) {
  187.                 Chan[chan].state = CHAN_CLOSE;
  188.                 Chan[chan].port  = (PORT *)ior->io_Offset;
  189.                 Chan[chan].flags = CHANF_RCLOSE;
  190.                 WritePort(Chan[chan].port, DNCMD_CLOSE, NULL, 0, PKT_REQ, chan);
  191.             }
  192.             if (ior->io_Length)
  193.                 FreeMem(ior->io_Data, ior->io_Length);
  194.             FreeMem(ior, sizeof(IOSTD));
  195.             ior = NULL;
  196.             }
  197.             break;
  198.         case DNCMD_EXEC:
  199.             Execute((char *)ior->io_Offset, NULL, NULL);
  200.             break;
  201.         }
  202.         if (ior)
  203.             ReplyMsg((MSG *)ior);
  204.         }
  205.     }
  206.     if (mask & imask) {
  207.         struct IntuiMessage *im;
  208.         while (im = (IMESS *)GetMsg(win->UserPort)) {
  209.         switch(im->Class) {
  210.         case CLOSEWINDOW:
  211.             notdone = 0;
  212.             Quit = 1;
  213.             break;
  214.         case MENUPICK:
  215.             switch((uword)((MENUNUM(im->Code)<<8)|ITEMNUM(im->Code))) {
  216.             case 0x0000:    /*    menu 0 item 0    */
  217.             NetBreak();
  218.             break;
  219.             case 0x0001:    /*    menu 0 item 1    */
  220.             notdone = 0;
  221.             OnLine = 1;
  222.             break;
  223.             case 0x0002:    /*    menu 0 item 2    */
  224.             notdone = 0;
  225.             Quit = 1;
  226.             break;
  227.             case 0x0100:    /*    menu 1 item 0    */
  228.             break;
  229.             }
  230.             break;
  231.         }
  232.         ReplyMsg((MSG *)im);
  233.         }
  234.     }
  235.     if (mask & smask) {
  236.         IOSER *ios;
  237.         if (ios = NetReadReady()) {
  238.         int n;
  239.         int actual;
  240.         ubyte *ptr;
  241.  
  242.         WaitIO((IOR *)ios);
  243.         actual = NetReadReturned(&ptr);
  244.         if (actual > 0) {
  245.             if (RSync && *ptr == PKCMD_RESTART) {
  246.             OnLine = 1;
  247.             notdone = 0;
  248.             }
  249.             RSync = (*ptr == SYNC);
  250.             for (n = 0; n < actual; ++n)
  251.             ptr[n] &= 0x7F;
  252.             oldbaud = CheckConnect(oldbaud, ptr, n);
  253.             iocw->io_Data   = (APTR)ptr;
  254.             iocw->io_Length = n;
  255.             DoIO((IOR *)iocw);
  256.         }
  257.         if ((n = NetReady()) >= sizeof(RcvBuf))
  258.             n = sizeof(RcvBuf) - 1;
  259.         if (n <= 0)
  260.             n = 1;
  261.         NetStartRead(n);
  262.         }
  263.         if (CheckIO(&Wto)) {
  264.         StartWriteTimeout(1);
  265.         NetReady();
  266.         }
  267.     }
  268.     if (mask & conmask) {
  269.         if (CheckIO((IOR *)iocr)) {
  270.         WaitIO((IOR *)iocr);
  271.         if (!Master8)
  272.             addparity((ubyte *)iocr->io_Data, iocr->io_Actual);
  273.         NetWrite(iocr->io_Data, iocr->io_Actual, 0);
  274.         NetWaitWrite();
  275.         iocr->io_Data = (APTR)&Cc;
  276.         iocr->io_Length = 1;
  277.         SendIO((IOR *)iocr);
  278.         }
  279.     }
  280.     NetReady();
  281.     if (Getty && !Cd) {
  282.         Quit = 1;
  283.         notdone = 0;
  284.         break;
  285.     }
  286.     if (AutoAnswer && Cd)
  287.         break;
  288.     }
  289.  
  290.     StartWriteTimeout(0);
  291.  
  292.     UnLoadConnectList();
  293.  
  294.     {
  295.     IOSER *ios = NetAbortRead();
  296.  
  297.     if (ios->IOSer.io_Actual > 0) {
  298.         ubyte *ptr = (ubyte *)ios->IOSer.io_Data;
  299.         short n;
  300.         for (n = 0; n < ios->IOSer.io_Actual; ++n)
  301.         ptr[n] &= 0x7F;
  302.         oldbaud = CheckConnect(oldbaud, ptr, n);
  303.     }
  304.     }
  305.  
  306.     if (DDebug)
  307.     printf("SETTING BAUD RATE TO %d\n", oldbaud);
  308.  
  309.     if (AutoAnswer && Cd) {
  310.     char *msg = "\r\nDNET AutoAnswer, Protocol Start\r\n";
  311.     OnLine = 1;
  312.     NetWrite(msg, strlen(msg), 0);
  313.     Cto.tr_time.tv_secs = 3;
  314.     Cto.tr_time.tv_micro= 0;
  315.     DoIO((IOR *)&Cto);
  316.     }
  317.  
  318.     NetStartRead(3);
  319.  
  320.     AbortIO((IOR *)iocr);
  321.     WaitIO((IOR *)iocr);
  322.     CloseConsole(iocr, iocw);
  323. e3:
  324.     CloseWindow(win);
  325. e1:
  326.     if (IntuitionBase)  CloseLibrary((LIB *)IntuitionBase);
  327.     if (GfxBase)        CloseLibrary((LIB *)GfxBase);
  328.     if (Quit)
  329.     puts("DNET EXITING");
  330.     else
  331.     puts("DNET RUNNING");
  332.     return;
  333. }
  334.  
  335. /*
  336.  *  PARITY ROUTINES
  337.  */
  338.  
  339. static ubyte Party[256/8];
  340.  
  341. void
  342. setparity(pand, por, peven)
  343. int pand, por, peven;
  344. {
  345.     short i, j, k, l;
  346.  
  347.     BZero(Party, sizeof(Party));
  348.     for (i = 0; i < 128; ++i) {
  349.     for (j = (~i & 127), k = 0; j; j >>= 1) {   /*  k = count # of 0's */
  350.         if (j & 1)
  351.         ++k;
  352.     }
  353.     k ^= peven;
  354.     l = (((k & 1) ? 0x01 : 0) | por) & pand;
  355.     Party[i >> 3] |= l << (i & 7);
  356.     }
  357. }
  358.  
  359. void
  360. addparity(buf, bytes)
  361. ubyte *buf;
  362. int bytes;
  363. {
  364.     short i;
  365.  
  366.     for (i = bytes - 1; i >= 0; --i) {
  367.     if (Party[buf[i]>>3] & (1 << (buf[i]&7)))
  368.         buf[i] |= 0x80;
  369.     }
  370. }
  371.  
  372. void
  373. OpenConsole(win, piocr, piocw)
  374. IOCON **piocr, **piocw;
  375. struct Window *win;
  376. {
  377.     PORT *port;
  378.     static IOCON iocr, iocw;
  379.     int error;
  380.  
  381.     port = CreatePort(NULL, 0);
  382.     iocr.io_Command = CMD_READ;
  383.     iocr.io_Data = (APTR)win;
  384.     iocr.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  385.     iocr.io_Message.mn_ReplyPort = port;
  386.     error = OpenDevice("console.device", 0, (IOR *)&iocr, 0);
  387.     if (!error) {
  388.     iocw = iocr;
  389.     iocw.io_Command = CMD_WRITE;
  390.     *piocr = &iocr;
  391.     *piocw = &iocw;
  392.     } else {
  393.     *piocr = *piocw = NULL;
  394.     }
  395. }
  396.  
  397. void
  398. CloseConsole(iocr, iocw)
  399. IOCON *iocr;
  400. IOCON *iocw;
  401. {
  402.     CloseDevice((IOR *)iocr);
  403.     DeletePort(iocr->io_Message.mn_ReplyPort);
  404. }
  405.  
  406. void
  407. InitDeemuNW(ary, nw)
  408. short *ary;
  409. NW *nw;
  410. {
  411.     short alen = ary[3];
  412.     SCR Scr;
  413.  
  414.     if (GetScreenData((char *)&Scr, sizeof(Scr), nw->Type, nw->Screen) == 0) {
  415.     Scr.Width = 320;
  416.     Scr.Height= 200;
  417.     }
  418.     if (alen >= 8) {
  419.     if ((nw->Width   = ary[6]) < 0)
  420.         nw->Width += Scr.Width;
  421.     if ((nw->Height  = ary[7]) < 0)
  422.         nw->Height+= Scr.Height;
  423.     }
  424.     if (alen >= 4) {
  425.     if ((nw->LeftEdge= ary[4]) < 0)
  426.         nw->LeftEdge += Scr.Width - nw->Width;
  427.     if ((nw->TopEdge = ary[5]) < 0)
  428.         nw->TopEdge += Scr.Height - nw->Height;
  429.     }
  430.     if (nw->LeftEdge < 0 || nw->TopEdge < 0 || nw->Width < 0 || nw->Height < 0 ||
  431.     nw->LeftEdge + nw->Width > Scr.Width || nw->TopEdge + nw->Height > Scr.Height) {
  432.  
  433.     nw->LeftEdge = nw->TopEdge = 0;
  434.     nw->Width = 320;
  435.     nw->Height= 100;
  436.     }
  437.     if (alen >= 9)
  438.     nw->DetailPen = ary[8] >> 8;
  439.     if (alen >= 10)
  440.     nw->BlockPen  = ary[8];
  441. }
  442.  
  443. /*
  444.  *
  445.  */
  446.  
  447. static MLIST CList;
  448.  
  449. void
  450. LoadConnectList()
  451. {
  452.     char *ptr;
  453.  
  454.     NewList((LIST *)&CList);
  455.  
  456.     OpenCfgFile();
  457.     while (ptr = GetCfgLine("AUTA")) {
  458.     long baudrate = atoi(ptr);
  459.     while (*ptr && *ptr != '\"')
  460.         ++ptr;
  461.     if (*ptr == '\"') {
  462.         char *en;
  463.         NODE *node;
  464.  
  465.         for (en = ++ptr; *en && *en != '\"'; ++en);
  466.         *en = 0;
  467.  
  468.         if (node = (NODE *)malloc(sizeof(NODE)+strlen(ptr)+1)) {
  469.         AddTail((LIST *)&CList, node);
  470.         node->ln_Name = (char *)baudrate;
  471.         strcpy((char *)(node + 1), ptr);
  472.         }
  473.     }
  474.     }
  475.     CloseCfgFile();
  476. }
  477.  
  478. void
  479. UnLoadConnectList()
  480. {
  481.     NODE *node;
  482.     while (node = RemHead((LIST *)&CList))
  483.     free(node);
  484. }
  485.  
  486. int
  487. CheckConnect(oldbaud, ptr, n)
  488. int oldbaud;
  489. char *ptr;
  490. int n;
  491. {
  492.     static char tmpbuf[64];
  493.     static short tlen;
  494.     short i;
  495.     NODE *node;
  496.     long newbaud = oldbaud;
  497.  
  498.     for (i = 0; i < n; ++i) {
  499.     if (ptr[i] == 13 || ptr[i] == 10) {
  500.         tmpbuf[tlen++] = 0;
  501.         if (tlen < 2) {
  502.         tlen = 0;
  503.         continue;
  504.         }
  505.         tlen = 0;
  506.         for (node = (NODE *)GetHead(&CList); node; node = (NODE *)GetSucc(node)) {
  507.         if (strcmp(tmpbuf, (char *)(node + 1)) == 0) {
  508.             newbaud = (long)node->ln_Name;
  509.             if (DDebug)
  510.             printf("Successful compare, baud -> %d\n", newbaud);
  511.             break;
  512.         }
  513.         }
  514.         if (GetHead(&CList) == NULL) {
  515.         if (strncmp(tmpbuf, "CONNECT", 7) == 0) {
  516.             newbaud = atoi(tmpbuf + 7);
  517.             if (newbaud == 0)
  518.             newbaud = 300;
  519.         }
  520.         }
  521.         continue;
  522.     }
  523.     tmpbuf[tlen++] = ptr[i];
  524.     if (tlen == sizeof(tmpbuf))
  525.         tlen = 0;
  526.     }
  527.     if (oldbaud != newbaud)
  528.     SetBaudRate(newbaud);
  529.     return(newbaud);
  530. }
  531.  
  532.