How termcap and terminfo Describe Terminals

The termcap and terminfo databases () describe the capabilities of terminals using a rather obscure and compact language. At this point, the ASCII terminal market has slowed down and standardized, so it is not as essential as it used to be to write new terminal entries. However, there are still times when it's useful to know how to read an entry. For example, you may want to use particular capabilities in a shell program () or in a function key map ().

We won't give you a detailed list of all of the possible capabilities - that you can get from the termcap or terminfo manual page on your system. However, we do want to give you an introduction to the language of termcap and terminfo.

Here's a simplified entry for the Wyse Technology Wyse-50 terminal. The capabilities described here are only a subset sufficient to introduce the basic syntax of the language:

# incomplete termcap entry for the Wyse WY-50 n9|wy50|WY50| Wyse Technology WY-50:\ :bs:am:co#80:li#24:\ :up=^K:cl=^Z:ho=^^:nd=^L:cm=\E=%+ %+ :

And here is the corresponding terminfo source file:

# incomplete terminfo entry for Wyse WY-50 wy50|WY50|Wyse Technology WY-50, am, cols#80, lines#24, cuu1=^K, clear=^Z, home=^^, cuf1=^L, cup=\E=%p1%'\s'%+%c%p2%'\s'%+%c,

The backslash () character is used to suppress the newline in termcap. termcap entries must be defined on a single logical line, with colons (:) separating each field. terminfo does not require the entry to be on a single line, so backslashes are not necessary. In terminfo, commas are used as the field separator.

The language certainly is not verbose! However, if we work through it methodically, it might begin to make sense.

There are three types of lines in a termcap or terminfo file: comment lines, lines that list alias names for the terminal, and lines that specify terminal capabilities.

There are three types of capability:

Now the Wyse-50 example should make more sense. First termcap:

Figure 41.1: A Simplified termcap Entry

Figure 41.1

Now terminfo:

Figure 41.2: A Simplified terminfo Entry

Figure 41.2

The examples demonstrate all three kinds of capabilities: Boolean, numeric, and string.

The first two capabilities in the termcap entry, and the first capability in the terminfo entry, are Boolean.

The next two capabilities are numeric.

You will find that 80 characters and 24 lines is the most common screen size but that there are exceptions. Eighty characters was originally chosen because it is the width of a punch card, and 24 lines was chosen to take advantage of cheap television screen technology.

The remainder of the fields in the Wyse-50 entry contain string capabilities. The first four of these are fairly simple:

Special Character Codes

No doubt the symbols ^K, ^Z, ^^, and ^L shown above are familiar to you. A caret (^) followed by a letter is a convention for representing an unprintable control character generated by holding down the CONTROL (CTRL) key on the keyboard while typing another. Note that control characters are entered into a terminal description as two characters by typing the caret character (^) followed by a letter, rather than by inserting the actual control character.

Both termcap and terminfo use other codes to write other unprintable characters, as well as characters that have special meaning in termcap or terminfo syntax. The other codes, most of which should be familiar to C developers, are listed in Table 41.1.

Termcap and Terminfo Special Character Codes
Code Description Comment
E escape termcap and terminfo
e escape terminfo only
^x control-x where x is any letter
n newline
r return
t tab
b backspace
f formfeed
s space terminfo only
l linefeed terminfo only
xxx octal value of xxx must be three characters
exclamation point ! shell history uses !
the character : termcap uses ordinary : as separator
null for null does not work
null terminfo only
^ caret terminfo only
backslash terminfo only
comma terminfo only
: colon terminfo only

Encoding Arguments

The last capability in the Wyse-50 example is the most complicated. cm= (termcap) and cup= (terminfo) specify the cursor motion capability, which describes how to move the cursor directly to a specific location. Since the desired location is specified by the program at run-time, the capability must provide some mechanism for encoding arguments. The program uses this description to figure out what string it needs to send to move the cursor to the desired location.

Because we aren't telling you how to write termcap or terminfo entries, but just to read them, all you need to know is that the percent sign (%) is used for encoding, and when it appears in a terminal entry, the capability is using run-time parameters.

If you need to write an entry, see Anonymous' termcap & terminfo.

- JS, TOR