home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc190.zip / ckucon.c < prev    next >
C/C++ Source or Header  |  1994-10-01  |  62KB  |  1,901 lines

  1. #include "ckcsym.h"
  2. #ifdef NOLOCAL
  3. char *connv = "";
  4. #else
  5. char *connv = "CONNECT Command for UNIX, 5A(073) 15 Sep 94";
  6.  
  7. /*  C K U C O N  --  Terminal connection to remote system, for UNIX  */
  8. /*
  9.   Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  10.   Columbia University Academic Information Systems, New York City.
  11.  
  12.   Copyright (C) 1985, 1994, Trustees of Columbia University in the City of New
  13.   York.  The C-Kermit software may not be, in whole or in part, licensed or
  14.   sold for profit as a software product itself, nor may it be included in or
  15.   distributed with commercial products or otherwise distributed by commercial
  16.   concerns to their clients or customers without written permission of the
  17.   Office of Kermit Development and Distribution, Columbia University.  This
  18.   copyright notice must not be removed, altered, or obscured.
  19.  
  20.   Special thanks to Eduard Vopicka in Prague, Czech Republic, for valuable
  21.   contributions to this module in July 1994.
  22. */
  23. #include "ckcdeb.h"            /* Common things first */
  24.  
  25. #ifdef NEXT
  26. #undef NSIG
  27. #include <sys/wait.h>            /* For wait() */
  28. #endif /* NEXT */
  29.  
  30. #include <signal.h>            /* Signals */
  31. #include <errno.h>            /* Error numbers */
  32.  
  33. #ifdef ZILOG                /* Longjumps */
  34. #include <setret.h>
  35. #else
  36. #include <setjmp.h>
  37. #endif /* ZILOG */
  38.  
  39. /* Kermit-specific includes */
  40.  
  41. #include "ckcasc.h"            /* ASCII characters */
  42. #include "ckcker.h"            /* Kermit things */
  43. #include "ckucmd.h"            /* For xxesc() prototype */
  44. #include "ckcnet.h"            /* Network symbols */
  45. #ifndef NOCSETS
  46. #include "ckcxla.h"            /* Character set translation */
  47. #endif /* NOCSETS */
  48.  
  49. /* Internal function prototypes */
  50.  
  51. _PROTOTYP( VOID ttflux, (void) );
  52. _PROTOTYP( VOID doesc, (char) );
  53. _PROTOTYP( static VOID logchar, (char) );
  54. _PROTOTYP( int hconne, (void) );
  55. _PROTOTYP( VOID shomdm, (void) );
  56. _PROTOTYP( static int kbget, (void) );
  57.  
  58. /*
  59.   For inter-fork signaling.  Normally we use SIGUSR1, except on SCO, where 
  60.   we use SIGUSR2 because SIGUSR1 is used by the system.  You can define
  61.   CK_FORK_SIG to be whatever other signal you might want to use at compile
  62.   time.  We don't use SIGUSR2 everywhere because in some systems, like
  63.   UnixWare, the default action for SIGUSR2 is to killthe process that gets it.
  64. */
  65. #ifndef CK_FORK_SIG
  66.  
  67. #ifndef SIGUSR1                /* User-defined signals */
  68. #define SIGUSR1 30
  69. #endif /* SIGUSR1 */
  70.  
  71. #ifndef SIGUSR2
  72. #define SIGUSR2 31
  73. #endif /* SIGUSR2 */
  74.  
  75. #ifdef M_UNIX
  76. #define CK_FORK_SIG SIGUSR2        /* SCO - use SIGUSR2 */
  77. #else
  78. #define CK_FORK_SIG SIGUSR1        /* Others - use SIGUSR1 */
  79. #endif /* M_UNIX */
  80.  
  81. #endif /* CK_FORK_SIG */
  82.  
  83. /* External variables */
  84.  
  85. extern int local, escape, duplex, parity, flow, seslog, sessft, debses,
  86.  mdmtyp, ttnproto, cmask, cmdmsk, network, nettype, deblog, sosi, tnlm,
  87.  xitsta, what, ttyfd, quiet, backgrd, pflag, tt_crd, tn_nlm, ttfdflg;
  88. extern long speed;
  89. extern char ttname[], sesfil[], myhost[], *ccntab[];
  90.  
  91. #ifdef CK_APC
  92. extern int apcactive;            /* Application Program Command (APC) */
  93. extern int apcstatus;            /* items ... */
  94. static int apclength = 0;          
  95. #ifdef DCMDBUF
  96. extern char *apcbuf;
  97. #else
  98. extern char apcbuf[];
  99. #endif /* DCMDBUF */
  100. static int apcbuflen = APCBUFLEN - 2;
  101. #endif /* CK_APC */
  102.  
  103. #ifndef NOSETKEY            /* Keyboard mapping */
  104. extern KEY *keymap;            /* Single-character key map */
  105. extern MACRO *macrotab;            /* Key macro pointer table */
  106. static MACRO kmptr = NULL;        /* Pointer to current key macro */
  107. #endif /* NOSETKEY */
  108.  
  109. /* Global variables local to this module */
  110.  
  111. static int quitnow = 0,            /* <esc-char>Q was typed */
  112.   dohangup = 0,                /* <esc-char>H was typed */
  113.   sjval,                /* Setjump return value */
  114.   goterr = 0,                /* Fork/pipe creation error flag */
  115. #ifndef SUNX25
  116.   active = 0,                /* Lower fork active flag */
  117. #endif /* SUNX25 */
  118.   inshift = 0,                /* SO/SI shift states */
  119.   outshift = 0;
  120.  
  121. static PID_T parent_id = (PID_T)0;    /* Process id of keyboard fork */
  122.  
  123. static char ecbuf[10], *ecbp;        /* Escape char buffer & pointer */
  124.  
  125. #ifdef pdp11
  126. #define IBUFL 1536            /* Input buffer length */
  127. #else
  128. #define IBUFL 4096
  129. #endif /* pdp11 */
  130.  
  131. static int obc = 0;            /* Output buffer count */
  132.  
  133. #ifndef OXOS
  134. #define OBUFL 1024            /* Output buffer length */
  135. #else
  136. #define OBUFL IBUFL
  137. #endif /* OXOS */
  138.  
  139. #define TMPLEN 200            /* Temporary message buffer length */
  140. #ifdef DYNAMIC
  141. static char *ibuf = NULL, *obuf = NULL, *temp = NULL; /* Buffers */
  142. #else
  143. static char ibuf[IBUFL], obuf[OBUFL], temp[TMPLEN];
  144. #endif /* DYNAMIC */
  145.  
  146. #ifdef DYNAMIC
  147. static char *ibp;            /* Input buffer pointer */
  148. #else
  149. static char *ibp = ibuf;        /* Input buffer pointer */
  150. #endif /*DYNAMIC */
  151. static int ibc = 0;            /* Input buffer count */
  152.  
  153. #ifdef DYNAMIC
  154. static char *obp;            /* Output buffer pointer */
  155. #else
  156. static char *obp = obuf;        /* Output buffer pointer */
  157. #endif /* DYNAMIC */
  158.  
  159. /* SunLink X.25 items */
  160.  
  161. #ifdef SUNX25
  162. static char *p;                /* General purpose pointer */
  163. char x25ibuf[MAXIX25];            /* Input buffer */
  164. char x25obuf[MAXOX25];            /* Output buffer */
  165. int active = 0;                /* Lower fork active flag */
  166. int ibufl;                /* Length of input buffer */
  167. int obufl;                /* Length of output buffer */
  168. unsigned char tosend = 0;
  169. int linkid, lcn;
  170. static int dox25clr = 0;
  171. CHAR padparms[MAXPADPARMS+1];
  172. #endif /* SUNX25 */
  173.  
  174. static int xpipe[2] = {-1, -1};    /* Pipe descriptor for child-parent messages */
  175. static PID_T pid;        /* Process ID of child */
  176.  
  177. /* Character-set items */
  178.  
  179. #ifndef NOCSETS
  180. #ifdef CK_ANSIC /* ANSI C prototypes... */
  181. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Character set */
  182. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* translation functions */
  183. static CHAR (*sxo)(CHAR);    /* Local translation functions */
  184. static CHAR (*rxo)(CHAR);    /* for output (sending) terminal chars */
  185. static CHAR (*sxi)(CHAR);    /* and for input (receiving) terminal chars. */
  186. static CHAR (*rxi)(CHAR);
  187. #else /* Not ANSI C... */
  188. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])();    /* Character set */
  189. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])();    /* translation functions. */
  190. static CHAR (*sxo)();        /* Local translation functions */
  191. static CHAR (*rxo)();        /* for output (sending) terminal chars */
  192. static CHAR (*sxi)();        /* and for input (receiving) terminal chars. */
  193. static CHAR (*rxi)();
  194. #endif /* CK_ANSIC */
  195. extern int language;        /* Current language. */
  196. static int langsv;        /* For remembering language setting. */
  197. extern struct csinfo fcsinfo[]; /* File character set info. */
  198. extern int tcsr, tcsl;        /* Terminal character sets, remote & local. */
  199. static int tcs;            /* Intermediate ("transfer") character set. */
  200. #endif /* NOCSETS */
  201.  
  202. /*
  203.   We do not need to parse and recognize escape sequences if we are being built
  204.   without character-set support AND without APC support.
  205. */
  206. #ifdef NOCSETS                /* No character sets */
  207. #ifndef CK_APC                /* No APC */
  208. #ifndef NOESCSEQ
  209. #define NOESCSEQ            /* So no escape sequence recognizer */
  210. #endif /* NOESCSEQ */
  211. #endif /* CK_APC */
  212. #endif /* NOCSETS */
  213. /*
  214.   Child process events / messages
  215. */
  216. #define CEV_NO  0            /* No event */
  217. #define CEV_HUP 1            /* Communications hangup */
  218. #define CEV_PAD 2            /* X.25 - change PAD parameters */
  219. #define CEV_DUP 3            /* Toggle duplex */
  220. #define CEV_APC 4            /* Execute APC */
  221.  
  222. #ifdef NOESCSEQ
  223. #define chkaes(x) 0
  224. #else
  225. /*
  226.   As of edit 178, the CONNECT command will skip past ANSI escape sequences
  227.   to avoid translating the characters within them.  This allows the CONNECT
  228.   command to work correctly when connected to a remote host that uses a
  229.   7-bit ISO 646 national character set, in which characters like '[' would
  230.   normally be translated into accented characters, ruining the terminal's
  231.   interpretation (and generation) of escape sequences.
  232.  
  233.   As of edit 190, the CONNECT command responds to APC escape sequences
  234.   (ESC _ text ESC \) if the user SETs TERMINAL APC ON or UNCHECKED, and the
  235.   program was built with CK_APC defined.
  236.  
  237.   Non-ANSI/ISO-compliant escape sequences are not handled.
  238. */
  239.  
  240. /*
  241.   States for the escape-sequence recognizer.
  242. */
  243. #define ES_NORMAL 0            /* Normal, not in an escape sequence */
  244. #define ES_GOTESC 1            /* Current character is ESC */
  245. #define ES_ESCSEQ 2            /* Inside an escape sequence */
  246. #define ES_GOTCSI 3            /* Inside a control sequence */
  247. #define ES_STRING 4            /* Inside DCS,OSC,PM, or APC string */
  248. #define ES_TERMIN 5            /* 1st char of string terminator */
  249.  
  250. static int
  251.   escseq = 0,                /* 1 = Recognizer is active */
  252.   inesc = ES_NORMAL,            /* State of sequence recognizer */
  253.   oldesc = -1;                /* Previous state of recognizer */
  254. /*
  255.   ANSI escape sequence handling.  Only the 7-bit form is treated, because
  256.   translation is not a problem in the 8-bit environment, in which all GL
  257.   characters are ASCII and no translation takes place.  So we don't check
  258.   for the 8-bit single-character versions of CSI, DCS, OSC, APC, or ST.
  259.   Here is the ANSI sequence recognizer state table, followed by the code
  260.   that implements it.
  261.  
  262.   Definitions:
  263.     CAN = Cancel                       01/08         Ctrl-X
  264.     SUB = Substitute                   01/10         Ctrl-Z
  265.     DCS = Device Control Sequence      01/11 05/00   ESC P
  266.     CSI = Control Sequence Introducer  01/11 05/11   ESC [
  267.     ST  = String Terminator            01/11 05/12   ESC \
  268.     OSC = Operating System Command     01/11 05/13   ESC ]
  269.     PM  = Privacy Message              01/11 05/14   ESC ^
  270.     APC = Application Program Command  01/11 05/15   ESC _
  271.  
  272.   ANSI escape sequence recognizer:
  273.  
  274.     State    Input  New State  ; Commentary
  275.  
  276.     NORMAL   (start)           ; Start in NORMAL state
  277.  
  278.     (any)    CAN    NORMAL     ; ^X cancels
  279.     (any)    SUB    NORMAL     ; ^Z cancels
  280.  
  281.     NORMAL   ESC    GOTESC     ; Begin escape sequence
  282.     NORMAL   other             ; NORMAL control or graphic character
  283.  
  284.     GOTESC   ESC               ; Start again
  285.     GOTESC   [      GOTCSI     ; CSI
  286.     GOTESC   P      STRING     ; DCS introducer, consume through ST
  287.     GOTESC   ]      STRING     ; OSC introducer, consume through ST
  288.     GOTESC   ^      STRING     ; PM  introducer, consume through ST
  289.     GOTESC   _      STRING     ; APC introducer, consume through ST
  290.     GOTESC   0..~   NORMAL     ; 03/00 through 17/14 = Final character
  291.     GOTESC   other  ESCSEQ     ; Intermediate or ignored control character
  292.  
  293.     ESCSEQ   ESC    GOTESC     ; Start again
  294.     ESCSEQ   0..~   NORMAL     ; 03/00 through 17/14 = Final character
  295.     ESCSEQ   other             ; Intermediate or ignored control character
  296.  
  297.     GOTCSI   ESC    GOTESC     ; Start again
  298.     GOTCSI   @..~   NORMAL     ; 04/00 through 17/14 = Final character
  299.     GOTCSI   other             ; Intermediate char or ignored control char
  300.  
  301.     STRING   ESC    TERMIN     ; Maybe have ST
  302.     STRING   other             ; Consume all else
  303.  
  304.     TERMIN   \      NORMAL     ; End of string
  305.     TERMIN   other  STRING     ; Still in string
  306. */
  307. /*
  308.   chkaes() -- Check ANSI Escape Sequence.
  309.   Call with EACH character in input stream.
  310.   Sets global inesc variable according to escape sequence state.
  311.   Returns 0 normally, 1 if an APC sequence is to be executed.
  312. */
  313. int
  314. #ifdef CK_ANSIC
  315. chkaes(char c)
  316. #else
  317. chkaes(c) char c;
  318. #endif /* CK_ANSIC */
  319. /* chkaes */ {
  320.  
  321.     oldesc = inesc;            /* Remember previous state */
  322.     if (c == CAN || c == SUB)        /* CAN and SUB cancel any sequence */
  323.       inesc = ES_NORMAL;
  324.     else                /* Otherwise */
  325.       switch (inesc) {            /* enter state switcher */
  326.  
  327.     case ES_NORMAL:            /* NORMAL state */
  328.       if (c == ESC)            /* Got an ESC */
  329.         inesc = ES_GOTESC;        /* Change state to GOTESC */
  330.       break;            /* Otherwise stay in NORMAL state */
  331.  
  332.     case ES_GOTESC:            /* GOTESC state */
  333.       if (c == '[')            /* Left bracket after ESC is CSI */
  334.         inesc = ES_GOTCSI;        /* Change to GOTCSI state */
  335.       else if (c == 'P' || (c > 0134 && c < 0140)) { /* P, [, ^, or _ */
  336.           inesc = ES_STRING;    /* Switch to STRING-absorption state */
  337. #ifdef CK_APC
  338.           if (c == '_' && pid == 0 && /* APC handled in child only */
  339.           apcstatus != APC_OFF) { 
  340.           debug(F100,"APC begin","",0);
  341.           apcactive = 1;    /* Set APC-Active flag */
  342.           apclength = 0;    /* and reset APC buffer pointer */
  343.           }
  344. #endif /* CK_APC */
  345.       } else if (c > 057 && c < 0177) /* Final character '0' thru '~' */
  346.         inesc = ES_NORMAL;        /* Back to normal */
  347.       else if (c != ESC)        /* ESC in an escape sequence... */
  348.         inesc = ES_ESCSEQ;        /* starts a new escape sequence */
  349.       break;            /* Intermediate or ignored ctrl char */
  350.  
  351.     case ES_ESCSEQ:            /* ESCSEQ -- in an escape sequence */
  352.       if (c > 057 && c < 0177)    /* Final character '0' thru '~' */
  353.         inesc = ES_NORMAL;        /* Return to NORMAL state. */
  354.       else if (c == ESC)        /* ESC ... */
  355.         inesc = ES_GOTESC;        /* starts a new escape sequence */
  356.       break;            /* Intermediate or ignored ctrl char */
  357.  
  358.     case ES_GOTCSI:            /* GOTCSI -- In a control sequence */
  359.       if (c > 077 && c < 0177)    /* Final character '@' thru '~' */
  360.         inesc = ES_NORMAL;        /* Return to NORMAL. */
  361.       else if (c == ESC)        /* ESC ... */
  362.         inesc = ES_GOTESC;        /* starts over. */
  363.       break;            /* Intermediate or ignored ctrl char */
  364.  
  365.     case ES_STRING:            /* Inside a string */
  366.       if (c == ESC)            /* ESC may be 1st char of terminator */
  367.         inesc = ES_TERMIN;        /* Go see. */
  368. #ifdef CK_APC
  369.       else if (apcactive && (apclength < apcbuflen)) /* If in APC, */
  370.         apcbuf[apclength++] = c;    /* deposit this character. */
  371. #endif /* CK_APC */
  372.       break;            /* Absorb all other characters. */
  373.  
  374.     case ES_TERMIN:            /* May have a string terminator */
  375.       if (c == '\\') {        /* which must be backslash */
  376.           inesc = ES_NORMAL;    /* If so, back to NORMAL */
  377. #ifdef CK_APC
  378.           if (apcactive) {        /* If it was an APC string, */
  379.           debug(F101,"APC terminated","",c);
  380.           apcbuf[apclength] = NUL; /* terminate it and then ... */
  381.           return(1);
  382.           }
  383. #endif /* CK_APC */
  384.       } else {            /* Otherwise */
  385.           inesc = ES_STRING;    /* Back to string absorption. */
  386. #ifdef CK_APC
  387.           if (apcactive && (apclength+1 < apcbuflen)) { /* In APC string */
  388.           apcbuf[apclength++] = ESC; /* deposit the Esc character */
  389.           apcbuf[apclength++] = c;   /* and this character too */
  390.           }
  391. #endif /* CK_APC */
  392.       }
  393.       }
  394.     return(0);
  395. }
  396. #endif /* NOESCSEQ */
  397.  
  398. /* Connect state parent/child communication signal handlers */
  399.  
  400. /* Routines used by the child process */
  401.  
  402. int
  403. pipemsg(n) int n; {            /* Send message ID to parent */
  404.     int code = n & 255;
  405.     return(write(xpipe[1], &code, sizeof(code)));
  406. }
  407.  
  408. /* Environment pointer for CK_FORK_SIG signal handling in child... */
  409.  
  410. #ifdef CK_POSIX_SIG
  411. static sigjmp_buf sig_env;
  412. #else
  413. static jmp_buf sig_env;
  414. #endif /* CK_POSIX_SIG */
  415.  
  416. static SIGTYP                /* CK_FORK_SIG handling in child ... */
  417. forkint(foo) int foo; {
  418.     /* It is important to disable CK_FORK_SIG before longjmp */
  419.     signal(CK_FORK_SIG, SIG_IGN);    /* Set to ignore CK_FORK_SIG */
  420.     debug(F100, "forkint - CK_FORK_SIG", "", 0);
  421.     /* Force return from ck_sndmsg() */
  422. #ifdef CK_POSIX_SIG
  423.     siglongjmp(sig_env, 1);
  424. #else
  425.     longjmp(sig_env, 1);
  426. #endif /* CK_POSIX_SIG */
  427.     /* NOTREACHED */
  428. }
  429.  
  430. static void
  431. ck_sndmsg() {                /* Executed by child only ... */
  432.     debug(F100, "ck_sndmsg - notifying parent", "", 0);
  433.     if (
  434. #ifdef CK_POSIX_SIG
  435.     sigsetjmp(sig_env,1)
  436. #else
  437.     setjmp(sig_env)
  438. #endif /* CK_POSIX_SIG */
  439.     == 0) {
  440.         signal(CK_FORK_SIG, forkint);    /* Set up signal handler */
  441.         kill(parent_id, CK_FORK_SIG);    /* Kick the parent */
  442.         for (;;) pause();        /* Wait for CK_FORK_SIG or SIGKILL */
  443.     /* NOTREACHED */
  444.     }
  445.     /* We come here from forkint() via [sig]longjmp(sig_env,1) */
  446.     debug(F100, "ck_sndmsg - returning", "", 0);
  447. }
  448.  
  449. /* Routines used by the parent process */
  450.  
  451. #ifdef CK_POSIX_SIG         /* Environment pointer for CONNECT errors */
  452. static sigjmp_buf con_env;
  453. #else
  454. static jmp_buf con_env;
  455. #endif /* CK_POSIX_SIG */
  456.  
  457. /*
  458.   pipeint() handles CK_FORK_SIG signals from the lower (port) fork.
  459.   It reads a function code from the pipe that connects the two forks,
  460.   then reads additional data from the pipe, then handles it.
  461. */
  462. static SIGTYP
  463. pipeint(arg) int arg; {            /* Dummy argument */
  464.     int code;
  465.     /* IMPORTANT: At this point, the child fork is waiting for CK_FORK_SIG
  466.        (eventually for SIGKILL) inside of ck_sndmsg().  So we can't get any
  467.        subsequent CK_FORK_SIG from child before we send it CK_FORK_SIG.
  468.     */
  469.     signal(CK_FORK_SIG, SIG_IGN);    /* Ignore CK_FORK_SIG now */
  470.     debug(F101,"pipeint arg","",arg);
  471.  
  472.     read(xpipe[0], &code, sizeof(code)); /* Get function code from pipe */
  473.     debug(F101,"pipeint code","",code);
  474.  
  475.     switch (code & 255) {
  476.       case CEV_HUP:
  477.         sjval = CEV_HUP;        /* Set global variable */
  478. #ifdef CK_POSIX_SIG            /* Notify parent process to stop */
  479.         siglongjmp(con_env,sjval);
  480. #else
  481.         longjmp(con_env,sjval);
  482. #endif /* CK_POSIX_SIG */
  483.         /* NOTREACHED */
  484.       case CEV_DUP:            /* Child sends duplex change */
  485.     read(xpipe[0], (char *)&duplex, sizeof(duplex));
  486.     debug(F101,"pipeint duplex","",duplex);
  487.     break;
  488.  
  489. #ifdef CK_APC
  490.       case CEV_APC:            /* Application Program Command */
  491.     read(xpipe[0], (char *)&apclength, sizeof(apclength));
  492.     read(xpipe[0], apcbuf, apclength+1); /* Include trailing zero byte */
  493.     debug(F111,"APC at parent",apcbuf,apclength);
  494.     read(xpipe[0], (char *)&ibc, sizeof(ibc)); /* Copy child's */
  495.     if (ibc > 0) {                   /* input buffer. */
  496.         read(xpipe[0], (char *)&ibp, sizeof(ibp));
  497.         read(xpipe[0], ibp, ibc);
  498.     }
  499.     obc = 0; obp = obuf; *obuf = NUL; /* Because port fork flushed */
  500.     sjval = CEV_APC;
  501. #ifdef CK_POSIX_SIG
  502.     siglongjmp(con_env,sjval);
  503. #else
  504.     longjmp(con_env,sjval);
  505. #endif /* CK_POSIX_SIG */
  506.     /* NOTREACHED */
  507. #endif /* CK_APC */
  508.  
  509. #ifdef SUNX25
  510.       case CEV_PAD:            /* X.25 PAD parameter change */
  511.     debug(F100,"pipeint PAD change","",0);
  512.     read(xpipe[0],padparms,MAXPADPARMS);
  513.     sjval = CEV_PAD;        /* Set global variable. */
  514. #ifdef COMMENT                /* We might not need to do this... */
  515. #ifdef CK_POSIX_SIG
  516.     siglongjmp(con_env,sjval);
  517. #else
  518.     longjmp(con_env,sjval);
  519. #endif /* CK_POSIX_SIG */
  520.     /* NOTREACHED */
  521. #else  /* COMMENT */
  522.     break;
  523. #endif /* COMMENT */
  524. #endif /* SUNX25 */
  525.     }
  526.     signal(CK_FORK_SIG, pipeint);    /* Set up signal handler */
  527.     kill(pid, CK_FORK_SIG);        /* Signal the port fork ... */
  528. }
  529.  
  530. /*  C K C P U T C  --  C-Kermit CONNECT Put Character to Screen  */
  531. /*
  532.   Output is buffered to avoid slow screen writes on fast connections.
  533.   NOTE: These could (easily?) become macros ...
  534. */
  535. int
  536. ckcputf() {                /* Dump the output buffer */
  537.     int x = 0;
  538.     if (obc > 0)            /* If we have any characters, */
  539.       x = conxo(obc,obuf);        /* dump them, */
  540.     obp = obuf;                /* reset the pointer */
  541.     obc = 0;                /* and the counter. */
  542.     return(x);                /* Return conxo's return code */
  543. }
  544.  
  545. int
  546. ckcputc(c) int c; {
  547.     int x;
  548.  
  549.     *obp++ = c & 0xff;            /* Deposit the character */
  550.     obc++;                /* Count it */
  551.     if (ibc == 0 ||            /* If input buffer about empty */
  552.     obc == OBUFL) {            /* or output buffer full */
  553.     debug(F101,"CKCPUTC obc","",obc);
  554.     x = conxo(obc,obuf);        /* dump the buffer, */
  555.     obp = obuf;            /* reset the pointer */
  556.     obc = 0;            /* and the counter. */
  557.     return(x);            /* Return conxo's return code */
  558.     } else return(0);
  559. }
  560.  
  561. /*  C K C G E T C  --  C-Kermit CONNECT Get Character  */
  562. /*
  563.   Buffered read from communication device.
  564.   Returns the next character, refilling the buffer if necessary.
  565.   On error, returns ttinc's return code (see ttinc() description).
  566.   Dummy argument for compatible calling conventions with ttinc().
  567.   NOTE: We don't have a macro for this because we have to pass
  568.   a pointer to this function as an argument to tn_doop().
  569. */
  570. int
  571. ckcgetc(dummy) int dummy; {
  572.     int c, n;
  573.  
  574.     debug(F101,"CKCGETC 1 ibc","",ibc); /* Log */
  575.     if (ibc < 1) {            /* Need to refill buffer? */
  576.     ibc = 0;            /* Yes, reset count */
  577.     ibp = ibuf;            /* and buffer pointer */
  578.     debug(F100,"CKCGETC 1 calling ttinc(0)","",0); /* Log */
  579.     c = ttinc(0);            /* Read one character, blocking */
  580.     debug(F101,"CKCGETC 1 ttinc(0)","",c); /* Log */
  581.     if (c < 0) {            /* If error, return error code */
  582.         return(c);
  583.     } else {            /* Otherwise, got one character */
  584.         *ibp++ = c;            /* Advance buffer pointer */
  585.         ibc++;            /* and count. */
  586.     }
  587.  
  588.     /* Now quickly read any more that might have arrived */
  589.  
  590.     if ((n = ttchk()) > 0) {    /* Any more waiting? */
  591.         if (n > (IBUFL - ibc))    /* Get them all at once. */
  592.           n = IBUFL - ibc;        /* Don't overflow buffer */
  593.         if ((n = ttxin(n,(CHAR *)ibp)) > 0)
  594.           ibc += n;            /* Advance counter */
  595.     }
  596.     debug(F101,"CKCGETC 2 ibc","",ibc); /* Log how many */
  597.     ibp = ibuf;
  598.     }
  599.     c = *ibp++ & 0xff;            /* Get next character from buffer */
  600.     ibc--;                /* Reduce buffer count */
  601.     return(c);                /* Return the character */
  602. }
  603.  
  604. /*
  605.    Keyboard handling, buffered for speed, which is needed when C-Kermit is
  606.    in CONNECT mode between two other computers that are transferring data.
  607. */
  608. static char *kbp;            /* Keyboard input buffer pointer */
  609. static int kbc;                /* Keyboard input buffer count */
  610.  
  611. #ifdef pdp11                /* Keyboard input buffer length */
  612. #define KBUFL 32            /* Small for PDP-11 UNIX */
  613. #else
  614. #define KBUFL 257            /* Regular kernel size for others */
  615. #endif /* pdp11 */
  616.  
  617. #ifdef DYNAMIC
  618. static char *kbuf = NULL;
  619. #else
  620. static char kbuf[KBUFL];
  621. #endif /* DYNAMIC */
  622.  
  623. /* Macro for reading keystrokes. */
  624.  
  625. #define CONGKS() (((--kbc)>=0) ? ((int)(*kbp++) & 0377) : kbget())
  626.  
  627. /*
  628.   Note that we call read() directly here, normally a no-no, but in this case
  629.   we know it's UNIX and we're only doing what coninc(0) would have done,
  630.   except we're reading a block of characters rather than just one.  There is,
  631.   at present, no conxin() analog to ttxin() for chunk reads, and instituting
  632.   one would only add function-call overhead as it would only be a wrapper for
  633.   a read() call anyway.
  634. */
  635. /*
  636.   Another note: We stick in this read() till the user types something.
  637.   But the other (lower) fork is running too, and on TELNET connections,
  638.   it will signal us to indicate echo-change negotiations, and this can
  639.   interrupt the read().  Some UNIXes automatically restart the interrupted
  640.   system call, others return from it with errno == EINTR.
  641. */
  642. static int                /* Keyboard buffer filler */
  643. kbget() {
  644. #ifdef EINTR
  645.     int tries = 10;            /* If read() is interrupted, */
  646.     int ok = 0;
  647.     while (tries-- > 0) {        /* try a few times... */
  648. #endif /* EINTR */
  649.     if ((kbc = conchk()) < 1)    /* How many chars waiting? */
  650.       kbc = 1;            /* If none or dunno, wait for one. */
  651.     else if (kbc > KBUFL)        /* If too many, */
  652.       kbc = KBUFL;            /* only read this many. */
  653.     if ((kbc = read(0, kbuf, kbc)) < 1) { /* Now read it/them. */
  654.         debug(F101,"kbget errno","",errno);    /* Got an error. */
  655. #ifdef EINTR
  656.         if (errno == EINTR)        /* Interrupted system call. */
  657.           continue;            /* Try again, up to limit. */
  658.         else            /* Something else. */
  659. #endif /* EINTR */
  660.           return(-1);        /* Pass along read() error. */
  661.     }
  662. #ifdef EINTR
  663.     else { ok = 1; break; }
  664.     }
  665.     if (!ok) return(-1);
  666. #endif /* EINTR */
  667.     kbp = kbuf;                /* Adjust buffer pointer, */
  668.     kbc--;                /* count, */
  669.     return((int)(*kbp++) & 0377);    /* and return first character. */
  670. }
  671.  
  672.  
  673. /*  C O N E C T  --  Perform terminal connection  */
  674.  
  675. int
  676. conect() {
  677.     int    n;            /* General purpose counter */
  678.  
  679.     int c;            /* c is a character, but must be signed 
  680.                    integer to pass thru -1, which is the
  681.                    modem disconnection signal, and is
  682.                    different from the character 0377 */
  683.     int c2;            /* A copy of c */
  684.     int csave;            /* Another copy of c */
  685.     int tx;            /* tn_doop() return code */
  686. #ifdef SUNX25
  687.     int i;            /* Worker for X.25 code */
  688. #endif /* SUNX25 */
  689. #ifdef NETCONN
  690.  
  691. /* NOTE: SIGPIPE is now ignored the whole time Kermit is running. */
  692. /* (as of 30 Aug 94) */
  693. /* #ifdef SIGPIPE */
  694. /*    SIGTYP (*sigpiph)() = NULL; */
  695. /* #endif */ /* SIGPIPE */
  696.  
  697. #endif /* NETCONN */
  698. #ifndef NOESCSEQ
  699.     int apcrc;
  700. #endif /* NOESCSEQ */
  701.  
  702.     int conret = 0;            /* Return value from conect() */
  703.  
  704.     debug(F101,"CONNECT fork signal","",CK_FORK_SIG);
  705.     signal(CK_FORK_SIG, SIG_IGN);    /* Initial CK_FORK_SIG handling, */
  706. /*
  707.   The following ttimoff() call should not be necessary, but evidently there
  708.   are cases where a timer is left active and then goes off, taking a longjmp
  709.   to nowhere after the program's stack has changed.  In any case, this is
  710.   safe because the CONNECT module uses no timer of any kind, and no other timer
  711.   should be armed while Kermit is in CONNECT mode.
  712. */
  713.     ttimoff();                /* Turn off any timer interrupts */
  714.  
  715.     if (!local) {
  716. #ifdef NETCONN
  717.     printf("Sorry, you must SET LINE or SET HOST first\n");
  718. #else
  719.     printf("Sorry, you must SET LINE first\n");
  720. #endif /* NETCONN */
  721.     goto conret0;
  722.     }
  723.     if (speed < 0L && network == 0 && ttfdflg == 0) {
  724.     printf("Sorry, you must SET SPEED first\n");
  725.     goto conret0;
  726.     }
  727. #ifdef TCPSOCKET
  728.     if (network && (nettype != NET_TCPB)
  729. #ifdef SUNX25
  730.         && (nettype != NET_SX25)
  731. #endif /* SUNX25 */
  732.     ) {
  733.     printf("Sorry, network type not supported\n");
  734.     goto conret0;
  735.     }
  736. #endif /* TCPSOCKET */
  737.  
  738. #ifdef DYNAMIC
  739.     if (!ibuf) {
  740.     if (!(ibuf = malloc(IBUFL+1))) { /* Allocate input line buffer */
  741.         printf("Sorry, CONNECT input buffer can't be allocated\n");
  742.         goto conret0;
  743.     } else {
  744.         ibp = ibuf;
  745.         ibc = 0;
  746.     }
  747.     }
  748.     if (!obuf) {
  749.     if (!(obuf = malloc(OBUFL+1))) {    /* Allocate output line buffer */
  750.         printf("Sorry, CONNECT output buffer can't be allocated\n");
  751.         goto conret0;
  752.     } else {
  753.         obp = obuf;
  754.         obc = 0;
  755.     }
  756.     }
  757.     if (!kbuf) {
  758.     if (!(kbuf = malloc(KBUFL+1))) { /* Allocate keyboard input buffer */
  759.         printf("Sorry, CONNECT keyboard buffer can't be allocated\n");
  760.         goto conret0;
  761.     }
  762.     }
  763.     if (!temp) {
  764.     if (!(temp = malloc(TMPLEN+1))) { /* Allocate temporary buffer */
  765.         printf("Sorry, CONNECT temporary buffer can't be allocated\n");
  766.         goto conret0;
  767.     }
  768.     }
  769. #else
  770. #ifdef COMMENT
  771.     ibp = ibuf;                
  772.     ibc = 0;
  773. #endif /* COMMENT */
  774.     obp = obuf;
  775.     obc = 0;
  776. #endif /* DYNAMIC */
  777.  
  778.     kbp = kbuf;                /* Always clear these. */
  779.     *kbp = NUL;                /* No need to preserve them between */
  780.     kbc = 0;                /* CONNECT sessions. */
  781.  
  782. #ifdef DEBUG
  783.     if (deblog) {
  784.     debug(F101,"ckucon entry ttyfd","",ttyfd);
  785.     debug(F101,"ckucon entry ibc","",ibc);
  786.     debug(F101,"ckucon entry obc","",obc);
  787.     debug(F101,"ckucon entry kbc","",kbc);
  788.     if (ttyfd > -1) {
  789.         n = ttchk();
  790.         debug(F101,"ckucon entry ttchk","",n);
  791.     }
  792.     }    
  793. #endif /* DEBUG */    
  794.  
  795.     if (ttyfd < 0) {            /* If communication device not open */
  796.     debug(F111,"ckucon opening",ttname,0); /* Open it now */
  797.     if (ttopen(ttname,
  798.            &local,
  799.            network ? -nettype : mdmtyp,
  800.            0
  801.            ) < 0) {
  802.         sprintf(temp,"Sorry, can't open %s",ttname);
  803.         perror(temp);
  804.         debug(F110,"ckucon open failure",ttname,0);
  805.         goto conret0;
  806.     }
  807.     }
  808.     dohangup = 0;            /* Hangup not requested yet */
  809. #ifdef SUNX25
  810.     dox25clr = 0;            /* X.25 clear not requested yet */
  811. #endif /* SUNX25 */
  812.  
  813.     if (!quiet
  814. #ifdef CK_APC
  815.     && !apcactive
  816. #endif /* CK_APC */
  817.     ) {
  818. #ifdef NETCONN
  819.     if (network) {
  820.         printf("Connecting to host %s",ttname);
  821. #ifdef SUNX25
  822.         if (nettype == NET_SX25)
  823.           printf(", Link ID %d, LCN %d",linkid,lcn);
  824. #endif /* SUNX25 */
  825.     } else {
  826. #endif /* NETCONN */
  827.         printf("Connecting to %s",ttname);
  828.         if (speed > -1L) printf(", speed %ld",speed);
  829. #ifdef NETCONN
  830.     }
  831. #endif /* NETCONN */
  832.     printf(".\r\nThe escape character is Ctrl-%c (ASCII %d, %s)\r\n",
  833.            ctl(escape), escape, (escape == 127 ? "DEL" : ccntab[escape]));
  834.     printf("Type the escape character followed by C to get back,\r\n");
  835.     printf("or followed by ? to see other options.\r\n");
  836.     if (seslog) {
  837.         printf("(Session logged to %s, ",sesfil);
  838.         printf("%s)\r\n", sessft ? "binary" : "text");
  839.     }
  840.     if (debses) printf("Debugging Display...)\r\n");
  841.     fflush(stdout);
  842.     }
  843.  
  844. /* Condition console terminal and communication line */        
  845.  
  846.     if (conbin((char)escape) < 0) {
  847.     printf("Sorry, can't condition console terminal\n");
  848.     goto conret0;
  849.     }
  850.     debug(F101,"CONNECT cmask","",cmask);
  851.     debug(F101,"CONNECT cmdmsk","",cmdmsk);
  852.     debug(F101,"CONNECT speed before ttvt","",speed);
  853.     if ((n = ttvt(speed,flow)) < 0) {    /* Enter "virtual terminal" mode */
  854.     debug(F101,"CONNECT ttvt","",n);
  855.     tthang();            /* Hang up and close the device. */
  856.     ttclos(0);
  857.     if (ttopen(ttname,        /* Open it again... */
  858.            &local,
  859.            network ? -nettype : mdmtyp,
  860.            0
  861.            ) < 0) {
  862.         sprintf(temp,"Sorry, can't reopen %s",ttname);
  863.         perror(temp);
  864.         goto conret0;
  865.     }
  866.     if (ttvt(speed,flow) < 0) {    /* Try virtual terminal mode again. */
  867.         conres();            /* Failure this time is fatal. */
  868.         printf("Sorry, Can't condition communication line\n");
  869.         goto conret0;
  870.     }
  871.     }
  872.     debug(F101,"CONNECT ttvt ok, escape","",escape);
  873.  
  874. #ifndef NOCSETS
  875. /* Set up character set translations */
  876.  
  877.     tcs = gettcs(tcsr,tcsl);        /* Get intermediate set. */
  878.  
  879.     if (tcsr == tcsl) {            /* Remote and local sets the same? */
  880.     sxo = rxo = NULL;        /* If so, no translation. */
  881.     sxi = rxi = NULL;
  882.     } else {                /* Otherwise, set up */
  883.     sxo = xls[tcs][tcsl];        /* translation function */
  884.     rxo = xlr[tcs][tcsr];        /* pointers for output functions */
  885.     sxi = xls[tcs][tcsr];        /* and for input functions. */
  886.     rxi = xlr[tcs][tcsl];
  887.     }
  888. /*
  889.   This is to prevent use of zmstuff() and zdstuff() by translation functions.
  890.   They only work with disk i/o, not with communication i/o.  Luckily Russian
  891.   translation functions don't do any stuffing...
  892. */
  893.     langsv = language;
  894. #ifndef NOCYRIL
  895.     if (language != L_RUSSIAN)
  896. #endif /* NOCYRIL */
  897.       language = L_USASCII;
  898.  
  899. #ifdef COMMENT
  900. #ifdef DEBUG
  901.     if (deblog) {
  902.     debug(F101,"tcs","",tcs);
  903.     debug(F101,"tcsl","",tcsl);
  904.     debug(F101,"tcsr","",tcsr);
  905.     debug(F101,"fcsinfo[tcsl].size","",fcsinfo[tcsl].size);
  906.     debug(F101,"fcsinfo[tcsr].size","",fcsinfo[tcsr].size);
  907.     }
  908. #endif /* DEBUG */
  909. #endif /* COMMENT */
  910.  
  911. #ifndef NOESCSEQ
  912. /*
  913.   We need to activate the escape-sequence recognition feature when:
  914.    (a) translation is elected, AND
  915.    (b) the local and/or remote set is a 7-bit set other than US ASCII.
  916.  Or:
  917.    SET TERMINAL APC is not OFF (handled in the next statement).
  918. */
  919.     escseq = (tcs != TC_TRANSP) &&    /* Not transparent */
  920.       (fcsinfo[tcsl].size == 128 || fcsinfo[tcsr].size == 128) && /* 7 bits */
  921.     (fcsinfo[tcsl].code != FC_USASCII); /* But not ASCII */
  922. #endif /* NOESCSEQ */
  923. #endif /* NOCSETS */
  924.  
  925. #ifndef NOESCSEQ
  926. #ifdef CK_APC
  927.     escseq = escseq || (apcstatus != APC_OFF);
  928.     apcactive = 0;            /* An APC command is not active */
  929.     apclength = 0;            /* ... */
  930. #endif /* CK_APC */
  931.     inesc = ES_NORMAL;            /* Initial state of recognizer */
  932.     debug(F101,"escseq","",escseq);
  933. #endif /* NOESCSEQ */
  934.  
  935. /*
  936.   This is a label we jump back to when the lower fork sensed the need
  937.   to change modes.  As of 5A(178), this is used only by X.25 code
  938.   (perhaps unnecessarily? -- The X.25 code needs a lot of testing and
  939.   cleaning up...)  As of 5A(190), it should not be used at all.
  940. */
  941. #ifdef COMMENT
  942. newfork: /* Label no longer used */
  943. #endif /* COMMENT */
  944.     debug(F100,"CONNECT newfork","",0);
  945.     parent_id = getpid();        /* Get parent's pid for signalling */
  946.  
  947.     if (xpipe[0] > -1)            /* If old pipe hanging around, close */
  948.       close(xpipe[0]);
  949.     xpipe[0] = -1;
  950.     if (xpipe[1] > -1)
  951.       close(xpipe[1]);
  952.     xpipe[1] = -1;
  953.     goterr = 0;                /* Error flag for pipe & fork */
  954.     if (pipe(xpipe) != 0) {        /* Create new pipe to pass info */
  955.     perror("Can't make pipe");    /* between forks. */
  956.     goterr = 1;
  957.     } else if ((pid = fork()) == (PID_T) -1) { /* Pipe OK, make port fork. */
  958.     perror("Can't make port fork");
  959.     goterr = 1;    
  960.     }
  961.     if (goterr) {            /* Failed to make pipe or fork */
  962.     conres();            /* Reset the console. */
  963.     if (!quiet) {
  964.         printf("\r\nCommunications disconnect (Back at %s)\r\n",
  965.            *myhost ?
  966.            myhost :
  967. #ifdef UNIX
  968.            "local UNIX system"
  969. #else
  970.            "local system"
  971. #endif /* UNIX */
  972.            );
  973.     }
  974.     printf("\n");
  975.     what = W_NOTHING;        /* So console modes are set right. */
  976. #ifndef NOCSETS
  977.     language = langsv;        /* Restore language */
  978. #endif /* NOCSETS */
  979.     parent_id = (PID_T) 0;        /* Clean up */
  980.     goto conret1;
  981.     }
  982.  
  983. /* Upper fork (KEYB fork) reads keystrokes and sends them out. */
  984.  
  985.     if (pid) {                /* pid != 0, so I am the upper fork. */
  986.   
  987. /*
  988.   Before doing anything significant, the child fork must wait for a go-ahead
  989.   character from xpipe[0].  Before starting to wait, we have enough time to
  990.   clear buffers and set up the signal handler.  When done with this, we will
  991.   allow the child to continue by satisfying its pending read.
  992.  
  993.   Remember the child and parent have separate address space.  The child has
  994.   its own copy of input buffers, so we must clear the input buffers in the
  995.   parent.  Otherwise strange effects may occur, like chunks of characters
  996.   repeatedly echoed on terminal screen.  The child process is designed to
  997.   empty its input buffers by reading all available characters and either
  998.   echoing them on the terminal screen or saving them for future use in the
  999.   parent.  The latter case happens during APC processing - see the code around
  1000.   CEV_APC occurrences to see how the child passes its ibuf etc to parent via
  1001.   xpipe, for preservation until the next entry to this module, to ensure that
  1002.   no characters are lost between CONNECT sessions.
  1003. */
  1004.  
  1005. /*
  1006.   This one needs a bit of extra explanation...  In addition to the CONNECT
  1007.   module's own buffers, which are communicated and synchronized via xpipe,
  1008.   the low-level UNIX communication routines (ttinc, ttxin, etc) are also
  1009.   buffered, statically, in the ckutio.c module.  But when the two CONNECT
  1010.   forks split off, the lower fork is updating this buffer's pointers and
  1011.   counts, but the upper fork never finds out about it and still has the old
  1012.   ones.  The following UNIX-specific call to the ckutio.c module takes care
  1013.   of this...  Without it, we get dual echoing of incoming characters.
  1014. */
  1015.     ttflux();
  1016. /*
  1017.   At this point, perhaps you are wondering why we use forks at all.  It is
  1018.   simply because there is no other method portable among all UNIX variations.
  1019. */
  1020.     ibp = ibuf;            /* Clear ibuf[]. */
  1021.     ibc = 0;            /* Child now has its own copy */
  1022.     signal(CK_FORK_SIG, pipeint);    /* Handler for messages from child. */
  1023.     write(xpipe[1], ibuf, 1);    /* Allow child to proceed */
  1024.     close(xpipe[1]); xpipe[1] = -1; /* Parent - prevent future writes */
  1025.  
  1026. #ifdef COMMENT
  1027. #ifdef NETCONN
  1028. #ifdef SIGPIPE
  1029.     /* SIGPIPE is raised if a process sends on a broken stream */
  1030.     /* (do it, but only once!) */
  1031.     if (network && ! sigpiph)
  1032.       sigpiph = signal(SIGPIPE, SIG_IGN);
  1033. #endif /* SIGPIPE */
  1034. #endif /* NETCONN */
  1035. #endif /* COMMENT */
  1036.  
  1037.     what = W_CONNECT;        /* Keep track of what we're doing */
  1038.     active = 1;
  1039.     debug(F101,"CONNECT keyboard fork duplex","",duplex);
  1040. /*
  1041.   Catch communication errors or mode changes in lower fork.
  1042.  
  1043.   Note: Some C compilers (e.g. Cray UNICOS) interpret the ANSI C standard
  1044.   about setjmp() in a way that disallows constructions like:
  1045.  
  1046.         if ((var = [sig]setjmp(env)) == 0) ...
  1047.  
  1048.   which prevents the value returned by longjmp() from being used at all.
  1049.   So the signal handlers set a global variable, sjval, instead.
  1050. */
  1051.     if (
  1052. #ifdef CK_POSIX_SIG
  1053.         sigsetjmp(con_env,1)
  1054. #else
  1055.         setjmp(con_env)
  1056. #endif /* CK_POSIX_SIG */
  1057.         == 0) {    /* Normal entry... */
  1058.         sjval = CEV_NO;        /* Initialize setjmp return code. */
  1059. #ifdef SUNX25
  1060.         if (network && nettype == NET_SX25) {
  1061.         obufl = 0;
  1062.         bzero (x25obuf,sizeof(x25obuf)) ;
  1063.         }
  1064. #endif /* SUNX25 */
  1065. /*
  1066.   Here is the big loop that gets characters from the keyboard and sends them
  1067.   out the communication device.  There are two components to the communication
  1068.   path: the connection from the keyboard to C-Kermit, and from C-Kermit to
  1069.   the remote computer.  The treatment of the 8th bit of keyboard characters 
  1070.   is governed by SET COMMAND BYTESIZE (cmdmsk).  The treatment of the 8th bit
  1071.   of characters sent to the remote is governed by SET TERMINAL BYTESIZE
  1072.   (cmask).   This distinction was introduced in edit 5A(164).
  1073. */
  1074.         while (active) {
  1075. #ifndef NOSETKEY
  1076.         if (kmptr) {        /* Have current macro? */
  1077.             debug(F100,"kmptr non NULL","",0);
  1078.             if ((c = (CHAR) *kmptr++) == NUL) { /* Get char from it */
  1079.             kmptr = NULL;    /* If no more chars,  */
  1080.             debug(F100,"macro empty, continuing","",0);
  1081.             continue;    /* reset pointer and continue */
  1082.             }
  1083.             debug(F000,"char from macro","",c);
  1084.         } else            /* No macro... */
  1085. #endif /* NOSETKEY */
  1086.           c = CONGKS();        /* Read from keyboard */
  1087.  
  1088.         debug(F101,"** KEYB","",c);
  1089.  
  1090.                 if (c == -1) {        /* If read() got an error... */
  1091.             debug(F101,"CONNECT keyboard read errno","",errno);
  1092. #ifdef COMMENT
  1093. /*
  1094.  This seems to cause problems.  If read() returns -1, the signal has already
  1095.  been delivered, and nothing will wake up the pause().
  1096. */
  1097.             pause();        /* Wait for transmitter to finish. */
  1098. #else
  1099. #ifdef A986
  1100. /*
  1101.   On Altos machines with Xenix 3.0, pressing DEL in connect mode brings us
  1102.   here (reason unknown).  The console line discipline at this point has
  1103.   intr = ^C.  The communications tty has intr = DEL but we get here after
  1104.   pressing DEL on the keyboard, even when the remote system has been set not
  1105.   to echo.  With A986 defined, we stay in the read loop and beep only if the
  1106.   offending character is not DEL.
  1107. */
  1108.             if ((c & 127) != 127) conoc(BEL);
  1109. #else
  1110. #ifdef EINTR
  1111. /*
  1112.    This can be caused by the other fork signalling this one about 
  1113.    an echoing change during TELNET negotiations.
  1114. */
  1115.             if (errno == EINTR)
  1116.               continue;
  1117. #endif /* EINTR */
  1118.             conoc(BEL);        /* Otherwise, beep */
  1119.             active = 0;        /* and terminate the read loop */
  1120.             continue;
  1121. #endif /* A986 */
  1122. #endif /* COMMENT */
  1123.         }
  1124.         c &= cmdmsk;        /* Do any requested masking */
  1125. #ifndef NOSETKEY
  1126. /*
  1127.   Note: kmptr is NULL if we got character c from the keyboard, and it is
  1128.   not NULL if it came from a macro.  In the latter case, we must avoid
  1129.   expanding it again.
  1130. */
  1131.         if (!kmptr && macrotab[c]) { /* Macro definition for c? */
  1132.             kmptr = macrotab[c];     /* Yes, set up macro pointer */
  1133.             continue;             /* and restart the loop, */
  1134.         } else c = keymap[c];         /* else use single-char keymap */
  1135. #endif /* NOSETKEY */
  1136.         if (
  1137. #ifndef NOSETKEY
  1138.             !kmptr &&
  1139. #endif /* NOSETKEY */
  1140.             ((c & 0x7f) == escape)) { /* Escape character? */
  1141.             debug(F000,"CONNECT got escape","",c);
  1142.             c = CONGKS() & 0177; /* Got esc, get its arg */
  1143.             /* No key mapping here */
  1144.             doesc((char) c);    /* Now process it */
  1145.  
  1146.         } else {        /* It's not the escape character */
  1147.             csave = c;        /* Save it before translation */
  1148.                     /* for local echoing. */
  1149. #ifndef NOCSETS
  1150. #ifdef NOESCSEQ
  1151.             /* Translate character sets */
  1152.             if (sxo) c = (*sxo)(c); /* From local to intermediate. */
  1153.             if (rxo) c = (*rxo)(c); /* From intermediate to remote. */
  1154. #else
  1155.             if (inesc == ES_NORMAL) { /* If not inside escape seq.. */
  1156.             /* Translate character sets */
  1157.             if (sxo) c = (*sxo)((char)c); /* Local-intermediate */
  1158.             if (rxo) c = (*rxo)((char)c); /* Intermediate-remote */
  1159.             }
  1160.             if (escseq) apcrc = chkaes((char)c);
  1161. #endif /* NOESCSEQ */
  1162. #endif /* NOCSETS */
  1163. /*
  1164.  If Shift-In/Shift-Out is selected and we have a 7-bit connection,
  1165.  handle shifting here.
  1166. */
  1167.             if (sosi) {             /* Shift-In/Out selected? */
  1168.             if (cmask == 0177) { /* In 7-bit environment? */
  1169.                 if (c & 0200) {          /* 8-bit character? */
  1170.                 if (outshift == 0) { /* If not shifted, */
  1171.                     ttoc(dopar(SO)); /* shift. */
  1172.                     outshift = 1;
  1173.                 }
  1174.                 } else {
  1175.                 if (outshift == 1) { /* 7-bit character */
  1176.                     ttoc(dopar(SI)); /* If shifted, */
  1177.                     outshift = 0;    /* unshift. */
  1178.                 }
  1179.                 }
  1180.             }
  1181.             if (c == SO) outshift = 1;   /* User typed SO */
  1182.             if (c == SI) outshift = 0;   /* User typed SI */
  1183.             }
  1184.             c &= cmask;        /* Apply Kermit-to-host mask now. */
  1185. #ifdef SUNX25
  1186.                     if (network && nettype == NET_SX25) {
  1187.                         if (padparms[PAD_ECHO]) {
  1188.                             if (debses)
  1189.                   conol(dbchr(c)) ;
  1190.                             else
  1191.                   if ((c != padparms[PAD_CHAR_DELETE_CHAR])   &&
  1192.                   (c != padparms[PAD_BUFFER_DELETE_CHAR]) &&
  1193.                   (c != padparms[PAD_BUFFER_DISPLAY_CHAR]))
  1194.                                 conoc(c) ;
  1195.                             if (seslog) logchar(c);
  1196.                         }
  1197.             if (c == CR && (padparms[PAD_LF_AFTER_CR] == 4 ||
  1198.                     padparms[PAD_LF_AFTER_CR] == 5)) {
  1199.                             if (debses)
  1200.                   conol(dbchr(LF)) ;
  1201.                             else
  1202.                   conoc(LF) ;
  1203.                             if (seslog) logchar(LF);
  1204.                         }
  1205.                         if (c == padparms[PAD_BREAK_CHARACTER])
  1206.               breakact();
  1207.                         else if (padparms[PAD_DATA_FORWARD_TIMEOUT]) {
  1208.                             tosend = 1;
  1209.                             x25obuf [obufl++] = c;
  1210.                         } else if (((c == padparms[PAD_CHAR_DELETE_CHAR])  ||
  1211.                     (c == padparms[PAD_BUFFER_DELETE_CHAR]) ||
  1212.                     (c == padparms[PAD_BUFFER_DISPLAY_CHAR])) 
  1213.                    && (padparms[PAD_EDITING]))
  1214.               if (c == padparms[PAD_CHAR_DELETE_CHAR])
  1215.                 if (obufl > 0) {
  1216.                 conol("\b \b"); obufl--;
  1217.                 } else {}
  1218.               else if (c == padparms[PAD_BUFFER_DELETE_CHAR]) {
  1219.                   conol ("\r\nPAD Buffer Deleted\r\n");
  1220.                   obufl = 0;
  1221.               }
  1222.               else if (c == padparms[PAD_BUFFER_DISPLAY_CHAR]) {
  1223.                   conol("\r\n");
  1224.                   conol(x25obuf);
  1225.                   conol("\r\n");
  1226.               } else {} 
  1227.                         else {
  1228.                             x25obuf [obufl++] = c;
  1229.                             if (obufl == MAXOX25) tosend = 1;
  1230.                             else if (c == CR) tosend = 1;
  1231.                         }
  1232.                         if (tosend) 
  1233.               if (ttol(x25obuf,obufl) < 0) {
  1234.                   perror ("\r\nCan't send characters");
  1235.                   active = 0;
  1236.               } else {
  1237.                   bzero (x25obuf,sizeof(x25obuf));
  1238.                   obufl = 0;
  1239.                   tosend = 0;
  1240.               } else {};
  1241.                     } else {
  1242. #endif /* SUNX25 */ 
  1243.             if (c == '\015') {        /* Carriage Return */
  1244.             int stuff = -1;
  1245.             if (tnlm) {        /* TERMINAL NEWLINE ON */
  1246.                 stuff = LF;     /* Stuff LF */
  1247. #ifdef TNCODE
  1248.             } else if (network &&    /* TELNET NEWLINE ON/OFF/RAW */
  1249.                    (ttnproto == NP_TELNET) &&
  1250.                    (tn_nlm != TNL_CR)) {
  1251.                 stuff = (tn_nlm == TNL_CRLF) ? LF : NUL;
  1252. #endif /* TNCODE */
  1253.             }
  1254.             if (stuff > -1) {
  1255.                 ttoc(dopar('\015'));    /* Send CR */
  1256.                 if (duplex) conoc('\015');    /* Maybe echo CR */
  1257.                 c = stuff;            /* Char to stuff */
  1258.                 csave = c;
  1259.             }
  1260.             }
  1261. #ifdef TNCODE
  1262. /* If user types the 0xff character (TELNET IAC), it must be doubled. */
  1263.             else        /* Not CR */
  1264.               if ((dopar((CHAR) c) == IAC) && /* IAC (0xff) */
  1265.               network && (ttnproto == NP_TELNET)) {
  1266.                     /* Send one copy now */
  1267.             ttoc((char)IAC); /* and the other one just below. */
  1268.             }
  1269. #endif /* TNCODE */
  1270.             /* Send the character */
  1271.  
  1272.             if (ttoc((char)dopar((CHAR) c)) > -1) {
  1273.                 if (duplex) {    /* If half duplex, must echo */
  1274.                 if (debses)
  1275.                   conol(dbchr(csave)); /* the original char */
  1276.                 else               /* not the translated one */
  1277.                   conoc((char)csave);
  1278.                 if (seslog) { /* And maybe log it too */
  1279.                 c2 = csave;
  1280.                 if (sessft == 0 && csave == '\r')
  1281.                   c2 = '\n';
  1282.                 logchar((char)c2);
  1283.                 }
  1284.             }
  1285.                     } else {
  1286.             perror("\r\nCan't send character");
  1287.             active = 0;
  1288.             }
  1289. #ifdef SUNX25
  1290.         } 
  1291. #endif /* SUNX25 */
  1292.         }
  1293.         }
  1294.         /* now active == 0 */
  1295.             signal(CK_FORK_SIG, SIG_IGN); /* Turn off CK_FORK_SIG */
  1296.         sjval = CEV_NO;        /* Set to hangup */
  1297.     }                /* Come here on termination of child */
  1298.  
  1299. /* longjmp() executed in pipeint() (parent only!) comes here */
  1300.  
  1301. /*
  1302.   Now the child fork is gone or is waiting for CK_FORK_SIG in ck_sndmsg().
  1303.   So we can't get (in the parent) any subsequent CK_FORK_SIG signals until
  1304.   we signal the child with CK_FORK_SIG.
  1305. */
  1306.     debug(F100,"CONNECT signaling port fork","",0);
  1307.     signal(CK_FORK_SIG, SIG_IGN);    /* Turn this off */
  1308.     debug(F100,"CONNECT killing port fork","",0);
  1309.     kill(pid,9);            /* Done, kill inferior fork. */
  1310.     debug(F100,"CONNECT killed port fork","",0);
  1311.     pid = -1;
  1312.     wait((WAIT_T *)0);        /* Wait till gone. */
  1313.     if (sjval == CEV_HUP) {        /* Read error on comm device */
  1314.         dohangup = 1;        /* so we want to hang up our side */
  1315. #ifdef NETCONN
  1316.         if (network) {        /* and/or close network connection */
  1317.         ttclos(0);
  1318. #ifdef SUNX25
  1319.         if (nettype == NET_SX25) /* If X.25, restore the PAD params */
  1320.           initpad();
  1321. #endif /* SUNX25 */
  1322.         }
  1323. #endif /* NETCONN */
  1324.     }
  1325. #ifdef SUNX25
  1326. #ifdef COMMENT
  1327.     /* Not used, should not be needed -- see pipeint()... */
  1328.     if (sjval == CEV_PAD)        /* If it was a mode change, go back */
  1329.       goto newfork;            /* and coordinate with other fork. */
  1330. #endif /* COMMENT */
  1331. #endif /* SUNX25 */
  1332.  
  1333. #ifdef CK_APC
  1334.     if (sjval == CEV_APC) {        /* Application Program Command rec'd */
  1335.         apcactive = 1;        /* Flag APC as active */
  1336.         active = 0;            /* Flag CONNECT as inactive */
  1337.     }
  1338. #endif /* CK_APC */
  1339.     conres();            /* Reset the console. */
  1340.     if (dohangup > 0) {        /* If hangup requested, do that. */
  1341. #ifndef NODIAL
  1342.         if (dohangup > 1)        /* User asked for it */
  1343.           if (mdmhup() < 1)        /* Maybe hang up via modem */
  1344. #endif /* NODIAL */
  1345.         tthang();        /* And make sure we don't hang up */
  1346.         dohangup = 0;        /* again unless requested again. */
  1347.     }
  1348.  
  1349. #ifdef COMMENT
  1350. #ifdef NETCONN
  1351. #ifdef SIGPIPE
  1352.     if (network && sigpiph)        /* Restore previous SIGPIPE handler */
  1353.       (VOID) signal(SIGPIPE, sigpiph);
  1354. #endif /* SIGPIPE */
  1355. #endif /* NETCONN */
  1356. #endif /* COMMENT */
  1357.  
  1358. #ifdef SUNX25
  1359.     if (dox25clr) {            /* If X.25 Clear requested */
  1360.         x25clear();            /* do that. */
  1361.         initpad();
  1362.         dox25clr = 0;        /* But only once. */
  1363.     }
  1364. #endif /* SUNX25 */
  1365.  
  1366.     if (quitnow) doexit(GOOD_EXIT,xitsta); /* Exit now if requested. */
  1367.       if (!quiet
  1368. #ifdef CK_APC
  1369.         && !apcactive
  1370. #endif /* CK_APC */
  1371.         )
  1372.       printf("(Back at %s)", *myhost ? myhost : "local UNIX system");
  1373. #ifdef CK_APC
  1374.         if (!apcactive)
  1375. #endif /* CK_APC */
  1376.       printf("\n");
  1377.     what = W_NOTHING;        /* So console modes set right. */
  1378. #ifndef NOCSETS
  1379.     language = langsv;        /* Restore language */
  1380. #endif /* NOCSETS */
  1381.     parent_id = (PID_T) 0;
  1382.     goto conret1;
  1383.  
  1384.     } else {    /* *** */        /* Inferior reads, prints port input */
  1385.  
  1386.     if (priv_can()) {        /* Cancel all privs */
  1387.         printf("?setuid error - fatal\n");
  1388.         doexit(BAD_EXIT,-1);
  1389.     }
  1390.     signal(SIGINT, SIG_IGN);    /* In case these haven't been */
  1391.     signal(SIGQUIT, SIG_IGN);    /* inherited from above... */
  1392.     signal(CK_FORK_SIG, SIG_IGN);    /* CK_FORK_SIG not expected yet */
  1393.  
  1394.     inshift = outshift = 0;        /* Initial SO/SI shift state. */
  1395.     {                /* Wait for parent's setup */
  1396.         int i;
  1397.         while ((i = read(xpipe[0], &c, 1)) <= 0) {
  1398.         if (i < 0) {
  1399.             pipemsg(CEV_HUP);    /* Read error - hangup */
  1400.             ck_sndmsg();    /* Send and wait to be killed */
  1401.             /* NOTREACHED */
  1402.         }            /* Restart interrupted read() */
  1403.         }
  1404.     }
  1405.     close(xpipe[0]); xpipe[0] = -1;    /* Child - prevent future reads */
  1406.     debug(F100,"CONNECT starting port fork","",0);
  1407.     debug(F101,"CONNECT port fork ibc","",ibc);
  1408.     debug(F101,"CONNECT port fork obc","",obc);
  1409.  
  1410.     while (1) {            /* Fresh read, wait for a character. */
  1411. #ifdef SUNX25
  1412.         if (network && (nettype == NET_SX25)) {
  1413.         bzero(x25ibuf,sizeof(x25ibuf)) ;
  1414.         if ((ibufl = ttxin(MAXIX25,x25ibuf)) < 0) {
  1415.             if (ibufl == -2) {  /* PAD parms changes */
  1416.             pipemsg(CEV_PAD);
  1417.             write(xpipe[1],padparms,MAXPADPARMS);
  1418.             ck_sndmsg();
  1419.             } else {
  1420.             if (!quiet)
  1421.               printf("\r\nCommunications disconnect ");
  1422.                 pipemsg(CEV_HUP);
  1423.             ck_sndmsg();        /* Wait to be killed */
  1424.             /* NOTREACHED */
  1425.             }
  1426.             /* pause(); <--- SHOULD BE OBSOLETE NOW! */
  1427.             /* BECAUSE pause() is done inside of ck_sndmsg() */
  1428.         }
  1429.         if (debses) {        /* Debugging output */
  1430.             p = x25ibuf ;
  1431.                         while (ibufl--) { c = *p++; conol(dbchr(c)); }
  1432.         } else {
  1433.             if (sosi
  1434. #ifndef NOCSETS
  1435.             || tcsl != tcsr
  1436. #endif /* NOCSETS */
  1437.             ) { /* Character at a time */
  1438.             for (i = 1; i < ibufl; i++) {
  1439.                 c = x25ibuf[i] & cmask;
  1440.                 if (sosi) { /* Handle SI/SO */
  1441.                 if (c == SO) {
  1442.                     inshift = 1;
  1443.                     continue;
  1444.                 } else if (c == SI) {
  1445.                     inshift = 0;
  1446.                     continue;
  1447.                 }
  1448.                 if (inshift)
  1449.                   c |= 0200;
  1450.                 }
  1451. #ifndef NOCSETS
  1452. #ifdef NOESCSEQ
  1453.                 /* Translate character sets */
  1454.                 /* From local to intermediate. */
  1455.                 if (sxo) c = (*sxo)(c);
  1456.                 /* From intermediate to remote. */
  1457.                 if (rxo) c = (*rxo)(c);
  1458. #else
  1459.                 /* If not inside escape sequence... */
  1460.                 if (inesc == ES_NORMAL) {
  1461.                 /* Translate character sets */
  1462.                 if (sxo) c = (*sxo)(c);
  1463.                 if (rxo) c = (*rxo)(c);
  1464.                 }
  1465.                 if (escseq) apcrc = chkaes(c);
  1466. #endif /* NOESCSEQ */
  1467. #endif /* NOCSETS */
  1468.                 c &= cmdmsk; /* Apply command mask. */
  1469.                 conoc(c);    /* Output to screen */
  1470.                 logchar(c);  /* and session log */
  1471.             }
  1472.             } else {         /* All at once */
  1473.             for (i = 1; i < ibufl; i++)
  1474.               x25ibuf[i] &= (cmask & cmdmsk);
  1475.             conxo(ibufl,x25ibuf);
  1476.             if (seslog) zsoutx(ZSFILE,x25ibuf,ibufl);
  1477.             }
  1478.         }
  1479.         continue;
  1480.  
  1481.         } else {            /* Not X.25... */
  1482. #endif /* SUNX25 */
  1483. /*
  1484.   Get the next communication line character from our internal buffer.
  1485.   If the buffer is empty, refill it.
  1486. */
  1487.         c = ckcgetc(0);        /* Get next character */
  1488.         /* debug(F101,"CONNECT c","",c); */
  1489.         if (c < 0) {        /* Failed... */
  1490.             debug(F101,"CONNECT disconnect ibc","",ibc);
  1491.             debug(F101,"CONNECT disconnect obc","",obc);
  1492.             ckcputf();        /* Flush CONNECT output buffer */
  1493.             if (!quiet) {
  1494.             printf("\r\nCommunications disconnect ");
  1495.             if ( c == -3
  1496. #ifdef ultrix
  1497. /* This happens on Ultrix if there's no carrier */
  1498.                 && errno != EIO
  1499. #endif /* ultrix */
  1500. #ifdef UTEK
  1501. /* This happens on UTEK if there's no carrier */
  1502.                 && errno != EWOULDBLOCK
  1503. #endif /* UTEK */
  1504.                 )
  1505.               perror("\r\nCan't read character");
  1506.             }
  1507. #ifdef NOSETBUF
  1508.             fflush(stdout);
  1509. #endif /* NOSETBUF */
  1510.             tthang();        /* Hang up the connection */
  1511.             pipemsg(CEV_HUP);
  1512.             ck_sndmsg();    /* Wait to be killed */
  1513.             /* NOTREACHED */
  1514.         }
  1515.         debug(F101,"** PORT","",c); /* Got character c OK. */
  1516. #ifdef TNCODE
  1517.         /* Handle TELNET negotiations... */
  1518.  
  1519.         if (c == IAC && network && ttnproto == NP_TELNET) {
  1520.             debug(F100,"CONNECT got IAC","",0);
  1521.             ckcputf();        /* Dump screen-output buffer */
  1522.             if ((tx = tn_doop((CHAR)(c & 0xff),duplex,ckcgetc)) == 0) {
  1523.             continue;
  1524.             } else if (tx == -1) { /* I/O error */
  1525.             if (!quiet)
  1526.               printf("\r\nCommunications disconnect ");
  1527. #ifdef NOSETBUF
  1528.             fflush(stdout);
  1529. #endif /* NOSETBUF */
  1530.             pipemsg(CEV_HUP);
  1531.             ck_sndmsg();        /* Wait to be killed */
  1532.             /* NOTREACHED */
  1533.             } else if ((tx == 1) && (!duplex)) { /* ECHO change */
  1534.             duplex = 1;    /* Turn on local echo */
  1535.             debug(F101,"CONNECT TELNET duplex change","",duplex);
  1536.             pipemsg(CEV_DUP); /* Tell parent */
  1537.             write(xpipe[1], &duplex, sizeof(duplex));
  1538.             ck_sndmsg();      /* Tell the parent fork */
  1539.             continue;
  1540.             } else if ((tx == 2) && (duplex)) { /* ECHO change */
  1541.             duplex = 0;
  1542.             debug(F101,"CONNECT TELNET duplex change","",duplex);
  1543.             pipemsg(CEV_DUP);
  1544.             write(xpipe[1], &duplex, sizeof(duplex));
  1545.             ck_sndmsg();
  1546.             continue;
  1547.             } else if (tx == 3) { /* Quoted IAC */
  1548.             c = 255;
  1549.             } else continue;    /* Negotiation OK, get next char. */
  1550.         }
  1551. #endif /* TNCODE */
  1552.         if (debses) {        /* Output character to screen */
  1553.             char *s;        /* Debugging display... */
  1554.             s = dbchr(c);
  1555.             while (*s)
  1556.               ckcputc(*s++);
  1557.         } else {        /* Regular display ... */
  1558.             c &= cmask;        /* Apply Kermit-to-remote mask */
  1559.             if (sosi) {        /* Handle SI/SO */
  1560.             if (c == SO) {    /* Shift Out */
  1561.                 inshift = 1;
  1562.                 continue;
  1563.             } else if (c == SI) { /* Shift In */
  1564.                 inshift = 0;
  1565.                 continue;
  1566.             }
  1567.             if (inshift) c |= 0200; 
  1568.             }
  1569. #ifndef NOCSETS
  1570.             if (
  1571. #ifndef NOESCSEQ
  1572.             inesc == ES_NORMAL /* If not in an escape sequence */
  1573. #else
  1574.             1
  1575. #endif /* NOESCSEQ */
  1576.             ) {        /* Translate character sets */
  1577.             if (sxi) c = (*sxi)((CHAR)c);
  1578.             if (rxi) c = (*rxi)((CHAR)c);
  1579.             }
  1580. #endif /* NOCSETS */
  1581.  
  1582. #ifndef NOESCSEQ
  1583.             if (escseq)        /* If handling escape sequences */
  1584.               apcrc = chkaes((char)c); /* update our state */
  1585. #ifdef CK_APC
  1586. /*
  1587.   If we are handling APCs, we have several possibilities at this point:
  1588.    1. Ordinary character to be written to the screen.
  1589.    2. An Esc; we can't write it because it might be the beginning of an APC.
  1590.    3. The character following an Esc, in which case we write Esc, then char,
  1591.       but only if we have not just entered an APC sequence.
  1592. */
  1593.             if (escseq && apcstatus != APC_OFF) {
  1594.             if (inesc == ES_GOTESC)    /* Don't write ESC yet */
  1595.               continue;
  1596.             else if (oldesc == ES_GOTESC && !apcactive) {
  1597.                 ckcputc(ESC);    /* Write saved ESC */
  1598.                 if (seslog) logchar((char)ESC);
  1599.             } else if (apcrc) {     /* We have an APC */
  1600.                 debug(F111,"APC complete",apcbuf,apclength);
  1601.                 ckcputf();        /* Force screen update */
  1602.                 pipemsg(CEV_APC);    /* Notify parent */
  1603.                 write(xpipe[1],
  1604.                   (char *)&apclength,
  1605.                   sizeof(apclength)
  1606.                   );
  1607.                 /* Write buffer including trailing NUL byte */
  1608.  
  1609.                 write(xpipe[1], apcbuf, apclength+1);
  1610.  
  1611.                 /* Copy our input buffer to the parent fork */
  1612.  
  1613.                 debug(F101,"APC complete ibc","",ibc);
  1614.                 debug(F101,"APC complete obc","",obc);
  1615.                 write(xpipe[1], (char *)&ibc, sizeof(ibc));
  1616.                 if (ibc > 0) {
  1617.                 write(xpipe[1], (char *)&ibp, sizeof(ibp));
  1618.                 write(xpipe[1], ibp, ibc);
  1619.                 }            
  1620.                 ck_sndmsg();        /* Wait to be killed */
  1621.                 /* NOTREACHED */
  1622.             }
  1623.             }
  1624. #endif /* CK_APC */
  1625. #endif /* NOESCSEQ */
  1626.  
  1627.             if (
  1628. #ifdef CK_APC
  1629.             !apcactive    /* Ignore APC sequences */
  1630. #else
  1631.             1
  1632. #endif /* CK_APC */
  1633.             ) {
  1634.             c &= cmdmsk;    /* Apply command mask. */
  1635.             if (c == CR && tt_crd) { /* SET TERM CR-DISPLA CRLF? */
  1636.                 ckcputc(c);         /* Yes, output CR */
  1637.                 if (seslog) logchar((char)c);
  1638.                 c = LF;    /* and insert a linefeed */
  1639.             }
  1640.             ckcputc(c);    /* Write character to screen */
  1641.             }
  1642.             if (seslog) logchar((char)c); /* Handle session log */
  1643.         }
  1644. #ifdef SUNX25
  1645.         }   
  1646. #endif /* SUNX25 */    
  1647.     }
  1648.     }
  1649.  
  1650. conret1:
  1651.     conret = 1;
  1652. conret0:
  1653.     signal(CK_FORK_SIG, SIG_IGN);    /* In case this wasn't done already */
  1654.     debug(F101,"ckucon exit ibc","",ibc);
  1655.     debug(F101,"ckucon exit obc","",obc);
  1656.     close(xpipe[0]); xpipe[0] = -1;    /* Close the pipe */
  1657.     close(xpipe[1]); xpipe[1] = -1;
  1658.     return(conret);
  1659. }
  1660.  
  1661.  
  1662. /*  H C O N N E  --  Give help message for connect.  */
  1663.  
  1664. int
  1665. hconne() {
  1666.     int c;
  1667.     static char *hlpmsg[] = {
  1668. "\r\n  ? for this message",
  1669. "\r\n  0 (zero) to send a null",
  1670. "\r\n  B to send a BREAK",
  1671. #ifdef CK_LBRK
  1672. "\r\n  L to send a Long BREAK",
  1673. #endif /* CK_LBRK */
  1674. #ifdef NETCONN
  1675. "\r\n  I to send a network interrupt packet",
  1676. #ifdef TCPSOCKET
  1677. "\r\n  A to send Are You There?",
  1678. #endif /* TCPSOCKET */
  1679. #ifdef SUNX25
  1680. "\r\n  R to reset X.25 virtual circuit",
  1681. #endif /* SUNX25 */
  1682. #endif /* NETCONN */
  1683. "\r\n  H to hangup and close the connection",
  1684. "\r\n  Q to hangup and quit Kermit",
  1685. "\r\n  S for status",
  1686. "\r\n  ! to push to local shell",
  1687. "\r\n  Z to suspend",
  1688. "\r\n  \\ backslash code:",
  1689. "\r\n    \\nnn  decimal character code",
  1690. "\r\n    \\Onnn octal character code",
  1691. "\r\n    \\Xhh  hexadecimal character code",
  1692. "\r\n    terminate with carriage return.",
  1693. "\r\n Type the escape character again to send the escape character, or",
  1694. "\r\n press the space-bar to resume the CONNECT command.\r\n\r\n",
  1695. "" };
  1696.     conol("\r\nPress C to return to ");
  1697.     conol(*myhost ? myhost : "the C-Kermit prompt");
  1698.     conol(", or:");
  1699.     conola(hlpmsg);            /* Print the help message. */
  1700.     conol("Command>");            /* Prompt for command. */
  1701.     c = CONGKS() & 0177;        /* Get character, strip any parity. */
  1702.     /* No key mapping or translation here */
  1703.     if (c != CMDQ)
  1704.       conoll(""); 
  1705.    return(c);                /* Return it. */
  1706. }
  1707.  
  1708.  
  1709. /*  D O E S C  --  Process an escape character argument  */
  1710.  
  1711. VOID
  1712. #ifdef CK_ANSIC
  1713. doesc(char c)
  1714. #else
  1715. doesc(c) char c;
  1716. #endif /* CK_ANSIC */
  1717. /* doesc */ {
  1718.     CHAR d;
  1719.   
  1720.     debug(F101,"doesc","",c);
  1721.     while (1) {
  1722.     if (c == escape) {        /* Send escape character */
  1723.         d = dopar((CHAR) c); ttoc((char) d); return;
  1724.         } else                /* Or else look it up below. */
  1725.         if (isupper(c)) c = tolower(c);
  1726.  
  1727.     switch(c) {
  1728.  
  1729.     case 'c':            /* Escape back to prompt */
  1730.     case '\03':
  1731.         active = 0; conol("\r\n"); return;
  1732.  
  1733.     case 'b':            /* Send a BREAK signal */
  1734.     case '\02':
  1735.         ttsndb(); return;
  1736.  
  1737. #ifdef NETCONN
  1738.     case 'i':            /* Send Interrupt */
  1739.     case '\011':
  1740. #ifdef TCPSOCKET
  1741. #ifndef IP
  1742. #define IP 244
  1743. #endif /* IP */
  1744.         if (network && ttnproto == NP_TELNET) { /* TELNET */
  1745.         temp[0] = (CHAR) IAC;    /* I Am a Command */
  1746.         temp[1] = (CHAR) IP;    /* Interrupt Process */
  1747.         temp[2] = NUL;
  1748.         ttol((CHAR *)temp,2);
  1749.         } else 
  1750. #endif /* TCPSOCKET */
  1751. #ifdef SUNX25
  1752.             if (network && (nettype == NET_SX25)) { /* X.25 */
  1753.         (VOID) x25intr(0);                /* X.25 interrupt packet */
  1754.         conol("\r\n");
  1755.         } else
  1756. #endif /* SUNX25 */
  1757.           conoc(BEL);
  1758.         return;
  1759.  
  1760. #ifdef TCPSOCKET
  1761.     case 'a':            /* "Are You There?" */
  1762.     case '\01':
  1763. #ifndef AYT
  1764. #define AYT 246
  1765. #endif /* AYT */
  1766.         if (network && ttnproto == NP_TELNET) {
  1767.         temp[0] = (CHAR) IAC;    /* I Am a Command */
  1768.         temp[1] = (CHAR) AYT;    /* Are You There? */
  1769.         temp[2] = NUL;
  1770.         ttol((CHAR *)temp,2);
  1771.         } else conoc(BEL);
  1772.         return;
  1773. #endif /* TCPSOCKET */
  1774. #endif /* NETCONN */
  1775.  
  1776. #ifdef CK_LBRK
  1777.     case 'l':            /* Send a Long BREAK signal */
  1778.         ttsndlb(); return;
  1779. #endif /* CK_LBRK */
  1780.  
  1781.     case 'h':            /* Hangup */
  1782.      /*    case '\010': */            /* No, too dangerous */
  1783. #ifdef SUNX25
  1784.             if (network && (nettype == NET_SX25)) dox25clr = 1;
  1785.             else
  1786. #endif /* SUNX25 */
  1787.         dohangup = 2; active = 0; conol("\r\nHanging up "); return;
  1788.  
  1789. #ifdef SUNX25
  1790.         case 'r':                       /* Reset the X.25 virtual circuit */
  1791.         case '\022':
  1792.             if (network && (nettype == NET_SX25)) (VOID) x25reset(0,0);
  1793.             conol("\r\n"); return;
  1794. #endif /* SUNX25 */
  1795.  
  1796.     case 'q':            /* Quit */
  1797.         dohangup = 2; quitnow = 1; active = 0; conol("\r\n"); return;
  1798.  
  1799.     case 's':            /* Status */
  1800.         sprintf(temp,
  1801.             "\r\nConnected %s %s", network ? "to" : "through", ttname);
  1802.         conol(temp);
  1803. #ifdef SUNX25
  1804.             if (network && (nettype == NET_SX25)) {
  1805.                 sprintf(temp,", Link ID %d, LCN %d",linkid,lcn); conol(temp);
  1806.         }
  1807. #endif /* SUNX25 */
  1808.         if (speed >= 0L) {
  1809.         sprintf(temp,", speed %ld", speed);
  1810.         conoll(temp);
  1811.         } else conoll("");
  1812.         sprintf(temp,
  1813.             "Terminal bytesize: %d, Command bytesize: %d, Parity: ",
  1814.             (cmask  == 0177) ? 7 : 8,
  1815.             (cmdmsk == 0177) ? 7 : 8 );
  1816.         conol(temp);
  1817.  
  1818.         switch (parity) {
  1819.           case  0:  conoll("none");  break;
  1820.           case 'e': conoll("even");  break;
  1821.           case 'o': conoll("odd");   break;
  1822.           case 's': conoll("space"); break;
  1823.           case 'm': conoll("mark");  break;
  1824.         }
  1825.         sprintf(temp,"Terminal echo: %s", duplex ? "local" : "remote");
  1826.         conoll(temp);
  1827.         if (seslog) {
  1828.         conol("Logging to: "); conoll(sesfil);
  1829.             }
  1830.         if (!network) shomdm();
  1831.         return;
  1832.  
  1833.     case '?':            /* Help */
  1834.         c = hconne(); continue;
  1835.  
  1836.     case '0':            /* Send a null */
  1837.         c = '\0'; d = dopar((CHAR) c); ttoc((char) d); return;
  1838.  
  1839. #ifndef NOPUSH
  1840.     case 'z': case '\032':        /* Suspend */
  1841.         stptrap(0); return;
  1842.  
  1843.     case '@':            /* Start inferior command processor */
  1844.     case '!':
  1845.         conres();            /* Put console back to normal */
  1846.         zshcmd("");            /* Fork a shell. */
  1847.         if (conbin((char)escape) < 0) {
  1848.         printf("Error resuming CONNECT session\n");
  1849.         active = 0;
  1850.         }
  1851.         return;
  1852. #endif /* NOPUSH */
  1853.  
  1854.     case SP:            /* Space, ignore */
  1855.         return;
  1856.  
  1857.     default:            /* Other */
  1858.         if (c == CMDQ) {        /* Backslash escape */
  1859.         int x;
  1860.         ecbp = ecbuf;
  1861.         *ecbp++ = c;
  1862.         while (((c = (CONGKS() & cmdmsk)) != '\r') && (c != '\n'))
  1863.           *ecbp++ = c;
  1864.         *ecbp = NUL; ecbp = ecbuf;
  1865.         x = xxesc(&ecbp);    /* Interpret it */
  1866.         if (x >= 0) {        /* No key mapping here */
  1867.             c = dopar((CHAR) x);
  1868.             ttoc((char) c);
  1869.             return;
  1870.         } else {        /* Invalid backslash code. */
  1871.             conoc(BEL);
  1872.             return;
  1873.         }
  1874.         }
  1875.         conoc(BEL); return;     /* Invalid esc arg, beep */
  1876.         }
  1877.     }
  1878. }
  1879.  
  1880. static
  1881. VOID
  1882. #ifdef CK_ANSIC
  1883. logchar(char c)
  1884. #else
  1885. logchar(c) char c;
  1886. #endif /* CK_ANSIC */
  1887. /* logchar */ {            /* Log character c to session log */
  1888.     if (seslog) 
  1889.       if ((sessft != 0) ||
  1890.       (c != '\r' &&
  1891.        c != '\0' &&
  1892.        c != XON &&
  1893.        c != XOFF))
  1894.     if (zchout(ZSFILE,c) < 0) {
  1895.         conoll("");
  1896.         conoll("ERROR WRITING SESSION LOG, LOG CLOSED!");
  1897.         seslog = 0;
  1898.     }
  1899. }
  1900. #endif /* NOLOCAL */
  1901.