规范与非规范终端输入

发布于 2024-07-10 16:54:42 字数 244 浏览 4 评论 0原文

我正在准备考试,我对 Unix 中规范与非规范输入/输出的工作方式感到困惑(例如,curses)。 我知道有一个缓冲区可以应用“行规则”来进行规范输入。 这是否意味着非规范输入会绕过缓冲区,或者只是意味着不应用任何线路规则? 此过程对于输入和输出操作有何不同?

在我使用过的用于演示规范输入的curses 程序中,用户键入的输入会在键入一定数量的字符或经过一定时间后自动输入。 这些事情中的任何一个都被视为“线路纪律”还是完全是另一回事?

I am studying for an exam and I am confused as to how canonical vs. non-canonical input/output works in Unix (e.g., curses). I understand that there is a buffer to which "line disciplines" are applied for canonical input. Does this mean that the buffer is bypassed for non-canonical input, or does it simply mean that no line disciplines are applied? How does this process differ for input and output operations?

In the curses programs I have worked with that demonstrate canonical input, the input typed by a user is automatically entered either after a certain number of characters have been typed or a certain amount of time has passed. Are either of these things considered "line disciplines" or is this something else entirely?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2024-07-17 16:54:42

对于规范输入——想想 shell; 实际上,考虑一下老式的 Bourne shell,因为 Bash 和亲戚都有命令行编辑功能。 您键入一行输入; 如果你犯了错误,你可以使用擦除字符(默认是Backspace,通常;有时是Delete)来擦除前一个字符。 如果你完全搞砸了,你可以用行终止字符取消整行(不完全标准化,通常是 Control-X)。 在某些系统上,您可以使用 Control-W 进行单词擦除。 所有这些都是规范输入。 整行被收集并编辑,直到按下行结束符 - Return。 随后,整条线路可供等待程序使用。 根据未完成的 read() 系统调用,整行都可供读取(通过对 read() 的一次或多次调用)。

对于非规范输入 - 想想 vivim 或其他任何东西 - 你按下一个字符,程序就会立即使用它。 在您按回车键之前,您不会被耽搁。 系统不对字符进行编辑; 它们一输入就可供程序使用。 由程序来适当地解释事物。 现在,vim 确实做了一些看起来有点像规范输入的事情。 例如,退格键向后移动,并且在输入模式下擦除那里的内容。 但这是因为 vim 选择让它表现得像那样。

规范和非规范输出并不是那么重要的事情。 有一些细微的差别,涉及到是否在换行之前回车以及是否进行延迟(对于电子设备来说不是必需的;在输出设备可能是 110- 的时代很重要)波特率电传打字机)。 它还可以执行诸如处理不区分大小写的输出设备(再次是电传打字机)之类的操作。 小写字母以大写形式输出,大写字母以反斜杠和大写形式输出。

过去,如果您在登录提示中输入所有大写字母,那么登录程序会自动转换为所有大写字母都输出的模式,并在每个实际大写字母前面加上反斜杠。 我怀疑电子终端不再这样做了。


TitaniumDecoy 在评论中问道:

那么对于非规范输入,输入缓冲区是否完全被绕过? 另外,生产线纪律从何而来?

对于非规范输入,仍然使用输入缓冲区; 如果没有程序使用 read() 调用等待来自终端的输入,则字符将保存在输入缓冲区中。 不会发生对输入缓冲区的任何编辑。

线条规则类似于输入编辑所做的一组操作。 因此,行规则的一个方面是擦除字符在规范输入模式下擦除先前的字符。 如果您设置了 icase (输入大小写映射),则大写字符将映射为小写字符,除非前面有反斜杠; 我相信,这是一条生产线纪律,或者说是一条生产线纪律的一个方面。


我忘了提及 EOF 处理 (Control-D) 是在规范模式下处理的; 它实际上意味着“使累积的输入可供read()使用”; 如果没有累积输入(如果您在行的开头键入 Control-D),则 read() 将返回零字节,然后将其解释为按程序执行 EOF。 当然,之后你可以在键盘上愉快地输入更多字符,而忽略 EOF(或在非规范模式下运行)的程序会很高兴。

当然,在规范模式下,键盘输入的字符通常会回显到屏幕上; 您可以控制是否发生回声。 然而,这与规范输入有些背离; 即使回声关闭,也会进行正常编辑。

类似地,中断和退出信号是规范模式处理的产物。 作业控制信号(例如 Control-Z)也是如此,用于挂起当前进程并返回到 shell。 同样,流控制(Control-SControl-Q 停止和启动输出)由规范模式提供。

Rochkind 的 高级 Unix 编程,第二版 的第 4 章涵盖了终端 I/O 并给出了其中大部分信息——以及更多。 其他 UNIX 编程书籍(至少是好的书籍)也会介绍它。

For canonical input — think shell; actually, think good old-fashioned Bourne shell, since Bash and relatives have command-line editing. You type a line of input; if you make a mistake, you use the erase character (default is Backspace, usually; sometimes Delete) to erase the previous character. If you mess up completely, you can cancel the whole line with the line kill character (not completely standardized, often Control-X). On some systems, you get a word erase with Control-W. All this is canonical input. The entire line is gathered and edited up until the end of line character — Return — is pressed. Thereupon, the whole line is made available to waiting programs. Depending on the read() system calls that are outstanding, the whole line will be available to be read (by one or more calls to read()).

For non-canonical input — think vi or vim or whatever — you press a character, and it is immediately available to the program. You aren't held up until you hit return. The system does no editing of the characters; they are made available to the program as soon as they are typed. It is up to the program to interpret things appropriately. Now, vim does do a number of things that look a bit like canonical input. For example, backspace moves backwards, and in input mode erases what was there. But that's because vim chooses to make it behave like that.

Canonical and non-canonical output is a much less serious business. There are a few bits and pieces of difference, related to things like whether to echo carriage-return before line-feed, and whether to do delays (not necessary with electronics; important in the days when the output device might have been a 110-baud teletype). It can also do things like handle case-insensitive output devices — teletypes, again. Lower-case letters are output in caps, and upper-case letters as backslash and caps.

It used to be that if you typed all upper-case letters to the login prompt, then the login program would automatically convert to the mode where all caps were output with a backslash in front of each actual capital. I suspect that this is no longer done on electronic terminals.


In a comment, TitaniumDecoy asked:

So with non-canonical input, is the input buffer bypassed completely? Also, where do line disciplines come in?

With non-canonical input, the input buffer is still used; if there is no program with a read() call waiting for input from the terminal, the characters are held in the input buffer. What doesn't happen is any editing of the input buffer.

Line disciplines are things like the set of manipulations that the input editing does. So, one aspect of the line discipline is that the erase character erases a prior character in canonical input mode. If you have icase (input case-mapping) set, then upper-case characters are mapped to lower-case unless preceded by a backslash; that is a line discipline, I believe, or an aspect of a line discipline.


I forgot to mention that EOF processing (Control-D) is handled in canonical mode; it actually means 'make the accumulated input available to read()'; if there is no accumulated input (if you type Control-D at the beginning of a line), then the read() will return zero bytes, which is then interpreted as EOF by programs. Of course, you can merrily type more characters on the keyboard after that, and programs that ignore EOF (or run in non-canonical mode) will be quite happy.

Of course, in canonical mode, the characters typed at the keyboard are normally echoed to the screen; you can control whether that echoing occurs. However, this is somewhat tangential to canonical input; the normal editing occurs even when echo is off.

Similarly, the interrupt and quit signals are artefacts of canonical mode processing. So too are the job control signals such as Control-Z to suspend the current process and return to the shell. Likewise, flow control (Control-S, Control-Q to stop and start output) is provided by canonical mode.

Chapter 4 of Rochkind's Advanced Unix Programming, 2nd Edn covers terminal I/O and gives much of this information — and a whole lot more. Other UNIX programming books (at least, the good ones) will also cover it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文