home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / networking / dnet / dnet2.3.2 / amiga / client / cliterm.c next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  7.7 KB  |  396 lines

  1.  
  2. /*
  3.  *  CLITERM.C
  4.  *
  5.  *  DNET (c)Copyright 1988-1989, Matthew Dillon, All Rights Reserved.
  6.  *
  7.  *  FTERM [-Nnet] [port]
  8.  */
  9.  
  10. #include "defs.h"
  11.  
  12. typedef struct IORequest RIO;
  13.  
  14.  
  15. #ifndef DEVICES_CONUNIT_H
  16. typedef struct ConUnit        CONUNIT;
  17. typedef struct IOStdReq     IOCON;
  18. #endif
  19.  
  20. short IgnoreNS = 0;
  21.  
  22. TA Ta = { (ubyte *)"topaz", 8 };
  23.  
  24. ITEXT IText[] = {
  25.     { 0, 1, JAM2, 0, 0, &Ta, (ubyte *)"Flush"          }
  26. };
  27.  
  28. ITEM Item[] = {
  29.     { NULL    , 0, 0, 100, 10, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[0], NULL, 'o' }
  30. };
  31.  
  32. MENU Menu[] = {
  33.     { NULL    , 0, 0, 100, 20, MENUENABLED, "Control", &Item[0] }
  34. };
  35.  
  36. char Title[80];
  37.  
  38. NW Nw = {
  39.     0, 12, 640, 60, -1, -1,
  40.     NEWSIZE|CLOSEWINDOW|MENUPICK,
  41.     WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|NOCAREREFRESH|ACTIVATE,
  42.     NULL, NULL, (ubyte *)Title, NULL, NULL,
  43.     32, 32, -1, -1, WBENCHSCREEN
  44. };
  45.  
  46. WIN *Win;
  47.  
  48. #ifdef MANX
  49. extern int Enable_Abort;
  50. #endif
  51. char Buf[512];
  52. char Term[64] = { "FTERM (UNNAMED SHELL)" };
  53. char Cc;
  54.  
  55. struct IntuitionBase *IntuitionBase;
  56. struct GfxBase *GfxBase;
  57. IOCON *iocr, *iocw;
  58.  
  59. void OpenConsole ARGS((WIN *, IOCON **, IOCON **));
  60. void HandleIoctl ARGS((short, short, char, WIN *, IOCON *));
  61. void CloseConsole ARGS((IOCON *, IOCON *));
  62. void setsize ARGS((IOCON *, void *, WIN *));
  63. void localecho ARGS((int));
  64.  
  65. void main ARGS((int, char **));
  66.  
  67. int
  68. brk()
  69. {
  70.     return(0);
  71. }
  72.  
  73.  
  74. void
  75. main(ac,av)
  76. char *av[];
  77. {
  78.     void *chan;
  79.     long imask, conmask, dmask, mask;
  80.     char notdone = 1;
  81.     char portspec = 0;
  82.     char *host = NULL;
  83.     uword port = PORT_AMIGASHELL;
  84.     int i;
  85.     int bp = 0;
  86.     char inputbuf[256];
  87.  
  88.     onbreak(brk);
  89.  
  90.     sprintf(Title, "CLITerm V%s%s opening, wait..", VERSION, CLITERM_VERSION);
  91.     {
  92.     short i;
  93.     for (i = 1; i < ac; ++i) {
  94.         if (strncmp(av[i], "-N", 2) == 0) {
  95.         host = av[i] + 2;
  96.         continue;
  97.         }
  98.         portspec = 1;
  99.         port = atoi(av[i]);
  100.     }
  101.     }
  102.     if (portspec)
  103.     printf("Using port %ld\n", port);
  104. #ifdef MANX
  105.     Enable_Abort = 0;
  106. #endif
  107.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
  108.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0);
  109.     Win = OpenWindow(&Nw);
  110.     if (Win == NULL)
  111.     goto e1;
  112.     OpenConsole(Win, &iocr, &iocw);
  113.     if (iocr == NULL || iocw == NULL)
  114.     goto e3;
  115.  
  116.     /*
  117.      *    We delay here to allow DNET to go through its RESTART sequence
  118.      *    (when DNET automatically runs FTERM, it does so to quickly).
  119.      *    Such a hack!
  120.      */
  121.  
  122.     Delay(50 * 4);
  123.     chan = DOpen(host, port, 20, 15);
  124.  
  125.     if (!chan) {
  126.     puts("Unable to connect");
  127.     goto e3;
  128.     }
  129.     DQueue(chan, 32);
  130.     SetMenuStrip(Win, Menu);
  131.     SetWindowTitles(Win, Term, (char *)-1);
  132.     imask   = 1 << Win->UserPort->mp_SigBit;
  133.     dmask   = 1 << ((PORT *)chan)->mp_SigBit;
  134.     conmask = 1 << iocr->io_Message.mn_ReplyPort->mp_SigBit;
  135.     iocr->io_Data = (APTR)&Cc;
  136.     iocr->io_Length = 1;
  137.     SendIO((RIO *)iocr);
  138.  
  139.     setsize(iocw, chan, Win);
  140.  
  141.     while (notdone) {
  142.     mask = Wait(imask|dmask|conmask);
  143.     if (mask & imask) {
  144.         IMESS *im;
  145.         while (im = (IMESS *)GetMsg(Win->UserPort)) {
  146.         switch(im->Class) {
  147.         case NEWSIZE:
  148.             if (IgnoreNS) {
  149.             --IgnoreNS;
  150.             setsize(iocw, NULL, Win);
  151.             } else {
  152.             setsize(iocw, chan, Win);
  153.             }
  154.             break;
  155.         case CLOSEWINDOW:
  156.             notdone = 0;
  157.             break;
  158.         case MENUPICK:
  159.             switch((uword)((MENUNUM(im->Code)<<8)|ITEMNUM(im->Code))) {
  160.             case 0x0000:    /*    menu 0 item 0    */
  161.             DIoctl(chan, CIO_FLUSH, 0, 0);
  162.             break;
  163.             case 0x0001:    /*    menu 0 item 1    */
  164.             case 0x0002:    /*    menu 0 item 2    */
  165.             case 0x0100:    /*    menu 1 item 0    */
  166.             break;
  167.             }
  168.             break;
  169.         default:
  170.             break;
  171.         }
  172.         ReplyMsg((MSG *)im);
  173.         }
  174.     }
  175.     if (mask & dmask) {
  176.         char buf[256];
  177.         int n;
  178.         if ((n = DNRead(chan, buf, 256)) > 0) {
  179.         for (i = 0; i < n; i++) {
  180.             if (buf[i] == '\n') {
  181. #ifdef __GNUC__
  182.             memmove(&buf[i+1], &buf[i], n - i);
  183. #else
  184.             movmem(&buf[i], &buf[i + 1], n - i);
  185. #endif
  186.             buf[i] = '\r';
  187.             i++;
  188.             n++;
  189.             }
  190.         }
  191.         iocw->io_Data = (APTR)buf;
  192.         iocw->io_Length = n;
  193.         DoIO((RIO *)iocw);
  194.         }
  195.         if (n == -2) {
  196.         short val;
  197.         short cmd;
  198.         char aux;
  199.  
  200.         cmd = DGetIoctl(chan, &val, &aux);
  201.         HandleIoctl(cmd, val, aux, Win, iocw);
  202.         /*
  203.         if (cmd == CIO_MODE) {
  204.             if (val)
  205.             SetWindowTitles(Win, "(Cooked)", (char *)-1);
  206.             else
  207.             SetWindowTitles(Win, "(Raw)", (char *)-1);
  208.         }
  209.         */
  210.         } else if (n < 0)
  211.         notdone = 0;
  212.     }
  213.     if (mask & conmask) {
  214.         if (CheckIO((RIO *)iocr)) {
  215.         WaitIO((RIO *)iocr);
  216.  
  217.         if (Cc == '\r')
  218.             Cc = '\n';
  219.         inputbuf[bp++] = Cc;
  220.  
  221.         if (Cc == 0x08) {
  222.             if (bp <= 1) {
  223.             bp = 0;
  224.             } else {
  225.             localecho(0x08);
  226.             localecho(0x20);
  227.             localecho(0x08);
  228.             bp -= 2;
  229.             }
  230.         } else if (Cc == 0x18) {
  231.             while (--bp > 0) {
  232.             localecho(0x08);
  233.             localecho(0x20);
  234.             localecho(0x08);
  235.             }
  236.         } else if (Cc == '\n') {
  237.             localecho('\n');
  238.             localecho('\r');
  239.             DWrite(chan, inputbuf, bp);
  240.             bp = 0;
  241.         } else {
  242.             localecho(Cc);
  243.         }
  244.  
  245.         iocr->io_Data = (APTR)&Cc;
  246.         iocr->io_Length = 1;
  247.         SendIO((RIO *)iocr);
  248. /*
  249.         if (Cc == '\r') {
  250.             Cc = '\n';
  251.             DWrite(chan, &Cc, 1);
  252.             iocr->io_Data = (APTR)&Cc;
  253.             iocr->io_Length = 1;
  254.             SendIO(iocr);
  255.         }
  256. */
  257.         }
  258.     }
  259.     }
  260.     AbortIO((RIO *)iocr);
  261.     WaitIO((RIO *)iocr);
  262.     SetWindowTitles(Win, "Closing...", (char *)-1);
  263.     DClose(chan);
  264. e3: CloseConsole(iocr,iocw);
  265.     CloseWindow(Win);
  266. e1: CloseLibrary((LIB *)IntuitionBase);
  267.     CloseLibrary((LIB *)GfxBase);
  268. }
  269.  
  270. void
  271. localecho(c)
  272. int c;
  273. {
  274.     char cc;
  275.  
  276.     cc = c;
  277.     iocw->io_Data = (APTR)&cc;
  278.     iocw->io_Length = 1;
  279.     DoIO((RIO *)iocw);
  280. }
  281.  
  282. void
  283. OpenConsole(win, piocr, piocw)
  284. IOCON **piocr, **piocw;
  285. WIN *win;
  286. {
  287.     PORT *port;
  288.     static IOCON iocr, iocw;
  289.     int error;
  290.  
  291.     port = CreatePort(NULL, 0);
  292.     iocr.io_Command = CMD_READ;
  293.     iocr.io_Data = (APTR)win;
  294.     iocr.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  295.     iocr.io_Message.mn_ReplyPort = port;
  296.     error = OpenDevice("console.device", 0, (RIO *)&iocr, 0);
  297.     if (!error) {
  298.     iocw = iocr;
  299.     iocw.io_Command = CMD_WRITE;
  300.     *piocr = &iocr;
  301.     *piocw = &iocw;
  302.     } else {
  303.     *piocr = *piocw = NULL;
  304.     }
  305. }
  306.  
  307. void
  308. CloseConsole(iocr, iocw)
  309. IOCON *iocr;
  310. IOCON *iocw;
  311. {
  312.     IOCON *tmp = (iocr) ? iocr : iocw;
  313.     if (tmp) {
  314.     CloseDevice((RIO *)tmp);
  315.     DeletePort(tmp->io_Message.mn_ReplyPort);
  316.     }
  317. }
  318.  
  319. void
  320. setsize(iocw, chan, win)
  321. IOCON *iocw;
  322. void *chan;
  323. WIN *win;
  324. {
  325.     struct ConUnit *cu = (struct ConUnit *)iocw->io_Unit;
  326.     static char IStr[] = { "\033c\23320l\233t\233u" };
  327.  
  328.     /*
  329.     if (Cooked & 4)
  330.     IStr[5] = 'h';
  331.     else
  332.     IStr[5] = 'l';
  333.     */
  334.     iocw->io_Data = (APTR)IStr;
  335.     iocw->io_Length = sizeof(IStr) - 1;
  336.     DoIO((RIO *)iocw);
  337.     if (chan) {
  338.     DIoctl(chan, CIO_SETROWS, (uword)(cu->cu_YMax+1), 0);
  339.     DIoctl(chan, CIO_SETCOLS, (uword)(cu->cu_XMax+1), 0);
  340.     }
  341.     sprintf(Term, "FTERM   %ld x %ld", cu->cu_YMax+1, cu->cu_XMax+1);
  342.     SetWindowTitles(win, Term, (char *)-1);
  343. }
  344.  
  345. void
  346. HandleIoctl(cmd, val, aux, win, iocw)
  347. short cmd, val;
  348. char aux;
  349. WIN *win;
  350. IOCON *iocw;
  351. {
  352.     static short saverows;
  353.     short height, width;
  354.     short dx, dy;
  355.  
  356.     switch(cmd) {
  357.     case CIO_MODE:
  358.     /*
  359.     Cooked = val;
  360.     */
  361.     break;
  362.     case CIO_SETROWS:
  363.     saverows = val;
  364.     break;
  365.     case CIO_SETCOLS:
  366.     width = val * win->RPort->TxWidth + win->BorderLeft + win->BorderRight;
  367.     height= saverows * win->RPort->TxHeight + win->BorderTop + win->BorderBottom;
  368.  
  369.     dx = win->WScreen->Width - (win->LeftEdge + width);
  370.     if (dx > 0)
  371.         dx = 0;
  372.     if (-dx > win->LeftEdge) {
  373.         dx = -win->LeftEdge;
  374.         width = win->WScreen->Width;
  375.     }
  376.  
  377.     dy = win->WScreen->Height - (win->TopEdge + height);
  378.     if (dy > 0)
  379.         dy = 0;
  380.     if (-dy > win->TopEdge) {
  381.         dy = -win->TopEdge;
  382.         height = win->WScreen->Height;
  383.     }
  384.  
  385.     if (dx || dy) {
  386.         MoveWindow(win, dx, dy);
  387.     }
  388.     if (win->Width != width || win->Height != height) {
  389.         SizeWindow(win, width - win->Width, height - win->Height);
  390.         ++IgnoreNS;
  391.     }
  392.     break;
  393.     }
  394. }
  395.  
  396.