home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc190.zip / ckvcon.c < prev    next >
C/C++ Source or Header  |  1994-09-18  |  35KB  |  1,048 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 5A(036) 15 Sep 94";
  6. #ifdef __ALPHA
  7.          /* do nothing */
  8. #else
  9. # ifdef VAX
  10. #  module ckvcon "5.0-036"
  11. # else
  12. #ifndef __GNUC__
  13.       ERROR -- CKVCON.C unknown architecture, neither VAX(tm) nor Alpha
  14. # endif /* __GNUC__ */
  15. # endif /* VAX */
  16. #endif /* __ALPHA */
  17.  
  18. char *connv = CONN_OS_ARCH_STRING;
  19.  
  20. /*  C K V C O N  --  Dumb terminal connection to remote system, for VMS  */
  21. /*
  22.   Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  23.   Columbia University Academic Information Systems, New York City.
  24.  
  25.   Copyright (C) 1985, 1994, Trustees of Columbia University in the City of New
  26.   York.  The C-Kermit software may not be, in whole or in part, licensed or
  27.   sold for profit as a software product itself, nor may it be included in or
  28.   distributed with commercial products or otherwise distributed by commercial
  29.   concerns to their clients or customers without written permission of the
  30.   Office of Kermit Development and Distribution, Columbia University.  This
  31.   copyright notice must not be removed, altered, or obscured.
  32. */
  33. /*
  34.  * Orignally adapted from the UNIX C-Kermit CONNECT module by S. Rubenstein
  35.  * for systems without fork().  This version of conect() uses contti(&c, &src)
  36.  * to return when a character is available from either the console or the comm
  37.  * line, to allow sending/receiving without breaking the connection.
  38.  *
  39.  * Edit by
  40.  *
  41.  * 010 06-Mar-1989 mab    General cleanup
  42.  * 011 23-Mar-1989 mab    Clean up doesc() code.  Add malloc() in place of
  43.  *             static buffer space.  Also increase buffer space.
  44.  * 012 27-Sep-1989 mab    Add XON sequence.
  45.  * 013 30-Mar-1991 fdc  Add so/si, character set translation.
  46.  * 014 06-Apr-1991 fdc  Adapted for TGV MultiNet TCP/IP connections.
  47.  * 015 21-Jun-1991 tmk  Cleaned up typo in session logging display.
  48.  * 016 28-Nov-1991 fdc  "Back at <hostname>", disallow CONNECT in background
  49.  * 017 25-Dec-1991 fdc  Added support for NOPUSH, added "Hanging up" message
  50.  * 018 11-Jan-1992 fdc  Added support for key mapping
  51.  * 019 27-Jan-1992 fdc  Added support for ANSI escape sequence recognition
  52.  * 020 00-May-1992 fdc  Rewrote conect() to used buffered i/o.
  53.  * 021 10-Jun-1992 fdc  Added support for Wollongong WIN/TCP from Ray Hunter.
  54.  *                      Fix messed-up help message.
  55.  * 022 20-Jun-1992 fdc  Fixed handling of CR on TELNET sessions.
  56.  * 023 28-Jun-1992 fdc  Cosmetic cleanup of "Connecting..." message.
  57.  * 024 12-Jul-1992 fdc  Added mdmhup() feature (see ckudia.c).
  58.  * 025 29-Jul-1992 fdc  Grouped TELNET IP and AYT into single writes.
  59.  * 026 13-Aug-1992 fdc  Added support for SET TELNET NEWLINE-MODE.
  60.  * 027 05-Sep-1992 lt   Added architecture ifdefs for OpenVMS, Alpha, at top.
  61.  * 028 08-Sep-1992 fdc  Separated input and output SO/SO shift states.
  62.  * 029 11-Oct-1992 fdc  Reduce modem-vs-net confusion in ttopen() calls.
  63.  * 030 27-Oct-1993 fdc  Correct a typo in network protocol-checking code.
  64.  * 031 23-Oct-1993 fdc  Reset i/o buffer pointers upon new connection.
  65.  * 032 14-Feb-1994 fdc  Some minor cleanups.
  66.  * 033  9-Mar-1994 fdc  Fixed failure to display quoted TELNET IAC (255).
  67.  * 034  2-Jul-1994 fdc  Add initial APC support.
  68.  * 035  9-Jul-1994 fdc  Fix APC support.
  69.  * 036 15-Sep-1994 fdc  Add support for SET TELNET NEWLINE RAW.
  70.  */
  71.  
  72. #include "ckcdeb.h"
  73. #include "ckcasc.h"
  74. #include "ckcker.h"
  75. #include "ckucmd.h"
  76. #include "ckcnet.h"
  77. #include "ckvvms.h"
  78. #ifndef NOCSETS
  79. #include "ckcxla.h"            /* Character set translation */
  80. #endif /* NOCSETS */
  81. #include <stdio.h>
  82. #include <ctype.h>
  83. #include <signal.h>
  84. #include <setjmp.h>
  85.  
  86. static int src;                /* Where input character came from */
  87.  
  88. extern int local, speed, escape, duplex, parity, flow, seslog, mdmtyp, batch;
  89. extern int cmask, cmdmsk, debses, sosi, ttyfd, what, quiet, tnlm,
  90.  tt_crd, tn_nlm;
  91. extern char ttname[], sesfil[], myhost[];
  92.  
  93. #ifndef NOICP                /* Keyboard mapping */
  94. #ifndef NOSETKEY
  95. extern KEY *keymap;            /* Single-character key map */
  96. extern MACRO *macrotab;            /* Key macro pointer table */
  97. static MACRO kmptr = NULL;        /* Pointer to current key macro */
  98. #endif /* NOSETKEY */
  99. #endif /* NOICP */
  100.  
  101. /* Network support */
  102. extern int ttnproto,            /* Virtual terminal protocol */
  103.   network,                /* Network connection active */
  104.   nettype;                /* Network type */
  105.  
  106. #ifndef NOCSETS
  107. #ifdef CK_ANSIC
  108. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Character set */
  109. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* translation functions */
  110. static CHAR (*sxo)(CHAR);    /* Local translation functions */
  111. static CHAR (*rxo)(CHAR);    /* for output (sending) terminal chars */
  112. static CHAR (*sxi)(CHAR);    /* and for input (receiving) terminal chars. */
  113. static CHAR (*rxi)(CHAR);
  114. #else
  115. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])();    /* Character set */
  116. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])();    /* translation functions. */
  117. static CHAR (*sxo)();        /* Local translation functions */
  118. static CHAR (*rxo)();        /* for output (sending) terminal chars */
  119. static CHAR (*sxi)();        /* and for input (receiving) terminal chars. */
  120. static CHAR (*rxi)();
  121. #endif /* CK_ANSIC */
  122. extern int language;        /* Current language. */
  123. extern struct csinfo fcsinfo[]; /* File character set info */
  124. extern int tcsr, tcsl;        /* Terminal character sets, remote & local. */
  125. static int langsv;        /* Remember language */
  126. static int tcs;            /* Intermediate (xfer) char set */
  127.  
  128. _PROTOTYP( VOID doesc, (unsigned char) );
  129. #endif /* NOCSETS */
  130.  
  131. #ifdef NOCSETS                /* No character sets */
  132. #ifndef CK_APC                /* No APC */
  133. #ifndef NOESCSEQ
  134. #define NOESCSEQ            /* So no escape sequence recognizer */
  135. #endif /* NOESCSEQ */
  136. #endif /* CK_APC */
  137. #endif /* NOCSETS */
  138.  
  139. #ifdef CK_APC
  140. extern int apcactive;            /* Application Program Command (APC) */
  141. extern int apcstatus;            /* items ... */
  142. static int apclength = 0;          
  143. #ifdef DCMDBUF
  144. extern char *apcbuf;
  145. #else
  146. extern char apcbuf[];
  147. #endif /* DCMDBUF */
  148. static int apcbuflen = APCBUFLEN - 2;
  149. #endif /* CK_APC */
  150.  
  151. int i, active;                /* Variables global to this module */
  152. static char *p;
  153.  
  154. #ifndef NOESCSEQ
  155. /*
  156.   As of edit 178, the CONNECT command will skip past ANSI escape sequences
  157.   to avoid translating the characters within them.  This allows the CONNECT
  158.   command to work correctly when connected to a remote host that uses a
  159.   7-bit ISO 646 national character set, in which characters like '[' would
  160.   normally be translated into accented characters, ruining the terminal's
  161.   interpretation (and generation) of escape sequences.
  162.  
  163.   Escape sequences of non-ANSI/ISO-compliant terminals are not handled.
  164. */
  165. /*
  166.   States for the escape-sequence recognizer.
  167. */
  168. #define ES_NORMAL 0            /* Normal, not in escape sequence */
  169. #define ES_GOTESC 1            /* Current character is ESC */
  170. #define ES_ESCSEQ 2            /* Inside an escape sequence */
  171. #define ES_GOTCSI 3            /* Inside a control sequence */
  172. #define ES_STRING 4            /* Inside OSC, PM, or APC string */
  173. #define ES_TERMIN 5            /* 1st char of string terminator */
  174.  
  175. static int
  176.   escseq = 0,                /* Skip over ANSI escape sequences */
  177.   inesc = ES_NORMAL,            /* State of sequence recognizer */
  178.   oldesc = -1;                /* Previous state of recognizer */
  179. /*
  180.   ANSI escape sequence handling.  Only the 7-bit form is treated, because
  181.   translation is not a problem in the 8-bit environment, in which all GL
  182.   characters are ASCII and no translation takes place.  So we don't check
  183.   for the 8-bit single-character versions of CSI, DCS, OSC, APC, or ST.  Here
  184.   is the ANSI sequence recognizer state table, followed by the code that
  185.   implements it.
  186.  
  187.   Definitions:
  188.     CAN = Cancel                       01/08         Ctrl-X
  189.     SUB = Substitute                   01/10         Ctrl-Z
  190.     DCS = Device Control Sequence      01/11 05/00   ESC P
  191.     CSI = Control Sequence Introducer  01/11 05/11   ESC [
  192.     ST  = String Terminator            01/11 05/12   ESC \
  193.     OSC = Operating System Command     01/11 05/13   ESC ]
  194.     PM  = Privacy Message              01/11 05/14   ESC ^
  195.     APC = Application Program Command  01/11 05/15   ESC _
  196.  
  197.   ANSI escape sequence recognizer:
  198.  
  199.     State    Input  New State  ; Commentary
  200.  
  201.     NORMAL   (start)           ; Start in NORMAL state
  202.  
  203.     (any)    CAN    NORMAL     ; ^X cancels
  204.     (any)    SUB    NORMAL     ; ^Z cancels
  205.  
  206.     NORMAL   ESC    GOTESC     ; Begin escape sequence
  207.     NORMAL   other             ; NORMAL control or graphic character
  208.  
  209.     GOTESC   ESC               ; Start again
  210.     GOTESC   [      GOTCSI     ; CSI
  211.     GOTESC   P      STRING     ; DCS introducer, consume through ST
  212.     GOTESC   ]      STRING     ; OSC introducer, consume through ST
  213.     GOTESC   ^      STRING     ; PM  introducer, consume through ST
  214.     GOTESC   _      STRING     ; APC introducer, consume through ST
  215.     GOTESC   0..~   NORMAL     ; 03/00 through 17/14 = Final character
  216.     GOTESC   other  ESCSEQ     ; Intermediate or ignored control character
  217.  
  218.     ESCSEQ   ESC    GOTESC     ; Start again
  219.     ESCSEQ   0..~   NORMAL     ; 03/00 through 17/14 = Final character
  220.     ESCSEQ   other             ; Intermediate or ignored control character
  221.  
  222.     GOTCSI   ESC    GOTESC     ; Start again
  223.     GOTCSI   @..~   NORMAL     ; 04/00 through 17/14 = Final character
  224.     GOTCSI   other             ; Intermediate char or ignored control char
  225.  
  226.     STRING   ESC    TERMIN     ; Maybe have ST
  227.     STRING   other             ; Consume all else
  228.  
  229.     TERMIN   \      NORMAL     ; End of string
  230.     TERMIN   other  STRING     ; Still in string
  231. */
  232. /*
  233.   chkaes() -- Check ANSI Escape Sequence.
  234.   Call with EACH character in input stream.
  235.   Sets global inesc variable according to escape sequence state.
  236. */
  237. VOID
  238. #ifdef CK_ANSIC
  239. chkaes(char c)
  240. #else
  241. chkaes(c) char c;
  242. #endif /* CK_ANSIC */
  243. /* chkaes */ {
  244.  
  245.     oldesc = inesc;            /* Remember previous state */
  246.     if (c == CAN || c == SUB)        /* CAN and SUB cancel any sequence */
  247.       inesc = ES_NORMAL;
  248.     else                /* Otherwise */
  249.       switch (inesc) {            /* enter state switcher */
  250.  
  251.     case ES_NORMAL:            /* NORMAL state */
  252.       if (c == ESC)            /* Got an ESC */
  253.         inesc = ES_GOTESC;        /* Change state to GOTESC */
  254.       break;            /* Otherwise stay in NORMAL state */
  255.  
  256.     case ES_GOTESC:            /* GOTESC state */
  257.       if (c == '[')            /* Left bracket after ESC is CSI */
  258.         inesc = ES_GOTCSI;        /* Change to GOTCSI state */
  259.       else if (c == 'P' || (c > 0134 && c < 0140)) { /* P, [, ^, or _ */
  260.           inesc = ES_STRING;    /* Switch to STRING-absorption state */
  261. #ifdef CK_APC
  262.           if (c == '_' && apcstatus != APC_OFF) { /* APC */
  263.           debug(F100,"APC begin","",0);
  264.           apcactive = 1;    /* Set APC-Active flag */
  265.           apclength = 0;    /* and reset APC buffer pointer */
  266.           }
  267. #endif /* CK_APC */
  268.       } else if (c > 057 && c < 0177) /* Final character '0' thru '~' */
  269.         inesc = ES_NORMAL;        /* Back to normal */
  270.       else if (c != ESC)        /* ESC in an escape sequence... */
  271.         inesc = ES_ESCSEQ;        /* starts a new escape sequence */
  272.       break;            /* Intermediate or ignored ctrl char */
  273.  
  274.     case ES_ESCSEQ:            /* ESCSEQ -- in an escape sequence */
  275.       if (c > 057 && c < 0177)    /* Final character '0' thru '~' */
  276.         inesc = ES_NORMAL;        /* Return to NORMAL state. */
  277.       else if (c == ESC)        /* ESC ... */
  278.         inesc = ES_GOTESC;        /* starts a new escape sequence */
  279.       break;            /* Intermediate or ignored ctrl char */
  280.  
  281.     case ES_GOTCSI:            /* GOTCSI -- In a control sequence */
  282.       if (c > 077 && c < 0177)    /* Final character '@' thru '~' */
  283.         inesc = ES_NORMAL;        /* Return to NORMAL. */
  284.       else if (c == ESC)        /* ESC ... */
  285.         inesc = ES_GOTESC;        /* starts over. */
  286.       break;            /* Intermediate or ignored ctrl char */
  287.  
  288.     case ES_STRING:            /* Inside a string */
  289.       if (c == ESC)            /* ESC may be 1st char of terminator */
  290.         inesc = ES_TERMIN;        /* Go see. */
  291. #ifdef CK_APC
  292.       else if (apcactive && (apclength < apcbuflen)) /* If in APC string */
  293.         apcbuf[apclength++] = c;    /* deposit this character */
  294. #endif /* CK_APC */
  295.       break;            /* Absorb all other characters. */
  296.  
  297.     case ES_TERMIN:            /* May have a string terminator */
  298.       if (c == '\\') {        /* which must be backslash */
  299.           inesc = ES_NORMAL;    /* If so, back to NORMAL */
  300. #ifdef CK_APC
  301.           if (apcactive) {        /* If it was an APC string, */
  302.           apcbuf[apclength] = NUL; /* terminate it and then ... */
  303.           debug(F111,"APC complete",apcbuf,apclength);
  304.           printf("\r\n");    /* prevent CR-overstriking and...    */
  305.           ckcputf();        /* flush screen-output buffer and... */
  306.           active = 0;        /* exit from the terminal emulator.. */
  307.           return;        /* with the apcactive flag still set */
  308.           }
  309. #endif /* CK_APC */
  310.       } else {            /* Otherwise */
  311.           inesc = ES_STRING;    /* Back to string absorption. */
  312. #ifdef CK_APC
  313.           if (apcactive && (apclength+1 < apcbuflen)) { /* In APC string */
  314.           apcbuf[apclength++] = ESC; /* deposit the Esc character */
  315.           apcbuf[apclength++] = c;   /* and this character too */
  316.           }
  317. #endif /* CK_APC */
  318.       }
  319.       }
  320. }
  321. #endif /* NOESCSEQ */
  322.  
  323. static char *ibp;            /* Input buffer pointer */
  324. static int ibc;                /* Input buffer count */
  325. #define IBUFL 1024            /* Input buffer length */
  326.  
  327. static char *obp;            /* Output buffer pointer */
  328. static int obc;                /* Output buffer count */
  329. #define OBUFL 1024            /* Output buffer length */
  330.  
  331. #ifdef DYNAMIC
  332. static char *ibuf = NULL, *obuf = NULL;    /* Line and temp buffers */
  333. #else
  334. static char ibuf[IBUFL], obuf[OBUFL];
  335. #endif /* DYNAMIC */
  336.  
  337. char kbuf[10], *kbp;            /* Keyboard buffer */
  338.  
  339. /*  C O N E C T  --  Perform terminal connection  */
  340.  
  341. int inshift, outshift;            /* SO/SI shift states */
  342.  
  343. /*  C K C P U T C  --  C-Kermit CONNECT Put Character to Screen  */
  344. /*
  345.   Output is buffered to avoid slow screen writes on fast connections.
  346. */
  347. int
  348. ckcputf() {                /* Dump the output buffer */
  349.     int x;
  350.     if (obc > 0)            /* If we have any characters, */
  351.       x = conxo(obc,obuf);        /* dump them, */
  352.     obp = obuf;                /* reset the pointer */
  353.     obc = 0;                /* and the counter. */
  354.     return(x);                /* Return conxo's return code */
  355. }
  356.  
  357. int
  358. ckcputc(c) int c; {
  359.     int x;
  360.  
  361.     *obp++ = c & 0xff;            /* Deposit the character */
  362.     obc++;                /* Count it */
  363.     if (ibc == 0 ||            /* If input buffer empty */
  364.     obc == OBUFL) {            /* or output buffer full */
  365.     x = conxo(obc,obuf);        /* dump the buffer, */
  366.     obp = obuf;            /* reset the pointer */
  367.     obc = 0;            /* and the counter. */
  368.     return(x);            /* Return conxo's return code */
  369.     } else return(0);
  370. }
  371.  
  372. /*  C K C G E T C  --  C-Kermit CONNECT Get Character  */
  373. /*
  374.   Buffered read from communication device.
  375.   Returns the next character, refilling the buffer if necessary.
  376.   On error, returns ttinc's return code (see ttinc() description).
  377.   Dummy argument for compatible calling conventions with ttinc().
  378.   NOTE: We don't have a macro for this because we have to pass
  379.   a pointer to this function as an argument to tn_doop().
  380. */
  381. int
  382. ckcgetc(dummy) int dummy; {
  383.     int c, n;
  384.  
  385.     if (ibc > 0) {            /* Have buffered port characters */
  386.     src = 1;            /* Say source is port */
  387.     c = *ibp++ & 0xff;        /* Get next character */
  388.     ibc--;                /* Reduce input buffer count */
  389.     debug(F101,"CKCGETC buffered port char","",c);
  390.     return(c);            /* Return buffered port character */
  391.     } else {                /* Need to refill buffer */
  392.     contti(&c, &src);        /* Read one character */
  393.     if (src < 0) {            /* If error, return error code */
  394.         return(src);
  395.     } else if (src == 0) {        /* Got a character from the keyboard */
  396.         debug(F101,"CKCGETC keyboard char","",c);
  397.         return(c);
  398.     } else {            /* Got a port character */
  399.         ibp = ibuf;            /* Reset buffer pointer */
  400.         *ibp++ = c;            /* Deposit the character */
  401.         ibc++;            /* and count it */
  402.         if ((n = ttchk()) > 0) {    /* Any more characters waiting? */
  403.         if (n > (IBUFL - ibc))    /* Get them all at once. */
  404.           n = IBUFL - ibc;    /* Don't overflow the buffer */
  405.         if ((n = ttxin(n,(CHAR *)ibp)) > 0) /* Read waiting chars */
  406.           ibc += n;        /* Advance counter */
  407.         }
  408.         ibp = ibuf;            /* Reset buffer pointer again */
  409.         c = *ibp++ & 0xff;        /* Get first character from buffer */
  410.         ibc--;            /* Reduce buffer count */
  411.         debug(F101,"CKCGETC port char","",c);
  412.         return(c);            /* Return the character */
  413.     }
  414.     }
  415. }
  416.  
  417. int
  418. conect() {
  419.     int c;            /* c is a character, but must be signed
  420.                    integer to pass thru -1, which is the
  421.                    modem disconnection signal, and is
  422.                    different from the character 0377 */
  423.     int c2, csave;        /* Copies of c */
  424.     char errmsg[50], *erp, *cp;
  425.     int n, x;                /* Workers */
  426.  
  427.     if (!local) {
  428. #ifdef NETCONN
  429.     printf("Sorry, you must SET LINE or SET HOST first\n");
  430. #else
  431.     printf("Sorry, you must SET LINE first\n");
  432. #endif /* NETCONN */
  433.     return(-2);
  434.     }
  435.     if (batch) {
  436.     printf("\n\
  437. Sorry, Kermit's CONNECT command can be used only on a real terminal.\n");
  438.     printf("If this is not a batch a job, then you must:\n\n");
  439.     printf("$ DEFINE SYS$INPUT SYS$COMMAND\n\n");
  440.     printf("in your DCL command procedure before starting Kermit.\n\n");
  441.     return(0);
  442.     }
  443. #ifdef TCPSOCKET
  444.     if (network && (nettype != NET_TCPB)) {
  445.     printf("Sorry, network type not supported yet\n");
  446.     return(0);
  447.     }
  448. #endif /* TCPSOCKET */
  449.  
  450.     if (speed < 0 && network == 0) {
  451.     printf("Sorry, you must SET SPEED first\n");
  452.     return(-2);
  453.     }
  454.     if ((escape < 0) || (escape > 0177)) {
  455.     printf("Your escape character is not ASCII - %d\n",escape);
  456.     return(-2);
  457.     }
  458.     if (ttyfd < 0) {            /* If communication device not open */
  459.     debug(F111,"ckvcon opening",ttname,0); /* Open it now. */
  460.     if (ttopen(ttname,
  461.            &local,
  462.            network ? -nettype : mdmtyp,
  463.            0
  464.            ) < 0) {
  465.         erp = errmsg;
  466.         sprintf(erp,"Sorry, can't open %s",ttname);
  467.         perror(errmsg);
  468.         debug(F110,"ckvcon open failure",errmsg,0);
  469.         return(-2);
  470.     }
  471.     }
  472.     if (!quiet) {
  473. #ifdef NETCONN
  474.     if (network) {
  475.         printf("\nConnecting to host %s",ttname);
  476.     } else {
  477. #endif /* NETCONN */
  478.         printf("\nConnecting to %s",ttname);
  479.         if (speed > -1L) printf(", speed %ld",speed);
  480. #ifdef NETCONN
  481.     }
  482. #endif /* NETCONN */
  483.     printf(".\r\nThe escape character is %s (ASCII %d).\r\n",
  484.            dbchr(escape),escape);
  485.     printf("Type the escape character followed by C to get back,\r\n");
  486.     printf("or followed by ? to see other options.\r\n");
  487.     if (seslog) {
  488.         printf("(Session logged to %s)\r\n",sesfil);
  489.     }
  490.     if (debses) printf("Debugging Display...)\r\n");
  491.     printf("\r\n");
  492.     }
  493.  
  494. /* Condition console terminal and communication line */
  495.  
  496.     if (conbin(escape) < 0) {
  497.     printf("Sorry, can't condition console terminal\n");
  498.     return(-2);
  499.     }
  500.     if (ttvt(speed,flow) < 0) {
  501.     /* tthang(); */ /* Closing it should be quite enough! */
  502.     ttclos(0);
  503.     if (ttopen(ttname,        /* Open it again... */
  504.            &local,
  505.            network ? -nettype : mdmtyp,
  506.            0
  507.            ) < 0) {
  508.         erp = errmsg;
  509.         sprintf(erp,"Sorry, can't reopen %s",ttname);
  510.         perror(errmsg);
  511.         return(0);
  512.     }
  513.     if (ttvt(speed,flow) < 0) {    /* Try virtual terminal mode again. */
  514.         conres();            /* Failure this time is fatal. */
  515.         printf("Sorry, Can't condition communication line\n");
  516.         return(0);
  517.     }
  518.     }
  519.     debug(F101,"connect ttvt ok, escape","",escape);
  520.  
  521. #ifdef CK_APC
  522.     apcactive = apclength = 0;
  523. #endif /* CK_APC */
  524.  
  525. #ifndef NOCSETS
  526. /* Set up character set translations */
  527.  
  528. #ifdef KANJI
  529. /* Kanji not supported yet */
  530.     if (fcsinfo[tcsr].alphabet == AL_JAPAN ||
  531.     fcsinfo[tcsl].alphabet == AL_JAPAN ) {
  532.     tcs = TC_TRANSP;
  533.     } else
  534. #endif /* KANJI */
  535. #ifdef CYRILLIC
  536.       if (fcsinfo[tcsl].alphabet == AL_CYRIL) {
  537.       tcs = TC_CYRILL;
  538.       } else
  539. #endif /* CYRILLIC */
  540.     tcs = TC_1LATIN;
  541.  
  542.     if (tcsr == tcsl) {            /* Remote and local sets the same? */
  543.     sxo = rxo = NULL;        /* If so, no translation. */
  544.     sxi = rxi = NULL;
  545.     } else {                /* Otherwise, set up */
  546.     sxo = xls[tcs][tcsl];        /* translation function */
  547.     rxo = xlr[tcs][tcsr];        /* pointers for output functions */
  548.     sxi = xls[tcs][tcsr];        /* and for input functions. */
  549.     rxi = xlr[tcs][tcsl];
  550.     }
  551. /*
  552.   This is to prevent use of zmstuff() and zdstuff() by translation functions.
  553.   They only work with disk i/o, not with communication i/o.  Luckily Russian
  554.   translation functions don't do any stuffing...
  555. */
  556.     langsv = language;
  557. #ifndef NOCYRIL
  558.     if (language != L_RUSSIAN)
  559. #endif /* NOCYRIL */
  560.       language = L_USASCII;
  561.  
  562. #ifndef NOESCSEQ
  563. /*
  564.   We need to activate escape-sequence recognition when:
  565.   (a) translation is elected, and:
  566.   (b) the local and/or remote set is 7-bit set other than US or UK ASCII, or:
  567.   (c) TERMINAL APC is not OFF.
  568. */
  569.     escseq = (tcs != TC_TRANSP) &&    /* Not transparent */
  570.       (fcsinfo[tcsl].size == 128 || fcsinfo[tcsr].size == 128) && /* 7 bits */
  571.     (fcsinfo[tcsl].code != FC_USASCII) && /* Not US ASCII */
  572.     (fcsinfo[tcsl].code != FC_UKASCII);   /* Not UK ASCII */
  573. #ifdef COMMENT
  574.     debug(F101,"tcs","",tcs);
  575.     debug(F101,"tcsl","",tcsl);
  576.     debug(F101,"tcsr","",tcsr);
  577.     debug(F101,"fcsinfo[tcsl].size","",fcsinfo[tcsl].size);
  578.     debug(F101,"fcsinfo[tcsr].size","",fcsinfo[tcsr].size);
  579. #endif /* COMMENT */
  580. #endif /* NOESCSEQ */
  581. #endif /* NOCSETS */
  582.  
  583. #ifndef NOESCSEQ
  584. #ifdef CK_APC
  585.     escseq = escseq || (apcstatus != APC_OFF);
  586. #endif /* CK_APC */
  587.     inesc = ES_NORMAL;            /* Initial state of recognizer */
  588.     debug(F101,"escseq","",escseq);
  589. #endif /* NOESCSEQ */
  590.  
  591. #ifdef DYNAMIC
  592.     if (!ibuf) {
  593.     if (!(ibuf = malloc(IBUFL+1))) { /* Allocate input line buffer */
  594.         printf("Sorry, CONNECT input buffer can't be allocated\n");
  595.         return(0);
  596.     } else {
  597.         ibp = ibuf;
  598.         ibc = 0;
  599.     }
  600.     }
  601.     if (!obuf) {
  602.     if (!(obuf = malloc(OBUFL+1))) {    /* Allocate output line buffer */
  603.         printf("Sorry, CONNECT output buffer can't be allocated\n");
  604.         return(0);
  605.     } else {
  606.         obp = obuf;
  607.         obc = 0;
  608.     }
  609.     }
  610. #else
  611.     ibp = ibuf;
  612.     ibc = 0;
  613.     obp = obuf;
  614.     obc = 0;
  615. #endif /* DYNAMIC */
  616.  
  617.     inshift = outshift = 0;        /* Initial SI/SO states */
  618.     active = 1;
  619.  
  620.     do {                /* Top of big loop */
  621. #ifndef NOSETKEY
  622. /*
  623.   Before reading anything from the keyboard, continue expanding the current
  624.   active keyboard macro, if any.
  625. */
  626.     if (kmptr) {            /* Have active macro */
  627.         src = 0;            /* Pretend char came from keyboard */
  628.         if ((c = (CHAR) *kmptr++) == NUL) { /* but get it from the macro */
  629.         kmptr = NULL;        /* If no more chars in macro,  */
  630.         continue;        /* reset pointer and continue. */
  631.         }
  632.     } else                 /* OTHERWISE... */
  633. #endif /* NOSETKEY */
  634. /*
  635.   contti() samples the keyboard and the communication device, in that order,
  636.   in what amounts to a busy loop, and does not return until it has something
  637.   from one or the other.  This drives the VAX crazy, and also gives poor
  638.   performance: input is character-at a time, and the keyboard buffer is
  639.   checked at least once for every character that comes in from the remote.
  640.   Somebody who knows something about VMS, PLEASE FIND A BETTER WAY.
  641.  
  642.   We want a version of contti() that does what select() does in BSD, and we
  643.   also want it to be buffered internally, so it doesn't have to call the
  644.   operating system for every single input character.  And most of all we don't
  645.   want it to run a busy loop all the time.  No doubt this involves ASTs,
  646.   things of which I know nothing.    - fdc
  647. */
  648.     c = ckcgetc(0);            /* Calls contti()... */
  649. /*
  650.   Get here with a character in c, and:
  651.  
  652.   src = -1 Communication error
  653.          1 Character from comm line
  654.          0 Character from console
  655. */
  656.     if (src < 0) {            /* Comm line hangup or other error */
  657. /*
  658.   We should check WHY src < 0 and not just dive under for ANY reason.
  659. */
  660.         if (!quiet) printf("\r\nCommunications disconnect ");
  661.         active = 0;
  662.     } else if (!src) {
  663. /*
  664.    Character from console
  665. */
  666.         c &= cmdmsk;        /* Do requested masking */
  667. #ifndef NOICP
  668. #ifndef NOSETKEY
  669. /*
  670.   Note: kmptr is NULL if we got character c from the keyboard, and it is
  671.   not NULL if it came from a macro.  In the latter case, we must avoid
  672.   expanding it again.
  673. */
  674.         if (!kmptr && macrotab[c]) { /* If a macro is assigned to it */
  675.         kmptr = macrotab[c];    /* set up the pointer */
  676.         continue;        /* and do this again. */
  677.         } else c = keymap[c];    /* Else use single-char keymap */
  678. #endif /* NOSETKEY */
  679. #endif /* NOICP */
  680.         csave = c;
  681.         if (
  682. #ifndef NOICP
  683. #ifndef NOSETKEY
  684.             !kmptr &&
  685. #endif /* NOSETKEY */
  686. #endif /* NOICP */
  687.         ((c & 0x7f) == escape)) { /* Escape character? */
  688.         conresne();        /* Restore to normal attributes */
  689.         c = coninc(0) & 0177;
  690.         doesc(c);
  691.         conbin(escape);
  692.         } else {            /* Ordinary character */
  693. #ifndef NOCSETS
  694. #ifdef NOESCSEQ
  695.         /* Translate character sets */
  696.         if (sxo) c = (*sxo)(c); /* From local to intermediate. */
  697.         if (rxo) c = (*rxo)(c); /* From intermediate to remote. */
  698. #else
  699.         if (inesc == ES_NORMAL) { /* If not inside escape seq.. */
  700.             /* Translate character sets */
  701.             if (sxo) c = (*sxo)(c); /* Local to intermediate. */
  702.             if (rxo) c = (*rxo)(c); /* Intermediate to remote. */
  703.         }
  704.         if (escseq) chkaes(c); /* Check escape sequence status */
  705. #endif /* NOESCSEQ */
  706. #endif /* NOCSETS */
  707. /*
  708.  If Shift-In/Shift-Out is selected and we have a 7-bit connection,
  709.  handle shifting here.
  710. */
  711.         if (sosi) {             /* Shift-In/Out selected? */
  712.             if (cmask == 0177) { /* In 7-bit environment? */
  713.             if (c & 0200) {          /* 8-bit character? */
  714.                 if (outshift == 0) { /* If not shifted, */
  715.                 if (ttoc(dopar(SO)) < 0) { /* shift. */
  716.                     active = 0;
  717.                     continue;
  718.                 }
  719.                 outshift = 1;
  720.                 }
  721.             } else {
  722.                 if (outshift == 1) { /* 7-bit character */
  723.                 if (ttoc(dopar(SI)) < 0) { /* If shifted, */
  724.                     active = 0;
  725.                     continue;
  726.                 }
  727.                 outshift = 0; /* unshift. */
  728.                 }
  729.             }
  730.             }
  731.             if (c == SO) outshift = 1;    /* User typed SO */
  732.             if (c == SI) outshift = 0;    /* User typed SI */
  733.         }
  734.         c &= cmask;        /* Apply Kermit-to-host mask now. */
  735.  
  736.         if (c == '\015') {    /* Carriage Return */
  737.             int stuff = -1;
  738.             if (tnlm) {        /* TERMINAL NEWLINE ON */
  739.             stuff = LF;     /* Stuff LF */
  740. #ifdef TNCODE
  741.             } else if (network && /* TELNET NEWLINE ON/OFF/RAW */
  742.                    (ttnproto == NP_TELNET) &&
  743.                    (tn_nlm != TNL_CR)) {
  744.             stuff = (tn_nlm == TNL_CRLF) ? LF : NUL;
  745. #endif /* TNCODE */
  746.             }
  747.             if (stuff > -1) {
  748.             ttoc(dopar('\015'));    /* Send CR */
  749.             if (duplex) conoc('\015');    /* Maybe echo CR */
  750.             c = stuff;            /* Char to stuff */
  751.             csave = c;
  752.             }
  753.         }
  754. #ifdef TNCODE
  755. /* If user types the 0xff character (TELNET IAC), it must be doubled. */
  756.         else
  757.           if (dopar(c) == IAC && network && ttnproto == NP_TELNET) {
  758.                           /* Send one copy now */
  759.               ttoc(IAC);    /* and the other one just below. */
  760.           }
  761. #endif /* TNCODE */
  762.         if (ttoc(dopar(c)) < 0) { /* Now send the character. */
  763.             active = 0;
  764.             continue;
  765.         }
  766.         if (duplex) {        /* Half duplex? */
  767.             if (debses)        /* Yes, echo locally */
  768.               conol(dbchr(csave)); /* in appropriate mode */
  769.             else
  770.               conoc(csave);
  771.             if (seslog) zchout(ZSFILE,c); /* And maybe log it. */
  772.         }            
  773.         }
  774.     } else {
  775. /*
  776.   Character from communications device or network connection...
  777. */
  778. #ifdef TNCODE
  779.         /* Handle telnet options */
  780.         if (network && ttnproto == NP_TELNET && ((c & 0xff) == IAC)) {
  781.         ckcputf();        /* Dump output buffer */
  782.         if ((x = tn_doop(c & 0xff, duplex, ckcgetc)) == -1 && !quiet)
  783.           printf("\r\nCommunications disconnect ");
  784.         if (x == 1) duplex = 1;    /* Change duplex if necessary. */
  785.         if (x == 2) duplex = 0;
  786.         if (x == 3)        /* Quoted IAC */
  787.           c = 255;
  788.         else
  789.           continue;
  790.         }
  791. #endif /* TNCODE */
  792.         if (debses) {        /* Output character to screen */
  793.         char *s;        /* Debugging display... */
  794.         s = dbchr(c);
  795.         while (*s)
  796.           ckcputc(*s++);
  797.         } else {            /* or regular... */
  798.         c &= cmask;        /* Do first masking */
  799.         if (sosi) {        /* Handle SI/SO */
  800.             if (c == SO) {    /* Shift Out */
  801.             inshift = 1;
  802.             continue;
  803.             } else if (c == SI) { /* Shift In */
  804.             inshift = 0;
  805.             continue;
  806.             }
  807.             if (inshift) c |= 0200;
  808.         }
  809. #ifndef NOCSETS
  810.         if (
  811. #ifndef NOESCSEQ
  812.             inesc == ES_NORMAL    /* If not in an escape sequence */
  813. #else
  814.             1
  815. #endif /* NOESCSEQ */
  816.             ) {            /* Translate character sets */
  817.             if (sxi) c = (*sxi)((CHAR)c);
  818.             if (rxi) c = (*rxi)((CHAR)c);
  819.         }
  820. #endif /* NOCSETS */
  821.  
  822. #ifndef NOESCSEQ
  823.             if (escseq)        /* If handling escape sequences */
  824.               chkaes((char)c);    /* update our state */
  825. #ifdef CK_APC
  826. /*
  827.   If we are handling APCs, we have several possibilities at this point:
  828.    1. Ordinary character to be written to the screen.
  829.    2. An Esc; we can't write it because it might be the beginning of an APC.
  830.    3. The character following an Esc, in which case we write Esc, then char,
  831.       but only if we have not just entered an APC sequence.
  832. */
  833.         if (escseq && apcstatus != APC_OFF) {
  834.             if (inesc == ES_GOTESC) /* Don't write ESC yet */
  835.               continue;
  836.             else if (oldesc == ES_GOTESC && !apcactive) {
  837.             ckcputc(ESC);    /* Write saved ESC */
  838.             if (seslog) zchout(ZSFILE,c);
  839.             }
  840.         }
  841. #endif /* CK_APC */
  842. #endif /* NOESCSEQ */
  843.         if (
  844. #ifdef CK_APC
  845.             !apcactive
  846. #else
  847.             1
  848. #endif /* CK_APC */
  849.             ) {
  850.             c &= cmdmsk;    /* Apply mask */
  851.             if (c == CR && tt_crd) { /* SET TERM CR-DISPLA CRLF? */
  852.             ckcputc(c);    /* Yes, output CR */
  853.             if (seslog) zchout(ZSFILE,c);
  854.             c = LF;        /* and insert a linefeed */
  855.             }
  856.             ckcputc(c);        /* Put it on the screen. */
  857.         }
  858.         if (seslog) zchout(ZSFILE,c); /* If logging, log it. */
  859.         }
  860.     }
  861.     } while (active);
  862.     cancio();
  863.     conres();
  864.     if (!quiet
  865. #ifdef CK_APC
  866.     && !apcactive
  867. #endif /* CK_APC */
  868.     )
  869.       printf("\r\n(Back at %s)\r\n",
  870.          *myhost ? myhost : "local VMS system");
  871.     what = W_NOTHING;
  872. #ifndef NOCSETS
  873.     language = langsv;            /* Restore language */
  874. #endif /* NOCSETS */
  875.     return(1);
  876. }
  877.  
  878. /*  H C O N N E  --  Give help message for connect.  */
  879.  
  880. VOID
  881. hconne() {
  882.     int c;
  883.     static char *hlpmsg[] = {
  884. "\n",
  885. "  C to return to C-Kermit prompt,   H to hangup and close the connection,\n",
  886. "  B to send a BREAK,                L to send a Long BREAK,\n",
  887. #ifdef TNCODE
  888. "  A to send TELNET Are You There,   I to send TELNET Interrupt,\n",
  889. #endif /* TNCODE */
  890. "  0 (zero) to send a null,          X to send an XON,\n",
  891. #ifdef NOPUSH
  892. "  S for status of connection,       ? for this message, or:\n",
  893. #else
  894. "  @ to enter DCL,                   S for status of connection,\n",
  895. "  ? for this message, or:\n",
  896. #endif /* NOPUSH */
  897. "  \\ to begin a backslash escape:\n",
  898. "    \\nnn  (decimal character code)\n",
  899. "    \\Onnn (octal character code)\n",
  900. "    \\Xhh  (hexadecimal character code)\n",
  901. "    Terminate with carriage return.\n\n",
  902. " Type the escape character again to send the escape character, or\n",
  903. " press the space-bar to resume the CONNECT command.\n\n",
  904. "" };
  905. /*
  906.   Need to save term characteristics/ allow disable binary mode
  907.   print message, get text and then restore previous state.
  908. */
  909.     conol("\r\nPress C to return to ");
  910.     conol(*myhost ? myhost : "the C-Kermit prompt");
  911.     conoll(", or:");
  912.     conola(hlpmsg);            /* Print the help message. */
  913.     conol("Command>");            /* Prompt for command. */
  914.     c = coninc(0) & 0x7f;
  915.     conoc(c);                /* Echo it. */
  916.     if (c != CMDQ)
  917.       conoll("");
  918.     doesc(c);
  919. }
  920.  
  921. /*  D O E S C  --  Process an escape character argument  */
  922.  
  923. VOID
  924. #ifdef CK_ANSIC
  925. doesc(register unsigned char c)
  926. #else
  927. doesc(c) register unsigned char c;
  928. #endif /* CK_ANSIC */
  929. /* doesc() */ {
  930.     int d;
  931.     char sbuf[35];
  932.  
  933.     c &= 0177;                /* Mask off 8th bit */
  934.  
  935.     if (c == escape) {            /* If it's the escape character, */
  936.         d = dopar(c);            /* just send it. */
  937.         ttoc(d);
  938.     return;
  939.     }
  940.     if (isupper(c)) c = tolower(c);    /* Convert to lowercase letter. */
  941.     if (iscntrl(c)) c += 'a' - '\001';
  942.  
  943.     switch (c) {            /* Take requested action. */
  944.       case 'b':
  945.     ttsndb();            /* Send a BREAK signal */
  946.     break;
  947. #ifdef TNCODE
  948.       case 'i':                /* Send TELNET interrupt */
  949. #ifndef IP
  950. #define IP 244
  951. #endif /* IP */
  952.     if (network && ttnproto == NP_TELNET) { /* TELNET */
  953.         CHAR temp[3];
  954.         temp[0] = IAC;        /* I Am a Command */
  955.         temp[1] = IP;        /* Interrupt Process */
  956.         temp[2] = NUL;
  957.         ttol((CHAR *)temp,2);
  958.     } else conoc(BEL);
  959.     break;
  960.       case 'a':                /* "Are You There?" */
  961.       case '\01':
  962. #ifndef AYT
  963. #define AYT 246
  964. #endif /* AYT */
  965.     if (network && ttnproto == NP_TELNET) {
  966.         CHAR temp[3];
  967.         temp[0] = IAC;        /* I Am a Command */
  968.         temp[1] = AYT;        /* Are You There? */
  969.         temp[2] = NUL;
  970.         ttol((CHAR *)temp,2);
  971.     } else conoc(BEL);
  972. #endif /* TNCODE */
  973.     break;
  974.       case 'c':                /* Return to prompt */
  975.     active = 0;
  976.     conol("\r\n");
  977.     break;
  978.       case 'h':                /* Hang up the connection */
  979. #ifndef NODIAL
  980.     if (mdmhup() < 1)        /* Try via modem first, otherwise */
  981. #endif /* NODIAL */
  982.       tthang();            /* the old-fashioned way. */
  983.     conol("\r\nHanging up ");
  984.     break;
  985.       case 'l':                /* Send a Long BREAK signal */
  986.     ttsndlb();
  987.     break;
  988.       case 's':                /* Status */
  989.     sprintf(sbuf,"Connected thru %s",ttname);
  990.     conol(sbuf);
  991.     sprintf(sbuf,", speed: %d",speed);
  992.     conol(sbuf);
  993.     if (seslog) {
  994.         sprintf(sbuf,", logging file: %s",sesfil);
  995.         conol(sbuf);
  996.     }
  997.     conoll("");
  998.     break;
  999. #ifndef NOPUSH
  1000.       case '!':
  1001.       case '@':
  1002.     conres();            /* Put console back to normal */
  1003.     zshcmd("");            /* Start DCL. */
  1004.     if (conbin(escape) < 0) {
  1005.         printf("Error returning to remote session\n");
  1006.         active = 0;
  1007.     }
  1008.     return;
  1009. #endif /* NOPUSH */
  1010.  
  1011.       case 'x':                /* XON */
  1012. /*
  1013.   NOTE: Here we should also issue QIO's to clear XOFF deadlock.
  1014. */
  1015.     ttoc(dopar(XON));
  1016.     break;
  1017.       case '?':                /* Give Help */
  1018.     hconne();
  1019.     break;
  1020.       case '0':                /* Send a NULL */
  1021.     c = '\0';
  1022.     d = dopar(c);
  1023.     ttoc(d);
  1024.     break;
  1025.       case SP:                /* Ignore space */
  1026.     break;
  1027.       default:
  1028.     if (c == CMDQ) {        /* Backslash escape */
  1029.         int x;
  1030.         kbp = kbuf;
  1031.         *kbp++ = c;
  1032.         while (((c = (coninc(0) & cmdmsk)) != '\r') && (c != '\n'))
  1033.           *kbp++ = c;
  1034.         *kbp = NUL; kbp = kbuf;
  1035.         x = xxesc(&kbp);
  1036.         if (x >= 0) {
  1037.         c = dopar(x);
  1038.         ttoc(c);
  1039.         return;
  1040.         } else {
  1041.         conoc(BEL);
  1042.         return;
  1043.         }
  1044.     }
  1045.     conoc(BEL); return;        /* Invalid esc arg, beep */
  1046.     }
  1047. }
  1048.