📄 xbd_chap11.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>General Terminal Interface</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h2><a name="tag_11"></a>General Terminal Interface</h2><p>This chapter describes a general terminal interface that shall be provided. It shall be supported on any asynchronouscommunications ports if the implementation provides them. It is implementation-defined whether it supports network connections orsynchronous ports, or both.</p><h3><a name="tag_11_01"></a>Interface Characteristics</h3><h4><a name="tag_11_01_01"></a>Opening a Terminal Device File</h4><p>When a terminal device file is opened, it normally causes the thread to wait until a connection is established. In practice,application programs seldom open these files; they are opened by special programs and become an application's standard input,output, and error files.</p><p>As described in <a href="../functions/open.html"><i>open</i>()</a>, opening a terminal device file with the O_NONBLOCK flagclear shall cause the thread to block until the terminal device is ready and available. If CLOCAL mode is not set, this meansblocking until a connection is established. If CLOCAL mode is set in the terminal, or the O_NONBLOCK flag is specified in the <ahref="../functions/open.html"><i>open</i>()</a>, the <a href="../functions/open.html"><i>open</i>()</a> function shall return afile descriptor without waiting for a connection to be established.</p><h4><a name="tag_11_01_02"></a>Process Groups</h4><p>A terminal may have a foreground process group associated with it. This foreground process group plays a special role inhandling signal-generating input characters, as discussed in <a href="#tag_11_01_09">Special Characters</a> .</p><p>A command interpreter process supporting job control can allocate the terminal to different jobs, or process groups, by placingrelated processes in a single process group and associating this process group with the terminal. A terminal's foreground processgroup may be set or examined by a process, assuming the permission requirements are met; see <a href="../functions/tcgetpgrp.html"><i>tcgetpgrp</i>()</a> and <a href="../functions/tcsetpgrp.html"><i>tcsetpgrp</i>()</a>. The terminalinterface aids in this allocation by restricting access to the terminal by processes that are not in the current process group; see<a href="#tag_11_01_04">Terminal Access Control</a> .</p><p>When there is no longer any process whose process ID or process group ID matches the foreground process group ID, the terminalshall have no foreground process group. It is unspecified whether the terminal has a foreground process group when there is aprocess whose process ID matches the foreground process group ID, but whose process group ID does not. No actions defined inIEEE Std 1003.1-2001, other than allocation of a controlling terminal or a successful call to <a href="../functions/tcsetpgrp.html"><i>tcsetpgrp</i>()</a>, shall cause a process group to become the foreground process group of theterminal.</p><h4><a name="tag_11_01_03"></a>The Controlling Terminal</h4><p>A terminal may belong to a process as its controlling terminal. Each process of a session that has a controlling terminal hasthe same controlling terminal. A terminal may be the controlling terminal for at most one session. The controlling terminal for asession is allocated by the session leader in an implementation-defined manner. If a session leader has no controlling terminal,and opens a terminal device file that is not already associated with a session without using the O_NOCTTY option (see <a href="../functions/open.html"><i>open</i>()</a>), it is implementation-defined whether the terminal becomes the controlling terminal ofthe session leader. If a process which is not a session leader opens a terminal file, or the O_NOCTTY option is used on <a href="../functions/open.html"><i>open</i>()</a>, then that terminal shall not become the controlling terminal of the calling process.When a controlling terminal becomes associated with a session, its foreground process group shall be set to the process group ofthe session leader.</p><p>The controlling terminal is inherited by a child process during a <a href="../functions/fork.html"><i>fork</i>()</a> functioncall. A process relinquishes its controlling terminal when it creates a new session with the <a href="../functions/setsid.html"><i>setsid</i>()</a> function; other processes remaining in the old session that had this terminal astheir controlling terminal continue to have it. Upon the close of the last file descriptor in the system (whether or not it is inthe current session) associated with the controlling terminal, it is unspecified whether all processes that had that terminal astheir controlling terminal cease to have any controlling terminal. Whether and how a session leader can reacquire a controllingterminal after the controlling terminal has been relinquished in this fashion is unspecified. A process does not relinquish itscontrolling terminal simply by closing all of its file descriptors associated with the controlling terminal if other processescontinue to have it open.</p><p>When a controlling process terminates, the controlling terminal is dissociated from the current session, allowing it to beacquired by a new session leader. Subsequent access to the terminal by other processes in the earlier session may be denied, withattempts to access the terminal treated as if a modem disconnect had been sensed.</p><h4><a name="tag_11_01_04"></a>Terminal Access Control</h4><p>If a process is in the foreground process group of its controlling terminal, read operations shall be allowed, as described in<a href="#tag_11_01_05">Input Processing and Reading Data</a> . Any attempts by a process in a background process group to readfrom its controlling terminal cause its process group to be sent a SIGTTIN signal unless one of the following special casesapplies: if the reading process is ignoring or blocking the SIGTTIN signal, or if the process group of the reading process isorphaned, the <a href="../functions/read.html"><i>read</i>()</a> shall return -1, with <i>errno</i> set to [EIO] and no signalshall be sent. The default action of the SIGTTIN signal shall be to stop the process to which it is sent. See <a href="signal.h.html"><i><signal.h></i></a> .</p><p>If a process is in the foreground process group of its controlling terminal, write operations shall be allowed as described in<a href="#tag_11_01_08">Writing Data and Output Processing</a> . Attempts by a process in a background process group to write toits controlling terminal shall cause the process group to be sent a SIGTTOU signal unless one of the following special casesapplies: if TOSTOP is not set, or if TOSTOP is set and the process is ignoring or blocking the SIGTTOU signal, the process isallowed to write to the terminal and the SIGTTOU signal is not sent. If TOSTOP is set, and the process group of the writing processis orphaned, and the writing process is not ignoring or blocking the SIGTTOU signal, the <a href="../functions/write.html"><i>write</i>()</a> shall return -1, with <i>errno</i> set to [EIO] and no signal shall be sent.</p><p>Certain calls that set terminal parameters are treated in the same fashion as <a href="../functions/write.html"><i>write</i>()</a>, except that TOSTOP is ignored; that is, the effect is identical to that of terminalwrites when TOSTOP is set (see <a href="#tag_11_02_05">Local Modes</a> , <a href="../functions/tcdrain.html"><i>tcdrain</i>()</a>,<a href="../functions/tcflow.html"><i>tcflow</i>()</a>, <a href="../functions/tcflush.html"><i>tcflush</i>()</a>, <a href="../functions/tcsendbreak.html"><i>tcsendbreak</i>()</a>, <a href="../functions/tcsetattr.html"><i>tcsetattr</i>()</a>, and <ahref="../functions/tcsetpgrp.html"><i>tcsetpgrp</i>()</a>).</p><h4><a name="tag_11_01_05"></a>Input Processing and Reading Data</h4><p>A terminal device associated with a terminal device file may operate in full-duplex mode, so that data may arrive even whileoutput is occurring. Each terminal device file has an input queue associated with it, into which incoming data is stored by thesystem before being read by a process. The system may impose a limit, {MAX_INPUT}, on the number of bytes that may be stored in theinput queue. The behavior of the system when this limit is exceeded is implementation-defined.</p><p>Two general kinds of input processing are available, determined by whether the terminal device file is in canonical mode ornon-canonical mode. These modes are described in <a href="#tag_11_01_06">Canonical Mode Input Processing</a> and <a href="#tag_11_01_07">Non-Canonical Mode Input Processing</a> . Additionally, input characters are processed according to the<i>c_iflag</i> (see <a href="#tag_11_02_02">Input Modes</a> ) and <i>c_lflag</i> (see <a href="#tag_11_02_05">Local Modes</a> )fields. Such processing can include ``echoing'', which in general means transmitting input characters immediately back to theterminal when they are received from the terminal. This is useful for terminals that can operate in full-duplex mode.</p><p>The manner in which data is provided to a process reading from a terminal device file is dependent on whether the terminal fileis in canonical or non-canonical mode, and on whether or not the O_NONBLOCK flag is set by <a href="../functions/open.html"><i>open</i>()</a> or <a href="../functions/fcntl.html"><i>fcntl</i>()</a>.</p><p>If the O_NONBLOCK flag is clear, then the read request shall be blocked until data is available or a signal has been received.If the O_NONBLOCK flag is set, then the read request shall be completed, without blocking, in one of three ways:</p><ol><li><p>If there is enough data available to satisfy the entire request, the <a href="../functions/read.html"><i>read</i>()</a> shallcomplete successfully and shall return the number of bytes read.</p></li><li><p>If there is not enough data available to satisfy the entire request, the <a href="../functions/read.html"><i>read</i>()</a>shall complete successfully, having read as much data as possible, and shall return the number of bytes it was able to read.</p></li><li><p>If there is no data available, the <a href="../functions/read.html"><i>read</i>()</a> shall return -1, with <i>errno</i> set to[EAGAIN].</p></li></ol><p>When data is available depends on whether the input processing mode is canonical or non-canonical. <a href="#tag_11_01_06">Canonical Mode Input Processing</a> and <a href="#tag_11_01_07">Non-Canonical Mode Input Processing</a> describeeach of these input processing modes.</p><h4><a name="tag_11_01_06"></a>Canonical Mode Input Processing</h4><p>In canonical mode input processing, terminal input is processed in units of lines. A line is delimited by a newline character(NL), an end-of-file character (EOF), or an end-of-line (EOL) character. See <a href="#tag_11_01_09">Special Characters</a> formore information on EOF and EOL. This means that a read request shall not return until an entire line has been typed or a signalhas been received. Also, no matter how many bytes are requested in the <a href="../functions/read.html"><i>read</i>()</a> call, atmost one line shall be returned. It is not, however, necessary to read a whole line at once; any number of bytes, even one, may berequested in a <a href="../functions/read.html"><i>read</i>()</a> without losing information.</p><p>If {MAX_CANON} is defined for this terminal device, it shall be a limit on the number of bytes in a line. The behavior of thesystem when this limit is exceeded is implementation-defined. If {MAX_CANON} is not defined, there shall be no such limit; see <ahref="../functions/pathconf.html"><i>pathconf</i>()</a>.</p><p>Erase and kill processing occur when either of two special characters, the ERASE and KILL characters (see <a href="#tag_11_01_09">Special Characters</a> ), is received. This processing shall affect data in the input queue that has not yet beendelimited by an NL, EOF, or EOL character. This un-delimited data makes up the current line. The ERASE character shall delete thelast character in the current line, if there is one. The KILL character shall delete all data in the current line, if there is any.The ERASE and KILL characters shall have no effect if there is no data in the current line. The ERASE and KILL charactersthemselves shall not be placed in the input queue.</p><h4><a name="tag_11_01_07"></a>Non-Canonical Mode Input Processing</h4><p>In non-canonical mode input processing, input bytes are not assembled into lines, and erase and kill processing shall not occur.The values of the MIN and TIME members of the <i>c_cc</i> array are used to determine how to process the bytes received.IEEE Std 1003.1-2001 does not specify whether the setting of O_NONBLOCK takes precedence over MIN or TIME settings.Therefore, if O_NONBLOCK is set, <a href="../functions/read.html"><i>read</i>()</a> may return immediately, regardless of thesetting of MIN or TIME. Also, if no data is available, <a href="../functions/read.html"><i>read</i>()</a> may either return 0, orreturn -1 with <i>errno</i> set to [EAGAIN].</p><p>MIN represents the minimum number of bytes that should be received when the <a href="../functions/read.html"><i>read</i>()</a>function returns successfully. TIME is a timer of 0.1 second granularity that is used to time out bursty and short-term datatransmissions. If MIN is greater than {MAX_INPUT}, the response to the request is undefined. The four possible values for MIN andTIME and their interactions are described below.</p><h5><a name="tag_11_01_07_01"></a>Case A: MIN>0, TIME>0</h5><p>In case A, TIME serves as an inter-byte timer which shall be activated after the first byte is received. Since it is aninter-byte timer, it shall be reset after a byte is received. The interaction between MIN and TIME is as follows. As soon as onebyte is received, the inter-byte timer shall be started. If MIN bytes are received before the inter-byte timer expires (rememberthat the timer is reset upon receipt of each byte), the read shall be satisfied. If the timer expires before MIN bytes arereceived, the characters received to that point shall be returned to the user. Note that if TIME expires at least one byte shall bereturned because the timer would not have been enabled unless a byte was received. In this case (MIN>0, TIME>0) the readshall block until the MIN and TIME mechanisms are activated by the receipt of the first byte, or a signal is received. If data isin the buffer at the time of the <a href="../functions/read.html"><i>read</i>()</a>, the result shall be as if data has beenreceived immediately after the <a href="../functions/read.html"><i>read</i>()</a>.</p><h5><a name="tag_11_01_07_02"></a>Case B: MIN>0, TIME=0</h5><p>In case B, since the value of TIME is zero, the timer plays no role and only MIN is significant. A pending read shall not besatisfied until MIN bytes are received (that is, the pending read shall block until MIN bytes are received), or a signal isreceived. A program that uses case B to read record-based terminal I/O may block indefinitely in the read operation.</p><h5><a name="tag_11_01_07_03"></a>Case C: MIN=0, TIME>0</h5><p>In case C, since MIN=0, TIME no longer represents an inter-byte timer. It now serves as a read timer that shall be activated assoon as the <a href="../functions/read.html"><i>read</i>()</a> function is processed. A read shall be satisfied as soon as a singlebyte is received or the read timer expires. Note that in case C if the timer expires, no bytes shall be returned. If the timer doesnot expire, the only way the read can be satisfied is if a byte is received. If bytes are not received, the read shall not blockindefinitely waiting for a byte; if no byte is received within TIME*0.1 seconds after the read is initiated, the <a href="../functions/read.html"><i>read</i>()</a> shall return a value of zero, having read no data. If data is in the buffer at the timeof the <a href="../functions/read.html"><i>read</i>()</a>, the timer shall be started as if data has been received immediatelyafter the <a href="../functions/read.html"><i>read</i>()</a>.</p><h5><a name="tag_11_01_07_04"></a>Case D: MIN=0, TIME=0</h5><p>The minimum of either the number of bytes requested or the number of bytes currently available shall be returned without waitingfor more bytes to be input. If no characters are available, <a href="../functions/read.html"><i>read</i>()</a> shall return a valueof zero, having read no data.</p><h4><a name="tag_11_01_08"></a>Writing Data and Output Processing</h4><p>When a process writes one or more bytes to a terminal device file, they are processed according to the <i>c_oflag</i> field (see<a href="#tag_11_02_03">Output Modes</a> ). The implementation may provide a buffering mechanism; as such, when a call to <a href="../functions/write.html"><i>write</i>()</a> completes, all of the bytes written have been scheduled for transmission to thedevice, but the transmission has not necessarily completed. See <a href="../functions/write.html"><i>write</i>()</a> for theeffects of O_NONBLOCK on <a href="../functions/write.html"><i>write</i>()</a>.</p><h4><a name="tag_11_01_09"></a>Special Characters</h4><p>Certain characters have special functions on input or output or both. These functions are summarized as follows:</p><dl compact><dt>INTR</dt><dd>Special character on input, which is recognized if the ISIG flag is set. Generates a SIGINT signal which is sent to allprocesses in the foreground process group for which the terminal is the controlling terminal. If ISIG is set, the INTR charactershall be discarded when processed.</dd><dt>QUIT</dt><dd>Special character on input, which is recognized if the ISIG flag is set. Generates a SIGQUIT signal which is sent to allprocesses in the foreground process group for which the terminal is the controlling terminal. If ISIG is set, the QUIT charactershall be discarded when processed.</dd><dt>ERASE</dt><dd>Special character on input, which is recognized if the ICANON flag is set. Erases the last character in the current line; see<a href="#tag_11_01_06">Canonical Mode Input Processing</a> . It shall not erase beyond the start of a line, as delimited by an NL,EOF, or EOL character. If ICANON is set, the ERASE character shall be discarded when processed.</dd><dt>KILL</dt><dd>Special character on input, which is recognized if the ICANON flag is set. Deletes the entire line, as delimited by an NL, EOF,or EOL character. If ICANON is set, the KILL character shall be discarded when processed.</dd><dt>EOF</dt><dd>Special character on input, which is recognized if the ICANON flag is set. When received, all the bytes waiting to be read areimmediately passed to the process without waiting for a newline, and the EOF is discarded. Thus, if there are no bytes waiting(that is, the EOF occurred at the beginning of a line), a byte count of zero shall be returned from the <a href="../functions/read.html"><i>read</i>()</a>, representing an end-of-file indication. If ICANON is set, the EOF character shall bediscarded when processed.</dd><dt>NL</dt><dd>Special character on input, which is recognized if the ICANON flag is set. It is the line delimiter newline. It cannot bechanged.</dd><dt>EOL</dt><dd>Special character on input, which is recognized if the ICANON flag is set. It is an additional line delimiter, like NL.</dd><dt>SUSP</dt><dd>If the ISIG flag is set, receipt of the SUSP character shall cause a SIGTSTP signal to be sent to all processes in theforeground process group for which the terminal is the controlling terminal, and the SUSP character shall be discarded whenprocessed.</dd><dt>STOP</dt><dd>Special character on both input and output, which is recognized if the IXON (output control) or IXOFF (input control) flag isset. Can be used to suspend output temporarily. It is useful with CRT terminals to prevent output from disappearing before it canbe read. If IXON is set, the STOP character shall be discarded when processed.</dd><dt>START</dt><dd>Special character on both input and output, which is recognized if the IXON (output control) or IXOFF (input control) flag isset. Can be used to resume output that has been suspended by a STOP character. If IXON is set, the START character shall bediscarded when processed.</dd><dt>CR</dt><dd>Special character on input, which is recognized if the ICANON flag is set; it is the carriage-return character. When ICANON andICRNL are set and IGNCR is not set, this character shall be translated into an NL, and shall have the same effect as an NLcharacter.</dd></dl><p>The NL and CR characters cannot be changed. It is implementation-defined whether the START and STOP characters can be changed.The values for INTR, QUIT, ERASE, KILL, EOF, EOL, and SUSP shall be changeable to suit individual tastes. Special characterfunctions associated with changeable special control characters can be disabled individually.</p><p>If two or more special characters have the same value, the function performed when that character is received is undefined.</p><p>A special character is recognized not only by its value, but also by its context; for example, an implementation may supportmulti-byte sequences that have a meaning different from the meaning of the bytes when considered individually. Implementations mayalso support additional single-byte functions. These implementation-defined multi-byte or single-byte functions shall be recognizedonly if the IEXTEN flag is set; otherwise, data is received without interpretation, except as required to recognize the specialcharacters defined in this section.</p><p><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">If IEXTEN is set, the ERASE, KILL, and EOF characters can be escaped by a preceding <tt>'\'</tt> character, in which case nospecial function shall occur. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><h4><a name="tag_11_01_10"></a>Modem Disconnect</h4><p>If a modem disconnect is detected by the terminal interface for a controlling terminal, and if CLOCAL is not set in the<i>c_cflag</i> field for the terminal (see <a href="#tag_11_02_04">Control Modes</a> ), the SIGHUP signal shall be sent to thecontrolling process for which the terminal is the controlling terminal. Unless other arrangements have been made, this shall causethe controlling process to terminate (see <a href="../functions/exit.html"><i>exit</i>()</a>). Any subsequent read from theterminal device shall return the value of zero, indicating end-of-file; see <a href="../functions/read.html"><i>read</i>()</a>.Thus, processes that read a terminal file and test for end-of-file can terminate appropriately after a disconnect. If the EIOcondition as specified in <a href="../functions/read.html"><i>read</i>()</a> also exists, it is unspecified whether on EOFcondition or [EIO] is returned. Any subsequent <a href="../functions/write.html"><i>write</i>()</a> to the terminal device shallreturn -1, with <i>errno</i> set to [EIO], until the device is closed.</p><h4><a name="tag_11_01_11"></a>Closing a Terminal Device File</h4><p>The last process to close a terminal device file shall cause any output to be sent to the device and any input to be discarded.If HUPCL is set in the control modes and the communications port supports a disconnect function, the terminal device shall performa disconnect.</p><h3><a name="tag_11_02"></a>Parameters that Can be Set</h3><h4><a name="tag_11_02_01"></a>The termios Structure</h4><p>Routines that need to control certain terminal I/O characteristics shall do so by using the <b>termios</b> structure as definedin the <a href="termios.h.html"><i><termios.h></i></a> header. The members of this structure include (but are not limitedto):</p><center><table border="1" cellpadding="3" align="center"><tr valign="top"><th align="center"><p class="tent"><b>Member</b></p></th><th align="center"><p class="tent"><b>Array</b></p></th><th align="center"><p class="tent"><b>Member</b></p></th><th align="center"><p class="tent"><b> </b></p></th></tr><tr valign="top"><th align="center"><p class="tent"><b>Type</b></p></th><th align="center"><p class="tent"><b>Size</b></p></th><th align="center"><p class="tent"><b>Name</b></p></th><th align="center"><p class="tent"><b>Description</b></p></th></tr><tr valign="top"><td align="left"><p class="tent"><b>tcflag_t</b></p></td><td align="left"><p class="tent"> </p></td><td align="left"><p class="tent"><i>c_iflag</i></p></td><td align="left"><p class="tent">Input modes.</p></td></tr><tr valign="top"><td align="left"><p class="tent"><b>tcflag_t</b></p></td><td align="left"><p class="tent"> </p></td><td align="left"><p class="tent"><i>c_oflag</i></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -