📄 termios.4
字号:
.\" Copyright (c) 1991, 1992, 1993.\" The Regents of the University of California. All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\" must display the following acknowledgement:.\" This product includes software developed by the University of.\" California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)termios.4 8.4 (Berkeley) 4/19/94.\".Dd April 19, 1994.Dt TERMIOS 4.Os BSD 4.Sh NAME.Nm termios.Nd general terminal line discipline.Sh SYNOPSIS.Fd #include <termios.h>.Sh DESCRIPTIONThis describes a general terminal line discipline that issupported on tty asynchronous communication ports..Ss Opening a Terminal Device FileWhen a terminal file is opened, it normally causes the process to waituntil a connection is established. For most hardware, the presenceof a connection is indicated by the assertion of the hardware.Dv CARRIER line.If the termios structure associated with the terminal file has the.Dv CLOCALflag set in the cflag, or if the.Dv O_NONBLOCKflag is setin the.Xr open 2call, then the open will succeed even withouta connection being present.In practice, applicationsseldom open these files; they are opened by special programs, suchas.Xr getty 2or.Xr rlogind 2 ,and becomean application's standard input, output, and error files..Ss Job Control in a NutshellEvery process is associated with a particular process group and session.The grouping is hierarchical: every member of a particular process group is amember of the same session. This structuring is used in managing groupsof related processes for purposes of.\" .Gw "job control" ;.Em "job control" ;that is, theability from the keyboard (or from program control) to simultaneouslystop or restarta complex command (a command composed of one or more relatedprocesses). The grouping into process groups allows deliveringof signals that stop or start the group as a whole, along witharbitrating which process group has access to the single controllingterminal. The grouping at a higher layer into sessions is to restrictthe job control related signals and system calls to within processesresulting from a particular instance of a "login". Typically, a sessionis created when a user logs in, and the login terminal is setupto be the controlling terminal; all processes spawned from thatlogin shell are in the same session, and inherit the controllingterminal.A job control shelloperating interactively (that is, reading commands from a terminal)normally groups related processes together by placing them into thesame process group. A set of processes in the same process groupis collectively referred to as a "job". When the foreground processgroup of the terminal is the same as the process group of a particularjob, that job is said to be in the "foreground". When the processgroup of the terminal is different than the process group ofa job (but is still the controlling terminal), that job is saidto be in the "background". Normally theshell reads a command and starts the job that implements thatcommand. If the command is to be started in the foreground (typical), itsets the process group of the terminal to the process groupof the started job, waits for the job to complete, and thensets the process group of the terminal back to its own processgroup (it puts itself into the foreground). If the job is tobe started in the background (as denoted by the shell operator "&"),it never changes the process group of the terminal and doesn'twait for the job to complete (that is, it immediately attempts to read the nextcommand). If the job is started in the foreground, the user maytype a key (usually.Ql \&^Z )which generates the terminal stop signal.Pq Dv SIGTSTPand has the affect of stopping the entire job.The shell will notice that the job stopped, and will resume running afterplacing itself in the foreground.The shell also has commands for placing stopped jobs in the background,and for placing stopped or background jobs into the foreground..Ss Orphaned Process GroupsAn orphaned process group is a process group that has no processwhose parent is in a different process group, yet is in the samesession. Conceptually it means a process group that doesn't havea parent that could do anything if it were to be stopped. For example,the initial login shell is typically in an orphaned process group.Orphaned process groups are immune to keyboard generated stopsignals and job control signals resulting from reads or writes to thecontrolling terminal..Ss The Controlling TerminalA terminal may belong to a process as its controlling terminal. Eachprocess of a session that has a controlling terminal has the samecontrolling terminal. A terminal may be the controlling terminal for atmost one session. The controlling terminal for a session is allocated bythe session leader by issuing the.Dv TIOCSCTTYioctl. A controlling terminalis never acquired by merely opening a terminal device file.When a controlling terminal becomesassociated with a session, its foreground process group is set tothe process group of the session leader..PpThe controlling terminal is inherited by a child process during a.Xr fork 2function call. A process relinquishes its controlling terminal when itcreates a new session with the.Xd setsid 2function; other processesremaining in the old session that had this terminal as their controllingterminal continue to have it.A process does not relinquish itscontrolling terminal simply by closing all of its file descriptorsassociated with the controlling terminal if other processes continue tohave it open..PpWhen a controlling process terminates, the controlling terminal isdisassociated from the current session, allowing it to be acquired by anew session leader. Subsequent access to the terminal by other processesin the earlier session will be denied, with attempts to access theterminal treated as if modem disconnect had been sensed..Ss Terminal Access ControlIf a process is in the foreground process group of its controllingterminal, read operations are allowed.Any attempts by a processin a background process group to read from its controlling terminalcauses a.Dv SIGTTINsignal to be sent tothe process's groupunless one of thefollowing special cases apply: If the reading process is ignoring orblocking the.Dv SIGTTIN signal, or if the process group of the readingprocess is orphaned, the.Xr read 2returns -1 with.Va errno set to.Er Dv EIOand nosignal is sent. The default action of the.Dv SIGTTINsignal is to stop theprocess to which it is sent..PpIf a process is in the foreground process group of its controllingterminal, write operations are allowed.Attempts by a process in a background process group to write to itscontrolling terminal will cause the process group to be sent a.Dv SIGTTOUsignal unless one of the following special cases apply: If.Dv TOSTOPis notset, or if.Dv TOSTOPis set and the process is ignoring or blocking the.Dv SIGTTOUsignal, the process is allowed to write to the terminal and the.Dv SIGTTOUsignal is not sent. If.Dv TOSTOPis set, and the process group ofthe writing process is orphaned, and the writing process is not ignoringor blocking.Dv SIGTTOU ,the.Xr writereturns -1 witherrno set to.Er Dv EIOand no signal is sent..PpCertain calls that set terminal parameters are treated in the samefashion as write, except that.Dv TOSTOPis ignored; that is, the effect isidentical to that of terminal writes when.Dv TOSTOPis set..Ss Input Processing and Reading DataA terminal device associated with a terminal device file may operate infull-duplex mode, so that data may arrive even while output is occurring.Each terminal device file has associated with it an input queue, intowhich incoming data is stored by the system before being read by aprocess. The system imposes a limit,.Pf \&{ Dv MAX_INPUT Ns \&} ,on the number ofbytes that may be stored in the input queue. The behavior of the systemwhen this limit is exceeded depends on the setting of the.Dv IMAXBELflag in the termios.Fa c_iflag .If this flag is set, the terminalis sent an.Tn ASCII.Dv BELcharacter each time a character is receivedwhile the input queue is full. Otherwise, the input queue is flushedupon receiving the character..PpTwo general kinds of input processing are available, determined bywhether the terminal device file is in canonical mode or noncanonicalmode. Additionally,input characters are processed according to the.Fa c_iflagand.Fa c_lflagfields. Such processing can include echoing, whichin general means transmitting input characters immediately back to theterminal when they are received from the terminal. This is useful forterminals that can operate in full-duplex mode..PpThe manner in which data is provided to a process reading from a terminaldevice file is dependent on whether the terminal device file is incanonical or noncanonical mode..PpAnother dependency is whether the.Dv O_NONBLOCKflag is set by.Xr open()or.Xr fcntl() .If the.Dv O_NONBLOCKflag is clear, then the read request isblocked until data is available or a signal has been received. If the.Dv O_NONBLOCKflag is set, then the read request is completed, withoutblocking, in one of three ways:.Bl -enum -offset indent.ItIf there is enough data available to satisfy the entire request,and the read completes successfully the number ofbytes read is returned..ItIf there is not enough data available to satisfy the entirerequest, and the read completes successfully, having read asmuch data as possible, the number of bytes read is returned..ItIf there is no data available, the read returns -1, witherrno set to.Er EAGAIN ..El.PpWhen data is available depends on whether the input processing mode iscanonical or noncanonical..Ss Canonical Mode Input ProcessingIn canonical mode input processing, terminal input is processed in unitsof lines. A line is delimited by a newline.Ql \&\encharacter, an end-of-file.Pq Dv EOFcharacter, or an end-of-line.Pq Dv EOLcharacter. See the.Sx "Special Characters"section formore information on.Dv EOFand.Dv EOL .This means that a read request willnot return until an entire line has been typed, or a signal has beenreceived. Also, no matter how many bytes are requested in the read call,at most one line is returned. It is not, however, necessary toread a whole line at once; any number of bytes, even one, may berequested in a read without losing information..Pp.Pf \&{ Dv MAX_CANON Ns \&}is a limit on thenumber of bytes in a line.The behavior of the system when this limit isexceeded is the same as when the input queue limit.Pf \&{ Dv MAX_INPUT Ns \&} ,is exceeded..PpErase and kill processing occur when either of two special characters,the.Dv ERASEand.Dv KILLcharacters (see the.Sx "Special Characters section" ) ,is received.This processing affects data in the input queue that has not yet beendelimited by a newline.Dv NL,.Dv EOF ,or.Dv EOLcharacter. This un-delimiteddata makes up the current line. The.Dv ERASEcharacter deletes the lastcharacter in the current line, if there is any. The.Dv KILLcharacterdeletes all data in the current line, if there is any. The.Dv ERASEand.Dv KILLcharacters have no effect if there is no data in the current line.The.Dv ERASEand.Dv KILLcharacters themselves are not placed in the inputqueue..Ss Noncanonical Mode Input ProcessingIn noncanonical mode input processing, input bytes are not assembled intolines, and erase and kill processing does not occur. The values of the.Dv MINand.Dv TIMEmembers of the.Fa c_ccarray are used to determine how toprocess the bytes received..Pp.Dv MINrepresents the minimum number of bytes that should be received whenthe.Xr readfunction successfully returns..Dv TIMEis a timer of 0.1 secondgranularity that is used to time out bursty and short term datatransmissions. If.Dv MINis greater than.Dv \&{ Dv MAX_INPUT Ns \&} ,the response to therequest is undefined. The four possible values for.Dv MINand.Dv TIMEandtheir interactions are described below..Ss "Case A: MIN > 0, TIME > 0"In this case.Dv TIMEserves as an inter-byte timer and is activated afterthe first byte is received. Since it is an inter-byte timer, it is resetafter a byte is received. The interaction between.Dv MINand.Dv TIMEis asfollows: as soon as one byte is received, the inter-byte timer isstarted. If.Dv MINbytes are received before the inter-byte timer expires(remember that the timer is reset upon receipt of each byte), the read issatisfied. If the timer expires before.Dv MINbytes are received, thecharacters received to that point are returned to the user. Note that if.Dv TIMEexpires at least one byte is returned because the timer wouldnot have been enabled unless a byte was received. In this case.Pf \&( Dv MIN> 0,.Dv TIME> 0) the read blocks until the.Dv MINand.Dv TIMEmechanisms areactivated by the receipt of the first byte, or a signal is received. Ifdata is in the buffer at the time of the read(), the result is asif data had been received immediately after the read()..Ss "Case B: MIN > 0, TIME = 0"In this case, since the value of.Dv TIMEis zero, the timer plays no roleand only.Dv MINis significant. A pending read is not satisfied until.Dv MINbytes are received (i.e., the pending read blocks until.Dv MINbytesare received), or a signal is received. A program that uses this case toread record-based terminal.Dv I/Omay block indefinitely in the readoperation..Ss "Case C: MIN = 0, TIME > 0"In this case, since.Dv MIN= 0,.Dv TIMEno longer represents an inter-bytetimer. It now serves as a read timer that is activated as soon as theread function is processed. A read is satisfied as soon as a singlebyte is received or the read timer expires. Note that in this case ifthe timer expires, no bytes are returned. If the timer does notexpire, the only way the read can be satisfied is if a byte is received.In this case the read will not block indefinitely waiting for a byte; ifno byte is received within.Dv TIME Ns *0.1seconds after the read is initiated,the read returns a value of zero, having read no data. If data isin the buffer at the time of the read, the timer is started as ifdata had been received immediately after the read..Ss Case D: MIN = 0, TIME = 0The minimum of either the number of bytes requested or the number ofbytes currently available is returned without waiting for morebytes to be input. If no characters are available, read returns avalue of zero, having read no data..Ss Writing Data and Output ProcessingWhen a process writes one or more bytes to a terminal device file, theyare processed according to the.Fa c_oflagfield (see the.Sx "Output Modessection). Theimplementation may provide a buffering mechanism; as such, when a call towrite() completes, all of the bytes written have been scheduled fortransmission to the device, but the transmission will not necessarilyhave been completed..\" See also .Sx "6.4.2" for the effects of.\" .Dv O_NONBLOCK.\" on write..Ss Special CharactersCertain characters have special functions on input or output or both.These functions are summarized as follows:.Bl -tag -width indent.It Dv INTRSpecial character on input and is recognized if the.Dv ISIGflag (see the.Sx "Local Modes"section) is enabled. Generates a.Dv SIGINTsignal which is sent to all processes in the foreground
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -