Terminals differ. Manufacturers produce a variety of terminals, each one including a particular set of features for a certain price. There are new terminals and old, smart terminals and dumb ones, terminals with big screens and terminals with small screens, printing terminals and video displays, and terminals with all sorts of special features.
Differences between terminals do not matter much to programs like cat (25.2) or who (51.4) that use the terminal screen as a sort of typewriter with an endless scroll of paper. These programs produce sequential output and do not make use of the terminal's special features; they do not need to know much to do their job. Only programs such as screen editors, which make use of screen-handling features, need to know about differences between terminals.
In the late 1970s, Bill Joy created the vi (30.2) text editor at U.C. Berkeley. Like all screen-oriented editors, vi uses the terminal screen non-sequentially. A program performing non-sequential output does not just print character after character, but must manipulate the text that was sent before, scroll the page, move the cursor, delete lines, insert characters, and more. While it would be possible to keep redrawing the screen in its entirety, many features are provided in hardware or firmware by the terminal itself, and save too much time and trouble to be ignored.
The first version of vi was written specifically for Lear Siegler ADM3a terminals. vi was such an improvement over line-oriented editors that there was great demand to port vi to other brands of terminals. The problem was that each terminal had different features and used different control codes to manipulate the features that they did have in common.
Rather than write separate terminal drivers (42.1) for each terminal type, Bill Joy did something very clever, which all UNIX users now take for granted. He wrote a version of vi with generic commands to manipulate the screen instead of hardcoding the control codes and dimensions for a particular terminal.
The generic terminal-handling mechanism Joy came up with had two parts: a database describing the capabilities of each of the terminals to be supported, and a subroutine library that allows programs to query that database and to make use of the capability values it contains. Both the library and the database were given the name termcap, which is short for terminal capabilities.
At this point, users take for granted the fact that you can use just about any terminal with a UNIX system and use screen-oriented programs like vi without any problem. But it is really quite remarkable!
The termcap database is contained in a single text file, which grew quite large over the years to include descriptions of hundreds of different terminals. To improve performance, AT&T later introduced a database called terminfo, which stores terminal descriptions, in compiled form, in a separate file for each terminal.
If a program is designed to use termcap or terminfo, it queries an environment variable called TERM (5.10) to determine the terminal type, then looks up the entry for that terminal in the terminal database, and reads the definition of any capabilities it plans to use into external variables. Programs that use termcap or terminfo range from screen editors like vi and emacs (32.1), which use the complete terminal description, to a program like clear (22.18), which needs to know only one capability (the escape sequence to clear the screen). Other programs include more (25.3), pg, rogue, tset (5.3), ul, and nroff (43.13).
- from O'Reilly & Associates' termcap & terminfo, Chapter 1