规范与非规范终端输入 [英] Canonical vs. non-canonical terminal input

查看:80
本文介绍了规范与非规范终端输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为考试而学习,我对Unix中规范输入与非规范输入/输出的工作方式感到困惑(例如curses).我了解有一个缓冲,可以将学科"应用于规范输入.这是否意味着为非规范输入绕过了缓冲区,还是仅意味着未应用任何行规?对于输入和输出操作,此过程有何不同?

在我使用过的curses程序中,它们演示了规范的输入,在键入了一定数量的字符或经过了一定的时间后,会自动输入由用户键入的输入.这些东西是被视为学科"还是完全是其他东西?

解决方案

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

对于非规范输入—考虑vivim或其他—您按一个字符,该程序将立即可用.直到您按下回车键,您才被阻止.系统不编辑字符;键入后即可将其提供给程序.程序可以适当地解释事物.现在,vim确实做了很多看起来像规范输入的事情.例如,退格键向后移动,而在输入模式下则擦除那里的内容.但这是因为vim选择使其具有这种行为.

规范和非规范输出的重要性要小得多.两者之间存在一些细微的差异,例如是否在换行之前回送回车,以及是否进行延迟(对于电子设备而言不是必需的;在输出设备可能是110-波特电传).它还可以执行诸如不区分大小写的输出设备的处理-电传打字机,再次.小写字母以大写字母输出,大写字母以反斜杠和大写字母输出.

过去,如果您在登录提示中键入所有大写字母,则登录程序将自动转换为输出所有大写字母且每个实际大写字母前面都带有反斜杠的模式.我怀疑这不再在电子终端上完成.


在评论中,TitaniumDecoy问:

那么对于非规范输入,输入缓冲区是否被完全旁路?另外,学科学科从何而来?

对于非规范输入,仍使用输入缓冲区;如果没有带有read()调用的程序正在等待来自终端的输入,则这些字符将保留在输入缓冲区中.什么都不会发生,就是对输入缓冲区进行任何编辑.

行规是输入编辑所做的一系列操作.因此,行规的一个方面是,擦除字符在规范输入模式下擦除了先前的字符.如果设置了icase(输入大小写映射),则大写字符将映射为小写,除非在前面加上反斜杠;否则,将大写字符映射为小写.我认为这是一门学科,或者一门学科的一个方面.


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

当然,在规范模式下,通常会在键盘上回显在键盘上键入的字符.您可以控制是否发生回声.但是,这在某种程度上与规范输入相切.即使关闭回声,也会进行正常的编辑.

类似地,中断和退出信号是规范模式处理的伪像.诸如 Control-Z 之类的作业控制信号也将暂停当前进程并返回到Shell.同样,规范模式提供了流控制( Control-S Control-Q 用于停止和开始输出).

Rochkind的高级Unix编程,第二版Edn 涵盖了终端I/O,并提供了许多此类信息-还有更多.其他UNIX编程书籍(至少是好的书籍)也将对此进行介绍.

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?

解决方案

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.

这篇关于规范与非规范终端输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆