How and where is $TERM interpreted?

13,881

Solution 1

$TERM is read and interpreted by the terminfo system. terminfo also refers to the database of terminal descriptions which you can find on most systems in /usr/share/terminfo. $TERM must match one of the entries in that database. There was also an older library called termcap which had fewer capabilities, but terminfo has replaced it. In modern systems, terminfo is part of the ncurses library.

Applications usually either fetch terminal capabilities directly using library functions like tigetstr() or they use higher level curses interfaces to manage the layout of the screen. Either way, $TERM and the terminfo database will be consulted.

Solution 2

The TERM variable is interpreted by each application, via system libraries. Its value is a name which is looked up in a database. Depending on the Unix flavor and its age, the database can be either termcap (traditional, not used much nowadays) or terminfo (modern, inasmuch as it was meant as an improvement on termcap, and used by most systems nowadays).

Both the termcap and terminfo databases associate capability names with values. Capabilities are either descriptions of what the terminal can do (number of lines, ability to underline, etc.) or strings that can be exchanged with the terminal (escape sequences to format text, move the cursor around, etc., and in the other direction escape sequences sent by function keys). You can look at man 5 termcap and man 5 terminfo to see what capabilities are known on your system.

For example, when you press Ctrl+L to redraw the screen, it reads capabilities in the terminal database to find out what escape sequences it must use to move the cursor around. If TERM is not set or incorrectly set, screen has no way to know how to move the cursor around.

You can use the tput command to retrieve entries in the terminfo database. For example tput lines prints out the number of lines on the terminal. tput clear clears the screen (because its output is getting printed on the terminal); to see what the corresponding escape sequence is, print it out in a readable form, e.g. tput clear | cat -v.

The number of colors has quite a bit of history that makes it not work as it should by rights: terminal emulators tend to underreport their number of colors, to avoid breaking some applications. See tmux, TERM and 256 colours support for more on this, especially in the context of tmux. The difference between xterm-256colors and xterm is that the former reports 256 colors in terminfo while the latter reports the traditional 8.

Solution 3

TERM, by convention refers to a terminal description. Originally this named a section of a termcap text-file (starting in the late 1970s). In the mid-1980s, terminfo was introduced as a compiled (binary) file which saved time when obtaining the terminal description. While both are available for all Unix-like platforms, termcap is rarely used today except as an emulation using terminfo.

For both of these data formats, applications usually extract data from the terminal database with reusable programming libraries. The terminfo programming library is usually part of the higher-level curses library although it may be provided (e.g., optionally as in ncurses) as a separate library file. Whether the terminfo library is provided separately or not, in these cases it is considered part of the curses library. (There are also a few other higher-level libraries such as slang).

The terminal database entry for each terminal contains properties referred to as capabilities. They tell the curses library (or applications using termcap/terminfo directly) how to do useful operations such as clear the screen. For most terminals that is an escape sequence. A few terminals may not support an escape sequence for this purpose; there are other capabilities which may be combined by the curses library to clear the screen (such as clearing each line). Not all capabilities are escape sequences. There are boolean and number capabilities as well, e.g., to tell whether a feature is supported, and how large something is (such as screen size).

Each application which uses termcap/terminfo uses the corresponding library to retrieve the terminal description, as well as to perform operations such as substituting parameters into certain capabilities. For example, most terminals provide a capability to move the cursor by a given number of columns or rows from its current location. The tparm (or tiparm) functions substitute the number into the capability to obtain the actual escape sequence.

The curses library has command-line applications which maintain the terminal database (tic, infocmp) and some which are used in shell scripts for querying the terminal database or performing low-level operations with terminal capabilities (tput, tset/reset).

There are unconventional applications which use TERM without using the terminal database. Most of these simply hardcode their behavior (such as GNU grep, groff, and the links/links2/elinks textual web browsers), while a few have what amounts to their own terminal database (such as GNU ls), but using different rules and behavior.

Back to the question(s):

  • So where is this variable interpreted and allows for example resetting my terminal screen using CTRL+l if I set the right value there?

    The application and the underlying libraries interpret this value. For controlL, that may be done for bash in the readline library (which uses a termcap programming interface).

  • Who checks for example which colors are supported (xterm vs xterm-256color)?

    The terminal database stores the number of colors as a capability, along with capabilities for setting the foreground and background colors and resetting colors. Some applications combine these capabilities with other information (such as a developer's assertion that xterm is "really" xterm-256color).

  • The shell?

    Most shells use a termcap programming interface to obtain the terminal information. However, they are applications which have their own behavior (not necessarily the same as curses).

  • The application or a library like ncurses?

    (see above: shells are a particular type of application)

  • And where are the possible values / terminal types defined?

    Usually that is in a terminal database shared by applications using the curses or slang libraries. Some applications are hardcoded or use a private database.

Further reading:

Share:
13,881

Related videos on Youtube

muffel
Author by

muffel

Updated on September 18, 2022

Comments

  • muffel
    muffel over 1 year

    I'm wondering how some terminal magic works internally.

    While playing around with docker containers, the environment variable $TERM was not set. This led to strange-looking console applications like vim and tmux, but also to CTRL+l (clear screen) being ignored.

    I'm pretty sure that all feature like partial screen updates, colors, commands like screen reset etc. are realized using escape codes, right?

    So where is this variable interpreted and allows for example resetting my terminal screen using CTRL+l if I set the right value there? Who checks for example which colors are supported (xterm vs xterm-256color)? The shell? The application or a library like ncurses? And where are the possible values / terminal types defined?