?? tty layer - the user perspective.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0060)http://www.geocities.com/marco_corvi/games/lkpe/tty/user.htm -->
<HTML><HEAD><TITLE>The TTY layer</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"><LINK
href="tty layer - The user perspective_file/style.css" rel=stylesheet>
<META content="MSHTML 6.00.2800.1170" name=GENERATOR></HEAD>
<BODY>
<H2>tty layer - The user perspective</H2>
<DIV>References:<BR>W.R. Stevens, "Adavnced Programming in the UNIX
environment", Addison-Wesley 1993 <BR></DIV><BR clear=all><BR clear=all><BR
clear=all>
<DIV><IMG height=200 src="tty layer - The user perspective_file/ttyio.gif"
width=240 align=right> The user perspective of the tty layer is the terminal
I/O. This is a complex topic because it is used to control a lot of different
devices (terminals, modem, serial lines, ...). The logical view is shown in the
figure on the side. Between the user input/output, mediated by the device
drivers, and the process read/write, there is the tty layer, which buffers the
data in two queues. Both queues have a maximum size. When the output queue is
full the process is put to wait. When the input queue is full characters are
lost. If echo is enabled the input characters are echoed on the output queue.
</DIV>
<DIV>The kernel characteristics of a tty are contained in a <CODE>struct
termio</CODE> (defined in include/asm/termios.h). This contains
<UL>
<LI><CODE>c_iflag</CODE>, the input mode flags;
<LI><CODE>c_oflag</CODE>, the output mode flags;
<LI><CODE>c_lflag</CODE>, the local mode flags;
<LI><CODE>c_cflag</CODE>, the control mode flags;
<LI><CODE>c_line</CODE>, line discipline type (this is not seen by the user);
<LI><CODE>c_cc</CODE>, an array of control characters </LI></UL>A similar
structure exists (<CODE>termios</CODE>) which differ only in the typing of the
fields (POSIX like). Userland has the <CODE>struct termios</CODE> (define in
/usr/include/bits/termios.h) which contains also i/o speed fields. The values of
the flags bits and the special characters are listed in the tables below. </DIV>
<TABLE cellSpacing=0 cellPadding=2 border=1>
<TBODY>
<TR>
<TH rowSpan=14>c_iflag</TH>
<TD>BRKINT</TD>
<TD>generate SIGINT on BREAK</TD></TR>
<TR>
<TD>ICRNL</TD>
<TD>map CR to NL on input</TD></TR>
<TR>
<TD>IGNBRK</TD>
<TD>ignore BREAK</TD></TR>
<TR>
<TD>IGNCR</TD>
<TD>ignore CR</TD></TR>
<TR>
<TD>IGNPAR</TD>
<TD>ignore characters with parity errors</TD></TR>
<TR>
<TD>IMAXBEL</TD>
<TD>ring bell on input when the input queue is full</TD></TR>
<TR>
<TD>INLCR</TD>
<TD>map NL to CR </TD></TR>
<TR>
<TD>INPCK</TD>
<TD>enable input parity checking</TD></TR>
<TR>
<TD>ISTRIP</TD>
<TD>strip eighth bit off input characters</TD></TR>
<TR>
<TD>IUCLC</TD>
<TD>map uppercase to lowercase on input</TD></TR>
<TR>
<TD>IXANY</TD>
<TD>exable any character to restart output</TD></TR>
<TR>
<TD>IXOFF</TD>
<TD>enable start/stop input flow control</TD></TR>
<TR>
<TD>IXON</TD>
<TD>enable start/stop output flow control</TD></TR>
<TR>
<TD>PARMRK</TD>
<TD>mark parity errors</TD></TR>
<TR>
<TH rowSpan=16>c_oflag</TH>
<TD>BSDLY</TD>
<TD>backspace delay mask</TD></TR>
<TR>
<TD>CRDLY</TD>
<TD>CR delay mask</TD></TR>
<TR>
<TD>FFDLY</TD>
<TD>formfeed delay mask</TD></TR>
<TR>
<TD>NLDLY</TD>
<TD>NL delay mask</TD></TR>
<TR>
<TD>OCRNL</TD>
<TD>map CR to NL on output</TD></TR>
<TR>
<TD>OFDEL</TD>
<TD>fill is DEL (else is NUL)</TD></TR>
<TR>
<TD>OFILL</TD>
<TD>use fill characters for delay</TD></TR>
<TR>
<TD>OLCUL</TD>
<TD>map lowercase to uppercase on output</TD></TR>
<TR>
<TD>ONLCR</TD>
<TD>map NL to CR-NL (this is CRMOD)</TD></TR>
<TR>
<TD>ONLRET</TD>
<TD>NL performs CR function</TD></TR>
<TR>
<TD>ONOCR</TD>
<TD>no CR output at column 0</TD></TR>
<TR>
<TD>ONOEOT</TD>
<TD>discard EOF (^D) on output</TD></TR>
<TR>
<TD>OPOST</TD>
<TD>perform output processing</TD></TR>
<TR>
<TD>OXTABS</TD>
<TD>expand tabs to spaces</TD></TR>
<TR>
<TD>TABDLY</TD>
<TD>horizontal tab delay mask</TD></TR>
<TR>
<TD>VTDLY</TD>
<TD>vertical tab delay mask</TD></TR>
<TR>
<TH rowSpan=11>c_cflag</TH>
<TD>CCTS_OFLOW</TD>
<TD>CTS flow control of output</TD></TR>
<TR>
<TD>CIGNORE</TD>
<TD>ignore control flags</TD></TR>
<TR>
<TD>CLOCAL</TD>
<TD>ignore modem status line</TD></TR>
<TR>
<TD>CREAD</TD>
<TD>enable receiver</TD></TR>
<TR>
<TD>CRTS_IFLOW</TD>
<TD>RTS flow control on input</TD></TR>
<TR>
<TD>CSIZE</TD>
<TD>character size mask</TD></TR>
<TR>
<TD>CSTOPB</TD>
<TD>send two stop bits (else send one)</TD></TR>
<TR>
<TD>HUPCL</TD>
<TD>hangup on last close</TD></TR>
<TR>
<TD>MDMBUF</TD>
<TD>flow control output via Carrier</TD></TR>
<TR>
<TD>PARENB</TD>
<TD>parity enable</TD></TR>
<TR>
<TD>PARODD</TD>
<TD>parity is odd (else is even)</TD></TR>
<TR>
<TH rowSpan=17>c_lflag</TH>
<TD>ALTWERASE</TD>
<TD>use alternate WERASE algorithm</TD></TR>
<TR>
<TD>ECHO</TD>
<TD>enable echo</TD></TR>
<TR>
<TD>ECHOCTL</TD>
<TD>echo control characters as ^Char</TD></TR>
<TR>
<TD>ECHOE</TD>
<TD>visually erase characters</TD></TR>
<TR>
<TD>ECHOK</TD>
<TD>echo KILL (by erasing the entire line)</TD></TR>
<TR>
<TD>ECHOKE</TD>
<TD>visual erase for KILL</TD></TR>
<TR>
<TD>ECHONL</TD>
<TD>echo NL</TD></TR>
<TR>
<TD>ECHOPRT</TD>
<TD>visula erase mode for hardcopy</TD></TR>
<TR>
<TD>FLUSHO</TD>
<TD>output being flushed</TD></TR>
<TR>
<TD>ICANON</TD>
<TD>canonical input</TD></TR>
<TR>
<TD>IEXTEN</TD>
<TD>enable extended input character processing</TD></TR>
<TR>
<TD>ISIG</TD>
<TD>enable terminal-generated signals</TD></TR>
<TR>
<TD>NOFLSH</TD>
<TD>disable flush after interrupt or quit</TD></TR>
<TR>
<TD>NOKERNINFO</TD>
<TD>no kernel output from STATUS</TD></TR>
<TR>
<TD>PENDIN</TD>
<TD>retype pending input</TD></TR>
<TR>
<TD>TOSTOP</TD>
<TD>send SIGTTOU for background output</TD></TR>
<TR>
<TD>XCASE</TD>
<TD>canonical upper/lower presentation</TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=2 border=1>
<TBODY>
<TR>
<TH colSpan=2>Special characters</TH>
<TH>subscript</TH>
<TH>enabled by</TH></TR>
<TR>
<TD>CR</TD>
<TD>carriage return</TD>
<TD>-</TD>
<TD rowSpan=10>ICANON</TD></TR>
<TR>
<TD>EOF</TD>
<TD>end-of-file</TD>
<TD>VEOF</TD></TR>
<TR>
<TD>EOL</TD>
<TD>end-of-line</TD>
<TD>VEOL</TD></TR>
<TR>
<TD>EOL2</TD>
<TD>alternate end-of-line</TD>
<TD>VEOL2</TD></TR>
<TR>
<TD>ERASE</TD>
<TD>backspace one character</TD>
<TD>VERASE</TD></TR>
<TR>
<TD>KILL</TD>
<TD>VKILL</TD>
<TD>erase line</TD></TR>
<TR>
<TD>NL</TD>
<TD>linefeed</TD>
<TD>-</TD></TR>
<TR>
<TD>REPRINT</TD>
<TD>reprint all input</TD>
<TD>VREPRINT</TD></TR>
<TR>
<TD>STATUS</TD>
<TD>status request</TD>
<TD>VSTATUS</TD></TR>
<TR>
<TD>WERASE</TD>
<TD>backspace one word</TD>
<TD>VWERASE</TD></TR>
<TR>
<TD>START</TD>
<TD>resume output</TD>
<TD>VSTART</TD>
<TD rowSpan=2>IXON / IXOFF</TD></TR>
<TR>
<TD>STOP</TD>
<TD>stop output</TD>
<TD>VSTOP</TD></TR>
<TR>
<TD>DSUSP</TD>
<TD>delayed suspend (SIGTSTP)</TD>
<TD>VDSUSP</TD>
<TD rowSpan=4>ISIG</TD></TR>
<TR>
<TD>INTR</TD>
<TD>interrupt signal (SIGINT)</TD>
<TD>VINTR</TD></TR>
<TR>
<TD>QUIT</TD>
<TD>quit signal (SIGQUIT)</TD>
<TD>VQUT</TD></TR>
<TR>
<TD>SUSP</TD>
<TD>suspend signal (SIGTSTP)</TD>
<TD>VSUSP</TD></TR>
<TR>
<TD>DISCARD</TD>
<TD>discard output</TD>
<TD>VDISCARD</TD>
<TD rowSpan=2>IEXTEN</TD></TR>
<TR>
<TD>LNEXT</TD>
<TD>literal next</TD>
<TD>VLNEXT</TD></TR></TBODY></TABLE>
<DIV>The user control of the tty can be done with the <CODE>ioctl()</CODE>
function, but its last argument depends on the action being performed. This
makes type checking impossible. Therefore POSIX defines 12 functions,
<UL>
<LI><CODE>tcsetattr</CODE>, <CODE>tcgetattr</CODE>: to set/get the tty
attributes;
<LI><CODE>cfgetispeed</CODE>, <CODE>cfsetispeed</CODE>,
<CODE>cfgetospeed</CODE>, <CODE>cfsetospeed</CODE>: for the i/o speeds (baud
rate);
<LI><CODE>tcdrain</CODE>, <CODE>tcflow</CODE>, <CODE>tcflush</CODE> and
<CODE>tcsendbreak</CODE> are line control functions;
<LI><CODE>tcgetpgrp</CODE> and <CODE>tcsetpgrp</CODE>: get/set foreground
process group ID. </LI></UL></DIV>
<DIV><B>Canonical mode</B><BR>Canonical mode (cooked) is simple, from the user
point of view: reads are linewise, ie, the read() call returns when a complete
line has been entered by the device driver. A line is complete when one of the
folloing delimiters is entered: NL, EOL, EOL2, EOF (and CR if ICRNL is set and
INGCR is not set). Note that EOF is not returned with the read. </DIV>
<DIV><B>Noncanonical mode</B><BR>This is set by turning off ICANON. The input
characters are not assembled into lines. These special characters are not
processed: ERASE, KILL, EOF, NL, EOL, EOL2, CR, REPRINT, STATUS, WERASE. The two
entries <CODE>c_cc[VMIN]</CODE> and <CODE>c_cc[VTIME]</CODE> determine the
behaviour of the <CODE>read()</CODE>. There are four cases:
<TABLE cellSpacing=0 cellPadding=2 border=1>
<TBODY>
<TR>
<TD>MIN=0</TD>
<TD>TIME=0</TD>
<TD>read() returns immediately with whatever data are available (from 0 if
none, to the maximum number requested in the call)</TD></TR>
<TR>
<TD>MIN=0</TD>
<TD>TIME>0</TD>
<TD>read() returns between 1 and the requested number if there are data
before the time expires; it returns 0 if the time expires</TD></TR>
<TR>
<TD>MIN>0</TD>
<TD>TIME=0</TD>
<TD>read() returns between 1 and the requested number when there are data
available (may wait forever)</TD></TR>
<TR>
<TD>MIN>0</TD>
<TD>TIME>0</TD>
<TD>read() returns between MIN and the number requested before the time
expires, or or between 1 and MIN-1 if the time expires. It can block
forever if there are no data</TD></TR></TBODY></TABLE></DIV>
<DIV>There are two noncanonical modes (i am aware of), cbreak and raw.
<TABLE cellSpacing=0 cellPadding=2 border=1>
<TBODY>
<TR>
<TH>cbreak </TH>
<TD>ECHO and ICANON off</TD>
<TD> </TD>
<TD> </TD>
<TD> </TD>
<TD>MIN=1, TIME=0 </TD></TR>
<TR>
<TH>raw </TH>
<TD>ECHO, ICANON, IEXTEN and ISIG off </TD>
<TD>BRKINT, ICRNL, INPCK, ISTRIP, IXON off </TD>
<TD>CSIZE, PARENB and OPOST off </TD>
<TD>CS8 (set 8-bit characters) </TD>
<TD>MIN=1, TIME=0 </TD></TR></TBODY></TABLE></DIV><BR clear=all>
<DIV>To conclude this summary of terminal I/O look at this example from Steven's
book. <BR><A
href="http://www.geocities.com/marco_corvi/games/lkpe/tty/tty_raw.c">tty_raw.c</A>,
set a tty in raw mode<BR></DIV><BR clear=all><FONT size=-1>Marco Corvi -
2003</FONT>
<!-- text below generated by server. PLEASE REMOVE --></OBJECT></LAYER>
<DIV></DIV></SPAN></STYLE></NOSCRIPT></TABLE></SCRIPT></APPLET>
<SCRIPT
language=JavaScript>var PUpage="76001084"; var PUprop="geocities"; </SCRIPT>
<SCRIPT language=JavaScript
src="tty layer - The user perspective_file/pu5geo.js"></SCRIPT>
<SCRIPT language=JavaScript
src="tty layer - The user perspective_file/ygIELib9.js"></SCRIPT>
<SCRIPT language=JavaScript>var yviContents='http://us.toto.geo.yahoo.com/toto?s=76001084&l=NE&b=1&t=1057747208';yviR='us';yfiEA(0);</SCRIPT>
<SCRIPT language=JavaScript
src="tty layer - The user perspective_file/mc.js"></SCRIPT>
<SCRIPT language=JavaScript
src="tty layer - The user perspective_file/geov2.js"></SCRIPT>
<SCRIPT language=javascript>geovisit();</SCRIPT>
<NOSCRIPT><IMG height=1 alt=setstats
src="tty layer - The user perspective_file/visit.gif" width=1
border=0></NOSCRIPT> <IMG height=1 alt=1
src="tty layer - The user perspective_file/serv.gif" width=1> <!-- w31.geo.scd.yahoo.com compressed/chunked Wed Jul 9 03:40:08 PDT 2003 --></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -