Confusion about raw vs. cooked terminal modes?

前端 未结 2 1232
耶瑟儿~
耶瑟儿~ 2020-12-18 04:35

I currently have a chat client that communicates successfully with a server. I am supposed to modify the behavior so that the prompt moves

相关标签:
2条回答
  • 2020-12-18 04:50

    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.

    0 讨论(0)
  • 2020-12-18 04:55

    In raw mode, characters are passed directly to your program. For example, when you type in a terminal in cooked mode, the characters you type are instantly echoed on the screen. In raw mode, the characters are ignored by the terminal (that is, not echoed) and your program has the option of whether or not to echo it.

    EDIT: Wikipedia has a good article on cooked mode. http://en.wikipedia.com/wiki/Cooked_Mode Also, note that in raw mode, even and similar characters are handed directly to your program.

    0 讨论(0)
提交回复
热议问题