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 / client / fterm.c < prev    next >
C/C++ Source or Header  |  1989-12-11  |  10KB  |  433 lines

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