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.
  • 相关阅读:
    【超详细教程】使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结
    Jquery+asp.net后台数据传到前台js进行解析的方法
    OWASP Top 10 – 2013, 最新十大安全隐患(ASP.NET解决方法)
    Owasp Top 10 Security Risks for 2014
    js中cookie的使用详细分析
    Cookie/Session机制详解
    ASP.NET Web API 简介
    未找到具有固定名称“System.Data.SQLite”的 ADO.NET 提供程序的实体框架提供程序
    网页被Chrome识别成英语,区域,语言,网站
    16进制字符串转换为byte数组
  • 原文地址:https://www.cnblogs.com/xiayong123/p/3717257.html
Copyright © 2011-2022 走看看