⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ckutio.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 5 页
字号:
inttthang() {    int x = 0;				/* Sometimes used as return code. */#ifndef POSIX    int z;				/* worker */#endif /* POSIX */#ifdef SVORPOSIX			/* AT&T, POSIX, HPUX declarations. */    int spdsav;				/* for saving speed */    int spdsavi;#ifdef HPUX    mflag dtr_down = 00000000000,    modem_rtn;    mflag modem_sav;    char modem_state[64];#endif /* HPUX */    int flags;				/* fcntl flags */    unsigned short ttc_save;#endif /* SVORPOSIX */    if (ttyfd < 0) return(0);           /* Don't do this if not open  */    if (xlocal < 1) return(0);		/* Don't do this if not local */#ifdef NETCONN    if (netconn)			/* Network connection. */      return((netclos() < 0) ? -1 : 1);	/* Just close it. */#endif /* NETCONN *//* From here down, we handle real tty devices. */#ifdef BSD44ORPOSIX/* Should add some error checking here... */    debug(F100,"tthang POSIX style","",0);    spdsav = cfgetospeed(&ttcur);	/* Get current speed */    spdsavi = cfgetispeed(&ttcur);	/* Get current speed */    cfsetospeed(&ttcur,B0);		/* Replace by 0 */    cfsetispeed(&ttcur,B0);    if (tcsetattr(ttyfd,TCSADRAIN,&ttcur) == -1)      debug(F100,"tthang tcsetattr fails","",errno);    msleep(HUPTIME);			/* Sleep */    cfsetospeed(&ttcur,spdsav);		/* Restore previous speed */    cfsetispeed(&ttcur,spdsavi);    tcsetattr(ttyfd,TCSADRAIN,&ttcur);    return(1);#else /* BSD44ORPOSIX */#ifdef aegis				/* Apollo Aegis */    sio_$control((short)ttyfd, sio_$dtr, false, st);    /* DTR down */    msleep(HUPTIME);					/* pause */    sio_$control((short)ttyfd, sio_$dtr, true,  st);    /* DTR up */    return(1);#endif /* aegis */#ifdef ANYBSD				/* Any BSD version. */    debug(F100,"tthang BSD style","",0);    if (ioctl(ttyfd,TIOCCDTR,0) < 0) {	/* Clear DTR. */	debug(F101,"tthang TIOCCDTR fails","",errno);	return(-1);    }    msleep(HUPTIME);			/* For about 1/2 sec */    errno = 0;    x = ioctl(ttyfd,TIOCSDTR,0);	/* Restore DTR */    if (x < 0) {	/*	  For some reason, this tends to fail with "no such device or address"	  but the operation still works, probably because of the close/open	  later on.  So let's not scare the user unnecessarily here.	*/	debug(F101,"tthang TIOCSDTR errno","",errno); /* Log the error */	x = 1;				/* Pretend we succeeded */    } else if (x == 0) x = 1;		/* Success */#ifdef COMMENT#ifdef FT21    ioctl(ttyfd, TIOCSAVEMODES, 0);    ioctl(ttyfd, TIOCHPCL, 0);    close(ttyfd);			/* Yes, must do this twice */    if ((ttyfd = open(ttnmsv,2)) < 0)	/* on Fortune computers... */      return(-1);			/* (but why?) */    else x = 1;#endif /* FT21 */#endif /* COMMENT */    close(do_open(ttnmsv));		/* Clear i/o error condition */    errno = 0;#ifdef COMMENT/* This is definitely dangerous.  Why was it here? */    z = ttvt(ttspeed,ttflow);		/* Restore modes. */    debug(F101,"tthang ttvt returns","",z);    return(z < 0 ? -1 : 1);#else    return(x);#endif /* COMMENT */#endif /* ANYBSD */#ifdef ATTSV				/* AT&T UNIX section, includes HP-UX and generic AT&T System III/V... */#ifdef HPUX /* Hewlett Packard allows explicit manipulation of modem signals. */#ifdef COMMENT/* Old way... */    debug(F100,"tthang HP-UX style","",0);    if (ioctl(ttyfd,MCSETAF,&dtr_down) < 0)        /* lower DTR */      return(-1);		    	           /* oops, can't. */    msleep(HUPTIME);			           /* Pause half a second. */    x = 1;				           /* Set return code */    if (ioctl(ttyfd,MCGETA,&modem_rtn) > -1) {     /* Get line status. */	if ((modem_rtn & MDCD) != 0)      	   /* Check if CD is low. */	  x = -1;                                  /* CD didn't drop, fail. */    } else x = -1;    /* Even if above calls fail, RTS & DTR should be turned back on. */    modem_rtn = MRTS | MDTR;    if (ioctl(ttyfd,MCSETAF,&modem_rtn) < 0) x = -1;    return(x);#else/* New way, from Hellmuth Michaelis */    debug(F100,"tthang HP-UX style, HPUXDEBUG","",0);    if (ioctl(ttyfd,MCGETA,&modem_rtn) == -1) { /* Get current status. */	debug(F100,"tthang HP-UX: can't get modem lines, NO HANGUP!","",0);	return(-1);    }    sprintf(modem_state,"%#lx",modem_rtn);    debug(F110,"tthang HP-UX: modem lines got = ",modem_state,0);        modem_sav = modem_rtn;		/* save line status */    modem_rtn &= ~MDTR;			/* DTR bit down */    sprintf(modem_state,"%#lx",modem_rtn);    debug(F110,"tthang HP-UX: modem lines lowered DTR = ",modem_state,0);     if (ioctl(ttyfd,MCSETAF,&modem_rtn) < 0) { /* lower DTR */	debug(F100,"tthang HP-UX: can't lower DTR!","",0);	return(-1);			/* oops, can't. */    }    msleep(HUPTIME);			/* Pause half a second. */    x = 1;				/* Set return code */    if (ioctl(ttyfd,MCGETA,&modem_rtn) > -1) { /* Get line status. */	sprintf(modem_state,"%#lx",modem_rtn);	debug(F110,"tthang HP-UX: modem lines got = ",modem_state,0);    	if ((modem_rtn & MDCD) != 0) {	/* Check if CD is low. */	    debug(F100,"tthang HP-UX: DCD didn't get down!","",0);	    x = -1;			/* CD didn't drop, fail. */	} else {	    debug(F100,"tthang HP-UX: DCD down!","",0);	}    } else {	x = -1;	debug(F100,"tthang HP-UX: can't get DCD status !","",0);    }    /* Even if above calls fail, RTS & DTR should be turned back on. */    modem_sav |= (MRTS | MDTR);    if (ioctl(ttyfd,MCSETAF,&modem_sav) < 0) {	x = -1;	debug(F100,"tthang HP-UX: can't set saved state |=(RTS | DTR)","",0);    } else {	sprintf(modem_state,"%#lx",modem_sav);	debug(F110,"tthang HP-UX: final modem lines = ",modem_state,0);        }    	    return(x);#endif /* COMMENT */#else /* AT&T but not HP-UX *//* SVID for AT&T System V R3 defines ioctl's for handling modem signals. *//* It is not known how many, if any, systems actually implement them, *//* so we include them here in ifdef's. */#ifndef _IBMR2/*  No modem-signal twiddling for IBM RT PC or RS/6000.  In AIX 3.1 and earlier, the ioctl() call is broken.  This code could be activated for AIX 3.1 with PTF 2006 or later  (e.g. AIX 3.2), but close/open does the job too, so why bother.*/#ifdef TIOCMBIS				/* Bit Set */#ifdef TIOCMBIC				/* Bit Clear */#ifdef TIOCM_DTR			/* DTR *//* Clear DTR, sleep 300 msec, turn it back on. *//* If any of the ioctl's return failure, go on to the next section. */    z = TIOCM_DTR;			/* Code for DTR. */#ifdef TIOCM_RTS			/* Lower RTS too if symbol is known. */    z |= TIOCM_RTS;			#endif /* TIOCM_RTS */    debug(F101,"tthang TIOCM signal mask","",z);    if (ioctl(ttyfd,TIOCMBIC,&z) > -1) {   /* Try to lower DTR. */	debug(F100,"tthang TIOCMBIC ok","",0);	msleep(HUPTIME);		   /* Pause half a second. */	if (ioctl(ttyfd,TIOCMBIS,&z) > -1) { /* Try to turn it back on. */	    debug(F100,"tthang TIOCMBIS ok","",0);#ifndef CLSOPN	    return(1);			/* Success, done. */#endif /* CLSOPN */	} else {			/* Couldn't raise, continue. */	    debug(F101,"tthang TIOCMBIS errno","",errno);	}    } else {				/* Couldn't lower, continue. */ 	debug(F101,"tthang TIOCMBIC errno","",errno);    }#endif /* TIOCM_DTR */#endif /* TIOCMBIC */#endif /* TIOCMBIS */#endif /* _IBMR2 *//*  General AT&T UNIX case, not HPUX.  The following code is highly suspect.  No  two AT&T-based systems seem to do this the same way.  The object is simply  to turn off DTR and then turn it back on.  SVID says the universal method  for turning off DTR is to set the speed to zero, and this does seem to do  the trick in all cases.  But neither SVID nor any known man pages say how to  turn DTR back on again.  Some variants, like most Xenix implementations,  raise DTR again when the speed is restored to a nonzero value.  Others  require the device to be closed and opened again, but this is risky because  getty could seize the device during the instant it is closed.*//* Return code for ioctl failures... */#ifdef ATT6300    x = 1;				/* ATT6300 doesn't want to fail... */#else    x = -1;#endif /* ATT6300 */    debug(F100,"tthang get settings","",0);    if (ioctl(ttyfd,TCGETA,&ttcur) < 0) /* Get current settings. */      return(x);			/* Fail if this doesn't work. */    if ((flags = fcntl(ttyfd,F_GETFL,0)) < 0) /* Get device flags. */      return(x);    ttc_save = ttcur.c_cflag;		/* Remember current speed. */    spdsav = ttc_save & CBAUD;    debug(F101,"tthang speed","",spdsav);#ifdef O_NDELAY    debug(F100,"tthang turning O_NDELAY on","",0);    fcntl(ttyfd, F_SETFL, flags | O_NDELAY); /* Activate O_NDELAY */#endif /* O_NDELAY */#ifdef ATT7300 /* This is the way it is SUPPOSED to work */    ttcur.c_cflag &= ~CBAUD;		/* Change the speed to zero.  */#else#ifdef RTAIX    ttcur.c_cflag &= ~CBAUD;		/* Change the speed to zero.  */#else          /* This way really works but may be dangerous */#ifdef u3b2    ttcur.c_cflag = ~(CBAUD|CLOCAL);	/* Special for AT&T 3B2s */					/* (CLOCAL must be OFF) */#else#ifdef SCO3R2				/* SCO UNIX 3.2 *//*  This is complete nonsense, but an SCO user claimed this change made  hanging up work.  Comments from other SCO UNIX 3.2 users would be   appreciated.*/    ttcur.c_cflag = CBAUD|B0;#else					/* None of the above. */    ttcur.c_cflag = CLOCAL|HUPCL;	/* Change all but these to zero */					/* (CLOCAL must be ON) */#endif /* SCO3R2 */#endif /* u3b2 */#endif /* RTAIX */#endif /* ATT7300 */#ifdef COMMENT    /* and if none of those work, try one of these... */    ttcur.c_cflag = 0;    ttcur.c_cflag = CLOCAL;    ttcur.c_cflag &= ~(CBAUD|HUPCL);    ttcur.c_cflag &= ~(CBAUD|CREAD);    ttcur.c_cflag &= ~(CBAUD|CREAD|HUPCL);    /* or other combinations */#endif /* COMMENT */#ifdef TCXONC    debug(F100,"tthang TCXONC","",0);    ioctl(ttyfd, TCXONC, 1);#endif /* TCXONC */#ifdef TIOCSTART    debug(F100,"tthang TIOCSTART","",0);    ioctl(ttyfd, TIOCSTART, 0);#endif /* TIOCSTART */    if (ioctl(ttyfd,TCSETAF,&ttcur) < 0) { /* Fail if we can't. */	fcntl(ttyfd, F_SETFL, flags);	/* Restore flags */	return(-1);			/* before returning. */    }    msleep(300);			/* Give modem time to notice. *//* Now, even though it doesn't say this in SVID or any man page, we have *//* to close and reopen the device.  This is not necessary for all systems, *//* but it's impossible to predict which ones need it and which ones don't. */#ifdef ATT7300/*  Special handling for ATT 7300 UNIX PC and 3B1, which have "phone"  related ioctl's for their internal modems.  attmodem has getty status and   modem-in-use bit.  Reportedly the ATT7300/3B1 PIOCDISC call is necessary,   but also ruins the file descriptor, and no other phone(7) ioctl call can fix   it.  Whateverit does, it seems to escape detection with PIOCGETA and TCGETA.  The only way to undo the damage is to close the fd and then reopen it.*/    if (attmodem & ISMODEM) {	debug(F100,"tthang attmodem close/open","",0);	ioctl(ttyfd,PIOCUNHOLD,&dialer); /* Return call to handset. */	ioctl(ttyfd,PIOCDISC,&dialer);	/* Disconnect phone. */	close(ttyfd);			/* Close and reopen the fd. */	ttyfd = priv_opn(ttnmsv, O_RDWR | O_NDELAY);	attmodem &= ~ISMODEM;		/* Phone no longer in use. */    }#else /* !ATT7300 *//* It seems we have to close and open the device for other AT&T systems *//* too, and this is the place to do it.  The following code does the *//* famous close(open(...)) magic by default.  If that doesn't work for you, *//* then try uncommenting the following statement or putting -DCLSOPN in *//* the makefile CFLAGS. *//* #define CLSOPN */#ifndef SCO32 /* Not needed by, and harmful to, SCO UNIX 3.2 / Xenix 2.3 */#ifdef O_NDELAY#define OPENFLGS O_RDWR | O_NDELAY#else#define OPENFLGS O_RDWR#endif#ifndef CLSOPN/* This method is used by default, i.e. unless CLSOPN is defined. *//* It is thought to be safer because there is no window where getty *//* can seize control of the device.  The drawback is that it might not work. */    debug(F101,"tthang close(open()), OPENFLGS","",OPENFLGS);    close(priv_opn(ttnmsv, OPENFLGS));#else/* This method is used if you #define CLSOPN.  It is more likely to work *//* than the previous method, but it's also more dangerous. */    debug(F101,"tthang close/open, OPENFLGS","",OPENFLGS);    close(ttyfd);    msleep(10);    ttyfd = priv_opn(ttnmsv, OPENFLGS);	/* Open it again */#endif /* CLSOPN */#undef OPENFLGS#endif /* SCO32 */#endif /* ATT7300 *//* Now put all flags & modes back the way we found them. *//* (Does the order of ioctl & fcntl matter ? ) */    debug(F100,"tthang restore settings","",0);    ttcur.c_cflag = ttc_save;		/* Get old speed back. */    if (ioctl(ttyfd,TCSETAF,&ttcur) < 0) /* ioctl parameters. */      return(-1); #ifdef O_NDELAY/*  This is required for IBM RT and RS/6000, probably helps elsewhere too (?).  After closing a modem line, the modem will probably not be asserting  carrier any more, so we should not require carrier any more.  If this  causes trouble on non-IBM UNIXes, change the #ifdef to use _IBMR2 rather  than O_NDELAY.*/    flags &= ~O_NDELAY;			/* Don't require carrier on reopen */#endif /* O_NDELAY */    if (fcntl(ttyfd,F_SETFL,flags) < 0)	/* fcntl parameters */      return(-1);    return(1);#endif /* not HPUX */#endif /* ATTSV */#endif /* BSD44ORPOSIX */}/*  Major change in 5A(174).  We used to use LPASS8, if it was defined, to  allow 8-bit data and Xon/Xoff flow control at the same time.  But this  LPASS8 business seems to have been causing trouble for everybody but me!  For example, Annex terminal servers, commonly used with Encore computers,  do not support LPASS8 even though the Encore itself does.  Ditto for many  other terminal servers, TELNET connections, rlogin connections, etc etc.  Now, reportedly, even vanilla 4.3 BSD systems can't do this right on their  serial lines, even though LPASS8 is a feature of 4.3BSD.  So let's turn it  off for everybody.  That means we goes back to using raw mode, with no  flow control.  Phooey.  NOTE: This must be done before the first reference to LPASS8 in this file,  and after the last #include statment.*/#ifdef LPASS8#undef LPASS8#endif /* LPASS8 *//*  T T R E S  --  Restore terminal to "normal" mode.  *//* ske@pkmab.se: There are two choices for what this function should do. * (1) Restore the tty to current "normal" mode, with carrier treatment * according to ttcarr, to be used after every kermit command. (2) Restore * the tty to the state it was in before kermit opened it. These choices * conflict, since ttold can't hold both choices of tty parameters.  ttres() * is currently being called as in choice (1), but ttold basically holds * the initial parameters, as in (2), and the description at the beginning * of this file says (2). * * I don't think restoring tty parameters after all kermit commands makes * much of a difference.  Restoring them upon exit from kermit may be of * some use in some cases (when the line is not restored automatically on * close, by the operating system). * * I can't choose which one it should be, so I haven't changed it. It * probably works as it is, too. It would probably even work even with * ttres() entirely deleted... * * (from

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -