zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    配置Podfile 一个工程内的多个Target
    iOS开发中的测试框架
    iOS 设置button文字过长而显示省略号的解决办法
    iOS界面的绘制和渲染
    iOS单元测试
    iOS消息转发机制
    对runtime的总结:让你会用Runtime
    Xcode的Refactor使用
    工厂设计模式
    iOS中的数据存储
  • 原文地址:https://www.cnblogs.com/xiayong123/p/3717257.html
Copyright © 2011-2022 走看看