Ctl-H "Rubout"

来源:互联网 发布:大数据就业前景 知乎 编辑:程序博客网 时间:2024/06/07 07:40
Hello Jean,

I am quoting following from bash scripting guide by Mendel Cooper -
TLDP

Ctl-H
"Rubout" (destructive backspace). Erases characters the cursor backs
over while backspacing.

What is that I am missing ?
=============================================
They are partly unrelated.

One thing is what a terminal, or a terminal emulator nowadays, does
when it receives some character, another thing is what the terminal
is doing when it receive some character from the keyboard.

The scripting guide is talking about the latter feature, while the \b
echo is about the former.
[...]
============================================
Actually, it's not the terminal emulator (xterm) (on Unix) that
does things when you press the erase key, it's the
pseudo-terminal driver (the Unix kernel) or the application
(bash) that does things to handle an incoming "erase" character..

If you configure your pseudo-terminal driver with

stty echoe erase='^K' icanon
(and run a command that does not modify the terminal settings
such as sleep 2000)

Upon entering the ^K character (generally by pressing <Ctrl-K>),
the terminal driver, seeing that it is in canon mode and that
this character is the erase character, will look at its internal
line edition buffer. If it's not empty, it will remove the last
character in it and (as echoe is on) will send as many "\b \b"
sequences as need on output (there may need 2 for instance if
echoctl is on and the character to erase is \a that has been
displayed as ^G) for the corresponding character to be deleted
on the terminal display.

If you unset echoe and echoctl and "erase" happens to be ^H
(\b), then upon pressing backspace, the terminal will perform
the internal edition of its edition buffer but send the input
character to the output as is, so as it's ^H, that will cause
the cursor to go backward without the characters to be erased.

In non-icanon mode (that is when the pseudo-terminal driver
doesn't handle an internal line edition buffer), like for
instance at the bash prompt, it's the application (bash) that is
responsible of processing the input characters (that it gets as
soon as they are entered, not only on pressing <Return>) and of
sending the correct characters to the terminal for the display
to look nice.

The application can do as it wants. For instance, upon receiving
^H, it may decide to shorten its internal buffer, but for the
display to look nice, sending "\b \b" may not be enough, as
there may be characters after the cursor, so it may decide to
redraw the whole line (sending \r, end redrawing the whole line,
and then some escape sequences to position the cursor at the
right position, or if the terminal supports it, send an escape
sequence to the terminal that causes it to scroll to the left
the region of the line that is at the right of the cursor, or

.... this is up to the application).

======================================

Yes, I agree with all about the tty/shell feature you describe,
which I hadn't developed myself for clarity, but do not overlook the
terminal emulator *may* also do things when some keys are depressed.

For example the gnome terminal under Solaris JDS is, by default,
sending delete for backspace, and the escape sequence "<ESC>[3~"
for the Delete key.

===============================================

Yes, upon pressing keys (X <KeyPress> events), it sends a
character or a sequence character to the master side of the
pseudo terminal, according to internal translation table mapping
X event <--> string

It is not involved in the erasing deleting. It sends the
characters corresponding the pressed keys to the pseudo-terminal
driver and it relies on the pseudo-terminal driver to update its
display.

If you press <Backspace>, you generate a "Backspace" KeyPress X
event, that causes gnome-terminal to send the 0x7f character to
the master side of the pseudo-terminal. Depending on its current
settings (of echo, echoe, echoctl, icanon, erase char) and
internal state (whether the internal line edition buffer is
empty or not), the pseudo-terminal driver will or will not send
the ^? to the slave side (to the application), and will or will
not send "\b \b" or "^?" or DEL to gnome-terminal for display.

Upon, hitting <Delete>, gnome-terminal will send the 4 chars
"<ESC>[3~" to the pseudo-terminal. Probably neither of those is
a special character to it, so it will either discard (-cread)
them, or send it to the application, or append it to its internal
buffer... and either send <ESC>[3~ or ^[[3~ or nothing for
display to gnome-terminal. ^[[3~ may be a valid escape sequence
for gnome-terminal, but I doubt so, so in the first case (stty
echo -echoctl), nothing will be displayed (and further <Del>s
will put some mess).

===================================






0 0
原创粉丝点击