home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / c-kermit / ckvcon.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  54KB  |  1,740 lines

  1. #ifndef VMS
  2.       ERROR -- CKVCON.C is used only on the OpenVMS(tm) Operating System
  3. #else  /* VMS */
  4. #ifdef MULTINET
  5. #define MULTINET_OLD_STYLE
  6. #endif /* MULTINET */
  7. #endif /* VMS */
  8.  
  9. char *connv = "CONNECT Command 8.0.063 10 March 2010";
  10.  
  11. /*  C K V C O N  --  Terminal session to remote system, for VMS  */
  12. /*
  13.   Author: Frank da Cruz <fdc@columbia.edu>,
  14.   Columbia University Academic Information Systems, New York City.
  15.  
  16.   Copyright (C) 1985, 2010,
  17.     Trustees of Columbia University in the City of New York.
  18.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  19.     copyright text in the ckcmai.c module for disclaimer and permissions.
  20. */
  21. /*
  22.  * Originally adapted from the UNIX C-Kermit CONNECT module by S. Rubenstein
  23.  * for systems without fork(), circa 1985.  This version of conect() uses
  24.  * contti(&c, &src) to return when a character is available from either the
  25.  * console or the communications connection -- kind of like select().
  26.  *
  27.  * Edit Date       By   What
  28.  *
  29.  * 010 06-Mar-1989 mab    General cleanup
  30.  * 011 23-Mar-1989 mab    Clean up doesc() code.  Add malloc() in place of
  31.  *             static buffer space.  Also increase buffer space.
  32.  * 012 27-Sep-1989 mab    Add XON sequence.
  33.  * 013 30-Mar-1991 fdc  Add so/si, character set translation.
  34.  * 014 06-Apr-1991 fdc  Adapted for TGV MultiNet TCP/IP connections.
  35.  * 015 21-Jun-1991 tmk  Cleaned up typo in session logging display.
  36.  * 016 28-Nov-1991 fdc  "Back at <hostname>", disallow CONNECT in background
  37.  * 017 25-Dec-1991 fdc  Added support for NOPUSH, added "Hanging up" message
  38.  * 018 11-Jan-1992 fdc  Added support for key mapping
  39.  * 019 27-Jan-1992 fdc  Added support for ANSI escape sequence recognition
  40.  * 020 00-May-1992 fdc  Rewrote conect() to used buffered i/o.
  41.  * 021 10-Jun-1992 fdc  Added support for Wollongong WIN/TCP from Ray Hunter.
  42.  *                      Fix messed-up help message.
  43.  * 022 20-Jun-1992 fdc  Fixed handling of CR on TELNET sessions.
  44.  * 023 28-Jun-1992 fdc  Cosmetic cleanup of "Connecting..." message.
  45.  * 024 12-Jul-1992 fdc  Added mdmhup() feature (see ckudia.c).
  46.  * 025 29-Jul-1992 fdc  Grouped TELNET IP and AYT into single writes.
  47.  * 026 13-Aug-1992 fdc  Added support for SET TELNET NEWLINE-MODE.
  48.  * 027 05-Sep-1992 lt   Added architecture ifdefs for OpenVMS, Alpha, at top.
  49.  * 028 08-Sep-1992 fdc  Separated input and output SO/SO shift states.
  50.  * 029 11-Oct-1992 fdc  Reduce modem-vs-net confusion in ttopen() calls.
  51.  * 030 27-Oct-1993 fdc  Correct a typo in network protocol-checking code.
  52.  * 031 23-Oct-1993 fdc  Reset i/o buffer pointers upon new connection.
  53.  * 032 14-Feb-1994 fdc  Some minor cleanups.
  54.  * 033  9-Mar-1994 fdc  Fixed failure to display quoted TELNET IAC (255).
  55.  * 034  2-Jul-1994 fdc  Add initial APC support.
  56.  * 035  9-Jul-1994 fdc  Fix APC support.
  57.  * 036 15-Sep-1994 fdc  Add support for SET TELNET NEWLINE RAW.
  58.  * 037 24-Mar-1996 fdc  Add support for nopush and autodownload.
  59.  * 038 11-May-1996 fdc  Add support for SET TERM ESC { ENABLED, DISABLED }
  60.  * 039  5-Jun-1996 fdc  Change H to mean Help rather than Hangup, U = hangUp.
  61.  * 040  6-Jun-1996 fdc  Add Autoupload.
  62.  * 041  6-Sep-1996 fdc  Try to handle parity better.
  63.  * 042  1-Sep-1997 fdc  Add triggers.
  64.  * 043 21-Sep-1997 fdc  Don't send NAK any more if autodownload detected.
  65.  * 044  1-Jan-1998 fdc  Enforce SET CARRIER-WATCH.
  66.  * 045  8-Feb-1998 fdc  In ckcgetc() allow contti() to return nothing.
  67.  * 046 27-Dec-1998 fdc  Remove obsolete references to me_binary.
  68.  * 047  8-Feb-1999 fdc  Allow for -2 and -3 returns from tn_doop().
  69.  * 048 22-Jul-1999 fdc  Fix <escchar>S bug (print buffer sbuf[] too small)
  70.  *                      and added more info to status display.
  71.  * 049 24-Aug-1999 fdc  Add Unicode (UTF-8) support.
  72.  * 050 24-Oct-1999 fdc  Correct #ifdefs for NOCSETS case.
  73.  * 051 30-Oct-1999 fdc  Fix broken escape sequence recognizer.
  74.  * 052  4-Nov-1999 fdc  Keep autodownload trigger to ourselves.
  75.  * 053 15-Nov-1999 fdc  Fix broken escape sequence recognizer again.
  76.  * 054 24-Nov-2000 fdc  Add learned scripts (search for CKLEARN).
  77.  * 055  7-Dec-2000 mb   Fix Multinet/DECC header file conflict.
  78.  * 056  5-May-2001 fdc  Use logchar() for session logging.
  79.  * 057 27-Jun-2001 fdc  Set cx_status to indicate CONNECT status, fix ^\u.
  80.  * 058 11-Sep-2002 fdc  Fix remote charset to local UTF-8 translation.
  81.  * 059 24-Oct-2002 jea  Add SSL/TLS support (preliminary).
  82.  * 060  5-Nov-2002 jea  More SSL work.
  83.  * 061 29-Nov-2002 jea  Fix disconnection returns codes, which makes SSL
  84.  *                      connections terminate correctly, plus fixes to Telnet
  85.  *                      Com Port Control.
  86.  * 062 05-Apr-2004 fdc  Allow for IA64 builds.
  87.  * 063 15-Mar-2010 fdc  Make inesc[] and oldesc[] not static (for INPUT)
  88.  */
  89. #include "ckcdeb.h"
  90. #include "ckcasc.h"
  91. #include "ckcker.h"
  92. #include "ckucmd.h"
  93. #include "ckcnet.h"
  94. #include "ckvvms.h"
  95. #ifndef NOCSETS
  96. #include "ckcxla.h"            /* Character set translation */
  97. #endif /* NOCSETS */
  98. #include <stdio.h>
  99. #include <ctype.h>
  100. #include <signal.h>
  101. #include <setjmp.h>
  102.  
  103. #ifdef CKLEARN
  104. #include <time.h>
  105. #endif /* CKLEARN */
  106.  
  107. static int src;                /* Where input character came from */
  108.  
  109. extern long speed;
  110.  
  111. extern int local, escape, duplex, parity, flow, seslog, mdmtyp, batch;
  112. extern int cmask, cmdmsk, debses, sosi, ttyfd, what, quiet, tnlm, hints,
  113.  tt_crd, tn_nlm, tt_escape, itsatty;
  114. extern char ttname[], sesfil[], myhost[];
  115. #ifdef TNCODE
  116. extern int tn_b_nlm;
  117. #endif /* TNCODE */
  118.  
  119. #ifdef CKLEARN
  120. extern FILE * learnfp;
  121. extern int learning;
  122. static ULONG learnt1;
  123. static char learnbuf[LEARNBUFSIZ] = { NUL, NUL };
  124. static int  learnbc = 0;
  125. static int  learnbp = 0;
  126. static int  learnst = 0;
  127. #endif /* CKLEARN */
  128.  
  129. #ifdef CK_TRIGGER
  130. extern char * tt_trigger[], * triggerval;
  131. #endif /* CK_TRIGGER */
  132.  
  133. extern int nopush;            /* Runtime NOPUSH */
  134. extern int cx_status;            /* CONNECT status code */
  135.  
  136. #ifndef NOICP                /* Keyboard mapping */
  137. #ifndef NOSETKEY
  138. extern KEY *keymap;            /* Single-character key map */
  139. extern MACRO *macrotab;            /* Key macro pointer table */
  140. static MACRO kmptr = NULL;        /* Pointer to current key macro */
  141. #endif /* NOSETKEY */
  142. #endif /* NOICP */
  143.  
  144. extern int carrier;            /* CARRIER-WATCH selection */
  145.  
  146. /* Network support */
  147. extern int ttnproto,            /* Virtual terminal protocol */
  148.   network,                /* Network connection active */
  149.   nettype;                /* Network type */
  150.  
  151. static int unicode = 0;
  152.  
  153. #ifndef NOCSETS
  154. #ifdef CK_ANSIC
  155. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Character set */
  156. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* translation functions */
  157. static CHAR (*sxo)(CHAR);    /* Local translation functions */
  158. static CHAR (*rxo)(CHAR);    /* for output (sending) terminal chars */
  159. static CHAR (*sxi)(CHAR);    /* and for input (receiving) terminal chars. */
  160. static CHAR (*rxi)(CHAR);
  161. #else
  162. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])();    /* Character set */
  163. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])();    /* translation functions. */
  164. static CHAR (*sxo)();        /* Local translation functions */
  165. static CHAR (*rxo)();        /* for output (sending) terminal chars */
  166. static CHAR (*sxi)();        /* and for input (receiving) terminal chars. */
  167. static CHAR (*rxi)();
  168. #endif /* CK_ANSIC */
  169. extern int language;        /* Current language. */
  170. static int langsv;        /* Remember language */
  171. extern struct csinfo fcsinfo[]; /* File character set info */
  172. extern int tcsr, tcsl;        /* Terminal character sets, remote & local. */
  173. static int tcs;            /* Intermediate (xfer) char set */
  174. static int tcssize = 0;        /* Size of tcs */
  175. #ifdef UNICODE                /* UTF-8 support */
  176. #ifdef CK_ANSIC
  177. extern int (*xl_ufc[MAXFCSETS+1])(USHORT);  /* Unicode to FCS */
  178. extern USHORT (*xl_fcu[MAXFCSETS+1])(CHAR); /* FCS to Unicode */
  179. extern int (*xuf)(USHORT);        /* Translation function UCS to FCS */
  180. extern USHORT (*xfu)(CHAR);        /* Translation function FCS to UCS */
  181. #else
  182. extern int (*xl_ufc[MAXFCSETS+1])();
  183. extern USHORT (*xl_fcu[MAXFCSETS+1])();
  184. extern int (*xuf)();
  185. extern USHORT (*xfu)();
  186. #endif /* CK_ANSIC */
  187. #endif /* UNICODE */
  188. #endif /* NOCSETS */
  189.  
  190. _PROTOTYP( VOID doesc, (unsigned char) );
  191.  
  192. #ifndef NOSHOW
  193. _PROTOTYP( VOID shomdm, (void) );
  194. #endif /* NOSHOW */
  195.  
  196. /* Internal prototypes */
  197. _PROTOTYP( static int ckcputf, (void) );
  198. _PROTOTYP( int conresne, (void) );
  199. _PROTOTYP( int contti, (int *, int*) );
  200. #ifdef CK_SSL
  201. _PROTOTYP( int ssl_contti, (int *, int*) );
  202. #endif /* CK_SSL */
  203.  
  204. #ifdef CK_TRIGGER
  205. _PROTOTYP( int autoexitchk, (CHAR) );
  206. #endif /* CK_TRIGGER */
  207.  
  208. #ifdef CK_APC
  209. extern int apcactive;            /* Application Program Command (APC) */
  210. extern int apcstatus;            /* items ... */
  211. static int apclength = 0;
  212. #ifdef DCMDBUF
  213. extern char *apcbuf;
  214. #else
  215. extern char apcbuf[];
  216. #endif /* DCMDBUF */
  217. static int apcbuflen = APCBUFLEN - 2;
  218. extern int autodl, protocol;        /* Autodownload */
  219. #endif /* CK_APC */
  220.  
  221. int i, active;                /* Variables global to this module */
  222. static char *p;
  223.  
  224. #define OUTXBUFSIZ 15
  225. static CHAR inxbuf[OUTXBUFSIZ+1];    /* Host-to-screen expansion buffer */
  226. static int inxcount = 0;        /* and count */
  227. static CHAR outxbuf[OUTXBUFSIZ+1];    /* Keyboard-to-host expansion buf */
  228. static int outxcount = 0;        /* and count */
  229.  
  230. static int escseq = 0;            /* 1 = Recognizer is active */
  231. /* static */ int inesc[2] = { 0, 0 };    /* State of sequence recognizer */
  232. /* static */ int oldesc[2] = { -1, -1 }; /* Previous state of recognizer */
  233.  
  234. #ifndef NOESCSEQ
  235. /*
  236.   As of edit 178, the CONNECT command skips past ANSI escape sequences to
  237.   avoid translating the characters within them.  This allows the CONNECT
  238.   command to work correctly with a host that uses a 7-bit ISO 646 national
  239.   character set, in which characters like '[' would normally be translated
  240.   into accented characters, ruining the terminal's interpretation (and
  241.   generation) of escape sequences.
  242.  
  243.   As of edit 190, the CONNECT command responds to APC escape sequences
  244.   (ESC _ text ESC \) if the user SETs TERMINAL APC ON or UNCHECKED, and the
  245.   program was built with CK_APC defined.
  246.  
  247.   Non-ANSI/ISO-compliant escape sequences are not handled.
  248. */
  249.  
  250. /* States for the escape-sequence recognizer. */
  251.  
  252. #define ES_NORMAL 0            /* Normal, not in an escape sequence */
  253. #define ES_GOTESC 1            /* Current character is ESC */
  254. #define ES_ESCSEQ 2            /* Inside an escape sequence */
  255. #define ES_GOTCSI 3            /* Inside a control sequence */
  256. #define ES_STRING 4            /* Inside DCS,OSC,PM, or APC string */
  257. #define ES_TERMIN 5            /* 1st char of string terminator */
  258.  
  259. /*
  260.   ANSI escape sequence handling.  Only the 7-bit form is treated, because
  261.   translation is not a problem in the 8-bit environment, in which all GL
  262.   characters are ASCII and no translation takes place.  So we don't check
  263.   for the 8-bit single-character versions of CSI, DCS, OSC, APC, or ST.
  264.   Here is the ANSI sequence recognizer state table, followed by the code
  265.   that implements it.
  266.  
  267.   Definitions:
  268.     CAN = Cancel                       01/08         Ctrl-X
  269.     SUB = Substitute                   01/10         Ctrl-Z
  270.     DCS = Device Control Sequence      01/11 05/00   ESC P
  271.     CSI = Control Sequence Introducer  01/11 05/11   ESC [
  272.     ST  = String Terminator            01/11 05/12   ESC \
  273.     OSC = Operating System Command     01/11 05/13   ESC ]
  274.     PM  = Privacy Message              01/11 05/14   ESC ^
  275.     APC = Application Program Command  01/11 05/15   ESC _
  276.  
  277.   ANSI escape sequence recognizer:
  278.  
  279.     State    Input  New State  ; Commentary
  280.  
  281.     NORMAL   (start)           ; Start in NORMAL state
  282.  
  283.     (any)    CAN    NORMAL     ; ^X cancels
  284.     (any)    SUB    NORMAL     ; ^Z cancels
  285.  
  286.     NORMAL   ESC    GOTESC     ; Begin escape sequence
  287.     NORMAL   other             ; NORMAL control or graphic character
  288.  
  289.     GOTESC   ESC               ; Start again
  290.     GOTESC   [      GOTCSI     ; CSI
  291.     GOTESC   P      STRING     ; DCS introducer, consume through ST
  292.     GOTESC   ]      STRING     ; OSC introducer, consume through ST
  293.     GOTESC   ^      STRING     ; PM  introducer, consume through ST
  294.     GOTESC   _      STRING     ; APC introducer, consume through ST
  295.     GOTESC   0..~   NORMAL     ; 03/00 through 17/14 = Final character
  296.     GOTESC   other  ESCSEQ     ; Intermediate or ignored control character
  297.  
  298.     ESCSEQ   ESC    GOTESC     ; Start again
  299.     ESCSEQ   0..~   NORMAL     ; 03/00 through 17/14 = Final character
  300.     ESCSEQ   other             ; Intermediate or ignored control character
  301.  
  302.     GOTCSI   ESC    GOTESC     ; Start again
  303.     GOTCSI   @..~   NORMAL     ; 04/00 through 17/14 = Final character
  304.     GOTCSI   other             ; Intermediate char or ignored control char
  305.  
  306.     STRING   ESC    TERMIN     ; Maybe have ST
  307.     STRING   other             ; Consume all else
  308.  
  309.     TERMIN   \      NORMAL     ; End of string
  310.     TERMIN   other  STRING     ; Still in string
  311. */
  312. /*
  313.   C H K A E S  --  Check ANSI Escape Sequence.
  314.  
  315.   Call with EACH character in input stream.
  316.   src = 0 means c is incoming from remote; 1 = char from keyboard.
  317.   Sets global inesc[src] variable according to escape sequence state.
  318.   Returns 0 normally, 1 if an APC sequence is to be executed.
  319.   Handles transparent printing internally.
  320. */
  321. int
  322. #ifdef CK_ANSIC
  323. chkaes(char c, int src)
  324. #else
  325. chkaes(c,src) char c; int src;
  326. #endif /* CK_ANSIC */
  327. /* chkaes */ {
  328.  
  329.     debug(F111,"chkaes entry inesc",ckitoa(src),inesc[src]);
  330.     debug(F101,"chkaes c","",c);
  331.  
  332.     if (src < 0 || src > 1)        /* Don't allow bad args. */
  333.       return(0);
  334.  
  335.     oldesc[src] = inesc[src];        /* Remember previous state */
  336.  
  337. #ifdef XPRINT
  338.     if (inesc[src] && !src) {        /* Save up escape seq for printing  */
  339.     if (!c) return(0);        /* Ignore NULs */
  340.     if (escbufc < ESCBUFLEN) {
  341.         escbuf[escbufc++] = c;
  342.         escbuf[escbufc] = NUL;
  343.         debug(F111,"ESCBUF 1",escbuf,escbufc);
  344.     } else {            /* Buffer overrun */
  345.         if (printing && escbufc)    /* Print what's there so far */
  346.           zsoutx(ZMFILE,escbuf,escbufc);
  347.         escbufc = 1;        /* clear it out */
  348.         escbuf[0] = c;        /* and start off fresh buffer */
  349.         escbuf[1] = NUL;        /* with this character. */
  350.     }
  351.     }
  352. #endif /* XPRINT */
  353.  
  354.     if (c == CAN || c == SUB) {        /* CAN and SUB cancel any sequence */
  355. #ifdef XPRINT
  356.     if (!src) {
  357.         if (printing && escbufc > 1)
  358.           zsoutx(ZMFILE,escbuf,escbufc-1);
  359.         escbufc = 0;        /* Clear buffer */
  360.         escbuf[0] = NUL;
  361.     }
  362. #endif /* XPRINT */
  363.     inesc[src] = ES_NORMAL;
  364.     } else                /* Otherwise */
  365.  
  366.       switch (inesc[src]) {        /* enter state switcher */
  367.     case ES_NORMAL:            /* NORMAL state */
  368.       if (c == ESC) {        /* Got an ESC */
  369.           inesc[src] = ES_GOTESC;    /* Change state to GOTESC */
  370. #ifdef XPRINT
  371.           if (!src) {
  372.           escbufc = 1;        /* Clear escape sequence buffer */
  373.           escbuf[0] = c;    /* and deposit the ESC */
  374.           escbuf[1] = NUL;
  375.           debug(F111,"ESCBUF 2",escbuf,escbufc);
  376.           }
  377. #endif /* XPRINT */
  378.       }
  379.       break;            /* Otherwise stay in NORMAL state */
  380.  
  381.     case ES_GOTESC:            /* GOTESC state - prev char was ESC*/
  382.       if (c == '[') {        /* Left bracket after ESC is CSI */
  383.           inesc[src] = ES_GOTCSI;    /* Change to GOTCSI state */
  384.       } else if (c == 'P' || (c > 0134 && c < 0140)) { /* P, ], ^, or _ */
  385.           inesc[src] = ES_STRING;    /* Switch to STRING-absorption state */
  386. #ifdef XPRINT
  387.           debug(F111,"ESCBUF STRING",escbuf,escbufc);
  388. #endif /* XPRINT */
  389. #ifdef CK_APC
  390.           /* If APC not disabled */
  391.           if (!src && c == '_' && apcstatus != APC_OFF) {
  392.           debug(F100,"CONNECT APC begin","",0);
  393.           apcactive = APC_REMOTE; /* Set APC-Active flag */
  394.           apclength = 0;    /* and reset APC buffer pointer */
  395.           }
  396. #endif /* CK_APC */
  397.       } else if (c > 057 && c < 0177) { /* Final character '0' thru '~' */
  398.           inesc[src] = ES_NORMAL;    /* Back to normal */
  399. #ifdef XPRINT
  400.           if (!src) {
  401.           if (printing & escbufc > 1) {
  402.               /* Dump esc seq buf to printer */
  403.               zsoutx(ZMFILE,escbuf,escbufc-1);
  404.               debug(F111,"ESCBUF PRINT 1",escbuf,escbufc);
  405.           }
  406.           escbufc = 0;        /* Clear parameter buffer */
  407.           escbuf[0] = NUL;
  408.           }
  409. #endif /* XPRINT */
  410.       } else if (c != ESC) {    /* ESC in an escape sequence... */
  411.           inesc[src] = ES_ESCSEQ;    /* starts a new escape sequence */
  412.       }
  413.       break;            /* Intermediate or ignored ctrl char */
  414.  
  415.     case ES_ESCSEQ:            /* ESCSEQ -- in an escape sequence */
  416.       if (c > 057 && c < 0177) {    /* Final character '0' thru '~' */
  417.           inesc[src] = ES_NORMAL;    /* Return to NORMAL state. */
  418. #ifdef XPRINT
  419.           if (!src) {
  420.           if (printing && escbufc > 1) {
  421.               zsoutx(ZMFILE,escbuf,escbufc-1);
  422.               debug(F111,"ESCBUF PRINT 2",escbuf,escbufc);
  423.           }
  424.           escbufc = 0;        /* Clear escseq buffer */
  425.           escbuf[0] = NUL;
  426.           }
  427. #endif /* XPRINT */
  428.       } else if (c == ESC) {    /* ESC ... */
  429.           inesc[src] = ES_GOTESC;    /* starts a new escape sequence */
  430.       }
  431.       break;            /* Intermediate or ignored ctrl char */
  432.  
  433.     case ES_GOTCSI:            /* GOTCSI -- In a control sequence */
  434.       if (c > 077 && c < 0177) {    /* Final character '@' thru '~' */
  435. #ifdef XPRINT
  436.           if (!src && tt_print) {    /* Printer enabled? */
  437.           if (c == 'i') {    /* Final char is "i"? */
  438.               char * p = (char *) (escbuf + escbufc - 4);
  439.               if (!strncmp(p, "\033[5i", 4)) /* Yes, turn printer on */
  440.             printon();
  441.               else if (!strncmp(p, "\033[4i", 4)) { /* Or off... */
  442.               int i;
  443.               printoff();    /* Turn off printer. */
  444.               for (i = 0; i < escbufc; i++)    /* And output the */
  445.                 ckcputc(escbuf[i]);         /* sequence. */
  446.               } else if (printing && escbufc > 1) {
  447.               zsoutx(ZMFILE,escbuf,escbufc-1);
  448.               debug(F111,"ESCBUF PRINT 3",escbuf,escbufc);
  449.               }
  450.           } else if (printing && escbufc > 1) {
  451.               zsoutx(ZMFILE,escbuf,escbufc-1);
  452.               debug(F111,"ESCBUF PRINT 4",escbuf,escbufc);
  453.           }
  454.           }
  455.           if (!src) {
  456.           escbufc = 0;        /* Clear esc sequence buffer */
  457.           escbuf[0] = NUL;
  458.           }
  459. #endif /* XPRINT */
  460.           inesc[src] = ES_NORMAL;    /* Return to NORMAL. */
  461.       } else if (c == ESC) {    /* ESC ... */
  462.           inesc[src] = ES_GOTESC;    /* starts over. */
  463.       }
  464.       break;
  465.  
  466.     case ES_STRING:            /* Inside a string */
  467.       if (c == ESC)            /* ESC may be 1st char of terminator */
  468.         inesc[src] = ES_TERMIN;    /* Go see. */
  469. #ifdef CK_APC
  470.       else if (apcactive) {        /* If in APC */
  471.           if (apclength < apcbuflen) { /* and there is room... */
  472.           apcbuf[apclength++] = c; /* deposit this character. */
  473.           } else {            /* Buffer overrun */
  474.           apcactive = 0;    /* Discard what we got */
  475.           apclength = 0;    /* and go back to normal */
  476.           apcbuf[0] = 0;    /* Not pretty, but what else */
  477.           inesc[src] = ES_NORMAL; /* can we do?  (ST might not come) */
  478.           }
  479.       }
  480. #endif /* CK_APC */
  481.       break;            /* Absorb all other characters. */
  482.  
  483.     case ES_TERMIN:            /* Maybe a string terminator */
  484.       if (c == '\\') {        /* which must be backslash */
  485.           inesc[src] = ES_NORMAL;    /* If so, back to NORMAL */
  486. #ifdef XPRINT
  487.           if (!src) {
  488.           if (printing && escbufc > 1) { /* If printing... */
  489.               /* Print esc seq buffer */
  490.               zsoutx(ZMFILE,escbuf,escbufc-1);
  491.               debug(F111,"ESCBUF PRINT 5",escbuf,escbufc);
  492.           }
  493.           escbufc = 0;        /* Clear escseq buffer */
  494.           escbuf[0] = NUL;
  495.           }
  496. #endif /* XPRINT */
  497. #ifdef CK_APC
  498.           if (!src && apcactive) {    /* If it was an APC string, */
  499.           debug(F101,"CONNECT APC terminated","",c);
  500.           apcbuf[apclength] = NUL; /* terminate it and then ... */
  501.           return(1);
  502.           }
  503. #endif /* CK_APC */
  504.       } else {            /* It's not a backslash so... */
  505.           inesc[src] = ES_STRING;    /* back to string absorption. */
  506. #ifdef CK_APC
  507.           if (apcactive) {        /* In APC string */
  508.           if (apclength+1 < apcbuflen) { /* If enough room */
  509.               apcbuf[apclength++] = ESC; /* deposit the Esc */
  510.               apcbuf[apclength++] = c;   /* and this character too. */
  511.           } else {        /* Buffer overrun */
  512.               apcactive = 0;
  513.               apclength = 0;
  514.               apcbuf[0] = 0;
  515.               inesc[src] = ES_NORMAL;
  516.           }
  517.           }
  518. #endif /* CK_APC */
  519.       }
  520.       }    /* switch() */
  521.     debug(F111,"chkaes exit inesc",ckitoa(src),inesc[src]);
  522.     return(0);
  523. }
  524. #endif /* NOESCSEQ */
  525.  
  526. static char *ibp;            /* Input buffer pointer */
  527. static int ibc;                /* Input buffer count */
  528. #define IBUFL 1024            /* Input buffer length */
  529.  
  530. static char *obp;            /* Output buffer pointer */
  531. static int obc;                /* Output buffer count */
  532. #define OBUFL 1024            /* Output buffer length */
  533.  
  534. #ifdef DYNAMIC
  535. static char *ibuf = NULL, *obuf = NULL;    /* Line and temp buffers */
  536. #else
  537. static char ibuf[IBUFL], obuf[OBUFL];
  538. #endif /* DYNAMIC */
  539.  
  540. char kbuf[10], *kbp;            /* Keyboard buffer */
  541.  
  542. #ifdef CKLEARN
  543. static VOID
  544. learnchar(c) int c; {            /* Learned script keyboard character */
  545.     int cc;
  546.     char xbuf[8];
  547.  
  548.     if (!learning || !learnfp)
  549.       return;
  550.  
  551.     switch (learnst) {            /* Learn state... */
  552.       case 0:                /* Neutral */
  553.       case 1:                /* Net */
  554.     if (learnbc > 0) {        /* Have net characters? */
  555.         char buf[LEARNBUFSIZ];
  556.         int i, j, n;
  557.         ULONG t;
  558.  
  559.         t = (ULONG) time(0);    /* Calculate INPUT timeout */
  560.         j = t - learnt1;
  561.         j += (j / 4) > 0 ? (j / 4) : 1; /* Add some slop */
  562.         if (j < 2) j = 2;            /* 2 seconds minimum */
  563.  
  564.         fputs("\nINPUT ",learnfp);    /* Give INPUT command for them */
  565.         fputs(ckitoa(j),learnfp);
  566.         fputs(" {",learnfp);
  567.         learnt1 = t;
  568.  
  569.         n = LEARNBUFSIZ;
  570.         if (learnbc < LEARNBUFSIZ) {  /* Circular buffer */
  571.         n = learnbc;          /*  hasn't wrapped yet. */
  572.         learnbp = 0;
  573.         }
  574.         j = 0;            /* Copy to linear buffer */
  575.         for (i = 0; i < n; i++) {    /* Number of chars in circular buf */
  576.  
  577.         cc = learnbuf[(learnbp + i) % LEARNBUFSIZ];
  578.  
  579.         /* Later account for prompts that end with a newline? */
  580.  
  581.         if (cc == CR && j > 0) {
  582.             if (buf[j-1] != LF)
  583.               j = 0;
  584.         }
  585.         buf[j++] = cc;
  586.         }
  587.         for (i = 0; i < j; i++) {    /* Now copy out the buffer */
  588.         cc = buf[i];        /* interpreting control chars */
  589.         if (cc == 0) {        /* We don't INPUT NULs */
  590.             continue;
  591.         } else if (cc < SP ||    /* Controls need quoting */
  592.                cc > 126 && cc < 160) {
  593.             ckmakmsg(xbuf,8,"\\{",ckitoa((int)cc),"}",NULL);
  594.             fputs(xbuf,learnfp);
  595.         } else {        /* Plain character */
  596.             putc(cc,learnfp);
  597.         }
  598.         }                  
  599.         fputs("}\nIF FAIL STOP 1 INPUT timeout",learnfp);
  600.         learnbc = 0;
  601.     }
  602.     learnbp = 0;
  603.     fputs("\nPAUSE 1\nOUTPUT ",learnfp); /* Emit OUTPUT and fall thru */
  604.  
  605.       case 2:                /* Already in Keyboard state */
  606.     if (c == 0) {
  607.         fputs("\\N",learnfp);
  608.     } else if (c == -7) {
  609.         fputs("\\B",learnfp);
  610.     } else if (c == -8) {
  611.         fputs("\\L",learnfp);
  612.     } else if (c < SP || c > 126 && c < 160) {
  613.         ckmakmsg(xbuf,8,"\\{",ckitoa((int)c),"}",NULL);
  614.         fputs(xbuf,learnfp);
  615.     } else {
  616.         putc(c,learnfp);
  617.     }
  618.     }
  619. }
  620. #endif /* CKLEARN */
  621.  
  622. /*  C O N E C T  --  Perform terminal connection  */
  623.  
  624. int inshift, outshift;            /* SO/SI shift states */
  625.  
  626. /*  C K C P U T C  --  C-Kermit CONNECT Put Character to Screen  */
  627. /*
  628.   Output is buffered to avoid slow screen writes on fast connections.
  629. */
  630. int
  631. ckcputf() {                /* Dump the output buffer */
  632.     int x;
  633.     if (obc > 0)            /* If we have any characters, */
  634.       x = conxo(obc,obuf);        /* dump them, */
  635.     obp = obuf;                /* reset the pointer */
  636.     obc = 0;                /* and the counter. */
  637.     return(x);                /* Return conxo's return code */
  638. }
  639.  
  640. int
  641. ckcputc(c) int c; {
  642.     int x;
  643.  
  644.     *obp++ = c & 0xff;            /* Deposit the character */
  645.     obc++;                /* Count it */
  646.     if (ibc == 0 ||            /* If input buffer empty */
  647.     obc == OBUFL) {            /* or output buffer full */
  648.     x = conxo(obc,obuf);        /* dump the buffer, */
  649.     obp = obuf;            /* reset the pointer */
  650.     obc = 0;            /* and the counter. */
  651.     return(x);            /* Return conxo's return code */
  652.     } else return(0);
  653. }
  654.  
  655. #ifdef CK_SSL
  656.     extern int ssl_active_flag, tls_active_flag;
  657. #endif /* CK_SSL */
  658.  
  659. /*  C K C G E T C  --  C-Kermit CONNECT Get Character  */
  660. /*
  661.   Buffered read from communication device.
  662.   Returns the next character, refilling the buffer if necessary.
  663.   On error, returns ttinc's return code (see ttinc() description).
  664.   Dummy argument for compatible calling conventions with ttinc().
  665.   NOTE: We don't have a macro for this because we have to pass
  666.   a pointer to this function as an argument to tn_doop().
  667. */
  668. int
  669. ckcgetc(dummy) int dummy; {
  670.     int c, i, n;
  671.  
  672.     if (ibc > 0) {            /* Have buffered port characters */
  673.     src = 1;            /* Say source is port */
  674.     c = *ibp++ & 0xff;        /* Get next character */
  675.     ibc--;                /* Reduce input buffer count */
  676. #ifdef COMMENT
  677. /* This makes the debug log really big. */
  678.     debug(F101,"CKCGETC buffered port char","",c);
  679. #endif /* COMMENT */
  680.     return(c);            /* Return buffered port character */
  681.     } else {                /* Need to refill buffer */
  682.     for (i = 10; i >= 0; i--) {    /* Try 10 times... */
  683. #ifdef CK_SSL
  684.         if ( ssl_active_flag || tls_active_flag )
  685.         n = ssl_contti(&c, &src);
  686.         else
  687. #endif /* CK_SSL */
  688.           n = contti(&c, &src);    /* to read one character */
  689.         if (src == -1) {        /* Connection dropped */
  690.         return(n < 0 ? -2 : -1);/* Have to give up */
  691.         } else if (src == -2) {    /* HANGUP with CARRIER-WATCH OFF */
  692.         msleep(i*10);        /* and no character arrived */
  693.         debug(F101,"CKCGETC -2 countdown","",i);
  694.         continue;        /* Try so many times */
  695.         } else
  696.           break;
  697.     }
  698.     if (src < 0) {            /* Give up after too many tries */
  699.         debug(F100,"CKCGETC -2 gives up","",0);
  700.         return(-1);
  701.     }
  702.     if (src == 0) {            /* Got a character from the keyboard */
  703.         debug(F101,"CKCGETC keyboard char","",c);
  704.         return(c);
  705.     } else {            /* Got a port character */
  706.         ibp = ibuf;            /* Reset buffer pointer */
  707.         *ibp++ = c;            /* Deposit the character */
  708.         ibc++;            /* and count it */
  709. /*
  710.   Quickly read any more characters that might have arrived.  Ignore any errors
  711.   here; we need to return all the characters that arrived.  When we run out,
  712.   contti() will catch the error and return it.
  713. */
  714.         if ((n = ttchk()) > 0) {    /* Any more characters waiting? */
  715.         if (n > (IBUFL - ibc))    /* Get them all at once. */
  716.           n = IBUFL - ibc;    /* Don't overflow the buffer */
  717.         if ((n = ttxin(n,(CHAR *)ibp)) > 0) /* Read waiting chars */
  718.           ibc += n;        /* Advance counter */
  719.         }
  720.         ibp = ibuf;            /* Reset buffer pointer again */
  721.         c = *ibp++ & 0xff;        /* Get first character from buffer */
  722.         ibc--;            /* Reduce buffer count */
  723.         debug(F101,"CKCGETC port char","",c);
  724.         return(c);            /* Return the character */
  725.     }
  726.     }
  727. }
  728.  
  729. int
  730. conect() {
  731.     int c;            /* c is a character, but must be signed
  732.                    integer to pass thru -1, which is the
  733.                    modem disconnection signal, and is
  734.                    different from the character 0377 */
  735.     int c2, csave;        /* Copies of c */
  736.     char errmsg[50], *erp, *cp;
  737.     int n, x;                /* Workers */
  738.     int msgflg = 0;            /* Whether to print messages */
  739.     int apcrc;
  740.     int rc = 0;
  741. #ifdef CKLEARN
  742.     int crflag = 0;
  743. #endif /* CKLEARN */
  744.  
  745. #ifdef CK_TRIGGER
  746.     int ix;
  747. #endif /* CK_TRIGGER */
  748.  
  749.     cx_status = CSX_INTERNAL;
  750.     debok = 1;
  751.  
  752.     if (!local) {
  753. #ifdef NETCONN
  754.     printf("Sorry, you must SET LINE or SET HOST first\n");
  755. #else
  756.     printf("Sorry, you must SET LINE first\n");
  757. #endif /* NETCONN */
  758.     return(-2);
  759.     }
  760.     if (batch) {
  761.     printf("\n\
  762. Sorry, Kermit's CONNECT command requires a real terminal;\n");
  763.     printf("It can't be used in a batch job.\n");
  764.     printf("Use INPUT and OUTPUT commands instead.\n");
  765.     return(0);
  766.     } else if (!itsatty) {
  767.     printf("\n\
  768. Sorry, Kermit's CONNECT command can be used only on a real terminal.\n");
  769.     printf(
  770. "If you have started Kermit from a DCL command procedure, it needs:\n\n");
  771.     printf("$ DEFINE /USER SYS$INPUT SYS$COMMAND\n\n");
  772.     printf("before starting Kermit.\n\n");
  773.     return(0);
  774.     }
  775. #ifdef TCPSOCKET
  776.     if (network && (nettype != NET_TCPB)) {
  777.     printf("Sorry, network type not supported yet\n");
  778.     return(0);
  779.     }
  780. #endif /* TCPSOCKET */
  781.  
  782.     debug(F101,"ckvcon network","",network);
  783.     debug(F101,"ckvcon speed","",speed);
  784.     debug(F101,"ckvcon ttyfd","",ttyfd);
  785.     debug(F110,"ckvcon host",ttname,0);
  786. #ifdef CK_TRIGGER
  787.     debug(F110,"ckvcon trigger",tt_trigger[0],0);
  788. #endif /* CK_TRIGGER */
  789.  
  790.     if (speed < 0 && network == 0) {
  791.     printf("Sorry, you must SET SPEED first\n");
  792.     return(-2);
  793.     }
  794.     if ((escape < 0) || (escape > 0177)) {
  795.     printf("Your escape character is not ASCII - %d\n",escape);
  796.     return(-2);
  797.     }
  798.     if (ttyfd < 0) {            /* If communication device not open */
  799.     debug(F111,"ckvcon opening",ttname,0); /* Open it now. */
  800.     if (ttopen(ttname,
  801.            &local,
  802.            network ? -nettype : mdmtyp,
  803.            0
  804.            ) < 0) {
  805.         erp = errmsg;
  806.         sprintf(erp,"Sorry, can't open %s",ttname);
  807.         perror(errmsg);
  808.         debug(F110,"ckvcon open failure",errmsg,0);
  809.         return(-2);
  810.     }
  811. #ifdef IKS_OPTION
  812.     /* If peer is in Kermit server mode, return now. */
  813.     if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start) {
  814.         cx_status = CSX_IKSD;
  815.         return(0);
  816.     }
  817. #endif /* IKS_OPTION */
  818.     }
  819.     msgflg = !quiet
  820. #ifdef CK_APC
  821.       && !apcactive
  822. #endif /* CK_APC */
  823.     ;
  824.  
  825.     if (msgflg) {
  826. #ifdef NETCONN
  827.     if (network) {
  828.         printf("\nConnecting to host %s",ttname);
  829.     } else {
  830. #endif /* NETCONN */
  831.         printf("\nConnecting to %s",ttname);
  832.         if (speed > -1L) printf(", speed %ld",speed);
  833. #ifdef NETCONN
  834.     }
  835. #endif /* NETCONN */
  836.     if (tt_escape) {
  837.         printf("\r\n");
  838.         shoesc(escape);
  839.         printf("Type the escape character followed by C to get back,\r\n");
  840.         printf("or followed by ? to see other options.\r\n");
  841.     } else {
  842.         printf(".\r\n\nESCAPE CHARACTER IS DISABLED\r\n\n");
  843.     }
  844.     if (seslog) {
  845.         extern int slogts, sessft;
  846.         char * s = "";
  847.         switch (sessft) {
  848.           case XYFT_D:
  849.         s = "debug"; break;
  850.           case XYFT_T:
  851.         s = slogts ? "timestamped-text" : "text"; break;
  852.           default:
  853.         s = "binary";
  854.         }
  855.         printf("Session Log: %s, %s\r\n",sesfil,s);
  856.     }
  857.     if (debses) printf("Debugging Display...)\r\n");
  858.         printf("----------------------------------------------------\r\n");
  859.     printf("\r\n");
  860.     }
  861.  
  862. /* Condition console terminal and communication line */
  863.  
  864.     rc = 0;
  865.     if (conbin(escape) < 0) {
  866.     printf("Sorry, can't condition console terminal\n");
  867.         printf("----------------------------------------------------\r\n");
  868.     goto conret0;
  869.     }
  870.     debug(F101,"ckvcon flow","",flow);
  871.     if (ttvt(speed,flow) < 0) {
  872.     /* tthang(); */ /* Closing it should be quite enough! */
  873.     ttclos(0);
  874.     if (ttopen(ttname,        /* Open it again... */
  875.            &local,
  876.            network ? -nettype : mdmtyp,
  877.            0
  878.            ) < 0) {
  879.         erp = errmsg;
  880.         sprintf(erp,"Sorry, can't reopen %s",ttname);
  881.         perror(errmsg);
  882.         goto conret0;
  883.     }
  884. #ifdef IKS_OPTION
  885.     if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start) {
  886.         cx_status = CSX_IKSD;
  887.         goto conret0;
  888.     }
  889. #endif /* IKS_OPTION */
  890.     if (ttvt(speed,flow) < 0) {    /* Try virtual terminal mode again. */
  891.         conres();            /* Failure this time is fatal. */
  892.         printf("Sorry, Can't condition communication line\n");
  893.         goto conret0;
  894.     }
  895.     }
  896.     debug(F101,"CONNECT ttvt ok, escape","",escape);
  897.  
  898.     debug(F101,"CONNECT carrier-watch","",carrier);
  899.     if ((!network 
  900. #ifdef TN_COMPORT
  901.      || istncomport()
  902. #endif /* TN_COMPORT */
  903.      )&& (carrier != CAR_OFF)) {
  904.     int x;
  905.     x = ttgmdm();
  906.     debug(F100,"CONNECT ttgmdm","",x);
  907.     if ((x > -1) && !(x & BM_DCD)) {
  908.         debug(F100,"CONNECT ttgmdm CD test fails","",x);
  909.         conres();
  910.         printf("?Carrier required but not detected.\n");
  911.         rc = 0;
  912.         if (hints) {
  913.         printf("***********************************\n");
  914.         printf(" Hint: To CONNECT to a serial device that\n");
  915.         printf(" is not presenting the Carrier Detect signal,\n");
  916.         printf(" first tell C-Kermit to:\n\n");
  917.         printf("   SET CARRIER-WATCH OFF\n\n");
  918.         printf("***********************************\n\n");
  919.         }
  920.         cx_status = CSX_CARRIER;
  921.         goto conret0;
  922.     }
  923.     debug(F100,"CONNECT ttgmdm ok","",0);
  924.     }
  925.  
  926. #ifdef CK_APC
  927.     apcactive = 0;
  928.     apclength = 0;
  929. #endif /* CK_APC */
  930.  
  931. #ifndef NOCSETS
  932. /* Set up character set translations */
  933.  
  934.     unicode = 0;            /* Assume Unicode won't be involved */
  935.     tcs = 0;                /* "Transfer" or "Other" charset */
  936.     sxo = rxo = NULL;            /* Initialize byte-to-byte functions */
  937.     sxi = rxi = NULL;
  938.     if (tcsr != tcsl) {            /* Remote and local sets differ... */
  939. #ifdef UNICODE
  940.     if (tcsr == FC_UTF8 ||        /* Remote charset is UTF-8 */
  941.         tcsl == FC_UTF8) {        /* or local one is. */
  942.         xuf = xl_ufc[tcsl];        /* Incoming Unicode to local */
  943.         if (xuf || tcsl == FC_UTF8) {
  944.         tcs = (tcsr == FC_UTF8) ? tcsl : tcsr; /* The "other" set */
  945.         xfu = xl_fcu[tcs];    /* Local byte to remote Unicode */
  946.         if (xfu)
  947.           unicode = (tcsr == FC_UTF8) ? 1 : 2;
  948.         }
  949.         tcssize = fcsinfo[tcs].size; /* Size of other character set. */
  950.     } else {
  951. #endif /* UNICODE */
  952.         tcs = gettcs(tcsr,tcsl);    /* Get intermediate set. */
  953.         sxo = xls[tcs][tcsl];    /* translation function */
  954.         rxo = xlr[tcs][tcsr];    /* pointers for output functions */
  955.         sxi = xls[tcs][tcsr];    /* and for input functions. */
  956.         rxi = xlr[tcs][tcsl];
  957. #ifdef UNICODE
  958.     }
  959. #endif /* UNICODE */
  960.     }
  961. /*
  962.   This is to prevent use of zmstuff() and zdstuff() by translation functions.
  963.   They only work with disk i/o, not with communication i/o.  Luckily Russian
  964.   translation functions don't do any stuffing...
  965. */
  966.     langsv = language;
  967. #ifndef NOCYRIL
  968.     if (language != L_RUSSIAN)
  969. #endif /* NOCYRIL */
  970.       language = L_USASCII;
  971.  
  972. #ifndef NOESCSEQ
  973. /*
  974.   We need to activate escape-sequence recognition when:
  975.   (a) translation is elected, and:
  976.   (b) the local and/or remote set is 7-bit set other than US or UK ASCII, or:
  977.   (c) TERMINAL APC is not OFF.
  978. */
  979.     escseq = (tcs != TC_TRANSP) &&    /* Not transparent */
  980.       (fcsinfo[tcsl].size == 128 || fcsinfo[tcsr].size == 128) && /* 7 bits */
  981.     (fcsinfo[tcsl].code != FC_USASCII) && /* Not US ASCII */
  982.     (fcsinfo[tcsl].code != FC_UKASCII);   /* Not UK ASCII */
  983. #ifdef COMMENT
  984.     debug(F101,"tcs","",tcs);
  985.     debug(F101,"tcsl","",tcsl);
  986.     debug(F101,"tcsr","",tcsr);
  987.     debug(F101,"fcsinfo[tcsl].size","",fcsinfo[tcsl].size);
  988.     debug(F101,"fcsinfo[tcsr].size","",fcsinfo[tcsr].size);
  989. #endif /* COMMENT */
  990. #endif /* NOESCSEQ */
  991. #endif /* NOCSETS */
  992.  
  993. #ifndef NOESCSEQ
  994. #ifdef CK_APC
  995.     escseq = escseq || (apcstatus != APC_OFF);
  996.     apcactive = 0;            /* An APC command is not active */
  997.     apclength = 0;            /* ... */
  998. #endif /* CK_APC */
  999.     inesc[0] = ES_NORMAL;        /* Initial state of recognizer */
  1000.     inesc[1] = ES_NORMAL;
  1001.     debug(F101,"CONNECT escseq","",escseq);
  1002. #endif /* NOESCSEQ */
  1003.  
  1004.     rc = 0;
  1005. #ifdef DYNAMIC
  1006.     if (!ibuf) {
  1007.     if (!(ibuf = malloc(IBUFL+1))) { /* Allocate input line buffer */
  1008.         printf("Sorry, CONNECT input buffer can't be allocated\n");
  1009.         goto conret0;
  1010.     } else {
  1011.         ibp = ibuf;
  1012.         ibc = 0;
  1013.     }
  1014.     }
  1015.     if (!obuf) {
  1016.     if (!(obuf = malloc(OBUFL+1))) {    /* Allocate output line buffer */
  1017.         printf("Sorry, CONNECT output buffer can't be allocated\n");
  1018.         goto conret0;
  1019.     } else {
  1020.         obp = obuf;
  1021.         obc = 0;
  1022.     }
  1023.     }
  1024. #else
  1025.     ibp = ibuf;
  1026.     ibc = 0;
  1027.     obp = obuf;
  1028.     obc = 0;
  1029. #endif /* DYNAMIC */
  1030.  
  1031. #ifdef CKLEARN
  1032.     if (learning) {            /* Learned script active... */
  1033.     learnbp = 0;            /* INPUT buffer pointer */
  1034.     learnbc = 0;            /* INPUT buffer count */
  1035.     learnst = 0;            /* State (0 = neutral, none) */
  1036.     learnt1 = (ULONG) time(0);
  1037.     }
  1038. #endif /* CKLEARN */
  1039.  
  1040.     inshift = outshift = 0;        /* Initial SI/SO states */
  1041.     active = 1;
  1042.  
  1043.     debug(F100,"CONNECT starting contti loop","",0);
  1044.     do {                /* Top of big loop */
  1045. #ifndef NOSETKEY
  1046. /*
  1047.   Before reading anything from the keyboard, continue expanding the current
  1048.   active keyboard macro, if any.
  1049. */
  1050.     if (kmptr) {            /* Have active macro */
  1051.         src = 0;            /* Pretend char came from keyboard */
  1052.         if ((c = (CHAR) *kmptr++) == NUL) { /* but get it from the macro */
  1053.         kmptr = NULL;        /* If no more chars in macro,  */
  1054.         continue;        /* reset pointer and continue. */
  1055.         }
  1056.     } else                 /* OTHERWISE... */
  1057. #endif /* NOSETKEY */
  1058. /*
  1059.   contti() samples the keyboard and the communication device, in that order,
  1060.   in what amounts to a busy loop, and does not return until it has something
  1061.   from one or the other.  This drives the VAX crazy, and also gives poor
  1062.   performance: input is character-at a time, and the keyboard buffer is
  1063.   checked at least once for every character that comes in from the remote.
  1064.   Somebody who knows something about VMS, PLEASE FIND A BETTER WAY.
  1065.  
  1066.   We want a version of contti() that does what select() does in BSD, and we
  1067.   also want it to be buffered internally, so it doesn't have to call the
  1068.   operating system for every single input character.  And most of all we don't
  1069.   want it to run a busy loop all the time.  No doubt this involves ASTs,
  1070.   things of which I know nothing.    - fdc
  1071. */
  1072.     c = ckcgetc(0);            /* Calls contti()... */
  1073.     debug(F000,"CKCGETC","c",c);
  1074. /*
  1075.   Get here with a character in c, and:
  1076.  
  1077.   src = -1 Communication error
  1078.          1 Character from comm line
  1079.          0 Character from console
  1080. */
  1081.     if (c < 0 || src < 0) {        /* Comm line hangup or other error */
  1082.         ckcputf();            /* flush screen-output buffer */
  1083.         if (msgflg)
  1084.           printf("\r\nCommunications disconnect ");
  1085.         ttclos(0);            /* Close our end of the connection */
  1086.         if (cx_status != CSX_USERDISC)
  1087.           cx_status = CSX_HOSTDISC;
  1088.         active = 0;
  1089.  
  1090.     } else if (!src) {        /* Character from console */
  1091.         c &= cmdmsk;        /* Do requested masking */
  1092. #ifndef NOICP
  1093. #ifndef NOSETKEY
  1094. /*
  1095.   Note: kmptr is NULL if we got character c from the keyboard, and it is
  1096.   not NULL if it came from a macro.  In the latter case, we must avoid
  1097.   expanding it again.
  1098. */
  1099.         if (!kmptr && macrotab[c]) { /* If a macro is assigned to it */
  1100.         kmptr = macrotab[c];    /* set up the pointer */
  1101.         continue;        /* and do this again. */
  1102.         } else c = keymap[c];    /* Else use single-char keymap */
  1103. #endif /* NOSETKEY */
  1104. #endif /* NOICP */
  1105.         csave = c;
  1106. #ifdef CKLEARN
  1107.         crflag = (c == CR);        /* Remember if it was CR. */
  1108. #endif /* CKLEARN */
  1109.  
  1110.         if (
  1111. #ifndef NOICP
  1112. #ifndef NOSETKEY
  1113.             !kmptr &&
  1114. #endif /* NOSETKEY */
  1115. #endif /* NOICP */
  1116.         (tt_escape && (c & 0xff) == escape)) { /* Escape character? */
  1117.         conresne();        /* Restore to normal attributes */
  1118.         c = coninc(0) & 0177;
  1119.         doesc(c);
  1120.         conbin(escape);
  1121.         } else {            /* Ordinary character */
  1122. #ifndef NOCSETS
  1123.         if (inesc[1] == ES_NORMAL) { /* If not inside escape seq.. */
  1124. #ifdef UNICODE                /* Translate character sets */
  1125.             int x;
  1126.             CHAR ch;
  1127.             ch = c;
  1128.             if (unicode == 1) {    /* Remote is UTF-8 */
  1129.             outxcount = b_to_u(ch,outxbuf,OUTXBUFSIZ,tcssize);
  1130.             outxbuf[outxcount] = NUL;
  1131.             } else if (unicode == 2) { /* Local is UTF-8 */
  1132.             x = u_to_b(ch);    /* So translate to remote byte */
  1133.             if (x < 0)
  1134.               continue;
  1135.             outxbuf[0] = (unsigned)(x & 0xff);
  1136.             outxcount = 1;
  1137.             outxbuf[outxcount] = NUL;
  1138.             } else {
  1139. #endif /* UNICODE */
  1140.             if (sxo) c = (*sxo)((char)c); /* Local-intermediate */
  1141.             if (rxo) c = (*rxo)((char)c); /* Intermediate-remote */
  1142.             outxbuf[0] = c;
  1143.             outxcount = 1;
  1144.             outxbuf[outxcount] = NUL;
  1145. #ifdef UNICODE
  1146.             }
  1147. #endif /* UNICODE */
  1148.         } else {
  1149.             outxbuf[0] = c;
  1150.             outxcount = 1;
  1151.             outxbuf[outxcount] = NUL;
  1152.         }
  1153.         if (escseq)
  1154.           apcrc = chkaes((char)c,1);
  1155. #else  /* NOCSETS */
  1156.         outxbuf[0] = c;
  1157.         outxcount = 1;
  1158.         outxbuf[outxcount] = NUL;
  1159. #endif /* NOCSETS */
  1160.  
  1161.         for (i = 0; i < outxcount; i++) {
  1162.             c = outxbuf[i];
  1163. /*
  1164.   If Shift-In/Shift-Out is selected and we have a 7-bit connection,
  1165.   handle shifting here.
  1166. */
  1167.             if (sosi) {             /* Shift-In/Out selected? */
  1168.             if (cmask == 0177) { /* In 7-bit environment? */
  1169.                 if (c & 0200) { /* 8-bit character? */
  1170.                 if (outshift == 0) { /* If not shifted, */
  1171.                     if (ttoc(dopar(SO)) < 0) { /* shift. */
  1172.                     ckcputf();
  1173.                     cx_status = CSX_INTERNAL;
  1174.                     active = 0;
  1175.                     continue;
  1176.                     }
  1177.                     outshift = 1;
  1178.                 }
  1179.                 } else {
  1180.                 if (outshift == 1) { /* 7-bit character */
  1181.                     if (ttoc(dopar(SI)) < 0) {
  1182.                     ckcputf();
  1183.                     cx_status = CSX_INTERNAL;
  1184.                     active = 0;
  1185.                     continue;
  1186.                     }
  1187.                     outshift = 0; /* unshift. */
  1188.                 }
  1189.                 }
  1190.             }
  1191.             if (c == SO) outshift = 1; /* User typed SO */
  1192.             if (c == SI) outshift = 0; /* User typed SI */
  1193.             }
  1194.             c &= cmask;        /* Apply Kermit-to-host mask now. */
  1195.             if (c == '\015') {    /* Carriage Return */
  1196.             int stuff = -1;
  1197.             if (tnlm) {    /* TERMINAL NEWLINE ON */
  1198.                 stuff = LF;    /* Stuff LF */
  1199. #ifdef TNCODE
  1200.             } else if (network &&    /* TELNET NEWLINE ON/OFF/RAW */
  1201.                    (ttnproto == NP_TELNET)) {
  1202.                 switch(!TELOPT_ME(TELOPT_BINARY) ?
  1203.                    tn_nlm :
  1204.                    tn_b_nlm
  1205.                    ) {
  1206.                   case TNL_CRLF:
  1207.                 stuff = LF;
  1208.                 break;
  1209.                   case TNL_CRNUL:
  1210.                 stuff = NUL;
  1211.                 break;
  1212.                 }
  1213. #endif /* TNCODE */
  1214.             }
  1215.             if (stuff > -1) {
  1216.                 ttoc(dopar('\015')); /* Send CR */
  1217.                 if (duplex) conoc('\015'); /* Maybe echo CR */
  1218.                 c = stuff;    /* Char to stuff */
  1219.                 csave = c;
  1220.             }
  1221.             }
  1222. #ifdef TNCODE
  1223. /* If user types the 0xff character (TELNET IAC), it must be doubled. */
  1224.             else
  1225.               if (dopar(c) == IAC &&
  1226.               network &&
  1227.               ttnproto == NP_TELNET
  1228.               ) {        /* Send one copy now */
  1229.               ttoc(IAC);    /* and the other one just below. */
  1230.               }
  1231. #endif /* TNCODE */
  1232.             if (ttoc(dopar(c)) < 0) { /* Now send the character. */
  1233.             ckcputf();
  1234.             cx_status = CSX_INTERNAL;
  1235.             active = 0;
  1236.             continue;
  1237.             }
  1238. #ifdef CKLEARN
  1239.             if (learning) {    /* Learned script active */
  1240.             if (crflag) {
  1241.                 learnchar(CR);
  1242.                 learnst = 0;
  1243.             } else {
  1244.                 learnchar(c);
  1245.                 learnst = 2; /* Change state to keyboard */
  1246.             }
  1247.             }
  1248. #endif /* CKLEARN */
  1249.             if (duplex) {    /* Half duplex? */
  1250.             if (debses)    /* Yes, echo locally */
  1251.               conol(dbchr(csave)); /* in appropriate mode */
  1252.             else
  1253.               conoc(csave);
  1254.             if (seslog) logchar(c); /* And maybe log it. */
  1255.             }
  1256.         } /* for... */
  1257.         }
  1258.     } else {
  1259. /*
  1260.   Character from communications device or network connection...
  1261. */
  1262. #ifdef TNCODE
  1263.         /* Handle telnet options */
  1264.         if (network && ttnproto == NP_TELNET && ((c & 0xff) == IAC)) {
  1265.         ckcputf();        /* Dump output buffer */
  1266.         if ((x = tn_doop(c & 0xff, duplex, ckcgetc)) == -1 && msgflg)
  1267.           printf("\r\nCommunications disconnect ");
  1268.         if (x == -2 && msgflg)
  1269.           printf("\r\nConnection closed by peer ");
  1270.         if (x == -3 && msgflg)
  1271.           printf("\r\nConnection closed due to Telnet policy ");
  1272.         if (x == 1) duplex = 1;    /* Change duplex if necessary. */
  1273.         if (x == 2) duplex = 0;
  1274.         if (x == 3)        /* Quoted IAC */
  1275.           c = parity ? 127 : 255;
  1276. #ifdef IKS_OPTION
  1277.                 else if (x == 4) {    /* IKS State Change */
  1278.                     if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start &&
  1279.             !tcp_incoming
  1280.             ) {
  1281.             cx_status = CSX_IKSD;
  1282.             active = 0;
  1283.             break;
  1284.                     }
  1285.                 }
  1286. #endif /* IKS_OPTION */
  1287.         else
  1288.           continue;
  1289.         } else if (parity)        /* NOW strip parity */
  1290.           c &= 0x7f;
  1291.  
  1292. #ifdef CKLEARN
  1293.         if (learning && learnst != 2) {
  1294.         learnbuf[learnbp++] = c;
  1295.         if (learnbp >= LEARNBUFSIZ)
  1296.           learnbp = 0;
  1297.         learnbc++;
  1298.         learnst = 1;
  1299.         }
  1300. #endif /* CKLEARN */
  1301.  
  1302. #endif /* TNCODE */
  1303.         if (debses) {        /* Output character to screen */
  1304.         char *s;        /* Debugging display... */
  1305.         s = dbchr(c);
  1306.         while (*s)
  1307.           ckcputc(*s++);
  1308.         } else {            /* or regular... */
  1309.         int k;
  1310.         c &= cmask;        /* Do first masking */
  1311. #ifdef CK_APC
  1312.         if (autodl && (k = kstart((CHAR)c))) { /* Saw S/I packet? */
  1313.             extern CHAR mystch, seol;
  1314.             extern int justone;
  1315.             CHAR buf[6];
  1316.             int ksign = 0;
  1317.             if (k < 0) {
  1318.             ksign = 1;
  1319.             k = 0 - k;
  1320.             justone = 1;
  1321.             } else {
  1322.             justone = 0;
  1323.             }
  1324.             if (k == PROTO_K
  1325. #ifdef CK_XYZ
  1326.             || k == PROTO_Z
  1327. #endif /* CK_XYZ */
  1328.             ) {
  1329.             /* Damage the packet so that it doesn't trigger */
  1330.             /* autodownload detection downstream. */
  1331.             if (k == PROTO_K) {
  1332.                 extern CHAR ksbuf[];
  1333.                 int i, len = strlen((char*)ksbuf);
  1334.                 for (i = 0; i < len; i++)
  1335.                   ckcputc(BS);
  1336.             }
  1337. #ifdef CK_XYZ
  1338.             else {
  1339.                 int i;
  1340.                 for (i = 0; i < 3; i++)
  1341.                   ckcputc(CAN);
  1342.             }
  1343. #endif /* CK_XYZ */
  1344.             }
  1345.             switch (protocol) { /* Stuff RECEIVE into APC buffer */
  1346.               case PROTO_K:
  1347.             strcpy(apcbuf, ksign ? "server" : "receive");
  1348.             break;
  1349. #ifdef CK_XYZ
  1350.               case PROTO_G:
  1351.             strcpy(apcbuf,
  1352.                    "set proto kermit, receive, set proto g");
  1353.             break;
  1354.               case PROTO_X:
  1355.             strcpy(apcbuf,
  1356.                    "set proto kermit, receive, set proto x");
  1357.             break;
  1358.               case PROTO_Y:
  1359.             strcpy(apcbuf,
  1360.                    "set proto kermit, receive, set proto y");
  1361.             break;
  1362.               case PROTO_Z:
  1363.             strcpy(apcbuf,
  1364.                    "set proto kermit, receive, set proto z");
  1365.             break;
  1366. #endif /* CK_XYZ */
  1367.             }
  1368.             apclength = strlen(apcbuf) ;
  1369.             debug(F110,"autodownload",apcbuf,0);
  1370.             apcactive = APC_LOCAL;
  1371.             ckcputf();        /* flush screen-output buffer and... */
  1372. #ifdef COMMENT
  1373.             printf("\r\n");    /* prevent CR-overstriking and...    */
  1374. #endif /* COMMENT */
  1375.             cx_status = CSX_APC;
  1376.             active = 0;        /* exit from the terminal emulator.. */
  1377.             break;
  1378.         }
  1379. #endif /* CK_APC */
  1380.         if (sosi) {        /* Handle SI/SO */
  1381.             if (c == SO) {    /* Shift Out */
  1382.             inshift = 1;
  1383.             continue;
  1384.             } else if (c == SI) { /* Shift In */
  1385.             inshift = 0;
  1386.             continue;
  1387.             }
  1388.             if (inshift) c |= 0200;
  1389.         }
  1390.         inxbuf[0] = c;        /* In case there is no translation */
  1391.         inxcount = 1;
  1392. #ifndef NOCSETS
  1393.         if (inesc[0] == ES_NORMAL) { /* If not in an escape sequence */
  1394. #ifdef UNICODE
  1395.             if (unicode == 1) {    /* Remote is UTF-8 */
  1396.             int x;
  1397.             x = u_to_b((CHAR)c);
  1398.             if (x == -1)
  1399.               continue;
  1400.             else if (x == -2) { /* LS or PS */
  1401.                 inxbuf[0] = CR;
  1402.                 inxbuf[1] = LF;
  1403.                 inxcount = 2;
  1404.             } else {
  1405.                 inxbuf[0] = (unsigned)(x & 0xff);
  1406.             }
  1407.             c = inxbuf[0];
  1408.             } else if (unicode == 2) { /* Local is UTF-8 */
  1409.             inxcount = b_to_u((CHAR)c,inxbuf,OUTXBUFSIZ,tcssize);
  1410.             c = inxbuf[0];
  1411.             } else {
  1412. #endif /* UNICODE */
  1413.             if (sxi) c = (*sxi)((CHAR)c);
  1414.             if (rxi) c = (*rxi)((CHAR)c);
  1415.             inxbuf[0] = c;
  1416. #ifdef UNICODE
  1417.             }
  1418. #endif /* UNICODE */
  1419.         }
  1420. #endif /* NOCSETS */
  1421.  
  1422. #ifndef NOESCSEQ
  1423.         if (escseq)        /* If handling escape sequences */
  1424.           chkaes((char)c,0);    /* update our state */
  1425. #ifdef CK_APC
  1426. /*
  1427.   If we are handling APCs, we have several possibilities at this point:
  1428.    1. Ordinary character to be written to the screen.
  1429.    2. An Esc; we can't write it because it might be the beginning of an APC.
  1430.    3. The character following an Esc, in which case we write Esc, then char,
  1431.       but only if we have not just entered an APC sequence.
  1432. */
  1433.         if (escseq && apcstatus != APC_OFF) {
  1434.             if (inesc[0] == ES_GOTESC) { /* Don't write ESC yet */
  1435.             debug(F100,"XXX SKIPPING ESC","",0);
  1436.             continue;
  1437.             } else if (oldesc[0] == ES_GOTESC && !apcactive) {
  1438.             debug(F100,"XXX WRITING ESC","",0);
  1439.             ckcputc(ESC);    /* Write saved ESC */
  1440.             if (seslog) logchar(ESC); /* And maybe log it. */
  1441.             }
  1442.         }
  1443. #endif /* CK_APC */
  1444. #endif /* NOESCSEQ */
  1445.  
  1446.         for (i = 0; i < inxcount; i++) { /* Loop thru */
  1447.             c = inxbuf[i];    /* input expansion buffer... */
  1448.             if (
  1449. #ifdef CK_APC
  1450.             !apcactive
  1451. #else
  1452.             1
  1453. #endif /* CK_APC */
  1454.             ) {
  1455.             c &= cmdmsk;    /* Apply mask */
  1456.             if (c == CR && tt_crd) { /* SET TERM CR-DISPLA CRLF? */
  1457.                 ckcputc(c);    /* Yes, output CR */
  1458.                 if (seslog) logchar(c);
  1459.                 c = LF;    /* and insert a linefeed */
  1460.             }
  1461.             ckcputc(c);    /* Put it on the screen. */
  1462.             }
  1463.             if (seslog) logchar(c); /* If logging, log it. */
  1464. #ifdef CK_TRIGGER
  1465.             /* Check for trigger string */
  1466.             if (tt_trigger[0]) if ((ix = autoexitchk((CHAR)c)) > -1) {
  1467.             int n;
  1468.             ckcputf();    /* Flush screen-output buffer */
  1469.             if (triggerval)    /* Make a copy of the trigger */
  1470.               free(triggerval);
  1471.             triggerval = NULL;
  1472.             n = strlen(tt_trigger[ix]);
  1473.             if (triggerval = (char *)malloc(n+1))
  1474.               strcpy(triggerval,tt_trigger[ix]);
  1475.             debug(F110,"CONNECT triggerval",triggerval,0);
  1476.             cx_status = CSX_TRIGGER;
  1477.             active = 0;
  1478.             }
  1479. #endif /* CK_TRIGGER */
  1480.         } /* for... */
  1481.         }
  1482.     }
  1483.     } while (active);
  1484.     rc = 1;
  1485.  
  1486.   conret0:
  1487.     ck_cancio();
  1488.     conres();
  1489.  
  1490. #ifdef CKLEARN
  1491.     if (learning && learnfp)
  1492.       fputs("\n",learnfp);
  1493. #endif /* CKLEARN */
  1494.  
  1495.     if (msgflg
  1496. #ifdef CK_APC
  1497.     && !apcactive
  1498. #endif /* CK_APC */
  1499.     )
  1500.       printf("(Back at %s)", *myhost ? myhost : "local VMS system");
  1501.     what = W_NOTHING;
  1502. #ifdef CK_APC
  1503.     if (!apcactive)
  1504. #endif /* CK_APC */
  1505.       printf("\n");
  1506. #ifndef NOCSETS
  1507.     language = langsv;            /* Restore language */
  1508. #endif /* NOCSETS */
  1509.     if (msgflg) {
  1510. #ifdef CK_APC
  1511.     if (apcactive == APC_LOCAL)
  1512.       printf("\n");
  1513. #endif /* CK_APC */
  1514.     printf("----------------------------------------------------\r\n");
  1515.     }
  1516.     return(rc);
  1517. }
  1518.  
  1519. /*  H C O N N E  --  Give help message for connect.  */
  1520.  
  1521. VOID
  1522. hconne() {
  1523.     int c;
  1524.     static char *hlpmsg[] = {
  1525. "\n",
  1526. "  C to return to C-Kermit prompt,   U to hangup and close the connection,\n",
  1527. "  B to send a BREAK,                L to send a Long BREAK,\n",
  1528. #ifdef TNCODE
  1529. "  A to send TELNET Are You There,   I to send TELNET Interrupt,\n",
  1530. #endif /* TNCODE */
  1531. "  0 (zero) to send a null,          X to send an XON,\n",
  1532. #ifdef NOPUSH
  1533. "  S for status of connection,       ? for this message, or:\n",
  1534. #else
  1535. "  @ to enter DCL,                   S for status of connection,\n",
  1536. "  ? for this message, or:\n",
  1537. #endif /* NOPUSH */
  1538. "  \\ to begin a backslash escape:\n",
  1539. "    \\nnn  (decimal character code)\n",
  1540. "    \\Onnn (octal character code)\n",
  1541. "    \\Xhh  (hexadecimal character code)\n",
  1542. "    Terminate with carriage return.\n\n",
  1543. " Type the escape character again to send the escape character, or\n",
  1544. " press the space-bar to resume the CONNECT command.\n",
  1545. "----------------------------------------------------\n\n",
  1546. "" };
  1547. /*
  1548.   Need to save term characteristics/ allow disable binary mode
  1549.   print message, get text and then restore previous state.
  1550. */
  1551.     conoll("\r\n----------------------------------------------------");
  1552.     conol("\r\nPress C to return to ");
  1553.     conol(*myhost ? myhost : "the C-Kermit prompt");
  1554.     conoll(", or:");
  1555.     conola(hlpmsg);            /* Print the help message. */
  1556.     conol("Command>");            /* Prompt for command. */
  1557.     c = coninc(0) & 0x7f;
  1558.     conoc(c);                /* Echo it. */
  1559.     if (c != CMDQ)
  1560.       conoll("");
  1561.     conoll("----------------------------------------------------");
  1562.     doesc(c);
  1563. }
  1564.  
  1565. /*  D O E S C  --  Process an escape character argument  */
  1566.  
  1567. VOID
  1568. #ifdef CK_ANSIC
  1569. doesc(register unsigned char c)
  1570. #else
  1571. doesc(c) register unsigned char c;
  1572. #endif /* CK_ANSIC */
  1573. /* doesc() */ {
  1574.     int d;
  1575.     char temp[1024];
  1576.  
  1577.     c &= 0177;                /* Mask off 8th bit */
  1578.  
  1579.     if (c == escape) {            /* If it's the escape character, */
  1580.         d = dopar(c);            /* just send it. */
  1581.         ttoc(d);
  1582.     return;
  1583.     }
  1584.     if (isupper(c)) c = tolower(c);    /* Convert to lowercase letter. */
  1585.     if (iscntrl(c)) c += 'a' - '\001';
  1586.  
  1587.     switch (c) {            /* Take requested action. */
  1588.       case 'b':
  1589. #ifdef CKLEARN
  1590.     learnchar(-7);
  1591. #endif /* CKLEARN */
  1592.     ttsndb();            /* Send a BREAK signal */
  1593.     break;
  1594. #ifdef TNCODE
  1595.       case 'i':                /* Send TELNET interrupt */
  1596. #ifndef IP
  1597. #define IP 244
  1598. #endif /* IP */
  1599.     if (network && ttnproto == NP_TELNET) { /* TELNET */
  1600.         CHAR temp[3];
  1601.         temp[0] = IAC;        /* I Am a Command */
  1602.         temp[1] = IP;        /* Interrupt Process */
  1603.         temp[2] = NUL;
  1604.         ttol((CHAR *)temp,2);
  1605.     } else conoc(BEL);
  1606.     break;
  1607.       case 'a':                /* "Are You There?" */
  1608.       case '\01':
  1609. #ifndef AYT
  1610. #define AYT 246
  1611. #endif /* AYT */
  1612.     if (network && ttnproto == NP_TELNET) {
  1613.         CHAR temp[3];
  1614.         temp[0] = IAC;        /* I Am a Command */
  1615.         temp[1] = AYT;        /* Are You There? */
  1616.         temp[2] = NUL;
  1617.         ttol((CHAR *)temp,2);
  1618.     } else conoc(BEL);
  1619. #endif /* TNCODE */
  1620.     break;
  1621.       case 'c':                /* Return to prompt */
  1622.     cx_status = CSX_ESCAPE;
  1623.     active = 0;
  1624.     conol("\r\n");
  1625.     break;
  1626.       case 'u':                /* Hang up the connection */
  1627.     cx_status = CSX_USERDISC;
  1628.     if (network)
  1629.       netclos();
  1630. #ifndef NODIAL
  1631.     else if (mdmhup() < 1)        /* Try via modem first, otherwise */
  1632.       tthang();            /* the old-fashioned way. */
  1633. #endif /* NODIAL */
  1634.     conol("\r\nHanging up ");
  1635.     break;
  1636.       case 'l':                /* Send a Long BREAK signal */
  1637. #ifdef CKLEARN
  1638.     learnchar(-8);
  1639. #endif /* CKLEARN */
  1640.     ttsndlb();
  1641.     break;
  1642.       case 's':                /* Status */
  1643.     conoll("");
  1644.     conoll("----------------------------------------------------");
  1645. #ifdef NETCMD
  1646.     if (ttpipe)
  1647.       sprintf(temp, " Pipe: \"%s\"", ttname);
  1648.     else
  1649. #endif /* NETCMD */
  1650.       sprintf(temp, " %s: %s", (network ? "Host" : "Device"), ttname);
  1651.     conoll(temp);
  1652.     if (!network && speed >= 0L) {
  1653.         sprintf(temp,"Speed %ld", speed);
  1654.         conoll(temp);
  1655.     }
  1656.     sprintf(temp," Terminal echo: %s", duplex ? "local" : "remote");
  1657.     conoll(temp);
  1658.     sprintf(temp," Terminal bytesize: %d", (cmask  == 0177) ? 7 : 8);
  1659.     conoll(temp);
  1660.     sprintf(temp," Command bytesize: %d", (cmdmsk == 0177) ? 7 : 8 );
  1661.     conoll(temp);
  1662.     sprintf(temp," Parity: %s", parnam(parity));
  1663.     conoll(temp);
  1664. #ifdef CK_APC
  1665.     sprintf(temp," Autodownload: %s", autodl ? "on" : "off");
  1666.     conoll(temp);
  1667. #endif /* CK_APC */
  1668.     sprintf(temp," Session log: %s", *sesfil ? sesfil : "(none)");
  1669.     conoll(temp);
  1670. #ifndef NOSHOW
  1671.         if (!network) shomdm();
  1672. #endif /* NOSHOW */
  1673. #ifdef CKLOGDIAL
  1674.         {
  1675.         long z;
  1676.         z = dologshow(0);
  1677.         if (z > -1L) {
  1678.             sprintf(temp," Elapsed time: %s",hhmmss(z));
  1679.             conoll(temp);
  1680.         }
  1681.         }
  1682. #endif /* CKLOGDIAL */
  1683.         conoll("----------------------------------------------------");
  1684.         return;
  1685.       case '!':
  1686.       case '@':
  1687. #ifndef NOPUSH
  1688.     if (!nopush) {
  1689.         conres();            /* Put console back to normal */
  1690.         zshcmd("");            /* Start DCL. */
  1691.         if (conbin(escape) < 0) {
  1692.         printf("Error returning to remote session\n");
  1693.         active = 0;
  1694.         }
  1695.     } else
  1696.       conoc(BEL);
  1697. #else
  1698.     conoc(BEL);
  1699. #endif /* NOPUSH */
  1700.     return;
  1701.  
  1702.       case 'x':                /* XON */
  1703. /*
  1704.   NOTE: Here we should also issue QIO's to clear XOFF deadlock.
  1705. */
  1706.     ttoc(dopar(XON));
  1707.     break;
  1708.       case 'h':                /* Help */
  1709.       case '?':                /* Give Help */
  1710.     hconne();
  1711.     break;
  1712.       case '0':                /* Send a NULL */
  1713.     c = '\0';
  1714.     d = dopar(c);
  1715.     ttoc(d);
  1716.     break;
  1717.       case SP:                /* Ignore space */
  1718.     break;
  1719.       default:
  1720.     if (c == CMDQ) {        /* Backslash escape */
  1721.         int x;
  1722.         kbp = kbuf;
  1723.         *kbp++ = c;
  1724.         while (((c = (coninc(0) & cmdmsk)) != '\r') && (c != '\n'))
  1725.           *kbp++ = c;
  1726.         *kbp = NUL; kbp = kbuf;
  1727.         x = xxesc(&kbp);
  1728.         if (x >= 0) {
  1729.         c = dopar(x);
  1730.         ttoc(c);
  1731.         return;
  1732.         } else {
  1733.         conoc(BEL);
  1734.         return;
  1735.         }
  1736.     }
  1737.     conoc(BEL); return;        /* Invalid esc arg, beep */
  1738.     }
  1739. }
  1740.