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

📄 library_16.html

📁 Linux程序员的工作手册
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!-- This HTML file has been created by texi2html 1.27     from library.texinfo on 3 March 1994 --><TITLE>The GNU C Library - Low-Level Terminal Interface</TITLE><P>Go to the <A HREF="library_15.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html">previous</A>, <A HREF="library_17.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html">next</A> section.<P><H1><A NAME="SEC268" HREF="library_toc.html#SEC268" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC268">Low-Level Terminal Interface</A></H1><P>This chapter describes functions that are specific to terminal devices.You can use these functions to do things like turn off input echoing;set serial line characteristics such as line speed and flow control; andchange which characters are used for end-of-file, command-line editing,sending signals, and similar control functions.<P>Most of the functions in this chapter operate on file descriptors.See section <A HREF="library_12.html#SEC171" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_12.html#SEC171">Low-Level Input/Output</A>, for more information about what a filedescriptor is and how to open a file descriptor for a terminal device.<P><A NAME="IDX1080"></A><A NAME="IDX1081"></A><H2><A NAME="SEC269" HREF="library_toc.html#SEC269" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC269">Identifying Terminals</A></H2><P>The functions described in this chapter only work on files thatcorrespond to terminal devices.  You can find out whether a filedescriptor is associated with a terminal by using the <CODE>isatty</CODE>function.<A NAME="IDX1082"></A><P>Prototypes for both <CODE>isatty</CODE> and <CODE>ttyname</CODE> are declared inthe header file <TT>`unistd.h'</TT>.<P><A NAME="IDX1083"></A><U>Function:</U> int <B>isatty</B> <I>(int <VAR>filedes</VAR>)</I><P>This function returns <CODE>1</CODE> if <VAR>filedes</VAR> is a file descriptorassociated with an open terminal device, and <CODE>0</CODE> otherwise.<P>If a file descriptor is associated with a terminal, you can get itsassociated file name using the <CODE>ttyname</CODE> function.  See also the<CODE>ctermid</CODE> function, described in section <A HREF="library_24.html#SEC426" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_24.html#SEC426">Identifying the Controlling Terminal</A>.<P><A NAME="IDX1084"></A><U>Function:</U> char * <B>ttyname</B> <I>(int <VAR>filedes</VAR>)</I><P>If the file descriptor <VAR>filedes</VAR> is associated with a terminaldevice, the <CODE>ttyname</CODE> function returns a pointer to astatically-allocated, null-terminated string containing the file name ofthe terminal file.  The value is a null pointer if the file descriptorisn't associated with a terminal, or the file name cannot be determined.<P><H2><A NAME="SEC270" HREF="library_toc.html#SEC270" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC270">I/O Queues</A></H2><P>Many of the remaining functions in this section refer to the input andoutput queues of a terminal device.  These queues implement a form ofbuffering <EM>within the kernel</EM> independent of the bufferingimplemented by I/O streams (see section <A HREF="library_11.html#SEC117" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC117">Input/Output on Streams</A>).<A NAME="IDX1085"></A><A NAME="IDX1086"></A><P>The <DFN>terminal input queue</DFN> is also sometimes referred to as its<DFN>typeahead buffer</DFN>.  It holds the characters that have been receivedfrom the terminal but not yet read by any process.<P>The size of the terminal's input queue is described by the<CODE>_POSIX_MAX_INPUT</CODE> and <CODE>MAX_INPUT</CODE> parameters; see section <A HREF="library_27.html#SEC463" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_27.html#SEC463">Limits on File System Capacity</A>.  If input flow control is enabled by setting the<CODE>IXOFF</CODE> input mode bit (see section <A HREF="library_16.html#SEC276" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC276">Input Modes</A>), the terminal drivertransmits STOP and START characters to the terminal when necessary toprevent the queue from overflowing.  Otherwise, input may be lost if itcomes in too fast from the terminal.  (This is unlikely if you aretyping the input by hand!)<A NAME="IDX1087"></A><P>The <DFN>terminal output queue</DFN> is like the input queue, but for output;it contains characters that have been written by processes, but not yettransmitted to the terminal.  If output flow control is enabled bysetting the <CODE>IXON</CODE> input mode bit (see section <A HREF="library_16.html#SEC276" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC276">Input Modes</A>), theterminal driver obeys STOP and STOP characters sent by the terminal tostop and restart transmission of output.<P><DFN>Clearing</DFN> the terminal input queue means discarding any charactersthat have been received but not yet read.  Similarly, clearing theterminal output queue means discarding any characters that have beenwritten but not yet transmitted.<P><H2><A NAME="SEC271" HREF="library_toc.html#SEC271" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC271">Two Styles of Input: Canonical or Not</A></H2><P>POSIX systems support two basic modes of input: canonical andnoncanonical.<A NAME="IDX1088"></A><P>In <DFN>canonical input processing</DFN> mode, terminal input is processed inlines terminated by newline (<CODE>'\n'</CODE>), EOF, or EOL characters.  Noinput can be read until an entire line has been typed by the user, andthe <CODE>read</CODE> function (see section <A HREF="library_12.html#SEC173" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_12.html#SEC173">Input and Output Primitives</A>) returns at most asingle line of input, no matter how many bytes are requested.<P>In canonical input mode, the operating system provides input editingfacilities: the ERASE and KILL characters are interpreted specially toperform editing operations within the current line of text.See section <A HREF="library_16.html#SEC282" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC282">Characters for Input Editing</A>.<P>The constants <CODE>_POSIX_MAX_CANON</CODE> and <CODE>MAX_CANON</CODE> parameterizethe maximum number of bytes which may appear in a single line ofcanonical input.  See section <A HREF="library_27.html#SEC463" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_27.html#SEC463">Limits on File System Capacity</A>.<A NAME="IDX1089"></A><P>In <DFN>noncanonical input processing</DFN> mode, characters are not groupedinto lines, and ERASE and KILL processing is not performed.  Thegranularity with which bytes are read in noncanonical input mode iscontrolled by the MIN and TIME settings.  See section <A HREF="library_16.html#SEC287" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC287">Noncanonical Input</A>.<P>Most programs use canonical input mode, because this gives the user away to edit input line by line.  The usual reason to use noncanonicalmode is when the program accepts single-character commands or providesits own editing facilities.<P>The choice of canonical or noncanonical input is controlled by the<CODE>ICANON</CODE> flag in the <CODE>c_lflag</CODE> member of <CODE>struct termios</CODE>.See section <A HREF="library_16.html#SEC279" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC279">Local Modes</A>.<P><H2><A NAME="SEC272" HREF="library_toc.html#SEC272" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC272">Terminal Modes</A></H2><A NAME="IDX1090"></A><P>This section describes the various terminal attributes that control howinput and output are done.  The functions, data structures, and symbolicconstants are all declared in the header file <TT>`termios.h'</TT>.<P><A NAME="IDX1091"></A><H3><A NAME="SEC273" HREF="library_toc.html#SEC273" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC273">Terminal Mode Data Types</A></H3><P>The entire collection of attributes of a terminal is stored in astructure of type <CODE>struct termios</CODE>.  This structure is usedwith the functions <CODE>tcgetattr</CODE> and <CODE>tcsetattr</CODE> to readand set the attributes.<P><A NAME="IDX1092"></A><U>Data Type:</U> <B>struct termios</B><P>Structure that records all the I/O attributes of a terminal.  Thestructure includes at least the following members:<P><DL COMPACT><DT><CODE>tcflag_t c_iflag</CODE><DD>A bit mask specifying input modes; see section <A HREF="library_16.html#SEC276" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC276">Input Modes</A>.<P><DT><CODE>tcflag_t c_oflag</CODE><DD>A bit mask specifying output modes; see section <A HREF="library_16.html#SEC277" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC277">Output Modes</A>.<P><DT><CODE>tcflag_t c_cflag</CODE><DD>A bit mask specifying control modes; 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>.<P><DT><CODE>tcflag_t c_lflag</CODE><DD>A bit mask specifying flags for local modes; see section <A HREF="library_16.html#SEC279" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC279">Local Modes</A>.<P><DT><CODE>cc_t c_cc[NCCS]</CODE><DD>An array specifying which characters are associated with variouscontrol functions; see section <A HREF="library_16.html#SEC281" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC281">Special Characters</A>.</DL><P>The <CODE>struct termios</CODE> structure also contains members whichencode input and output transmission speeds, but the representation isnot specified.  See section <A HREF="library_16.html#SEC280" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC280">Line Speed</A>, for how to examine and store thespeed values.<P>The following sections describe the details of the members of the<CODE>struct termios</CODE> structure.<P><A NAME="IDX1093"></A><U>Data Type:</U> <B>tcflag_t</B><P>This is an unsigned integer type used to represent the variousbit masks for terminal flags.<P><A NAME="IDX1094"></A><U>Data Type:</U> <B>cc_t</B><P>This is an unsigned integer type used to represent characters associatedwith various terminal control functions.<P><A NAME="IDX1095"></A><U>Macro:</U> int <B>NCCS</B><P>The value of this macro is the number of elements in the <CODE>c_cc</CODE>array.<P><A NAME="IDX1096"></A><H3><A NAME="SEC274" HREF="library_toc.html#SEC274" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC274">Terminal Mode Functions</A></H3><P><A NAME="IDX1097"></A><U>Function:</U> int <B>tcgetattr</B> <I>(int <VAR>filedes</VAR>, struct termios *<VAR>termios_p</VAR>)</I><P>This function is used to examine the attributes of the terminaldevice with file descriptor <VAR>filedes</VAR>.  The attributes are returnedin the structure that <VAR>termios_p</VAR> points to.<P>If successful, <CODE>tcgetattr</CODE> returns <CODE>0</CODE>.  A return value of <CODE>-1</CODE>indicates an error.  The following <CODE>errno</CODE> error conditions aredefined for this function:<P><DL COMPACT><DT><CODE>EBADF</CODE><DD>The <VAR>filedes</VAR> argument is not a valid file descriptor.<P><DT><CODE>ENOTTY</CODE><DD>The <VAR>filedes</VAR> is not associated with a terminal.</DL><P><A NAME="IDX1098"></A><U>Function:</U> int <B>tcsetattr</B> <I>(int <VAR>filedes</VAR>, int <VAR>when</VAR>, const struct termios *<VAR>termios_p</VAR>)</I><P>This function sets the attributes of the terminal device with filedescriptor <VAR>filedes</VAR>.  The new attributes are taken from thestructure that <VAR>termios_p</VAR> points to.<P>The <VAR>when</VAR> argument specifies how to deal with input and outputalready queued.  It can be one of the following values:<P><DL COMPACT><A NAME="IDX1099"></A><DT><CODE>TCSANOW</CODE><DD>Make the change immediately.<P><A NAME="IDX1100"></A><DT><CODE>TCSADRAIN</CODE><DD>Make the change after waiting until all queued output has been written.You should usually use this option when changing parameters that affectoutput.<P><A NAME="IDX1101"></A><DT><CODE>TCSAFLUSH</CODE><DD>This is like <CODE>TCSADRAIN</CODE>, but also discards any queued input.<P><A NAME="IDX1102"></A><DT><CODE>TCSASOFT</CODE><DD>This is a flag bit that you can add to any of the above alternatives.Its meaning is to inhibit alteration of the state of the terminalhardware.  It is a BSD extension; it has no effect on non-BSD systems.</DL><P>If this function is called from a background process on its controllingterminal, normally all processes in the process group are sent a<CODE>SIGTTOU</CODE> signal, in the same way as if the process were trying towrite to the terminal.  The exception is if the calling process itselfis ignoring or blocking <CODE>SIGTTOU</CODE> signals, in which case theoperation is performed and no signal is sent.  See section <A HREF="library_24.html#SEC411" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_24.html#SEC411">Job Control</A>.<P>If successful, <CODE>tcsetattr</CODE> returns <CODE>0</CODE>.  A return value of<CODE>-1</CODE> indicates an error.  The following <CODE>errno</CODE> errorconditions are defined for this function:<P><DL COMPACT><DT><CODE>EBADF</CODE><DD>The <VAR>filedes</VAR> argument is not a valid file descriptor.<P><DT><CODE>ENOTTY</CODE><DD>The <VAR>filedes</VAR> is not associated with a terminal.<P><DT><CODE>EINVAL</CODE><DD>Either the value of the <CODE>when</CODE> argument is not valid, or there issomething wrong with the data in the <VAR>termios_p</VAR> argument.</DL><P>Although <CODE>tcgetattr</CODE> and <CODE>tcsetattr</CODE> specify the terminaldevice with a file descriptor, the attributes are those of the terminaldevice itself and not of the file descriptor.  This means that theeffects of changing terminal attributes are persistent; if anotherprocess opens the terminal file later on, it will see the changedattributes even though it doesn't have anything to do with the open filedescriptor you originally specified in changing the attributes.<P>Similarly, if a single process has multiple or duplicated filedescriptors for the same terminal device, changing the terminalattributes affects input and output to all of these file

⌨️ 快捷键说明

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