?? ckutio.c
字號:
char *ckxv = "Unix tty I/O, 4C(038), 19 Mar 86";/* C K U T I O *//* C-Kermit interrupt, terminal control & i/o functions for Unix systems *//* Author: Frank da Cruz (SY.FDC@CU20B), Columbia University Center for Computing Activities, January 1985. Copyright (C) 1985, Trustees of Columbia University in the City of New York. Permission is granted to any individual or institution to use, copy, or redistribute this software so long as it is not sold for profit, provided this copyright notice is retained. *//* Includes for all Unixes (conditional includes come later) */#include <sys/types.h> /* Types */#include <sys/dir.h> /* Directory */#include <ctype.h> /* Character types */#ifdef NULL#undef NULL#endif NULL#include <stdio.h> /* Unix Standard i/o */#include <signal.h> /* Interrupts */#include <setjmp.h> /* Longjumps */#include "ckcdeb.h" /* Typedefs, formats for debug() *//* Maximum length for the name of a tty device */#ifndef DEVNAMLEN#define DEVNAMLEN 25#endif/* 4.1 BSD support added by Charles E. Brooks, EDN-VAX *//* Fortune 16:32 For:Pro 1.7 support mostly like 4.1, added by J-P Dumas */#ifdef BSD4#define ANYBSD#ifdef MAXNAMLEN#define BSD42char *ckxsys = " 4.2 BSD";#else#ifdef FT17#define BSD41char *ckxsys = " For:Pro Fortune 1.7";#else#define BSD41#ifndef C70char *ckxsys = " 4.1 BSD";#endif#endif#endif#endif/* 2.9bsd support contributed by Bradley Smith, UCLA */#ifdef BSD29#define ANYBSDchar *ckxsys = " 2.9 BSD";#endif/* Version 7 UNIX support contributed by Gregg Wonderly, Oklahoma State University: gregg@okstate.csnet*/#ifdef V7char *ckxsys = " Version 7 UNIX (tm)";#endif/* BBN C70 support from Frank Wancho, WANCHO@SIMTEL20 */#ifdef C70char *ckxsys = " BBN C/70";#endif/* Amdahl UTS 2.4 (v7 derivative) for IBM 370 series compatible mainframes *//* Contributed by Garard Gaye, Jean-Pierre Dumas, DUMAS@SUMEX-AIM. */#ifdef UTS24char *ckxsys = " Amdahl UTS 2.4";#endif/* Pro/Venix Version 1.x support from Columbia U. */#ifdef PROVX1char *ckxsys = " Pro-3xx Venix v1";#endif/* Tower support contributed by John Bray, Auburn, Alabama */#ifdef TOWER1char *ckxsys = " NCR Tower 1632, OS 1.02";#endif/* Sys III/V, Xenix, PC/IX support by Herm Fischer, Encino, CA */#ifdef UXIII#ifdef XENIXchar *ckxsys = " Xenix/286";#else#ifdef PCIXchar *ckxsys = " PC/IX";#else#ifdef ISIIIchar *ckxsys = " Interactive Systems Corp System III";#elsechar *ckxsys = " AT&T System III/System V";#endif#endif#endif#endif/* Features... *//* Do own buffering, using unbuffered read() calls... */#ifdef UXIII#define MYREAD#endif#ifdef BSD42#define MYREAD#include <errno.h>#endif/* Note - KERLD is the Berkeley Unix Berknet line driver, modified to pass through all 8 bits, and to allow an arbitrary break character to be set. Don't define this symbol unless you have made this modification to your 4.2BSD kernel!*/#ifdef BSD4/* #define KERLD */ /* <-- note, commented out */#endif/* Variables available to outside world: dftty -- Pointer to default tty name string, like "/dev/tty". dfloc -- 0 if dftty is console, 1 if external line. dfprty -- Default parity dfflow -- Default flow control ckxech -- Flag for who echoes console typein: 1 - The program (system echo is turned off) 0 - The system (or front end, or terminal). functions that want to do their own echoing should check this flag before doing so. flfnam -- Name of lock file, including its path, e.g., "/usr/spool/uucp/LCK..cul0" or "/etc/locks/tty77" hasLock -- Flag set if this kermit established a uucp lock. inbufc -- number of tty line rawmode unread characters (system III/V unixes) backgrd -- Flag indicating program executing in background ( & on end of shell command). Used to ignore INT and QUIT signals. Functions for assigned communication line (either external or console tty): sysinit() -- System dependent program initialization ttopen(ttname,local,mdmtyp) -- Open the named tty for exclusive access. ttclos() -- Close & reset the tty, releasing any access lock. ttpkt(speed,flow) -- Put the tty in packet mode and set the speed. ttvt(speed,flow) -- Put the tty in virtual terminal mode. or in DIALING or CONNECTED modem control state. ttinl(dest,max,timo) -- Timed read line from the tty. ttinc(timo) -- Timed read character from tty. myread() -- System 3 raw mode bulk buffer read, gives -- subsequent chars one at a time and simulates -- FIONREAD! myunrd(c) -- Places c back in buffer to be read (one only) ttchk() -- See how many characters in tty input buffer. ttxin(n,buf) -- Read n characters from tty (untimed). ttol(string,length) -- Write a string to the tty. ttoc(c) -- Write a character to the tty. ttflui() -- Flush tty input buffer. ttlock(ttname) -- Lock against uucp collisions (Sys III) ttunlck() -- Unlock " " " look4lk(ttname) -- Check if a lock file exists*//*Functions for console terminal: congm() -- Get console terminal modes. concb(esc) -- Put the console in single-character wakeup mode with no echo. conbin(esc) -- Put the console in binary (raw) mode. conres() -- Restore the console to mode obtained by congm(). conoc(c) -- Unbuffered output, one character to console. conol(s) -- Unbuffered output, null-terminated string to the console. conola(s) -- Unbuffered output, array of strings to the console. conxo(n,s) -- Unbuffered output, n characters to the console. conchk() -- Check if characters available at console (bsd 4.2). Check if escape char (^\) typed at console (System III/V). coninc(timo) -- Timed get a character from the console. conint() -- Enable terminal interrupts on the console if not background. connoi() -- Disable terminal interrupts on the console if not background.Time functions msleep(m) -- Millisecond sleep ztime(&s) -- Return pointer to date/time string rtimer() -- Reset timer gtimer() -- Get elapsed time since last call to rtimer()*//* Conditional Includes */#ifdef FT17#include <sys/file.h> /* File information */#endif#ifndef PROVX1#include <sys/file.h> /* File information */#endif/* System III, System V */#ifdef UXIII#include <termio.h>#include <sys/ioctl.h>#include <fcntl.h> /* directory reading for locking */#include <errno.h> /* error numbers for system returns */#endif/* Not Sys III/V */#ifndef UXIII#include <sgtty.h> /* Set/Get tty modes */#ifndef PROVX1#ifndef V7#ifndef BSD41#include <sys/time.h> /* Clock info (for break generation) */#endif#endif#endif#endif#ifdef BSD41#include <sys/timeb.h> /* BSD 4.1 ... ceb */#endif#ifdef BSD29#include <sys/timeb.h> /* BSD 2.9 (Vic Abell, Purdue) */#endif#ifdef TOWER1#include <sys/timeb.h> /* Clock info for NCR Tower */#endif/* Declarations */long time(); /* All Unixes should have this... */extern int errno; /* System call error return *//* Special stuff for V7 input buffer peeking */#ifdef V7int kmem[2] = { -1, -1};char *initrawq(), *qaddr[2]={0,0};#define CON 0#define TTY 1#endif/* dftty is the device name of the default device for file transfer *//* dfloc is 0 if dftty is the user's console terminal, 1 if an external line */#ifdef PROVX1 char *dftty = "/dev/com1.dout"; /* Only example so far of a system */ int dfloc = 1; /* that goes in local mode by default */#else char *dftty = CTTNAM; /* Remote by default, use normal */ int dfloc = 0; /* controlling terminal name. */#endif int dfprty = 0; /* Parity (0 = none) */ int dfflow = 1; /* Xon/Xoff flow control */ int backgrd = 0; /* Assume in foreground (no '&' ) */int ckxech = 0; /* 0 if system normally echoes console characters, else 1 *//* Declarations of variables global within this module */static long tcount; /* Elapsed time counter */static char *brnuls = "\0\0\0\0\0\0\0"; /* A string of nulls */static jmp_buf sjbuf, jjbuf; /* Longjump buffer */static int lkf = 0, /* Line lock flag */ conif = 0, /* Console interrupts on/off flag */ cgmf = 0, /* Flag that console modes saved */ xlocal = 0, /* Flag for tty local or remote */ ttyfd = -1; /* TTY file descriptor */static char escchr; /* Escape or attn character *//* Special line discipline, 4.2bsd only, and only with kernel mods... */#ifdef KERLD static int kerld = 1; /* Special Kermit line discipline... */ struct tchars oldc, newc; /* Special characters */ int ld = NETLDISC; /* Really a hack to "Berknet" l.d. */ int oldld; /* Old discipline */#else static int kerld = 0; /* Not selected, no special l.d. */#endif#ifdef BSD42 static struct timeval tv; /* For getting time, from sys/time.h */ static struct timezone tz;#endif#ifdef BSD29 static long clock; /* For getting time from sys/time.h */ static struct timeb ftp; /* And from sys/timeb.h */#endif#ifdef BSD41 static long clock; /* For getting time from sys/time.h */ static struct timeb ftp; /* And from sys/timeb.h */#endif#ifdef TOWER1static long clock; /* For getting time from sys/time.h */static struct timeb ftp; /* And from sys/timeb.h */#endif#ifdef V7static long clock;#endif#ifdef UXIII static struct termio /* sgtty info... */ ttold, ttraw, tttvt, /* for communication line */ ccold, ccraw, cccbrk; /* and for console */#else static struct sgttyb /* sgtty info... */ ttold, ttraw, tttvt, ttbuf, /* for communication line */ ccold, ccraw, cccbrk; /* and for console */#endifstatic char flfnam[80]; /* uucp lock file path name */static int hasLock = 0; /* =1 if this kermit locked uucp */static int inbufc = 0; /* stuff for efficient SIII raw line */static int ungotn = -1; /* pushback to unread character */static int conesc = 0; /* set to 1 if esc char (^\) typed */static int ttlock(); /* definition of ttlock subprocedure */static int ttunlck(); /* and unlock subprocedure */static char ttnmsv[DEVNAMLEN]; /* copy of open path for tthang *//* S Y S I N I T -- System-dependent program initialization. */sysinit() {/* for now, nothing... */}/* T T O P E N -- Open a tty for exclusive access. *//* Returns 0 on success, -1 on failure. *//* If called with lcl < 0, sets value of lcl as follows: 0: the terminal named by ttname is the job's controlling terminal. 1: the terminal named by ttname is not the job's controlling terminal. But watch out: if a line is already open, or if requested line can't be opened, then lcl remains (and is returned as) -1.*/ttopen(ttname,lcl,modem) char *ttname; int *lcl, modem; {#ifdef UXIII char *ctermid(); /* Wish they all had this! */#endif char *x; extern char* ttyname(); char cname[DEVNAMLEN+4]; if (ttyfd > -1) return(0); /* If already open, ignore this call */ xlocal = *lcl; /* Make this available to other fns */#ifdef UXIII /* if modem connection, don't wait for carrier */ ttyfd = open(ttname,O_RDWR | (modem ? O_NDELAY : 0) );#else ttyfd = open(ttname,2); /* Try to open for read/write */#endif if (ttyfd < 0) { /* If couldn't open, fail. */ perror(ttname); return(-1); } strncpy(ttnmsv,ttname,DEVNAMLEN); /* Open, keep copy of name locally. *//* Caller wants us to figure out if line is controlling tty */ debug(F111,"ttopen ok",ttname,*lcl); if (*lcl == -1) { if (strcmp(ttname,CTTNAM) == 0) { /* "/dev/tty" always remote */ debug(F110," Same as CTTNAM",ttname,0); xlocal = 0; } else if (isatty(0)) { /* Else, if stdin not redirected */ x = ttyname(0); /* then compare its device name */ strncpy(cname,x,DEVNAMLEN); /* (copy from internal static buf) */ debug(F110," ttyname(0)",x,0); x = ttyname(ttyfd); /* ...with real name of ttname. */ xlocal = (strncmp(x,cname,DEVNAMLEN) == 0) ? 0 : 1; debug(F111," ttyname",x,xlocal); } else { /* Else, if stdin redirected... */#ifdef UXIII/* Sys III/V provides nice ctermid() function to get name of controlling tty */ ctermid(cname); /* Get name of controlling terminal */ debug(F110," ctermid",cname,0); x = ttyname(ttyfd); /* Compare with name of comm line. */ xlocal = (strncmp(x,cname,DEVNAMLEN) == 0) ? 0 : 1; debug(F111," ttyname",x,xlocal);#else/* Just assume local, so "set speed" and similar commands will work *//* If not really local, how could it work anyway?... */ xlocal = 1; debug(F101," redirected stdin","",xlocal);#endif } } /* Now check if line is locked -- if so fail, else lock for ourselves */ lkf = 0; /* Check lock */ if (xlocal > 0) { if (ttlock(ttname) < 0) { fprintf(stderr,"Exclusive access to %s denied\n",ttname); close(ttyfd); ttyfd = -1; debug(F110," Access denied by lock",ttname,0); return(-1); /* Not if already locked */ } else lkf = 1; }
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -