ckucns.c
资源名称:cku197.tar.Z [点击查看]
上传用户:dufan58
上传日期:2007-01-05
资源大小:3407k
文件大小:66k
源码类别:
通讯/手机编程
开发平台:
Windows_Unix
- #include "ckcsym.h"
- #ifdef NOLOCAL
- char *connv = "";
- #else
- char *connv = "CONNECT Command for UNIX:select(), 7.0.110, 30 Dec 1999";
- /* C K U C N S -- Terminal connection to remote system, for UNIX */
- /*
- Author: Frank da Cruz <fdc@columbia.edu>,
- Columbia University Academic Information Systems, New York City.
- Copyright (C) 1985, 2000,
- Trustees of Columbia University in the City of New York.
- All rights reserved. See the C-Kermit COPYING.TXT file or the
- copyright text in the ckcmai.c module for disclaimer and permissions.
- */
- /*
- This version of the UNIX CONNECT module uses select(), which is required for
- Kerberos encryption. Thus it can be used only on UNIX systems that support
- select() on both TCP/IP and serial connections. A separate module that uses
- a completely portable fork() structure can be used on systems where select()
- is not available or does not work as required.
- */
- #include "ckcdeb.h" /* Common things first */
- #ifdef OSF13
- #ifdef CK_ANSIC
- #ifdef _NO_PROTO
- #undef _NO_PROTO
- #endif /* _NO_PROTO */
- #endif /* CK_ANSIC */
- #endif /* OSF13 */
- #include <errno.h> /* Error numbers */
- #ifdef __linux__
- #include <sys/time.h> /* For FD_blah */
- #endif /* __linux__ */
- /* Kermit-specific includes */
- #include "ckcasc.h" /* ASCII characters */
- #include "ckcker.h" /* Kermit things */
- #include "ckucmd.h" /* For xxesc() prototype */
- #include "ckcnet.h" /* Network symbols */
- #ifndef NOCSETS
- #include "ckcxla.h" /* Character set translation */
- #endif /* NOCSETS */
- #ifdef BEBOX
- #include <kernel/OS.h>
- #include <socket.h>
- #include <stdio.h>
- #endif /* BEBOX */
- #include <signal.h>
- #ifndef SCO_OSR504
- #ifdef SELECT_H
- #include <sys/select.h>
- #endif /* SELECT_H */
- #endif /* SCO_OSR504 */
- /* Internal function prototypes */
- _PROTOTYP( VOID ttflux, (void) );
- _PROTOTYP( VOID doesc, (char) );
- _PROTOTYP( int hconne, (void) );
- #ifndef NOSHOW
- _PROTOTYP( VOID shomdm, (void) );
- #endif /* NOSHOW */
- _PROTOTYP( static int kbget, (void) );
- _PROTOTYP( static int ckcputf, (void) );
- /* External variables */
- extern struct ck_p ptab[];
- extern int local, escape, duplex, parity, flow, seslog, sessft, debses,
- mdmtyp, ttnproto, cmask, cmdmsk, network, nettype, sosi, tnlm,
- xitsta, what, ttyfd, ttpipe, quiet, backgrd, pflag, tt_crd, tn_nlm, ttfdflg,
- tt_escape, justone, carrier, ttpty;
- extern long speed;
- extern char ttname[], sesfil[], myhost[], *ccntab[];
- #ifdef TNCODE
- extern int tn_b_nlm, tn_rem_echo;
- #endif /* TNCODE */
- #ifdef CK_TRIGGER
- extern char * tt_trigger[], * triggerval;
- #endif /* CK_TRIGGER */
- extern int nopush;
- #ifdef CK_APC
- extern int apcactive; /* Application Program Command (APC) */
- extern int apcstatus; /* items ... */
- static int apclength = 0;
- #ifdef DCMDBUF
- extern char *apcbuf;
- #else
- extern char apcbuf[];
- #endif /* DCMDBUF */
- static int apcbuflen = APCBUFLEN - 2;
- extern int protocol;
- #endif /* CK_APC */
- #ifndef NOXFER
- extern int autodl; /* Auto download */
- #endif /* NOXFER */
- #ifdef CK_AUTODL
- extern CHAR ksbuf[];
- #endif /* CK_AUTODL */
- #ifdef CK_ENCRYPTION
- extern int me_auth;
- #endif /* CK_ENCRYPTION */
- #ifdef CK_XYZ
- #ifdef XYZ_INTERNAL
- static int zmdlok = 1; /* Zmodem autodownloads available */
- #else
- static int zmdlok = 0; /* Depends on external protocol def */
- #endif /* XYZ_INTERNAL */
- #else
- static int zmdlok = 0; /* Not available at all */
- #endif /* CK_XYZ */
- #ifndef NOSETKEY /* Keyboard mapping */
- extern KEY *keymap; /* Single-character key map */
- extern MACRO *macrotab; /* Key macro pointer table */
- static MACRO kmptr = NULL; /* Pointer to current key macro */
- #endif /* NOSETKEY */
- /* Global variables local to this module */
- static int
- active = 0,
- quitnow = 0, /* <esc-char>Q was typed */
- dohangup = 0, /* <esc-char>H was typed */
- inshift = 0, /* SO/SI shift states */
- outshift = 0;
- static char ecbuf[10], *ecbp; /* Escape char buffer & pointer */
- #ifdef CK_SMALL
- #define IBUFL 1536 /* Input buffer length */
- #else
- #define IBUFL 4096
- #endif /* CK_SMALL */
- static int obc = 0; /* Output buffer count */
- #ifndef OXOS
- #define OBUFL 1024 /* Output buffer length */
- #else
- #define OBUFL IBUFL
- #endif /* OXOS */
- #ifdef BIGBUFOK
- #define TMPLEN 4096 /* Temporary message buffer length */
- #else
- #define TMPLEN 200
- #endif /* BIGBUFOK */
- #ifdef DYNAMIC
- static char *ibuf = NULL, *obuf = NULL, *temp = NULL; /* Buffers */
- #else
- static char ibuf[IBUFL], obuf[OBUFL], temp[TMPLEN];
- #endif /* DYNAMIC */
- #ifdef DYNAMIC
- static char *ibp; /* Input buffer pointer */
- #else
- static char *ibp = ibuf; /* Input buffer pointer */
- #endif /*DYNAMIC */
- static int ibc = 0; /* Input buffer count */
- #ifdef DYNAMIC
- static char *obp; /* Output buffer pointer */
- #else
- static char *obp = obuf; /* Output buffer pointer */
- #endif /* DYNAMIC */
- /* Character-set items */
- static int unicode = 0;
- #ifndef NOCSETS
- #ifdef CK_ANSIC /* ANSI C prototypes... */
- extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Character set */
- extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* translation functions */
- static CHAR (*sxo)(CHAR); /* Local translation functions */
- static CHAR (*rxo)(CHAR); /* for output (sending) terminal chars */
- static CHAR (*sxi)(CHAR); /* and for input (receiving) terminal chars. */
- static CHAR (*rxi)(CHAR);
- #else /* Not ANSI C... */
- extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(); /* Character set */
- extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(); /* translation functions. */
- static CHAR (*sxo)(); /* Local translation functions */
- static CHAR (*rxo)(); /* for output (sending) terminal chars */
- static CHAR (*sxi)(); /* and for input (receiving) terminal chars. */
- static CHAR (*rxi)();
- #endif /* CK_ANSIC */
- extern int language; /* Current language. */
- static int langsv; /* For remembering language setting. */
- extern struct csinfo fcsinfo[]; /* File character set info. */
- extern int tcsr, tcsl; /* Terminal character sets, remote & local. */
- static int tcs; /* Intermediate ("transfer") character set. */
- static int tcssize = 0; /* Size of tcs */
- #ifdef UNICODE /* UTF-8 support */
- #ifdef CK_ANSIC
- extern int (*xl_ufc[MAXFCSETS+1])(USHORT); /* Unicode to FCS */
- extern USHORT (*xl_fcu[MAXFCSETS+1])(CHAR); /* FCS to Unicode */
- extern int (*xuf)(USHORT); /* Translation function UCS to FCS */
- extern USHORT (*xfu)(CHAR); /* Translation function FCS to UCS */
- #else
- extern int (*xl_ufc[MAXFCSETS+1])();
- extern USHORT (*xl_fcu[MAXFCSETS+1])();
- extern int (*xuf)();
- extern USHORT (*xfu)();
- #endif /* CK_ANSIC */
- #endif /* UNICODE */
- #endif /* NOCSETS */
- static int printing = 0;
- /*
- We do not need to parse and recognize escape sequences if we are being built
- without character-set support AND without APC support.
- */
- #ifdef NOESCSEQ
- #ifdef XPRINT
- #undef XPRINT
- #endif /* XPRINT */
- #else /* NOESCSEQ not defined from outside */
- #ifdef NOCSETS /* No character sets */
- #ifndef CK_APC /* No APC */
- #ifndef XPRINT /* No transparent printing */
- #define NOESCSEQ /* So no escape sequence recognizer */
- #endif /* XPRINT */
- #endif /* CK_APC */
- #endif /* NOCSETS */
- #endif /* NOESCSEQ */
- static int escseq = 0; /* 1 = Recognizer is active */
- static int inesc[2] = { 0, 0 }; /* State of sequence recognizer */
- static int oldesc[2] = { -1, -1 }; /* Previous state of recognizer */
- #ifdef NOESCSEQ
- #define chkaes(x,y) 0
- #else
- /*
- As of edit 178, the CONNECT command skips past ANSI escape sequences to
- avoid translating the characters within them. This allows the CONNECT
- command to work correctly with a host that uses a 7-bit ISO 646 national
- character set, in which characters like '[' would normally be translated
- into accented characters, ruining the terminal's interpretation (and
- generation) of escape sequences.
- As of edit 190, the CONNECT command responds to APC escape sequences
- (ESC _ text ESC ) if the user SETs TERMINAL APC ON or UNCHECKED, and the
- program was built with CK_APC defined.
- Non-ANSI/ISO-compliant escape sequences are not handled.
- */
- /* States for the escape-sequence recognizer. */
- #define ES_NORMAL 0 /* Normal, not in an escape sequence */
- #define ES_GOTESC 1 /* Current character is ESC */
- #define ES_ESCSEQ 2 /* Inside an escape sequence */
- #define ES_GOTCSI 3 /* Inside a control sequence */
- #define ES_STRING 4 /* Inside DCS,OSC,PM, or APC string */
- #define ES_TERMIN 5 /* 1st char of string terminator */
- /*
- ANSI escape sequence handling. Only the 7-bit form is treated, because
- translation is not a problem in the 8-bit environment, in which all GL
- characters are ASCII and no translation takes place. So we don't check
- for the 8-bit single-character versions of CSI, DCS, OSC, APC, or ST.
- Here is the ANSI sequence recognizer state table, followed by the code
- that implements it.
- Definitions:
- CAN = Cancel 01/08 Ctrl-X
- SUB = Substitute 01/10 Ctrl-Z
- DCS = Device Control Sequence 01/11 05/00 ESC P
- CSI = Control Sequence Introducer 01/11 05/11 ESC [
- ST = String Terminator 01/11 05/12 ESC
- OSC = Operating System Command 01/11 05/13 ESC ]
- PM = Privacy Message 01/11 05/14 ESC ^
- APC = Application Program Command 01/11 05/15 ESC _
- ANSI escape sequence recognizer:
- State Input New State ; Commentary
- NORMAL (start) ; Start in NORMAL state
- (any) CAN NORMAL ; ^X cancels
- (any) SUB NORMAL ; ^Z cancels
- NORMAL ESC GOTESC ; Begin escape sequence
- NORMAL other ; NORMAL control or graphic character
- GOTESC ESC ; Start again
- GOTESC [ GOTCSI ; CSI
- GOTESC P STRING ; DCS introducer, consume through ST
- GOTESC ] STRING ; OSC introducer, consume through ST
- GOTESC ^ STRING ; PM introducer, consume through ST
- GOTESC _ STRING ; APC introducer, consume through ST
- GOTESC 0..~ NORMAL ; 03/00 through 17/14 = Final character
- GOTESC other ESCSEQ ; Intermediate or ignored control character
- ESCSEQ ESC GOTESC ; Start again
- ESCSEQ 0..~ NORMAL ; 03/00 through 17/14 = Final character
- ESCSEQ other ; Intermediate or ignored control character
- GOTCSI ESC GOTESC ; Start again
- GOTCSI @..~ NORMAL ; 04/00 through 17/14 = Final character
- GOTCSI other ; Intermediate char or ignored control char
- STRING ESC TERMIN ; Maybe have ST
- STRING other ; Consume all else
- TERMIN NORMAL ; End of string
- TERMIN other STRING ; Still in string
- */
- #ifdef XPRINT /* Transparent print support */
- /*
- We can't just print each byte as it comes in because then the printer-off
- sequence would be sent to the printer. Thus we have to buffer up escape
- sequences and print them only when they are complete AND we know they are
- not the printer-off sequence. All printing is done via zsoutx(ZMFILE,s,n).
- This allows for strings that contain NULs. Don't mix calls to zsoutx() with
- calls to zchout(), or the output will be scrambled. Also note that when
- printing a saved-up escape sequence, we never print its final character
- because that will be printed in the mainline code, upon return from
- chkaes(). Note that the printer-on sequence is passed to the screen; this
- is unavoidable, since we don't know what it is until after we get to the
- end, and for screen display purposes we can't buffer up escape sequences
- for numerous reasons. Therefore we also must output the printer-off
- sequence, otherwise a real terminal or emulator will be stuck in print mode.
- */
- extern int tt_print;
- #define ESCBUFLEN 63
- static char escbuf[ESCBUFLEN+1] = { NUL, NUL };
- static int escbufc = 0;
- VOID
- printon() { /* Turn printing on */
- int x, pp;
- char * p;
- extern int printpipe, noprinter;
- extern char * printername;
- if (noprinter) {
- debug(F110,"PRINTER ON NOPRINTER","",0);
- return;
- }
- p = printername;
- pp = printpipe;
- if (!p) p = "";
- if (!*p) {
- #ifdef ANYBSD
- p = "lpr";
- #else
- p = "lp";
- #endif /* ANYBSD */
- pp = 1;
- debug(F110,"PRINTER DEFAULT",p,0);
- }
- debug(F111,"PRINTER ON",p,pp);
- if (pp) { /* Printing to pipe */
- x = zxcmd(ZMFILE,p);
- } else { /* Append to file */
- struct filinfo xx;
- xx.bs = 0; xx.cs = 0; xx.rl = 0; xx.org = 0; xx.cc = 0;
- xx.typ = 0; xx.dsp = XYFZ_A; xx.os_specific = NUL;
- xx.lblopts = 0;
- x = zopeno(ZMFILE,p,NULL,&xx);
- }
- debug(F101,"PRINTER OPEN","",x);
- printing = 1;
- }
- VOID
- printoff() { /* Turn printing off */
- int x;
- extern int noprinter;
- printing = 0;
- if (noprinter) {
- debug(F110,"PRINTER OFF NOPRINTER","",0);
- return;
- }
- debug(F100,"PRINTER OFF","",0);
- if (printing) {
- x = zclose(ZMFILE);
- debug(F101,"PRINTER CLOSE","",x);
- }
- }
- #endif /* XPRINT */
- /*
- C H K A E S -- Check ANSI Escape Sequence.
- Call with EACH character in input stream.
- src = 0 means c is incoming from remote; 1 = char from keyboard.
- Sets global inesc[src] variable according to escape sequence state.
- Returns 0 normally, 1 if an APC sequence is to be executed.
- Handles transparent printing internally.
- */
- int
- #ifdef CK_ANSIC
- chkaes(char c, int src)
- #else
- chkaes(c,src) char c; int src;
- #endif /* CK_ANSIC */
- /* chkaes */ {
- debug(F111,"chkaes entry inesc",ckitoa(src),inesc[src]);
- debug(F101,"chkaes c","",c);
- if (src < 0 || src > 1) /* Don't allow bad args. */
- return(0);
- oldesc[src] = inesc[src]; /* Remember previous state */
- #ifdef XPRINT
- if (inesc[src] && !src) { /* Save up escape seq for printing */
- if (!c) return(0); /* Ignore NULs */
- if (escbufc < ESCBUFLEN) {
- escbuf[escbufc++] = c;
- escbuf[escbufc] = NUL;
- debug(F111,"ESCBUF 1",escbuf,escbufc);
- } else { /* Buffer overrun */
- if (printing && escbufc) /* Print what's there so far */
- zsoutx(ZMFILE,escbuf,escbufc);
- escbufc = 1; /* clear it out */
- escbuf[0] = c; /* and start off fresh buffer */
- escbuf[1] = NUL; /* with this character. */
- }
- }
- #endif /* XPRINT */
- if (c == CAN || c == SUB) { /* CAN and SUB cancel any sequence */
- #ifdef XPRINT
- if (!src) {
- if (printing && escbufc > 1)
- zsoutx(ZMFILE,escbuf,escbufc-1);
- escbufc = 0; /* Clear buffer */
- escbuf[0] = NUL;
- }
- #endif /* XPRINT */
- inesc[src] = ES_NORMAL;
- } else /* Otherwise */
- switch (inesc[src]) { /* enter state switcher */
- case ES_NORMAL: /* NORMAL state */
- if (c == ESC) { /* Got an ESC */
- inesc[src] = ES_GOTESC; /* Change state to GOTESC */
- #ifdef XPRINT
- if (!src) {
- escbufc = 1; /* Clear escape sequence buffer */
- escbuf[0] = c; /* and deposit the ESC */
- escbuf[1] = NUL;
- debug(F111,"ESCBUF 2",escbuf,escbufc);
- }
- #endif /* XPRINT */
- }
- break; /* Otherwise stay in NORMAL state */
- case ES_GOTESC: /* GOTESC state - prev char was ESC*/
- if (c == '[') { /* Left bracket after ESC is CSI */
- inesc[src] = ES_GOTCSI; /* Change to GOTCSI state */
- } else if (c == 'P' || (c > 0134 && c < 0140)) { /* P, ], ^, or _ */
- inesc[src] = ES_STRING; /* Switch to STRING-absorption state */
- #ifdef XPRINT
- debug(F111,"ESCBUF STRING",escbuf,escbufc);
- #endif /* XPRINT */
- #ifdef CK_APC
- /* If APC not disabled */
- if (!src && c == '_' && apcstatus != APC_OFF) {
- debug(F100,"CONNECT APC begin","",0);
- apcactive = APC_REMOTE; /* Set APC-Active flag */
- apclength = 0; /* and reset APC buffer pointer */
- }
- #endif /* CK_APC */
- } else if (c > 057 && c < 0177) { /* Final character '0' thru '~' */
- inesc[src] = ES_NORMAL; /* Back to normal */
- #ifdef XPRINT
- if (!src) {
- if (printing & escbufc > 1) {
- /* Dump esc seq buf to printer */
- zsoutx(ZMFILE,escbuf,escbufc-1);
- debug(F111,"ESCBUF PRINT 1",escbuf,escbufc);
- }
- escbufc = 0; /* Clear parameter buffer */
- escbuf[0] = NUL;
- }
- #endif /* XPRINT */
- } else if (c != ESC) { /* ESC in an escape sequence... */
- inesc[src] = ES_ESCSEQ; /* starts a new escape sequence */
- }
- break; /* Intermediate or ignored ctrl char */
- case ES_ESCSEQ: /* ESCSEQ -- in an escape sequence */
- if (c > 057 && c < 0177) { /* Final character '0' thru '~' */
- inesc[src] = ES_NORMAL; /* Return to NORMAL state. */
- #ifdef XPRINT
- if (!src) {
- if (printing && escbufc > 1) {
- zsoutx(ZMFILE,escbuf,escbufc-1);
- debug(F111,"ESCBUF PRINT 2",escbuf,escbufc);
- }
- escbufc = 0; /* Clear escseq buffer */
- escbuf[0] = NUL;
- }
- #endif /* XPRINT */
- } else if (c == ESC) { /* ESC ... */
- inesc[src] = ES_GOTESC; /* starts a new escape sequence */
- }
- break; /* Intermediate or ignored ctrl char */
- case ES_GOTCSI: /* GOTCSI -- In a control sequence */
- if (c > 077 && c < 0177) { /* Final character '@' thru '~' */
- #ifdef XPRINT
- if (!src && tt_print) { /* Printer enabled? */
- if (c == 'i') { /* Final char is "i"? */
- char * p = (char *) (escbuf + escbufc - 4);
- if (!strncmp(p, "