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

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