home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc190.zip / cklcon.c < prev    next >
C/C++ Source or Header  |  1994-09-18  |  34KB  |  1,081 lines

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