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

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