Section 21.1. Capability Databases


21.1. Capability Databases

The actions controlled by these escape sequences are often called capabilities. Some escape sequences are shared among many terminals, and many of those sequences are specified in the ANSI X3.64-1979 standard. Nearly all color-capable terminals use the same sequences to choose colors to display. However, many terminals have wildly different escape sequences. For instance, on a Wyse 30 terminal, pressing F1 sends the sequence ^A@\r, whereas pressing F1 on your Linux console sends the sequence ^[[[A. Similarly, to move the cursor up on a Wyse 30, you send it a ^K character; to move the cursor up on the Linux console, send the ^[[A sequence. To write a program that can work on either terminal, you clearly need a way to abstract these differences, which would allow you to program with the capabilities rather than the raw character sequences.

The most common programming library that provides this abstraction is called curses,[1] documented in Programming with curses [Strang, 1991A]. It has two important conceptual levels. The first level provides functions that (generally speaking) send one escape sequence to accomplish one action, such as move the cursor up, move it down, scroll the screen, and so on. The second level provides a concept of "windows": independent screen areas that can be manipulated separately, with curses quietly figuring out the shortest character sequences to send to effect the changes you request.

[1] The curses library is defined by X/Open. The implementation included in Linux is currently base-level conformant with X/Open XSI Curses.

The curses library determines which sequences to send to the terminal by consulting a database that, for each terminal, maps capability names to the strings that need to be sent.

Linux, like all modern Unix systems, provides two databases that describe terminals in terms of the capabilities they possess and which escape sequences map to which capabilities. The older database is called termcap (short for terminal capabilities) and is kept in one large ASCII flat file called /etc/termcap. This file has become unwieldy; it has grown to approximately half a megabyte. The newer database is called terminfo (short for terminal information) and is kept in many binary files, one per terminal, normally in subdirectories of the /usr/lib/terminfo directory.

Within each database, the capability information for each terminal is indexed by one or more unique names. Both databases use the same name for the same terminal. For instance, the Linux console is called linux in both termcap and terminfo. You tell programs which terminal entry to use by setting the TERM environment variable. When writing programs that use termcap and terminfo, you rarely need to look at the TERM environment variable; usually, the low-level libraries for accessing the termcap or terminfo databases automatically take care of that.

However, if you wish to do Linux-specific optimizations, especially if you want to use some of Linux's unusual capabilities that are not described in the capability databases, you can check the TERM environment variable with code like this:

 if (!strcmp("linux", getenv("TERM"))) {     /* should be a Linux console */ } else {     /* handle as a normal serial terminal */ } 

Also, if you wish to write programs in programming languages without easy access to curses or some other terminal abstraction library, you may find it convenient to use this documentation.

Of course, that your terminal type is linux does not guarantee that your program is running on a local terminal. If the terminal type is linux, you know you have access to the escape sequences documented in this chapter, but you do not know if you have access to the vcs devices (documented later in this chapter) or ioctl() s. POSIX specifies a ttyname() function that you can use to find the name of the device file for the controlling terminal. On a Linux system, the virtual consoles are named /dev/ttyn for n between 1 and 63 (/dev/tty0 is always the current console).

Complete documentation for the termcap and terminfo systems is available in termcap & terminfo [Strang, 1991B]. Eric Raymond currently maintains the termcap and terminfo databases, which he makes available via the Web at The source code to ncurses (new curses, the implementation of curses used on Linux) includes an introduction to curses programming in the misc/ncurses-intro.html file.


    Linux Application Development
    Linux Application Development (paperback) (2nd Edition)
    ISBN: 0321563220
    EAN: 2147483647
    Year: 2003
    Pages: 168

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: