curs_terminfo(3X)
β¨ NAME β¨
okay bestie, welcome to curs_terminfo(3X) from the Library calls section. π This page is all about: del_curterm, putp, restartterm, set_curterm, setupterm, tigetflag, tigetnum, tigetstr, tiparm, tiparm_s, tiscan_s, tparm, tputs, vid_attr, vid_puts, vidattr, vidputs β literally the curses interfaces to the terminfo database. π¦
π SYNOPSIS π
#include <curses.h>
#include <term.h>
TERMINAL * cur_term;
const char * const boolnames[];
const char * const boolcodes[];
const char * const boolfnames[];
const char * const numnames[];
const char * const numcodes[];
const char * const numfnames[];
const char * const strnames[];
const char * const strcodes[];
const char * const strfnames[];
int setupterm(const char * term, int filedes, int * errret);
TERMINAL * set_curterm(TERMINAL * nterm);
int del_curterm(TERMINAL * oterm);
int restartterm(const char * term, int filedes, int * errret);
char * tparm(const char * str, ...);
/* or */
char * tparm(const char * str, long p1 ... long p9);
int tputs(const char * str, int affcnt, int (* putc)(int));
int putp(const char * str);
int vidputs(chtype attrs, int (* putc)(int));
int vidattr(chtype attrs);
int vid_puts(attr_t attrs, short pair, void * opts, int (* putc)(int));
int vid_attr(attr_t attrs, short pair, void * opts);
int tigetflag(const char * cap-code);
int tigetnum(const char * cap-code);
char * tigetstr(const char * cap-code);
char * tiparm(const char * str, ...);
/* extensions */
char * tiparm_s(int expected, int mask, const char * str, ...);
int tiscan_s(int * expected, int * mask, const char * str);
/* deprecated */
int setterm(const char * term);
π DESCRIPTION π
so like, these are the lower-level functions of the curses standard. You must call them if your program deals directly with the terminfo database to handle certain terminal capabilities β like programming function keys, iykyk. For all other functionality? The higher-level curses functions are way more suitable and their use is recommended. periodt.
None of these functions use (or are aware of) multibyte character strings such as UTF-8. π
- Capability names and codes use the POSIX portable character set.
- Capability string values have no associated encoding; they are strings of 8-bit characters.
π± Initialization π±
Call setupterm from your application to have terminfo manage the terminal device. This action initializes the terminal-dependent variables listed in term_variables(3X). (A curses application calling initscr(3X) or newterm(3X) achieves the same result.) Applications can use the terminal capabilities either directly, by object definitions corresponding to capability names and codes (see term_variables(3X)) or by calling the functions documented here. setupterm initializes the terminfo variables lines and columns as described in use_env(3X).
Pass parameterized string capability values through tparm to instantiate them. All terminfo strings (including the output of tparm) should be sent to the terminal device with tputs or putp. Call reset_shell_mode(3X) to restore the terminal modes before exiting. (A curses application calling endwin(3X) achieves the same result.)
Programs that use cursor addressing should emit certain capabilities at specific times. Specifically, output
enter_ca_modeupon startup, andexit_ca_modebefore exiting.
Programs that execute shell subprocesses should
- call
reset_shell_mode(3X)and outputexit_ca_modebefore the shell is called, and - output
enter_ca_modeand callreset_prog_mode(3X)after returning from the shell.
setupterm reads in the terminfo database, initializing the terminfo structures, but does not set up the output virtualization structures used by curses. Its parameters follow.
termβ is the terminal type, a character string. Iftermis null, the environment variableTERMis read.filedesβ is the file descriptor used for getting and setting terminal I/O modes. Higher-level applications usenewterm(3X)to initialize the terminal, passing an output stream rather than a descriptor. In curses, the two are the same becausenewtermcallssetupterm, passing the file descriptor derived from its output stream parameter.errretβ points to an optional location where an error status can be returned to the caller. Iferrretis not null, thensetuptermreturnsOKorERRand stores a status value in the integer pointed to byerrret. A return value ofOKcombined with status of1inerrretis normal. IfERRis returned, examineerrret.1β means that the terminal is a hard-copy type (lacks destructive backspace), and cannot be used for curses applications. The library determines this fact by checking the terminal type's hardcopy (hc) capability.0β means that the terminal could not be found, or that it is a generic type, having too little information for curses applications to run.setuptermdetermines if the entry is a generic type by checking thegeneric_type(gn) capability.-1β means that the terminfo database could not be found. Iferrretis null,setuptermreports an error message upon finding an error and exits. Thus, the simplest call is:
setupterm((char *) NULL, 1, (int *) NULL);
which uses all the defaults and sends the output to stdout.
π§ The Terminal State π§
setupterm stores its information about the terminal in a TERMINAL structure pointed to by the global variable cur_term. If it detects an error, or decides that the terminal is unsuitable (hardcopy or generic), it discards this information, making it unavailable to applications. π
If setupterm is called repeatedly for the same terminal type, it reuses the information. It maintains only one copy of a given type's capabilities in memory. If called for different types, setupterm allocates new storage for each set of terminal capabilities.
set_curterm sets cur_term to nterm, making all of the terminfo Boolean, numeric, and string capabilities use the values from nterm. It returns the old value of cur_term.
del_curterm releases the memory pointed to by oterm. If oterm is the same as cur_term, references to any of the terminfo Boolean, numeric, and string capabilities thereafter may refer to invalid memory locations until setupterm is called again.
restartterm is similar to setupterm, but is intended for use after restoring program memory to a previous state (for example, when reloading an application that has been suspended from one terminal session and restored in another). restartterm assumes that the display dimensions and the input and output options are the same as when memory was saved, but the terminal type and line speed may differ. Accordingly, restartterm saves relevant terminal state, calls setupterm, then restores that state.
π Formatting Output π
tparm instantiates the string str with parameters pi. It returns a pointer to a character string representing str with the parameters applied to β%β expressions within. Application developers should keep in mind some quirks of the interface.
- Although
tparm's actual parameters may be integers or strings, the prototype expectslong(integer) values. - Aside from the
set_attributes(sgr) capability, most terminal capabilities require no more than one or two parameters. - Padding information is ignored by
tparm; it is interpreted bytputs. - The capability string is null-terminated. Use
β\200βwhere an ASCII NUL is needed in the output.
tiparm is a newer form of tparm that uses stdarg.h rather than a fixed-length parameter list. Its numeric parameters are ints rather than longs.
tparm and tiparm assume that the application passes parameters consistent with the terminal description. ncurses provides two extensions as alternatives to deal with untrusted data.
- The
tiparm_sextension is a safer formatting function thantparmortiparm, because it allows the developer to tell the curses library how many parameters to expect in the parameter list, and which may be string parameters. Themaskparameter has one bit set for each of the parameters (up to 9) passed ascharpointers rather than numbers. - The extension
tiscan_sallows the application to inspect a formatting capability to see what the curses library would assume.
π Output Functions π
String capabilities can contain padding, a time delay (accommodating performance limitations of hardware terminals) expressed as $<n>, where n is a nonnegative integral count of milliseconds. If n exceeds 30,000 (thirty seconds), ncurses caps it at that value.
tputs interprets time delays in the string str and acts upon them.
- The
strparameter must be a terminfo string capability or the return value oftparmortiparm. affcntis the number of lines affected, or1if not applicable.putcis aputchar-like function to which the characters are passed, one at a time.
tputs processes each time delay with the delay_output(3X) function, routing any resulting padding characters through this function.
putp calls βtputs(str, 1, putchar)β. The output of putp always goes to stdout, rather than the filedes specified in setupterm.
vidputs displays the string on the terminal in the video attribute mode attrs, which is any combination of the attributes listed in curses(3X). The characters are passed to the putchar-like function putc.
vidattr is like vidputs, except that it outputs through putchar(3).
vid_attr and vid_puts correspond to vidattr and vidputs, respectively. They use multiple parameters to represent the character attributes and color; namely,
attrs, of typeattr_t, for the attributes andpair, of typeshort, for the color pair number.
Use the attribute constants prefixed with βWA_β with vid_attr and vid_puts.
X/Open Curses reserves the opts argument for future use, saying that applications must provide a null pointer for that argument; but see section βEXTENSIONSβ below.
While putp is a lower-level function that does not use higher-level curses state, ncurses declares it in curses.h because System V did so (see section βHISTORYβ below).
π Terminal Capability Functions π
tigetflag, tigetnum, and tigetstr return the value of the capability corresponding to the terminfo cap-code, such as xenl, passed to them. The cap-code for each capability is given in the table column of that name in the βCapabilitiesβ section of terminfo(5).
These functions return special values to denote errors.
okay so like, when you call tigetflag, tigetnum, and tigetstr, they don't just vibe check you β they give back specific numbers when things are off. here's the tea:
tigetflagreturns -1 ifcap-codeis not a Boolean capability, or 0 if it is canceled or absent from the terminal description. π«Άtigetnumreturns -2 ifcap-codeis not a numeric capability, or -1 if it is canceled or absent from the terminal description. πtigetstrreturns(char *)-1ifcap-codeis not a string capability, orNULLif it is canceled or absent from the terminal description. π
β¨ Terminal Capability Names β¨
These null-terminated arrays contain:
- the short terminfo names (βcodesβ),
- the termcap names (βnamesβ), and
- the long terminfo names (βfnamesβ)
for each standard terminfo capability name. π
const char *boolnames[], *boolcodes[], *boolfnames[]
const char *numnames[], *numcodes[], *numfnames[]
const char *strnames[], *strcodes[], *strfnames[]
π Releasing Memory π
Each successful call to setupterm allocates memory to hold the terminal description. As a side effect, it sets cur_term to point to this memory. If an application calls
del_curterm(cur_term);
the memory will be freed. π§
The formatting functions tparm and tiparm extend the storage allocated by setupterm as follows.
- They add the βstaticβ terminfo variables
[a-z]. Before ncurses 6.3, those were shared by all screens. With ncurses 6.3, those are allocated per screen. Seeterminfo(5). πΈ - To improve performance, ncurses 6.3 caches the result of analyzing terminfo strings for their parameter types. That is stored as a binary tree referenced from the
TERMINALstructure. π₯
The higher-level initscr and newterm functions use setupterm. Normally they do not free this memory, but it is possible to do that using the delscreen(3X) function. π
π RETURN VALUE π
Functions that return integers return ERR upon failure and OK upon success. π
In ncurses,
del_curtermfails if its terminal parameter is null. πputpcallstputs, returning the same error codes. πrestarttermfails if the associated call tosetuptermreturnsERR. πsetuptermfails if it cannot allocate enough memory, or create the initial windows (stdscr,curscr, andnewscr). Other error conditions are documented above. πtparmreturns a null pointer if the capability would require unexpected parameters; that is, too many, too few, or incorrect types (strings where integers are expected, or vice versa). π«Άtputsfails if the string parameter is null. It does not detect I/O errors: X/Open Curses states thattputsignores the return value of the output functionputc. β
π NOTES π
The vid_attr function in ncurses is a special case. It was originally implemented based on a draft of X/Open Curses, as a macro, before other parts of the ncurses wide-character API were developed, and unlike the other wide-character functions, is also provided in the non-wide-character configuration. π
β¨ EXTENSIONS β¨
The functions marked as extensions originated in ncurses, and are not found in SVr4 curses, 4.4BSD curses, or any other previous curses implementation. π¦
ncurses allows opts to be a pointer to int, which overrides the pair (short) argument. π
π PORTABILITY π
Except for setterm, X/Open Curses Issue 4 describes these functions. It specifies no error conditions for them. π
SVr4 describes a successful return value except where βotherwise notedβ as βan integer value other than ERRβ. πΈ
π Header Files π
On legacy curses systems, include curses.h and term.h in that order to make visible the definitions of the string arrays storing the capability names and codes. π
π§ Compatibility Macros π§
ncurses implements a few macros for early System V curses compatibility (see section βHISTORYβ below). They include crmode, fixterm, gettmode, nocrmode, resetterm, saveterm, and setterm. π₯
In SVr4, these are found in curses.h, but except for setterm, are likewise macros. The one function, setterm, is mentioned in the manual page. It further notes that setterm was replaced by setupterm, stating that the call
setupterm(term, 1, (int *)0)
provides the same functionality as
setterm(term)
and discouraging the latter for new programs. π
π Legacy Data π
setupterm copies the terminal name to the array ttytype. This behavior is not specified by X/Open Curses, but is assumed by some applications. π
Other implementations may not declare the capability name arrays. Some provide them without declaring them. X/Open Curses does not specify them. π
Extended terminal capability names, as defined by βtic -xβ, are not stored in the arrays described here. β
π Output Buffering π
Older versions of ncurses assumed that the file descriptor passed to setupterm from initscr or newterm used buffered I/O, and wrote to the corresponding stdio stream. In addition to the limitation that the terminal was left in block-buffered mode on exit (like System V curses), it was problematic because ncurses did not allow a reliable way to clean up on receiving SIGTSTP. π«Ά
ncurses 6.x uses output buffers managed directly by ncurses. The lower-level functions described here that write to the terminal device do so via the standard output stream; they thus are not signal-safe. The higher-level functions in ncurses employ alternate versions of these functions using a more reliable buffering scheme. π₯
π§ Function Prototypes π§
The X/Open Curses prototypes are based on the SVr4 curses header declarations, which were defined at the same time the C language was first standardized in the late 1980s. πΈ
- X/Open Curses uses
constless effectively than a later design might, sometimes applying it needlessly to function parameters that are passed by value (and therefore copied), and in most cases overlooking parameters that normally would benefit fromconst. Passingconst-qualified parameters to functions that do not declare themconstmay prevent the program from compiling. On the other hand, βwritable stringsβ are an obsolescent C language feature. π
As an extension, ncurses can be configured to change the function prototypes to use the const keyword. The ncurses ABI 6 enables this feature by default. β¨
- X/Open Curses prototypes
tparmwith a fixed number of parameters, rather than a variable argument list. π¦
ncurses uses a variable argument list, but can be configured to use the fixed-parameter list. Portable applications should provide nine parameters after the format; zeroes are fine for this purpose. π
In response to review comments by Thomas E. Dickey, X/Open Curses Issue 7 proposed the tiparm function in mid-2009. π
While tiparm is always provided in ncurses, the older form is available only as a build-time configuration option. If not specially configured, tparm is the same as tiparm. πΈ
Both forms of tparm have drawbacks. π
- Most calls to
tparmrequire only one or two parameters. Passing nine on each call is awkward. π
Using long for the numeric parameter type is a workaround to make the parameter use the same amount of stack memory as a pointer. That approach dates to the mid-1980s, before C was standardized. Since ANSI C (1989), C language standards do not require a pointer to fit in a long). π
- Providing the right number of parameters for a variadic function such as
tiparmcan be a problem, in particular for string parameters. However, only a few terminfo capabilities use string parameters (for instance, the ones used for programmable function keys). π₯
The ncurses library checks usage of these capabilities, and returns ERR if the capability mishandles string parameters. But it cannot check if a calling program provides strings in the right places for the tparm calls. β
ncurses's tput(1) checks its use of these capabilities with a table, so that it calls tparm correctly. π
π Special TERM treatment π
If ncurses is configured to use a terminal driver that does not employ the POSIX termios API, as with the MinGW port, π«Ά
setupterminterprets a missing or emptyTERMvariable as the special value βunknownβ. π
SVr4 curses uses the special value βdumbβ. π
The difference between the two is that the former uses the generic_type (gn) terminfo capability, while the latter does not. A generic terminal is unsuitable for full-screen applications. π
setuptermallows explicit use of the Microsoft Windows console driver by checking whether theTERMenvironment variable has the value β#win32conβ or an abbreviation of that string. π
π Other Portability Issues π
In SVr4, set_curterm returns an int, OK or ERR. We have chosen to implement the X/Open Curses semantics. πΈ
In SVr4, the third argument of tputs has the type βint (*)(char)β. π§
At least one implementation of X/Open Curses (Solaris xcurses) returns a value other than OK or ERR from tputs. It instead returns the length of the string, and does no error checking. π
Very old versions of AIX curses required inclusion of curses.h before term.h. β¨
π HISTORY π
Okay so gather round, bestie, because this is the glow-up timeline of terminfo and it's honestly a whole saga. β¨
SVr2 (1984) walked in and introduced the terminfo feature, no big deal. Its programming manual name-dropped these low-level functions:
fixtermβ restore terminal to "in curses" stategettmodeβ establish current terminal modesmvcurβ low level cursor motionputpβ usetputsto send characters viaputcharresettermβ set terminal modes to "out of curses" stateresettyβ reset terminal flags to stored valuesavetermβ save current modes as "in curses" statesavettyβ store current terminal flagssettermβ establish terminal with given typesetuptermβ establish terminal with given typetparmβ interpolate parameters into string capabilitytputsβ apply padding information to a stringvidattrβ likevidputs, but output throughputcharvidputsβ write string to terminal, applying specified attributes
The programming manual also mentioned functions provided for termcap compatibility β and it literally commented that they "may go away at a later date" π, which is giving ominous foreshadowing fr fr:
tgetentβ look up termcap entry for given nametgetflagβ get Boolean entry for given idtgetnumβ get numeric entry for given idtgetstrβ get string entry for given idtgotoβ apply parameters to given capabilitytputsβ write characters via a function parameter, applying padding
Early terminfo programs got their capability values by reaching into the TERMINAL structure that setupterm initialized. iykyk.
SVr3 (1987) said "let's level up" π
and extended terminfo by adding functions to retrieve capability values (just like the termcap interface does), and it reused tgoto and tputs:
tigetflagβ get Boolean entry for given idtigetnumβ get numeric entry for given idtigetstrβ get string entry for given id
SVr3 also replaced several of the SVr2 terminfo functions that had no counterpart in the termcap interface, and it documented them as obsolete:
crmodeβ replaced bycbreakfixtermβ replaced byreset_prog_modegettmodeβ replaced byn/anocrmodeβ replaced bynocbreakresettermβ replaced byreset_shell_modesavetermβ replaced bydef_prog_modesettermβ replaced bysetupterm
SVr3 kept the mvcur, vidattr, and vidputs functions, along with putp, tparm, and tputs. Those last three were needed to support padding, and to handle capabilities accessed by functions such as vidattr (which used more than the two parameters supported by tgoto). The way tgoto was only packing two params? tragic. π
SVr3 also introduced the functions for switching between terminal descriptions β for example, set_curterm. Some of the changes were just incremental improvements to the SVr2 library, like:
- The
TERMINALtype definition was introduced in SVr3.01, for thetermstructure provided in SVr2. - Various global variables such as
boolnameswere mentioned in the programming manual at this point, though the variables had actually been provided back in SVr2.
SVr4 (1989) added the vid_attr and vid_puts functions. And that's the era's whole contribution, we love a low-effort queen. π
Other low-level functions are declared in the curses header files of Unix systems, but none are documented. And the ones SVr3 noted as "obsolete"? They stayed in use by System V's vi(1) editor. Not me being obsolete and still getting booked. π
π SEE ALSO π
curses(3X)curs_initscr(3X)curs_kernel(3X)curs_memleaks(3X)curs_termcap(3X)curs_variables(3X)putc(3)term_variables(3X)terminfo(5)
And that's the whole page bestie, we ate and left absolutely no crumbs. πβ¨ This one's from ncurses 6.6, dated 2025-11-11, and it's the curs_terminfo(3X) page β go forth and tputs responsibly, ily ππ¦π
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright 2018-2024,2025 Thomas E. Dickey
- Copyright 1998-2016,2017 Free Software Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms