📄 dttty.c
字号:
#if !defined(_QNX_SOURCE) && !defined(sun) && !defined(__MSDOS__) && !defined(__WIN32__) && !defined(HP_UX) /* * Save the line discipline. */ if ((status = ioctl (fd, TIOCGETD, &saved_ldisc)) < 0) { report_error ("TIOCGETD", FALSE); return (status); }#endif /* !defined(_QNX_SOURCE) */ /* * Save the terminal characteristics. */#if defined(sun) if ((status = ioctl (fd, TCGETS, &saved_tmodes)) < 0) { report_error ("TCGETS", FALSE); }#elif defined(ultrix) if ((status = ioctl (fd, TCGETP, &saved_tmodes)) < 0) { report_error ("TCGETP", FALSE); }#elif defined(_OSF_SOURCE) if ((status = ioctl (fd, TIOCGETA, &saved_tmodes)) < 0) { report_error ("TIOCGETA", FALSE); }#elif defined(_QNX_SOURCE) || defined(__MSDOS__) || defined(__WIN32__) if ((status = tcgetattr (fd, &saved_tmodes)) < 0) { report_error ("tcgetattr()", FALSE); }#endif /* defined(sun) */ if (status == SUCCESS) tty_saved = TRUE; /* * Since this function is called prior to starting the test, * we'll flush the input & output queues to discard any junk. */ if (flush_flag) { /* Allow user to control. */ if (tcflush (fd, TCIOFLUSH) < 0) { report_error ("tcflush(TCIOFLUSH)", FALSE); } } return (status);}/************************************************************************ * * * restore_tty() - Restore Saved Terminal Characteristics. * * * * Inputs: fd = The terminal file descriptor. * * * * Return Value: * * Returns SUCCESS / FAILURE / WARNING (never saved) * * * ************************************************************************/intrestore_tty (int fd){ int status = SUCCESS; if (tty_saved == FALSE) return (WARNING); if (debug_flag) { Printf ("Restoring saved terminal characteristics, fd = %d...\n", fd); } /* * Restore the saved terminal characteristics. */#if defined(sun) if ((status = ioctl (fd, TCSETSW, &saved_tmodes)) < 0) { report_error ("TCSETSW", TRUE); }#elif defined(ultrix) if ((status = ioctl (fd, TCSANOW, &saved_tmodes)) < 0) { report_error ("TCSANOW", TRUE); }#elif defined(_OSF_SOURCE) if ((status = ioctl (fd, TIOCSETA, &saved_tmodes)) < 0) { report_error ("TIOCSETA", TRUE); }#elif defined(_QNX_SOURCE) || defined(__MSDOS__) || defined(__WIN32__) if ((status = tcsetattr (fd, TCSANOW, &saved_tmodes)) < 0) { report_error ("tcsetattr()", TRUE); }#endif /* defined(sun) */#if !defined(_QNX_SOURCE) && !defined(sun) && !defined(__MSDOS__) && !defined(__WIN32__) && !defined(HP_UX) /* * Restore the saved line discipline. */ if (ioctl (fd, TIOCSETD, &saved_ldisc) < 0) { report_error ("TIOCSETD", TRUE); }#endif /* !defined(_QNX_SOURCE) */ return (status);}/************************************************************************ * * * setup_tty() - Setup terminal characteristics. * * * * Inputs: fd = The terminal file descriptor. * * * * Return Value: * * Returns SUCCESS / FAILURE. * * * ************************************************************************/intsetup_tty (int fd, int flushing){ int status = SUCCESS; struct termios tm; /* For terminal characteristics */#if !defined(_QNX_SOURCE) && !defined(sun) && !defined(__MSDOS__) && !defined(__WIN32__) int ldisc; /* For line discipline. */#endif if (debug_flag && !flushing) { Printf ("Setting up test terminal characteristics, fd = %d...\n", fd); }#if defined(sun) /* * If software carrier was detected, then enable it. */ switch (softcar_opt) { static int on = 1, off = 0; case ON: if (ioctl (fd, TIOCSSOFTCAR, &on) < 0) { report_error ("TIOCSOFTCAR", FALSE); } break; case OFF: if (ioctl (fd, TIOCSSOFTCAR, &off) < 0) { report_error ("TIOCSOFTCAR", FALSE); } break; }#endif /* defined(sun) */#if !defined(_QNX_SOURCE) && !defined(sun) && !defined(__MSDOS__) && !defined(__WIN32__) && !defined(SCO) && !defined(HP_UX) && !defined(AIX) /* * Ensure the correct line discipline is setup. */ if ((status = ioctl (fd, TIOCGETD, &ldisc)) < 0) { report_error ("TIOCGETD", FALSE); return (status); }#if defined(ultrix) if (ldisc != TERMIODISC) { ldisc = TERMIODISC; if ((status = ioctl (fd, TIOCSETD, &ldisc)) < 0) { report_error ("TIOCSETD", FALSE); return (status); } }#elif defined(__linux__) if (ldisc != N_TTY) { ldisc = N_TTY; if ((status = ioctl (fd, TIOCSETD, &ldisc)) < 0) { report_error ("TIOCSETD", FALSE); return (status); } }#else /* !defined(ultrix) */ if (ldisc != TTYDISC) { ldisc = TTYDISC; if ((status = ioctl (fd, TIOCSETD, &ldisc)) < 0) { report_error ("TIOCSETD", FALSE); return (status); } }#endif /* defined(ultrix) */#endif /* !defined(_QNX_SOURCE) */ /* * For terminals, get and set the terminal characteristics. */#if defined(sun) if ((status = ioctl (fd, TCGETS, &tm)) < 0) { report_error ("TCGETS", FALSE); return (status); }#elif defined(ultrix) if ((status = ioctl (fd, TCGETP, &tm)) < 0) { report_error ("TCGETP", FALSE); return (status); }#elif defined(_OSF_SOURCE) if ((status = ioctl (fd, TIOCGETA, &tm)) < 0) { report_error ("TIOCGETA", FALSE); return (status); }#elif defined(_QNX_SOURCE) || defined(__MSDOS__) || defined(__WIN32__) if ((status = tcgetattr (fd, &tm) < 0)) { report_error ("tcgetattr()", FALSE); return (status); }#endif /* defined(sun) */ tm.c_cflag = tm.c_iflag = tm.c_oflag = tm.c_lflag = 0; if (modem_flag) { tm.c_cflag = HUPCL; /* Hangup on last close. */ } else { tm.c_cflag = CLOCAL; /* Ignore modem signals. */ }#if defined(sun) || defined(__linux__) || defined(SCO) || defined(HP_UX) || defined(AIX) tm.c_cflag |= (baud_rate_code | data_bits_code | parity_code | CREAD);#else /* !defined(sun) */ tm.c_cflag |= (data_bits_code | parity_code | CREAD);#if defined(ultrix) tm.c_cflag |= ( (baud_rate_code << 16) | baud_rate_code);#else /* !defined(ultrix) */ tm.c_ispeed = tm.c_ospeed = baud_rate_code;#endif /* defined(ultrix) */#endif /* defined(sun) */ /* * Send two stop bits for slower speeds (is this right?). */ if (baud_rate_code < B300) { tm.c_cflag |= CSTOPB; } /* * Set VMIN & VTIME values, checking for maximum values. */ if (flushing) { tm.c_cc[VMIN] = 0; } else if (tty_minflag) { tm.c_cc[VMIN] = (tty_minimum > VMIN_MAX) ? VMIN_MAX : tty_minimum; } else { tm.c_cc[VMIN] = (block_size > VMIN_MAX) ? VMIN_MAX : block_size; } if (flushing) { tm.c_cc[VTIME] = 1; } else { tm.c_cc[VTIME] = (tty_timeout > VTIME_MAX) ? VTIME_MAX : tty_timeout; } /* * Set the desired flow control. */#if defined(sun) || defined(_OSF_SOURCE) || defined(__linux__) if (flow_type == CTS_RTS) { tm.c_cflag |= CRTSCTS; /* CTS/RTS flow control. */ } else if (flow_type == XON_XOFF) { tm.c_iflag |= (IXON | IXOFF); /* XON/XOFF Flow control. */ }#elif defined(_QNX_SOURCE) tm.c_lflag |= IEXTEN; /* QNX POSIX extensions. */ if (flow_type == CTS_RTS) { tm.c_lflag |= (IHFLOW | OHFLOW); /* CTS/RTS flow control. */ } else if (flow_type == XON_XOFF) { tm.c_iflag |= (IXON | IXOFF); /* XON/XOFF Flow control. */ }#else /* Ultrix, POSIX, and all others (I hope)... */ if (flow_type == XON_XOFF) { tm.c_iflag |= (IXON | IXOFF); /* XON/XOFF Flow control. */ }#endif /* defined(sun) || defined(_OSF_SOURCE) */ /* * If 7 bit data, or parity is being enabled, enable stripping * of the eight data bit, otherwise the driver passes it back. * We don't test generation of correct parity, driver does this. */ if ( (data_bits_code == CS7) || (parity_code & PARENB) ) { tm.c_iflag |= ISTRIP; /* Strip the 8th data bit */ /* * For parity, enable input parity checking. */ if (parity_code & PARENB) { tm.c_iflag |= INPCK; /* Check input parity. */ } } /* * Setup the terminal characteristics after output is done. */#if defined(sun) if ((status = ioctl (fd, TCSETSW, &tm)) < 0) { report_error ("TCSETSW", FALSE); return (status); }#elif defined(ultrix) if ((status = ioctl (fd, TCSADRAIN, &tm)) < 0) { report_error ("TCSADRAIN", FALSE); return (status); }#elif defined(_OSF_SOURCE) if ((status = ioctl (fd, TIOCSETAW, &tm)) < 0) { report_error ("TIOCSETAW", FALSE); return (status); }#elif defined(_QNX_SOURCE) || defined(__MSDOS__) || defined(__WIN32__) if ((status = tcsetattr (fd, TCSADRAIN, &tm)) < 0) { report_error ("tcsetattr()", FALSE); return (status); }#endif /* defined(sun) */ /* * For testing modem lines, wait for modem to be ready. */ if (modem_flag) {#if defined(_OSF_SOURCE) || defined(ultrix) if (debug_flag) (void) ShowModemSignals (fd);#if 0 status = WaitForCarrier (fd); if (debug_flag) (void) ShowModemSignals (fd); status = (status == TRUE) ? SUCCESS : FAILURE; if (status == SUCCESS) { status = SetBlockingMode (fd);#endif#endif /* defined(_OSF_SOURCE) || defined(ultrix) */ /* * Don't wait for modem signals, simply reset non-blocking * mode, and let the first read or write system call block. * [ NOTE: I've changed this logic for direct lines, since * I'm seeing the writer causing CTS/DTS/Carrier to set. * Plus, this code should work on all operating systems. ] */ status = SetBlockingMode (fd); } return (status);}/************************************************************************ * * * setup_baud_rate() - Setup the baud rate code. * * * * Description: * * This function takes a specified baud rate (i.e. 9600) and maps * * it to the baud rate code we must specify to the Unix terminal driver * * to set that baud rate. * * * * Inputs: baud = Pointer to baud rate string. * * * * Return Value: * * Returns SUCCESS / FAILURE = Valid Speed/Invalid Speed. * * * ************************************************************************/intsetup_baud_rate (u_int32 baud){ int i; struct tty_baud_rate *tsp; for (tsp = baud_rate_tbl, i = 0; i < num_baud_rates; i++, tsp++) { if (baud == tsp->usr_speed) { baud_rate_code = tsp->tty_speed; /* Save baud rate code. */ return (SUCCESS); /* Return success status. */ } } fprintf (efp, "Baud rate '%d' is invalid, valid entrys are:\n", baud); for (tsp = baud_rate_tbl, i = 0; i < num_baud_rates; i++, tsp++) { if ( (i % 6) == 0) fprintf (efp, "\n"); fprintf (efp, "%10d", tsp->usr_speed); } fprintf (efp, "\n"); return (FAILURE);}intSetBlockingMode (int fd){ int flags; if ( (flags = fcntl (fd, F_GETFL)) == FAILURE) { perror ("fcntl(F_GETFL)"); return (FAILURE); } /* * BEWARE: O_NDELAY & O_NONBLOCK are _not_ the same value anymore. * [ NOTE: O_NONBLOCK is defined by POSIX (O_NDELAY is _not_. ] */ flags &= ~(O_NONBLOCK); if ( (fcntl (fd, F_SETFL, flags)) == FAILURE) { perror ("fcntl(F_SETFL)"); return (FAILURE); } return (SUCCESS);}#if defined(_OSF_SOURCE) || defined(ultrix)/* * To the best of my knowledge, this is Digital Unix (OSF/Ultrix) specific: */unsigned intGetModemSignals (int fd){ unsigned int msigs; if (ioctl(fd, TIOCMGET, &msigs) < 0) { perror("TIOCMGET"); return(FAILURE); } return (msigs);}intSetModemSignals (int fd, int msigs){ if (ioctl (fd, TIOCMBIS, &msigs) < 0) { perror("TIOCMBIS"); return(FAILURE); } return (SUCCESS);}intHangupModem (int fd){ int status; unsigned int delay = 3; if ((status = ioctl(fd, TIOCCDTR, 0)) < 0) perror("TIOCCDTR"); sleep(delay); if ((status = ioctl(fd, TIOCSDTR, 0)) < 0) perror("TIOCSDTR"); sleep(1); return (status);}#define P(fmtstr) fprintf (efp, fmtstr)#define P1(fmtstr,arg) fprintf (efp, fmtstr, arg)intShowModemSignals (int fd){ unsigned int msigs; if ( (msigs = GetModemSignals(fd)) == FAILURE) { return(FAILURE); } P ("--------------------------------------------------\r\n"); P1("Modem Signals Set: 0x%x\r\n", msigs); if (msigs & TIOCM_LE) { P1(" 0x%x = TIOCM_LE = Line Enable.\r\n", TIOCM_LE); } if (msigs & TIOCM_DTR) { P1(" 0x%x = TIOCM_DTR = Data Terminal Ready.\r\n", TIOCM_DTR); } if (msigs & TIOCM_RTS) { P1(" 0x%x = TIOCM_RTS = Request To Send.\r\n", TIOCM_RTS); } if (msigs & TIOCM_ST) { P1(" 0x%x = TIOCM_ST = Secondary Transmit.\r\n", TIOCM_ST); } if (msigs & TIOCM_SR) { P1(" 0x%x = TIOCM_SR = Secondary Receive.\r\n", TIOCM_SR); } if (msigs & TIOCM_CTS) { P1(" 0x%x = TIOCM_CTS = Clear To Send.\r\n", TIOCM_CTS); } if (msigs & TIOCM_CAR) { P1(" 0x%x = TIOCM_CAR = Carrier Detect.\r\n", TIOCM_CAR); } if (msigs & TIOCM_RNG) { P1(" 0x%x = TIOCM_RNG = Ring Indicator.\r\n", TIOCM_RNG); } if (msigs & TIOCM_DSR) { P1(" 0x%x = TIOCM_DSR = Data Set Ready.\r\n", TIOCM_DSR); } P ("--------------------------------------------------\r\n"); return(SUCCESS);}intWaitForCarrier (int fd){ unsigned int msigs; unsigned int delay = 1; if (debug_flag) { Printf ("Waiting for carrier or DSR signals...\n"); } do { if ( (msigs = GetModemSignals(fd)) == FAILURE) { return(FALSE); } sleep (delay); } while ( (msigs & (TIOCM_CAR|TIOCM_DSR)) == 0); return (TRUE);}#endif /* defined(_OSF_SOURCE) || defined(ultrix) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -