Software: Apache/2.0.54 (Fedora). PHP/5.0.4 uname -a: Linux mina-info.me 2.6.17-1.2142_FC4smp #1 SMP Tue Jul 11 22:57:02 EDT 2006 i686 uid=48(apache) gid=48(apache) groups=48(apache) Safe-mode: OFF (not secure) /usr/share/doc/ncurses-devel-5.4/ drwxr-xr-x |
Viewing file: Select action/file-type: A Hacker's Guide to NCURSESContents
AbstractThis document is a hacker's tour of the ncurses library and utilities. It discusses design philosophy, implementation methods, and the conventions used for coding and documentation. It is recommended reading for anyone who is interested in porting, extending or improving the package.Objective of the PackageThe objective of the ncurses package is to provide a free software API for character-cell terminals and terminal emulators with the following characteristics:
Why System V Curses?We used System V curses as a model, reverse-engineering their API, in order to fulfill the first two objectives.System V curses implementations can support BSD curses programs with just a recompilation, so by capturing the System V API we also capture BSD's. More importantly for the future, the XSI Curses standard issued by X/Open is explicitly and closely modeled on System V. So conformance with System V took us most of the way to base-level XSI conformance. How to Design ExtensionsThe third objective (standards conformance) requires that it be easy to condition source code using ncurses so that the absence of nonstandard extensions does not break the code.Accordingly, we have a policy of associating with each nonstandard extension a feature macro, so that ncurses client code can use this macro to condition in or out the code that requires the ncurses extension.
For example, there is a macro Portability and ConfigurationCode written for ncurses may assume an ANSI-standard C compiler and POSIX-compatible OS interface. It may also assume the presence of a System-V-compatible select(2) call.We encourage (but do not require) developers to make the code friendly to less-capable UNIX environments wherever possible. We encourage developers to support OS-specific optimizations and methods not available under POSIX/ANSI, provided only that:
autoconf(1) as a tool to deal with portability issues.
The right way to leverage an OS-specific feature is to modify the autoconf
specification files (configure.in and aclocal.m4) to set up a new feature
macro, which you then use to condition your code.
Documentation ConventionsThere are three kinds of documentation associated with this package. Each has a different preferred format:
The reason for choosing HTML is that it's (a) well-adapted for on-line browsing through viewers that are everywhere; (b) more easily readable as plain text than most other mark-ups, if you don't have a viewer; and (c) carries enough information that you can generate a nice-looking printed version from it. Also, of course, it make exporting things like the announcement document to WWW pretty trivial. How to Report BugsThe reporting address for bugs is bug-ncurses@gnu.org. This is a majordomo list; to join, write tobug-ncurses-request@gnu.org with a message containing the line:
subscribe <name>@<host.domain>The ncurses code is maintained by a small group of
volunteers. While we try our best to fix bugs promptly, we simply
don't have a lot of hours to spend on elementary hand-holding. We rely
on intelligent cooperation from our users. If you think you have
found a bug in ncurses , there are some steps you can take
before contacting us that will help get the bug fixed quickly. In order to use our bug-fixing time efficiently, we put people who show us they've taken these steps at the head of our queue. This means that if you don't, you'll probably end up at the tail end and have to wait a while.
The most important of these is
If you think the vertical-scroll optimization is broken, or just want to
understand how it works better, build
There's one other interactive tester, A Tour of the Ncurses LibraryLibrary OverviewMost of the library is superstructure -- fairly trivial convenience interfaces to a small set of basic functions and data structures used to manipulate the virtual screen (in particular, none of this code does any I/O except through calls to more fundamental modules described below). The files
are all in this category. They are very
unlikely to need change, barring bugs or some fundamental
reorganization in the underlying data structures. These files are used only for debugging support:
It is rather unlikely you will ever need to change these, unless
you want to introduce a new debug trace level for some reasoon.There is another group of files that do direct I/O via tputs(), computations on the terminal capabilities, or queries to the OS environment, but nevertheless have only fairly low complexity. These include:
They are likely to need revision only if
ncurses is being ported to an environment without an underlying
terminfo capability representation. These files have serious hooks into the tty driver and signal facilities:
If you run into porting snafus
moving the package to another UNIX, the problem is likely to be in one
of these files.
The file lib_print.c uses sleep(2) and also
falls in this category.Almost all of the real work is done in the files
Most of the algorithmic complexity in the
library lives in these files.
If there is a real bug in ncurses itself, it's probably here.
We'll tour some of these files in detail
below (see The Engine Room). Finally, there is a group of files that is actually most of the terminfo compiler. The reason this code lives in the ncurses library is to support fallback to /etc/termcap. These files include
We'll discuss these in the compiler tour.
The Engine RoomKeyboard InputAllncurses input funnels through the function
wgetch() , defined in lib_getch.c . This function is
tricky; it has to poll for keyboard and mouse events and do a running
match of incoming input against the set of defined special keys.
The central data structure in this module is a FIFO queue, used to
match multiple-character input sequences against special-key
capabilities; also to implement pushback via
The
Hackers bruised by previous encounters with variant Mouse EventsIf the mouse interface is active,wgetch() polls for mouse
events each call, before it goes to the keyboard for input. It is
up to lib_mouse.c how the polling is accomplished; it may vary
for different devices. Under xterm, however, mouse event notifications come in via the keyboard input stream. They are recognized by having the kmous capability as a prefix. This is kind of klugey, but trying to wire in recognition of a mouse key prefix without going through the function-key machinery would be just too painful, and this turns out to imply having the prefix somewhere in the function-key capabilities at terminal-type initialization. This kluge only works because kmous isn't actually used by any historic terminal type or curses implementation we know of. Best guess is it's a relic of some forgotten experiment in-house at Bell Labs that didn't leave any traces in the publicly-distributed System V terminfo files. If System V or XPG4 ever gets serious about using it again, this kluge may have to change. Here are some more details about mouse event handling:
The
Functionally, the lower level's job is to pick up primitive events and
put them on the circular queue. This can happen in one of two ways:
either (a)
In either case, Output and Screen UpdatingWith the single exception of character echoes during awgetnstr()
call (which simulates cooked-mode line editing in an ncurses window),
the library normally does all its output at refresh time.
The main job is to go from the current state of the screen (as represented
in the
The brains of this operation are the modules
The
The
Then
If you want to work on screen optimizations, you should use the fact
that (in the trace-enabled version of the library) enabling the
In the trace-enabled version of the library, it is also possible to disable
and re-enable various optimizations at runtime by tweaking the variable
The Forms and Menu LibrariesThe forms and menu libraries should work reliably in any environment you can port ncurses to. The only portability issue anywhere in them is what flavor of regular expressions the built-in form field type TYPE_REGEXP will recognize.The configuration code prefers the POSIX regex facility, modeled on System V's, but will settle for BSD regexps if the former isn't available.
Historical note: the panels code was written primarily to assist in
porting u386mon 2.0 (comp.sources.misc v14i001-4) to systems lacking
panels support; u386mon 2.10 and beyond use it. This version has been
slightly cleaned up for A Tour of the Terminfo CompilerThe ncurses implementation of tic is rather complex internally; it has to do a trying combination of missions. This starts with the fact that, in addition to its normal duty of compiling terminfo sources into loadable terminfo binaries, it has to be able to handle termcap syntax and compile that too into terminfo entries.
The implementation therefore starts with a table-driven, dual-mode
lexical analyzer (in Translation of Non-use CapabilitiesTranslation of most things besides use capabilities is pretty straightforward. The lexical analyzer's tokenizer hands each capability name to a hash function, which drives a table lookup. The table entry yields an index which is used to look up the token type in another table, and controls interpretation of the value.
One possibly interesting aspect of the implementation is the way the
compiler tables are initialized. All the tables are generated by various
awk/sed/sh scripts from a master table
Thus, adding a new capability is usually pretty trivial, just a matter
of adding one line to the Use Capability ResolutionThe background problem that makes tic tricky isn't the capability translation itself, it's the resolution of use capabilities. Older versions would not handle forward use references for this reason (that is, a using terminal always had to follow its use target in the source file). By doing this, they got away with a simple implementation tactic; compile everything as it blows by, then resolve uses from compiled entries.This won't do for ncurses. The problem is that that the whole compilation process has to be embeddable in the ncurses library so that it can be called by the startup code to translate termcap entries on the fly. The embedded version can't go promiscuously writing everything it translates out to disk -- for one thing, it will typically be running with non-root permissions. So our tic is designed to parse an entire terminfo file into a doubly-linked circular list of entry structures in-core, and then do use resolution in-memory before writing everything out. This design has other advantages: it makes forward and back use-references equally easy (so we get the latter for free), and it makes checking for name collisions before they're written out easy to do. And this is exactly how the embedded version works. But the stand-alone user-accessible version of tic partly reverts to the historical strategy; it writes to disk (not keeping in core) any entry with no use references. This is strictly a core-economy kluge, implemented because the terminfo master file is large enough that some core-poor systems swap like crazy when you compile it all in memory...there have been reports of this process taking three hours, rather than the twenty seconds or less typical on the author's development box. So. The executable tic passes the entry-parser a hook that immediately writes out the referenced entry if it has no use capabilities. The compiler main loop refrains from adding the entry to the in-core list when this hook fires. If some other entry later needs to reference an entry that got written immediately, that's OK; the resolution code will fetch it off disk when it can't find it in core.
Name collisions will still be detected, just not as cleanly. The
Source-Form TranslationAnother use of tic is to do source translation between various termcap and terminfo formats. There are more variants out there than you might think; the ones we know about are described in the captoinfo(1) manual page.
The translation output code (
The
For circumstances where you need to do algorithmic translation, there
are functions in Other UtilitiesThe infocmp utility is just a wrapper around the same entry-dumping code used by tic for source translation. Perhaps the one interesting aspect of the code is the use of a predicate function passed in todump_entry() to control which
capabilities are dumped. This is necessary in order to handle both
the ordinary De-compilation case and entry difference reporting.
The tput and clear utilities just do an entry load
followed by a Style Tips for DevelopersSee the TO-DO file in the top-level directory of the source distribution for additions that would be particularly useful.
The prefix
Look for the string Don't try to auto-detect OS features in the main body of the C code. That's the job of the configuration system.
To hold down complexity, do make your code data-driven. Especially,
if you can drive logic from a table filtered out of
Have fun! Porting HintsThe following notes are intended to be a first step towards DOS and Macintosh ports of the ncurses libraries.
The following library modules are `pure curses'; they operate only on
the curses internal structures, do all output through other curses
calls (not including
This module is pure curses, but calls outstr():
These modules are pure curses, except that they use
This modules assist in POSIX emulation on non-POSIX systems:
The following modules will use open()/read()/write()/close()/lseek() on files, but no other OS calls.
The following modules are `pure curses' but contain assumptions inappropriate for a memory-mapped port.
Eric S. Raymond <esr@snark.thyrsus.com> (Note: This is not the bug address!) |
:: Command execute :: | |
--[ c99shell v. 1.0 pre-release build #16 powered by Captain Crunch Security Team | http://ccteam.ru | Generation time: 0.0029 ]-- |