关于原始与熟食终端模式的混淆? [英] Confusion about raw vs. cooked terminal modes?

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

问题描述

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

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 \b to erase the prompt, printing the message, and then re-printing the prompt).

我们获得了测试客户端测试服务器用于测试此功能,并且当前当用户有提示但没有输入任何内容时,他们可以接收来自其他用户的消息。当他们开始输入时,缓冲区不会刷新,直到他们输入整个消息并按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(即控制台)驱动程序将输入熟食模式。在此模式下,它提供了一定量的命令行编辑。用户可以键入一行输入,可能删除并重新输入其中一些(但这并不总是有效),程序在用户点击输入之前不会看到它。

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天全站免登陆