在终端中禁用本地回显的含义 [英] The meaning of disabling local echo in terminal

查看:74
本文介绍了在终端中禁用本地回显的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Linux操作系统上运行的c应用程序.此应用程序从终端获取键盘键,并将其发送到远程服务器.
下面的代码打开终端:

I have a c application running on Linux OS's. This app gets keyboard keys from a terminal and sends them to remote server.
The code below opens the terminal:

    // save old terminal attributes
if (tcgetattr(0, &ttyold) != 0) {
    fprintf(stderr, "Failed getting terminal attributes\n");
    goto out;
}

ttynew = ttyold;

ttynew.c_iflag = 0;
ttynew.c_oflag = 0;

// disable canonical mode (don't buffer by line)
ttynew.c_lflag &= ~ICANON;

// disable local echo
ttynew.c_lflag &= ~ECHO;

ttynew.c_cc[VMIN] = 1;
ttynew.c_cc[VTIME] = 1;

// set new terminal attributes 
if (tcsetattr(0, TCSANOW, &ttynew) != 0) {
    fprintf(stderr, "Failed setting terminal attributes\n");
    goto out;

我没有写这个程序,我只是想了解这段代码.我不明白为什么以前的生成器禁用了回声?必须发送的数据不是秘密.还有什么可能是什么意思?表现?禁用缓冲?
另外,我很乐意得到"ttynew.c_lflag& =〜ICANON;"的解释.代码.

I did not write this app, I'm just trying to understand this code. I don't understand why the previous engeneer disabled the echo? the data that has to be sent isn't secret. What else could be the meaning of this? performance? disable buffering?
In addition, I'll be happy to get an explanation of "ttynew.c_lflag &= ~ICANON;" code.

谢谢.

推荐答案

如果接收端未回显,则需要启用它.如果接收端回显,则将其禁用,否则最终会看到所有内容翻倍.

If the receiving end doesn't echo, you need to enable this. If the receiving end echoes, you disable it, otherwise you end up with seeing everything double.

此处解释了所有内容:

在标准模式下:逐行使输入可用.键入其中一个行定界符(NL,EOL,EOL2;或在行首的EOF)时,输入行可用.

In canonical mode: Input is made available line by line. An input line is available when one of the line delimiters is typed (NL, EOL, EOL2; or EOF at the start of line).

基本上,数据是在EOL之后发送的,而不是按字符发送的.

Basically, data is sent after EOL, rather than per character.

这篇关于在终端中禁用本地回显的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆