UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 41.10 Using terminfo Capabilities in Shell Programs Chapter 41
Terminal and Serial Line Settings
Next: 41.12 Finding Out What Characters Your Terminal's Special Keys Send
 

41.11 How termcap and terminfo Describe Terminals

The termcap and terminfo databases (5.2) 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 (41.10) or in a function key map (41.12).

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.

bs

is the termcap backspace capability, which means that the terminal will backspace when sent the CTRL-h (^H) character. There is no terminfo capability directly equivalent to bs, so it is considered obsolete by terminfo and by BSD 4.3 termcap. In place of the bs capability, terminfo would explicitly define CTRL-h as the string to send the cursor left cul1=^H).

am

am is the automargin capability, also known as wraparound. It means that when a line reaches the right edge of the screen, the terminal automatically continues on the next line.

The next two capabilities are numeric.

co#80
cols#80

says that the terminal has 80 columns.

li#24
lines#24

says that the terminal has 24 lines.

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:

up=^K
cuu1=^K

is the up capability; it says that to move the cursor up one line, send the ^K character to the terminal.

cl=^Z
clear=^Z

is the clear capability; it says that to clear the screen, send the ^Z character to the terminal.

ho=^^
home=^^

is the home capability; it says that to move the cursor Home (upper-left corner), send the ^^ character (CTRL-^) to the terminal.

nd=^L
cuf1=^L

is the non-destructive space capability; it says that to move the cursor one space to the right without changing the text, send the ^L character to the terminal.

41.11.1 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 programmers, are listed in Table 41.1.

Table 41.1: Termcap and Terminfo Special Character Codes
CodeDescriptionComment
\Eescapetermcap and terminfo
\eescapeterminfo only
^xcontrol-xwhere x is any letter
\nnewline
\rreturn
\ttab
\bbackspace
\fformfeed
\sspaceterminfo only
\llinefeedterminfo only
\xxxoctal value of xxxmust be three characters
\041exclamation point !C shell history uses !
\072the character :termcap uses ordinary : as separator
\200null\\000 for null does not work
\0nullterminfo only
\^caretterminfo only
\\backslashterminfo only
\,commaterminfo only
\:colonterminfo only

41.11.2 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 O'Reilly & Associates' termcap & terminfo.

- JS, TOR


Previous: 41.10 Using terminfo Capabilities in Shell Programs UNIX Power ToolsNext: 41.12 Finding Out What Characters Your Terminal's Special Keys Send
41.10 Using terminfo Capabilities in Shell Programs Book Index41.12 Finding Out What Characters Your Terminal's Special Keys Send

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System