home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / ckucns.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  78KB  |  2,694 lines

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