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