📄 ch41_02.htm
字号:
efficiency and a "complete" interface - one that allowed doingeverything a serial port could do.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45460"></A><ACLASS="indexterm"NAME="AUTOID-45462"></A><ACLASS="indexterm"NAME="AUTOID-45464"></A>Berkeley UNIX thus acquired <EMCLASS="emphasis">three</EM> terminal modes. It retained theoriginal <EMCLASS="emphasis">raw</EM> and <EMCLASS="emphasis">cooked</EM> modes, and added a new one called<EMCLASS="emphasis">cbreak.</EM>[5]In cbreak mode, some input processing was done, butmost characters were sent on to the application as they arrived. Sincethe kernel was not collecting lines, the erase and line-kill characterswere unneeded; these were sent on unchanged. Most of the processcontrol characters - interrupt, quit, and a new <EMCLASS="emphasis">stop</EM> or <EMCLASS="emphasis">suspend</EM>code - were still interpreted. To allow users to type these codes, anew "literal next" or "quote" character was introduced. BerkeleyUNIX also added more output processing, including a special translationoption for certain Hazeltine Corporation displays and features such asproper tab handling, output flush, and word erase.</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[5] <ACLASS="indexterm"NAME="AUTOID-45472"></A>This <EMCLASS="emphasis">cbreak</EM> mode has sometimes been referred to as "half-baked."</P></BLOCKQUOTE><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45479"></A><ACLASS="indexterm"NAME="AUTOID-45481"></A><ACLASS="indexterm"NAME="AUTOID-45483"></A><ACLASS="indexterm"NAME="AUTOID-45485"></A><ACLASS="indexterm"NAME="AUTOID-45487"></A><ACLASS="indexterm"NAME="AUTOID-45489"></A><ACLASS="indexterm"NAME="AUTOID-45491"></A>The System V base, on the other hand, dropped the idea of raw modeentirely. Instead, this system provided an individual control for eachoption. The <EMCLASS="emphasis">icanon</EM> option, for instance, controlled whether inputlines were to be collected or "canonicalized." The <EMCLASS="emphasis">isig</EM> optioncontrolled signals: when off, the <EMCLASS="emphasis">interrupt</EM> (DEL, or in modern systems,CTRL-c) and <EMCLASS="emphasis">quit</EM> characters were just ordinary characters. The<EMCLASS="emphasis">inpchk</EM> option controlled input parity checking, and so forth.Similarly, output processing had individual flags: <EMCLASS="emphasis">ocrnl</EM> forcarriage return-newline control, <EMCLASS="emphasis">opost</EM> for output processing ingeneral. By turning everything off individually, an application couldget the same effect as the old system's raw mode. The kernel alsoallowed control over the number of data bits in each serial frame, thenumber of stop bits, and so forth.[6]Thus, while Berkeley UNIX had niceline editing, it was incapable of attaching to five-bit Baudot systems.System V lacked the user interface features, but could talk to almostanything.</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[6] Different systems use anything from five to nine bits in aserial-port "byte." Most people, however, do not need to careabout all this. Most systems just use eight bits, either as sevendata bits and a parity check, or as eight data bits without parity.Thus, most people can ignore these options, and stick with either"seven bits, even parity" or "eight bits, no parity."</P></BLOCKQUOTE><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45503"></A><ACLASS="indexterm"NAME="AUTOID-45505"></A><ACLASS="indexterm"NAME="AUTOID-45507"></A>Since then, the world has become simpler in one way - those oldprinting teletypes are gone nearly everywhere, for instance - but more complicated in another. These days, many computers usebitmapped displays rather than individual remote terminals. UNIXsystems support networking, and use windowing systems such as the<SPANCLASS="link">X Window System (<ACLASS="linkend"HREF="ch01_31.htm"TITLE="The X Window System ">1.31</A>)</SPAN>.These in turn bring a myriad of options, windowmanagers, look-and-feel, and so on. But they all have one thing incommon: to run old applications, each window or network login mustprovide a <EMCLASS="emphasis">virtual terminal</EM> interface. UNIX systems generally do thiswith <EMCLASS="emphasis">pseudo teletypes</EM> or<SPANCLASS="link"><EMCLASS="emphasis">pty</EM>s (<ACLASS="linkend"HREF="ch41_08.htm"TITLE="ptys and Window Systems ">41.8</A>)</SPAN>.Each <EMCLASS="emphasis">pty</EM> exists to emulate adisplay terminal, which in turn is mainly pretending to be a teletypeprinter. (Sometimes one has to wonder where the progress lies.)</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45516"></A>A POSIX standardization committee has settled on a standardinterface, both at the UNIX kernel level and for the <EMCLASS="emphasis">stty</EM>command. Most UNIX systems, including Berkeley UNIX,have moved to embrace this standard. While it leaves a few looseends - mainly for reasons involving backwards compatibility for SystemV-it allows systems both the flexibility of the System V interfaceand the features of the Berkeley approach. This means that whilewindows and networks may be emulating ancient teletypes, at leastthey are all doing it in the same way.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1140-SECT-1.2">41.2.2 Handling Most Characters </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45522"></A><ACLASS="indexterm"NAME="AUTOID-45525"></A>With all that as background, let's take a look at what happens to aninput character, from the time you type it until anapplication can react. The details may vary - often wildly - dependingon your system and whether you are using a window, a terminal, anetwork, or some combination of all three, but the overall idea is thesame. For simplicity, we will assume you have an ordinary terminal.We will call this "the terminal" and the kernel's idea of it "thetty."</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45529"></A><ACLASS="indexterm"NAME="AUTOID-45531"></A><ACLASS="indexterm"NAME="AUTOID-45533"></A><ACLASS="indexterm"NAME="AUTOID-45535"></A>Suppose you type the letter <EMCLASS="emphasis">x</EM>. The terminal sends the<SPANCLASS="link">ASCII code (<ACLASS="linkend"HREF="ch51_03.htm"TITLE="ASCII Characters: Listing and Getting Values ">51.3</A>)</SPAN>for a lowercase X (120) to the UNIX kernel's tty. The kernel thenlooks at the <EMCLASS="emphasis">tty</EM> state. Assume for the sake of discussion that the ttyis in cooked or icanon mode, and that none of the specialcharacters has been set to <EMCLASS="emphasis">x</EM>. Then the letter <CODECLASS="literal">x</CODE> is placed inan input buffer and echoed back to the terminal, causing an <CODECLASS="literal">x</CODE> to bedisplayed on your screen. But if you really wanted to type a <EMCLASS="emphasis">c</EM>,you would now type your<SPANCLASS="link">erase character (<ACLASS="linkend"HREF="ch05_09.htm"TITLE="Setting Your Erase, Kill, and Interrupt Characters ">5.9</A>)</SPAN>(usually CTRL-h, BACKSPACE,or DELETE, which may or may not all be the same or all different,depending on your particular terminal or keyboard). The code for thischaracter will also be sent to the tty; this time it will match yourerase character. The kernel will then remove the last character fromthe input buffer. Since this - a lowercase X - is an ordinary printingcharacter, the kernel will send a single backspace, or the sequence"backspace space backspace," to the terminal. This will generally backthe cursor up over the character and then erase it from the screen.(On a POSIX system, you get the latter by setting <EMCLASS="emphasis">echoe</EM> mode.) Finally, when you type RETURN orENTER, your terminal sends an ASCII code 13. Since <EMCLASS="emphasis">icrnl</EM> is set, thekernel changes this to 10 (newline), which it then echoes to theterminal. Since <EMCLASS="emphasis">onlcr</EM> is set, this sends both a code 13 (carriagereturn) and a 10 (linefeed) to the terminal. The kernel sees that 10is a newline, wraps up the collected buffer, and passes it on towhatever application is currently reading from the tty.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45549"></A>If you turn off <EMCLASS="emphasis">icanon</EM> (or turn on <EMCLASS="emphasis">cbreak</EM>), the kernel takes anypartially collected buffer and passes those characters to theapplication, then passes on each ordinary input character as it comesin. The kernel still echoes input back to the terminal. If you turnoff the <EMCLASS="emphasis">echo</EM> flag in the tty, the kernel will stop doing echoing.This is how a full-screen editor like <EMCLASS="emphasis">vi</EM> works: it turns off<EMCLASS="emphasis">icanon</EM>, turns off <EMCLASS="emphasis">echo</EM>, and turns off some, but not all, of thespecialcharacters. The <EMCLASS="emphasis">vi</EM> program can then do its own echoing, so that whenyou type <CODECLASS="literal">i</CODE> to go into insert mode, no <CODECLASS="literal">i</CODE> appears on yourterminal.</P><PCLASS="para">One of several difficult areas involves turning <EMCLASS="emphasis">icanon</EM> back on. Inparticular, there may be some characters you typed at the terminalwhile <EMCLASS="emphasis">icanon</EM> was off. These reached the tty, which packaged them upand sent them off to an application. The application may not have readthem yet, but as far as the <EMCLASS="emphasis">tty</EM> is concerned, they are gone. Thus, youmay not be able to recover them for your current input line. OlderBerkeley UNIX systems are able to handle this case, but System Vsystems that use the STREAMS interface are not. As long as your systemis fast enough, though, you will never notice, because applicationswill always turn <EMCLASS="emphasis">icanon</EM> on before you can type anything at theterminal.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1140-SECT-1.3">41.2.3 What About TABs? </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45568"></A><ACLASS="indexterm"NAME="AUTOID-45570"></A>Tabs are another difficult issue. The history here predates computing;typewriter tabs are sometimes used as the "right" model.Nonetheless, different terminals behave differently, and different
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -