home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv196.zip / ckucns.c < prev    next >
C/C++ Source or Header  |  1999-12-30  |  70KB  |  2,275 lines

  1. #include "ckcsym.h"
  2. #ifdef NOLOCAL
  3. char *connv = "";
  4. #else
  5. char *connv = "CONNECT Command for UNIX:select(), 7.0.110, 30 Dec 1999";
  6.  
  7. /*  C K U C N S  --  Terminal connection to remote system, for UNIX  */
  8. /*
  9.   Author: Frank da Cruz <fdc@columbia.edu>,
  10.   Columbia University Academic Information Systems, New York City.
  11.  
  12.   Copyright (C) 1985, 2000,
  13.     Trustees of Columbia University in the City of New York.
  14.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  15.     copyright text in the ckcmai.c module for disclaimer and permissions.
  16. */
  17.  
  18. /*
  19.   This version of the UNIX CONNECT module uses select(), which is required for
  20.   Kerberos encryption.  Thus it can be used only on UNIX systems that support
  21.   select() on both TCP/IP and serial connections.  A separate module that uses
  22.   a completely portable fork() structure can be used on systems where select()
  23.   is not available or does not work as required.
  24. */
  25.  
  26. #include "ckcdeb.h"            /* Common things first */
  27. #include <errno.h>            /* Error numbers */
  28. #ifdef __linux__
  29. #include <sys/time.h>            /* For FD_blah */
  30. #endif /* __linux__ */
  31.  
  32. /* Kermit-specific includes */
  33.  
  34. #include "ckcasc.h"            /* ASCII characters */
  35. #include "ckcker.h"            /* Kermit things */
  36. #include "ckucmd.h"            /* For xxesc() prototype */
  37. #include "ckcnet.h"            /* Network symbols */
  38. #ifndef NOCSETS
  39. #include "ckcxla.h"            /* Character set translation */
  40. #endif /* NOCSETS */
  41.  
  42. #ifdef BEBOX
  43. #include <kernel/OS.h>
  44. #include <socket.h>
  45. #include <stdio.h>
  46. #endif /* BEBOX */
  47.  
  48. #include <signal.h>
  49.  
  50. #ifndef SCO_OSR504
  51. #ifdef SELECT_H
  52. #include <sys/select.h>
  53. #endif /* SELECT_H */
  54. #endif /* SCO_OSR504 */
  55.  
  56. /* Internal function prototypes */
  57.  
  58. _PROTOTYP( VOID ttflux, (void) );
  59. _PROTOTYP( VOID doesc, (char) );
  60. _PROTOTYP( int hconne, (void) );
  61. #ifndef NOSHOW
  62. _PROTOTYP( VOID shomdm, (void) );
  63. #endif /* NOSHOW */
  64. _PROTOTYP( static int kbget, (void) );
  65. _PROTOTYP( static int ckcputf, (void) );
  66.  
  67. /* External variables */
  68.  
  69. extern struct ck_p ptab[];
  70.  
  71. extern int local, escape, duplex, parity, flow, seslog, sessft, debses,
  72.  mdmtyp, ttnproto, cmask, cmdmsk, network, nettype, sosi, tnlm,
  73.  xitsta, what, ttyfd, ttpipe, quiet, backgrd, pflag, tt_crd, tn_nlm, ttfdflg,
  74.  tt_escape, justone, carrier, ttpty;
  75.  
  76. extern long speed;
  77. extern char ttname[], sesfil[], myhost[], *ccntab[];
  78. #ifdef TNCODE
  79. extern int tn_b_nlm, tn_rem_echo;
  80. #endif /* TNCODE */
  81.  
  82. #ifdef CK_TRIGGER
  83. extern char * tt_trigger[], * triggerval;
  84. #endif /* CK_TRIGGER */
  85.  
  86. extern int nopush;
  87.  
  88. #ifdef CK_APC
  89. extern int apcactive;            /* Application Program Command (APC) */
  90. extern int apcstatus;            /* items ... */
  91. static int apclength = 0;
  92. #ifdef DCMDBUF
  93. extern char *apcbuf;
  94. #else
  95. extern char apcbuf[];
  96. #endif /* DCMDBUF */
  97. static int apcbuflen = APCBUFLEN - 2;
  98. extern int protocol;
  99. #endif /* CK_APC */
  100. #ifndef NOXFER
  101. extern int autodl;            /* Auto download */
  102. #endif /* NOXFER */
  103.  
  104. #ifdef CK_AUTODL
  105. extern CHAR ksbuf[];
  106. #endif /* CK_AUTODL */
  107.  
  108. #ifdef CK_ENCRYPTION
  109. extern int me_auth;
  110. #endif /* CK_ENCRYPTION */
  111.  
  112. #ifdef CK_XYZ
  113. #ifdef XYZ_INTERNAL
  114. static int zmdlok = 1;            /* Zmodem autodownloads available */
  115. #else
  116. static int zmdlok = 0;            /* Depends on external protocol def */
  117. #endif /* XYZ_INTERNAL */
  118. #else
  119. static int zmdlok = 0;            /* Not available at all */
  120. #endif /* CK_XYZ */
  121.  
  122. #ifndef NOSETKEY            /* Keyboard mapping */
  123. extern KEY *keymap;            /* Single-character key map */
  124. extern MACRO *macrotab;            /* Key macro pointer table */
  125. static MACRO kmptr = NULL;        /* Pointer to current key macro */
  126. #endif /* NOSETKEY */
  127.  
  128. /* Global variables local to this module */
  129.  
  130. static int
  131.   active = 0,
  132.   quitnow = 0,                /* <esc-char>Q was typed */
  133.   dohangup = 0,                /* <esc-char>H was typed */
  134.   inshift = 0,                /* SO/SI shift states */
  135.   outshift = 0;
  136.  
  137. static char ecbuf[10], *ecbp;        /* Escape char buffer & pointer */
  138.  
  139. #ifdef CK_SMALL
  140. #define IBUFL 1536            /* Input buffer length */
  141. #else
  142. #define IBUFL 4096
  143. #endif /* CK_SMALL */
  144.  
  145. static int obc = 0;            /* Output buffer count */
  146.  
  147. #ifndef OXOS
  148. #define OBUFL 1024            /* Output buffer length */
  149. #else
  150. #define OBUFL IBUFL
  151. #endif /* OXOS */
  152.  
  153. #ifdef BIGBUFOK
  154. #define TMPLEN 4096            /* Temporary message buffer length */
  155. #else
  156. #define TMPLEN 200
  157. #endif /* BIGBUFOK */
  158.  
  159. #ifdef DYNAMIC
  160. static char *ibuf = NULL, *obuf = NULL, *temp = NULL; /* Buffers */
  161. #else
  162. static char ibuf[IBUFL], obuf[OBUFL], temp[TMPLEN];
  163. #endif /* DYNAMIC */
  164.  
  165. #ifdef DYNAMIC
  166. static char *ibp;            /* Input buffer pointer */
  167. #else
  168. static char *ibp = ibuf;        /* Input buffer pointer */
  169. #endif /*DYNAMIC */
  170. static int ibc = 0;            /* Input buffer count */
  171.  
  172. #ifdef DYNAMIC
  173. static char *obp;            /* Output buffer pointer */
  174. #else
  175. static char *obp = obuf;        /* Output buffer pointer */
  176. #endif /* DYNAMIC */
  177.  
  178. /* Character-set items */
  179.  
  180. static int unicode = 0;
  181.  
  182. #ifndef NOCSETS
  183. #ifdef CK_ANSIC /* ANSI C prototypes... */
  184. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Character set */
  185. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* translation functions */
  186. static CHAR (*sxo)(CHAR);    /* Local translation functions */
  187. static CHAR (*rxo)(CHAR);    /* for output (sending) terminal chars */
  188. static CHAR (*sxi)(CHAR);    /* and for input (receiving) terminal chars. */
  189. static CHAR (*rxi)(CHAR);
  190. #else /* Not ANSI C... */
  191. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])();    /* Character set */
  192. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])();    /* translation functions. */
  193. static CHAR (*sxo)();        /* Local translation functions */
  194. static CHAR (*rxo)();        /* for output (sending) terminal chars */
  195. static CHAR (*sxi)();        /* and for input (receiving) terminal chars. */
  196. static CHAR (*rxi)();
  197. #endif /* CK_ANSIC */
  198. extern int language;        /* Current language. */
  199. static int langsv;        /* For remembering language setting. */
  200. extern struct csinfo fcsinfo[]; /* File character set info. */
  201. extern int tcsr, tcsl;        /* Terminal character sets, remote & local. */
  202. static int tcs;            /* Intermediate ("transfer") character set. */
  203. static int tcssize = 0;        /* Size of tcs */
  204. #ifdef UNICODE                /* UTF-8 support */
  205. #ifdef CK_ANSIC
  206. extern int (*xl_ufc[MAXFCSETS+1])(USHORT);  /* Unicode to FCS */
  207. extern USHORT (*xl_fcu[MAXFCSETS+1])(CHAR); /* FCS to Unicode */
  208. extern int (*xuf)(USHORT);        /* Translation function UCS to FCS */
  209. extern USHORT (*xfu)(CHAR);        /* Translation function FCS to UCS */
  210. #else
  211. extern int (*xl_ufc[MAXFCSETS+1])();
  212. extern USHORT (*xl_fcu[MAXFCSETS+1])();
  213. extern int (*xuf)();
  214. extern USHORT (*xfu)();
  215. #endif /* CK_ANSIC */
  216. #endif /* UNICODE */
  217. #endif /* NOCSETS */
  218.  
  219. static int printing = 0;
  220.  
  221. /*
  222.   We do not need to parse and recognize escape sequences if we are being built
  223.   without character-set support AND without APC support.
  224. */
  225. #ifdef NOESCSEQ
  226. #ifdef XPRINT
  227. #undef XPRINT
  228. #endif /* XPRINT */
  229.  
  230. #else  /* NOESCSEQ not defined from outside */
  231.  
  232. #ifdef NOCSETS                /* No character sets */
  233. #ifndef CK_APC                /* No APC */
  234. #ifndef XPRINT                /* No transparent printing */
  235. #define NOESCSEQ            /* So no escape sequence recognizer */
  236. #endif /* XPRINT */
  237. #endif /* CK_APC */
  238. #endif /* NOCSETS */
  239. #endif /* NOESCSEQ */
  240.  
  241. static int escseq = 0;            /* 1 = Recognizer is active */
  242. static int inesc[2] = { 0, 0 };        /* State of sequence recognizer */
  243. static int oldesc[2] = { -1, -1 };    /* Previous state of recognizer */
  244.  
  245. #ifdef NOESCSEQ
  246. #define chkaes(x,y) 0
  247. #else
  248. /*
  249.   As of edit 178, the CONNECT command skips past ANSI escape sequences to
  250.   avoid translating the characters within them.  This allows the CONNECT
  251.   command to work correctly with a host that uses a 7-bit ISO 646 national
  252.   character set, in which characters like '[' would normally be translated
  253.   into accented characters, ruining the terminal's interpretation (and
  254.   generation) of escape sequences.
  255.  
  256.   As of edit 190, the CONNECT command responds to APC escape sequences
  257.   (ESC _ text ESC \) if the user SETs TERMINAL APC ON or UNCHECKED, and the
  258.   program was built with CK_APC defined.
  259.  
  260.   Non-ANSI/ISO-compliant escape sequences are not handled.
  261. */
  262.  
  263. /* States for the escape-sequence recognizer. */
  264.  
  265. #define ES_NORMAL 0            /* Normal, not in an escape sequence */
  266. #define ES_GOTESC 1            /* Current character is ESC */
  267. #define ES_ESCSEQ 2            /* Inside an escape sequence */
  268. #define ES_GOTCSI 3            /* Inside a control sequence */
  269. #define ES_STRING 4            /* Inside DCS,OSC,PM, or APC string */
  270. #define ES_TERMIN 5            /* 1st char of string terminator */
  271.  
  272. /*
  273.   ANSI escape sequence handling.  Only the 7-bit form is treated, because
  274.   translation is not a problem in the 8-bit environment, in which all GL
  275.   characters are ASCII and no translation takes place.  So we don't check
  276.   for the 8-bit single-character versions of CSI, DCS, OSC, APC, or ST.
  277.   Here is the ANSI sequence recognizer state table, followed by the code
  278.   that implements it.
  279.  
  280.   Definitions:
  281.     CAN = Cancel                       01/08         Ctrl-X
  282.     SUB = Substitute                   01/10         Ctrl-Z
  283.     DCS = Device Control Sequence      01/11 05/00   ESC P
  284.     CSI = Control Sequence Introducer  01/11 05/11   ESC [
  285.     ST  = String Terminator            01/11 05/12   ESC \
  286.     OSC = Operating System Command     01/11 05/13   ESC ]
  287.     PM  = Privacy Message              01/11 05/14   ESC ^
  288.     APC = Application Program Command  01/11 05/15   ESC _
  289.  
  290.   ANSI escape sequence recognizer:
  291.  
  292.     State    Input  New State  ; Commentary
  293.  
  294.     NORMAL   (start)           ; Start in NORMAL state
  295.  
  296.     (any)    CAN    NORMAL     ; ^X cancels
  297.     (any)    SUB    NORMAL     ; ^Z cancels
  298.  
  299.     NORMAL   ESC    GOTESC     ; Begin escape sequence
  300.     NORMAL   other             ; NORMAL control or graphic character
  301.  
  302.     GOTESC   ESC               ; Start again
  303.     GOTESC   [      GOTCSI     ; CSI
  304.     GOTESC   P      STRING     ; DCS introducer, consume through ST
  305.     GOTESC   ]      STRING     ; OSC introducer, consume through ST
  306.     GOTESC   ^      STRING     ; PM  introducer, consume through ST
  307.     GOTESC   _      STRING     ; APC introducer, consume through ST
  308.     GOTESC   0..~   NORMAL     ; 03/00 through 17/14 = Final character
  309.     GOTESC   other  ESCSEQ     ; Intermediate or ignored control character
  310.  
  311.     ESCSEQ   ESC    GOTESC     ; Start again
  312.     ESCSEQ   0..~   NORMAL     ; 03/00 through 17/14 = Final character
  313.     ESCSEQ   other             ; Intermediate or ignored control character
  314.  
  315.     GOTCSI   ESC    GOTESC     ; Start again
  316.     GOTCSI   @..~   NORMAL     ; 04/00 through 17/14 = Final character
  317.     GOTCSI   other             ; Intermediate char or ignored control char
  318.  
  319.     STRING   ESC    TERMIN     ; Maybe have ST
  320.     STRING   other             ; Consume all else
  321.  
  322.     TERMIN   \      NORMAL     ; End of string
  323.     TERMIN   other  STRING     ; Still in string
  324. */
  325.  
  326. #ifdef XPRINT                /* Transparent print support */
  327. /*
  328.   We can't just print each byte as it comes in because then the printer-off
  329.   sequence would be sent to the printer.  Thus we have to buffer up escape
  330.   sequences and print them only when they are complete AND we know they are
  331.   not the printer-off sequence.  All printing is done via zsoutx(ZMFILE,s,n).
  332.   This allows for strings that contain NULs.  Don't mix calls to zsoutx() with
  333.   calls to zchout(), or the output will be scrambled.  Also note that when
  334.   printing a saved-up escape sequence, we never print its final character
  335.   because that will be printed in the mainline code, upon return from
  336.   chkaes().  Note that the printer-on sequence is passed to the screen; this
  337.   is unavoidable, since we don't know what it is until after we get to the
  338.   end, and for screen display purposes we can't buffer up escape sequences
  339.   for numerous reasons.  Therefore we also must output the printer-off
  340.   sequence, otherwise a real terminal or emulator will be stuck in print mode.
  341. */
  342. extern int tt_print;
  343. #define ESCBUFLEN 63
  344. static char escbuf[ESCBUFLEN+1] = { NUL, NUL };
  345. static int escbufc = 0;
  346.  
  347. VOID
  348. printon() {                /* Turn printing on */
  349.     int x, pp;
  350.     char * p;
  351.     extern int printpipe, noprinter;
  352.     extern char * printername;
  353.  
  354.     if (noprinter) {
  355.     debug(F110,"PRINTER ON NOPRINTER","",0);
  356.     return;
  357.     }
  358.     p = printername;
  359.     pp = printpipe;
  360.     if (!p) p = "";
  361.     if (!*p) {
  362. #ifdef ANYBSD
  363.     p = "lpr";
  364. #else
  365.     p = "lp";
  366. #endif /* ANYBSD */
  367.     pp = 1;
  368.     debug(F110,"PRINTER DEFAULT",p,0);
  369.     }
  370.     debug(F111,"PRINTER ON",p,pp);
  371.     if (pp) {                /* Printing to pipe */
  372.     x = zxcmd(ZMFILE,p);
  373.     } else {                /* Append to file */
  374.     struct filinfo xx;
  375.     xx.bs = 0; xx.cs = 0; xx.rl = 0; xx.org = 0; xx.cc = 0;
  376.     xx.typ = 0; xx.dsp = XYFZ_A; xx.os_specific = NUL;
  377.     xx.lblopts = 0;
  378.     x = zopeno(ZMFILE,p,NULL,&xx);
  379.     }
  380.     debug(F101,"PRINTER OPEN","",x);
  381.     printing = 1;
  382. }
  383.  
  384. VOID
  385. printoff() {                /* Turn printing off */
  386.     int x;
  387.     extern int noprinter;
  388.     printing = 0;
  389.     if (noprinter) {
  390.     debug(F110,"PRINTER OFF NOPRINTER","",0);
  391.     return;
  392.     }
  393.     debug(F100,"PRINTER OFF","",0);
  394.     if (printing) {
  395.     x = zclose(ZMFILE);
  396.     debug(F101,"PRINTER CLOSE","",x);
  397.     }
  398. }
  399. #endif /* XPRINT */
  400.  
  401. /*
  402.   C H K A E S  --  Check ANSI Escape Sequence.
  403.  
  404.   Call with EACH character in input stream.
  405.   src = 0 means c is incoming from remote; 1 = char from keyboard.
  406.   Sets global inesc[src] variable according to escape sequence state.
  407.   Returns 0 normally, 1 if an APC sequence is to be executed.
  408.   Handles transparent printing internally.
  409. */
  410. int
  411. #ifdef CK_ANSIC
  412. chkaes(char c, int src)
  413. #else
  414. chkaes(c,src) char c; int src;
  415. #endif /* CK_ANSIC */
  416. /* chkaes */ {
  417.  
  418.     debug(F111,"chkaes entry inesc",ckitoa(src),inesc[src]);
  419.     debug(F101,"chkaes c","",c);
  420.  
  421.     if (src < 0 || src > 1)        /* Don't allow bad args. */
  422.       return(0);
  423.  
  424.     oldesc[src] = inesc[src];        /* Remember previous state */
  425.  
  426. #ifdef XPRINT
  427.     if (inesc[src] && !src) {        /* Save up escape seq for printing  */
  428.     if (!c) return(0);        /* Ignore NULs */
  429.     if (escbufc < ESCBUFLEN) {
  430.         escbuf[escbufc++] = c;
  431.         escbuf[escbufc] = NUL;
  432.         debug(F111,"ESCBUF 1",escbuf,escbufc);
  433.     } else {            /* Buffer overrun */
  434.         if (printing && escbufc)    /* Print what's there so far */
  435.           zsoutx(ZMFILE,escbuf,escbufc);
  436.         escbufc = 1;        /* clear it out */
  437.         escbuf[0] = c;        /* and start off fresh buffer */
  438.         escbuf[1] = NUL;        /* with this character. */
  439.     }
  440.     }
  441. #endif /* XPRINT */
  442.  
  443.     if (c == CAN || c == SUB) {        /* CAN and SUB cancel any sequence */
  444. #ifdef XPRINT
  445.     if (!src) {
  446.         if (printing && escbufc > 1)
  447.           zsoutx(ZMFILE,escbuf,escbufc-1);
  448.         escbufc = 0;        /* Clear buffer */
  449.         escbuf[0] = NUL;
  450.     }
  451. #endif /* XPRINT */
  452.     inesc[src] = ES_NORMAL;
  453.     } else                /* Otherwise */
  454.  
  455.       switch (inesc[src]) {        /* enter state switcher */
  456.     case ES_NORMAL:            /* NORMAL state */
  457.       if (c == ESC) {        /* Got an ESC */
  458.           inesc[src] = ES_GOTESC;    /* Change state to GOTESC */
  459. #ifdef XPRINT
  460.           if (!src) {
  461.           escbufc = 1;        /* Clear escape sequence buffer */
  462.           escbuf[0] = c;    /* and deposit the ESC */
  463.           escbuf[1] = NUL;
  464.           debug(F111,"ESCBUF 2",escbuf,escbufc);
  465.           }
  466. #endif /* XPRINT */
  467.       }
  468.       break;            /* Otherwise stay in NORMAL state */
  469.  
  470.     case ES_GOTESC:            /* GOTESC state - prev char was ESC*/
  471.       if (c == '[') {        /* Left bracket after ESC is CSI */
  472.           inesc[src] = ES_GOTCSI;    /* Change to GOTCSI state */
  473.       } else if (c == 'P' || (c > 0134 && c < 0140)) { /* P, ], ^, or _ */
  474.           inesc[src] = ES_STRING;    /* Switch to STRING-absorption state */
  475. #ifdef XPRINT
  476.           debug(F111,"ESCBUF STRING",escbuf,escbufc);
  477. #endif /* XPRINT */
  478. #ifdef CK_APC
  479.           /* If APC not disabled */
  480.           if (!src && c == '_' && apcstatus != APC_OFF) {
  481.           debug(F100,"CONNECT APC begin","",0);
  482.           apcactive = APC_REMOTE; /* Set APC-Active flag */
  483.           apclength = 0;    /* and reset APC buffer pointer */
  484.           }
  485. #endif /* CK_APC */
  486.       } else if (c > 057 && c < 0177) { /* Final character '0' thru '~' */
  487.           inesc[src] = ES_NORMAL;    /* Back to normal */
  488. #ifdef XPRINT
  489.           if (!src) {
  490.           if (printing & escbufc > 1) {
  491.               /* Dump esc seq buf to printer */
  492.               zsoutx(ZMFILE,escbuf,escbufc-1);
  493.               debug(F111,"ESCBUF PRINT 1",escbuf,escbufc);
  494.           }
  495.  
  496.           escbufc = 0;        /* Clear parameter buffer */
  497.           escbuf[0] = NUL;
  498.           }
  499. #endif /* XPRINT */
  500.       } else if (c != ESC) {    /* ESC in an escape sequence... */
  501.           inesc[src] = ES_ESCSEQ;    /* starts a new escape sequence */
  502.       }
  503.       break;            /* Intermediate or ignored ctrl char */
  504.  
  505.     case ES_ESCSEQ:            /* ESCSEQ -- in an escape sequence */
  506.       if (c > 057 && c < 0177) {    /* Final character '0' thru '~' */
  507.           inesc[src] = ES_NORMAL;    /* Return to NORMAL state. */
  508. #ifdef XPRINT
  509.           if (!src) {
  510.           if (printing && escbufc > 1) {
  511.               zsoutx(ZMFILE,escbuf,escbufc-1);
  512.               debug(F111,"ESCBUF PRINT 2",escbuf,escbufc);
  513.           }
  514.           escbufc = 0;        /* Clear escseq buffer */
  515.           escbuf[0] = NUL;
  516.           }
  517. #endif /* XPRINT */
  518.       } else if (c == ESC) {    /* ESC ... */
  519.           inesc[src] = ES_GOTESC;    /* starts a new escape sequence */
  520.       }
  521.       break;            /* Intermediate or ignored ctrl char */
  522.  
  523.     case ES_GOTCSI:            /* GOTCSI -- In a control sequence */
  524.       if (c > 077 && c < 0177) {    /* Final character '@' thru '~' */
  525. #ifdef XPRINT
  526.           if (!src && tt_print) {    /* Printer enabled? */
  527.           if (c == 'i') {    /* Final char is "i"? */
  528.               char * p = (char *) (escbuf + escbufc - 4);
  529.               if (!strncmp(p, "\033[5i", 4)) /* Yes, turn printer on */
  530.             printon();
  531.               else if (!strncmp(p, "\033[4i", 4)) { /* Or off... */
  532.               printoff();    /* Turn off printer. */
  533.               conxo(escbufc,escbuf); /* Display this sequence. */
  534.               } else if (printing && escbufc > 1) {
  535.               zsoutx(ZMFILE,escbuf,escbufc-1);
  536.               debug(F111,"ESCBUF PRINT 3",escbuf,escbufc);
  537.               }
  538.           } else if (printing && escbufc > 1) {
  539.               zsoutx(ZMFILE,escbuf,escbufc-1);
  540.               debug(F111,"ESCBUF PRINT 4",escbuf,escbufc);
  541.           }
  542.           }
  543.           if (!src) {
  544.           escbufc = 0;        /* Clear esc sequence buffer */
  545.           escbuf[0] = NUL;
  546.           }
  547. #endif /* XPRINT */
  548.           inesc[src] = ES_NORMAL;    /* Return to NORMAL. */
  549.       } else if (c == ESC) {    /* ESC ... */
  550.           inesc[src] = ES_GOTESC;    /* starts over. */
  551.       }
  552.       break;
  553.  
  554.     case ES_STRING:            /* Inside a string */
  555.       if (c == ESC)            /* ESC may be 1st char of terminator */
  556.         inesc[src] = ES_TERMIN;    /* Go see. */
  557. #ifdef CK_APC
  558.       else if (apcactive) {        /* If in APC */
  559.           if (apclength < apcbuflen) { /* and there is room... */
  560.           apcbuf[apclength++] = c; /* deposit this character. */
  561.           } else {            /* Buffer overrun */
  562.           apcactive = 0;    /* Discard what we got */
  563.           apclength = 0;    /* and go back to normal */
  564.           apcbuf[0] = 0;    /* Not pretty, but what else */
  565.           inesc[src] = ES_NORMAL; /* can we do?  (ST might not come) */
  566.           }
  567.       }
  568. #endif /* CK_APC */
  569.       break;            /* Absorb all other characters. */
  570.  
  571.     case ES_TERMIN:            /* Maybe a string terminator */
  572.       if (c == '\\') {        /* which must be backslash */
  573.           inesc[src] = ES_NORMAL;    /* If so, back to NORMAL */
  574. #ifdef XPRINT
  575.           if (!src) {
  576.           if (printing && escbufc > 1) { /* If printing... */
  577.               /* Print esc seq buffer */
  578.               zsoutx(ZMFILE,escbuf,escbufc-1);
  579.               debug(F111,"ESCBUF PRINT 5",escbuf,escbufc);
  580.           }
  581.           escbufc = 0;        /* Clear escseq buffer */
  582.           escbuf[0] = NUL;
  583.           }
  584. #endif /* XPRINT */
  585. #ifdef CK_APC
  586.           if (!src && apcactive) {    /* If it was an APC string, */
  587.           debug(F101,"CONNECT APC terminated","",c);
  588.           apcbuf[apclength] = NUL; /* terminate it and then ... */
  589.           return(1);
  590.           }
  591. #endif /* CK_APC */
  592.       } else {            /* It's not a backslash so... */
  593.           inesc[src] = ES_STRING;    /* back to string absorption. */
  594. #ifdef CK_APC
  595.           if (apcactive) {        /* In APC string */
  596.           if (apclength+1 < apcbuflen) { /* If enough room */
  597.               apcbuf[apclength++] = ESC; /* deposit the Esc */
  598.               apcbuf[apclength++] = c;   /* and this character too. */
  599.           } else {        /* Buffer overrun */
  600.               apcactive = 0;
  601.               apclength = 0;
  602.               apcbuf[0] = 0;
  603.               inesc[src] = ES_NORMAL;
  604.           }
  605.           }
  606. #endif /* CK_APC */
  607.       }
  608.       }    /* switch() */
  609.     debug(F111,"chkaes exit inesc",ckitoa(src),inesc[src]);
  610.     return(0);
  611. }
  612. #endif /* NOESCSEQ */
  613.  
  614. /*  C K C P U T C  --  C-Kermit CONNECT Put Character to Screen  */
  615. /*
  616.   Output is buffered to avoid slow screen writes on fast connections.
  617. */
  618. static int
  619. ckcputf() {                /* Dump the output buffer */
  620.     int x = 0;
  621.     if (obc > 0)            /* If we have any characters, */
  622.       x = conxo(obc,obuf);        /* dump them, */
  623.     obp = obuf;                /* reset the pointer */
  624.     obc = 0;                /* and the counter. */
  625.     return(x);                /* Return conxo's return code */
  626. }
  627.  
  628. /*
  629.   NOTE: This is probably the right place for character-set translation,
  630.   rather than down below in the mainline code.  ckcputc() would act like
  631.   xpnbyte() in ckcfns.c, and ckcgetc() would act like xgnbyte().  This
  632.   would shield the rest of the code from all the complexities of many-to-one
  633.   and one-to-many conversions, and would allow handling of Kanji and other
  634.   CJK sets along with UTF-8 and the rest.
  635. */
  636. int
  637. ckcputc(c) int c; {
  638.     int x;
  639.  
  640.     *obp++ = c & 0xff;            /* Deposit the character */
  641.     obc++;                /* Count it */
  642.     if (ibc == 0 ||            /* If input buffer about empty */
  643.     obc == OBUFL) {            /* or output buffer full */
  644.     debug(F101,"CONNECT CKCPUTC obc","",obc);
  645.     x = conxo(obc,obuf);        /* dump the buffer, */
  646.     obp = obuf;            /* reset the pointer */
  647.     obc = 0;            /* and the counter. */
  648.     return(x);            /* Return conxo's return code */
  649.     } else return(0);
  650. }
  651.  
  652. /*  C K C G E T C  --  C-Kermit CONNECT Get Character  */
  653. /*
  654.   Buffered read from communication device.
  655.   Returns the next character, refilling the buffer if necessary.
  656.   On error, returns ttinc's return code (see ttinc() description).
  657.   Dummy argument for compatible calling conventions with ttinc()
  658.   so a pointer to this function can be passed to tn_doop().
  659. */
  660. int
  661. ckcgetc(dummy) int dummy; {
  662.     int c, n;
  663. #ifdef CK_SSL
  664.     extern int ssl_active_flag, tls_active_flag;
  665. #endif /* CK_SSL */
  666.  
  667. #ifdef CK_ENCRYPTION
  668.     /* No buffering for possibly encrypted connections */
  669.     if (network && ttnproto == NP_TELNET && TELOPT_ME(TELOPT_AUTHENTICATION))
  670.       return(ttinc(0));
  671. #endif /* CK_ENCRYPTION */
  672. #ifdef CK_SSL
  673.     if (ssl_active_flag || tls_active_flag)
  674.         return(ttinc(0));
  675. #endif /* CK_SSL */
  676.  
  677.     if (ibc < 1) {            /* Need to refill buffer? */
  678.     ibc = 0;            /* Yes, reset count */
  679.     ibp = ibuf;            /* and buffer pointer */
  680.     c = ttinc(0);            /* Read one character, blocking */
  681.     if (c < 0) {            /* If error, return error code */
  682.         return(c);
  683.     } else {            /* Otherwise, got one character */
  684.         *ibp++ = c;            /* Advance buffer pointer */
  685.         ibc++;            /* and count. */
  686.     }
  687.     if ((n = ttchk()) > 0) {    /* Any more waiting? */
  688.         if (n > (IBUFL - ibc))    /* Get them all at once. */
  689.           n = IBUFL - ibc;        /* Don't overflow buffer */
  690.           if ((n = ttxin(n,(CHAR *)ibp)) > 0) {
  691. #ifdef CK_ENCRYPTION
  692.           if (TELOPT_U(TELOPT_ENCRYPTION))
  693.                     ck_tn_decrypt(ibp,n);
  694. #endif /* CK_ENCRYPTION */
  695.           ibc += n;        /* Advance counter */
  696.               }
  697.     } else if (n < 0) {        /* Error? */
  698.         return(n);            /* Return the error code */
  699.     }
  700.     ibp = ibuf;            /* Point to beginning of buffer */
  701.     }
  702.     c = *ibp++ & 0xff;            /* Get next character from buffer */
  703.     ibc--;                /* Reduce buffer count */
  704.     /* debug(F000,"CKCGETC","",c); */
  705.     return(c);                /* Return the character */
  706. }
  707.  
  708. /*
  709.    Keyboard handling, buffered for speed, which is needed when C-Kermit is
  710.    in CONNECT mode between two other computers that are transferring data.
  711. */
  712. static char *kbp;            /* Keyboard input buffer pointer */
  713. static int kbc;                /* Keyboard input buffer count */
  714.  
  715. #ifdef CK_SMALL                /* Keyboard input buffer length */
  716. #define KBUFL 32            /* Small for PDP-11 UNIX */
  717. #else
  718. #define KBUFL 257            /* Regular kernel size for others */
  719. #endif /* CK_SMALL */
  720.  
  721. #ifdef DYNAMIC
  722. static char *kbuf = NULL;
  723. #else
  724. static char kbuf[KBUFL];
  725. #endif /* DYNAMIC */
  726.  
  727. /* Macro for reading keystrokes. */
  728.  
  729. #define CONGKS() (((--kbc)>=0) ? ((int)(*kbp++) & 0377) : kbget())
  730.  
  731. /*
  732.   Note that we call read() directly here, normally a no-no, but in this case
  733.   we know it's UNIX and we're only doing what coninc(0) would have done,
  734.   except we're reading a block of characters rather than just one.  There is,
  735.   at present, no conxin() analog to ttxin() for chunk reads, and instituting
  736.   one would only add function-call overhead as it would only be a wrapper for
  737.   a read() call anyway.
  738.  
  739.   Another note: We stick in this read() till the user types something.
  740.   But we know they already did, since select() said so.  Therefore something
  741.   would need to be mighty wrong before we get in trouble here.
  742. */
  743. static int                /* Keyboard buffer filler */
  744. kbget() {
  745. #ifdef EINTR
  746.     int tries = 10;            /* If read() is interrupted, */
  747.     int ok = 0;
  748.     while (tries-- > 0) {        /* try a few times... */
  749. #endif /* EINTR */
  750.     kbc = conchk();            /* How many chars waiting? */
  751.     debug(F101,"kbget kbc","",kbc);
  752.     if (kbc < 1)
  753.       kbc = 1;            /* If none or dunno, wait for one. */
  754.     else if (kbc > KBUFL)        /* If too many, */
  755.       kbc = KBUFL;            /* only read this many. */
  756.     if ((kbc = read(0, kbuf, kbc)) < 1) { /* Now read it/them. */
  757.         debug(F101,"CONNECT kbget errno","",errno);    /* Got an error. */
  758. #ifdef EINTR
  759.         if (errno == EINTR)        /* Interrupted system call. */
  760.           continue;            /* Try again, up to limit. */
  761.         else            /* Something else. */
  762. #endif /* EINTR */
  763.           return(-1);        /* Pass along read() error. */
  764.     }
  765. #ifdef EINTR
  766.     else { ok = 1; break; }
  767.     }
  768.     if (!ok) return(-1);
  769. #endif /* EINTR */
  770.     kbp = kbuf;                /* Adjust buffer pointer, */
  771.     kbc--;                /* count, */
  772.     return((int)(*kbp++) & 0377);    /* and return first character. */
  773. }
  774.  
  775. #ifdef BEBOX
  776. /*
  777.  * CreateSocketPair --
  778.  *
  779.  *    This procedure creates a connected socket pair
  780.  *
  781.  * Results:
  782.  *    0 if OK, the error if not OK.
  783.  *
  784.  * Side effects:
  785.  *    None
  786.  */
  787. int
  788. socketpair(int *pair) {
  789.     int servsock;
  790.     int val;
  791.     struct sockaddr_in serv_addr, cli_addr;
  792.     extern char myipaddr[];
  793.  
  794.     debug(F110,"socketpair",myipaddr,0);
  795.  
  796.     if (myipaddr[0] == 0)
  797.       getlocalipaddr();
  798.  
  799.     servsock = socket(AF_INET, SOCK_STREAM, 0);
  800.     if (servsock == 0) {
  801.     return h_errno;
  802.     }
  803.     debug(F111,"socketpair","socket",servsock);
  804.  
  805.     memset(&serv_addr, 0, sizeof(serv_addr));
  806.     serv_addr.sin_family = AF_INET;
  807.     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  808.     serv_addr.sin_port = htons(0);
  809.  
  810.     val = sizeof(serv_addr);
  811.     if (bind(servsock, (struct sockaddr *) &serv_addr, val) < 0) {
  812.     closesocket(servsock);
  813.     return h_errno;
  814.     }
  815.     debug(F111,"socketpair","bind",0);
  816.  
  817.     listen(servsock, 1);
  818.     debug(F111,"socketpair","listen",0);
  819.  
  820.     if (getsockname(servsock, (struct sockaddr *) &serv_addr, &val) < 0) {
  821.     closesocket(servsock);
  822.     return h_errno;
  823.     }
  824.     debug(F111,"socketpair","getsockname",0);
  825.  
  826.     pair[0] = socket(AF_INET, SOCK_STREAM, 0);
  827.     if (pair[0] == 0) {
  828.     closesocket(servsock);
  829.     return h_errno;
  830.     }
  831.     debug(F111,"socketpair","socket",pair[0]);
  832.  
  833.     memset(&cli_addr, 0, sizeof(cli_addr));
  834.     cli_addr.sin_family = AF_INET;
  835.     cli_addr.sin_addr.s_addr = inet_addr(myipaddr[0]?myipaddr:"127.0.0.1");
  836.     cli_addr.sin_port = serv_addr.sin_port;
  837.  
  838.     if (connect(pair[0],(struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) {
  839.     closesocket(pair[0]);
  840.     closesocket(servsock);
  841.     return h_errno;
  842.     }
  843.     debug(F111,"socketpair","connect",0);
  844.  
  845.     pair[1] = accept(servsock, (struct sockaddr *) &serv_addr, &val);
  846.     if (pair[1] == 0) {
  847.     closesocket(pair[0]);
  848.     closesocket(servsock);
  849.     return h_errno;
  850.     }
  851.     debug(F111,"socketpair","accept",pair[1]);
  852.  
  853.     closesocket(servsock);
  854.     debug(F111,"socketpair","closesocket",0);
  855.     return 0;
  856. }
  857.  
  858. long
  859. kbdread(void * param) {
  860.     int sock = (int) param;
  861.     char ch;
  862.     int rc = 0;
  863.  
  864.     debug(F111,"kbdread","sock",sock);
  865.  
  866.     while (rc >= 0) {
  867.     rc = read(fileno(stdin), &ch, 1); /* Read a character. */
  868.     if (rc > 0) {
  869.         rc = send(sock,&ch,1,0);
  870.         /* debug(F000,"kbdread","send()",ch); */
  871.         printf("\r\ngot: %c rc = %d\r\n",ch,rc);
  872.     } else
  873.       msleep(100);
  874.     }
  875.     debug(F110,"kbdread","terminating",0);
  876.     return(rc);
  877. }
  878. #endif /* BEBOX */
  879.  
  880. static int printbar = 0;
  881.  
  882. #define OUTXBUFSIZ 15
  883. static CHAR inxbuf[OUTXBUFSIZ+1];    /* Host-to-screen expansion buffer */
  884. static int inxcount = 0;        /* and count */
  885. static CHAR outxbuf[OUTXBUFSIZ+1];    /* Keyboard-to-host expansion buf */
  886. static int outxcount = 0;        /* and count */
  887.  
  888. int
  889. conect() {
  890.     int rc = 0;                /* Return code: 0 = fail, 1 = OK */
  891.     int i, x = 0;
  892.     register int c, c2, csave;        /* Characters */
  893. #ifdef TNCODE
  894.     int tx;                /* For Telnet negotiations */
  895. #endif /* TNCODE */
  896.     int apcrc = 0;            /* For APC and transparent print */
  897.     int n, kbin, scrnout;        /* select() items... */
  898.     fd_set in, out, err;        /* File descriptor sets */
  899.     int gotnet = 0;            /* Flag for net ready to read */
  900.     int gotkbd = 0;            /* Flag for keyboard ready to read */
  901.     int oldprt = 0;            /* Used with printing */
  902.     int msgflg = 0;
  903.     char cbuf[2];            /* Ditto */
  904.  
  905. #ifdef BEBOX
  906.     int tid = 0;            /* Thread ID */
  907.     int pair[2];            /* Socket Pair */
  908.     CHAR ch;
  909.     CHAR buf[64];
  910. #endif /* BEBOX */
  911.  
  912. #ifdef BEBOX
  913.     {
  914.     /* Create a socket pair to be used for the keyboard input */
  915.     if (socketpair(pair)) {
  916.         debug(F110,"conect","unable to create socket pair",0);
  917.         return(-1);
  918.     }
  919.     debug(F111,"connect","socket pair[0]",pair[0]);
  920.     debug(F111,"connect","socket pair[1]",pair[1]);
  921.  
  922.     /* Assign one end of the socket to kbin */
  923.     kbin = pair[0];
  924.         tid = spawn_thread(kbdread,
  925.                "Kbd to Socket Pair",
  926.                 B_NORMAL_PRIORITY,
  927.                (void *)pair[1]
  928.                );
  929.         resume_thread(tid);
  930.     debug(F110,"connect","tid",tid);
  931.     }
  932. #else /* BEBOX */
  933.     kbin = fileno(stdin);        /* stdin file descriptor */
  934. #endif /* BEBOX */
  935.  
  936.     scrnout = fileno(stdout);        /* stdout file descriptor */
  937.  
  938. #ifdef CK_TRIGGER
  939.     makestr(&triggerval,NULL);        /* Reset trigger */
  940. #endif /* CK_TRIGGER */
  941.  
  942. #ifdef XPRINT
  943.     escbufc = 0;            /* Reset esc-sequence buffer */
  944.     escbuf[0] = NUL;
  945. #endif /* XPRINT */
  946.     cbuf[1] = NUL;
  947.  
  948.     ttimoff();                /* Turn off any timer interrupts */
  949.     if (!local) {            /* Be sure we're not in remote mode */
  950. #ifdef NETCONN
  951.     printf("Sorry, you must SET LINE or SET HOST first\n");
  952. #else
  953.     printf("Sorry, you must SET LINE first\n");
  954. #endif /* NETCONN */
  955.     return(0);
  956.     }
  957.     if (speed < 0L && network == 0 && ttfdflg == 0) {
  958.     printf("Sorry, you must SET SPEED first\n");
  959.     return(0);
  960.     }
  961. #ifdef TCPSOCKET
  962.     if (network && !ttpipe && (nettype != NET_TCPB && nettype != NET_PTY)) {
  963.     printf("Sorry, network type not supported\n");
  964.     return(0);
  965.     }
  966. #endif /* TCPSOCKET */
  967.  
  968. #ifdef DYNAMIC
  969.     if (!ibuf) {
  970.     if (!(ibuf = malloc(IBUFL+1))) { /* Allocate input line buffer */
  971.         printf("Sorry, CONNECT input buffer can't be allocated\n");
  972.         return(0);
  973.     } else {
  974.         ibp = ibuf;
  975.         ibc = 0;
  976.     }
  977.     }
  978.     if (!obuf) {
  979.     if (!(obuf = malloc(OBUFL+1))) { /* Allocate output line buffer */
  980.         printf("Sorry, CONNECT output buffer can't be allocated\n");
  981.         return(0);
  982.     } else {
  983.         obp = obuf;
  984.         obc = 0;
  985.     }
  986.     }
  987.     if (!kbuf) {
  988.     if (!(kbuf = malloc(KBUFL+1))) { /* Allocate keyboard input buffer */
  989.         printf("Sorry, CONNECT keyboard buffer can't be allocated\n");
  990.         return(0);
  991.     }
  992.     }
  993.     if (!temp) {
  994.     if (!(temp = malloc(TMPLEN+1))) { /* Allocate temporary buffer */
  995.         printf("Sorry, CONNECT temporary buffer can't be allocated\n");
  996.         return(0);
  997.     }
  998.     }
  999. #else
  1000.     obp = obuf;
  1001.     obc = 0;
  1002. #endif /* DYNAMIC */
  1003.  
  1004.     kbp = kbuf;                /* Always clear these. */
  1005.     *kbp = NUL;                /* No need to preserve them between */
  1006.     kbc = 0;                /* CONNECT sessions. */
  1007.  
  1008. #ifdef DEBUG
  1009.     if (deblog) {
  1010.     debug(F101,"CONNECT conect entry ttyfd","",ttyfd);
  1011.     debug(F101,"CONNECT conect entry ibc","",ibc);
  1012.     debug(F101,"CONNECT conect entry obc","",obc);
  1013.     debug(F101,"CONNECT conect entry kbc","",kbc);
  1014. #ifdef CK_TRIGGER
  1015.     debug(F110,"CONNECT conect trigger",tt_trigger[0],0);
  1016. #endif /* CK_TRIGGER */
  1017.     if (ttyfd > -1) {
  1018.         n = ttchk();
  1019.         debug(F101,"CONNECT conect entry ttchk","",n);
  1020.     }
  1021.     }
  1022. #endif /* DEBUG */
  1023.  
  1024.     if (ttyfd < 0) {            /* If communication device not open */
  1025. #ifdef TTLEBUF
  1026.         int n = le_inbuf();
  1027.         debug(F111,"CONNECT le_inbuf()","ttyfd < 0",n);
  1028.         if (n > 0) {
  1029.             while (n--) {
  1030.                 CHAR ch;
  1031.                 le_getchar(&ch);
  1032.                 conoc(ch);
  1033.             }
  1034.             return(0);
  1035.         }
  1036. #endif /* TTLEBUF */
  1037.  
  1038.     debug(F101,"CONNECT ttnproto","",ttnproto);
  1039.     debug(F111,"CONNECT opening",ttname,0); /* Open it now */
  1040.     if (ttopen(ttname,
  1041.            &local,
  1042.            network ? -nettype : mdmtyp,
  1043.            0
  1044.            ) < 0) {
  1045.         sprintf(temp,"Sorry, can't open %s",ttname);
  1046.         perror(temp);
  1047.         debug(F110,"CONNECT open failure",ttname,0);
  1048.         return(0);
  1049.     }
  1050. #ifdef IKS_OPTION
  1051.     /* If peer is in Kermit server mode, return now. */
  1052.     if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start)
  1053.       return(0);
  1054. #endif /* IKS_OPTION */
  1055.     }
  1056.     dohangup = 0;            /* Hangup not requested yet */
  1057.  
  1058.     msgflg = !quiet
  1059. #ifdef CK_APC
  1060.       && !apcactive
  1061. #endif /* CK_APC */
  1062.     ;
  1063.  
  1064.     if (msgflg) {
  1065. #ifdef NETCONN
  1066.     if (network) {
  1067. #ifdef CK_ENCRYPTION
  1068.         extern int me_encrypt, u_encrypt;
  1069.         if (ck_tn_encrypting() && ck_tn_decrypting())
  1070.           printf("SECURE connection to host %s",ttname);
  1071.         else
  1072. #endif /* CK_ENCRYPTION */
  1073.           if (ttpipe || ttpty)
  1074.         printf("Connecting via command \"%s\"",ttname);
  1075.           else
  1076.         printf("Connecting to host %s",ttname);
  1077.     } else {
  1078. #endif /* NETCONN */
  1079.         printf("Connecting to %s",ttname);
  1080.         if (speed > -1L) printf(", speed %ld",speed);
  1081. #ifdef NETCONN
  1082.     }
  1083. #endif /* NETCONN */
  1084.     if (tt_escape) {
  1085.         printf(".\r\nThe escape character is Ctrl-%c (ASCII %d, %s)\r\n",
  1086.            ctl(escape), escape,
  1087.            (escape == 127 ? "DEL" : ccntab[escape]));
  1088.         printf("Type the escape character followed by C to get back,\r\n");
  1089.         printf("or followed by ? to see other options.\r\n");
  1090.     } else {
  1091.         printf(".\r\n\nESCAPE CHARACTER IS DISABLED\r\n\n");
  1092.     }
  1093.     if (seslog) {
  1094.         printf("(Session logged to %s, ",sesfil);
  1095.         printf("%s)\r\n", sessft ? "binary" : "text");
  1096.     }
  1097.     if (debses) printf("Debugging Display...)\r\n");
  1098.     }
  1099.  
  1100. /* Condition console terminal and communication line */
  1101.  
  1102.     if (conbin((char)escape) < 0) {
  1103.     printf("Sorry, can't condition console terminal\n");
  1104.     fflush(stdout);
  1105.     return(0);
  1106.     }
  1107.     debug(F101,"CONNECT cmask","",cmask);
  1108.     debug(F101,"CONNECT cmdmsk","",cmdmsk);
  1109.     debug(F101,"CONNECT speed before ttvt","",speed);
  1110.     if ((n = ttvt(speed,flow)) < 0) {    /* Enter "virtual terminal" mode */
  1111.     if (!network) {
  1112.         debug(F101,"CONNECT ttvt","",n);
  1113.         tthang();            /* Hang up and close the device. */
  1114.         ttclos(0);
  1115.         if (ttopen(ttname,        /* Open it again... */
  1116.                &local,
  1117.                network ? -nettype : mdmtyp,
  1118.                0
  1119.                ) < 0) {
  1120.         sprintf(temp,"Sorry, can't reopen %s",ttname);
  1121.         perror(temp);
  1122.         return(0);
  1123.         }
  1124. #ifdef IKS_OPTION
  1125.         if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start)
  1126.           return(0);
  1127. #endif /* IKS_OPTION */
  1128.  
  1129.         if (ttvt(speed,flow) < 0) {    /* Try virtual terminal mode again. */
  1130.         conres();        /* Failure this time is fatal. */
  1131.         printf("Sorry, Can't condition communication line\n");
  1132.         return(0);
  1133.         }
  1134.     }
  1135.     }
  1136.     debug(F101,"CONNECT ttvt ok, escape","",escape);
  1137.  
  1138.     /* Despite ttvt() this is still needed in HP-UX */
  1139.     /* because of the HP-9000 <RESET> key.*/
  1140.  
  1141.     signal(SIGINT, SIG_IGN);
  1142.     signal(SIGQUIT, SIG_IGN);
  1143.  
  1144.     debug(F101,"CONNECT carrier-watch","",carrier);
  1145.     if (!network && (carrier != CAR_OFF)) {
  1146.     int x;
  1147.     x = ttgmdm();
  1148.     debug(F100,"CONNECT ttgmdm","",x);
  1149.     if ((x > -1) && !(x & BM_DCD)) {
  1150. #ifndef NOICP
  1151.         extern int hints;
  1152. #endif /* NOICP */
  1153.         debug(F100,"CONNECT ttgmdm CD test fails","",x);
  1154.         conres();
  1155.         printf("?Carrier required but not detected.\n");
  1156. #ifndef NOICP
  1157.         if (!hints)
  1158.           return(0);
  1159.         printf("***********************************\n");
  1160.         printf(" Hint: To CONNECT to a serial device that\n");
  1161.         printf(" is not presenting the Carrier Detect signal,\n");
  1162.         printf(" first tell C-Kermit to:\n\n");
  1163.         printf("   SET CARRIER-WATCH OFF\n\n");
  1164.         printf("***********************************\n\n");
  1165. #endif /* NOICP */
  1166.         return(0);
  1167.     }
  1168.     debug(F100,"CONNECT ttgmdm ok","",0);
  1169.     }
  1170.  
  1171.     /* Now we are connected. */
  1172.  
  1173.     if (msgflg || printbar)
  1174.       printf("----------------------------------------------------\r\n");
  1175.     fflush(stdout);
  1176.  
  1177. #ifndef NOCSETS
  1178. /* Set up character set translations */
  1179.  
  1180.     unicode = 0;            /* Assume Unicode won't be involved */
  1181.     tcs = 0;                /* "Transfer" or "Other" charset */
  1182.     sxo = rxo = NULL;            /* Initialize byte-to-byte functions */
  1183.     sxi = rxi = NULL;
  1184.     if (tcsr != tcsl) {            /* Remote and local sets differ... */
  1185. #ifdef UNICODE
  1186.     if (tcsr == FC_UTF8 ||        /* Remote charset is UTF-8 */
  1187.         tcsl == FC_UTF8) {        /* or local one is. */
  1188.         xuf = xl_ufc[tcsl];        /* Incoming Unicode to local */
  1189.         if (xuf) {
  1190.         tcs = (tcsr == FC_UTF8) ? tcsl : tcsr; /* The "other" set */
  1191.         xfu = xl_fcu[tcs];    /* Local byte to remote Unicode */
  1192.         if (xfu)
  1193.           unicode = (tcsr == FC_UTF8) ? 1 : 2;
  1194.         }
  1195.         tcssize = fcsinfo[tcs].size; /* Size of other character set. */
  1196.     } else {
  1197. #endif /* UNICODE */
  1198.         tcs = gettcs(tcsr,tcsl);    /* Get intermediate set. */
  1199.         sxo = xls[tcs][tcsl];    /* translation function */
  1200.         rxo = xlr[tcs][tcsr];    /* pointers for output functions */
  1201.         sxi = xls[tcs][tcsr];    /* and for input functions. */
  1202.         rxi = xlr[tcs][tcsl];
  1203. #ifdef UNICODE
  1204.     }
  1205. #endif /* UNICODE */
  1206.     }
  1207. /*
  1208.   This is to prevent use of zmstuff() and zdstuff() by translation functions.
  1209.   They only work with disk i/o, not with communication i/o.  Luckily Russian
  1210.   translation functions don't do any stuffing...
  1211. */
  1212.     langsv = language;
  1213. #ifndef NOCYRIL
  1214.     if (language != L_RUSSIAN)
  1215. #endif /* NOCYRIL */
  1216.       language = L_USASCII;
  1217.  
  1218. #ifdef COMMENT
  1219. #ifdef DEBUG
  1220.     if (deblog) {
  1221.     debug(F101,"CONNECT tcs","",tcs);
  1222.     debug(F101,"CONNECT tcsl","",tcsl);
  1223.     debug(F101,"CONNECT tcsr","",tcsr);
  1224.     debug(F101,"CONNECT fcsinfo[tcsl].size","",fcsinfo[tcsl].size);
  1225.     debug(F101,"CONNECT fcsinfo[tcsr].size","",fcsinfo[tcsr].size);
  1226.     debug(F101,"CONNECT unicode","",unicode);
  1227.     }
  1228. #endif /* DEBUG */
  1229. #endif /* COMMENT */
  1230.  
  1231. #ifdef CK_XYZ
  1232. #ifndef XYZ_INTERNAL
  1233.     {
  1234.     extern int binary;        /* See about ZMODEM autodownloads */
  1235.     char * s;
  1236.     s = binary ? ptab[PROTO_Z].p_b_rcmd : ptab[PROTO_Z].p_t_rcmd;
  1237.     if (!s) s = "";
  1238.     zmdlok = (*s != NUL);        /* OK if we have external commands */
  1239.     }
  1240. #endif /* XYZ_INTERNAL */
  1241. #endif /* CK_XYZ */
  1242.  
  1243. #ifndef NOESCSEQ
  1244. /*
  1245.   We need to activate the escape-sequence recognition feature when:
  1246.    (a) translation is elected, AND
  1247.    (b) the local and/or remote set is a 7-bit set other than US ASCII.
  1248.   Or:
  1249.    SET TERMINAL APC is not OFF (handled in the next statement).
  1250. */
  1251.     escseq = (tcs != TC_TRANSP) &&    /* Not transparent */
  1252.       (fcsinfo[tcsl].size == 128 || fcsinfo[tcsr].size == 128) && /* 7 bits */
  1253.     (fcsinfo[tcsl].code != FC_USASCII); /* But not ASCII */
  1254. #endif /* NOESCSEQ */
  1255. #endif /* NOCSETS */
  1256.  
  1257. #ifndef NOESCSEQ
  1258. #ifdef CK_APC
  1259.     escseq = escseq || (apcstatus != APC_OFF);
  1260.     apcactive = 0;            /* An APC command is not active */
  1261.     apclength = 0;            /* ... */
  1262. #endif /* CK_APC */
  1263. #ifdef XPRINT
  1264.     escseq |= tt_print;
  1265. #endif /* XPRINT */
  1266.     inesc[0] = ES_NORMAL;        /* Initial state of recognizer */
  1267.     inesc[1] = ES_NORMAL;
  1268.     debug(F101,"CONNECT escseq","",escseq);
  1269. #endif /* NOESCSEQ */
  1270.  
  1271.     if (ttyfd > -1) {            /* (just in case...) */
  1272.     what = W_CONNECT;        /* Keep track of what we're doing */
  1273.     FD_ZERO(&in);            /* Clear select() structs */
  1274.     FD_ZERO(&out);
  1275.     FD_ZERO(&err);
  1276.     active = 1;
  1277.     }
  1278.     while (active) {            /* Big loop... */
  1279.     debug(F100,"CONNECT top of loop","",0);
  1280.     gotkbd = 0;
  1281.     gotnet = ttpeek();        /* Something sitting in ckutio buf */
  1282.     debug(F101,"CONNECT ttpeek","",gotnet);
  1283.  
  1284.     if (
  1285. #ifndef NOSETKEY
  1286.         !kmptr            /* Check for key macro active */
  1287. #else
  1288.         1
  1289. #endif /* NOSETKEY */
  1290.         ) {
  1291.         if (obc) {            /* No key macro - set up for select */
  1292.         FD_SET(ttyfd, &out);    /* Have stuff to send to net */
  1293.         } else {
  1294.         FD_SET(kbin, &in);    /* Need to read stuff from keyboard */
  1295.         }
  1296. #ifdef BEBOX
  1297.         if (!(ibc || gotnet > 0))
  1298.         FD_SET(ttyfd, &in);    /* Need to read stuff from net */
  1299. #else /* BEBOX */
  1300.         if (ibc || gotnet > 0) {
  1301.         FD_SET(scrnout, &out);    /* Have stuff to put on screen */
  1302.         } else {
  1303.         FD_SET(ttyfd, &in);    /* Need to read stuff from net */
  1304.         }
  1305. #endif /* BEBOX */
  1306.             FD_SET(ttyfd, &err);
  1307. #ifdef CK_FORWARD_X
  1308.             fwdx_init_fd_set(&in);
  1309. #endif /* CK_FORWARD_X */
  1310.  
  1311.         /* Wait till the first one of the above is ready for i/o */
  1312.  
  1313.         /* NOTE: here we could implement idle timeouts */
  1314.  
  1315.         debug(F100,"CONNECT select() waiting...","",0);
  1316.         c = select(16, &in, &out, &err, 0);
  1317.         debug(F101,"CONNECT select()","",c);
  1318.         if (c < 1) {
  1319. #ifdef EINTR
  1320.         if (c == -1) {
  1321.             if (errno == EINTR) {
  1322.             continue;
  1323.             }
  1324.         }
  1325. #endif /* EINTR */
  1326.         sleep(1);
  1327.         continue;
  1328.         }
  1329. #ifndef BEBOX
  1330.         if (FD_ISSET(scrnout, &out)) {
  1331.         debug(F100,"CONNECT SELECT scrnout","",0);
  1332.         }
  1333. #endif /* BEBOX */
  1334. #ifdef CK_FORWARD_X
  1335.             fwdx_check_sockets(&in);
  1336. #endif /* CK_FORWARD_X */
  1337.         if (FD_ISSET(ttyfd, &in)) {    /* Read from net? */
  1338.         debug(F110,"CONNECT SELECT ttyfd","in",0);
  1339.         FD_CLR(ttyfd, &in);
  1340.         gotnet = 1;        /* Net is ready */
  1341.         }
  1342.         if (FD_ISSET(kbin, &in)) {    /* Read from keyboard? */
  1343.         debug(F100,"CONNECT SELECT kbin","",0);
  1344.         FD_CLR(kbin, &in);
  1345.         gotkbd = 1;        /* Keyboard is ready */
  1346.         }
  1347.             if (FD_ISSET(ttyfd, &err)) {
  1348.         debug(F110,"CONNECT SELECT ttyfd","err",0);
  1349.         FD_CLR(ttyfd, &err);
  1350. #ifdef NETPTY
  1351. #ifdef HAVE_PTYTRAP
  1352.         /* Special handling for HP-UX pty i/o */
  1353.                 if (ttpty) {
  1354.                     if (pty_trap_handler(ttyfd) > 0) {
  1355.                         ttclos(0);
  1356.                         goto conret1;
  1357.                     }
  1358.                     continue;
  1359.                 }
  1360. #endif /* HAVE_PTYTRAP */
  1361. #endif /* NETPTY */
  1362.         gotnet = 1;        /* Net is ready (don't set if pty) */
  1363.             }
  1364.     }
  1365.     debug(F101,"CONNECT gotkbd","",gotkbd);
  1366.     debug(F101,"CONNECT kbc","",kbc);
  1367. #ifndef NOSETKEY
  1368.     debug(F101,"CONNECT kmptr","",kmptr);
  1369. #endif /* NOSETKEY */
  1370.     while (gotkbd || kbc > 0    /* If we have keyboard chars */
  1371. #ifndef NOSETKEY
  1372.            || kmptr
  1373. #endif /* NOSETKEY */
  1374.            ) {
  1375. #ifndef NOSETKEY
  1376.         if (kmptr) {        /* Have current macro? */
  1377.         debug(F100,"CONNECT kmptr non NULL","",0);
  1378.         if ((c = (CHAR) *kmptr++) == NUL) { /* Get char from it */
  1379.             kmptr = NULL;    /* If no more chars,  */
  1380.             debug(F100,"CONNECT macro empty, continuing","",0);
  1381.             continue;    /* reset pointer and continue */
  1382.         }
  1383.         debug(F000,"CONNECT char from macro","",c);
  1384.         } else {            /* No macro... */
  1385. #endif /* NOSETKEY */
  1386. #ifdef BEBOX
  1387.         {
  1388.             int rc = 0;
  1389.             if ((rc = recv(kbin,buf,1,0)) > 0)
  1390.               c = buf[0];
  1391.             else
  1392.               c = -1;
  1393.             debug(F111,"recv","rc",rc);
  1394.             printf("\r\nrecv: %c rc=%d\r\n",buf[0],rc);
  1395.         }
  1396. #else /* BEBOX */
  1397.         c = CONGKS();        /* Yes, read from keyboard */
  1398. #endif /* BEBOX */
  1399.         gotkbd = 0;        /* Turn off select() result flag */
  1400. #ifndef NOSETKEY
  1401.         }
  1402. #endif /* NOSETKEY */
  1403.         if (c == -1) {
  1404. #ifdef EINTR
  1405.         if (errno == EINTR)
  1406.           continue;
  1407. #endif /* EINTR */
  1408.         conoc(BEL);
  1409.         goto conret0;
  1410.         }
  1411.         c &= cmdmsk;        /* Do any requested masking */
  1412. #ifndef NOSETKEY
  1413. /*
  1414.   Note: kmptr is NULL if we got character c from the keyboard, and it is
  1415.   not NULL if it came from a macro.  In the latter case, we must avoid
  1416.   expanding it again.
  1417. */
  1418.         if (!kmptr && macrotab[c]) { /* Macro definition for c? */
  1419.         debug(F000,"CONNECT macro key",macrotab[c],c);
  1420.         kmptr = macrotab[c];    /* Yes, set up macro pointer */
  1421.         continue;        /* and restart the loop, */
  1422.         } else c = keymap[c];    /* else use single-char keymap */
  1423. #endif /* NOSETKEY */
  1424.         if (
  1425. #ifndef NOSETKEY
  1426.         !kmptr &&
  1427. #endif /* NOSETKEY */
  1428.         (tt_escape && (c & 0x7f) == escape)) { /* Escape char? */
  1429.         debug(F000,"CONNECT got escape","",c);
  1430. #ifdef BEBOX
  1431.         if (recv(kbin,buf,1,0)>=0)
  1432.           c = buf[0];
  1433.         else
  1434.           c = -1;
  1435. #else /* BEBOX */
  1436.         c = CONGKS() & 0177;    /* Read argument */
  1437. #endif /* BEBOX */
  1438.         doesc((char) c);    /* Handle it */
  1439.         continue;        /* Back to loop */
  1440.         }
  1441.         csave = c;            /* Save it before translation */
  1442.                     /* for local echoing. */
  1443. #ifndef NOCSETS
  1444.         if (inesc[1] == ES_NORMAL) { /* If not inside escape seq.. */
  1445.         /* Translate character sets */
  1446. #ifdef UNICODE
  1447.         int x;
  1448.         if (unicode == 1) {    /* Remote is UTF-8 */
  1449.             outxcount = b_to_u((CHAR)c,outxbuf,OUTXBUFSIZ,tcssize);
  1450.             outxbuf[outxcount] = NUL;
  1451.         } else if (unicode == 2) { /* Local is UTF-8 */
  1452.             x = u_to_b((CHAR)c);
  1453.             if (x < 0)
  1454.               continue;
  1455.             outxbuf[0] = (unsigned)(x & 0xff);
  1456.             outxcount = 1;
  1457.             outxbuf[outxcount] = NUL;
  1458.         } else {
  1459. #endif /* UNICODE */
  1460.             if (sxo) c = (*sxo)((char)c); /* Local-intermediate */
  1461.             if (rxo) c = (*rxo)((char)c); /* Intermediate-remote */
  1462.             outxbuf[0] = c;
  1463.             outxcount = 1;
  1464.             outxbuf[outxcount] = NUL;
  1465. #ifdef UNICODE
  1466.         }
  1467. #endif /* UNICODE */
  1468.         } else {
  1469.         outxbuf[0] = c;
  1470.         outxcount = 1;
  1471.         outxbuf[outxcount] = NUL;
  1472.         }
  1473.         if (escseq)
  1474.           apcrc = chkaes((char)c,1);
  1475. #else
  1476.         outxbuf[0] = c;
  1477.         outxcount = 1;
  1478.         outxbuf[outxcount] = NUL;
  1479. #endif /* NOCSETS */
  1480.  
  1481.         debug(F111,"OUTXBUF",outxbuf,outxcount);
  1482.  
  1483.         for (i = 0; i < outxcount; i++) {
  1484.         c = outxbuf[i];
  1485. /*
  1486.  If Shift-In/Shift-Out is selected and we have a 7-bit connection,
  1487.  handle shifting here.
  1488. */
  1489.         if (sosi) {             /* Shift-In/Out selected? */
  1490.             if (cmask == 0177) {     /* In 7-bit environment? */
  1491.             if (c & 0200) {         /* 8-bit character? */
  1492.                 if (outshift == 0) { /* If not shifted, */
  1493.                 ttoc(dopar(SO)); /* shift. */
  1494.                 outshift = 1;
  1495.                 }
  1496.             } else {
  1497.                 if (outshift == 1) { /* 7-bit character */
  1498.                 ttoc(dopar(SI)); /* If shifted, */
  1499.                 outshift = 0;    /* unshift. */
  1500.                 }
  1501.             }
  1502.             }
  1503.             if (c == SO) outshift = 1; /* User typed SO */
  1504.             if (c == SI) outshift = 0; /* User typed SI */
  1505.         }
  1506.         c &= cmask;        /* Apply Kermit-to-host mask now. */
  1507.         if (c == '\015') {    /* Carriage Return */
  1508.             int stuff = -1;
  1509.             if (tnlm) {        /* TERMINAL NEWLINE ON */
  1510.             stuff = LF;     /* Stuff LF */
  1511. #ifdef TNCODE
  1512.             } else if (network && /* TELNET NEWLINE ON/OFF/RAW */
  1513.                    (ttnproto == NP_TELNET)) {
  1514.             switch (!TELOPT_ME(TELOPT_BINARY) ? tn_nlm : tn_b_nlm){
  1515.               case TNL_CRLF:
  1516.                 stuff = LF;
  1517.                 break;
  1518.               case TNL_CRNUL:
  1519.                 stuff = NUL;
  1520.                 break;
  1521.             }
  1522. #endif /* TNCODE */
  1523.             }
  1524.             if (stuff > -1) {
  1525.             ttoc(dopar('\015'));    /* Send CR */
  1526.             if (duplex) conoc('\015'); /* Maybe echo CR */
  1527.             c = stuff;    /* Char to stuff */
  1528.             csave = c;
  1529.             }
  1530.         }
  1531. #ifdef TNCODE
  1532. /* If user types the 0xff character (TELNET IAC), it must be doubled. */
  1533.         else        /* Not CR */
  1534.           if ((dopar((CHAR) c) == IAC) && /* IAC (0xff) */
  1535.               network && (ttnproto == NP_TELNET)) { /* Send one now */
  1536.               ttoc((char)IAC); /* and the other one just below. */
  1537.           }
  1538. #endif /* TNCODE */
  1539.         /* Send the character */
  1540.  
  1541.         x = ttoc((char)dopar((CHAR) c));
  1542.         if (x > -1) {
  1543.             if (duplex) {    /* If half duplex, must echo */
  1544.             if (debses)
  1545.               conol(dbchr(csave)); /* the original char */
  1546.             else        /* not the translated one */
  1547.               conoc((char)csave);
  1548.             if (seslog) {    /* And maybe log it too */
  1549.                 c2 = csave;
  1550.                 if (sessft == 0 && csave == '\r')
  1551.                   c2 = '\n';
  1552.                 logchar((char)c2);
  1553.             }
  1554.             }
  1555.         } else {
  1556.             perror("\r\nCan't send character");
  1557.             active = 0;
  1558.             break;
  1559.         }
  1560.         }
  1561.     }
  1562.     if (FD_ISSET(ttyfd, &out)) {
  1563.         FD_CLR(ttyfd, &out);
  1564.     }
  1565.     while (gotnet > 0 || ibc > 0) {
  1566.         gotnet = 0;
  1567.         c = ckcgetc(0);        /* Get next character */
  1568.         /* debug(F101,"CONNECT c","",c); */
  1569.         if (c < 0) {        /* Failed... */
  1570.         ckcputf();        /* Flush CONNECT output buffer */
  1571.         if (msgflg) {
  1572.             printf("\r\nCommunications disconnect ");
  1573. #ifdef COMMENT
  1574.             if ( c == -3
  1575. #ifdef ultrix
  1576. /* This happens on Ultrix if there's no carrier */
  1577.             && errno != EIO
  1578. #endif /* ultrix */
  1579. #ifdef UTEK
  1580. /* This happens on UTEK if there's no carrier */
  1581.             && errno != EWOULDBLOCK
  1582. #endif /* UTEK */
  1583.             )
  1584.               perror("\r\nCan't read character");
  1585. #endif /* COMMENT */
  1586.         }
  1587. #ifdef NOSETBUF
  1588.         fflush(stdout);
  1589. #endif /* NOSETBUF */
  1590.         tthang();        /* Hang up the connection */
  1591.         debug(F111,"CONNECT i/o error 1",ck_errstr(),errno);
  1592.         goto conret0;
  1593.         }
  1594. #ifdef TNCODE
  1595.         tx = 0;
  1596.         if ((c == IAC) && network && (ttnproto == NP_TELNET)) {
  1597. #ifdef CK_ENCRYPTION
  1598.         int x_auth = TELOPT_ME(TELOPT_AUTHENTICATION);
  1599. #else
  1600.         int x_auth = 0;
  1601. #endif /* CK_ENCRYPTION */
  1602.         int me_bin = TELOPT_ME(TELOPT_BINARY);
  1603.         int u_bin = TELOPT_U(TELOPT_BINARY);
  1604.         debug(F100,"CONNECT got IAC","",0);
  1605.         ckcputf();        /* Dump screen-output buffer */
  1606.         if ((tx = tn_doop((CHAR)(c & 0xff),duplex,ckcgetc)) == 0) {
  1607.             if (me_bin != TELOPT_ME(TELOPT_BINARY)) {
  1608.             me_bin = TELOPT_ME(TELOPT_BINARY);
  1609.             } else if (u_bin != TELOPT_U(TELOPT_BINARY)) {
  1610.             u_bin = TELOPT_U(TELOPT_BINARY);
  1611. #ifdef CK_ENCRYPTION
  1612. /*
  1613.   Here we have to push back any bytes we have read using block reads, so we
  1614.   can read them again using single-character reads, so they can be decrypted
  1615.   in case there was a switch to encryption in the block.  Note that we can't
  1616.   handle switches in the encryption state itself this way -- which would be
  1617.   nice, since it would eliminate the need for single-character reads.  Why?
  1618.   Because if a series of characters has already been decrypted that shouldn't
  1619.   have been, then (a) it's ruined, and (b) so is the state of the decryption
  1620.   machine.  Too bad.
  1621. */
  1622.             } else if (TELOPT_ME(TELOPT_AUTHENTICATION) != 0 &&
  1623.                    TELOPT_ME(TELOPT_AUTHENTICATION) != x_auth
  1624.                    ) {
  1625.             if (ttpushback((CHAR *)ibp,ibc) > -1) {
  1626.                 ibc = 0;
  1627.                 ibp = ibuf;
  1628.             }
  1629. #endif /* CK_ENCRYPTION */
  1630.             }
  1631.             continue;
  1632.         } else if (tx == -1) {    /* I/O error */
  1633.             if (msgflg)
  1634.               printf("\r\nCommunications disconnect ");
  1635. #ifdef NOSETBUF
  1636.             fflush(stdout);
  1637. #endif /* NOSETBUF */
  1638.             debug(F111,"CONNECT i/o error 2",ck_errstr(),errno);
  1639.             goto conret0;
  1640.         } else if (tx == -2) {    /* I/O error */
  1641.             if (msgflg)
  1642.               printf("\r\nConnection closed by peer");
  1643. #ifdef NOSETBUF
  1644.             fflush(stdout);
  1645. #endif /* NOSETBUF */
  1646.             debug(F111,"CONNECT i/o error 3",ck_errstr(),errno);
  1647.             goto conret0;
  1648.         } else if (tx == -3) {    /* I/O error */
  1649.             if (msgflg)
  1650.               printf("\r\nConnection closed due to telnet policy");
  1651. #ifdef NOSETBUF
  1652.             fflush(stdout);
  1653. #endif /* NOSETBUF */
  1654.             debug(F111,"CONNECT i/o error 4",ck_errstr(),errno);
  1655.             goto conret0;
  1656.         } else if ((tx == 1) && (!duplex)) { /* ECHO change */
  1657.             duplex = 1;        /* Turn on local echo */
  1658.             continue;
  1659.         } else if ((tx == 2) && (duplex)) { /* ECHO change */
  1660.             duplex = 0;
  1661.             continue;
  1662.         } else if (tx == 3) {    /* Quoted IAC */
  1663.             c = parity ? 127 : 255;
  1664.         }
  1665. #ifdef IKS_OPTION
  1666.                 else if (tx == 4) {   /* IKS State Change */
  1667.                     if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start &&
  1668.             !tcp_incoming
  1669.             ) {
  1670.                         /* here we need to print a msg that the other */
  1671.                         /* side is in SERVER mode and that REMOTE     */
  1672.                         /* commands should be used.  And CONNECT mode */
  1673.                         /* should be ended.                           */
  1674.             active = 0;
  1675.                     }
  1676.                 }
  1677. #endif /* IKS_OPTION */
  1678.                 else if (tx == 6) {
  1679.                     /* DO LOGOUT was received */
  1680.             if (msgflg)
  1681.               printf("\r\nRemote Logout ");
  1682. #ifdef NOSETBUF
  1683.             fflush(stdout);
  1684. #endif /* NOSETBUF */
  1685.             debug(F100,"CONNECT Remote Logout","",0);
  1686.             goto conret0;
  1687.                 } else
  1688.           continue;        /* Negotiation OK, get next char. */
  1689.         } else if (parity)
  1690.           c &= 0x7f;
  1691.  
  1692.         /* I'm echoing for the remote */
  1693.             if (TELOPT_ME(TELOPT_ECHO) && tn_rem_echo)
  1694.           ttoc((char)c);
  1695.  
  1696. #endif /* TNCODE */
  1697.         if (debses) {        /* Output character to screen */
  1698.         char *s;        /* Debugging display... */
  1699.         s = dbchr(c);
  1700.         while (*s)
  1701.           ckcputc(*s++);
  1702.         } else {            /* Regular display ... */
  1703.         c &= cmask;        /* Apply Kermit-to-remote mask */
  1704.         if (seslog && sessft)    /* If binary session log */
  1705.           logchar((char)c);    /* log the character now. */
  1706. #ifndef NOXFER
  1707. #ifdef CK_AUTODL
  1708. /*
  1709.   Autodownload.  Check for Kermit S packet prior to translation, since that
  1710.   can change the packet and make it unrecognizable (as when the terminal
  1711.   character set is an ISO 646 one)...  Ditto for Zmodem start packet.
  1712. */
  1713.         if (autodl        /* Autodownload enabled? */
  1714. #ifdef IKS_OPTION
  1715.             || TELOPT_SB(TELOPT_KERMIT).kermit.me_start
  1716. #endif /* IKS_OPTION */
  1717.             ) {
  1718.             int k;
  1719.             k = kstart((CHAR)c); /* Kermit S or I packet? */
  1720. #ifdef CK_XYZ
  1721.             if (!k && zmdlok)    /* Or an "sz" start? */
  1722.               k = zstart((CHAR)c);
  1723. #endif /* CK_XYZ */
  1724.             if (k) {
  1725.             int ksign = 0;
  1726.             debug(F101,"CONNECT autodownload k","",k);
  1727.             if (k < 0) { /* Minus-Protocol? */
  1728. #ifdef NOSERVER
  1729.                 goto noserver; /* Need server mode for this */
  1730. #else
  1731.                 ksign = 1; /* Remember */
  1732.                 k = 0 - k; /* Convert to actual protocol */
  1733.                 justone = 1; /* Flag for protocol module */
  1734. #endif /* NOSERVER */
  1735.             } else
  1736.               justone = 0;
  1737.             k--;        /* Adjust [kz]start's return value */
  1738.             if (k == PROTO_K
  1739. #ifdef CK_XYZ
  1740.                 || k == PROTO_Z
  1741. #endif /* CK_XYZ */
  1742.                 ) {
  1743.                             /* Damage the packet so that it doesn't trigger */
  1744.                 /* autodownload detection downstream. */
  1745.                             if (k == PROTO_K) {
  1746.                                 int i, len = strlen((char *)ksbuf);
  1747.                                 for (i = 0; i < len; i++)
  1748.                   ckcputc(BS);
  1749.                             }
  1750. #ifdef CK_XYZ
  1751.                             else {
  1752.                                 int i;
  1753.                                 for (i = 0; i < 3; i++)
  1754.                   ckcputc(CAN);
  1755.                             }
  1756. #endif /* CK_XYZ */
  1757.  
  1758. #ifndef NOICP
  1759.                 sprintf(apcbuf,
  1760.                     "set proto %s, %s, set proto %s",
  1761.                     ptab[k].p_name,
  1762.                     ksign ? "server" : "receive",
  1763.                     ptab[protocol].p_name
  1764.                     );
  1765.                 apclength = strlen(apcbuf);
  1766.                 debug(F111,"CONNECT ksbuf",ksbuf,k);
  1767.                 debug(F110,"CONNECT autodownload",apcbuf,0);
  1768.                 apcactive = APC_LOCAL;
  1769.                 ckcputf();    /* Force screen update */
  1770.                 goto conret1;
  1771. #else
  1772. /*
  1773.   Here's another way that doesn't require APC, but then we'll have to change
  1774.   all the other CONNECT modules, and then the mainline code that calls them.
  1775. */
  1776.                 {
  1777.                 extern char sstate;
  1778.                 sstate = ksign ? 'x' : 'v';
  1779.                 proto();
  1780.                 }
  1781. #endif /* NOICP */
  1782.             }
  1783.             }
  1784.         }
  1785. #ifdef NOSERVER
  1786.           noserver:
  1787. #endif /* NOSERVER */
  1788.  
  1789. #endif /* CK_AUTODL */
  1790. #endif /* NOXFER */
  1791.         if (sosi) {        /* Handle SI/SO */
  1792.             if (c == SO) {    /* Shift Out */
  1793.             inshift = 1;
  1794.             continue;
  1795.             } else if (c == SI) { /* Shift In */
  1796.             inshift = 0;
  1797.             continue;
  1798.             }
  1799.             if (inshift) c |= 0200;
  1800.         }
  1801.         inxbuf[0] = c;        /* In case there is no translation */
  1802.         inxcount = 1;        /* ... */
  1803. #ifndef NOCSETS
  1804.         if (inesc[0] == ES_NORMAL /* If not in an escape sequence */
  1805.             && !printing    /* and not in transparent print */
  1806.             ) {            /* Translate character sets */
  1807. #ifdef UNICODE
  1808.             int x;
  1809.             if (unicode == 1) {    /* Remote is UTF-8 */
  1810.             x = u_to_b((CHAR)c);
  1811.             if (x == -1)
  1812.               continue;
  1813.             else if (x == -2) { /* LS or PS */
  1814.                 inxbuf[0] = CR;
  1815.                 inxbuf[1] = LF;
  1816.                 inxcount = 2;
  1817.             } else if (x == -9) { /* UTF-8 error */
  1818.                 inxbuf[0] = '?';
  1819.                 inxbuf[1] = u_to_b2();
  1820.                 inxcount = 2;
  1821.             } else {
  1822.                 inxbuf[0] = (unsigned)(x & 0xff);
  1823.             }
  1824.             c = inxbuf[0];
  1825.             } else if (unicode == 2) { /* Local is UTF-8 */
  1826.             inxcount = b_to_u((CHAR)c,inxbuf,OUTXBUFSIZ,tcssize);
  1827.             c = inxbuf[0];
  1828.             } else {
  1829. #endif /* UNICODE */
  1830.             if (sxi) c = (*sxi)((CHAR)c);
  1831.             if (rxi) c = (*rxi)((CHAR)c);
  1832.             inxbuf[0] = c;
  1833. #ifdef UNICODE
  1834.             }
  1835. #endif /* UNICODE */
  1836.         }
  1837. #endif /* NOCSETS */
  1838.  
  1839. #ifndef NOESCSEQ
  1840.         if (escseq) {        /* If handling escape sequences */
  1841.             oldprt = printing;         /* remember printer state */
  1842.             apcrc = chkaes((char)c,0); /* and update escseq state. */
  1843.             if (printing && !oldprt) /* If printer was turned on */
  1844.               continue;        /* don't print final char of escseq */
  1845.         }
  1846. #ifdef CK_APC
  1847. /*
  1848.   If we are handling APCs, we have several possibilities at this point:
  1849.    1. Ordinary character to be written to the screen.
  1850.    2. An Esc; we can't write it because it might be the beginning of an APC.
  1851.    3. The character following an Esc, in which case we write Esc, then char,
  1852.       but only if we have not just entered an APC sequence.
  1853. */
  1854.         if (escseq && apcstatus != APC_OFF) {
  1855.             if (inesc[0] == ES_GOTESC) /* Don't write ESC yet */
  1856.               continue;
  1857.             else if (oldesc[0] == ES_GOTESC && !apcactive) {
  1858.             debug(F100,"XXX WRITING ESC","",0);
  1859.             ckcputc(ESC);    /* Write saved ESC */
  1860.             if (seslog && !sessft) logchar((char)ESC);
  1861.             } else if (apcrc) {    /* We have an APC */
  1862.             debug(F111,"CONNECT APC complete",apcbuf,apclength);
  1863.             ckcputf();    /* Force screen update */
  1864.             goto conret1;
  1865.             }
  1866.         }
  1867. #endif /* CK_APC */
  1868. #endif /* NOESCSEQ */
  1869.  
  1870.         debug(F111,"INXBUF",inxbuf,inxcount);
  1871.         for (i = 0; i < inxcount; i++) { /* Loop thru */
  1872.             c = inxbuf[i];    /* input expansion buffer... */
  1873.             if (
  1874. #ifdef CK_APC
  1875.             !apcactive &&    /* Don't display APC sequences */
  1876. #endif /* CK_APC */
  1877.             !printing    /* or transparent print material */
  1878.  
  1879.             ) {
  1880.             c &= cmdmsk;    /* Apply command mask. */
  1881.             if (c == CR && tt_crd) { /* SET TERM CR-DISPLA CRLF? */
  1882.                 ckcputc(c);    /* Yes, output CR */
  1883.                 if (seslog && !sessft) logchar((char)c);
  1884.                 c = LF;    /* and insert a linefeed */
  1885.             }
  1886.             ckcputc(c);    /* Write character to screen */
  1887.             }
  1888.             if (seslog && !sessft) /* Handle session log. */
  1889.               logchar((char)c);
  1890. #ifdef XPRINT
  1891.         if (printing && !inesc[0]) { /* Do transparent printing. */
  1892.             /* zchout() can't be used because */
  1893.             /* it's buffered differently. */
  1894.             cbuf[0] = c;
  1895.             zsoutx(ZMFILE,(char *)cbuf,1);
  1896.         }
  1897. #endif /* XPRINT */
  1898.  
  1899. #ifdef CK_TRIGGER
  1900.             /* Check for trigger string */
  1901.             if (tt_trigger[0]) {
  1902.             int i;
  1903.             if ((i = autoexitchk((CHAR)c)) > -1) {
  1904.                 makestr(&triggerval,tt_trigger[i]);
  1905.                 ckcputf();    /* Force screen update */
  1906. #ifdef NOSETBUF
  1907.                 fflush(stdout); /* I mean really force it */
  1908. #endif /* NOSETBUF */
  1909.                 goto conret1;
  1910.             }
  1911.             }
  1912. #endif /* CK_TRIGGER */
  1913.         }
  1914.         }
  1915.     }
  1916. #ifndef BEBOX
  1917.     if (FD_ISSET(scrnout, &out)) {
  1918.         FD_CLR(scrnout, &out);
  1919.     }
  1920. #endif /* BEBOX */
  1921.     } /* End of big loop */
  1922.   conret1:                /* Come here to succeed */
  1923.     rc = 1;
  1924.   conret0:                /* Common exit point */
  1925. #ifdef BEBOX
  1926.     {
  1927.     long ret_val;
  1928.     closesocket(pair[0]);
  1929.     closesocket(pair[1]);
  1930.     x = kill(tid,SIGKILLTHR);    /* Kill thread */
  1931.     wait_for_thread (tid, &ret_val);
  1932.     }
  1933. #endif /* BEBOX */
  1934.     conres();
  1935.     if (dohangup > 0) {
  1936. #ifndef NODIAL
  1937.     extern int dialmhu;
  1938. #endif /* NODIAL */
  1939. #ifdef NETCONN
  1940.     if (network)
  1941.       ttclos(0);
  1942. #endif /* NETCONN */
  1943.  
  1944. #ifndef COMMENT
  1945. /*
  1946.   This is bad because if they said SET MODEM HANGUP-METHOD MODEM-COMMAND,
  1947.   they mean it -- we shouldn't fall back on tthang() if mdmhup() fails,
  1948.   because maybe they have some special kind of connection.  On the other
  1949.   hand, making this change prevents dialing from working at all in some
  1950.   cases.  Further study needed.
  1951. */
  1952. #ifndef NODIAL
  1953.     if (dohangup > 1)        /* User asked for it */
  1954.       if (mdmhup() < 1)        /* Maybe hang up via modem */
  1955. #endif /* NODIAL */
  1956.         tthang();            /* And make sure we don't hang up */
  1957. #else
  1958.     if (!network) {            /* Serial connection. */
  1959.         if (dialmhu)        /* Hang up the way they said. */
  1960.           mdmhup();
  1961.         else
  1962.           tthang();
  1963.     }
  1964. #endif /* COMMENT */
  1965.     dohangup = 0;            /* again unless requested again. */
  1966.     }
  1967.     if (quitnow)            /* Exit now if requested. */
  1968.       doexit(GOOD_EXIT,xitsta);
  1969.     if (msgflg
  1970. #ifdef CK_APC
  1971.     && !apcactive
  1972. #endif /* CK_APC */
  1973.     )
  1974.       printf("(Back at %s)", *myhost ? myhost : "local UNIX system");
  1975. #ifdef CK_APC
  1976.     if (!apcactive)
  1977. #endif /* CK_APC */
  1978.       printf("\n");
  1979.     what = W_NOTHING;            /* So console modes set right. */
  1980. #ifndef NOCSETS
  1981.     language = langsv;            /* Restore language */
  1982. #endif /* NOCSETS */
  1983. #ifdef CK_APC
  1984.     debug(F101,"CONNECT exit apcactive","",apcactive);
  1985.     debug(F101,"CONNECT exit justone","",justone);
  1986. #endif /* CK_APC */
  1987.     if (msgflg) {
  1988. #ifdef CK_APC
  1989.     if (apcactive == APC_LOCAL)
  1990.       printf("\n");
  1991. #endif /* CK_APC */
  1992.     printf("----------------------------------------------------\n");
  1993.     printbar = 1;
  1994.     } else
  1995.     printbar = 0;
  1996.     fflush(stdout);
  1997.     return(1);
  1998. }
  1999.  
  2000. /*  H C O N N E  --  Give help message for connect.  */
  2001.  
  2002. #define CXM_SER 1            /* Serial connections only */
  2003. #define CXM_NET 2            /* Network only (but not Telnet) */
  2004. #define CXM_TEL 4            /* Telnet only */
  2005.  
  2006. static struct hmsgtab {
  2007.     char * hmsg;
  2008.     int hflags;
  2009. } hlpmsg[] = {
  2010.     "  ? or H for this message",                0,
  2011.     "  0 (zero) to send the NUL (0) character", 0,
  2012.     "  B to send a BREAK signal (0.275sec)",  CXM_SER,
  2013. #ifdef NETCONN
  2014.     "  B to send a network BREAK",            CXM_NET,
  2015.     "  B to send a Telnet BREAK",             CXM_TEL,
  2016. #endif /* NETCONN */
  2017. #ifdef CK_LBRK
  2018.     "  L to send a Long BREAK (1.5sec)",      CXM_SER,
  2019. #endif /* CK_LBRK */
  2020. #ifdef NETCONN
  2021.     "  I to send a network interrupt packet", CXM_NET,
  2022.     "  I to send a Telnet Interrupt request", CXM_TEL,
  2023. #ifdef TNCODE
  2024.     "  A to send Telnet Are-You-There?",      CXM_TEL,
  2025. #endif /* TNCODE */
  2026. #endif /* NETCONN */
  2027.     "  U to hangup and close the connection", 0,
  2028.     "  Q to hangup and quit Kermit",          0,
  2029.     "  S for status",                         0,
  2030. #ifdef NOPUSH
  2031.     "  ! to push to local shell (disabled)",  0,
  2032.     "  Z to suspend (disabled)",              0,
  2033. #else
  2034.     "  ! to push to local shell",             0,
  2035. #ifdef NOJC
  2036.     "  Z to suspend (disabled)",              0,
  2037. #else
  2038.     "  Z to suspend",                         0,
  2039. #endif /* NOJC */
  2040. #endif /* NOPUSH */
  2041.     "  \\ backslash code:",                   0,
  2042.     "    \\nnn  decimal character code",      0,
  2043.     "    \\Onnn octal character code",        0,
  2044.     "    \\Xhh  hexadecimal character code;", 0,
  2045.     "    terminate with Carriage Return.",    0,
  2046.     "  Type the escape character again to send the escape character itself,",0,
  2047.     "  or press the space-bar to resume the CONNECT session.",               0,
  2048.     NULL, 0
  2049. };
  2050.  
  2051. int
  2052. hconne() {
  2053.     int c, i, cxtype;
  2054.     if (network)
  2055.       cxtype = (ttnproto == NP_TELNET) ? CXM_TEL : CXM_NET;
  2056.     else
  2057.       cxtype = CXM_SER;
  2058.  
  2059.     conol("\r\n----------------------------------------------------\r\n");
  2060.     conoll("Press:");
  2061.     conol("  C to return to ");
  2062.     conoll(*myhost ? myhost : "the C-Kermit prompt");
  2063.     for (i = 0; hlpmsg[i].hmsg; i++) {
  2064.     if (!(hlpmsg[i].hflags) || (hlpmsg[i].hflags == cxtype))
  2065.       conoll(hlpmsg[i].hmsg);
  2066.     }
  2067.     conol("Press a key>");        /* Prompt for command. */
  2068.     c = CONGKS() & 0177;        /* Get character, strip any parity. */
  2069.     /* No key mapping or translation here */
  2070.     if (c != CMDQ)
  2071.       conoll("");
  2072.     conoll("----------------------------------------------------");
  2073.     return(c);                /* Return it. */
  2074. }
  2075.  
  2076.  
  2077. /*  D O E S C  --  Process an escape character argument  */
  2078.  
  2079. VOID
  2080. #ifdef CK_ANSIC
  2081. doesc(char c)
  2082. #else
  2083. doesc(c) char c;
  2084. #endif /* CK_ANSIC */
  2085. /* doesc */ {
  2086.     CHAR d;
  2087.  
  2088.     debug(F101,"CONNECT doesc","",c);
  2089.     while (1) {
  2090.     if (c == escape) {        /* Send escape character */
  2091.         d = dopar((CHAR) c); ttoc((char) d); return;
  2092.         } else                /* Or else look it up below. */
  2093.         if (isupper(c)) c = tolower(c);
  2094.  
  2095.     switch(c) {
  2096.  
  2097.       case 'c':            /* Escape back to prompt */
  2098.       case '\03':
  2099. #ifdef NOICP
  2100.         conoll("");
  2101.         conoll("");
  2102.         conoll(
  2103. "  WARNING: This version of C-Kermit has no command processor to escape"
  2104.            );
  2105.         conoll(
  2106. "  back to.  To return to your local system, log out from the remote and/or"
  2107.            );
  2108.         conoll(
  2109. "  use the escape character followed by the letter U to close (hang Up) the"
  2110.            );
  2111.         conoll(
  2112. "  connection.  Resuming your session..."
  2113.            );
  2114.         conoll("");
  2115.         return;
  2116. #else
  2117.         active = 0; conol("\r\n"); return;
  2118. #endif /* NOICP */
  2119.  
  2120.       case 'b':            /* Send a BREAK signal */
  2121.       case '\02':
  2122.         ttsndb(); return;
  2123.  
  2124. #ifdef NETCONN
  2125.       case 'i':            /* Send Interrupt */
  2126.       case '\011':
  2127. #ifdef TCPSOCKET
  2128. #ifndef IP
  2129. #define IP 244
  2130. #endif /* IP */
  2131.         if (network && ttnproto == NP_TELNET) { /* TELNET */
  2132.         temp[0] = (CHAR) IAC;    /* I Am a Command */
  2133.         temp[1] = (CHAR) IP;    /* Interrupt Process */
  2134.         temp[2] = NUL;
  2135.         ttol((CHAR *)temp,2);
  2136.         } else
  2137. #endif /* TCPSOCKET */
  2138.           conoc(BEL);
  2139.         return;
  2140.  
  2141. #ifdef TCPSOCKET
  2142.       case 'a':            /* "Are You There?" */
  2143.       case '\01':
  2144. #ifndef AYT
  2145. #define AYT 246
  2146. #endif /* AYT */
  2147.         if (network && ttnproto == NP_TELNET) {
  2148.         temp[0] = (CHAR) IAC;    /* I Am a Command */
  2149.         temp[1] = (CHAR) AYT;    /* Are You There? */
  2150.         temp[2] = NUL;
  2151.         ttol((CHAR *)temp,2);
  2152.         } else conoc(BEL);
  2153.         return;
  2154. #endif /* TCPSOCKET */
  2155. #endif /* NETCONN */
  2156.  
  2157. #ifdef CK_LBRK
  2158.       case 'l':            /* Send a Long BREAK signal */
  2159.         ttsndlb(); return;
  2160. #endif /* CK_LBRK */
  2161.  
  2162.       case 'u':            /* Hangup */
  2163.        /* case '\010': */        /* No, too dangerous */
  2164.         dohangup = 2; active = 0; conol("\r\nHanging up "); return;
  2165.  
  2166.       case 'q':            /* Quit */
  2167.         dohangup = 2; quitnow = 1; active = 0; conol("\r\n"); return;
  2168.  
  2169.       case 's':            /* Status */
  2170.         conoll("");
  2171.         conoll("----------------------------------------------------");
  2172. #ifdef PTYORPIPE
  2173.         if (ttpipe)
  2174.           sprintf(temp, " Pipe: \"%s\"", ttname);
  2175.         else if (ttpty)
  2176.           sprintf(temp, " Pty: \"%s\"", ttname);
  2177.         else
  2178. #endif /* PTYORPIPE */
  2179.           sprintf(temp, " %s: %s", (network ? "Host" : "Device"), ttname);
  2180.         conoll(temp);
  2181.         if (!network && speed >= 0L) {
  2182.         sprintf(temp,"Speed %ld", speed);
  2183.         conoll(temp);
  2184.         }
  2185.         sprintf(temp," Terminal echo: %s", duplex ? "local" : "remote");
  2186.         conoll(temp);
  2187.         sprintf(temp," Terminal bytesize: %d", (cmask  == 0177) ? 7 : 8);
  2188.         conoll(temp);
  2189.         sprintf(temp," Command bytesize: %d", (cmdmsk == 0177) ? 7 : 8 );
  2190.         conoll(temp);
  2191.         sprintf(temp," Parity: %s", parnam(parity));
  2192.         conoll(temp);
  2193. #ifndef NOXFER
  2194.         sprintf(temp," Autodownload: %s", autodl ? "on" : "off");
  2195.         conoll(temp);
  2196. #endif /* NOXFER */
  2197.         sprintf(temp," Session log: %s", *sesfil ? sesfil : "(none)");
  2198.         conoll(temp);
  2199. #ifndef NOSHOW
  2200.         if (!network) shomdm();
  2201. #endif /* NOSHOW */
  2202. #ifdef CKLOGDIAL
  2203.         {
  2204.         long z;
  2205.         z = dologshow(0);
  2206.         if (z > -1L) {
  2207.             sprintf(temp," Elapsed time: %s",hhmmss(z));
  2208.             conoll(temp);
  2209.         }
  2210.         }
  2211. #endif /* CKLOGDIAL */
  2212.         conoll("----------------------------------------------------");
  2213.         return;
  2214.  
  2215.       case 'h':            /* Help */
  2216.       case '?':            /* Help */
  2217.         c = hconne(); continue;
  2218.  
  2219.       case '0':            /* Send a null */
  2220.         c = '\0'; d = dopar((CHAR) c); ttoc((char) d); return;
  2221.  
  2222.       case 'z': case '\032':    /* Suspend */
  2223. #ifndef NOPUSH
  2224.         if (!nopush)
  2225.           stptrap(0);
  2226.         else
  2227.           conoc(BEL);
  2228. #else
  2229.         conoc(BEL);
  2230. #endif /* NOPUSH */
  2231.         return;
  2232.  
  2233.       case '@':            /* Start inferior command processor */
  2234.       case '!':
  2235. #ifndef NOPUSH
  2236.         if (!nopush) {
  2237.         conres();              /* Put console back to normal */
  2238.         zshcmd("");              /* Fork a shell. */
  2239.         if (conbin((char)escape) < 0) {
  2240.             printf("Error resuming CONNECT session\n");
  2241.             active = 0;
  2242.         }
  2243.         } else conoc(BEL);
  2244. #else
  2245.         conoc(BEL);
  2246. #endif /* NOPUSH */
  2247.         return;
  2248.  
  2249.       case SP:            /* Space, ignore */
  2250.         return;
  2251.  
  2252.       default:            /* Other */
  2253.         if (c == CMDQ) {        /* Backslash escape */
  2254.         int x;
  2255.         ecbp = ecbuf;
  2256.         *ecbp++ = c;
  2257.         while (((c = (CONGKS() & cmdmsk)) != '\r') && (c != '\n'))
  2258.           *ecbp++ = c;
  2259.         *ecbp = NUL; ecbp = ecbuf;
  2260.         x = xxesc(&ecbp);    /* Interpret it */
  2261.         if (x >= 0) {        /* No key mapping here */
  2262.             c = dopar((CHAR) x);
  2263.             ttoc((char) c);
  2264.             return;
  2265.         } else {        /* Invalid backslash code. */
  2266.             conoc(BEL);
  2267.             return;
  2268.         }
  2269.         }
  2270.         conoc(BEL); return;     /* Invalid esc arg, beep */
  2271.         }
  2272.     }
  2273. }
  2274. #endif /* NOLOCAL */
  2275.