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