终端I/O
本章接收POSIX.1所有终端函数,及某些平台特有的增加部分
概述
终端I/O有两种不同的工作模式。- 规范模式输入处理。对终端输入以行为单位处理。对每个读请求,终端驱动最多返回一行。- 非规范模式输入处理。输入字符不装配成行。默认为规范模式。POSIX.1定义了11个特殊输入字符。9个可以更改。可认为终端设备由通常位于内核的终端驱动控制。每个终端设备有一个输入队列和一个输出队列。- 如打开了回显,则在输入队列,输出队列间有一个隐含的连接- 输入队列长度MAX_INPUT是有限值。特定设备输入队列满,系统行为依赖实现。- MAX_CANON,限制一个规范输入行最大字节数- 输出队列长常也是有限的,但程序不能获知。输出队列将满时,内核使写进程休眠,有可用空间再唤醒。- 如何用tcflush冲洗输入或输出。说明tcsetattr时,会了解到如何通知系统只在输出队列为空时,才改变一个终端的属性。也可通知系统,让它在改变终端属性时丢弃输入队列的所有东西。多数UNIX系统在一个称为终端行规程的模块中进行全部规范处理。所有可检测和更改的终端设备特性都包含在termios结构中。该结构定义在<termios.h>struct termios{tcflag_t c_iflag;tcflag_t c_oflag;tcflag_t c_cflag;tcflag_t c_lflag;cc_t c_cc[NCCS];};输入标志通过终端设备驱动程序控制字符的输入输出标志则控制驱动程序输出控制标志影响RS-232串行线本地标志影响驱动程序和用户间接口c_cc数组包含了所有可更改的特殊字符。对终端设备,没有使用ioctl。因为对终端设备的ioctl,其最后一个参数的数据类型随执行动作不同而改变。故无法对参数做类型检测。对某个特定设备【假设其为终端,调制解调器,打印机,或其他设备】,决定要用哪些选项对我们也是一种挑战。
特殊输入字符
在POSIX.1的11个特殊字符中,9个字符的值可以任意更改。不能更改的两个特殊字符是换行符和回车符,也可能是STOP和START。为了更改,只需修改termios结构的c_cc数组的相应项。若将c_cc数组中的某项设置为_POSIX_VDISABLE,则禁用相应特殊字符。这些字符中的大多数在被终端驱动程序识别并进行特殊处理后会被丢弃,并不将它们返回给执行读终端操作的进程。返回给读进程的例外字符是换行符【NL,EOL,EOL2】和回车符【CR】
CR
The carriage return character. We cannot change this character. This character is recognized on input in canonical mode. When both ICANON (canonical mode) and ICRNL (map CR to NL) are set and IGNCR (ignore CR) is not set, the CR character is translated to NL and has the same effect as a NL character. This character is returned to the reading process (perhaps after being translated to a NL).
DISCARD
The discard character. This character, recognized on input in extended mode (IEXTEN), causes subsequent output to be discarded until another DISCARD character is entered or the discard condition is cleared (see the FLUSHO option). This character is discarded when processed (i.e., it is not passed to the process).
DSUSP
The delayed-suspend job-control character. This character is recognized on input in extended mode (IEXTEN) if job control is supported and if the ISIG flag is set. Like the SUSP character, this delayed-suspend character generates the SIGTSTP signal that is sent to all processes in the foreground process group (refer to Figure 9.7). However, the delayed-suspend character generates a signal only when a process reads from the controlling terminal, not when the character is typed. This character is discarded when processed (i.e., it is not passed to the process).
EOF
The end-of-file character. This character is recognized on input in canonical mode (ICANON). When we type this character, all bytes waiting to be read are immediately passed to the reading process. If no bytes are waiting to be read, a count of 0 is returned. Entering an EOF character at the beginning of the line is the normal way to indicate the end of file to a program. This character is discarded when processed in canonical mode (i.e., it is not passed to the process)
EOL
The additional line delimiter character, like NL. This character is recognized on input in canonical mode (ICANON) and is returned to the reading process; however, this character is not normally used.
EOL2
Another line delimiter character, like NL. This character is treated identically to the EOL character.
ERASE
The erase character (backspace). This character is recognized on input in canonical mode (ICANON) and erases the previous character in the line, not erasing beyond the beginning of the line. The erase character is discarded when processed in canonical mode (i.e., it is not passed to the process).
ERASE2
The alternate erase character (backspace). This character is treated exactlylike the erase character (ERASE)
INTR
The interrupt character. This character is recognized on input if the ISIG flag is set and generates the SIGINT signal that is sent to all processes in the foreground process group (refer to Figure 9.7). This character is discarded when processed (i.e., it is not passed to the process)
KILL
The kill character. (The name ‘‘kill’’ is overused; recall the kill function used to send a signal to a process. This character should be called the line-erase character; it has nothing to do with signals.) It is recognized on input in canonical mode (ICANON). It erases the entire line and is discarded when processed (i.e., it is not passed to the process).
LNEXT
The literal-next character. This character is recognized on input in extended mode (IEXTEN) and causes any special meaning of the next character to be ignored. This works for all special characters listed in this section. We can use this character to type any character to a program. The LNEXT character is discarded when processed, but the next character entered is passed to the process.
NL
The newline character, also called the line delimiter. We cannot change this character. It is recognized on input in canonical mode (ICANON). This character is returned to the reading process.
QUIT
The quit character. This character is recognized on input if the ISIG flag is set. The quit character generates the SIGQUIT signal, which is sent to all processes in the foreground process group (refer to Figure 9.7). This character is discarded when processed (i.e., it is not passed to the process).Recall from Figure 10.1 that the difference between INTR and QUIT is thatthe QUIT character not only terminates the process by default, but also generates a core file.
REPRINT
The reprint character. This character is recognized on input in extended,canonical mode (both IEXTEN and ICANON flags set) and causes all unread input to be output (reechoed). This character is discarded when processed (i.e., it is not passed to the process).
START
The start character. This character is recognized on input if the IXON flag is set and is automatically generated as output if the IXOFF flag is set. A received START character with IXON set causes stopped output (from a previously entered STOP character) to restart. In this case, the START character is discarded when processed (i.e., it is not passed to the process).When IXOFF is set, the terminal driver automatically generates a START character to resume input that it had previously stopped, when the new input will not overflow the input buffer.
STATUS
The BSD status-request character. This character is recognized on input in extended, canonical mode (both IEXTEN and ICANON flags set) and generates the SIGINFO signal, which is sent to all processes in the foreground process group (refer to Figure 9.7). Additionally, if the NOKERNINFO flag is not set, status information on the foreground process group is displayed on the terminal. This character is discarded when processed (i.e., it is not passed to the process)
STOP
The stop character. This character is recognized on input if the IXON flag is set and is automatically generated as output if the IXOFF flag is set. A received STOP character with IXON set stops the output. In this case, the STOP character is discarded when processed (i.e., it is not passed to the process). The stopped output is restarted when a START character is entered.When IXOFF is set, the terminal driver automatically generates a STOP character to prevent the input buffer from overflowing
SUSP
The suspend job-control character. This character is recognized on input if job control is supported and if the ISIG flag is set. The suspend character generates the SIGTSTP signal, which is sent to all processes in the foreground process group (refer to Figure 9.7). This character is discarded when processed (i.e., it is not passed to the process).
WERASE
The word-erase character. This character is recognized on input in extended, canonical mode (both IEXTEN and ICANON flags set) and causes the previous word to be erased. First, it skips backward over any white space (spaces or tabs), then skips backward over the previous token,leaving the cursor positioned where the first character of the previous token was located. Normally, the previous token ends when a white space character is encountered. We can change this behavior, however, by setting the ALTWERASE flag. This flag causes the previous token to end when the first nonalphanumeric character is encountered. The word-erase character is discarded when processed (i.e., it is not passed to the process).Another ‘‘character ’’ that we need to define for terminal devices is the BREAK character. BREAK is not really a character, but rather a condition that occurs during asynchronous serial data transmission. A BREAK condition is signaled to the device driver in various ways, depending on the serial interface.
获得和设置终端属性
#include <termios.h>// 成功,返回0// 出错,返回-1// 若fd没引用终端设备则返回-1int tcgetattr(int fd,struct termios *termptr);// 即使设置部分生效,也返回OKint tcsetattr(int fd,// TCSANOW更改立即发生// TCSADRAIN发送了所有输出后更改才发生// TCSAFLUSH发送了所有输出后更改才发生,更改发生时未读的输入数据被丢弃int opt,const struct termios *termptr);
终端选项标志
列出的所有选项标志【除所谓屏蔽字标志外】都用一位或多位表示。屏蔽字标志定义多个位,它们组合在一起,可定义一组值。屏蔽字标志有一个定义名,每个值也有一个名字。
ALTWERASE
(c_lflag, FreeBSD, Mac OS X) If set, an alternate word-erase algorithm is used when the WERASE character is entered. Instead of moving backward until the previous white space character, this flag causes the WERASE character to move backward until the first nonalphanumeric character is encountered.
BRKINT
(c_iflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris)If this flag is set and IGNBRK is not set, the input and output queues are flushedwhen a BREAK is received, and a SIGINT signal is generated. This signal is generated for the foreground process group if the terminal device is a controlling terminal.If neither IGNBRK nor BRKINT is set, then a BREAK is read as a single character \0, unless PARMRK is set; in that case the BREAK is read as the 3-byte sequence \377, \0, \0.
BSDLY
(c_oflag, XSI, Linux, Solaris) Backspace delay mask. The values for the mask are BS0 or BS1.
CBAUDEXT
(c_cflag, Solaris) Extended baud rates. Used to enable baud rates greater than B38400. (We discuss baud rates in Section 18.7.)
CCAR_OFLOW
(c_cflag, FreeBSD, Mac OS X) Enable hardware flow control of the output using the RS-232 modem carrier signal Data-Carrier-Detect (DCD). This is the same as the old MDMBUF flag.
CCTS_OFLOW
(c_cflag, FreeBSD, Mac OS X, Solaris) Enable hardware flow control of the output using the Clear-To-Send (CTS) RS-232 signal.
CDSR_OFLOW
(c_cflag, FreeBSD, Mac OS X) Flow control the output according to the Data-Set-Ready (DSR) RS-232 signal.
CDTR_IFLOW
(c_cflag, FreeBSD, Mac OS X) Flow control the input according to the Data-Terminal-Ready (DTR) RS-232 signal.
CIBAUDEXT
(c_cflag, Solaris) Extended input baud rates. Used to enable input baud rates greater than B38400. (We discuss baud rates in Section 18.7.)
CIGNORE
(c_cflag, FreeBSD, Mac OS X) Ignore control flags.
CLOCAL
(c_cflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set, the modem status lines are ignored. This usually means that the device is directly attached. When this flag is not set, an open of a terminal device usually blocks until the modem answers a call and establishes a connection, for example.
CMSPAR
(c_oflag, Linux) Select mark or space parity. If PARODD is set, the parity bit is always 1 (mark parity). Otherwise, the parity bit is always 0(space parity).
CRDLY
(c_oflag, XSI, Linux, Solaris) Carriage return delay mask. Possible values for the mask are CR0, CR1, CR2, and CR3
CREAD
(c_cflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set, the receiver is enabled and characters can be received.
CRTSCTS
(c_cflag, FreeBSD, Linux, Mac OS X, Solaris) Behavior depends on platform. For Solaris, enables outbound hardware flow control if set.On the other three platforms, enables both inbound and outbound hardware flow control (equivalent to CCTS_OFLOW|CRTS_IFLOW).
CRTS_IFLOW
(c_cflag, FreeBSD, Mac OS X, Solaris) Request-To-Send (RTS) flow control of input.
CRTSXOFF
(c_cflag, Solaris) If set, inbound hardware flow control is enabled.The state of the Request-To-Send RS-232 signal controls the flow control.
CSIZE
(c_cflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) This field is a mask that specifies the number of bits per byte for both transmission and reception. This size does not include the parity bit, if any. The values for the field defined by this mask are CS5, CS6, CS7, and CS8, for 5, 6, 7, and 8 bits per byte, respectively.
CSTOPB
(c_cflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set, two stop bits are used; otherwise, one stop bit is used.
ECHO
(c_lflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set, input characters are echoed back to the terminal device. Input characters can be echoed in either canonical or noncanonical mode.
ECHOCTL
(c_lflag, FreeBSD, Linux, Mac OS X, Solaris) If set and if ECHO is set,ASCII control characters (those characters in the range 0 through octal 37, inclusive) other than the ASCII TAB, the ASCII NL, and the STARTand STOP characters are echoed as ˆX, where X is the character formed by adding octal 100 to the control character. For example, the ASCIIControl-A character (octal 1) is echoed as ˆA and the ASCII DELETE character (octal 177) is echoed as ˆ?. If this flag is not set, the ASCII control characters are echoed as themselves. As with the ECHO flag, this flag affects the echoing of control characters in both canonical and noncanonical modes.Be aware that some systems echo the EOF character differently, since its typical value is Control-D. (Control-D is the ASCII EOT character, which can cause some terminals to hang up.) Check your manual.
ECHOE
(c_lflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set and if ICANON is set, the ERASE character erases the last character in the current line from the display. This is usually done in the terminal driver by writing the three-character sequence backspace, space, backspace.If the WERASE character is supported, ECHOE causes the previous word to be erased using one or more of the same three-character sequence.If the ECHOPRT flag is supported, the actions described here for ECHOEassume that the ECHOPRT flag is not set.
ECHOK
(c_lflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set and if ICANON is set, the KILL character erases the current line from the display or outputs the NL character (to emphasize that the entire line was erased).If the ECHOKE flag is supported, this description of ECHOK assumes that ECHOKE is not set.
ECHOKE
(c_lflag, FreeBSD, Linux, Mac OS X, Solaris) If set and if ICANON is set, the KILL character is echoed by erasing each character on the line.The way in which each character is erased is selected by the ECHOE and ECHOPRT flags.
ECHONL
(c_lflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set and if ICANON is set, the NL character is echoed, even if ECHO is not set.
ECHOPRT
(c_lflag, FreeBSD, Linux, Mac OS X, Solaris) If set and if both ICANON and ECHO are set, then the ERASE character (and WERASE character, if supported) cause all the characters being erased to be printed as they are erased. This is often useful on a hard-copy terminalto see exactly which characters are being deleted.
EXTPROC
(c_lflag, FreeBSD, Linux, Mac OS X) If set, canonical character processing is performed external to the operating system. This can be the case if the serial communication peripheral card can offload the host processor by doing some of the line discipline processing. This can also be the case when using pseudo terminals (Chapter 19).
FFDLY
(c_oflag, XSI, Linux, Solaris) Form feed delay mask. The values for the mask are FF0 or FF1.
FLUSHO
(c_lflag, FreeBSD, Linux, Mac OS X, Solaris) If set, output is being flushed. This flag is set when we type the DISCARD character; the flag is cleared when we type another DISCARD character. We can also set or clear this condition by setting or clearing this terminal flag
HUPCL
(c_cflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set, the modem control lines are lowered (i.e., the modem connection is broken)when the last process closes the device.
ICANON
(c_lflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set, canonical mode is in effect (Section 18.10). This enables the following characters:EOF, EOL, EOL2, ERASE, KILL, REPRINT, STATUS, and WERASE. The input characters are assembled into lines.If canonical mode is not enabled, read requests are satisfied directly from the input queue. A read does not return until at least MIN bytes have been received or the timeout value TIME has expired between bytes. Refer to Section 18.11 for additional details.
ICRNL
(c_iflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set and if IGNCR is not set, a received CR character is translated into a NL character.
IEXTEN
(c_lflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set, the extended, implementation-defined special characters are recognized and processed.
IGNBRK
(c_iflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) When set, a BREAK condition on input is ignored. See BRKINT for a way to have a BREAK condition either generate a SIGINT signal or be read as data.
IGNCR
(c_iflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set, a received CR character is ignored. If this flag is not set, it is possible to translate the received CR into a NL character if the ICRNL flag is set.
IGNPAR
(c_iflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) When set, an input byte with a framing error (other than a BREAK) or an input byte with a parity error is ignored.
IMAXBEL
(c_iflag, FreeBSD, Linux, Mac OS X, Solaris) Ring bell when input queue is full.
INLCR
(c_iflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set, a received NL character is translated into a CR character.
INPCK
(c_iflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) When set,input parity checking is enabled. If INPCK is not set, input parity checking is disabled.Parity‘‘generation and detection’’ and ‘‘input parity checking’’ are two different things. The generation and detection of parity bits is controlledby the PARENB flag. Setting this flag usually causes the device driver for the serial interface to generate parity for outgoing characters and to verify the parity of incoming characters. The flag PARODD determines whether the parity should be odd or even. If an input character arriveswith the wrong parity, then the state of the INPCK flag is checked. If this flag is set, then the IGNPAR flag is checked (to see whether the input byte with the parity error should be ignored); if the byte should not be ignored, then the PARMRK flag is checked to see which characters should be passed to the reading process.
ISIG
(c_lflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set, the input characters are compared against the special characters that cause the terminal-generated signals to be generated (INTR, QUIT, SUSP, and DSUSP); if equal, the corresponding signal is generated.
ISTRIP
(c_iflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) When set,valid input bytes are stripped to 7 bits. When this flag is not set, all 8 bits are processed.
IUCLC
(c_iflag, Linux, Solaris) Map uppercase to lowercase on input.
IUTF8
(c_iflag, Linux, Mac OS X) Allow character erase processing to workwith UTF-8 multibyte characters.
IXANY
(c_iflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) Enable any characters to restart output.
IXOFF
(c_iflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set,start–stop input control is enabled. When it notices that the input queue is getting full, the terminal driver outputs a STOP character. This character should be recognized by the device that is sending the data and cause the device to stop. Later, when the characters on the input queue have been processed, the terminal driver will output a START character. This should cause the device to resume sending data.
IXON
(c_iflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set,start–stop output control is enabled. When the terminal driver receives a STOP character, output stops. While the output is stopped, the next START character resumes the output. If this flag is not set, the START and STOP characters are read by the process as normal characters
MDMBUF
(c_cflag, FreeBSD, Mac OS X) Flow control the output according to the modem carrier flag. This is the old name for the CCAR_OFLOW flag
NLDLY
(c_oflag, XSI, Linux, Solaris) Newline delay mask. The values for the mask are NL0 or NL1.
NOFLSH
(c_lflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) By default,when the terminal driver generates the SIGINT and SIGQUIT signals,both the input and output queues are flushed. Also, when it generates the SIGSUSP signal, the input queue is flushed. If the NOFLSH flag is set, this normal flushing of the queues does not occur when these signals are generated.
NOKERNINFO
(c_lflag, FreeBSD, Mac OS X) When set, this flag prevents the STATUS character from printing information on the foreground process group. Regardless of whether this flag is set, however, the STATUS character still causes the SIGINFO signal to be sent to the foreground process group
OCRNL
(c_oflag, XSI, FreeBSD, Linux, Solaris) If set, map CR to NL on output.
OFDEL
(c_oflag, XSI, Linux, Solaris) If set, the output fill character is ASCII DEL; otherwise, it’s ASCII NUL. See the OFILL flag.
OFILL
(c_oflag, XSI, Linux, Solaris) If set, fill characters (either ASCII DEL orASCII NUL; see the OFDEL flag) are transmitted for a delay, instead ofusing a timed delay. See the six delay masks: BSDLY, CRDLY, FFDLY,NLDLY, TABDLY, and VTDLY.
OLCUC
(c_oflag, Linux, Solaris) If set, map lowercase characters to uppercase characters on output.
ONLCR
(c_oflag, XSI, FreeBSD, Linux, Mac OS X, Solaris) If set, map NL to CR-NL on output.
ONLRET
(c_oflag, XSI, FreeBSD, Linux, Solaris) If set, the NL character is assumed to perform the carriage return function on output
ONOCR
(c_oflag, XSI, FreeBSD, Linux, Solaris) If set, a CR is not output at column 0.
ONOEOT
(c_oflag, FreeBSD, Mac OS X) If set, EOT (ˆD) characters are discarded on output. This may be necessary on some terminals that interpret Control-D as a hangup
OPOST
(c_oflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set,implementation-defined output processing takes place. Refer to Figure 18.6 for the various implementation-defined flags for the c_oflag field.
OXTABS
(c_oflag, FreeBSD, Mac OS X) If set, tabs are expanded to spaces on output. This produces the same effect as setting the horizontal tab delay(TABDLY) to XTABS or TAB3.
PARENB
(c_cflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set, parity generation is enabled for outgoing characters, and parity checking is performed on incoming characters. The parity is odd if PARODD is set;otherwise, it is even parity. See also the discussion of the INPCK,IGNPAR, and PARMRK flags.
PAREXT
(c_cflag, Solaris) Select mark or space parity. If PARODD is set, the parity bit is always 1 (mark parity). Otherwise, the parity bit is always 0 (space parity).
PARMRK
(c_iflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) When set andif IGNPAR is not set, a byte with a framing error (other than a BREAK)or a byte with a parity error is read by the process as the three-character sequence \377, \0, X, where X is the byte received in error. If ISTRIP is not set, a valid \377 is passed to the process as \377, \377. If neither IGNPAR nor PARMRK is set, a byte with a framing error (other than a BREAK) or with a parity error is read as a single character \0.
PARODD
(c_cflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set, the parity for outgoing and incoming characters is odd parity. Otherwise,the parity is even parity. Note that the PARENB flag controls the generation and detection of parity.The PARODD flag also controls whether mark or space parity is usedwhen either the CMSPAR or PAREXT flag is set.
PENDIN
(c_lflag, FreeBSD, Linux, Mac OS X, Solaris) If set, any input that has not been read is reprinted by the system when the next character is input. This action is similar to what happens when we type the REPRINT character.
TABDLY
(c_oflag, XSI, Linux, Mac OS X, Solaris) Horizontal tab delay mask.The values for the mask are TAB0, TAB1, TAB2, or TAB3.The value XTABS is equal to TAB3. This value causes the system to expand tabs into spaces. The system assumes a tab stop every eight spaces, and we can’t change this assumption
TOSTOP
(c_lflag, POSIX.1, FreeBSD, Linux, Mac OS X, Solaris) If set and if the implementation supports job control, the SIGTTOU signal is sent to the process group of a background process that tries to write to its controlling terminal. By default, this signal stops all the processes in the process group. This signal is not generated by the terminal driver if the background process that is writing to the controlling terminal is either ignoring or blocking the signal.
VTDLY
(c_oflag, XSI, Linux, Solaris) Vertical tab delay mask. The values for the mask are VT0 and VT1.
XCASE
(c_lflag, Linux, Solaris) If set and if ICANON is also set, the terminal is assumed to be uppercase only, and all input is converted to lowercase.To input an uppercase character, precede it with a backslash. Similarly,the system outputs an uppercase character by preceding it with a backslash.(This option flag is obsolete today, since most, if not all,uppercase-only terminals have disappeared.)
stty(1) command
波特率函数
#include <termios.h>speed_t cfgetispeed(const struct termios *termptr);speed_t cfgetospeed(const struct termios *termptr);Both return: baud rate valueint cfsetispeed(struct termios *termptr, speed_t speed);int cfsetospeed(struct termios *termptr, speed_t speed);Both return: 0 if OK, −1 on errorthe speed setting or getting must be one of the following constants: B50, B75, B110, B134, B150, B200,B300, B600, B1200, B1800, B2400, B4800, B9600, B19200, or B38400常量B0表示挂断。调用tcsetattr时,如若将输出波特率指定为B0,则调制解调器的控制线就不再起作用。
行控制函数
#include <termios.h>// 等待所有输出被传递int tcdrain(int fd);// 用于对输入和输出流控制进行控制int tcflow(int fd, // TCOOFF输出被挂起// TCOON重新启动以前被挂起的输出// TCIOFF系统发送一个STOP字符,这将使终端设备停止发送数据// TCION系统发送一个START字符,使终端设备恢复发送数据int action);// 冲洗输入缓冲区【其中的数据是终端驱动程序已接收到,但用户程序尚未读取的】// 或冲洗输出缓冲区【其中的数据是用户程序已经写入,尚未被传递的】// queue需是下列3个常量之一// TCIFLUSH// TCOFLUSH// TCIOFLUSHint tcflush(int fd, int queue);// 在一个指定的时间区间内发送连续的0值位流。// 若duration为0,则此传递延续0.25~0.5s。int tcsendbreak(int fd, int duration);All four return: 0 if OK, −1 on error
终端标识
多数UNIX系统中,控制终端名字一直是/dev/tty。POSIX.1提供了一个运行时,函数。用来确定控制终端名字。#include <stdio.h>char *ctermid(char *ptr);Returns: pointer to name of controlling terminal on success, pointer to empty string on error如果ptr非空,则被认为是一个指针。指向长度至少为L_ctermid字节的数组。进程的控制终端名存储在该数组中。常量L_ctermid被定义在<stdio.h>。 若ptr是空指针,指向的是静态空间某个数组。#include <unistd.h>// 若为终端设备,返回1// 否则,返回0int isatty(int fd);// 指向终端路径名的指针// 若出错,返回NULL。char* ttyname(int fd);