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 / fterm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  10.6 KB  |  484 lines

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