Uh... no. No no no. C irks have nothing to do with this - I don't think so, at least.

Within terminal emulators, there are 3 character table sets, G0, G1 and G2, each can contain a set of character glyphs, and one of these is loaded into GL so it's the one that's actively used. G0 is the set you typically have loaded. Certain escape sequences can switch the character table into and out of different modes; the mode you're describing above is called the "box drawing" or "line drawing" or "special characters" mode. This mode remaps certain characters to different glyphs so that operations such as drawing of box characters can be done by simply echoing ASCII characters to the screen.

Different environments require different behavior to select these drawing characters and put them into play. In X11, one needs to switch the mode desired into G0. At the console, G1 is typically already loaded with the box drawing character set, so all that's required is to switch GL into G1.

Where ^[ is meant to mean the escape character, ASCII code 27...

The sequence ^[(0 switches G0 into line drawing mode, as applicable for X11, and can be sent to the terminal via

echo -e '\e(0'

The sequence ^[(B switches G0 back into standard or normal mode, and can similarly be sent via

echo -e '\e(B'

In console mode, the key combination CTRL+N will send the ^N sequence to the terminal (defined in ANSI as SO, defined in POSIX to mean LS1), switching it to the G1 character set, and CTRL+O will send what is defined in ANSI as SO but defined in POSIX as LS0, switching it back to G1. However, these key combinations are interpreted to mean other things when pressed in a terminal environment, and only take effect when their character code counterparts (SO or LS1, which is 14 decimal, 0xE hexadecimal, or 016 octal, and SI or LS0, which is 15 / 0xF / 017), are echoed to the terminal. To achieve this, try the commands below (where words contained in < and > are intended to be pressed as key combos). To echo SO, send

echo <CTRL+V><CTRL+N>

And to echo SI, send:

echo <CTRL+V><CTRL+O>

Therefore, When you cat a binary stream of data, there is a high chance the sequence 27, 40, 48 (switch G1 into GL), 27, 40, 66 (switch G0 into GL), 14 (SO) or 15 (SI) is likely to occur, in which case your terminal obeys these perfectly valid escape sequences, switching into or out of the box drawing character set and displaying whatever data proceeds these commands using that character set.

The chances of this occuring are threefold in console mode, since thise mode by default requires a 3rd of the chars required by X11 terminal emulators to switch to the box drawing character set, so console mode is more likely to be susceptible to this issue.

It is for this reason that terminals may also clear (the UNIX 'clear' command typically sends a longer sequence, but the sequence 27 99 appears to erase the display) and I have personally seen this sequence sequence sent to my terminal at least once), or exhibit other odd side effects - the result of different escape sequences that are being sent to the terminal.

A fun example:

When in box drawing mode, the following keys are mapped to the following alternate box drawing glyphs:

j bottom right m bottom left k top right l top left q horiz line x vert line

With that knowledge, run the following code and observe how a square box is drawn on the screen.

echo -e '\e(0lqqqqqqqqqqk

x x

x x

x x

mqqqqqqqqqqj\e(B'

Or, a little more spaced out (indentation added purely for conciseness):

echo -e '\e(0' echo lqqqqqqqqqqk echo x x echo x x echo x x echo mqqqqqqqqqqj echo '\e(B'

Yes, that's how ncurses works too, in case you wondered.

Also, "reset" can be replaced by "tput reset" - this sends the same escape sequences (more escape sequences!) as "reset", but doesn't delay at all.

References:

- the "console_codes" manpage

- man dtterm(5), not available in Arch but mirrored at this ancient HP web url: http://h30097.www3.hp.com/docs/base_doc … 00____.HTM

For curious minds:

- Piping the output of something that uses escape sequences through a script like this PHP code (where " \e" is what you want to replace the escape character to):

php -r 'echo str_replace(chr("27"), " \e", `tput reset`);'; echo

is an easy way to investigate what sequences are being sent to the terminal. Simple commands like "clear", "tput reset" ("reset" sends some signals directly to the terminal emulator process itself I think, NOT escape sequences, so is beyond the scope of this post), and so on are good starting points.

- Additionally, you can substitute editing of the output stream of an application via PHP or another language with redirection. Redirecting the output of a program to a file is a good way to poke about inside the file, although beware that the files created with this method should only be looked at with simple editors like nano or e3, which don't try to have a go at figuring out the file content and/or "cleaning" it, like vi(m) or emacs might. NOTE that even when you're redirecting the output of a program to a file, it still accepts input! If your application for example accepts the key F10 to quit it, press F10 after you think your app is done loading, and the command you ran to start the app (for example "mc > mc-output") should quit.

- Ncurses applications are a good place to learn about escape sequences without diving into sourcecode because the ncurses library uses a lot of undocumented escape sequences which do interesting things. You'll almost certainly want to redirect the output of the command to a file as per the method above, and additionally be prepared to do a LOT of digging around inside the output, as the escape sequences will be interspersed with a LOT of other characters, those being the program's perfectly normal output.

Whew, that was quite a post. I'm glad that I had a good memory when I was somewhere between 8 to 11, when I learnt about DOS escape sequences. That was 9 to 6 years ago, and the knowledge has helped me handle UNIX's infentesimally more complex escape sequences and find them actually learnable.

EDIT: Updated a bit of the text, fixed a typo, added more info

-dav7

Last edited by dav7 (2008-09-23 23:47:25)