home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / tmp4 / ckucns.c < prev    next >
C/C++ Source or Header  |  2009-10-22  |  81KB  |  2,719 lines

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