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()
).
对于非规范输入——思考vi
or vim
或者其他什么——你按下一个字符,程序就会立即使用它。在您按回车键之前,您不会被耽搁。系统不对字符进行编辑;它们一输入就可供程序使用。由程序来适当地解释事物。现在,vim
确实做了一些看起来有点像规范输入的事情。例如,退格键向后移动,并且在输入模式下擦除那里的内容。但那是因为vim
选择让它表现得像那样。
规范和非规范输出并不是那么重要的事情。有一些细微的差别,涉及到是否在换行之前回车以及是否进行延迟(对于电子设备来说没有必要;在输出设备可能是 110- 的时代很重要)波特率电传打字机)。它还可以执行诸如处理不区分大小写的输出设备(再次是电传打字机)之类的操作。小写字母以大写形式输出,大写字母以反斜杠和大写形式输出。
过去,如果您在登录提示符下输入所有大写字母,那么登录程序会自动转换为所有大写字母都输出的模式,并在每个实际大写字母前面加上反斜杠。我怀疑电子终端不再这样做了。
Titanium Decoy 在评论中问道:
那么对于非规范输入,输入缓冲区是否完全被绕过?另外,生产线纪律从何而来?
对于非规范输入,仍然使用输入缓冲区;如果没有程序read()
调用等待来自终端的输入,字符保存在输入缓冲区中。不会发生对输入缓冲区的任何编辑。
线条规则类似于输入编辑所做的一组操作。因此,行规则的一个方面是擦除字符在规范输入模式下擦除先前的字符。如果你有icase
(输入大小写映射)设置,则大写字符将映射为小写字符,除非前面有反斜杠;我相信,这是一条生产线纪律,或者说是一条生产线纪律的一个方面。
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.
当然,在规范模式下,键盘输入的字符通常会回显到屏幕上;您可以控制是否发生回声。然而,这与规范输入有些背离;即使回声关闭,也会进行正常编辑。
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.
罗奇金德的第四章高级 Unix 编程,第二届 https://rads.stackoverflow.com/amzn/click/com/0131411543涵盖终端 I/O 并提供大部分此类信息 — 以及更多信息。其他 UNIX 编程书籍(至少是好的书籍)也会介绍它。