home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / old / ckermit70 / ckvcon.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  49KB  |  1,547 lines

  1. #ifndef VMS
  2.       ERROR -- CKVCON.C is used only on the OpenVMS(tm) Operating System
  3. #endif /* VMS */
  4. #define CONN_OS_ARCH_STRING \
  5.         "Connect Command 6.1.053 15 Nov 1999"
  6. #ifdef __ALPHA
  7.          /* do nothing */
  8. #else
  9. # ifdef VAX
  10. # else
  11. #ifndef __GNUC__
  12.       ERROR -- CKVCON.C unknown architecture, neither VAX(tm) nor Alpha
  13. # endif /* __GNUC__ */
  14. # endif /* VAX */
  15. #endif /* __ALPHA */
  16.  
  17. char *connv = CONN_OS_ARCH_STRING;
  18.  
  19. /*  C K V C O N  --  Terminal session to remote system, for VMS  */
  20. /*
  21.   Author: Frank da Cruz <fdc@columbia.edu>,
  22.   Columbia University Academic Information Systems, New York City.
  23.  
  24.   Copyright (C) 1985, 2000,
  25.     Trustees of Columbia University in the City of New York.
  26.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  27.     copyright text in the ckcmai.c module for disclaimer and permissions.
  28. */
  29. /*
  30.  * Originally adapted from the UNIX C-Kermit CONNECT module by S. Rubenstein
  31.  * for systems without fork(), circa 1985.  This version of conect() uses
  32.  * contti(&c, &src) to return when a character is available from either the
  33.  * console or the communications connection -- kind of like select().
  34.  *
  35.  * Edit Date       By   What
  36.  *
  37.  * 010 06-Mar-1989 mab    General cleanup
  38.  * 011 23-Mar-1989 mab    Clean up doesc() code.  Add malloc() in place of
  39.  *             static buffer space.  Also increase buffer space.
  40.  * 012 27-Sep-1989 mab    Add XON sequence.
  41.  * 013 30-Mar-1991 fdc  Add so/si, character set translation.
  42.  * 014 06-Apr-1991 fdc  Adapted for TGV MultiNet TCP/IP connections.
  43.  * 015 21-Jun-1991 tmk  Cleaned up typo in session logging display.
  44.  * 016 28-Nov-1991 fdc  "Back at <hostname>", disallow CONNECT in background
  45.  * 017 25-Dec-1991 fdc  Added support for NOPUSH, added "Hanging up" message
  46.  * 018 11-Jan-1992 fdc  Added support for key mapping
  47.  * 019 27-Jan-1992 fdc  Added support for ANSI escape sequence recognition
  48.  * 020 00-May-1992 fdc  Rewrote conect() to used buffered i/o.
  49.  * 021 10-Jun-1992 fdc  Added support for Wollongong WIN/TCP from Ray Hunter.
  50.  *                      Fix messed-up help message.
  51.  * 022 20-Jun-1992 fdc  Fixed handling of CR on TELNET sessions.
  52.  * 023 28-Jun-1992 fdc  Cosmetic cleanup of "Connecting..." message.
  53.  * 024 12-Jul-1992 fdc  Added mdmhup() feature (see ckudia.c).
  54.  * 025 29-Jul-1992 fdc  Grouped TELNET IP and AYT into single writes.
  55.  * 026 13-Aug-1992 fdc  Added support for SET TELNET NEWLINE-MODE.
  56.  * 027 05-Sep-1992 lt   Added architecture ifdefs for OpenVMS, Alpha, at top.
  57.  * 028 08-Sep-1992 fdc  Separated input and output SO/SO shift states.
  58.  * 029 11-Oct-1992 fdc  Reduce modem-vs-net confusion in ttopen() calls.
  59.  * 030 27-Oct-1993 fdc  Correct a typo in network protocol-checking code.
  60.  * 031 23-Oct-1993 fdc  Reset i/o buffer pointers upon new connection.
  61.  * 032 14-Feb-1994 fdc  Some minor cleanups.
  62.  * 033  9-Mar-1994 fdc  Fixed failure to display quoted TELNET IAC (255).
  63.  * 034  2-Jul-1994 fdc  Add initial APC support.
  64.  * 035  9-Jul-1994 fdc  Fix APC support.
  65.  * 036 15-Sep-1994 fdc  Add support for SET TELNET NEWLINE RAW.
  66.  * 037 24-Mar-1996 fdc  Add support for nopush and autodownload.
  67.  * 038 11-May-1996 fdc  Add support for SET TERM ESC { ENABLED, DISABLED }
  68.  * 039  5-Jun-1996 fdc  Change H to mean Help rather than Hangup, U = hangUp.
  69.  * 040  6-Jun-1996 fdc  Add Autoupload.
  70.  * 041  6-Sep-1996 fdc  Try to handle parity better.
  71.  * 042  1-Sep-1997 fdc  Add triggers.
  72.  * 043 21-Sep-1997 fdc  Don't send NAK any more if autodownload detected.
  73.  * 044  1-Jan-1998 fdc  Enforce SET CARRIER-WATCH.
  74.  * 045  8-Feb-1998 fdc  In ckcgetc() allow contti() to return nothing.
  75.  * 046 27-Dec-1998 fdc  Remove obsolete references to me_binary.
  76.  * 047  8-Feb-1999 fdc  Allow for -2 and -3 returns from tn_doop().
  77.  * 048 22-Jul-1999 fdc  Fix <escchar>S bug (print buffer sbuf[] too small)
  78.  *                      and added more info to status display.
  79.  * 049 24-Aug-1999 fdc  Add Unicode (UTF-8) support.
  80.  * 050 24-Oct-1999 fdc  Correct #ifdefs for NOCSETS case.
  81.  * 051 30-Oct-1999 fdc  Fix broken escape sequence recognizer.
  82.  * 052  4-Nov-1999 fdc  Keep autodownload trigger to ourselves.
  83.  * 053 15-Nov-1999 fdc  Fix broken escape sequence recognizer again.
  84.  */
  85. #include "ckcdeb.h"
  86. #include "ckcasc.h"
  87. #include "ckcker.h"
  88. #include "ckucmd.h"
  89. #include "ckcnet.h"
  90. #include "ckvvms.h"
  91. #ifndef NOCSETS
  92. #include "ckcxla.h"            /* Character set translation */
  93. #endif /* NOCSETS */
  94. #include <stdio.h>
  95. #include <ctype.h>
  96. #include <signal.h>
  97. #include <setjmp.h>
  98.  
  99. static int src;                /* Where input character came from */
  100.  
  101. extern long speed;
  102.  
  103. extern int local, escape, duplex, parity, flow, seslog, mdmtyp, batch;
  104. extern int cmask, cmdmsk, debses, sosi, ttyfd, what, quiet, tnlm, hints,
  105.  tt_crd, tn_nlm, tt_escape, itsatty;
  106. extern char ttname[], sesfil[], myhost[];
  107. #ifdef TNCODE
  108. extern int tn_b_nlm;
  109. #endif /* TNCODE */
  110.  
  111. #ifdef CK_TRIGGER
  112. extern char * tt_trigger[], * triggerval;
  113. #endif /* CK_TRIGGER */
  114.  
  115. extern int nopush;            /* Runtime NOPUSH */
  116.  
  117. #ifndef NOICP                /* Keyboard mapping */
  118. #ifndef NOSETKEY
  119. extern KEY *keymap;            /* Single-character key map */
  120. extern MACRO *macrotab;            /* Key macro pointer table */
  121. static MACRO kmptr = NULL;        /* Pointer to current key macro */
  122. #endif /* NOSETKEY */
  123. #endif /* NOICP */
  124.  
  125. extern int carrier;            /* CARRIER-WATCH selection */
  126.  
  127. /* Network support */
  128. extern int ttnproto,            /* Virtual terminal protocol */
  129.   network,                /* Network connection active */
  130.   nettype;                /* Network type */
  131.  
  132. static int unicode = 0;
  133.  
  134. #ifndef NOCSETS
  135. #ifdef CK_ANSIC
  136. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Character set */
  137. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* translation functions */
  138. static CHAR (*sxo)(CHAR);    /* Local translation functions */
  139. static CHAR (*rxo)(CHAR);    /* for output (sending) terminal chars */
  140. static CHAR (*sxi)(CHAR);    /* and for input (receiving) terminal chars. */
  141. static CHAR (*rxi)(CHAR);
  142. #else
  143. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])();    /* Character set */
  144. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])();    /* translation functions. */
  145. static CHAR (*sxo)();        /* Local translation functions */
  146. static CHAR (*rxo)();        /* for output (sending) terminal chars */
  147. static CHAR (*sxi)();        /* and for input (receiving) terminal chars. */
  148. static CHAR (*rxi)();
  149. #endif /* CK_ANSIC */
  150. extern int language;        /* Current language. */
  151. static int langsv;        /* Remember language */
  152. extern struct csinfo fcsinfo[]; /* File character set info */
  153. extern int tcsr, tcsl;        /* Terminal character sets, remote & local. */
  154. static int tcs;            /* Intermediate (xfer) char set */
  155. static int tcssize = 0;        /* Size of tcs */
  156. #ifdef UNICODE                /* UTF-8 support */
  157. #ifdef CK_ANSIC
  158. extern int (*xl_ufc[MAXFCSETS+1])(USHORT);  /* Unicode to FCS */
  159. extern USHORT (*xl_fcu[MAXFCSETS+1])(CHAR); /* FCS to Unicode */
  160. extern int (*xuf)(USHORT);        /* Translation function UCS to FCS */
  161. extern USHORT (*xfu)(CHAR);        /* Translation function FCS to UCS */
  162. #else
  163. extern int (*xl_ufc[MAXFCSETS+1])();
  164. extern USHORT (*xl_fcu[MAXFCSETS+1])();
  165. extern int (*xuf)();
  166. extern USHORT (*xfu)();
  167. #endif /* CK_ANSIC */
  168. #endif /* UNICODE */
  169. #endif /* NOCSETS */
  170.  
  171. _PROTOTYP( VOID doesc, (unsigned char) );
  172.  
  173. #ifndef NOSHOW
  174. _PROTOTYP( VOID shomdm, (void) );
  175. #endif /* NOSHOW */
  176.  
  177. /* Internal prototypes */
  178. _PROTOTYP( static int ckcputf, (void) );
  179. _PROTOTYP( int conresne, (void) );
  180. _PROTOTYP( int contti, (int *, int*) );
  181.  
  182. #ifdef CK_TRIGGER
  183. _PROTOTYP( int autoexitchk, (CHAR) );
  184. #endif /* CK_TRIGGER */
  185.  
  186. #ifdef CK_APC
  187. extern int apcactive;            /* Application Program Command (APC) */
  188. extern int apcstatus;            /* items ... */
  189. static int apclength = 0;
  190. #ifdef DCMDBUF
  191. extern char *apcbuf;
  192. #else
  193. extern char apcbuf[];
  194. #endif /* DCMDBUF */
  195. static int apcbuflen = APCBUFLEN - 2;
  196. extern int autodl, protocol;        /* Autodownload */
  197. #endif /* CK_APC */
  198.  
  199. int i, active;                /* Variables global to this module */
  200. static char *p;
  201.  
  202. #define OUTXBUFSIZ 15
  203. static CHAR inxbuf[OUTXBUFSIZ+1];    /* Host-to-screen expansion buffer */
  204. static int inxcount = 0;        /* and count */
  205. static CHAR outxbuf[OUTXBUFSIZ+1];    /* Keyboard-to-host expansion buf */
  206. static int outxcount = 0;        /* and count */
  207.  
  208. static int escseq = 0;            /* 1 = Recognizer is active */
  209. static int inesc[2] = { 0, 0 };        /* State of sequence recognizer */
  210. static int oldesc[2] = { -1, -1 };    /* Previous state of recognizer */
  211.  
  212. #ifndef NOESCSEQ
  213. /*
  214.   As of edit 178, the CONNECT command skips past ANSI escape sequences to
  215.   avoid translating the characters within them.  This allows the CONNECT
  216.   command to work correctly with a host that uses a 7-bit ISO 646 national
  217.   character set, in which characters like '[' would normally be translated
  218.   into accented characters, ruining the terminal's interpretation (and
  219.   generation) of escape sequences.
  220.  
  221.   As of edit 190, the CONNECT command responds to APC escape sequences
  222.   (ESC _ text ESC \) if the user SETs TERMINAL APC ON or UNCHECKED, and the
  223.   program was built with CK_APC defined.
  224.  
  225.   Non-ANSI/ISO-compliant escape sequences are not handled.
  226. */
  227.  
  228. /* States for the escape-sequence recognizer. */
  229.  
  230. #define ES_NORMAL 0            /* Normal, not in an escape sequence */
  231. #define ES_GOTESC 1            /* Current character is ESC */
  232. #define ES_ESCSEQ 2            /* Inside an escape sequence */
  233. #define ES_GOTCSI 3            /* Inside a control sequence */
  234. #define ES_STRING 4            /* Inside DCS,OSC,PM, or APC string */
  235. #define ES_TERMIN 5            /* 1st char of string terminator */
  236.  
  237. /*
  238.   ANSI escape sequence handling.  Only the 7-bit form is treated, because
  239.   translation is not a problem in the 8-bit environment, in which all GL
  240.   characters are ASCII and no translation takes place.  So we don't check
  241.   for the 8-bit single-character versions of CSI, DCS, OSC, APC, or ST.
  242.   Here is the ANSI sequence recognizer state table, followed by the code
  243.   that implements it.
  244.  
  245.   Definitions:
  246.     CAN = Cancel                       01/08         Ctrl-X
  247.     SUB = Substitute                   01/10         Ctrl-Z
  248.     DCS = Device Control Sequence      01/11 05/00   ESC P
  249.     CSI = Control Sequence Introducer  01/11 05/11   ESC [
  250.     ST  = String Terminator            01/11 05/12   ESC \
  251.     OSC = Operating System Command     01/11 05/13   ESC ]
  252.     PM  = Privacy Message              01/11 05/14   ESC ^
  253.     APC = Application Program Command  01/11 05/15   ESC _
  254.  
  255.   ANSI escape sequence recognizer:
  256.  
  257.     State    Input  New State  ; Commentary
  258.  
  259.     NORMAL   (start)           ; Start in NORMAL state
  260.  
  261.     (any)    CAN    NORMAL     ; ^X cancels
  262.     (any)    SUB    NORMAL     ; ^Z cancels
  263.  
  264.     NORMAL   ESC    GOTESC     ; Begin escape sequence
  265.     NORMAL   other             ; NORMAL control or graphic character
  266.  
  267.     GOTESC   ESC               ; Start again
  268.     GOTESC   [      GOTCSI     ; CSI
  269.     GOTESC   P      STRING     ; DCS introducer, consume through ST
  270.     GOTESC   ]      STRING     ; OSC introducer, consume through ST
  271.     GOTESC   ^      STRING     ; PM  introducer, consume through ST
  272.     GOTESC   _      STRING     ; APC introducer, consume through ST
  273.     GOTESC   0..~   NORMAL     ; 03/00 through 17/14 = Final character
  274.     GOTESC   other  ESCSEQ     ; Intermediate or ignored control character
  275.  
  276.     ESCSEQ   ESC    GOTESC     ; Start again
  277.     ESCSEQ   0..~   NORMAL     ; 03/00 through 17/14 = Final character
  278.     ESCSEQ   other             ; Intermediate or ignored control character
  279.  
  280.     GOTCSI   ESC    GOTESC     ; Start again
  281.     GOTCSI   @..~   NORMAL     ; 04/00 through 17/14 = Final character
  282.     GOTCSI   other             ; Intermediate char or ignored control char
  283.  
  284.     STRING   ESC    TERMIN     ; Maybe have ST
  285.     STRING   other             ; Consume all else
  286.  
  287.     TERMIN   \      NORMAL     ; End of string
  288.     TERMIN   other  STRING     ; Still in string
  289. */
  290. /*
  291.   C H K A E S  --  Check ANSI Escape Sequence.
  292.  
  293.   Call with EACH character in input stream.
  294.   src = 0 means c is incoming from remote; 1 = char from keyboard.
  295.   Sets global inesc[src] variable according to escape sequence state.
  296.   Returns 0 normally, 1 if an APC sequence is to be executed.
  297.   Handles transparent printing internally.
  298. */
  299. int
  300. #ifdef CK_ANSIC
  301. chkaes(char c, int src)
  302. #else
  303. chkaes(c,src) char c; int src;
  304. #endif /* CK_ANSIC */
  305. /* chkaes */ {
  306.  
  307.     debug(F111,"chkaes entry inesc",ckitoa(src),inesc[src]);
  308.     debug(F101,"chkaes c","",c);
  309.  
  310.     if (src < 0 || src > 1)        /* Don't allow bad args. */
  311.       return(0);
  312.  
  313.     oldesc[src] = inesc[src];        /* Remember previous state */
  314.  
  315. #ifdef XPRINT
  316.     if (inesc[src] && !src) {        /* Save up escape seq for printing  */
  317.     if (!c) return(0);        /* Ignore NULs */
  318.     if (escbufc < ESCBUFLEN) {
  319.         escbuf[escbufc++] = c;
  320.         escbuf[escbufc] = NUL;
  321.         debug(F111,"ESCBUF 1",escbuf,escbufc);
  322.     } else {            /* Buffer overrun */
  323.         if (printing && escbufc)    /* Print what's there so far */
  324.           zsoutx(ZMFILE,escbuf,escbufc);
  325.         escbufc = 1;        /* clear it out */
  326.         escbuf[0] = c;        /* and start off fresh buffer */
  327.         escbuf[1] = NUL;        /* with this character. */
  328.     }
  329.     }
  330. #endif /* XPRINT */
  331.  
  332.     if (c == CAN || c == SUB) {        /* CAN and SUB cancel any sequence */
  333. #ifdef XPRINT
  334.     if (!src) {
  335.         if (printing && escbufc > 1)
  336.           zsoutx(ZMFILE,escbuf,escbufc-1);
  337.         escbufc = 0;        /* Clear buffer */
  338.         escbuf[0] = NUL;
  339.     }
  340. #endif /* XPRINT */
  341.     inesc[src] = ES_NORMAL;
  342.     } else                /* Otherwise */
  343.  
  344.       switch (inesc[src]) {        /* enter state switcher */
  345.     case ES_NORMAL:            /* NORMAL state */
  346.       if (c == ESC) {        /* Got an ESC */
  347.           inesc[src] = ES_GOTESC;    /* Change state to GOTESC */
  348. #ifdef XPRINT
  349.           if (!src) {
  350.           escbufc = 1;        /* Clear escape sequence buffer */
  351.           escbuf[0] = c;    /* and deposit the ESC */
  352.           escbuf[1] = NUL;
  353.           debug(F111,"ESCBUF 2",escbuf,escbufc);
  354.           }
  355. #endif /* XPRINT */
  356.       }
  357.       break;            /* Otherwise stay in NORMAL state */
  358.  
  359.     case ES_GOTESC:            /* GOTESC state - prev char was ESC*/
  360.       if (c == '[') {        /* Left bracket after ESC is CSI */
  361.           inesc[src] = ES_GOTCSI;    /* Change to GOTCSI state */
  362.       } else if (c == 'P' || (c > 0134 && c < 0140)) { /* P, ], ^, or _ */
  363.           inesc[src] = ES_STRING;    /* Switch to STRING-absorption state */
  364. #ifdef XPRINT
  365.           debug(F111,"ESCBUF STRING",escbuf,escbufc);
  366. #endif /* XPRINT */
  367. #ifdef CK_APC
  368.           /* If APC not disabled */
  369.           if (!src && c == '_' && apcstatus != APC_OFF) {
  370.           debug(F100,"CONNECT APC begin","",0);
  371.           apcactive = APC_REMOTE; /* Set APC-Active flag */
  372.           apclength = 0;    /* and reset APC buffer pointer */
  373.           }
  374. #endif /* CK_APC */
  375.       } else if (c > 057 && c < 0177) { /* Final character '0' thru '~' */
  376.           inesc[src] = ES_NORMAL;    /* Back to normal */
  377. #ifdef XPRINT
  378.           if (!src) {
  379.           if (printing & escbufc > 1) {
  380.               /* Dump esc seq buf to printer */
  381.               zsoutx(ZMFILE,escbuf,escbufc-1);
  382.               debug(F111,"ESCBUF PRINT 1",escbuf,escbufc);
  383.           }
  384.           escbufc = 0;        /* Clear parameter buffer */
  385.           escbuf[0] = NUL;
  386.           }
  387. #endif /* XPRINT */
  388.       } else if (c != ESC) {    /* ESC in an escape sequence... */
  389.           inesc[src] = ES_ESCSEQ;    /* starts a new escape sequence */
  390.       }
  391.       break;            /* Intermediate or ignored ctrl char */
  392.  
  393.     case ES_ESCSEQ:            /* ESCSEQ -- in an escape sequence */
  394.       if (c > 057 && c < 0177) {    /* Final character '0' thru '~' */
  395.           inesc[src] = ES_NORMAL;    /* Return to NORMAL state. */
  396. #ifdef XPRINT
  397.           if (!src) {
  398.           if (printing && escbufc > 1) {
  399.               zsoutx(ZMFILE,escbuf,escbufc-1);
  400.               debug(F111,"ESCBUF PRINT 2",escbuf,escbufc);
  401.           }
  402.           escbufc = 0;        /* Clear escseq buffer */
  403.           escbuf[0] = NUL;
  404.           }
  405. #endif /* XPRINT */
  406.       } else if (c == ESC) {    /* ESC ... */
  407.           inesc[src] = ES_GOTESC;    /* starts a new escape sequence */
  408.       }
  409.       break;            /* Intermediate or ignored ctrl char */
  410.  
  411.     case ES_GOTCSI:            /* GOTCSI -- In a control sequence */
  412.       if (c > 077 && c < 0177) {    /* Final character '@' thru '~' */
  413. #ifdef XPRINT
  414.           if (!src && tt_print) {    /* Printer enabled? */
  415.           if (c == 'i') {    /* Final char is "i"? */
  416.               char * p = (char *) (escbuf + escbufc - 4);
  417.               if (!strncmp(p, "\033[5i", 4)) /* Yes, turn printer on */
  418.             printon();
  419.               else if (!strncmp(p, "\033[4i", 4)) { /* Or off... */
  420.               printoff();    /* Turn off printer. */
  421.               conxo(escbufc,escbuf); /* Display this sequence. */
  422.               } else if (printing && escbufc > 1) {
  423.               zsoutx(ZMFILE,escbuf,escbufc-1);
  424.               debug(F111,"ESCBUF PRINT 3",escbuf,escbufc);
  425.               }
  426.           } else if (printing && escbufc > 1) {
  427.               zsoutx(ZMFILE,escbuf,escbufc-1);
  428.               debug(F111,"ESCBUF PRINT 4",escbuf,escbufc);
  429.           }
  430.           }
  431.           if (!src) {
  432.           escbufc = 0;        /* Clear esc sequence buffer */
  433.           escbuf[0] = NUL;
  434.           }
  435. #endif /* XPRINT */
  436.           inesc[src] = ES_NORMAL;    /* Return to NORMAL. */
  437.       } else if (c == ESC) {    /* ESC ... */
  438.           inesc[src] = ES_GOTESC;    /* starts over. */
  439.       }
  440.       break;
  441.  
  442.     case ES_STRING:            /* Inside a string */
  443.       if (c == ESC)            /* ESC may be 1st char of terminator */
  444.         inesc[src] = ES_TERMIN;    /* Go see. */
  445. #ifdef CK_APC
  446.       else if (apcactive) {        /* If in APC */
  447.           if (apclength < apcbuflen) { /* and there is room... */
  448.           apcbuf[apclength++] = c; /* deposit this character. */
  449.           } else {            /* Buffer overrun */
  450.           apcactive = 0;    /* Discard what we got */
  451.           apclength = 0;    /* and go back to normal */
  452.           apcbuf[0] = 0;    /* Not pretty, but what else */
  453.           inesc[src] = ES_NORMAL; /* can we do?  (ST might not come) */
  454.           }
  455.       }
  456. #endif /* CK_APC */
  457.       break;            /* Absorb all other characters. */
  458.  
  459.     case ES_TERMIN:            /* Maybe a string terminator */
  460.       if (c == '\\') {        /* which must be backslash */
  461.           inesc[src] = ES_NORMAL;    /* If so, back to NORMAL */
  462. #ifdef XPRINT
  463.           if (!src) {
  464.           if (printing && escbufc > 1) { /* If printing... */
  465.               /* Print esc seq buffer */
  466.               zsoutx(ZMFILE,escbuf,escbufc-1);
  467.               debug(F111,"ESCBUF PRINT 5",escbuf,escbufc);
  468.           }
  469.           escbufc = 0;        /* Clear escseq buffer */
  470.           escbuf[0] = NUL;
  471.           }
  472. #endif /* XPRINT */
  473. #ifdef CK_APC
  474.           if (!src && apcactive) {    /* If it was an APC string, */
  475.           debug(F101,"CONNECT APC terminated","",c);
  476.           apcbuf[apclength] = NUL; /* terminate it and then ... */
  477.           return(1);
  478.           }
  479. #endif /* CK_APC */
  480.       } else {            /* It's not a backslash so... */
  481.           inesc[src] = ES_STRING;    /* back to string absorption. */
  482. #ifdef CK_APC
  483.           if (apcactive) {        /* In APC string */
  484.           if (apclength+1 < apcbuflen) { /* If enough room */
  485.               apcbuf[apclength++] = ESC; /* deposit the Esc */
  486.               apcbuf[apclength++] = c;   /* and this character too. */
  487.           } else {        /* Buffer overrun */
  488.               apcactive = 0;
  489.               apclength = 0;
  490.               apcbuf[0] = 0;
  491.               inesc[src] = ES_NORMAL;
  492.           }
  493.           }
  494. #endif /* CK_APC */
  495.       }
  496.       }    /* switch() */
  497.     debug(F111,"chkaes exit inesc",ckitoa(src),inesc[src]);
  498.     return(0);
  499. }
  500. #endif /* NOESCSEQ */
  501.  
  502. static char *ibp;            /* Input buffer pointer */
  503. static int ibc;                /* Input buffer count */
  504. #define IBUFL 1024            /* Input buffer length */
  505.  
  506. static char *obp;            /* Output buffer pointer */
  507. static int obc;                /* Output buffer count */
  508. #define OBUFL 1024            /* Output buffer length */
  509.  
  510. #ifdef DYNAMIC
  511. static char *ibuf = NULL, *obuf = NULL;    /* Line and temp buffers */
  512. #else
  513. static char ibuf[IBUFL], obuf[OBUFL];
  514. #endif /* DYNAMIC */
  515.  
  516. char kbuf[10], *kbp;            /* Keyboard buffer */
  517.  
  518. /*  C O N E C T  --  Perform terminal connection  */
  519.  
  520. int inshift, outshift;            /* SO/SI shift states */
  521.  
  522. /*  C K C P U T C  --  C-Kermit CONNECT Put Character to Screen  */
  523. /*
  524.   Output is buffered to avoid slow screen writes on fast connections.
  525. */
  526. int
  527. ckcputf() {                /* Dump the output buffer */
  528.     int x;
  529.     if (obc > 0)            /* If we have any characters, */
  530.       x = conxo(obc,obuf);        /* dump them, */
  531.     obp = obuf;                /* reset the pointer */
  532.     obc = 0;                /* and the counter. */
  533.     return(x);                /* Return conxo's return code */
  534. }
  535.  
  536. int
  537. ckcputc(c) int c; {
  538.     int x;
  539.  
  540.     *obp++ = c & 0xff;            /* Deposit the character */
  541.     obc++;                /* Count it */
  542.     if (ibc == 0 ||            /* If input buffer empty */
  543.     obc == OBUFL) {            /* or output buffer full */
  544.     x = conxo(obc,obuf);        /* dump the buffer, */
  545.     obp = obuf;            /* reset the pointer */
  546.     obc = 0;            /* and the counter. */
  547.     return(x);            /* Return conxo's return code */
  548.     } else return(0);
  549. }
  550.  
  551. /*  C K C G E T C  --  C-Kermit CONNECT Get Character  */
  552. /*
  553.   Buffered read from communication device.
  554.   Returns the next character, refilling the buffer if necessary.
  555.   On error, returns ttinc's return code (see ttinc() description).
  556.   Dummy argument for compatible calling conventions with ttinc().
  557.   NOTE: We don't have a macro for this because we have to pass
  558.   a pointer to this function as an argument to tn_doop().
  559. */
  560. int
  561. ckcgetc(dummy) int dummy; {
  562.     int c, i, n;
  563.  
  564.     if (ibc > 0) {            /* Have buffered port characters */
  565.     src = 1;            /* Say source is port */
  566.     c = *ibp++ & 0xff;        /* Get next character */
  567.     ibc--;                /* Reduce input buffer count */
  568. #ifdef COMMENT
  569. /* This makes the debug log really big. */
  570.     debug(F101,"CKCGETC buffered port char","",c);
  571. #endif /* COMMENT */
  572.     return(c);            /* Return buffered port character */
  573.     } else {                /* Need to refill buffer */
  574.     for (i = 10; i >= 0; i--) {    /* Try 10 times... */
  575.         contti(&c, &src);        /* to read one character */
  576.         if (src == -1) {        /* Connection dropped */
  577.         return(src);        /* Have to give up */
  578.         } else if (src == -2) {    /* HANGUP with CARRIER-WATCH OFF */
  579.         msleep(i*10);        /* and no character arrived */
  580.         debug(F101,"CKCGETC -2 countdown","",i);
  581.         continue;        /* Try so many times */
  582.         } else
  583.           break;
  584.     }
  585.     if (src < 0) {            /* Give up after too many tries */
  586.         debug(F100,"CKCGETC -2 gives up","",0);
  587.         return(-1);
  588.     }
  589.     if (src == 0) {            /* Got a character from the keyboard */
  590.         debug(F101,"CKCGETC keyboard char","",c);
  591.         return(c);
  592.     } else {            /* Got a port character */
  593.         ibp = ibuf;            /* Reset buffer pointer */
  594.         *ibp++ = c;            /* Deposit the character */
  595.         ibc++;            /* and count it */
  596. /*
  597.   Quickly read any more characters that might have arrived.  Ignore any errors
  598.   here; we need to return all the characters that arrived.  When we run out,
  599.   contti() will catch the error and return it.
  600. */
  601.         if ((n = ttchk()) > 0) {    /* Any more characters waiting? */
  602.         if (n > (IBUFL - ibc))    /* Get them all at once. */
  603.           n = IBUFL - ibc;    /* Don't overflow the buffer */
  604.         if ((n = ttxin(n,(CHAR *)ibp)) > 0) /* Read waiting chars */
  605.           ibc += n;        /* Advance counter */
  606.         }
  607.         ibp = ibuf;            /* Reset buffer pointer again */
  608.         c = *ibp++ & 0xff;        /* Get first character from buffer */
  609.         ibc--;            /* Reduce buffer count */
  610.         debug(F101,"CKCGETC port char","",c);
  611.         return(c);            /* Return the character */
  612.     }
  613.     }
  614. }
  615.  
  616. int
  617. conect() {
  618.     int c;            /* c is a character, but must be signed
  619.                    integer to pass thru -1, which is the
  620.                    modem disconnection signal, and is
  621.                    different from the character 0377 */
  622.     int c2, csave;        /* Copies of c */
  623.     char errmsg[50], *erp, *cp;
  624.     int n, x;                /* Workers */
  625.     int msgflg = 0;            /* Whether to print messages */
  626.     int apcrc;
  627.     int rc = 0;
  628.  
  629. #ifdef CK_TRIGGER
  630.     int ix;
  631. #endif /* CK_TRIGGER */
  632.  
  633.     if (!local) {
  634. #ifdef NETCONN
  635.     printf("Sorry, you must SET LINE or SET HOST first\n");
  636. #else
  637.     printf("Sorry, you must SET LINE first\n");
  638. #endif /* NETCONN */
  639.     return(-2);
  640.     }
  641.     if (batch) {
  642.     printf("\n\
  643. Sorry, Kermit's CONNECT command requires a real terminal;\n");
  644.     printf("It can't be used in a batch job.\n");
  645.     printf("Use INPUT and OUTPUT commands instead.\n");
  646.     return(0);
  647.     } else if (!itsatty) {
  648.     printf("\n\
  649. Sorry, Kermit's CONNECT command can be used only on a real terminal.\n");
  650.     printf(
  651. "If you have started Kermit from a DCL command procedure, it needs:\n\n");
  652.     printf("$ DEFINE /USER SYS$INPUT SYS$COMMAND\n\n");
  653.     printf("before starting Kermit.\n\n");
  654.     return(0);
  655.     }
  656. #ifdef TCPSOCKET
  657.     if (network && (nettype != NET_TCPB)) {
  658.     printf("Sorry, network type not supported yet\n");
  659.     return(0);
  660.     }
  661. #endif /* TCPSOCKET */
  662.  
  663.     debug(F101,"ckvcon network","",network);
  664.     debug(F101,"ckvcon speed","",speed);
  665.     debug(F101,"ckvcon ttyfd","",ttyfd);
  666.     debug(F110,"ckvcon host",ttname,0);
  667. #ifdef CK_TRIGGER
  668.     debug(F110,"ckvcon trigger",tt_trigger[0],0);
  669. #endif /* CK_TRIGGER */
  670.  
  671.     if (speed < 0 && network == 0) {
  672.     printf("Sorry, you must SET SPEED first\n");
  673.     return(-2);
  674.     }
  675.     if ((escape < 0) || (escape > 0177)) {
  676.     printf("Your escape character is not ASCII - %d\n",escape);
  677.     return(-2);
  678.     }
  679.     if (ttyfd < 0) {            /* If communication device not open */
  680.     debug(F111,"ckvcon opening",ttname,0); /* Open it now. */
  681.     if (ttopen(ttname,
  682.            &local,
  683.            network ? -nettype : mdmtyp,
  684.            0
  685.            ) < 0) {
  686.         erp = errmsg;
  687.         sprintf(erp,"Sorry, can't open %s",ttname);
  688.         perror(errmsg);
  689.         debug(F110,"ckvcon open failure",errmsg,0);
  690.         return(-2);
  691.     }
  692. #ifdef IKS_OPTION
  693.     /* If peer is in Kermit server mode, return now. */
  694.     if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start)
  695.       return(0);
  696. #endif /* IKS_OPTION */
  697.     }
  698.     msgflg = !quiet
  699. #ifdef CK_APC
  700.       && !apcactive
  701. #endif /* CK_APC */
  702.     ;
  703.  
  704.     if (msgflg) {
  705. #ifdef NETCONN
  706.     if (network) {
  707.         printf("\nConnecting to host %s",ttname);
  708.     } else {
  709. #endif /* NETCONN */
  710.         printf("\nConnecting to %s",ttname);
  711.         if (speed > -1L) printf(", speed %ld",speed);
  712. #ifdef NETCONN
  713.     }
  714. #endif /* NETCONN */
  715.     if (tt_escape) {
  716.         printf(".\r\nThe escape character is %s (ASCII %d).\r\n",
  717.            dbchr(escape),escape);
  718.         printf("Type the escape character followed by C to get back,\r\n");
  719.         printf("or followed by ? to see other options.\r\n");
  720.     } else {
  721.         printf(".\r\n\nESCAPE CHARACTER IS DISABLED\r\n\n");
  722.     }
  723.     if (seslog) {
  724.         printf("(Session logged to %s)\r\n",sesfil);
  725.     }
  726.     if (debses) printf("Debugging Display...)\r\n");
  727.         printf("----------------------------------------------------\r\n");
  728.     printf("\r\n");
  729.     }
  730.  
  731. /* Condition console terminal and communication line */
  732.  
  733.     rc = 0;
  734.     if (conbin(escape) < 0) {
  735.     printf("Sorry, can't condition console terminal\n");
  736.         printf("----------------------------------------------------\r\n");
  737.     goto conret0;
  738.     }
  739.     debug(F101,"ckvcon flow","",flow);
  740.     if (ttvt(speed,flow) < 0) {
  741.     /* tthang(); */ /* Closing it should be quite enough! */
  742.     ttclos(0);
  743.     if (ttopen(ttname,        /* Open it again... */
  744.            &local,
  745.            network ? -nettype : mdmtyp,
  746.            0
  747.            ) < 0) {
  748.         erp = errmsg;
  749.         sprintf(erp,"Sorry, can't reopen %s",ttname);
  750.         perror(errmsg);
  751.         goto conret0;
  752.     }
  753. #ifdef IKS_OPTION
  754.     if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start) {
  755.         goto conret0;
  756.     }
  757. #endif /* IKS_OPTION */
  758.     if (ttvt(speed,flow) < 0) {    /* Try virtual terminal mode again. */
  759.         conres();            /* Failure this time is fatal. */
  760.         printf("Sorry, Can't condition communication line\n");
  761.         goto conret0;
  762.     }
  763.     }
  764.     debug(F101,"CONNECT ttvt ok, escape","",escape);
  765.  
  766.     debug(F101,"CONNECT carrier-watch","",carrier);
  767.     if (!network && (carrier != CAR_OFF)) {
  768.     int x;
  769.     x = ttgmdm();
  770.     debug(F100,"CONNECT ttgmdm","",x);
  771.     if ((x > -1) && !(x & BM_DCD)) {
  772.         debug(F100,"CONNECT ttgmdm CD test fails","",x);
  773.         conres();
  774.         printf("?Carrier required but not detected.\n");
  775.         rc = 0;
  776.         if (hints) {
  777.         printf("***********************************\n");
  778.         printf(" Hint: To CONNECT to a serial device that\n");
  779.         printf(" is not presenting the Carrier Detect signal,\n");
  780.         printf(" first tell C-Kermit to:\n\n");
  781.         printf("   SET CARRIER-WATCH OFF\n\n");
  782.         printf("***********************************\n\n");
  783.         }
  784.         goto conret0;
  785.     }
  786.     debug(F100,"CONNECT ttgmdm ok","",0);
  787.     }
  788.  
  789. #ifdef CK_APC
  790.     apcactive = 0;
  791.     apclength = 0;
  792. #endif /* CK_APC */
  793.  
  794. #ifndef NOCSETS
  795. /* Set up character set translations */
  796.  
  797.     unicode = 0;            /* Assume Unicode won't be involved */
  798.     tcs = 0;                /* "Transfer" or "Other" charset */
  799.     sxo = rxo = NULL;            /* Initialize byte-to-byte functions */
  800.     sxi = rxi = NULL;
  801.     if (tcsr != tcsl) {            /* Remote and local sets differ... */
  802. #ifdef UNICODE
  803.     if (tcsr == FC_UTF8 ||        /* Remote charset is UTF-8 */
  804.         tcsl == FC_UTF8) {        /* or local one is. */
  805.         xuf = xl_ufc[tcsl];        /* Incoming Unicode to local */
  806.         if (xuf) {
  807.         tcs = (tcsr == FC_UTF8) ? tcsl : tcsr; /* The "other" set */
  808.         xfu = xl_fcu[tcs];    /* Local byte to remote Unicode */
  809.         if (xfu)
  810.           unicode = (tcsr == FC_UTF8) ? 1 : 2;
  811.         }
  812.         tcssize = fcsinfo[tcs].size; /* Size of other character set. */
  813.     } else {
  814. #endif /* UNICODE */
  815.         tcs = gettcs(tcsr,tcsl);    /* Get intermediate set. */
  816.         sxo = xls[tcs][tcsl];    /* translation function */
  817.         rxo = xlr[tcs][tcsr];    /* pointers for output functions */
  818.         sxi = xls[tcs][tcsr];    /* and for input functions. */
  819.         rxi = xlr[tcs][tcsl];
  820. #ifdef UNICODE
  821.     }
  822. #endif /* UNICODE */
  823.     }
  824. /*
  825.   This is to prevent use of zmstuff() and zdstuff() by translation functions.
  826.   They only work with disk i/o, not with communication i/o.  Luckily Russian
  827.   translation functions don't do any stuffing...
  828. */
  829.     langsv = language;
  830. #ifndef NOCYRIL
  831.     if (language != L_RUSSIAN)
  832. #endif /* NOCYRIL */
  833.       language = L_USASCII;
  834.  
  835. #ifndef NOESCSEQ
  836. /*
  837.   We need to activate escape-sequence recognition when:
  838.   (a) translation is elected, and:
  839.   (b) the local and/or remote set is 7-bit set other than US or UK ASCII, or:
  840.   (c) TERMINAL APC is not OFF.
  841. */
  842.     escseq = (tcs != TC_TRANSP) &&    /* Not transparent */
  843.       (fcsinfo[tcsl].size == 128 || fcsinfo[tcsr].size == 128) && /* 7 bits */
  844.     (fcsinfo[tcsl].code != FC_USASCII) && /* Not US ASCII */
  845.     (fcsinfo[tcsl].code != FC_UKASCII);   /* Not UK ASCII */
  846. #ifdef COMMENT
  847.     debug(F101,"tcs","",tcs);
  848.     debug(F101,"tcsl","",tcsl);
  849.     debug(F101,"tcsr","",tcsr);
  850.     debug(F101,"fcsinfo[tcsl].size","",fcsinfo[tcsl].size);
  851.     debug(F101,"fcsinfo[tcsr].size","",fcsinfo[tcsr].size);
  852. #endif /* COMMENT */
  853. #endif /* NOESCSEQ */
  854. #endif /* NOCSETS */
  855.  
  856. #ifndef NOESCSEQ
  857. #ifdef CK_APC
  858.     escseq = escseq || (apcstatus != APC_OFF);
  859.     apcactive = 0;            /* An APC command is not active */
  860.     apclength = 0;            /* ... */
  861. #endif /* CK_APC */
  862.     inesc[0] = ES_NORMAL;        /* Initial state of recognizer */
  863.     inesc[1] = ES_NORMAL;
  864.     debug(F101,"CONNECT escseq","",escseq);
  865. #endif /* NOESCSEQ */
  866.  
  867.     rc = 0;
  868. #ifdef DYNAMIC
  869.     if (!ibuf) {
  870.     if (!(ibuf = malloc(IBUFL+1))) { /* Allocate input line buffer */
  871.         printf("Sorry, CONNECT input buffer can't be allocated\n");
  872.         goto conret0;
  873.     } else {
  874.         ibp = ibuf;
  875.         ibc = 0;
  876.     }
  877.     }
  878.     if (!obuf) {
  879.     if (!(obuf = malloc(OBUFL+1))) {    /* Allocate output line buffer */
  880.         printf("Sorry, CONNECT output buffer can't be allocated\n");
  881.         goto conret0;
  882.     } else {
  883.         obp = obuf;
  884.         obc = 0;
  885.     }
  886.     }
  887. #else
  888.     ibp = ibuf;
  889.     ibc = 0;
  890.     obp = obuf;
  891.     obc = 0;
  892. #endif /* DYNAMIC */
  893.  
  894.     inshift = outshift = 0;        /* Initial SI/SO states */
  895.     active = 1;
  896.  
  897.     debug(F100,"CONNECT starting contti loop","",0);
  898.     do {                /* Top of big loop */
  899. #ifndef NOSETKEY
  900. /*
  901.   Before reading anything from the keyboard, continue expanding the current
  902.   active keyboard macro, if any.
  903. */
  904.     if (kmptr) {            /* Have active macro */
  905.         src = 0;            /* Pretend char came from keyboard */
  906.         if ((c = (CHAR) *kmptr++) == NUL) { /* but get it from the macro */
  907.         kmptr = NULL;        /* If no more chars in macro,  */
  908.         continue;        /* reset pointer and continue. */
  909.         }
  910.     } else                 /* OTHERWISE... */
  911. #endif /* NOSETKEY */
  912. /*
  913.   contti() samples the keyboard and the communication device, in that order,
  914.   in what amounts to a busy loop, and does not return until it has something
  915.   from one or the other.  This drives the VAX crazy, and also gives poor
  916.   performance: input is character-at a time, and the keyboard buffer is
  917.   checked at least once for every character that comes in from the remote.
  918.   Somebody who knows something about VMS, PLEASE FIND A BETTER WAY.
  919.  
  920.   We want a version of contti() that does what select() does in BSD, and we
  921.   also want it to be buffered internally, so it doesn't have to call the
  922.   operating system for every single input character.  And most of all we don't
  923.   want it to run a busy loop all the time.  No doubt this involves ASTs,
  924.   things of which I know nothing.    - fdc
  925. */
  926.     c = ckcgetc(0);            /* Calls contti()... */
  927.     debug(F000,"CKCGETC","",c);
  928. /*
  929.   Get here with a character in c, and:
  930.  
  931.   src = -1 Communication error
  932.          1 Character from comm line
  933.          0 Character from console
  934. */
  935.     if (src < 0) {            /* Comm line hangup or other error */
  936.         ckcputf();            /* flush screen-output buffer */
  937.         if (msgflg)
  938.           printf("\r\nCommunications disconnect ");
  939.         ttclos(0);            /* Close our end of the connection */
  940.         active = 0;
  941.  
  942.     } else if (!src) {        /* Character from console */
  943.         c &= cmdmsk;        /* Do requested masking */
  944. #ifndef NOICP
  945. #ifndef NOSETKEY
  946. /*
  947.   Note: kmptr is NULL if we got character c from the keyboard, and it is
  948.   not NULL if it came from a macro.  In the latter case, we must avoid
  949.   expanding it again.
  950. */
  951.         if (!kmptr && macrotab[c]) { /* If a macro is assigned to it */
  952.         kmptr = macrotab[c];    /* set up the pointer */
  953.         continue;        /* and do this again. */
  954.         } else c = keymap[c];    /* Else use single-char keymap */
  955. #endif /* NOSETKEY */
  956. #endif /* NOICP */
  957.         csave = c;
  958.         if (
  959. #ifndef NOICP
  960. #ifndef NOSETKEY
  961.             !kmptr &&
  962. #endif /* NOSETKEY */
  963. #endif /* NOICP */
  964.         (tt_escape && (c & 0x7f) == escape)) { /* Escape character? */
  965.         conresne();        /* Restore to normal attributes */
  966.         c = coninc(0) & 0177;
  967.         doesc(c);
  968.         conbin(escape);
  969.         } else {            /* Ordinary character */
  970. #ifndef NOCSETS
  971.         if (inesc[1] == ES_NORMAL) { /* If not inside escape seq.. */
  972. #ifdef UNICODE                /* Translate character sets */
  973.             int x;
  974.             CHAR ch;
  975.             ch = c;
  976.             if (unicode == 1) {    /* Remote is UTF-8 */
  977.             outxcount = b_to_u(ch,outxbuf,OUTXBUFSIZ,tcssize);
  978.             outxbuf[outxcount] = NUL;
  979.             } else if (unicode == 2) { /* Local is UTF-8 */
  980.             x = u_to_b(ch);    /* So translate to remote byte */
  981.             if (x < 0)
  982.               continue;
  983.             outxbuf[0] = (unsigned)(x & 0xff);
  984.             outxcount = 1;
  985.             outxbuf[outxcount] = NUL;
  986.             } else {
  987. #endif /* UNICODE */
  988.             if (sxo) c = (*sxo)((char)c); /* Local-intermediate */
  989.             if (rxo) c = (*rxo)((char)c); /* Intermediate-remote */
  990.             outxbuf[0] = c;
  991.             outxcount = 1;
  992.             outxbuf[outxcount] = NUL;
  993. #ifdef UNICODE
  994.             }
  995. #endif /* UNICODE */
  996.         } else {
  997.             outxbuf[0] = c;
  998.             outxcount = 1;
  999.             outxbuf[outxcount] = NUL;
  1000.         }
  1001.         if (escseq)
  1002.           apcrc = chkaes((char)c,1);
  1003. #else  /* NOCSETS */
  1004.         outxbuf[0] = c;
  1005.         outxcount = 1;
  1006.         outxbuf[outxcount] = NUL;
  1007. #endif /* NOCSETS */
  1008.  
  1009.         for (i = 0; i < outxcount; i++) {
  1010.             c = outxbuf[i];
  1011. /*
  1012.   If Shift-In/Shift-Out is selected and we have a 7-bit connection,
  1013.   handle shifting here.
  1014. */
  1015.             if (sosi) {             /* Shift-In/Out selected? */
  1016.             if (cmask == 0177) { /* In 7-bit environment? */
  1017.                 if (c & 0200) { /* 8-bit character? */
  1018.                 if (outshift == 0) { /* If not shifted, */
  1019.                     if (ttoc(dopar(SO)) < 0) { /* shift. */
  1020.                     ckcputf();
  1021.                     active = 0;
  1022.                     continue;
  1023.                     }
  1024.                     outshift = 1;
  1025.                 }
  1026.                 } else {
  1027.                 if (outshift == 1) { /* 7-bit character */
  1028.                     if (ttoc(dopar(SI)) < 0) {
  1029.                     ckcputf();
  1030.                     active = 0;
  1031.                     continue;
  1032.                     }
  1033.                     outshift = 0; /* unshift. */
  1034.                 }
  1035.                 }
  1036.             }
  1037.             if (c == SO) outshift = 1; /* User typed SO */
  1038.             if (c == SI) outshift = 0; /* User typed SI */
  1039.             }
  1040.             c &= cmask;        /* Apply Kermit-to-host mask now. */
  1041.             if (c == '\015') {    /* Carriage Return */
  1042.             int stuff = -1;
  1043.             if (tnlm) {    /* TERMINAL NEWLINE ON */
  1044.                 stuff = LF;    /* Stuff LF */
  1045. #ifdef TNCODE
  1046.             } else if (network &&    /* TELNET NEWLINE ON/OFF/RAW */
  1047.                    (ttnproto == NP_TELNET)) {
  1048.                 switch(!TELOPT_ME(TELOPT_BINARY) ?
  1049.                    tn_nlm :
  1050.                    tn_b_nlm
  1051.                    ) {
  1052.                   case TNL_CRLF:
  1053.                 stuff = LF;
  1054.                 break;
  1055.                   case TNL_CRNUL:
  1056.                 stuff = NUL;
  1057.                 break;
  1058.                 }
  1059. #endif /* TNCODE */
  1060.             }
  1061.             if (stuff > -1) {
  1062.                 ttoc(dopar('\015')); /* Send CR */
  1063.                 if (duplex) conoc('\015'); /* Maybe echo CR */
  1064.                 c = stuff;    /* Char to stuff */
  1065.                 csave = c;
  1066.             }
  1067.             }
  1068. #ifdef TNCODE
  1069. /* If user types the 0xff character (TELNET IAC), it must be doubled. */
  1070.             else
  1071.               if (dopar(c) == IAC &&
  1072.               network &&
  1073.               ttnproto == NP_TELNET
  1074.               ) {        /* Send one copy now */
  1075.               ttoc(IAC);    /* and the other one just below. */
  1076.               }
  1077. #endif /* TNCODE */
  1078.             if (ttoc(dopar(c)) < 0) { /* Now send the character. */
  1079.             ckcputf();
  1080.             active = 0;
  1081.             continue;
  1082.             }
  1083.             if (duplex) {    /* Half duplex? */
  1084.             if (debses)    /* Yes, echo locally */
  1085.               conol(dbchr(csave)); /* in appropriate mode */
  1086.             else
  1087.               conoc(csave);
  1088.             if (seslog) zchout(ZSFILE,c); /* And maybe log it. */
  1089.             }
  1090.         } /* for... */
  1091.         }
  1092.     } else {
  1093. /*
  1094.   Character from communications device or network connection...
  1095. */
  1096. #ifdef TNCODE
  1097.         /* Handle telnet options */
  1098.         if (network && ttnproto == NP_TELNET && ((c & 0xff) == IAC)) {
  1099.         ckcputf();        /* Dump output buffer */
  1100.         if ((x = tn_doop(c & 0xff, duplex, ckcgetc)) == -1 && msgflg)
  1101.           printf("\r\nCommunications disconnect ");
  1102.         if (x == -2 && msgflg)
  1103.           printf("\r\nConnection closed by peer ");
  1104.         if (x == -3 && msgflg)
  1105.           printf("\r\nConnection closed due to Telnet policy ");
  1106.         if (x == 1) duplex = 1;    /* Change duplex if necessary. */
  1107.         if (x == 2) duplex = 0;
  1108.         if (x == 3)        /* Quoted IAC */
  1109.           c = parity ? 127 : 255;
  1110. #ifdef IKS_OPTION
  1111.                 else if (x == 4) {    /* IKS State Change */
  1112.                     if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start &&
  1113.             !tcp_incoming
  1114.             ) {
  1115.             active = 0;
  1116.             break;
  1117.                     }
  1118.                 }
  1119. #endif /* IKS_OPTION */
  1120.         else
  1121.           continue;
  1122.         } else if (parity)        /* NOW strip parity */
  1123.           c &= 0x7f;
  1124.  
  1125. #endif /* TNCODE */
  1126.         if (debses) {        /* Output character to screen */
  1127.         char *s;        /* Debugging display... */
  1128.         s = dbchr(c);
  1129.         while (*s)
  1130.           ckcputc(*s++);
  1131.         } else {            /* or regular... */
  1132.         int k;
  1133.         c &= cmask;        /* Do first masking */
  1134. #ifdef CK_APC
  1135.         if (autodl && (k = kstart((CHAR)c))) { /* Saw S/I packet? */
  1136.             extern CHAR mystch, seol;
  1137.             extern int justone;
  1138.             CHAR buf[6];
  1139.             int ksign = 0;
  1140.             if (k < 0) {
  1141.             ksign = 1;
  1142.             k = 0 - k;
  1143.             justone = 1;
  1144.             } else {
  1145.             justone = 0;
  1146.             }
  1147.             if (k == PROTO_K
  1148. #ifdef CK_XYZ
  1149.             || k == PROTO_Z
  1150. #endif /* CK_XYZ */
  1151.             ) {
  1152.             /* Damage the packet so that it doesn't trigger */
  1153.             /* autodownload detection downstream. */
  1154.             if (k == PROTO_K) {
  1155.                 extern CHAR ksbuf[];
  1156.                 int i, len = strlen((char*)ksbuf);
  1157.                 for (i = 0; i < len; i++)
  1158.                   ckcputc(BS);
  1159.             }
  1160. #ifdef CK_XYZ
  1161.             else {
  1162.                 int i;
  1163.                 for (i = 0; i < 3; i++)
  1164.                   ckcputc(CAN);
  1165.             }
  1166. #endif /* CK_XYZ */
  1167.             }
  1168.             switch (protocol) { /* Stuff RECEIVE into APC buffer */
  1169.               case PROTO_K:
  1170.             strcpy(apcbuf, ksign ? "server" : "receive");
  1171.             break;
  1172. #ifdef CK_XYZ
  1173.               case PROTO_G:
  1174.             strcpy(apcbuf,
  1175.                    "set proto kermit, receive, set proto g");
  1176.             break;
  1177.               case PROTO_X:
  1178.             strcpy(apcbuf,
  1179.                    "set proto kermit, receive, set proto x");
  1180.             break;
  1181.               case PROTO_Y:
  1182.             strcpy(apcbuf,
  1183.                    "set proto kermit, receive, set proto y");
  1184.             break;
  1185.               case PROTO_Z:
  1186.             strcpy(apcbuf,
  1187.                    "set proto kermit, receive, set proto z");
  1188.             break;
  1189. #endif /* CK_XYZ */
  1190.             }
  1191.             apclength = strlen(apcbuf) ;
  1192.             debug(F110,"autodownload",apcbuf,0);
  1193.             apcactive = APC_LOCAL;
  1194.             ckcputf();        /* flush screen-output buffer and... */
  1195. #ifdef COMMENT
  1196.             printf("\r\n");    /* prevent CR-overstriking and...    */
  1197. #endif /* COMMENT */
  1198.             active = 0;        /* exit from the terminal emulator.. */
  1199.             break;
  1200.         }
  1201. #endif /* CK_APC */
  1202.         if (sosi) {        /* Handle SI/SO */
  1203.             if (c == SO) {    /* Shift Out */
  1204.             inshift = 1;
  1205.             continue;
  1206.             } else if (c == SI) { /* Shift In */
  1207.             inshift = 0;
  1208.             continue;
  1209.             }
  1210.             if (inshift) c |= 0200;
  1211.         }
  1212.         inxbuf[0] = c;        /* In case there is no translation */
  1213.         inxcount = 1;
  1214. #ifndef NOCSETS
  1215.         if (inesc[0] == ES_NORMAL) { /* If not in an escape sequence */
  1216. #ifdef UNICODE
  1217.             if (unicode == 1) {    /* Remote is UTF-8 */
  1218.             int x;
  1219.             x = u_to_b((CHAR)c);
  1220.             if (x == -1)
  1221.               continue;
  1222.             else if (x == -2) { /* LS or PS */
  1223.                 inxbuf[0] = CR;
  1224.                 inxbuf[1] = LF;
  1225.                 inxcount = 2;
  1226.             } else {
  1227.                 inxbuf[0] = (unsigned)(x & 0xff);
  1228.             }
  1229.             c = inxbuf[0];
  1230.             } else if (unicode == 2) { /* Local is UTF-8 */
  1231.             inxcount = b_to_u((CHAR)c,inxbuf,OUTXBUFSIZ,tcssize);
  1232.             c = inxbuf[0];
  1233.             } else {
  1234. #endif /* UNICODE */
  1235.             if (sxi) c = (*sxi)((CHAR)c);
  1236.             if (rxi) c = (*rxi)((CHAR)c);
  1237.             inxbuf[0] = c;
  1238. #ifdef UNICODE
  1239.             }
  1240. #endif /* UNICODE */
  1241.         }
  1242. #endif /* NOCSETS */
  1243.  
  1244. #ifndef NOESCSEQ
  1245.         if (escseq)        /* If handling escape sequences */
  1246.           chkaes((char)c,0);    /* update our state */
  1247. #ifdef CK_APC
  1248. /*
  1249.   If we are handling APCs, we have several possibilities at this point:
  1250.    1. Ordinary character to be written to the screen.
  1251.    2. An Esc; we can't write it because it might be the beginning of an APC.
  1252.    3. The character following an Esc, in which case we write Esc, then char,
  1253.       but only if we have not just entered an APC sequence.
  1254. */
  1255.         debug(F000,"XXX A","",c);
  1256.         if (escseq && apcstatus != APC_OFF) {
  1257.             if (inesc[0] == ES_GOTESC) { /* Don't write ESC yet */
  1258.             debug(F100,"XXX SKIPPING ESC","",0);
  1259.             continue;
  1260.             } else if (oldesc[0] == ES_GOTESC && !apcactive) {
  1261.             debug(F100,"XXX WRITING ESC","",0);
  1262.             ckcputc(ESC);    /* Write saved ESC */
  1263.             if (seslog) zchout(ZSFILE,ESC);
  1264.             }
  1265.         }
  1266. #endif /* CK_APC */
  1267. #endif /* NOESCSEQ */
  1268.  
  1269.         for (i = 0; i < inxcount; i++) { /* Loop thru */
  1270.             c = inxbuf[i];    /* input expansion buffer... */
  1271.             if (
  1272. #ifdef CK_APC
  1273.             !apcactive
  1274. #else
  1275.             1
  1276. #endif /* CK_APC */
  1277.             ) {
  1278.             c &= cmdmsk;    /* Apply mask */
  1279.             if (c == CR && tt_crd) { /* SET TERM CR-DISPLA CRLF? */
  1280.                 ckcputc(c);    /* Yes, output CR */
  1281.                 if (seslog) zchout(ZSFILE,c);
  1282.                 c = LF;    /* and insert a linefeed */
  1283.             }
  1284.             ckcputc(c);    /* Put it on the screen. */
  1285.             }
  1286.             if (seslog) zchout(ZSFILE,c); /* If logging, log it. */
  1287. #ifdef CK_TRIGGER
  1288.             /* Check for trigger string */
  1289.             if (tt_trigger[0]) if ((ix = autoexitchk((CHAR)c)) > -1) {
  1290.             int n;
  1291.             ckcputf();    /* Flush screen-output buffer */
  1292.             if (triggerval)    /* Make a copy of the trigger */
  1293.               free(triggerval);
  1294.             triggerval = NULL;
  1295.             n = strlen(tt_trigger[ix]);
  1296.             if (triggerval = (char *)malloc(n+1))
  1297.               strcpy(triggerval,tt_trigger[ix]);
  1298.             debug(F110,"CONNECT triggerval",triggerval,0);
  1299.             active = 0;
  1300.             }
  1301. #endif /* CK_TRIGGER */
  1302.         } /* for... */
  1303.         }
  1304.     }
  1305.     } while (active);
  1306.     rc = 1;
  1307.  
  1308.   conret0:
  1309.     ck_cancio();
  1310.     conres();
  1311.     if (msgflg
  1312. #ifdef CK_APC
  1313.     && !apcactive
  1314. #endif /* CK_APC */
  1315.     )
  1316.       printf("(Back at %s)", *myhost ? myhost : "local VMS system");
  1317.     what = W_NOTHING;
  1318. #ifdef CK_APC
  1319.     if (!apcactive)
  1320. #endif /* CK_APC */
  1321.       printf("\n");
  1322. #ifndef NOCSETS
  1323.     language = langsv;            /* Restore language */
  1324. #endif /* NOCSETS */
  1325.     if (msgflg) {
  1326. #ifdef CK_APC
  1327.     if (apcactive == APC_LOCAL)
  1328.       printf("\n");
  1329. #endif /* CK_APC */
  1330.     printf("----------------------------------------------------\r\n");
  1331.     }
  1332.     return(rc);
  1333. }
  1334.  
  1335. /*  H C O N N E  --  Give help message for connect.  */
  1336.  
  1337. VOID
  1338. hconne() {
  1339.     int c;
  1340.     static char *hlpmsg[] = {
  1341. "\n",
  1342. "  C to return to C-Kermit prompt,   U to hangup and close the connection,\n",
  1343. "  B to send a BREAK,                L to send a Long BREAK,\n",
  1344. #ifdef TNCODE
  1345. "  A to send TELNET Are You There,   I to send TELNET Interrupt,\n",
  1346. #endif /* TNCODE */
  1347. "  0 (zero) to send a null,          X to send an XON,\n",
  1348. #ifdef NOPUSH
  1349. "  S for status of connection,       ? for this message, or:\n",
  1350. #else
  1351. "  @ to enter DCL,                   S for status of connection,\n",
  1352. "  ? for this message, or:\n",
  1353. #endif /* NOPUSH */
  1354. "  \\ to begin a backslash escape:\n",
  1355. "    \\nnn  (decimal character code)\n",
  1356. "    \\Onnn (octal character code)\n",
  1357. "    \\Xhh  (hexadecimal character code)\n",
  1358. "    Terminate with carriage return.\n\n",
  1359. " Type the escape character again to send the escape character, or\n",
  1360. " press the space-bar to resume the CONNECT command.\n",
  1361. "----------------------------------------------------\n\n",
  1362. "" };
  1363. /*
  1364.   Need to save term characteristics/ allow disable binary mode
  1365.   print message, get text and then restore previous state.
  1366. */
  1367.     conoll("\r\n----------------------------------------------------");
  1368.     conol("\r\nPress C to return to ");
  1369.     conol(*myhost ? myhost : "the C-Kermit prompt");
  1370.     conoll(", or:");
  1371.     conola(hlpmsg);            /* Print the help message. */
  1372.     conol("Command>");            /* Prompt for command. */
  1373.     c = coninc(0) & 0x7f;
  1374.     conoc(c);                /* Echo it. */
  1375.     if (c != CMDQ)
  1376.       conoll("");
  1377.     conoll("----------------------------------------------------");
  1378.     doesc(c);
  1379. }
  1380.  
  1381. /*  D O E S C  --  Process an escape character argument  */
  1382.  
  1383. VOID
  1384. #ifdef CK_ANSIC
  1385. doesc(register unsigned char c)
  1386. #else
  1387. doesc(c) register unsigned char c;
  1388. #endif /* CK_ANSIC */
  1389. /* doesc() */ {
  1390.     int d;
  1391.     char temp[1024];
  1392.  
  1393.     c &= 0177;                /* Mask off 8th bit */
  1394.  
  1395.     if (c == escape) {            /* If it's the escape character, */
  1396.         d = dopar(c);            /* just send it. */
  1397.         ttoc(d);
  1398.     return;
  1399.     }
  1400.     if (isupper(c)) c = tolower(c);    /* Convert to lowercase letter. */
  1401.     if (iscntrl(c)) c += 'a' - '\001';
  1402.  
  1403.     switch (c) {            /* Take requested action. */
  1404.       case 'b':
  1405.     ttsndb();            /* Send a BREAK signal */
  1406.     break;
  1407. #ifdef TNCODE
  1408.       case 'i':                /* Send TELNET interrupt */
  1409. #ifndef IP
  1410. #define IP 244
  1411. #endif /* IP */
  1412.     if (network && ttnproto == NP_TELNET) { /* TELNET */
  1413.         CHAR temp[3];
  1414.         temp[0] = IAC;        /* I Am a Command */
  1415.         temp[1] = IP;        /* Interrupt Process */
  1416.         temp[2] = NUL;
  1417.         ttol((CHAR *)temp,2);
  1418.     } else conoc(BEL);
  1419.     break;
  1420.       case 'a':                /* "Are You There?" */
  1421.       case '\01':
  1422. #ifndef AYT
  1423. #define AYT 246
  1424. #endif /* AYT */
  1425.     if (network && ttnproto == NP_TELNET) {
  1426.         CHAR temp[3];
  1427.         temp[0] = IAC;        /* I Am a Command */
  1428.         temp[1] = AYT;        /* Are You There? */
  1429.         temp[2] = NUL;
  1430.         ttol((CHAR *)temp,2);
  1431.     } else conoc(BEL);
  1432. #endif /* TNCODE */
  1433.     break;
  1434.       case 'c':                /* Return to prompt */
  1435.     active = 0;
  1436.     conol("\r\n");
  1437.     break;
  1438.       case 'u':                /* Hang up the connection */
  1439. #ifndef NODIAL
  1440.     if (mdmhup() < 1)        /* Try via modem first, otherwise */
  1441. #endif /* NODIAL */
  1442.       tthang();            /* the old-fashioned way. */
  1443.     conol("\r\nHanging up ");
  1444.     break;
  1445.       case 'l':                /* Send a Long BREAK signal */
  1446.     ttsndlb();
  1447.     break;
  1448.       case 's':                /* Status */
  1449.     conoll("");
  1450.     conoll("----------------------------------------------------");
  1451. #ifdef NETCMD
  1452.     if (ttpipe)
  1453.       sprintf(temp, " Pipe: \"%s\"", ttname);
  1454.     else
  1455. #endif /* NETCMD */
  1456.       sprintf(temp, " %s: %s", (network ? "Host" : "Device"), ttname);
  1457.     conoll(temp);
  1458.     if (!network && speed >= 0L) {
  1459.         sprintf(temp,"Speed %ld", speed);
  1460.         conoll(temp);
  1461.     }
  1462.     sprintf(temp," Terminal echo: %s", duplex ? "local" : "remote");
  1463.     conoll(temp);
  1464.     sprintf(temp," Terminal bytesize: %d", (cmask  == 0177) ? 7 : 8);
  1465.     conoll(temp);
  1466.     sprintf(temp," Command bytesize: %d", (cmdmsk == 0177) ? 7 : 8 );
  1467.     conoll(temp);
  1468.     sprintf(temp," Parity: %s", parnam(parity));
  1469.     conoll(temp);
  1470. #ifdef CK_APC
  1471.     sprintf(temp," Autodownload: %s", autodl ? "on" : "off");
  1472.     conoll(temp);
  1473. #endif /* CK_APC */
  1474.     sprintf(temp," Session log: %s", *sesfil ? sesfil : "(none)");
  1475.     conoll(temp);
  1476. #ifndef NOSHOW
  1477.         if (!network) shomdm();
  1478. #endif /* NOSHOW */
  1479. #ifdef CKLOGDIAL
  1480.         {
  1481.         long z;
  1482.         extern int dologshow();
  1483.         z = dologshow(0);
  1484.         if (z > -1L) {
  1485.             sprintf(temp," Elapsed time: %s",hhmmss(z));
  1486.             conoll(temp);
  1487.         }
  1488.         }
  1489. #endif /* CKLOGDIAL */
  1490.         conoll("----------------------------------------------------");
  1491.         return;
  1492.       case '!':
  1493.       case '@':
  1494. #ifndef NOPUSH
  1495.     if (!nopush) {
  1496.         conres();            /* Put console back to normal */
  1497.         zshcmd("");            /* Start DCL. */
  1498.         if (conbin(escape) < 0) {
  1499.         printf("Error returning to remote session\n");
  1500.         active = 0;
  1501.         }
  1502.     } else
  1503.       conoc(BEL);
  1504. #else
  1505.     conoc(BEL);
  1506. #endif /* NOPUSH */
  1507.     return;
  1508.  
  1509.       case 'x':                /* XON */
  1510. /*
  1511.   NOTE: Here we should also issue QIO's to clear XOFF deadlock.
  1512. */
  1513.     ttoc(dopar(XON));
  1514.     break;
  1515.       case 'h':                /* Help */
  1516.       case '?':                /* Give Help */
  1517.     hconne();
  1518.     break;
  1519.       case '0':                /* Send a NULL */
  1520.     c = '\0';
  1521.     d = dopar(c);
  1522.     ttoc(d);
  1523.     break;
  1524.       case SP:                /* Ignore space */
  1525.     break;
  1526.       default:
  1527.     if (c == CMDQ) {        /* Backslash escape */
  1528.         int x;
  1529.         kbp = kbuf;
  1530.         *kbp++ = c;
  1531.         while (((c = (coninc(0) & cmdmsk)) != '\r') && (c != '\n'))
  1532.           *kbp++ = c;
  1533.         *kbp = NUL; kbp = kbuf;
  1534.         x = xxesc(&kbp);
  1535.         if (x >= 0) {
  1536.         c = dopar(x);
  1537.         ttoc(c);
  1538.         return;
  1539.         } else {
  1540.         conoc(BEL);
  1541.         return;
  1542.         }
  1543.     }
  1544.     conoc(BEL); return;        /* Invalid esc arg, beep */
  1545.     }
  1546. }
  1547.