Control Codes

The mad programmer strikes again

I have a fascination for old computing standards, in particular the ones that are not completely dead, but still present in present computers, but largely forgotten. One such standard are ANSI escape sequences, which enable rich features in terminal, like coloured text. One aspect of these standards I did not understand were C1 control codes, i.e. control characters in the above code x7F, that is, 8 bit control codes.

Even in today’s Unicode standard character codes 0 to x20 (C0) and x7F to x9F (C1) are reserved for control codes. Except for four code point: space, carriage return, line feed and form feed, the usage of C0 control codes is not recommended by RFC 5198 and the usage of the C1 codes is explicitly forbidden. If you consider how UTF-8 works, this is a shame: 22% of the code-points that can be expressed using a single byte are unused, so are 1.6% of the code-points that can be expressed using two bytes.

While most of the C0 code are related to controlling devices and line parameters, the C1 ones seem more esoteric, they are mostly related to specifications that have died out. The only one that feels vaguely relevant is CSI (Control Sequence Introducer), which sounds like what ANSI would use to send control sequences. Then I realised that there is a thing called 7-Bit Code Extension Technique which basically lets a C1 code be encoded using 7 bit characters. This is done with the sequence ␛ followed by C1 – 0x40. So CSI becomes ␛[, which is basically the preamble of most ANSI sequences.

On Mac OS X, with Terminal.app set up in Unicode/UTF-8 mode, C1 codes are not active, which makes sense, as the only advantage of using C1 codes over their 7 bit extension equivalent is that it saves a byte, except in the UTF-8 Encoding where both use two bytes.

Still I was curious to see what ANSI features are supported by the terminal in Mac OS. One good way to do this is to download vttest and run the various tests. When I was at the university, I used some real VT-220 terminals, so I expected the graphical features of these devices to mostly work. I found that the OS X terminal supports the following features:

  • Basic terminal text styling: bold, blink, underline, reverse video.
  • 256 colours.
  • Double sized text (first time I realised this feature was present).
  • dtterm window manipulation: move, resize, change title.

The way the double size text is implemented is pretty neat: ␛#3 starts a mode where the upper half of the double size text is printed, ␛#4 starts a mode where the lower half of the double size text is printed. So this means that if the code is not supported, you get twice the text instead of single big text. There is also a double height, single width mode with codes 1 and 2, but this not supported by the OS X’s terminal nor by xterm.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: