termcap(3NCURSES)

πŸ’… termcap(3NCURSES) β€” Library Calls πŸ’…

okay bestie, welcome to the ncurses take on termcap ✨ it's the whole "curses emulation of termcap" situation, and honestly? it's giving legacy-but-iconic. let's get into it 🫢


🌸 NAME 🌸

PC, UP, BC, ospeed, tgetent, tgetflag, tgetnum, tgetstr, tgoto, tputs β€” the entire squad. this is the curses emulation of termcap, no cap.


πŸ“œ SYNOPSIS πŸ“œ

#include <curses.h>
#include <term.h>

char PC;
char * UP;
char * BC;
short ospeed;

int tgetent(char * bp, const char * name);
int tgetflag(const char * id);
int tgetnum(const char * id);
char * tgetstr(const char * id, char ** sbuf);
char * tgoto(const char * cap, int col, int row);
int tputs(const char * str, int affcnt, int (* putc)(int));

πŸ¦‹ DESCRIPTION πŸ¦‹

so basically, ncurses hands you all those variables and functions up there as a compatibility layer for programs that use the termcap library. the API is exactly the same, but the behavior? emulated, bestie β€” it's all done through the terminfo database instead.

which means the tea is: you can only use this to query capabilities of terminal database entries that have a terminfo entry compiled for them. that's the whole vibe check right there πŸ’…

πŸŽ€ Initialization πŸŽ€

tgetent loads the terminal database entry for name; peep term(7) for the deets. this is mandatory β€” you gotta do it before you call any of the other functions, fr fr. and it returns:

  • 1 on success ✨
  • 0 if there's no such entry (or if the matching entry describes a generic terminal, meaning it has too little information for curses applications to actually run) 😭
  • -1 if the terminfo database could not be found πŸ’€

and now the plot twist: this implementation differs from those of historical termcap libraries.

  • ncurses straight up ignores the buffer pointer bp, same as other termcap implementations that conform to portions of X/Open Curses that have since been withdrawn. the BSD termcap library would store a copy of the terminal type description in the buffer referenced by this pointer. but terminfo stores terminal type descriptions in compiled form, which is just... not the same thing, bestie.
  • the meanings of the return values differ πŸ‘€ the BSD termcap library does not check whether the terminal type description includes the generic (gn) capability, and it also doesn't check whether the terminal type description supports an addressable cursor β€” a property that is essential for any curses implementation to operate. so yeah, different rules entirely.

πŸ” Retrieving Capability Values πŸ”

tgetflag reports the Boolean entry for id, or zero if it isn't available.

tgetnum obtains the numeric entry for id, or -1 if it isn't available.

tgetstr returns the string entry for id, or NULL if it isn't available. and then you use tputs to output the string it returned. the sbuf parameter gets used like this:

  • it's assumed to be the address of a pointer to a buffer that's managed by the calling application.
  • however, ncurses checks to make sure sbuf is not NULL, and that the pointer you get by dereferencing sbuf is also not NULL. if either check fails, ncurses just ignores sbuf. πŸ«₯
  • if the checks do succeed, ncurses also copies the return value into the buffer pointed to by sbuf, and the library updates sbuf to point past the null character terminating this value.
  • the return value itself is an address in the terminal type description that's been loaded into memory.

🎨 Applying String Capabilities 🎨

string capabilities can be parameterized; see the subsection "Parameterized Strings" in terminfo(5). tgoto applies its second and third arguments to the parametric placeholders in the capability stored in the first argument.

  • the capability may contain padding specifications; see the subsection "Delays and Padding" of terminfo(5). so the output of tgoto should be passed to tputs, not to some other output function like printf(3). that's the move, trust.
  • while tgoto is assumed to be used for the two-parameter cursor positioning capability, termcap applications also use it for single-parameter capabilities πŸ‘‘

and doing that reveals a lil quirk in tgoto: most hardware terminals use cursor addressing with row first, but the original developers of the termcap interface chose to put the col (column) parameter first. so the tgoto function swaps the order of its parameters. it does this even for calls that only require a single parameter β€” in that case, the first parameter is merely a placeholder.

  • normally the ncurses library is compiled without full termcap support. in that case, tgoto uses an internal version of tparm(3NCURSES) (which is a more capable function).

because it uses tparm internally, tgoto is able to use some terminfo features β€” but not all of them. in particular, it allows only numeric parameters; tparm supports string parameters too.

however, tparm is not a termcap feature, and portable termcap applications should not rely upon its availability. periodt.

tputs is described over in terminfo(3NCURSES). it can retrieve capabilities by either termcap or terminfo code, so it's flexible like that πŸ’…

🌟 Global Variables 🌟

tgetent sets the variables PC, UP, and BC to the terminfo entry's data for pad_char (pad), cursor_up (cuu1), and backspace_if_not_bs (OTbs), respectively.

ncurses does not employ cuu1 internally, jsyk. delay_output(3NCURSES) uses pad, while the tgoto emulation uses the obsolete termcap capability bs, which is represented in ncurses terminfo as "OTbs".

and ncurses assigns the variable ospeed a system-specific value to encode the terminal's data rate.

🧹 Releasing Memory 🧹

the termcap functions provide no means of freeing memory, because legacy termcap implementations only used the storage provided by the caller via tgetent and tgetstr. those buffers are just... unused in terminfo. leftover vibes πŸ™ˆ

by contrast, terminfo does allocate memory. it uses setupterm(3NCURSES) to obtain the data used by tgetent and the functions that retrieve capability values. one could use

del_curterm(cur_term);

to free this memory β€” but there's an additional complication with ncurses 😭 it uses a fixed-size pool of storage locations, one per value of the terminal name parameter given to tgetent. and the screen(1) program relies upon this arrangement to improve its performance. so that pool is doing a lot of heavy lifting, bestie.

an application that uses only the termcap functions, and not the higher-level curses API, could release the memory using del_curterm(3NCURSES), because the pool is freed using other functions; see memleaks(3NCURSES).


πŸ’– RETURN VALUE πŸ’–

the return values of tgetent, tgetflag, tgetname, and tgetstr are documented up above, go reread if you need to 🫢

tgoto returns NULL on error. and the error conditions include:

  • uninitialized state (meaning tgetent was not called successfully) πŸ’€
  • cap being a null pointer
  • cap referring to a canceled capability
  • cap being a capability with string-valued parameters (a terminfo-only feature), and
  • cap being a capability with more than two parameters

see terminfo(3NCURSES) regarding tputs.


πŸ“ NOTES πŸ“

ncurses compares only the first two characters of the id parameter of tgetflag, tgetnum, and tgetstr to the capability names in the database. just two. that's it. iykyk πŸ‘€


🌍 PORTABILITY 🌍

these functions are no longer standardized (and the variables never were, not even once); see the section "HISTORY" below. ncurses provides them to support legacy applications β€” they should not be used in new programs. like, genuinely. new code, new you πŸ’…

SVr4 describes a successful return value only as "an integer value other than ERR".

neither X/Open Curses nor the SVr4 man pages documented the return values of tgetent correctly, even though all three shown here were in fact returned ever since SVr1. in particular, an omission in the X/Open Curses specification has been misinterpreted to mean that tgetent returns OK or ERR. and because the whole point of these functions is to provide compatibility with the termcap library, that is a defect in X/Open Curses Issue 4 Version 2 rather than in ncurses. not our fault, bestie 🫢

🀝 Compatibility with BSD termcap 🀝

ncurses provides externally visible variables to support certain termcap applications. however, their correct usage is poorly documented; for example, it's unclear when reading and writing them is even meaningful. in particular, some applications are reported to declare and/or modify ospeed. the audacity, honestly 😭

the constraint that only the first two characters of the id parameter are looked up in the terminal database escapes many application developers. the BSD termcap library did not require a trailing null character after the capability identifier passed to tgetstr, tgetnum, and tgetflag. so some applications assume that the termcap interface doesn't require that trailing null character for the capability identifier either. but ncurses disallows matches by the termcap interface against extended capability names that are longer than two characters; see user_caps(5).

the BSD termcap function tgetent returns the text of a termcap entry in the buffer passed as an argument. ncurses, like other terminfo implementations, does not store terminal type descriptions as text. instead, it sets the buffer contents to a null-terminated string.

πŸ“‚ Header File πŸ“‚

ncurses includes a termcap.h header file for compatibility with other implementations, but it's rarely used β€” because those other implementations are not mutually compatible with each other; see below.


(and that's only part 1, bestie β€” more coming, don't go anywhere πŸ¦‹)

πŸ“œ HISTORY πŸ“œ

Okay bestie, storytime, because this lore is actually iconic. Bill Joy originated a forerunner of termcap called ttycap, dated September 1977, and it got released in 1BSD (March 1978). The way that it used so many of the same function names as the later termcap β€” like tgetent, tgetflag, tgetnum, and tgetstr β€” no cap. ✨

A clear descendant, the termlib library, followed in 2BSD (May 1979), and it added tgoto and tputs. Fun fact: tgoto back then only applied to cursor positioning capabilities, which is literally why it has that overly specific name. 🎯 Little changed in 3BSD (late 1979) except the addition of test programs and a termlib man page, which documented the API shown up in SYNOPSIS above. πŸ’…

Then 4BSD (November 1980) renamed termlib to termcap and added another test program. The library stayed pretty much the same all the way through 4.3BSD (June 1986). And 4.4BSD-Lite (June 1994) refactored it, but left the API unchanged. glow-up with zero breaking changes, we love to see it. πŸ‘‘

Function prototypes were a feature of ANSI C (1989). But this library long antedated the standard and thus provided no header file declaring them. And yet?? The BSD sources included two different termcap.h header files over time. messy but make it historic:

  • One was used internally by jove(1) from 4.3BSD onward. It declared global symbols for the termcap variables that it used.
  • The other appeared in 4.4BSD-Lite Release 2 (June 1995) as part of libedit (also known as the editline library). CSRG source history shows that this was added in mid-1992. The libedit header file was used internally as a convenience for compiling the editline library. It declared function prototypes, but no global variables. NetBSD's termcap library added this header file in mid-1994.

Meanwhile, GNU termcap began development in 1990. Its first release (1.0) in 1991 included a termcap.h header file. Its second (1.1) release in September 1992 modified the file to use const for the function prototypes in the header where one would expect parameters to be read-only. BSD termcap did not. The prototype for tputs also differed, but in that instance, it was libedit that differed from BSD termcap. 😭

GNU bash(1) has bundled GNU termcap 1.3 since mid-1993 to support its readline(3) library, and continues to use it if configured to do so. 🫢

ncurses 1.8.1 (November 1993) provided a termcap.h file. It reflected influence from GNU termcap and emacs(1) (rather than jove(1)), providing the following interface:

  • global symbols used by emacs,
  • const-qualified function prototypes, and
  • a prototype for tparam, a GNU termcap feature.

Later (in mid-1996) the tparam function was removed from ncurses. So any two of the four implementations thus differ, and programs that intend to work with all termcap library interfaces must account for that fact. it's giving compatibility nightmare, ngl. πŸ’€

X/Open Curses Issue 4, Version 2 (1996) describes these functions, marking them as β€œTO BE WITHDRAWN”. 🚩

X/Open Curses Issue 7 (2009) withdrew the termcap interface (along with the vwprintw and vwscanw functions). rip bozo, it's gone. πŸ’


πŸ’€ BUGS πŸ’€

Okay so listen up, this part is important and it will humble you:

If you call tgetstr to fetch column_address (ch) or any other parameterized string capability, be aware that it is returned in terminfo notation, not the older and not-quite-compatible termcap notation. This does not cause problems if all you do with it is call tgoto or tparm, which both parametrically expand terminfo-style string capabilities as terminfo does. (If ncurses is configured to support termcap, tgoto checks whether the string is terminfo-style by looking for %p parameters or <...> delays, and invokes a termcap-style parser if the string appears not to use terminfo syntax.) πŸ”

Because terminfo's syntax for padding in string capabilities differs from termcap's, users can be surprised. πŸ™ˆ And not in the fun way:

  • tputs("50") in a terminfo system transmits β€œ50” rather than busy-waiting for 50 milliseconds.

However, if ncurses is configured to support termcap, it may also have been configured to support BSD-style padding. In that case, tputs inspects strings passed to it, looking for digits at the beginning of the string.

  • tputs("50") in a termcap system may busy-wait for 50 milliseconds rather than transmitting β€œ50”.

termcap has nothing analogous to terminfo's set_attributes (sgr) capability. One consequence is that termcap applications assume that β€œme” (equivalent to terminfo's exit_attribute_mode (sgr0) capability) does not reset the alternate character set. ncurses inspects the data shared with the termcap interface and modifies it as necessary to accommodate the latter's limitation in this respect. πŸ› οΈ


πŸ‘€ SEE ALSO πŸ‘€

  • TCTEST β€” A Termcap Test Utility
  • ncurses(3NCURSES), terminfo(3NCURSES), putc(3), terminfo_variables(3NCURSES), terminfo(5)

and that's the whole page, bestie!! πŸ’–βœ¨πŸŽ€ This one shipped with ncurses 6.6, dated 2025-08-16, and it's termcap(3NCURSES) β€” the termcap-compatibility queen herself. go forth and tgetstr responsibly, ily, mwah πŸ«ΆπŸŒΈπŸ¦‹

πŸ“¦ where this page comes from