Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was wondering how the "GUI" of a command line application is communicated over a network. Most of the time, it's quite simple (plain text / input) but sometimes it's more complex (aptitude).

Is it defined by some sort of standard so that anyone can write their own terminal and that all terminal implementations behave in the same way (colors, positioning, etc.)?

share|improve this question

1 Answer 1

up vote 32 down vote accepted

Console programs typically use curses or one of its successors¹ to build the sorts of text user interfaces you're talking about.

These libraries use one of two databases, called termcap and terminfo.² These databases contain maps that tell how to control the many terminal types. The vast majority of the terminal types you'll find defined in these databases didn't survive the days of real terminals, and so are now only of historical interest.

ANSI Terminals

Modern Unix terminal emulators³ use ANSI X3.64 or one of its later variants:

  • ANSI X3.64: A standard for controlling "glass terminals" — as opposed to teletypes — it is based on special sequences of characters which the remote terminal interprets. For instance, if the Unix box wants to tell the terminal to move its cursor to the upper left corner of the screen, it sends the characters ESC [ 1 ; 1 H. The first two characters tell the terminal to expect a control sequence, the 1s are the row and column, and H is the command meaning "move cursor".⁴

    Trivia: many PC BBSes used ANSI codes, too. (Still do, actually.)

  • DEC VT100: A variant of the ANSI terminal standard, it became popular enough that it's still supported by most terminal programs in some form. Sometimes you see this called the VT102 protocol, that being a later cost-reduced — and therefore more popular — version of the VT100 plus all available expansion options built-in.

    The DEC terminal protocols are a backwards-compatible series, extending from the first ANSI-compatible model introduced in 1978 (the VT100) up through the VT500 series models produced by Boundless Technologies after they bought the terminal business from DEC in 1995.

  • xterm: A kind of amalgam of ANSI and the VT-whatever standards. Whenever you're using a GUI terminal emulator like xterm or one of its derivatives, you're usually also using the xterm terminal protocol, typically the more modern xterm-color or xterm-color256 variants.

  • Linux: The Linux console also uses an extended variant of the ANSI terminal protocol, in the same spirit as the xterm protocols. Most of its extensions have to do with the differences between a PC and a glass terminal. For example, the IBM keyboard has some keys not on a DEC VT-whatever. (And vice versa.)

    Like Linux, some Unix systems have their own console terminal protocol, too. There's the scoansi ANSI X3.64 variant for SCO Unixes, for example.

A typical terminal emulator program is something of a mongrel, and doesn't emulate any single terminal model exactly. It might support 96% of all DEC VT escape sequences up through the VT320, yet also support extensions like ANSI color (a VT525 feature) and an arbitrary number of rows and columns. The 4% of codes it doesn't understand may not be missed if your programs don't need those features, even though you've told curses (or whatever) that you want programs using it to use the VT320 protocol. Such a program might advertise itself as VT320 compatible, then, even though, strictly speaking, it is not.⁵

Non-ANSI Terminals

There are a few other notable standards that you still sometimes come across:

  • Wyse: One of the earliest independent producers of glass terminals, Wyse started making glass terminals in the early 1980s, before workstation computing began displacing minicomputers. Although Wyse terminals were able to emulate the VT100 and other popular terminal protocols, they also had their own native codes.

  • IBM 3270: Although this is not strictly a "Unix" terminal type, the need to connect Unix systems to IBM mainframes led to the creation of IBM 3270 series terminal emulator programs, which you can still find in use. Emulators for the later IBM 5250 series terminals are also fairly common, most often used these days for connecting to AS/400 and System i minicomputers.

  • Tektronix 4014: Before PCs and workstations largely displaced glass terminals and thus made bitmapped graphics a standard feature, there were expensive graphics terminals, which drew graphics on the screen in response to text commands similar to the escape sequences described above. Probably the most popular of these was the Tektronix 4010 series.

    They were quite fun to use. You could write a program that drew a graphic, but instead of simply running it, you could redirect its output to a file:

    $ ./my4014program > my-neat-graphic
    

    You could then send that file to someone else, and they could cat it on their terminal to see the graphic without having your program. Part of the charm was how slow these terminals were in drawing, so you could watch the graphic build up over several seconds.

Working With Unix Terminal Emulation Today

You can find out which terminal standard you're asking libraries like curses to use by looking at the TERM environment variable:

$ echo $TERM
xterm-color

When you ssh to another system, the TERM variable is carried along so the remote Unix box knows how to communicate with your local terminal.

Because so many of these protocols are ANSI X3.64 variants, and because the ubiquitous ASCII and UTF-8 character coding standards take care of so much else, an incorrect TERM variable isn't typically catastrophic. Things that tend to break are extended keys like Home and Page Up, Alt-whatever key combinations, and typographical display features like color, boldface, etc.


Footnotes:

  1. Most commonly, ncurses.

    There are also outright competitors to the curses API, such as S-Lang.

  2. AT&T promulgated terminfo as a replacement for BSD's termcap database, and it was largely successful in replacing it, but there are still programs out there that still use the old termcap database. It is one of the many BSD vs. AT&T differences you can still find on modern systems.

    My OS X box doesn't have /etc/termcap, but it does have /usr/share/terminfo, whereas a standard installation of FreeBSD is the opposite way around, even though these two OSes are often quite similar at the command line level.

  3. minicom, xterm, mintty, GNOME Terminal, OS X Terminal, etc.

  4. Properly-written Unix programs don't emit these escape sequences directly. Instead, they use one of the libraries mentioned above, telling it to "move the cursor to position (1,1)" or whatever, and the library emits the necessary terminal control codes.

  5. Old text terminals had a lot of strange features that didn't get a lot of use by programs, so many popular terminal emulator programs simply don't implement these features. Common omissions are support for sixel graphics and double-width/height text modes.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.