📄 ch41_02.htm
字号:
people make different assumptions about how tabs should work. TheASCII code for TAB, code 9, is intended to move the cursor right to thenext tabstop. But where <EMCLASS="emphasis">is</EM> that? Moreover, once the cursor hasgone there, how does the kernel move it back if you decided to erasethe tab?</P><PCLASS="para">Many UNIX kernels can be told to expand tabs. When they do this, theyset the tabstops at every eight characters. This is where they thinktabstops belong. That is, if you print a newline, two ordinaryletters, and a tab, the tab will turn into six spaces. If a <EMCLASS="emphasis">tty</EM> is inicanon/cooked mode, and is expanding tabs, it can "unexpand" themto backspace over the tab. Berkeley kernels will do this, and itworks fairly well. They can get it wrong, however, under certainconditions. For instance, if you set the <EMCLASS="emphasis">tty</EM> to pass tabs unmodified,and if the terminal itself puts tabstops at every tencharacters - this would be the proper setting for dealing with aDEC-10, for instance - the kernel <EMCLASS="emphasis">tty</EM> code will put out fewerbackspaces than needed.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45579"></A><ACLASS="indexterm"NAME="AUTOID-45582"></A><ACLASS="indexterm"NAME="AUTOID-45585"></A>Even if the terminal sets its tabstops at eight, the kernel's <EMCLASS="emphasis">tty</EM> codeand the terminal can get different ideas of the current cursor column.Most Berkeley kernels count control codes as "ordinary" outputcharacters, for instance, even though those characters are likely tohave no effect on the cursor, or might even move it to an arbitraryposition. To help prevent <EMCLASS="emphasis">input</EM> control characters from goofing upbackspacing, Berkeley kernels can echo them as two-charactersequences. For instance, CTRL-g will normally echo as <CODECLASS="literal">^G</CODE>. Erasingsuch a control character works properly: the <EMCLASS="emphasis">tty</EM> code puts out <EMCLASS="emphasis">two</EM>backspaces, <EMCLASS="emphasis">two</EM> spaces, and two more backspaces. Erasing morecharacters, possibly including a TAB, then still works. This "controlecho" can be switched on and off individually as well.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45594"></A>In addition to carriage return delays, which exist to allow timefor the teletype's print carriage to move left, some UNIX systemsalso support tab delays, for more or less the same reason. Likereturn-delays, these are pretty much outmoded and useless. ThePOSIX standard leaves room for both kinds of delay, but does notmandate either one. You may see them in <EMCLASS="emphasis">stty</EM> output, as <EMCLASS="emphasis">cr2</EM>,<EMCLASS="emphasis">cr3</EM>, <EMCLASS="emphasis">tab1</EM>, and the like, but your system's default is probably"no delay," and few people are likely to change this deliberately.</P><PCLASS="para">Article<ACLASS="xref"HREF="ch41_04.htm"TITLE="How UNIX Handles TAB Characters ">41.4</A>has some higher-level information about TABs.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1140-SECT-1.4">41.2.4 Flow Control (We Hope) </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45605"></A><ACLASS="indexterm"NAME="AUTOID-45607"></A><ACLASS="indexterm"NAME="AUTOID-45609"></A><ACLASS="indexterm"NAME="AUTOID-45611"></A><ACLASS="indexterm"NAME="AUTOID-45613"></A><ACLASS="indexterm"NAME="AUTOID-45615"></A>Finally, flow control - avoiding lost input and output characters - isperhaps the dirtiest swamp of all. Most of the terminals built in the1980s support, and at higher speeds require, something called\h'-1p'<EMCLASS="emphasis">XON/XOFF</EM> flow control. Here, when the terminal falls behind inprinting characters, it shouts "stop!" by sending an XOFFcharacter - ASCII code 19, or CTRL-s - to the UNIX system. If theUNIX machine does not stop soon enough, some text will be lost. Whenthe terminal is ready for more, it sends a "go" character - an XON,ASCII code 17, or CTRL-q. These were never intended as a generalflow control mechanism - on some of the original teletypes, they turnedthe paper tape punch off and on - but they have that meaning now.Unfortunately, most terminals also allow users to type CTRL-s andCTRL-q, but they provide no way to distinguish between the terminalyelling "stop" and the user pushing CTRL-s. The result is aconstant battle between people who want to use CTRL-s and computersystems that want to take it for themselves.</P><PCLASS="para">Other systems, notably HP-based systems, use something called <EMCLASS="emphasis">ENQ/ACK</EM>flow control. Here the terminal and the host system must agree up-front on a minimum buffer size. Then either system is allowed to sendthat many characters to the other, after which it must stop and waitfor a "go-ahead" signal. Each system requests such a signal bysending an "enquire": ASCII code 5, or CTRL-e. When the listeningsystem encounters the ENQ, and is ready for more, it sends anacknowledgement: ASCII code 6, or CTRL-f. This system is superiorto the XON/XOFF system in one way, as it never has problems with abusy system failing to stop immediately on command, but it still doesnot prevent users from typing CTRL-e and CTRL-f. Moreover, itis not implemented on most UNIX systems.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45621"></A><ACLASS="indexterm"NAME="AUTOID-45623"></A><ACLASS="indexterm"NAME="AUTOID-45626"></A>A third method of flow control, and the most reliable where it isavailable, is the so-called <EMCLASS="emphasis">out of band</EM> approach. "Out of band"simply means that users cannot accidentally simulate it by typingcontrol characters. Out of band control can be done in software,using something similar to HP's ENQ/ACK and some encoding tricks,but in practice, most UNIX machines that support any kind of outof band flow control use something called either "hardware flowcontrol" or "<EMCLASS="emphasis">RTS/CTS</EM> flow control." (This can be implementedwith no special hardware at all on many systems, so the latter nameis better.)</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45631"></A><ACLASS="indexterm"NAME="AUTOID-45633"></A>With RTS/CTS flow control, two existing serial-cable wires, RTS andCTS, are "taken over." (RTS and CTS-which stand for Request toSend and Clear to Send respectively - were originally intended for usewith half duplex modems. Since half duplex modems are today merelymuseum pieces, this is a sensible approach, but it does violate theRS232 standard.) RTS at the terminal is cross-connected to CTS at thehost computer, and vice versa. The terminal and the computer bothassert RTS whenever they are ready to receive data, and wait for CTSbefore sending. Unfortunately, not enough systems implement this, andof those that do, many get it wrong.[7]Thus, while RTS/CTS flowcontrol offers the possibility of working perfectly, you cannot counton it. Still, it is worth looking for an <EMCLASS="emphasis">rts/cts</EM> option in yourUNIX's <EMCLASS="emphasis">stty</EM>.</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[7] For instance, on Sun workstations, RTS/CTS is supported inhardware, but the particular Zilog chip that does this also usesthe DCD (Data Carrier Detect) line to control the receiver. Thus,if you set <EMCLASS="emphasis">stty crtscts</EM>, you cannot tell a modem to dial out,because DCD is off. It is possible to work around this, but onlywith control over both the hardware and the UNIX kernel.</P></BLOCKQUOTE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1140-SECT-1.5">41.2.5 Then What? </A></H3><PCLASS="para">If you think this is complicated, just hope you never have to deal withsynchronous transmission, RS422, DIN connectors, lightning strikeprotection, and many of the other hardware and electrical aspects thatsurround computer communications. Getting two arbitrary computers totalk to each other can be excessively difficult. Here again, standardscome to the rescue. If everything you have is proper RS232-<SPANCLASS="link">modulo (<ACLASS="linkend"HREF="glossary.htm#UPT-ART-1010"TITLE="Glossary">52.9</A>)</SPAN>,perhaps, RTS/CTS flow control - and POSIX, things should usually gosmoothly.</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">CT</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch41_01.htm"TITLE="41.1 Delving a Little Deeper "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 41.1 Delving a Little Deeper "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch41_03.htm"TITLE="41.3 Find Out Terminal Settings with stty "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 41.3 Find Out Terminal Settings with stty "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">41.1 Delving a Little Deeper </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">41.3 Find Out Terminal Settings with stty </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -