Skip to contentSkip to conversation

tty canonical mode: nicer erase behaviour

PATCH In review [email protected] 5 participants 7 messages Sep 23, 2001 – Sep 24, 2001

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty canonical mode: nicer erase behaviour

problem rationale
-----------------

One of the long-standing problems preventing Unix from being a user-friendly desktop OS is its handling of erase keys. There are often two such keys on a keyboard (Backspace and Delete), and which one works depends very much on context -- many text editing programs will only accept one of the erase-related characters (^H and ^?), and the mapping from keys to characters is terminal-dependent. Unfortunately, any solution to this problem has to be implemented in each program that faces this problem.

Theoretically every program should be able to determine which erase character to accept by looking at terminfo, but that's very cumbersome, particularly in programs that might not otherwise need to use terminfo. It also utterly fails in programs that don't know what kind of terminal they are interacting with. The more practical solution, therefore, is that the program should accept *both* ^H and ^? as erase characters. Look at bash's and zsh's command line editors for examples of this approach.

One of the programs that exhibits the ^H/^? problem is the tty line discipline in the Linux kernel. It falls into the category of programs that don't know (and don't care) what kind of terminal they are interacting with. This patch implements the accept-both-characters solution.

patch rationale
---------------

The exact semantics changed are: in canonical mode, if IEXTEN is on and the ERASE character is set to either ^H or ^?, then both ^H and ^? are treated as ERASE characters. The standard single-erase-character behaviour is followed if IEXTEN is off (which prohibits non-POSIX extended behaviour) or if the ERASE character is neither ^H or ^? (indicating that the user actually wants erase behaviour other than the usual use of the erase keys).

At first glance, a more obvious way to implement handling of both erase characters would be to have an ERASE2 character setting in the tty state, much as there is EOL and EOL2. However, this solution would suffer some annoying problems. It would rely on the user setting the two erase characters properly in order to take advantage of it. Even if they're set to ^H and ^? by default, user code that tries to set ERASE based on the terminal type (and doesn't know about ERASE2) might end up with ERASE and ERASE2 set to the same character, breaking the solution. Also, code (and users) that doesn't know about ERASE2 would get surprising results if they try to set ERASE to something other than ^H/^?. All of this points to it being preferable not to change the tty settings interface, but only change the behaviour to this preferable form.

the patch
---------

This patch is based on kernel version 2.4.8. It will also apply cleanly up to at least kernel version 2.4.10-pre14.

char/n_tty.c renamed from char/n_tty.c.orig +25 -13
@@ -329,9 +329,11 @@
 	}
 }
 
-static void eraser(unsigned char c, struct tty_struct *tty)
+enum kill_type { ERASE, WERASE, KILL };
+
+static void eraser(int kill_type, unsigned char cc, struct tty_struct *tty)
 {
-	enum { ERASE, WERASE, KILL } kill_type;
+	unsigned char c;
 	int head, seen_alnums;
 	unsigned long flags;
 
@@ -339,11 +341,7 @@
 		/* opost('\a', tty); */		/* what do you think? */
 		return;
 	}
-	if (c == ERASE_CHAR(tty))
-		kill_type = ERASE;
-	else if (c == WERASE_CHAR(tty))
-		kill_type = WERASE;
-	else {
+	if (kill_type == KILL) {
 		if (!L_ECHO(tty)) {
 			spin_lock_irqsave(&tty->read_lock, flags);
 			tty->read_cnt -= ((tty->read_head - tty->canon_head) &
@@ -359,13 +357,12 @@
 			tty->read_head = tty->canon_head;
 			spin_unlock_irqrestore(&tty->read_lock, flags);
 			finish_erasing(tty);
-			echo_char(KILL_CHAR(tty), tty);
+			echo_char(cc, tty);
 			/* Add a newline if ECHOK is on and ECHOKE is off. */
 			if (L_ECHOK(tty))
 				opost('\n', tty);
 			return;
 		}
-		kill_type = KILL;
 	}
 
 	seen_alnums = 0;
@@ -392,7 +389,7 @@
 				}
 				echo_char(c, tty);
 			} else if (kill_type == ERASE && !L_ECHOE(tty)) {
-				echo_char(ERASE_CHAR(tty), tty);
+				echo_char(cc, tty);
 			} else if (c == '\t') {
 				unsigned int col = tty->canon_column;
 				unsigned long tail = tty->canon_head;
@@ -590,9 +587,19 @@
 		}
 	}
 	if (tty->icanon) {
-		if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
-		    (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
-			eraser(c, tty);
+		if (c == ERASE_CHAR(tty) ||
+		    (L_IEXTEN(tty) &&
+		     (ERASE_CHAR(tty) == 8 || ERASE_CHAR(tty) == 127) &&
+		     (c == 8 || c == 127))) {
+			eraser(ERASE, c, tty);
+			return;
+		}
+		if (c == KILL_CHAR(tty)) {
+			eraser(KILL, c, tty);
+			return;
+		}
+		if (c == WERASE_CHAR(tty) && L_IEXTEN(tty)) {
+			eraser(WERASE, c, tty);
 			return;
 		}
 		if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
@@ -822,6 +829,11 @@
 			set_bit('\n', &tty->process_char_map);
 			set_bit(EOL_CHAR(tty), &tty->process_char_map);
 			if (L_IEXTEN(tty)) {
+				if (ERASE_CHAR(tty) == 8 ||
+				    ERASE_CHAR(tty) == 127) {
+					set_bit(8, &tty->process_char_map);
+					set_bit(127, &tty->process_char_map);
+				}
 				set_bit(WERASE_CHAR(tty),
 					&tty->process_char_map);
 				set_bit(LNEXT_CHAR(tty),

-zefram

#1 lore raw
Quoted 2 lines

One of the long-standing problems preventing Unix from being a user-friendly desktop OS is its handling of erase keys. There are

Not a kernel space issue

Quoted 3 lines

often two such keys on a keyboard (Backspace and Delete), and which one works depends very much on context -- many text editing programs will only accept one of the erase-related characters (^H and ^?), and the

They do different things, they are different keys.

Erase character policy is precisely defined by posix. Fix problem apps. Debian set a policy on this a long time back and have done wonders since

#2 lore raw
In reply to [email protected]

On Sun, Sep 23, 2001, Alan Cox wrote about "Re: [PATCH] tty canonical mode: nicer erase behaviour":

Quoted 2 lines

Erase character policy is precisely defined by posix. Fix problem apps. Debian set a policy on this a long time back and have done wonders since

Just too bad Debian's policy is to make ^? the erase character - pretty much the opposite of what most Unix users used before that. Pretending ASCII BS (^H) doesn't exist any more is an interesting exercise, but it isn't easy to change habits and standards that existed for a couple of decades... The same problem exists for the ASCII DEL (^?) which was also used in many Unix systems, but usually as a intr key, not a "delete-forward" type of thing...
[see http://www.debian.org/doc/debian-policy/ch-opersys.html#s10.8
for the mentioned Debian policy]

Debian's solution isn't a silver bullet, in my opinion... It just means the ^H/^? confusion stops being a problem in a stand-alone system (if all your applications and configuration files come as defaults from Debian, they are consistent) but it just increased the mess when you log in from one system type to another (one of them being none-Linux Unix)...

P.S.
The only relation any of this has to the kernel is the behavior of the "cooked" line discipline, as Zefram already said. I think I like his erase2 idea better than his forget-the-difference-between-^H-and-^? idea. However, this former idea will work well only if applications like ssh, for example, will know how to propegate erase2, and they currently don't. But this is again not really a kernel issue...

Signature · 4 lines
Nadav Har'El                        |       Sunday, Sep 23 2001, 7 Tishri 5762
[email protected]             |-----------------------------------------
Phone: +972-53-245868, ICQ 13349191 |Experience is what causes a person to
http://nadav.harel.org.il           |make new mistakes instead of old ones.
#3 lore raw
Quoted 5 lines

Debian's solution isn't a silver bullet, in my opinion... It just means the ^H/^? confusion stops being a problem in a stand-alone system (if all your applications and configuration files come as defaults from Debian, they are consistent) but it just increased the mess when you log in from one system type to another (one of them being none-Linux Unix)...

Thats in many ways a design flaw in the protocols. Original telnet has IAC sequences to send a "delete" regardless of keymapping policies that are not known at end points. X also does it right with the keysyms. Ssh seems to lack this

#4 lore raw
In reply to Nadav Har'El

[email protected] (Nadav Har'El) wrote on 23.09.01 in <[email protected]>:

Quoted 2 lines

Just too bad Debian's policy is to make ^? the erase character - pretty much the opposite of what most Unix users used before that. Pretending

Actually, the main argument for chosing this was that this *was* the traditional behaviour - as seen by vtxxx terminals and traditional Unix applications like emacs. (If ^H was the traditional erase, how come emacs wants ^H for help? Doesn't add up.)

MfG Kai

#5 lore raw
In reply to Nadav Har'El

On Sun, Sep 23, 2001, Alan Cox wrote about "Re: [PATCH] tty canonical mode: nicer erase behaviour":

Quoted 4 lines

Thats in many ways a design flaw in the protocols. Original telnet has IAC sequences to send a "delete" regardless of keymapping policies that are not known at end points. X also does it right with the keysyms. Ssh seems to lack this

Consider the letter "a". There is no such problem of "keymapping policies" or how to send it on telnet or ssh, because ASCII, in 1965, (or even before that), standardized it. The last two characters in that acronym stand for "Information Interchange", of course.
Anyway, ASCII's character 010 (^H) is called "BS", i.e., backspace. What's so wrong with using that as the standard way to send a backspace? Is it because someone decided that it would be cool to have help be summoned in Emacs with a ^H? Next thing we know there'll be someone annoyed by the fact that the Escape key sends ^[ and ruins his ability to map Control-[ to something in Emacs (running on a tty), and we'll need to change the way Escape is coded too?

Of course there are opposite arguments, with people calling their backspace key a "rubout" and then claiming it is justified to use the ASCII rubout character (0177, DEL) for it. Which makes this whole situation even uglier :(

Signature · 4 lines
Nadav Har'El                        |       Monday, Sep 24 2001, 7 Tishri 5762
[email protected]             |-----------------------------------------
Phone: +972-53-245868, ICQ 13349191 |Hardware, n.: The parts of a computer
http://nadav.harel.org.il           |system that can be kicked.
#6 lore raw
In reply to [email protected]
Quoted 7 lines · Nadav Har'El

Just too bad Debian's policy is to make ^? the erase character - pretty much the opposite of what most Unix users used before that. Pretending ASCII BS (^H) doesn't exist any more is an interesting exercise, but it isn't easy to change habits and standards that existed for a couple of decades... The same problem exists for the ASCII DEL (^?) which was also used in many Unix systems, but usually as a intr key, not a "delete-forward" type of thing...

[OT RANT]

Well, it's also fun to log in to a Solaris 2.5.1 ksh (which doesn't talk bind, but just alias and soft-keys) from a Linux xterm which sends funny Esc [3~ sequences. You can "fix" that with "Del sends Del", but you then end up having either Del and Backspace both do Backspace or have their meanings reversed from what the "left array" on the Backspace key suggests. The Linux TTYs behave the same way (sending Esc [3~), and you don't easily fix that without confusing EOF detection or things.

As appreciable a solution to consistently make "Delete" do "delete-forward" and "Backspace" do "delete-backward" would be, as low are the chances to get this done before the year 2039.