c_lflag中ICANON的作用

 
by Linux 程序设计

QUOTE
Canonical versus Non-Canonical Modes
The two problems are closely related. By default, terminal input is not made available to a program until
the user presses Enter. In most cases, this is a benefit because it allows the user to correct typing mistakes
using Backspace or Delete. Only when they’re happy with what they see on the screen do they press
Enter to make the input available to the program.
This behavior is called canonical, or standard, mode. All the input is processed in terms of lines. Until a
line of input is complete (usually when the user presses Enter), the terminal interface manages all the
key presses, including Backspace, and no characters may be read by the application.

The opposite of this is non-canonical mode, where the application has much greater control over the processing
of input characters. We’ll come back to these two modes again a little later.
Among other things, the Linux terminal handler likes translating interrupt characters to signals and can
automatically perform Backspace and Delete processing for you, so you don’t have to reimplement it in
each program you write. We’ll find out more about signals in Chapter 11.
So, what’s happening in our program? Well, Linux is saving the input until the user presses Enter, then
passing both the choice character and the subsequent Enter to the program. So, each time you enter a menu
choice, the program calls getchar, processes the character, then calls getchar again, which immediately
returns with the Enter character.
The character the program actually sees isn’t an ASCII carriage return, CR (decimal 13, hex 0D), but a line
feed, LF (decimal 10, hex 0A). This is because, internally, Linux (like UNIX) always uses a line feed to
end lines of text; that is, UNIX uses a line feed alone to mean a newline, where other systems, such as
MS-DOS, use a carriage return and a line feed together as a pair. If the input or output device also sends
or requires a carriage return, the Linux terminal processing takes care of it. This might seem a little
strange if you’re used to MS-DOS or other environments, but one of the very considerable benefits is
that there is no real difference between text and binary files on Linux. Only when you input or output
to a terminal or some printers and plotters are carriage returns processed.
We can correct the major deficiency in our menu routine simply by ignoring the additional line feed
character with some code such as this:
CODE
do {
selected = getchar();
} while(selected == ‘\n’);

This solves the immediate problem. We’ll return to the second problem of needing to press Enter, and a
more elegant solution to the line feed handling later.
posted @ 2011-07-19 10:17  Lunaa  阅读(170)  评论(0编辑  收藏  举报