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

📄 terminal.texi

📁 一个C源代码分析器
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@node Low-Level Terminal Interface@chapter Low-Level Terminal InterfaceThis 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.Most of the functions in this chapter operate on file descriptors.@xref{Low-Level I/O}, for more information about what a filedescriptor is and how to open a file descriptor for a terminal device.@menu* Is It a Terminal::            How to determine if a file is a terminal			         device, and what its name is.* I/O Queues::                  About flow control and typeahead.* Canonical or Not::            Two basic styles of input processing.* Terminal Modes::              How to examine and modify flags controlling			         details of terminal I/O: echoing,                                 signals, editing. * Line Control::                Sending break sequences, clearing                                 terminal buffers @dots{} * Noncanon Example::            How to read single characters without echo.@end menu@node Is It a Terminal@section Identifying Terminals@cindex terminal identification@cindex identifying terminalsThe 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}function.@pindex unistd.hPrototypes for both @code{isatty} and @code{ttyname} are declared inthe header file @file{unistd.h}.@comment unistd.h@comment POSIX.1@deftypefun int isatty (int @var{filedes})This function returns @code{1} if @var{filedes} is a file descriptorassociated with an open terminal device, and @code{0} otherwise.@end deftypefunIf a file descriptor is associated with a terminal, you can get itsassociated file name using the @code{ttyname} function.  See also the@code{ctermid} function, described in @ref{Identifying the Terminal}.@comment unistd.h@comment POSIX.1@deftypefun {char *} ttyname (int @var{filedes})If the file descriptor @var{filedes} is associated with a terminaldevice, the @code{ttyname} 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.@end deftypefun@node I/O Queues@section I/O QueuesMany of the remaining functions in this section refer to the input andoutput queues of a terminal device.  These queues implement a form ofbuffering @emph{within the kernel} independent of the bufferingimplemented by I/O streams (@pxref{I/O on Streams}).@cindex terminal input queue@cindex typeahead bufferThe @dfn{terminal input queue} is also sometimes referred to as its@dfn{typeahead buffer}.  It holds the characters that have been receivedfrom the terminal but not yet read by any process.The size of the terminal's input queue is described by the@code{_POSIX_MAX_INPUT} and @code{MAX_INPUT} parameters; see @ref{Limitsfor Files}.  You are guaranteed a queue size of at least@code{MAX_INPUT}, but the queue might be larger, and might evendynamically change size.  If input flow control is enabled by settingthe @code{IXOFF} input mode bit (@pxref{Input Modes}), the terminaldriver transmits STOP and START characters to the terminal whennecessary to prevent the queue from overflowing.  Otherwise, input maybe lost if it comes in too fast from the terminal.  In canonical mode,all input stays in the queue until a newline character is received, sothe terminal input queue can fill up when you type a very long line.@xref{Canonical or Not}.@cindex terminal output queueThe @dfn{terminal output queue} 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} input mode bit (@pxref{Input Modes}), theterminal driver obeys STOP and STOP characters sent by the terminal tostop and restart transmission of output.@dfn{Clearing} 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.@node Canonical or Not@section Two Styles of Input: Canonical or NotPOSIX systems support two basic modes of input: canonical andnoncanonical.@cindex canonical input processingIn @dfn{canonical input processing} mode, terminal input is processed inlines terminated by newline (@code{'\n'}), EOF, or EOL characters.  Noinput can be read until an entire line has been typed by the user, andthe @code{read} function (@pxref{I/O Primitives}) returns at most asingle line of input, no matter how many bytes are requested.In canonical input mode, the operating system provides input editingfacilities: some characters are interpreted specially to perform editingoperations within the current line of text, such as ERASE and KILL.@xref{Editing Characters}.The constants @code{_POSIX_MAX_CANON} and @code{MAX_CANON} parameterizethe maximum number of bytes which may appear in a single line ofcanonical input.  @xref{Limits for Files}.  You are guaranteed a maximumline length of at least @code{MAX_CANON} bytes, but the maximum might belarger, and might even dynamically change size.@cindex noncanonical input processingIn @dfn{noncanonical input processing} 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.  @xref{Noncanonical Input}.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.The choice of canonical or noncanonical input is controlled by the@code{ICANON} flag in the @code{c_lflag} member of @code{struct termios}.@xref{Local Modes}.@node Terminal Modes@section Terminal Modes@pindex termios.hThis 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 @file{termios.h}.@c !!! should mention terminal attributes are distinct from file attributes@menu* Mode Data Types::             The data type @code{struct termios} and                                 related types. * Mode Functions::              Functions to read and set the terminal                                 attributes. * Setting Modes::               The right way to set terminal attributes                                 reliably.* Input Modes::                 Flags controlling low-level input handling.* Output Modes::                Flags controlling low-level output handling.* Control Modes::               Flags controlling serial port behavior.* Local Modes::                 Flags controlling high-level input handling.* Line Speed::                  How to read and set the terminal line speed.* Special Characters::          Characters that have special effects,			         and how to change them.* Noncanonical Input::          Controlling how long to wait for input.@end menu@node Mode Data Types@subsection Terminal Mode Data Types@cindex terminal mode data typesThe entire collection of attributes of a terminal is stored in astructure of type @code{struct termios}.  This structure is usedwith the functions @code{tcgetattr} and @code{tcsetattr} to readand set the attributes.@comment termios.h@comment POSIX.1@deftp {Data Type} {struct termios}Structure that records all the I/O attributes of a terminal.  Thestructure includes at least the following members:@table @code@item tcflag_t c_iflagA bit mask specifying flags for input modes; see @ref{Input Modes}.@item tcflag_t c_oflagA bit mask specifying flags for output modes; see @ref{Output Modes}.@item tcflag_t c_cflagA bit mask specifying flags for control modes; see @ref{Control Modes}.@item tcflag_t c_lflagA bit mask specifying flags for local modes; see @ref{Local Modes}.@item cc_t c_cc[NCCS]An array specifying which characters are associated with variouscontrol functions; see @ref{Special Characters}.@end tableThe @code{struct termios} structure also contains members whichencode input and output transmission speeds, but the representation isnot specified.  @xref{Line Speed}, for how to examine and store thespeed values.@end deftpThe following sections describe the details of the members of the@code{struct termios} structure.@comment termios.h@comment POSIX.1@deftp {Data Type} tcflag_tThis is an unsigned integer type used to represent the variousbit masks for terminal flags.@end deftp@comment termios.h@comment POSIX.1@deftp {Data Type} cc_tThis is an unsigned integer type used to represent characters associatedwith various terminal control functions.@end deftp@comment termios.h@comment POSIX.1@deftypevr Macro int NCCSThe value of this macro is the number of elements in the @code{c_cc}array.@end deftypevr@node Mode Functions@subsection Terminal Mode Functions@cindex terminal mode functions@comment termios.h@comment POSIX.1@deftypefun int tcgetattr (int @var{filedes}, struct termios *@var{termios-p})This function is used to examine the attributes of the terminaldevice with file descriptor @var{filedes}.  The attributes are returnedin the structure that @var{termios-p} points to.If successful, @code{tcgetattr} returns @code{0}.  A return value of @code{-1}indicates an error.  The following @code{errno} error conditions aredefined for this function:@table @code@item EBADFThe @var{filedes} argument is not a valid file descriptor.@item ENOTTYThe @var{filedes} is not associated with a terminal.@end table@end deftypefun@comment termios.h@comment POSIX.1@deftypefun int tcsetattr (int @var{filedes}, int @var{when}, const struct termios *@var{termios-p})This function sets the attributes of the terminal device with filedescriptor @var{filedes}.  The new attributes are taken from thestructure that @var{termios-p} points to.The @var{when} argument specifies how to deal with input and outputalready queued.  It can be one of the following values:@table @code@comment termios.h@comment POSIX.1@item TCSANOW@vindex TCSANOWMake the change immediately.@comment termios.h@comment POSIX.1@item TCSADRAIN@vindex TCSADRAINMake the change after waiting until all queued output has been written.You should usually use this option when changing parameters that affectoutput.@comment termios.h@comment POSIX.1@item TCSAFLUSH@vindex TCSAFLUSHThis is like @code{TCSADRAIN}, but also discards any queued input.@comment termios.h@comment BSD@item TCSASOFT@vindex TCSASOFTThis 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 is only supported on BSD systemsand the GNU system.Using @code{TCSASOFT} is exactly the same as setting the @code{CIGNORE}bit in the @code{c_cflag} member of the structure @var{termios-p} pointsto.  @xref{Control Modes}, for a description of @code{CIGNORE}.@end tableIf this function is called from a background process on its controllingterminal, normally all processes in the process group are sent a@code{SIGTTOU} 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} signals, in which case theoperation is performed and no signal is sent.  @xref{Job Control}.If successful, @code{tcsetattr} returns @code{0}.  A return value of@code{-1} indicates an error.  The following @code{errno} errorconditions are defined for this function:@table @code@item EBADFThe @var{filedes} argument is not a valid file descriptor.@item ENOTTYThe @var{filedes} is not associated with a terminal.@item EINVALEither the value of the @code{when} argument is not valid, or there issomething wrong with the data in the @var{termios-p} argument.@end table@end deftypefunAlthough @code{tcgetattr} and @code{tcsetattr} 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.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 filedescriptors.  This means, for example, that you can't open one filedescriptor or stream to read from a terminal in the normalline-buffered, echoed mode; and simultaneously have another filedescriptor for the same terminal that you use to read from it insingle-character, non-echoed mode.  Instead, you have to explicitlyswitch the terminal back and forth between the two modes.@node Setting Modes@subsection Setting Terminal Modes ProperlyWhen you set terminal modes, you should call @code{tcgetattr} first toget the current modes of the particular terminal device, modify onlythose modes that you are really interested in, and store the result with@code{tcsetattr}.It's a bad idea to simply initialize a @code{struct termios} structureto a chosen set of attributes and pass it directly to @code{tcsetattr}.Your program may be run years from now, on systems that support membersnot documented in this manual.  The way to avoid setting these membersto unreasonable values is to avoid changing them.What's more, different terminal devices may require different modesettings in order to function properly.  So you should avoid blindlycopying attributes from one terminal device to another.

⌨️ 快捷键说明

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