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.

转载于:https://www.cnblogs.com/xiayong123/archive/2011/07/19/3717257.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值