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