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