关于生与熟终端模式的困惑? [英] Confusion about raw vs. cooked terminal modes?

查看:15
本文介绍了关于生与熟终端模式的困惑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个聊天客户端,它可以与服务器成功通信.我应该修改行为,以便在收到另一条聊天消息时提示向下移动(即,通过打印  来擦除消息,将消息打印在提示上方"提示,打印消息,然后重新打印提示).

I currently have a chat client that communicates successfully with a server. I am supposed to modify the behavior so that the prompt moves down when another chat message is received (i.e. the messages are printed "above" the prompt by printing  to erase the prompt, printing the message, and then re-printing the prompt).

我们得到了一个 test clienttest server 来测试这个功能,目前当用户有提示但没有输入时,他们可以接收来自其他人的消息用户.当他们开始输入时,缓冲区不会刷新,直到他们输入整个消息并按 ENTER 键.

We were given a test client and test server for testing this functionality, and currently when the user has a prompt but nothing typed in, they can receive messages from other users. When they start typing, the buffer doesn't flush until they type their whole message and hit ENTER.

我应该复制这种行为,但是我对原始模式实际上是什么感到非常困惑.

I am supposed to replicate this behavior, but I am very confused about what raw mode actually is.

谁能告诉我如何通过原始模式控制输入/输出,或者为什么我上面描述的测试程序的行为发生在使用原始模式?

Can anyone tell me how to control input/output via raw mode, or why the behavior of the test programs I described above occurs from using raw mode?

推荐答案

默认情况下,Unix 风格的 tty(即控制台)驱动程序将在cooked 模式"下接受输入.在这种模式下,它提供了一定数量的命令行编辑.用户可以输入一行输入,可能会删除并重新输入其中的一些(但这并不总是有效),并且在用户按下 Enter 键之前程序不会看到它.

By default, Unix-style tty (i.e. console) drivers will take input in "cooked mode". In this mode, it provides a certain amount of command-line editing. The user can type in a line of input, possibly deleting and retyping some of it (but that doesn't always work) and the program won't see it until the user hits enter.

这可能让人想起硬件终端通过串行线连接到计算机的时代;如果终端处理一些低级编辑,则计算机不必处理.它还免费为简单的 C 程序提供了一些基本的输入编辑.

This probably harkens back to the days of hardware terminals connected to the computer via a serial line; if the terminal handles some of the low-level editing, the computer doesn't have to. It also gives trivial C programs some basic input editing for free.

相反,原始模式将 TTY 驱动程序设置为在键入时将每个字符传递给程序.程序(在 Unixish 操作系统上)默认以熟模式启动,需要启用原始模式.

In contrast, raw mode sets up the TTY driver to pass every character to the program as it is typed. Programs (on Unixish operating systems) are started in cooked mode by default and need to enable raw mode.

过去如何做到这一点在操作系统之间差异很大,尽管现在 POSIX 已经标准化了这些东西.在 Linux 上,您可以阅读文档的termios"和tty_ioctl"手册页.基本上,您会获得一个包含 tty 设置的数据结构,修改您关心的部分(特别是启用原始模式),然后将其传回.

How to do this used to vary wildly between operating systems, although POSIX has standardized this stuff these days. On Linux, you can read the "termios" and "tty_ioctl" man pages for the documentation. Basically, you get a data structure containing the tty settings, modify the parts you care about (specifically, enabling raw mode) and then pass it back.

另一种可能是只使用 ncurses 库.它为您提取了所有这些内容.

Another possibility is to just use the ncurses library. It abstracts away all of that stuff for you.

这篇关于关于生与熟终端模式的困惑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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