1<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 3.0//EN"> 2<!-- 3 $Id: hackguide.html,v 1.25 2000/03/25 18:45:21 tom Exp $ 4--> 5<HTML> 6<HEAD> 7<TITLE>A Hacker's Guide to Ncurses Internals</TITLE> 8<link rev="made" href="mailto:bugs-ncurses@gnu.org"> 9<!-- 10This document is self-contained, *except* that there is one relative link to 11the ncurses-intro.html document, expected to be in the same directory with 12this one. 13--> 14</HEAD> 15<BODY> 16 17<H1>A Hacker's Guide to NCURSES</H1> 18 19<H1>Contents</H1> 20<UL> 21<LI><A HREF="#abstract">Abstract</A> 22<LI><A HREF="#objective">Objective of the Package</A> 23<UL> 24<LI><A HREF="#whysvr4">Why System V Curses?</A> 25<LI><A HREF="#extensions">How to Design Extensions</A> 26</UL> 27<LI><A HREF="#portability">Portability and Configuration</A> 28<LI><A HREF="#documentation">Documentation Conventions</A> 29<LI><A HREF="#bugtrack">How to Report Bugs</A> 30<LI><A HREF="#ncurslib">A Tour of the Ncurses Library</A> 31<UL> 32<LI><A HREF="#loverview">Library Overview</A> 33<LI><A HREF="#engine">The Engine Room</A> 34<LI><A HREF="#input">Keyboard Input</A> 35<LI><A HREF="#mouse">Mouse Events</A> 36<LI><A HREF="#output">Output and Screen Updating</A> 37</UL> 38<LI><A HREF="#fmnote">The Forms and Menu Libraries</A> 39<LI><A HREF="#tic">A Tour of the Terminfo Compiler</A> 40<UL> 41<LI><A HREF="#nonuse">Translation of Non-<STRONG>use</STRONG> Capabilities</A> 42<LI><A HREF="#uses">Use Capability Resolution</A> 43<LI><A HREF="#translation">Source-Form Translation</A> 44</UL> 45<LI><A HREF="#utils">Other Utilities</A> 46<LI><A HREF="#style">Style Tips for Developers</A> 47<LI><A HREF="#port">Porting Hints</A> 48</UL> 49 50<H1><A NAME="abstract">Abstract</A></H1> 51 52This document is a hacker's tour of the <STRONG>ncurses</STRONG> library and utilities. 53It discusses design philosophy, implementation methods, and the 54conventions used for coding and documentation. It is recommended 55reading for anyone who is interested in porting, extending or improving the 56package. 57 58<H1><A NAME="objective">Objective of the Package</A></H1> 59 60The objective of the <STRONG>ncurses</STRONG> package is to provide a free software API for 61character-cell terminals and terminal emulators with the following 62characteristics: 63 64<UL> 65<LI>Source-compatible with historical curses implementations (including 66 the original BSD curses and System V curses. 67<LI>Conformant with the XSI Curses standard issued as part of XPG4 by 68 X/Open. 69<LI>High-quality -- stable and reliable code, wide portability, good 70 packaging, superior documentation. 71<LI>Featureful -- should eliminate as much of the drudgery of C interface 72 programming as possible, freeing programmers to think at a higher 73 level of design. 74</UL> 75 76These objectives are in priority order. So, for example, source 77compatibility with older version must trump featurefulness -- we cannot 78add features if it means breaking the portion of the API corresponding 79to historical curses versions. 80 81<H2><A NAME="whysvr4">Why System V Curses?</A></H2> 82 83We used System V curses as a model, reverse-engineering their API, in 84order to fulfill the first two objectives. <P> 85 86System V curses implementations can support BSD curses programs with 87just a recompilation, so by capturing the System V API we also 88capture BSD's. <P> 89 90More importantly for the future, the XSI Curses standard issued by X/Open 91is explicitly and closely modeled on System V. So conformance with 92System V took us most of the way to base-level XSI conformance. 93 94<H2><A NAME="extensions">How to Design Extensions</A></H2> 95 96The third objective (standards conformance) requires that it be easy to 97condition source code using <STRONG>ncurses</STRONG> so that the absence of nonstandard 98extensions does not break the code. <P> 99 100Accordingly, we have a policy of associating with each nonstandard extension 101a feature macro, so that ncurses client code can use this macro to condition 102in or out the code that requires the <STRONG>ncurses</STRONG> extension. <P> 103 104For example, there is a macro <CODE>NCURSES_MOUSE_VERSION</CODE> which XSI Curses 105does not define, but which is defined in the <STRONG>ncurses</STRONG> library header. 106You can use this to condition the calls to the mouse API calls. 107 108<H1><A NAME="portability">Portability and Configuration</A></H1> 109 110Code written for <STRONG>ncurses</STRONG> may assume an ANSI-standard C compiler and 111POSIX-compatible OS interface. It may also assume the presence of a 112System-V-compatible <EM>select(2)</EM> call. <P> 113 114We encourage (but do not require) developers to make the code friendly 115to less-capable UNIX environments wherever possible. <P> 116 117We encourage developers to support OS-specific optimizations and methods 118not available under POSIX/ANSI, provided only that: 119 120<UL> 121<LI>All such code is properly conditioned so the build process does not 122 attempt to compile it under a plain ANSI/POSIX environment. 123<LI>Adding such implementation methods does not introduce incompatibilities 124 in the <STRONG>ncurses</STRONG> API between platforms. 125</UL> 126 127We use GNU <CODE>autoconf(1)</CODE> as a tool to deal with portability issues. 128The right way to leverage an OS-specific feature is to modify the autoconf 129specification files (configure.in and aclocal.m4) to set up a new feature 130macro, which you then use to condition your code. 131 132<H1><A NAME="documentation">Documentation Conventions</A></H1> 133 134There are three kinds of documentation associated with this package. Each 135has a different preferred format: 136 137<UL> 138<LI>Package-internal files (README, INSTALL, TO-DO etc.) 139<LI>Manual pages. 140<LI>Everything else (i.e., narrative documentation). 141</UL> 142 143Our conventions are simple: 144<OL> 145<LI><STRONG>Maintain package-internal files in plain text.</STRONG> 146 The expected viewer for them <EM>more(1)</EM> or an editor window; there's 147 no point in elaborate mark-up. 148 149<LI><STRONG>Mark up manual pages in the man macros.</STRONG> These have to be viewable 150 through traditional <EM>man(1)</EM> programs. 151 152<LI><STRONG>Write everything else in HTML.</STRONG> 153</OL> 154 155When in doubt, HTMLize a master and use <EM>lynx(1)</EM> to generate 156plain ASCII (as we do for the announcement document). <P> 157 158The reason for choosing HTML is that it's (a) well-adapted for on-line 159browsing through viewers that are everywhere; (b) more easily readable 160as plain text than most other mark-ups, if you don't have a viewer; and (c) 161carries enough information that you can generate a nice-looking printed 162version from it. Also, of course, it make exporting things like the 163announcement document to WWW pretty trivial. 164 165<H1><A NAME="bugtrack">How to Report Bugs</A></H1> 166 167The <A NAME="bugreport">reporting address for bugs</A> is 168<A HREF="mailto:bug-ncurses@gnu.org">bug-ncurses@gnu.org</A>. 169This is a majordomo list; to join, write 170to <CODE>bug-ncurses-request@gnu.org</CODE> with a message containing the line: 171<PRE> 172 subscribe <name>@<host.domain> 173</PRE> 174 175The <CODE>ncurses</CODE> code is maintained by a small group of 176volunteers. While we try our best to fix bugs promptly, we simply 177don't have a lot of hours to spend on elementary hand-holding. We rely 178on intelligent cooperation from our users. If you think you have 179found a bug in <CODE>ncurses</CODE>, there are some steps you can take 180before contacting us that will help get the bug fixed quickly. <P> 181 182In order to use our bug-fixing time efficiently, we put people who 183show us they've taken these steps at the head of our queue. This 184means that if you don't, you'll probably end up at the tail end and 185have to wait a while. 186 187<OL> 188<LI>Develop a recipe to reproduce the bug. 189<p> 190Bugs we can reproduce are likely to be fixed very quickly, often 191within days. The most effective single thing you can do to get a 192quick fix is develop a way we can duplicate the bad behavior -- 193ideally, by giving us source for a small, portable test program that 194breaks the library. (Even better is a keystroke recipe using one of 195the test programs provided with the distribution.) 196 197<LI>Try to reproduce the bug on a different terminal type. <P> 198 199In our experience, most of the behaviors people report as library bugs 200are actually due to subtle problems in terminal descriptions. This is 201especially likely to be true if you're using a traditional 202asynchronous terminal or PC-based terminal emulator, rather than xterm 203or a UNIX console entry. <P> 204 205It's therefore extremely helpful if you can tell us whether or not your 206problem reproduces on other terminal types. Usually you'll have both 207a console type and xterm available; please tell us whether or not your 208bug reproduces on both. <P> 209 210If you have xterm available, it is also good to collect xterm reports for 211different window sizes. This is especially true if you normally use an 212unusual xterm window size -- a surprising number of the bugs we've seen 213are either triggered or masked by these. 214 215<LI>Generate and examine a trace file for the broken behavior. <P> 216 217Recompile your program with the debugging versions of the libraries. 218Insert a <CODE>trace()</CODE> call with the argument set to <CODE>TRACE_UPDATE</CODE>. 219(See <A HREF="ncurses-intro.html#debugging">"Writing Programs with 220NCURSES"</A> for details on trace levels.) 221Reproduce your bug, then look at the trace file to see what the library 222was actually doing. <P> 223 224Another frequent cause of apparent bugs is application coding errors 225that cause the wrong things to be put on the virtual screen. Looking 226at the virtual-screen dumps in the trace file will tell you immediately if 227this is happening, and save you from the possible embarrassment of being 228told that the bug is in your code and is your problem rather than ours. <P> 229 230If the virtual-screen dumps look correct but the bug persists, it's 231possible to crank up the trace level to give more and more information 232about the library's update actions and the control sequences it issues 233to perform them. The test directory of the distribution contains a 234tool for digesting these logs to make them less tedious to wade 235through. <P> 236 237Often you'll find terminfo problems at this stage by noticing that the 238escape sequences put out for various capabilities are wrong. If not, 239you're likely to learn enough to be able to characterize any bug in 240the screen-update logic quite exactly. 241 242<LI>Report details and symptoms, not just interpretations. <P> 243 244If you do the preceding two steps, it is very likely that you'll discover 245the nature of the problem yourself and be able to send us a fix. This 246will create happy feelings all around and earn you good karma for the first 247time you run into a bug you really can't characterize and fix yourself. <P> 248 249If you're still stuck, at least you'll know what to tell us. Remember, we 250need details. If you guess about what is safe to leave out, you are too 251likely to be wrong. <P> 252 253If your bug produces a bad update, include a trace file. Try to make 254the trace at the <EM>least</EM> voluminous level that pins down the 255bug. Logs that have been through tracemunch are OK, it doesn't throw 256away any information (actually they're better than un-munched ones because 257they're easier to read). <P> 258 259If your bug produces a core-dump, please include a symbolic stack trace 260generated by gdb(1) or your local equivalent. <P> 261 262Tell us about every terminal on which you've reproduced the bug -- and 263every terminal on which you can't. Ideally, sent us terminfo sources 264for all of these (yours might differ from ours). <P> 265 266Include your ncurses version and your OS/machine type, of course! You can 267find your ncurses version in the <CODE>curses.h</CODE> file. 268</OL> 269 270If your problem smells like a logic error or in cursor movement or 271scrolling or a bad capability, there are a couple of tiny test frames 272for the library algorithms in the progs directory that may help you 273isolate it. These are not part of the normal build, but do have their 274own make productions. <P> 275 276The most important of these is <CODE>mvcur</CODE>, a test frame for the 277cursor-movement optimization code. With this program, you can see 278directly what control sequences will be emitted for any given cursor 279movement or scroll/insert/delete operations. If you think you've got 280a bad capability identified, you can disable it and test again. The 281program is command-driven and has on-line help. <P> 282 283If you think the vertical-scroll optimization is broken, or just want to 284understand how it works better, build <CODE>hashmap</CODE> and read the 285header comments of <CODE>hardscroll.c</CODE> and <CODE>hashmap.c</CODE>; then try 286it out. You can also test the hardware-scrolling optimization separately 287with <CODE>hardscroll</CODE>. <P> 288 289There's one other interactive tester, <CODE>tctest</CODE>, that exercises 290translation between termcap and terminfo formats. If you have a serious 291need to run this, you probably belong on our development team! 292 293<H1><A NAME="ncurslib">A Tour of the Ncurses Library</A></H1> 294 295<H2><A NAME="loverview">Library Overview</A></H2> 296 297Most of the library is superstructure -- fairly trivial convenience 298interfaces to a small set of basic functions and data structures used 299to manipulate the virtual screen (in particular, none of this code 300does any I/O except through calls to more fundamental modules 301described below). The files 302<blockquote> 303<CODE> 304lib_addch.c 305lib_bkgd.c 306lib_box.c 307lib_chgat.c 308lib_clear.c 309lib_clearok.c 310lib_clrbot.c 311lib_clreol.c 312lib_colorset.c 313lib_data.c 314lib_delch.c 315lib_delwin.c 316lib_echo.c 317lib_erase.c 318lib_gen.c 319lib_getstr.c 320lib_hline.c 321lib_immedok.c 322lib_inchstr.c 323lib_insch.c 324lib_insdel.c 325lib_insstr.c 326lib_instr.c 327lib_isendwin.c 328lib_keyname.c 329lib_leaveok.c 330lib_move.c 331lib_mvwin.c 332lib_overlay.c 333lib_pad.c 334lib_printw.c 335lib_redrawln.c 336lib_scanw.c 337lib_screen.c 338lib_scroll.c 339lib_scrollok.c 340lib_scrreg.c 341lib_set_term.c 342lib_slk.c 343lib_slkatr_set.c 344lib_slkatrof.c 345lib_slkatron.c 346lib_slkatrset.c 347lib_slkattr.c 348lib_slkclear.c 349lib_slkcolor.c 350lib_slkinit.c 351lib_slklab.c 352lib_slkrefr.c 353lib_slkset.c 354lib_slktouch.c 355lib_touch.c 356lib_unctrl.c 357lib_vline.c 358lib_wattroff.c 359lib_wattron.c 360lib_window.c 361</CODE> 362</blockquote> 363are all in this category. They are very 364unlikely to need change, barring bugs or some fundamental 365reorganization in the underlying data structures. <P> 366 367These files are used only for debugging support: 368<blockquote> 369<code> 370lib_trace.c 371lib_traceatr.c 372lib_tracebits.c 373lib_tracechr.c 374lib_tracedmp.c 375lib_tracemse.c 376trace_buf.c 377</code> 378</blockquote> 379It is rather unlikely you will ever need to change these, unless 380you want to introduce a new debug trace level for some reasoon.<P> 381 382There is another group of files that do direct I/O via <EM>tputs()</EM>, 383computations on the terminal capabilities, or queries to the OS 384environment, but nevertheless have only fairly low complexity. These 385include: 386<blockquote> 387<code> 388lib_acs.c 389lib_beep.c 390lib_color.c 391lib_endwin.c 392lib_initscr.c 393lib_longname.c 394lib_newterm.c 395lib_options.c 396lib_termcap.c 397lib_ti.c 398lib_tparm.c 399lib_tputs.c 400lib_vidattr.c 401read_entry.c. 402</code> 403</blockquote> 404They are likely to need revision only if 405ncurses is being ported to an environment without an underlying 406terminfo capability representation. <P> 407 408These files 409have serious hooks into 410the tty driver and signal facilities: 411<blockquote> 412<code> 413lib_kernel.c 414lib_baudrate.c 415lib_raw.c 416lib_tstp.c 417lib_twait.c 418</code> 419</blockquote> 420If you run into porting snafus 421moving the package to another UNIX, the problem is likely to be in one 422of these files. 423The file <CODE>lib_print.c</CODE> uses sleep(2) and also 424falls in this category.<P> 425 426Almost all of the real work is done in the files 427<blockquote> 428<code> 429hardscroll.c 430hashmap.c 431lib_addch.c 432lib_doupdate.c 433lib_getch.c 434lib_mouse.c 435lib_mvcur.c 436lib_refresh.c 437lib_setup.c 438lib_vidattr.c 439</code> 440</blockquote> 441Most of the algorithmic complexity in the 442library lives in these files. 443If there is a real bug in <STRONG>ncurses</STRONG> itself, it's probably here. 444We'll tour some of these files in detail 445below (see <A HREF="#engine">The Engine Room</A>). <P> 446 447Finally, there is a group of files that is actually most of the 448terminfo compiler. The reason this code lives in the <STRONG>ncurses</STRONG> 449library is to support fallback to /etc/termcap. These files include 450<blockquote> 451<code> 452alloc_entry.c 453captoinfo.c 454comp_captab.c 455comp_error.c 456comp_hash.c 457comp_parse.c 458comp_scan.c 459parse_entry.c 460read_termcap.c 461write_entry.c 462</code> 463</blockquote> 464We'll discuss these in the compiler tour. 465 466<H2><A NAME="engine">The Engine Room</A></H2> 467 468<H3><A NAME="input">Keyboard Input</A></H3> 469 470All <CODE>ncurses</CODE> input funnels through the function 471<CODE>wgetch()</CODE>, defined in <CODE>lib_getch.c</CODE>. This function is 472tricky; it has to poll for keyboard and mouse events and do a running 473match of incoming input against the set of defined special keys. <P> 474 475The central data structure in this module is a FIFO queue, used to 476match multiple-character input sequences against special-key 477capabilities; also to implement pushback via <CODE>ungetch()</CODE>. <P> 478 479The <CODE>wgetch()</CODE> code distinguishes between function key 480sequences and the same sequences typed manually by doing a timed wait 481after each input character that could lead a function key sequence. 482If the entire sequence takes less than 1 second, it is assumed to have 483been generated by a function key press. <P> 484 485Hackers bruised by previous encounters with variant <CODE>select(2)</CODE> 486calls may find the code in <CODE>lib_twait.c</CODE> interesting. It deals 487with the problem that some BSD selects don't return a reliable 488time-left value. The function <CODE>timed_wait()</CODE> effectively 489simulates a System V select. 490 491<H3><A NAME="mouse">Mouse Events</A></H3> 492 493If the mouse interface is active, <CODE>wgetch()</CODE> polls for mouse 494events each call, before it goes to the keyboard for input. It is 495up to <CODE>lib_mouse.c</CODE> how the polling is accomplished; it may vary 496for different devices. <P> 497 498Under xterm, however, mouse event notifications come in via the keyboard 499input stream. They are recognized by having the <STRONG>kmous</STRONG> capability 500as a prefix. This is kind of klugey, but trying to wire in recognition of 501a mouse key prefix without going through the function-key machinery would 502be just too painful, and this turns out to imply having the prefix somewhere 503in the function-key capabilities at terminal-type initialization. <P> 504 505This kluge only works because <STRONG>kmous</STRONG> isn't actually used by any 506historic terminal type or curses implementation we know of. Best 507guess is it's a relic of some forgotten experiment in-house at Bell 508Labs that didn't leave any traces in the publicly-distributed System V 509terminfo files. If System V or XPG4 ever gets serious about using it 510again, this kluge may have to change. <P> 511 512Here are some more details about mouse event handling: <P> 513 514The <CODE>lib_mouse()</CODE>code is logically split into a lower level that 515accepts event reports in a device-dependent format and an upper level that 516parses mouse gestures and filters events. The mediating data structure is a 517circular queue of event structures. <P> 518 519Functionally, the lower level's job is to pick up primitive events and 520put them on the circular queue. This can happen in one of two ways: 521either (a) <CODE>_nc_mouse_event()</CODE> detects a series of incoming 522mouse reports and queues them, or (b) code in <CODE>lib_getch.c</CODE> detects the 523<STRONG>kmous</STRONG> prefix in the keyboard input stream and calls _nc_mouse_inline 524to queue up a series of adjacent mouse reports. <P> 525 526In either case, <CODE>_nc_mouse_parse()</CODE> should be called after the 527series is accepted to parse the digested mouse reports (low-level 528events) into a gesture (a high-level or composite event). 529 530<H3><A NAME="output">Output and Screen Updating</A></H3> 531 532With the single exception of character echoes during a <CODE>wgetnstr()</CODE> 533call (which simulates cooked-mode line editing in an ncurses window), 534the library normally does all its output at refresh time. <P> 535 536The main job is to go from the current state of the screen (as represented 537in the <CODE>curscr</CODE> window structure) to the desired new state (as 538represented in the <CODE>newscr</CODE> window structure), while doing as 539little I/O as possible. <P> 540 541The brains of this operation are the modules <CODE>hashmap.c</CODE>, 542<CODE>hardscroll.c</CODE> and <CODE>lib_doupdate.c</CODE>; the latter two use 543<CODE>lib_mvcur.c</CODE>. Essentially, what happens looks like this: <P> 544 545The <CODE>hashmap.c</CODE> module tries to detect vertical motion 546changes between the real and virtual screens. This information 547is represented by the oldindex members in the newscr structure. 548These are modified by vertical-motion and clear operations, and both are 549re-initialized after each update. To this change-journalling 550information, the hashmap code adds deductions made using a modified Heckel 551algorithm on hash values generated from the line contents. <P> 552 553The <CODE>hardscroll.c</CODE> module computes an optimum set of scroll, 554insertion, and deletion operations to make the indices match. It calls 555<CODE>_nc_mvcur_scrolln()</CODE> in <CODE>lib_mvcur.c</CODE> to do those motions. <P> 556 557Then <CODE>lib_doupdate.c</CODE> goes to work. Its job is to do line-by-line 558transformations of <CODE>curscr</CODE> lines to <CODE>newscr</CODE> lines. Its main 559tool is the routine <CODE>mvcur()</CODE> in <CODE>lib_mvcur.c</CODE>. This routine 560does cursor-movement optimization, attempting to get from given screen 561location A to given location B in the fewest output characters posible. <P> 562 563If you want to work on screen optimizations, you should use the fact 564that (in the trace-enabled version of the library) enabling the 565<CODE>TRACE_TIMES</CODE> trace level causes a report to be emitted after 566each screen update giving the elapsed time and a count of characters 567emitted during the update. You can use this to tell when an update 568optimization improves efficiency. <P> 569 570In the trace-enabled version of the library, it is also possible to disable 571and re-enable various optimizations at runtime by tweaking the variable 572<CODE>_nc_optimize_enable</CODE>. See the file <CODE>include/curses.h.in</CODE> 573for mask values, near the end. 574 575<H1><A NAME="fmnote">The Forms and Menu Libraries</A></H1> 576 577The forms and menu libraries should work reliably in any environment you 578can port ncurses to. The only portability issue anywhere in them is what 579flavor of regular expressions the built-in form field type TYPE_REGEXP 580will recognize. <P> 581 582The configuration code prefers the POSIX regex facility, modeled on 583System V's, but will settle for BSD regexps if the former isn't available. <P> 584 585Historical note: the panels code was written primarily to assist in 586porting u386mon 2.0 (comp.sources.misc v14i001-4) to systems lacking 587panels support; u386mon 2.10 and beyond use it. This version has been 588slightly cleaned up for <CODE>ncurses</CODE>. 589 590<H1><A NAME="tic">A Tour of the Terminfo Compiler</A></H1> 591 592The <STRONG>ncurses</STRONG> implementation of <STRONG>tic</STRONG> is rather complex 593internally; it has to do a trying combination of missions. This starts 594with the fact that, in addition to its normal duty of compiling 595terminfo sources into loadable terminfo binaries, it has to be able to 596handle termcap syntax and compile that too into terminfo entries. <P> 597 598The implementation therefore starts with a table-driven, dual-mode 599lexical analyzer (in <CODE>comp_scan.c</CODE>). The lexer chooses its 600mode (termcap or terminfo) based on the first `,' or `:' it finds in 601each entry. The lexer does all the work of recognizing capability 602names and values; the grammar above it is trivial, just "parse entries 603till you run out of file". 604 605<H2><A NAME="nonuse">Translation of Non-<STRONG>use</STRONG> Capabilities</A></H2> 606 607Translation of most things besides <STRONG>use</STRONG> capabilities is pretty 608straightforward. The lexical analyzer's tokenizer hands each capability 609name to a hash function, which drives a table lookup. The table entry 610yields an index which is used to look up the token type in another table, 611and controls interpretation of the value. <P> 612 613One possibly interesting aspect of the implementation is the way the 614compiler tables are initialized. All the tables are generated by various 615awk/sed/sh scripts from a master table <CODE>include/Caps</CODE>; these 616scripts actually write C initializers which are linked to the compiler. 617Furthermore, the hash table is generated in the same way, so it doesn't 618have to be generated at compiler startup time (another benefit of this 619organization is that the hash table can be in shareable text space). <P> 620 621Thus, adding a new capability is usually pretty trivial, just a matter 622of adding one line to the <CODE>include/Caps</CODE> file. We'll have more 623to say about this in the section on <A HREF="#translation">Source-Form 624Translation</A>. 625 626<H2><A NAME="uses">Use Capability Resolution</A></H2> 627 628The background problem that makes <STRONG>tic</STRONG> tricky isn't the capability 629translation itself, it's the resolution of <STRONG>use</STRONG> capabilities. Older 630versions would not handle forward <STRONG>use</STRONG> references for this reason 631(that is, a using terminal always had to follow its use target in the 632source file). By doing this, they got away with a simple implementation 633tactic; compile everything as it blows by, then resolve uses from compiled 634entries. <P> 635 636This won't do for <STRONG>ncurses</STRONG>. The problem is that that the whole 637compilation process has to be embeddable in the <STRONG>ncurses</STRONG> library 638so that it can be called by the startup code to translate termcap 639entries on the fly. The embedded version can't go promiscuously writing 640everything it translates out to disk -- for one thing, it will typically 641be running with non-root permissions. <P> 642 643So our <STRONG>tic</STRONG> is designed to parse an entire terminfo file into a 644doubly-linked circular list of entry structures in-core, and then do 645<STRONG>use</STRONG> resolution in-memory before writing everything out. This 646design has other advantages: it makes forward and back use-references 647equally easy (so we get the latter for free), and it makes checking for 648name collisions before they're written out easy to do. <P> 649 650And this is exactly how the embedded version works. But the stand-alone 651user-accessible version of <STRONG>tic</STRONG> partly reverts to the historical 652strategy; it writes to disk (not keeping in core) any entry with no 653<STRONG>use</STRONG> references. <P> 654 655This is strictly a core-economy kluge, implemented because the 656terminfo master file is large enough that some core-poor systems swap 657like crazy when you compile it all in memory...there have been reports of 658this process taking <STRONG>three hours</STRONG>, rather than the twenty seconds 659or less typical on the author's development box. <P> 660 661So. The executable <STRONG>tic</STRONG> passes the entry-parser a hook that 662<EM>immediately</EM> writes out the referenced entry if it has no use 663capabilities. The compiler main loop refrains from adding the entry 664to the in-core list when this hook fires. If some other entry later 665needs to reference an entry that got written immediately, that's OK; 666the resolution code will fetch it off disk when it can't find it in 667core. <P> 668 669Name collisions will still be detected, just not as cleanly. The 670<CODE>write_entry()</CODE> code complains before overwriting an entry that 671postdates the time of <STRONG>tic</STRONG>'s first call to 672<CODE>write_entry()</CODE>, Thus it will complain about overwriting 673entries newly made during the <STRONG>tic</STRONG> run, but not about 674overwriting ones that predate it. 675 676<H2><A NAME="translation">Source-Form Translation</A></H2> 677 678Another use of <STRONG>tic</STRONG> is to do source translation between various termcap 679and terminfo formats. There are more variants out there than you might 680think; the ones we know about are described in the <STRONG>captoinfo(1)</STRONG> 681manual page. <P> 682 683The translation output code (<CODE>dump_entry()</CODE> in 684<CODE>ncurses/dump_entry.c</CODE>) is shared with the <STRONG>infocmp(1)</STRONG> 685utility. It takes the same internal representation used to generate 686the binary form and dumps it to standard output in a specified 687format. <P> 688 689The <CODE>include/Caps</CODE> file has a header comment describing ways you 690can specify source translations for nonstandard capabilities just by 691altering the master table. It's possible to set up capability aliasing 692or tell the compiler to plain ignore a given capability without writing 693any C code at all. <P> 694 695For circumstances where you need to do algorithmic translation, there 696are functions in <CODE>parse_entry.c</CODE> called after the parse of each 697entry that are specifically intended to encapsulate such 698translations. This, for example, is where the AIX <STRONG>box1</STRONG> capability 699get translated to an <STRONG>acsc</STRONG> string. 700 701<H1><A NAME="utils">Other Utilities</A></H1> 702 703The <STRONG>infocmp</STRONG> utility is just a wrapper around the same 704entry-dumping code used by <STRONG>tic</STRONG> for source translation. Perhaps 705the one interesting aspect of the code is the use of a predicate 706function passed in to <CODE>dump_entry()</CODE> to control which 707capabilities are dumped. This is necessary in order to handle both 708the ordinary De-compilation case and entry difference reporting. <P> 709 710The <STRONG>tput</STRONG> and <STRONG>clear</STRONG> utilities just do an entry load 711followed by a <CODE>tputs()</CODE> of a selected capability. 712 713<H1><A NAME="style">Style Tips for Developers</A></H1> 714 715See the TO-DO file in the top-level directory of the source distribution 716for additions that would be particularly useful. <P> 717 718The prefix <CODE>_nc_</CODE> should be used on library public functions that are 719not part of the curses API in order to prevent pollution of the 720application namespace. 721 722If you have to add to or modify the function prototypes in curses.h.in, 723read ncurses/MKlib_gen.sh first so you can avoid breaking XSI conformance. 724 725Please join the ncurses mailing list. See the INSTALL file in the 726top level of the distribution for details on the list. <P> 727 728Look for the string <CODE>FIXME</CODE> in source files to tag minor bugs 729and potential problems that could use fixing. <P> 730 731Don't try to auto-detect OS features in the main body of the C code. 732That's the job of the configuration system. <P> 733 734To hold down complexity, do make your code data-driven. Especially, 735if you can drive logic from a table filtered out of 736<CODE>include/Caps</CODE>, do it. If you find you need to augment the 737data in that file in order to generate the proper table, that's still 738preferable to ad-hoc code -- that's why the fifth field (flags) is 739there. <P> 740 741Have fun! 742 743<H1><A NAME="port">Porting Hints</A></H1> 744 745The following notes are intended to be a first step towards DOS and Macintosh 746ports of the ncurses libraries. <P> 747 748The following library modules are `pure curses'; they operate only on 749the curses internal structures, do all output through other curses 750calls (not including <CODE>tputs()</CODE> and <CODE>putp()</CODE>) and do not 751call any other UNIX routines such as signal(2) or the stdio library. 752Thus, they should not need to be modified for single-terminal 753ports. 754 755<blockquote> 756<code> 757lib_addch.c 758lib_addstr.c 759lib_bkgd.c 760lib_box.c 761lib_clear.c 762lib_clrbot.c 763lib_clreol.c 764lib_delch.c 765lib_delwin.c 766lib_erase.c 767lib_inchstr.c 768lib_insch.c 769lib_insdel.c 770lib_insstr.c 771lib_keyname.c 772lib_move.c 773lib_mvwin.c 774lib_newwin.c 775lib_overlay.c 776lib_pad.c 777lib_printw.c 778lib_refresh.c 779lib_scanw.c 780lib_scroll.c 781lib_scrreg.c 782lib_set_term.c 783lib_touch.c 784lib_tparm.c 785lib_tputs.c 786lib_unctrl.c 787lib_window.c 788panel.c 789</code> 790</blockquote> 791<P> 792 793This module is pure curses, but calls outstr(): 794 795<blockquote> 796<code> 797lib_getstr.c 798</code> 799</blockquote> 800<P> 801 802These modules are pure curses, except that they use <CODE>tputs()</CODE> 803and <CODE>putp()</CODE>: 804 805<blockquote> 806<code> 807lib_beep.c 808lib_color.c 809lib_endwin.c 810lib_options.c 811lib_slk.c 812lib_vidattr.c 813</code> 814</blockquote> 815<P> 816 817This modules assist in POSIX emulation on non-POSIX systems: 818<DL> 819<DT> sigaction.c 820<DD> signal calls 821</DL> 822 823The following source files will not be needed for a 824single-terminal-type port. 825 826<blockquote> 827<code> 828alloc_entry.c 829captoinfo.c 830clear.c 831comp_captab.c 832comp_error.c 833comp_hash.c 834comp_main.c 835comp_parse.c 836comp_scan.c 837dump_entry.c 838infocmp.c 839parse_entry.c 840read_entry.c 841tput.c 842write_entry.c 843</code> 844</blockquote> 845<P> 846 847The following modules will use open()/read()/write()/close()/lseek() on files, 848but no other OS calls. 849 850<DL> 851<DT>lib_screen.c 852<DD>used to read/write screen dumps 853<DT>lib_trace.c 854<DD>used to write trace data to the logfile 855</DL> 856 857Modules that would have to be modified for a port start here: <P> 858 859The following modules are `pure curses' but contain assumptions inappropriate 860for a memory-mapped port. 861 862<dl> 863<dt>lib_longname.c<dd>assumes there may be multiple terminals 864<dt>lib_acs.c<dd>assumes acs_map as a double indirection 865<dt>lib_mvcur.c<dd>assumes cursor moves have variable cost 866<dt>lib_termcap.c<dd>assumes there may be multiple terminals 867<dt>lib_ti.c<dd>assumes there may be multiple terminals 868</dl> 869 870The following modules use UNIX-specific calls: 871 872<dl> 873<dt>lib_doupdate.c<dd>input checking 874<dt>lib_getch.c<dd>read() 875<dt>lib_initscr.c<dd>getenv() 876<dt>lib_newterm.c 877<dt>lib_baudrate.c 878<dt>lib_kernel.c<dd>various tty-manipulation and system calls 879<dt>lib_raw.c<dd>various tty-manipulation calls 880<dt>lib_setup.c<dd>various tty-manipulation calls 881<dt>lib_restart.c<dd>various tty-manipulation calls 882<dt>lib_tstp.c<dd>signal-manipulation calls 883<dt>lib_twait.c<dd>gettimeofday(), select(). 884</dl> 885 886<HR> 887<ADDRESS>Eric S. Raymond <esr@snark.thyrsus.com></ADDRESS> 888(Note: This is <EM>not</EM> the <A HREF="#bugtrack">bug address</A>!) 889</BODY> 890</HTML> 891