In Unix and other POSIX-compliant systems that support the POSIX terminal interface, these capabilities are encoded in databases that are configured by a system administrator and accessed from programs via the terminfo library (which supersedes the older termcap library), upon which in turn are built libraries such as the curses and ncurses libraries, by which applications programs use the terminal capabilities to provide textual user interfaces with windows, dialogue boxes, buttons, labels, input fields, menus, and so forth.34 The intention is that this allows applications programs to be independent of actual terminal characteristics. They don't need to hardwire any control codes or escape sequences into their code, and so don't have problems being used on a range of terminals with a range of capabilities.5
Main article: termcap
The termcap (for "terminal capabilities") library was developed for BSD systems. It uses a database stored in the file /etc/termcap. This database consists of a series of records (each of which consists of one or more lines in the file, joined by backslash characters at the ends of each line that continues onto a following one) each of which represents the capabilities of a particular terminal. The fields of the record comprise the terminal type name, or names, followed by a sequence of capabilities, separated by colons.67 The capability fields themselves fall into three groups:
Main article: terminfo
The terminfo ("terminal information") library was developed for System V systems. It uses a database stored in multiple files within a directory, which can be variously (on different Unices and POSIX-compatible systems) /usr/lib/terminfo, /usr/share/terminfo, or even /usr/share/lib/terminfo.12 (Its location isn't even uniform across different distributions of Linux.13)
Unlike the termcap database, the terminfo database is compiled, a machine-readable database that is constructed from a human-readable source file format by a utility program, tic. They can be decompiled from machine-readable form back to human-readable form by another utility program, infocmp.14 The command to output the human-readable form of the "vt100" terminal definition, for example, is:15
The use of a machine-readable format was to avoid the unnecessary overhead, in applications programs using systems such as the termcap library, of repeatedly parsing the database content to read the fields of a record. The use of multiple files was to avoid the similar overhead of parsing the database content to find the database record for the target terminal type. The terminal type name index is, effectively, the Unix/POSIX filesystem's ordinary directory structure. Originally, Unix had severe performance problems with large directories containing many files, and thus terminfo uses a two-level structure, dividing up the directory entries by first letter into a series of subdirectories. More recent filesystem formats used on Unix systems don't suffer as much from such problems (because their on-disc directory structures are no longer simple arrays of entries, but are organized into trees or hash tables) and so the necessity for this design element, that still exists in modern terminfo implementations, has since disappeared.16
Further information: tput
On Unix systems, the tput command is used to look up a specific capability in the system's database, and output it to the command's standard output (which is, presumably, the terminal by which the function denoted by the capability is to be performed).17 One of the simplest operations is clearing the screen. The name of the database field that stores the output sequence for this is clear, so the command arguments to the tput program to clear the screen are 18
Another operation is initializing or resetting the terminal to a known default state (of character attributes, fonts, colours, and so forth). The commands for this are:19
and
Normally the tput command uses the terminal type specified by the TERM environment variable, one of the controlling environment variables of the POSIX terminal interface. This can be overridden, however, to force tput to look up a different terminal type in the database, with a command-line option to the command.20 So, for example, to issue the reset sequence appropriate for the type of terminal named "vt100" in the database (usually a DEC VT100 terminal), irrespective of terminal type specified in environment variables, the command is:
Afzal 2008, p. 419. - Afzal, Amir (2008). UNIX unbounded: a beginning approach (5th ed.). Prentice Hall. ISBN 978-0-13-119449-6. ↩
Frisch 2002, p. 770. - Frisch, Æleen (2002). Essential system administration. A nutshell handbook (3rd ed.). O'Reilly Media, Inc. ISBN 978-0-596-00343-2. https://archive.org/details/essentialsystema00fris_0 ↩
Coffin 1991, p. 115. - Coffin, Stephen (1991). UNIX system V release 4: the complete reference. Osborne McGraw-Hill. ISBN 978-0-07-881653-6. https://archive.org/details/unixsystemvrelea00coff ↩
Coffin 1991, p. 372. - Coffin, Stephen (1991). UNIX system V release 4: the complete reference. Osborne McGraw-Hill. ISBN 978-0-07-881653-6. https://archive.org/details/unixsystemvrelea00coff ↩
Frisch 2002, p. 769. - Frisch, Æleen (2002). Essential system administration. A nutshell handbook (3rd ed.). O'Reilly Media, Inc. ISBN 978-0-596-00343-2. https://archive.org/details/essentialsystema00fris_0 ↩
Kumar 2004, p. 417. - Kumar, Sudhir (2004). Encyclopaedia of Operating System. Anmol Publications PVT. LTD. ISBN 978-81-261-1792-5. ↩
Blum 2008, p. 31. - Blum, Richard (2008). Linux Command Line and Shell Scripting Bible. Bible Series. John Wiley and Sons. ISBN 978-0-470-25128-7. https://archive.org/details/linuxcommandline00blum_0 ↩
Raymond 2004, p. 144–145. - Raymond, Eric S. (2004). The art of Unix programming. Addison-Wesley professional computing series. Addison-Wesley. ISBN 978-0-13-142901-7. https://archive.org/details/The_Art_of_UNIX_Programming ↩
Winsor 2001, p. 244–245. - Winsor, Janice (2001). Solaris 8 Advanced System Administrator's Guide (3rd ed.). Prentice Hall PTR. ISBN 978-0-13-027703-9. ↩