Subject: Escape sequences (was: Re: home end keys do not work)
To: Lubos Vrbka <shnek@tiscali.cz>
From: Richard Rauch <rkr@olib.org>
List: netbsd-users
Date: 07/13/2004 20:05:54
To answer the question about the tilde:

The short answer is that the tilde is a terminator to the
control sequence.


A longer answer:

Usually, if an escape sequence begins with

  ^[ [

(0x1b 0x5b), that is called a "CSI" (Control Sequence Introducer).
There is also a single-byte CSI (0x9b), but that may not work with
older terminal types.

The CSI may be followed by zero or more semi-colon separated decimal
numbers (parameters to the sequence) and finally some kind of
terminator.

For example, "mode" changes (including color) have a terminator of
"m".  Where supported, you can set the foreground color with a
number between 30 and 37, and set the background with a number
between 40 and 47, for example:

  "\x1b[31;42m"

is a C-style string for the ANSI color selection of "color number
1 for the foreground, and color number 2 for the background".  The
order of the numbers is not important and you can opt to only
specify one (say only the foreground, if the background color is
to remain unchanged).  (Or you can specify a long string of
mode-related parameters in any order.)  There is a standard color
scheme (0 is black, 1 is red, etc.), though you cannot always count
on that scheme; the Amiga, for example, let the user change these
at will in most terminal emulators.

You can think of the terminator as the actual "action" code that
says what to do with the parameters.


Digits and semi-colons are not terminators.  Offhand, I'm not sure
if there are any other ASCII characters that are not generally
terminators, but the upper- and lower-case letters are terminators,
as are at least some punctuation symbols---such as the tilde.

I am not aware of any formal limit to the number or size of the
numeric parameters, though of course real terminals are finite.
Since the action to take isn't known until you get to the end of
the sequence, it would be bothersome to let parameters interact.
("30;31" could be row/column parameters for a cursor-positioning
sequence, or they could be color codes with the "31" overwriting
the "30" for example; you don't know which until you get the
terminator.)  That's not to say, of course, that you couldn't do
it; there are a finite number of ways that the sequence could end,
so you could speculatively carry out all possible interactions and
when you get the terminator use that to select the proper
interpretation.

So...maybe more than you wanted to know.  (^&  But it may help you
to understand what the tilde is doing in those sequences.

-- 
  "I probably don't know what I'm talking about."  http://www.olib.org/~rkr/