Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

c - Confusion about raw vs. cooked terminal modes?

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).

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...