home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / c-kermit / ckicon.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  9KB  |  316 lines

  1. char *connv = "Amiga Connect command $Id: ckicon.c,v 1.6 1993/08/03 08:25:40 swalton Exp swalton $";
  2.  
  3. /*  C K I C O N  --  Dumb terminal connection to remote system, for Amiga  */
  4. /*
  5.   Author: Frank da Cruz (SY.FDC@CU20B),
  6.   Columbia University Center for Computing Activities, January 1985.
  7.   Copyright (C) 1985, Trustees of Columbia University in the City of New York.
  8.   Modified for Amiga by Jack J. Rouse, The Software Distillery
  9.  
  10.   Copyright (C) 1985, 1992, Trustees of Columbia University in the City of New
  11.   York.  Permission is granted to any individual or institution to use this
  12.   software as long as it is not sold for profit.  This copyright notice must be
  13.   retained.  This software may not be included in commercial products without
  14.   written permission of Columbia University.
  15.  
  16.   Uses the following special functions from ckitio:
  17.     conttb() -- Prepares for connect mode.
  18.     contte() -- Terminates connect mode.  If any active write does
  19.         terminate in 1 second, it is aborted and the serial
  20.         device restarted to break XOFF deadlocks.
  21.     ttocq(c) -- Outputs character to serial device.  If it can not
  22.         be sent immediately, it is queued (as when XOFF'ed).
  23.         If the queue (currently 64 characters) overflows, it
  24.         returns an error.
  25.     contti() -- Waits for console or serial input, returning console
  26.         character, or -2 if serial input is available first.
  27.         It sends queued serial output when the serial device
  28.         is ready for it.
  29.  
  30.  
  31.  * $Log: ckicon.c,v $
  32.  * Revision 1.6  1993/08/03 08:25:40  swalton
  33.  * Changes from Olaf Barthel to handle 8-bit terminal connections properly.
  34.  *
  35.  * Revision 1.5  93/04/18  16:51:23  swalton
  36.  * Increased size of line buffer from 200 to 4096, following ckucon.c
  37.  * Seems to help with high baud rates.  Also get an actual buffer size
  38.  * of 4097, because ttxin() adds a null to the end of the line.
  39.  * 
  40.  * Revision 1.4  92/10/30  16:10:48  swalton
  41.  * Change in the format of the copyright notice.  No code changes.
  42.  * 
  43.  * Revision 1.3  92/01/15  17:13:43  swalton
  44.  * Use Id rather than Header in identification string.
  45.  * 
  46.  * Revision 1.2  91/05/29  09:05:44  swalton
  47.  * Changed all function declarations to prototype style.
  48.  * Also removed standard include files (stdio.h, stdlib.h) as these are
  49.  * now included by ckcdeb..
  50.  * 
  51.  * Revision 1.1  90/07/12  22:29:31  swalton
  52.  * Administrative check-in only;  no modifications were made to this modue
  53.  * for version 5A(149) of C Kermit.
  54.  *
  55.  * Revision 1.0  90/04/30  11:54:35  swalton
  56.  * Initial revision
  57.  *
  58. */
  59.  
  60. #include "ckcdeb.h"
  61. #include "ckcker.h"
  62. #include "ckcasc.h"
  63.  
  64. extern int local, escape, duplex, parity, cmask, flow, seslog, mdmtyp;
  65. extern long speed;
  66. extern int errno;
  67. extern char ttname[], sesfil[];
  68.  
  69. int ttocq(char);
  70. void doesc(int);
  71.  
  72. /* Variables global to this module */
  73. extern char *chstr();
  74. static int active;
  75. static int logging;            /* session logging active */
  76.  
  77. #define LBUFL 4096            /* Line buffer length */
  78. #ifdef DYNAMIC
  79. char *lbuf = NULL;
  80. #else
  81. char lbuf[LBUFL+1];            /* +1 because ttxin() adds a null */
  82. #endif
  83.  
  84. /*  C O N E C T  --  Perform terminal connection  */
  85.  
  86. conect() {
  87.     int c, n, i;
  88.     int seenescape;
  89.     int overrun;
  90.  
  91.     if (!local) {
  92.     printf("Sorry, you must 'set line' first\n");
  93.     return(-2);
  94.     }
  95.     if (speed < 0) {
  96.     printf("Sorry, you must 'set speed' first\n");
  97.     return(-2);
  98.     }
  99.     if (escape < 0 || escape > 0177) {
  100.     printf("Your escape character is not ASCII - %d\n",escape);
  101.     return(-2);
  102.     }
  103.     if (ttopen(ttname,&local,mdmtyp,0) < 0) {
  104.     printf("Sorry, can't open %s\n",ttname);
  105.     return(-2);
  106.     }
  107. #ifdef DYNAMIC
  108.     if ((lbuf = (char *) malloc(LBUFL+1)) == NULL) {
  109.     printf("Can't get buffer in ckicon\n");
  110.     return(-2);
  111.     }
  112. #endif
  113.     printf("Connecting thru %s, speed %d.\r\n",ttname,speed);
  114.     printf("The escape character is %s (%d).\r\n",chstr(escape),escape);
  115.     printf("Type the escape character followed by C to get back,\r\n");
  116.     printf("or followed by ? to see other options.\r\n");
  117.     if (seslog) printf("(Session logged to %s.)\r\n",sesfil);
  118.  
  119.     /* Condition console terminal and communication line */
  120.  
  121.     if (conbin(escape) < 0) {
  122.     printf("Sorry, can't condition console terminal\n");
  123.     return(-2);
  124.     }
  125.     if (ttvt(speed,flow) < 0) {
  126.     conres();
  127.     printf("Sorry, Can't condition communication line\n");
  128.     return(-2);
  129.     }
  130.  
  131.     connoi();            /* disable interrupts so we can use ^C, ^D */
  132.     conttb();            /* prepare contti interface */
  133.     active = logging = 1;
  134.     seenescape = overrun = 0;
  135.     while (active) {
  136.     if ((c = contti()) >= 0) {
  137.         if (seenescape) {
  138.         doesc(c);
  139.         seenescape = 0;
  140.         }
  141.         else if (c == escape)
  142.         seenescape = 1;
  143.         else if ((i = ttocq(dopar(c))) >= 0) { /* Ordinary character */
  144.         overrun = 0;
  145.         if (duplex)
  146.         {   /* Half duplex? */
  147.             conoc(c);    /* Yes, also echo it. */
  148.             if (seslog && logging)     /* And maybe log it. */
  149.             if (zchout(ZSFILE,c) < 0)
  150.                 seslog = 0;
  151.         }
  152.         }
  153.         else if (i == -2) { /* overrun */
  154.         if (!overrun) conoc(BEL);
  155.         overrun = 1;
  156.         }
  157.         else {
  158.         printf("Can't send character\n");
  159.         active = 0;
  160.         }
  161.     }
  162.     else if (c != -2)
  163.     {
  164.         printf("Can't get character\n");
  165.         break;
  166.     }
  167.     if (!active) break;
  168.     n = ttchk();
  169.     if (n > LBUFL)
  170.         n = LBUFL;
  171.     else if (n < 0) {
  172.         printf("Can't get character\n");
  173.         active = 0;
  174.     }
  175.     if (n > 0 && (n = ttxin(n, lbuf)) > 0) {
  176.         if (cmask != 0377)
  177.         for (i = 0; i < n; ++i) lbuf[i] &= cmask; /* Strip */
  178.         conxo(n, lbuf);
  179.         if (seslog) zsoutx(ZSFILE,lbuf,n);        /* Log */
  180.     }
  181.     }
  182.  
  183.     contte();            /* clean up contti stuff */
  184.     conres();            /* Reset the console. */
  185. #ifdef DYNAMIC
  186.     if (lbuf != NULL) free(lbuf);    /* Be paranoid */
  187.     lbuf = NULL;
  188. #endif
  189.     /* reset funny modes and print reassuring message */
  190.     printf("\017\23320h[Back at Local System]\n");
  191.     return(0);
  192. }
  193.  
  194. /*  H C O N N E  --  Give help message for connect.  */
  195.  
  196. hconne() {
  197.     int c;
  198.     static char *hlpmsg[] = {"\
  199. \r\nC to close the connection, or:",
  200. "\r\n  0 (zero) to send a null",
  201. "\r\n  B to send a BREAK",
  202. "\r\n  H to hangup and close connection",
  203. "\r\n  Q to suspend logging",
  204. "\r\n  R to resume logging",
  205. "\r\n  S for status",
  206. "\r\n  ? for help",
  207. "\r\n escape character twice to send the escape character.\r\n\r\n",
  208. "" };
  209.  
  210.     conola(hlpmsg);            /* Print the help message. */
  211.     conol("Command>");            /* Prompt for command. */
  212.     while ((c = contti()) < 0);        /* wait for char, ignoring errors */
  213.     conoc(c);                /* Echo it. */
  214.     conoll("");
  215.     c &= 0177;                /* Strip any parity. */
  216.     return(c);                /* Return it. */
  217. }
  218.  
  219.  
  220. /*  C H S T R  --  Make a printable string out of a character  */
  221.  
  222. char *
  223. chstr(c) int c; {
  224.     static char s[8];
  225.     char *cp = s;
  226.  
  227.     if (c < SP) {
  228.     sprintf(cp,"CTRL-%c",ctl(c));
  229.     } else sprintf(cp,"'%c'\n",c);
  230.     cp = s;
  231.     return(cp);
  232. }
  233.  
  234. /*  D O E S C  --  Process an escape character argument  */
  235.  
  236. void
  237. doesc(c) char c; {
  238.     CHAR d;
  239.     char temp[50];
  240.     int n;
  241.  
  242.     c &= 0177;
  243.     while (1) {
  244.     if (c == escape) {        /* Send escape character */
  245.         d = dopar(c); ttocq(d); return;
  246.         } else                /* Or else look it up below. */
  247.         if (isupper(c)) c = tolower(c);
  248.  
  249.     switch (c) {
  250.  
  251.     case 'c':            /* Close connection */
  252.     case '\03':
  253.         active = 0; conol("\r\n"); return;
  254.  
  255.     case 'b':            /* Send a BREAK signal */
  256.     case '\02':
  257.         ttsndb(); return;
  258.  
  259.     case 'h':            /* Hangup */
  260.     case '\010':
  261.         tthang(); active = 0; conol("\r\n"); return;
  262.  
  263.     case 'q':
  264.     case '\021':
  265.         if (logging && seslog) conoll("(Logging suspended)");
  266.         logging = 0;
  267.         return;
  268.  
  269.     case 'r':
  270.     case '\022':
  271.         if (!logging && seslog) conoll("(Logging resumed)");
  272.         logging = 1;
  273.         return;
  274.  
  275.     case 's':            /* Status */
  276.     case '\023':
  277.         conol("\r\nConnected thru ");
  278.         conol(ttname);
  279.         if (speed >= 0) {
  280.         sprintf(temp,", speed %d",speed); conol(temp);
  281.         }
  282.         if (parity) {
  283.         conol(", ");
  284.         switch (parity) {
  285.             case 'e': conol("even");  break;
  286.             case 'o': conol("odd");   break;
  287.             case 's': conol("space"); break;
  288.             case 'm': conol("mark");  break;
  289.         }
  290.         conol(" parity");
  291.         }
  292.         if (seslog) {
  293.         conol(", logging to "); conol(sesfil);
  294.         if (!logging) conol(" (suspended)");
  295.             }
  296.         if ((n = ttonq()) > 0) {
  297.         sprintf(temp, ", %d output chars queued", n);
  298.         conol(temp);
  299.         }
  300.         conoll(""); return;
  301.  
  302.     case '?':            /* Help */
  303.         c = hconne(); continue;
  304.  
  305.     case '0':            /* Send a null */
  306.         c = '\0'; d = dopar(c); ttocq(d); return;
  307.  
  308.     case SP:            /* Space, ignore */
  309.         return;
  310.  
  311.     default:            /* Other */
  312.         conoc(BEL); return;     /* Invalid esc arg, beep */
  313.         }
  314.     }
  315. }
  316.