📄 library_16.html
字号:
process opens the terminal file later on, it will see the changed
attributes even though it doesn't have anything to do with the open file
descriptor you originally specified in changing the attributes.
<P>
Similarly, if a single process has multiple or duplicated file
descriptors for the same terminal device, changing the terminal
attributes affects input and output to all of these file
descriptors. This means, for example, that you can't open one file
descriptor or stream to read from a terminal in the normal
line-buffered, echoed mode; and simultaneously have another file
descriptor for the same terminal that you use to read from it in
single-character, non-echoed mode. Instead, you have to explicitly
switch the terminal back and forth between the two modes.
<P>
<H3><A NAME="SEC275" HREF="library_toc.html#SEC275" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC275">Setting Terminal Modes Properly</A></H3>
<P>
When you set terminal modes, you should call <CODE>tcgetattr</CODE> first to
get the current modes of the particular terminal device, modify only
those modes that you are really interested in, and store the result with
<CODE>tcsetattr</CODE>.
<P>
It's a bad idea to simply initialize a <CODE>struct termios</CODE> structure
to a chosen set of attributes and pass it directly to <CODE>tcsetattr</CODE>.
Your program may be run years from now, on systems that support members
not documented in this manual. The way to avoid setting these members
to unreasonable values is to avoid changing them.
<P>
What's more, different terminal devices may require different mode
settings in order to function properly. So you should avoid blindly
copying attributes from one terminal device to another.
<P>
When a member contains a collection of independent flags, as the
<CODE>c_iflag</CODE>, <CODE>c_oflag</CODE> and <CODE>c_cflag</CODE> members do, even
setting the entire member is a bad idea, because particular operating
systems have their own flags. Instead, you should start with the
current value of the member and alter only the flags whose values matter
in your program, leaving any other flags unchanged.
<P>
Here is an example of how to set one flag (<CODE>ISTRIP</CODE>) in the
<CODE>struct termios</CODE> structure while properly preserving all the other
data in the structure:
<P>
<PRE>
int
set_istrip (int desc, int value)
{
struct termios settings;
int result;
result = tcgetattr (desc, &settings);
if (result < 0)
{
perror ("error in tcgetattr");
return 0;
}
settings.c_iflag &= ~ISTRIP;
if (value)
settings.c_iflag |= ISTRIP;
result = tcgetattr (desc, &settings);
if (result < 0)
{
perror ("error in tcgetattr");
return;
}
return 1;
}
</PRE>
<P>
<H3><A NAME="SEC276" HREF="library_toc.html#SEC276" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC276">Input Modes</A></H3>
<P>
This section describes the terminal attribute flags that control
fairly low-level aspects of input processing: handling of parity errors,
break signals, flow control, and <KBD>RET</KBD> and <KBD>LFD</KBD> characters.
<P>
All of these flags are bits in the <CODE>c_iflag</CODE> member of the
<CODE>struct termios</CODE> structure. The member is an integer, and you
change flags using the operators <CODE>&</CODE>, <CODE>|</CODE> and <CODE>^</CODE>. Don't
try to specify the entire value for <CODE>c_iflag</CODE>---instead, change
only specific flags and leave the rest untouched (see section <A HREF="library_16.html#SEC275" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC275">Setting Terminal Modes Properly</A>).
<P>
<A NAME="IDX1103"></A>
<DL COMPACT>
<A NAME="IDX1104"></A>
<DT><CODE>INPCK</CODE>
<DD>If this bit is set, input parity checking is enabled. If it is not set,
no checking at all is done for parity errors on input; the
characters are simply passed through to the application.
<P>
Parity checking on input processing is independent of whether parity
detection and generation on the underlying terminal hardware is enabled;
see section <A HREF="library_16.html#SEC278" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC278">Control Modes</A>. For example, you could clear the <CODE>INPCK</CODE>
input mode flag and set the <CODE>PARENB</CODE> control mode flag to ignore
parity errors on input, but still generate parity on output.
<P>
If this bit is set, what happens when a parity error is detected depends
on whether the <CODE>IGNPAR</CODE> or <CODE>PARMRK</CODE> bits are set. If neither
of these bits are set, a byte with a parity error is passed to the
application as a <CODE>'\0'</CODE> character.
<A NAME="IDX1105"></A>
<P>
<DT><CODE>IGNPAR</CODE>
<DD>If this bit is set, any byte with a framing or parity error is ignored.
This is only useful if <CODE>INPCK</CODE> is also set.
<A NAME="IDX1106"></A>
<P>
<DT><CODE>PARMRK</CODE>
<DD>If this bit is set, input bytes with parity or framing errors are marked
when passed to the program. This bit is meaningful only when
<CODE>INPCK</CODE> is set and <CODE>IGNPAR</CODE> is not set.
<P>
The way erroneous bytes are marked is with two preceding bytes,
<CODE>377</CODE> and <CODE>0</CODE>. Thus, the program actually reads three bytes
for one erroneous byte received from the terminal.
<P>
If a valid byte has the value <CODE>0377</CODE>, and <CODE>ISTRIP</CODE> (see below)
is not set, the program might confuse it with the prefix that marks a
parity error. So a valid byte <CODE>0377</CODE> is passed to the program as
two bytes, <CODE>0377</CODE> <CODE>0377</CODE>, in this case.
<A NAME="IDX1107"></A>
<P>
<DT><CODE>ISTRIP</CODE>
<DD>If this bit is set, valid input bytes are stripped to seven bits;
otherwise, all eight bits are available for programs to read.
<A NAME="IDX1108"></A>
<P>
<DT><CODE>IGNBRK</CODE>
<DD>If this bit is set, break conditions are ignored.
<A NAME="IDX1109"></A>
<P>
A <DFN>break condition</DFN> is defined in the context of asynchronous
serial data transmission as a series of zero-value bits longer than a
single byte.
<A NAME="IDX1110"></A>
<P>
<DT><CODE>BRKINT</CODE>
<DD>If this bit is set and <CODE>IGNBRK</CODE> is not set, a break condition
clears the terminal input and output queues and raises a <CODE>SIGINT</CODE>
signal for the foreground process group associated with the terminal.
<P>
If neither <CODE>BRKINT</CODE> nor <CODE>IGNBRK</CODE> are set, a break condition is
passed to the application as a single <CODE>'\0'</CODE> character if
<CODE>PARMRK</CODE> is not set, or otherwise as a three-character sequence
<CODE>'\377'</CODE>, <CODE>'\0'</CODE>, <CODE>'\0'</CODE>.
<A NAME="IDX1111"></A>
<P>
<DT><CODE>IGNCR</CODE>
<DD>If this bit is set, carriage return characters (<CODE>'\r'</CODE>) are
discarded on input. Discarding carriage return may be useful on
terminals that send both carriage return and linefeed when you type the
<KBD>RET</KBD> key.
<A NAME="IDX1112"></A>
<P>
<DT><CODE>ICRNL</CODE>
<DD>If this bit is set and <CODE>IGNCR</CODE> is not set, carriage return characters
(<CODE>'\r'</CODE>) received as input are passed to the application as newline
characters (<CODE>'\n'</CODE>).
<A NAME="IDX1113"></A>
<P>
<DT><CODE>INLCR</CODE>
<DD>If this bit is set, newline characters (<CODE>'\n'</CODE>) received as input
are passed to the application as carriage return characters (<CODE>'\r'</CODE>).
<A NAME="IDX1114"></A>
<P>
<DT><CODE>IXOFF</CODE>
<DD>If this bit is set, start/stop control on input is enabled. In other
words, the computer sends STOP and START characters as necessary to
prevent input from coming in faster than programs are reading it. The
idea is that the actual terminal hardware that is generating the input
data responds to a STOP character by suspending transmission, and to a
START character by resuming transmission. See section <A HREF="library_16.html#SEC285" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC285">Special Characters for Flow Control</A>.
<A NAME="IDX1115"></A>
<P>
<DT><CODE>IXON</CODE>
<DD>If this bit is set, start/stop control on output is enabled. In other
words, if the computer receives a STOP character, it suspends output
until a START character is received. In this case, the STOP and START
characters are never passed to the application program. If this bit is
not set, then START and STOP can be read as ordinary characters.
See section <A HREF="library_16.html#SEC285" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC285">Special Characters for Flow Control</A>.
<P>
<DT><CODE>IXANY</CODE>
<DD>If this bit is set, any input character restarts output when output has
been suspended with the STOP character. Otherwise, only the START
character restarts output.
<P>
<DT><CODE>IMAXBEL</CODE>
<DD>If this bit is set, then filling up the terminal input buffer sends a
BEL character (code <CODE>007</CODE>) to the terminal to ring the bell.
</DL>
<P>
<H3><A NAME="SEC277" HREF="library_toc.html#SEC277" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC277">Output Modes</A></H3>
<P>
This section describes the terminal flags and fields that control how
output characters are translated and padded for display. All of these
are contained in the <CODE>c_oflag</CODE> member of the <CODE>struct termios</CODE>
structure.
<P>
The <CODE>c_oflag</CODE> member itself is an integer, and you change the flags
and fields using the operators <CODE>&</CODE>, <CODE>|</CODE>, and <CODE>^</CODE>. Don't
try to specify the entire value for <CODE>c_oflag</CODE>---instead, change
only specific flags and leave the rest untouched (see section <A HREF="library_16.html#SEC275" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC275">Setting Terminal Modes Properly</A>).
<P>
<A NAME="IDX1116"></A>
<U>Macro:</U> int <B>OPOST</B><P>
If this bit is set, output data is processed in some unspecified way so
that it is displayed appropriately on the terminal device. This
typically includes mapping newline characters (<CODE>'\n'</CODE>) onto
carriage return and linefeed pairs.
<P>
If this bit isn't set, the characters are transmitted as-is.
<P>
The following three bits are BSD features, and they have no effect on
non-BSD systems. On all systems, they are effective only if
<CODE>OPOST</CODE> is set.
<P>
<A NAME="IDX1117"></A>
<U>Macro:</U> int <B>ONLCR</B><P>
If this bit is set, convert the newline character on output into a pair
of characters, carriage return followed by linefeed.
<P>
<A NAME="IDX1118"></A>
<U>Macro:</U> int <B>OXTABS</B><P>
If this bit is set, convert tab characters on output into the appropriate
number of spaces to emulate a tab stop every eight columns.
<P>
<A NAME="IDX1119"></A>
<U>Macro:</U> int <B>ONOEOT</B><P>
If this bit is set, discard <KBD>C-d</KBD> characters (code <CODE>004</CODE>) on
output. These characters cause many dial-up terminals to disconnect.
<P>
<H3><A NAME="SEC278" HREF="library_toc.html#SEC278" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC278">Control Modes</A></H3>
<P>
This section describes the terminal flags and fields that control
parameters usually associated with asynchronous serial data
transmission. These flags may not make sense for other kinds of
terminal ports (such as a network connection pseudo-terminal). All of
these are contained in the <CODE>c_cflag</CODE> member of the <CODE>struct
termios</CODE> structure.
<P>
The <CODE>c_cflag</CODE> member itself is an integer, and you change the flags
and fields using the operators <CODE>&</CODE>, <CODE>|</CODE>, and <CODE>^</CODE>. Don't
try to specify the entire value for <CODE>c_cflag</CODE>---instead, change
only specific flags and leave the rest untouched (see section <A HREF="library_16.html#SEC275" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC275">Setting Terminal Modes Properly</A>).
<P>
<A NAME="IDX1120"></A>
<DL COMPACT>
<DT><CODE>CLOCAL</CODE>
<DD>If this bit is set, it indicates that the terminal is connected
"locally" and that the modem status lines (such as carrier detect)
should be ignored.
<A NAME="IDX1122"></A>
<A NAME="IDX1121"></A>
<P>
If this bit is not set and you call <CODE>open</CODE> without the
<CODE>O_NONBLOCK</CODE> flag set, <CODE>open</CODE> blocks until a modem
connection is established.
<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -