ncurses: Portable Screen-Handling for Linux

Have you ever written a screen-oriented program under Linux and been annoyed by the limitations of the standard curses screen manipulation library? Here is a programmer's introduction to the vastly superior ncurses library.
Quality Improvements

Much has changed in the last year, out of sight of the Linux community at large. We have fixed many bugs and filled out the API. We've written more complete documentation, including man pages. We've done extensive compatibility testing against SVR4, linking identical programs to both libraries and comparing behavior. We've audited the code carefully for cross-platform portability and replaced a rather clunky home-brewed configuration system with GNU autoconf. We've tested the code for memory leaks with Purify and improved argument validation in many critical functions. The overall quality of the code has been dramatically improved.

We've also added several Bell Labs-like utilities missing from older ncurses versions, including:

  • infocmp(1) a terminfo entry lister and comparator.

  • captoinfo(1) a termcap to terminfo translator.

  • clear(1) a trivial screen clearer.

  • tput(1) a terminfo capability access for shell scripts.

The captoinfo, clear, and tput utilities are based on code from Ross Ridge's mytinfo package (which we've effectively subsumed).

We chose System V Release 4.0 curses as our emulation target, and now support all of its very extensive features. We also include a clone of the SVR4 panels library, a curses extension that makes it easy to program stacks of windows with backing store.

The new ncurses package also includes a full and up-to-date set of man pages organized similarly to SVR4's.

In a few areas, we go beyond SVR4 curses, for example:

  • The new cursor-movement optimizer and incremental-screen-update algorithms are smarter than the Bell Labs versions (and much smarter than the BSD versions) leading to significant update-speed gains for slow terminals.

  • Unlike previous curses versions, ncurses can write the lower right hand corner cell of a terminal with automargin wrap (provided it has an insert-character capability).

  • On Intel boxes, curses permits you to display not just the IBM high-half characters but also the ROM graphics in characters 0-31.

  • Our terminfo tools recognize all the terminfo and termcap extensions found in GNU termcap, mytinfo, and the University of Waterloo libraries.

  • We feature a C++ class derived from the libg++ CursesWindow class, but enhanced for ncurses.

  • The ncurses suite now includes an ncurses program which allows you explicitly test most of ncurses's capabilities on any new platform.

  • We've souped up the tracing feature. It is now possible to pick one of several levels of output tracing, and the trace log has been made easier to interpret (with symbolic dumping of screen attributes, colors, and so forth).

  • Automatic fallback to the /etc/termcap file can be compiled in for systems without a terminfo tree. This feature is neither fast nor cheap, so you don't want to use it unless you have to.

Along with ncurses, you get a very complete terminfo file. I accepted the maintainer's baton for the 4.4BSD master termcap file from John Kunze in January 1995; I've since translated it to terminfo and added a lot of information from vendors like SCO, Digital Equipment Corporation, and Wyse. (The terminfo file is available separately from my WWW home page,

How Do I Use ncurses?

Listing 1 gives a program skeleton that illustrates how to set up to use some of the new features.

Note the way we arrange for endwin() to get called by the signal catcher. This is necessary, otherwise a stray signal might leave the tty modes in a less than useful state.

Our next program fragment illustrates the use of the keypad mapping feature. This code, from an actual test program distributed with ncurses, is shown is Listing 2.

The example code illustrates how you can get function key tokens as single values outside the ASCII range from getch(). The call keypad(stdscr, TRUE) sets this up.

The ncurses distribution contains a more detailed tutorial in HTML form and also has several code examples in the test directory.

The Future of ncurses

The ncurses library seems to have achieved a stable plateau, suitable for production use, in the 1.9.1 and 1.9.2 releases. We're looking ahead to some interesting possibilities—most notably at supporting XPG4 Curses extended-level features for wide and multi-byte character sets. We're also thinking about MS-DOS and MS-Windows ports.

With the features we already have in a freeware library, you may be wondering whether there are good reasons for BSD curses to continue to exist. The answer is “probably not”.

Keith Bostic, maintainer of BSD curses, has agreed that if ncurses 1.9 proves stable and usable with nvi (new vi), he will switch to ncurses for the nvi distribution and pronounce BSD curses officially dead. The ncurses library has already been used to successfully support nvi for production, so it's a good bet that by the time you read this, Keith will have finished his testing—and BSD curses will be history.

This, in turn, means the Unix world will finally get a common freeware API that supports color, multiple highlights, and all the other capabilities common to PC consoles and today's character-cell terminals.