]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/curses.priv.h
ncurses 5.7 - patch 20090425
[ncurses.git] / ncurses / curses.priv.h
1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  *     and: Juergen Pfeifer                                                 *
34  ****************************************************************************/
35
36
37 /*
38  * $Id: curses.priv.h,v 1.410 2009/04/25 23:38:37 tom Exp $
39  *
40  *      curses.priv.h
41  *
42  *      Header file for curses library objects which are private to
43  *      the library.
44  *
45  */
46
47 #ifndef CURSES_PRIV_H
48 #define CURSES_PRIV_H 1
49
50 #include <ncurses_dll.h>
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56 #include <ncurses_cfg.h>
57
58 #if USE_RCS_IDS
59 #define MODULE_ID(id) static const char Ident[] = id;
60 #else
61 #define MODULE_ID(id) /*nothing*/
62 #endif
63
64 #include <stdlib.h>
65 #include <string.h>
66 #include <sys/types.h>
67
68 #if HAVE_UNISTD_H
69 #include <unistd.h>
70 #endif
71
72 #if HAVE_SYS_BSDTYPES_H
73 #include <sys/bsdtypes.h>       /* needed for ISC */
74 #endif
75
76 #if HAVE_LIMITS_H
77 # include <limits.h>
78 #elif HAVE_SYS_PARAM_H
79 # include <sys/param.h>
80 #endif
81
82 #include <assert.h>
83 #include <stdio.h>
84
85 #include <errno.h>
86
87 #ifndef PATH_MAX
88 # if defined(_POSIX_PATH_MAX)
89 #  define PATH_MAX _POSIX_PATH_MAX
90 # elif defined(MAXPATHLEN)
91 #  define PATH_MAX MAXPATHLEN
92 # else
93 #  define PATH_MAX 255  /* the Posix minimum path-size */
94 # endif
95 #endif
96
97 #if DECL_ERRNO
98 extern int errno;
99 #endif
100
101 /* Some systems have a broken 'select()', but workable 'poll()'.  Use that */
102 #if HAVE_WORKING_POLL
103 #define USE_FUNC_POLL 1
104 #if HAVE_POLL_H
105 #include <poll.h>
106 #else
107 #include <sys/poll.h>
108 #endif
109 #else
110 #define USE_FUNC_POLL 0
111 #endif
112
113 /* include signal.h before curses.h to work-around defect in glibc 2.1.3 */
114 #include <signal.h>
115
116 /* Alessandro Rubini's GPM (general-purpose mouse) */
117 #if HAVE_LIBGPM && HAVE_GPM_H
118 #define USE_GPM_SUPPORT 1
119 #else
120 #define USE_GPM_SUPPORT 0
121 #endif
122
123 /* QNX mouse support */
124 #if defined(__QNX__) && !defined(__QNXNTO__)
125 #define USE_QNX_MOUSE 1
126 #else
127 #define USE_QNX_MOUSE 0
128 #endif
129
130 /* EMX mouse support */
131 #ifdef __EMX__
132 #define USE_EMX_MOUSE 1
133 #else
134 #define USE_EMX_MOUSE 0
135 #endif
136
137 #define DEFAULT_MAXCLICK 166
138 #define EV_MAX          8       /* size of mouse circular event queue */
139
140 /*
141  * If we don't have signals to support it, don't add a sigwinch handler.
142  * In any case, resizing is an extended feature.  Use it if we've got it.
143  */
144 #if !NCURSES_EXT_FUNCS
145 #undef HAVE_SIZECHANGE
146 #define HAVE_SIZECHANGE 0
147 #endif
148
149 #if HAVE_SIZECHANGE && USE_SIGWINCH && defined(SIGWINCH)
150 #define USE_SIZECHANGE 1
151 #else
152 #define USE_SIZECHANGE 0
153 #undef USE_SIGWINCH
154 #define USE_SIGWINCH 0
155 #endif
156
157 /*
158  * If desired, one can configure this, disabling environment variables that
159  * point to custom terminfo/termcap locations.
160  */
161 #ifdef USE_ROOT_ENVIRON
162 #define use_terminfo_vars() 1
163 #else
164 #define use_terminfo_vars() _nc_env_access()
165 extern NCURSES_EXPORT(int) _nc_env_access (void);
166 #endif
167
168 /*
169  * Not all platforms have memmove; some have an equivalent bcopy.  (Some may
170  * have neither).
171  */
172 #if USE_OK_BCOPY
173 #define memmove(d,s,n) bcopy(s,d,n)
174 #elif USE_MY_MEMMOVE
175 #define memmove(d,s,n) _nc_memmove(d,s,n)
176 extern NCURSES_EXPORT(void *) _nc_memmove (void *, const void *, size_t);
177 #endif
178
179 /*
180  * Scroll hints are useless when hashmap is used
181  */
182 #if !USE_SCROLL_HINTS
183 #if !USE_HASHMAP
184 #define USE_SCROLL_HINTS 1
185 #else
186 #define USE_SCROLL_HINTS 0
187 #endif
188 #endif
189
190 #if USE_SCROLL_HINTS
191 #define if_USE_SCROLL_HINTS(stmt) stmt
192 #else
193 #define if_USE_SCROLL_HINTS(stmt) /*nothing*/
194 #endif
195
196 /*
197  * Note:  ht/cbt expansion flakes out randomly under Linux 1.1.47, but only
198  * when we're throwing control codes at the screen at high volume.  To see
199  * this, re-enable USE_HARD_TABS and run worm for a while.  Other systems
200  * probably don't want to define this either due to uncertainties about tab
201  * delays and expansion in raw mode.
202  */
203
204 #define TRIES struct tries
205 typedef TRIES {
206         TRIES    *child;            /* ptr to child.  NULL if none          */
207         TRIES    *sibling;          /* ptr to sibling.  NULL if none        */
208         unsigned char    ch;        /* character at this node               */
209         unsigned short   value;     /* code of string so far.  0 if none.   */
210 #undef TRIES
211 } TRIES;
212
213 /*
214  * Common/troublesome character definitions
215  */
216 #define StringOf(ch) {ch, 0}
217
218 #define L_BRACE '{'
219 #define R_BRACE '}'
220 #define S_QUOTE '\''
221 #define D_QUOTE '"'
222
223 #define VT_ACSC "``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~"
224
225 /*
226  * Structure for palette tables
227  */
228
229 typedef struct
230 {
231     short red, green, blue;     /* what color_content() returns */
232     short r, g, b;              /* params to init_color() */
233     int init;                   /* true if we called init_color() */
234 }
235 color_t;
236
237 #define MAXCOLUMNS    135
238 #define MAXLINES      66
239 #define FIFO_SIZE     MAXCOLUMNS+2  /* for nocbreak mode input */
240
241 #define ACS_LEN       128
242
243 #define WINDOWLIST struct _win_list
244
245 #if USE_WIDEC_SUPPORT
246 #define _nc_bkgd    _bkgrnd
247 #else
248 #undef _XOPEN_SOURCE_EXTENDED
249 #define _nc_bkgd    _bkgd
250 #define wgetbkgrnd(win, wch)    *wch = win->_bkgd
251 #define wbkgrnd     wbkgd
252 #endif
253
254 #undef NCURSES_OPAQUE
255 #define NCURSES_INTERNALS 1
256 #define NCURSES_OPAQUE 0
257
258 #include <curses.h>     /* we'll use -Ipath directive to get the right one! */
259
260 /*
261  * If curses.h did not expose the SCREEN-functions, then we do not need the
262  * parameter in the corresponding unextended functions.
263  */
264 #if NCURSES_SP_FUNCS
265 #define SP_PARM         sp      /* use parameter */
266 #define NCURSES_SP_ARG          SP_PARM
267 #define NCURSES_SP_DCL  SCREEN *NCURSES_SP_ARG
268 #define NCURSES_SP_DCL0 NCURSES_SP_DCL
269 #define NCURSES_SP_ARGx         NCURSES_SP_ARG,
270 #define NCURSES_SP_DCLx SCREEN *NCURSES_SP_ARGx
271 #else
272 #define SP_PARM         SP      /* use global variable */
273 #define NCURSES_SP_ARG
274 #define NCURSES_SP_DCL
275 #define NCURSES_SP_DCL0 void
276 #define NCURSES_SP_ARGx
277 #define NCURSES_SP_DCLx
278 #endif
279
280 #include <nc_panel.h>
281
282 #define IsPreScreen(sp)      (((sp)!=0) && sp->_prescreen)
283 #define HasTerminal(sp)      (((sp)!=0) && (0!=((sp)->_term)))
284 #define IsValidScreen(sp)    (HasTerminal(sp) && !sp->_prescreen)
285 #if BROKEN_LINKER || USE_REENTRANT
286 #define TerminalOf(sp)       ((sp)?((sp)->_term?(sp)->_term:_nc_prescreen._cur_term): _nc_prescreen._cur_term)
287 #else
288 #define TerminalOf(sp)       ((sp)?((sp)->_term?(sp)->_term:cur_term):cur_term)
289 #endif
290
291 #include <term.h>
292 #include <term_entry.h>
293 #include <nc_tparm.h>
294
295 #if NCURSES_EXT_COLORS && USE_WIDEC_SUPPORT
296 #define if_EXT_COLORS(stmt)     stmt
297 #define NetPair(value,p)        (value).ext_color = (p), \
298                                 AttrOf(value) &= ALL_BUT_COLOR, \
299                                 AttrOf(value) |= (A_COLOR & COLOR_PAIR((p > 255) ? 255 : p))
300 #define SetPair(value,p)        (value).ext_color = (p)
301 #define GetPair(value)          (value).ext_color
302 #define unColor(n)              (AttrOf(n) & ALL_BUT_COLOR)
303 #define GET_WINDOW_PAIR(w)      (w)->_color
304 #define SET_WINDOW_PAIR(w,p)    (w)->_color = (p)
305 #define SameAttrOf(a,b)         (AttrOf(a) == AttrOf(b) && GetPair(a) == GetPair(b))
306 #define VIDATTR(attr, pair)     vid_attr(attr, pair, 0)
307 #else
308 #define if_EXT_COLORS(stmt)     /* nothing */
309 #define SetPair(value,p)        RemAttr(value, A_COLOR), \
310                                 SetAttr(value, AttrOf(value) | (A_COLOR & COLOR_PAIR(p)))
311 #define GetPair(value)          PAIR_NUMBER(AttrOf(value))
312 #define unColor(n)              (AttrOf(n) & ALL_BUT_COLOR)
313 #define GET_WINDOW_PAIR(w)      PAIR_NUMBER(WINDOW_ATTRS(w))
314 #define SET_WINDOW_PAIR(w,p)    WINDOW_ATTRS(w) &= ALL_BUT_COLOR, \
315                                 WINDOW_ATTRS(w) |= (A_COLOR & COLOR_PAIR(p))
316 #define SameAttrOf(a,b)         (AttrOf(a) == AttrOf(b))
317 #define VIDATTR(attr, pair)     vidattr(attr)
318 #endif
319
320 #if NCURSES_NO_PADDING
321 #define GetNoPadding(sp)        ((sp) ? (sp)->_no_padding : _nc_prescreen._no_padding)
322 #define SetNoPadding(sp)        _nc_set_no_padding(sp)
323 extern NCURSES_EXPORT(void)     _nc_set_no_padding(SCREEN *);
324 #else
325 #define GetNoPadding(sp)        FALSE
326 #define SetNoPadding(sp)        /*nothing*/
327 #endif
328
329 #define WINDOW_ATTRS(w)         ((w)->_attrs)
330
331 #define SCREEN_ATTRS(s)         (*((s)->_current_attr))
332 #define GET_SCREEN_PAIR(s)      GetPair(SCREEN_ATTRS(s))
333 #define SET_SCREEN_PAIR(s,p)    SetPair(SCREEN_ATTRS(s), p)
334
335 #if USE_REENTRANT
336 NCURSES_EXPORT(int *)        _nc_ptr_Lines (void);
337 NCURSES_EXPORT(int *)        _nc_ptr_Cols (void);
338 #define ptrLines(sp)         (sp ? &(sp->_LINES) : &(_nc_prescreen._LINES))
339 #define ptrCols(sp)          (sp ? &(sp->_COLS) : &(_nc_prescreen._COLS))
340 #define SET_LINES(value)     *_nc_ptr_Lines() = value
341 #define SET_COLS(value)      *_nc_ptr_Cols() = value
342 #else
343 #define ptrLines(sp)         &LINES
344 #define ptrCols(sp)          &COLS
345 #define SET_LINES(value)     LINES = value
346 #define SET_COLS(value)      COLS = value
347 #endif
348
349 #define TR_MUTEX(data) _tracef("%s@%d: me:%08lX COUNT:%2u/%2d/%6d/%2d/%s%9u: " #data, \
350             __FILE__, __LINE__, \
351             (unsigned long) (pthread_self()), \
352             data.__data.__lock, \
353             data.__data.__count, \
354             data.__data.__owner, \
355             data.__data.__kind, \
356             (data.__data.__nusers > 5) ? " OOPS " : "", \
357             data.__data.__nusers)
358 #define TR_GLOBAL_MUTEX(name) TR_MUTEX(_nc_globals.mutex_##name)
359
360 #ifdef USE_PTHREADS
361
362 #if USE_REENTRANT
363 #include <pthread.h>
364 extern NCURSES_EXPORT(void) _nc_init_pthreads(void);
365 extern NCURSES_EXPORT(void) _nc_mutex_init(pthread_mutex_t *);
366 extern NCURSES_EXPORT(int) _nc_mutex_lock(pthread_mutex_t *);
367 extern NCURSES_EXPORT(int) _nc_mutex_trylock(pthread_mutex_t *);
368 extern NCURSES_EXPORT(int) _nc_mutex_unlock(pthread_mutex_t *);
369 #define _nc_lock_global(name)   _nc_mutex_lock(&_nc_globals.mutex_##name)
370 #define _nc_try_global(name)    _nc_mutex_trylock(&_nc_globals.mutex_##name)
371 #define _nc_unlock_global(name) _nc_mutex_unlock(&_nc_globals.mutex_##name)
372
373 #else
374 #error POSIX threads requires --enable-reentrant option
375 #endif
376
377 #if USE_WEAK_SYMBOLS
378 #if defined(__GNUC__)
379 #  if defined __USE_ISOC99
380 #    define _cat_pragma(exp)    _Pragma(#exp)
381 #    define _weak_pragma(exp)   _cat_pragma(weak name)
382 #  else
383 #    define _weak_pragma(exp)
384 #  endif
385 #  define _declare(name)        __extension__ extern __typeof__(name) name
386 #  define weak_symbol(name)     _weak_pragma(name) _declare(name) __attribute__((weak))
387 #endif
388 #endif
389
390 #ifdef USE_PTHREADS
391 #  if USE_WEAK_SYMBOLS
392 weak_symbol(pthread_sigmask);
393 weak_symbol(pthread_self);
394 weak_symbol(pthread_equal);
395 weak_symbol(pthread_mutex_init);
396 weak_symbol(pthread_mutex_lock);
397 weak_symbol(pthread_mutex_unlock);
398 weak_symbol(pthread_mutex_trylock);
399 weak_symbol(pthread_mutexattr_settype);
400 weak_symbol(pthread_mutexattr_init);
401 extern NCURSES_EXPORT(int) _nc_sigprocmask(int, const sigset_t *, sigset_t *);
402 #    undef  sigprocmask
403 #    define sigprocmask _nc_sigprocmask
404 #  endif
405 #endif
406
407 #if HAVE_NANOSLEEP
408 #undef HAVE_NANOSLEEP
409 #define HAVE_NANOSLEEP 0        /* nanosleep suspends all threads */
410 #endif
411
412 #else /* !USE_PTHREADS */
413
414 #define _nc_init_pthreads()     /* nothing */
415 #define _nc_mutex_init(obj)     /* nothing */
416
417 #define _nc_lock_global(name)   /* nothing */
418 #define _nc_try_global(name)    0
419 #define _nc_unlock_global(name) /* nothing */
420
421 #endif /* USE_PTHREADS */
422
423 #if HAVE_GETTIMEOFDAY
424 # define PRECISE_GETTIME 1
425 # define TimeType struct timeval
426 #else
427 # define PRECISE_GETTIME 0
428 # define TimeType time_t
429 #endif
430
431 /*
432  * Definitions for color pairs
433  */
434 typedef unsigned colorpair_t;   /* type big enough to store PAIR_OF() */
435 #define C_SHIFT 9               /* we need more bits than there are colors */
436 #define C_MASK                  ((1 << C_SHIFT) - 1)
437 #define PAIR_OF(fg, bg)         ((((fg) & C_MASK) << C_SHIFT) | ((bg) & C_MASK))
438 #define FORE_OF(c)              (((c) >> C_SHIFT) & C_MASK)
439 #define BACK_OF(c)              ((c) & C_MASK)
440 #define isDefaultColor(c)       ((c) >= COLOR_DEFAULT || (c) < 0)
441
442 #define COLOR_DEFAULT           C_MASK
443
444 #if defined(USE_TERMLIB) && !defined(NEED_NCURSES_CH_T)
445
446 #undef NCURSES_CH_T             /* this is not a termlib feature */
447 #define NCURSES_CH_T void       /* ...but we need a pointer in SCREEN */
448
449 #endif  /* USE_TERMLIB */
450
451 #ifndef USE_TERMLIB
452 struct ldat
453 {
454         NCURSES_CH_T    *text;          /* text of the line */
455         NCURSES_SIZE_T  firstchar;      /* first changed character in the line */
456         NCURSES_SIZE_T  lastchar;       /* last changed character in the line */
457         NCURSES_SIZE_T  oldindex;       /* index of the line at last update */
458 };
459 #endif  /* USE_TERMLIB */
460
461 typedef enum {
462         M_XTERM = -1            /* use xterm's mouse tracking? */
463         ,M_NONE = 0             /* no mouse device */
464 #if USE_GPM_SUPPORT
465         ,M_GPM                  /* use GPM */
466 #endif
467 #if USE_SYSMOUSE
468         ,M_SYSMOUSE             /* FreeBSD sysmouse on console */
469 #endif
470 } MouseType;
471
472 /*
473  * Structures for scrolling.
474  */
475
476 typedef struct {
477         unsigned long hashval;
478         int oldcount, newcount;
479         int oldindex, newindex;
480 } HASHMAP;
481
482 /*
483  * Structures for soft labels.
484  */
485
486 struct _SLK;
487
488 #ifndef USE_TERMLIB
489
490 typedef struct
491 {
492         char *ent_text;         /* text for the label */
493         char *form_text;        /* formatted text (left/center/...) */
494         int ent_x;              /* x coordinate of this field */
495         char dirty;             /* this label has changed */
496         char visible;           /* field is visible */
497 } slk_ent;
498
499 typedef struct _SLK {
500         bool    dirty;          /* all labels have changed */
501         bool    hidden;         /* soft labels are hidden */
502         WINDOW  *win;
503         slk_ent *ent;
504         short   maxlab;         /* number of available labels */
505         short   labcnt;         /* number of allocated labels */
506         short   maxlen;         /* length of labels */
507         NCURSES_CH_T attr;      /* soft label attribute */
508 } SLK;
509
510 #endif  /* USE_TERMLIB */
511
512 typedef struct {
513         WINDOW *win;            /* the window used in the hook      */
514         int     line;           /* lines to take, < 0 => from bottom*/
515         int     (*hook)(WINDOW *, int); /* callback for user        */
516 } ripoff_t;
517
518 #if USE_GPM_SUPPORT
519 #undef buttons                  /* term.h defines this, and gpm uses it! */
520 #include <gpm.h>
521
522 #ifdef HAVE_LIBDL
523 /* link dynamically to GPM */
524 typedef int *TYPE_gpm_fd;
525 typedef int (*TYPE_Gpm_Open) (Gpm_Connect *, int);
526 typedef int (*TYPE_Gpm_Close) (void);
527 typedef int (*TYPE_Gpm_GetEvent) (Gpm_Event *);
528
529 #define my_gpm_fd       SP_PARM->_mouse_gpm_fd
530 #define my_Gpm_Open     SP_PARM->_mouse_Gpm_Open
531 #define my_Gpm_Close    SP_PARM->_mouse_Gpm_Close
532 #define my_Gpm_GetEvent SP_PARM->_mouse_Gpm_GetEvent
533 #else
534 /* link statically to GPM */
535 #define my_gpm_fd       &gpm_fd
536 #define my_Gpm_Open     Gpm_Open
537 #define my_Gpm_Close    Gpm_Close
538 #define my_Gpm_GetEvent Gpm_GetEvent
539 #endif /* HAVE_LIBDL */
540 #endif /* USE_GPM_SUPPORT */
541
542 typedef struct {
543     long sequence;
544     bool last_used;
545     char *fix_sgr0;             /* this holds the filtered sgr0 string */
546     char *last_bufp;            /* help with fix_sgr0 leak */
547     TERMINAL *last_term;
548 } TGETENT_CACHE;
549
550 #define TGETENT_MAX 4
551
552 /*
553  * State of tparm().
554  */
555 #define STACKSIZE 20
556
557 typedef struct {
558         union {
559                 int     num;
560                 char    *str;
561         } data;
562         bool num_type;
563 } STACK_FRAME;
564
565 #define NUM_VARS 26
566
567 typedef struct {
568 #ifdef TRACE
569         const char      *tname;
570 #endif
571         const char      *tparam_base;
572
573         STACK_FRAME     stack[STACKSIZE];
574         int             stack_ptr;
575
576         char            *out_buff;
577         size_t          out_size;
578         size_t          out_used;
579
580         char            *fmt_buff;
581         size_t          fmt_size;
582
583         int             dynamic_var[NUM_VARS];
584         int             static_vars[NUM_VARS];
585 } TPARM_STATE;
586
587 typedef struct {
588     char *text;
589     size_t size;
590 } TRACEBUF;
591
592 /*
593  * The filesystem database normally uses a single-letter for the lower level
594  * of directories.  Use a hexadecimal code for filesystems which do not
595  * preserve mixed-case names.
596  */
597 #if MIXEDCASE_FILENAMES
598 #define LEAF_FMT "%c"
599 #else
600 #define LEAF_FMT "%02x"
601 #endif
602
603 /*
604  * TRACEMSE_FMT is no longer than 80 columns, there are 5 numbers that
605  * could at most have 10 digits, and the mask contains no more than 32 bits
606  * with each bit representing less than 15 characters.  Usually the whole
607  * string is less than 80 columns, but this buffer size is an absolute
608  * limit.
609  */
610 #define TRACEMSE_MAX    (80 + (5 * 10) + (32 * 15))
611 #define TRACEMSE_FMT    "id %2d  at (%2d, %2d, %2d) state %4lx = {" /* } */
612
613 /*
614  * Global data which is not specific to a screen.
615  */
616 typedef struct {
617         SIG_ATOMIC_T    have_sigwinch;
618         SIG_ATOMIC_T    cleanup_nested;
619
620         bool            init_signals;
621         bool            init_screen;
622
623         const char      *comp_sourcename;
624         char            *comp_termtype;
625
626         bool            have_tic_directory;
627         bool            keep_tic_directory;
628         const char      *tic_directory;
629
630         char            *dbi_list;
631         int             dbi_size;
632
633         char            *first_name;
634         char            **keyname_table;
635
636         int             slk_format;
637
638         char            *safeprint_buf;
639         size_t          safeprint_used;
640
641         TGETENT_CACHE   tgetent_cache[TGETENT_MAX];
642         int             tgetent_index;
643         long            tgetent_sequence;
644
645         WINDOWLIST      *_nc_windowlist;
646 #define _nc_windows     _nc_globals._nc_windowlist
647
648 #if USE_HOME_TERMINFO
649         char            *home_terminfo;
650 #endif
651
652 #if !USE_SAFE_SPRINTF
653         int             safeprint_cols;
654         int             safeprint_rows;
655 #endif
656
657 #ifdef TRACE
658         bool            init_trace;
659         char            trace_fname[PATH_MAX];
660         int             trace_level;
661         FILE            *trace_fp;
662
663         char            *tracearg_buf;
664         size_t          tracearg_used;
665
666         TRACEBUF        *tracebuf_ptr;
667         size_t          tracebuf_used;
668
669         char            tracechr_buf[40];
670
671         char            *tracedmp_buf;
672         size_t          tracedmp_used;
673
674         unsigned char   *tracetry_buf;
675         size_t          tracetry_used;
676
677         char            traceatr_color_buf[2][80];
678         int             traceatr_color_sel;
679         int             traceatr_color_last;
680
681 #endif  /* TRACE */
682
683 #ifdef USE_PTHREADS
684         pthread_mutex_t mutex_curses;
685         pthread_mutex_t mutex_tst_tracef;
686         pthread_mutex_t mutex_tracef;
687         int             nested_tracef;
688         int             use_pthreads;
689 #define _nc_use_pthreads        _nc_globals.use_pthreads
690 #endif
691 } NCURSES_GLOBALS;
692
693 extern NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals;
694
695 #define N_RIPS 5
696
697 /*
698  * Global data which can be swept up into a SCREEN when one is created.
699  * It may be modified before the next SCREEN is created.
700  */
701 typedef struct {
702         bool            use_env;
703         bool            filter_mode;
704         attr_t          previous_attr;
705         ripoff_t        rippedoff[N_RIPS];
706         ripoff_t        *rsp;
707         TPARM_STATE     tparm_state;
708         TTY             *saved_tty;     /* savetty/resetty information      */
709 #if NCURSES_NO_PADDING
710         bool            _no_padding;    /* flag to set if padding disabled  */
711 #endif
712 #if BROKEN_LINKER || USE_REENTRANT
713         chtype          *real_acs_map;
714         int             _LINES;
715         int             _COLS;
716         TERMINAL        *_cur_term;
717 #ifdef TRACE
718         long            _outchars;
719         const char      *_tputs_trace;
720 #endif
721 #endif
722 } NCURSES_PRESCREEN;
723
724 #define ripoff_sp       _nc_prescreen.rsp
725 #define ripoff_stack    _nc_prescreen.rippedoff
726
727 extern NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen;
728
729 /*
730  * The SCREEN structure.
731  */
732
733 struct screen {
734         int             _ifd;           /* input file ptr for screen        */
735         FILE            *_ofp;          /* output file ptr for screen       */
736         char            *_setbuf;       /* buffered I/O for output          */
737         bool            _filtered;      /* filter() was called              */
738         bool            _buffered;      /* setvbuf uses _setbuf data        */
739         bool            _prescreen;     /* is in prescreen phase            */
740         bool            _use_env;       /* LINES & COLS from environment?   */
741         int             _checkfd;       /* filedesc for typeahead check     */
742         TERMINAL        *_term;         /* terminal type information        */
743         TTY             _saved_tty;     /* savetty/resetty information      */
744         NCURSES_SIZE_T  _lines;         /* screen lines                     */
745         NCURSES_SIZE_T  _columns;       /* screen columns                   */
746
747         NCURSES_SIZE_T  _lines_avail;   /* lines available for stdscr       */
748         NCURSES_SIZE_T  _topstolen;     /* lines stolen from top            */
749
750         WINDOW          *_curscr;       /* current screen                   */
751         WINDOW          *_newscr;       /* virtual screen to be updated to  */
752         WINDOW          *_stdscr;       /* screen's full-window context     */
753
754         TRIES           *_keytry;       /* "Try" for use with keypad mode   */
755         TRIES           *_key_ok;       /* Disabled keys via keyok(,FALSE)  */
756         bool            _tried;         /* keypad mode was initialized      */
757         bool            _keypad_on;     /* keypad mode is currently on      */
758
759         bool            _called_wgetch; /* check for recursion in wgetch()  */
760         int             _fifo[FIFO_SIZE];       /* input push-back buffer   */
761         short           _fifohead,      /* head of fifo queue               */
762                         _fifotail,      /* tail of fifo queue               */
763                         _fifopeek,      /* where to peek for next char      */
764                         _fifohold;      /* set if breakout marked           */
765
766         int             _endwin;        /* are we out of window mode?       */
767         NCURSES_CH_T    *_current_attr; /* holds current attributes set     */
768         int             _coloron;       /* is color enabled?                */
769         int             _color_defs;    /* are colors modified              */
770         int             _cursor;        /* visibility of the cursor         */
771         int             _cursrow;       /* physical cursor row              */
772         int             _curscol;       /* physical cursor column           */
773         bool            _notty;         /* true if we cannot switch non-tty */
774         int             _nl;            /* True if NL -> CR/NL is on        */
775         int             _raw;           /* True if in raw mode              */
776         int             _cbreak;        /* 1 if in cbreak mode              */
777                                         /* > 1 if in halfdelay mode         */
778         int             _echo;          /* True if echo on                  */
779         int             _use_meta;      /* use the meta key?                */
780         struct _SLK     *_slk;          /* ptr to soft key struct / NULL    */
781         int             slk_format;     /* selected format for this screen  */
782         /* cursor movement costs; units are 10ths of milliseconds */
783 #if NCURSES_NO_PADDING
784         bool            _no_padding;    /* flag to set if padding disabled  */
785 #endif
786         int             _char_padding;  /* cost of character put            */
787         int             _cr_cost;       /* cost of (carriage_return)        */
788         int             _cup_cost;      /* cost of (cursor_address)         */
789         int             _home_cost;     /* cost of (cursor_home)            */
790         int             _ll_cost;       /* cost of (cursor_to_ll)           */
791 #if USE_HARD_TABS
792         int             _ht_cost;       /* cost of (tab)                    */
793         int             _cbt_cost;      /* cost of (backtab)                */
794 #endif /* USE_HARD_TABS */
795         int             _cub1_cost;     /* cost of (cursor_left)            */
796         int             _cuf1_cost;     /* cost of (cursor_right)           */
797         int             _cud1_cost;     /* cost of (cursor_down)            */
798         int             _cuu1_cost;     /* cost of (cursor_up)              */
799         int             _cub_cost;      /* cost of (parm_cursor_left)       */
800         int             _cuf_cost;      /* cost of (parm_cursor_right)      */
801         int             _cud_cost;      /* cost of (parm_cursor_down)       */
802         int             _cuu_cost;      /* cost of (parm_cursor_up)         */
803         int             _hpa_cost;      /* cost of (column_address)         */
804         int             _vpa_cost;      /* cost of (row_address)            */
805         /* used in tty_update.c, must be chars */
806         int             _ed_cost;       /* cost of (clr_eos)                */
807         int             _el_cost;       /* cost of (clr_eol)                */
808         int             _el1_cost;      /* cost of (clr_bol)                */
809         int             _dch1_cost;     /* cost of (delete_character)       */
810         int             _ich1_cost;     /* cost of (insert_character)       */
811         int             _dch_cost;      /* cost of (parm_dch)               */
812         int             _ich_cost;      /* cost of (parm_ich)               */
813         int             _ech_cost;      /* cost of (erase_chars)            */
814         int             _rep_cost;      /* cost of (repeat_char)            */
815         int             _hpa_ch_cost;   /* cost of (column_address)         */
816         int             _cup_ch_cost;   /* cost of (cursor_address)         */
817         int             _cuf_ch_cost;   /* cost of (parm_cursor_right)      */
818         int             _inline_cost;   /* cost of inline-move              */
819         int             _smir_cost;     /* cost of (enter_insert_mode)      */
820         int             _rmir_cost;     /* cost of (exit_insert_mode)       */
821         int             _ip_cost;       /* cost of (insert_padding)         */
822         /* used in lib_mvcur.c */
823         char *          _address_cursor;
824         /* used in tty_update.c */
825         int             _scrolling;     /* 1 if terminal's smart enough to  */
826
827         /* used in lib_color.c */
828         color_t         *_color_table;  /* screen's color palette            */
829         int             _color_count;   /* count of colors in palette        */
830         colorpair_t     *_color_pairs;  /* screen's color pair list          */
831         int             _pair_count;    /* count of color pairs              */
832         int             _pair_limit;    /* actual limit of color-pairs       */
833 #if NCURSES_EXT_FUNCS
834         bool            _default_color; /* use default colors                */
835         bool            _has_sgr_39_49; /* has ECMA default color support    */
836         int             _default_fg;    /* assumed default foreground        */
837         int             _default_bg;    /* assumed default background        */
838         int             _default_pairs; /* count pairs using default color   */
839 #endif
840         chtype          _ok_attributes; /* valid attributes for terminal     */
841         chtype          _xmc_suppress;  /* attributes to suppress if xmc     */
842         chtype          _xmc_triggers;  /* attributes to process if xmc      */
843         chtype *        _acs_map;       /* the real alternate-charset map    */
844         bool *          _screen_acs_map;
845
846
847         /* used in lib_vidattr.c */
848         bool            _use_rmso;      /* true if we may use 'rmso'         */
849         bool            _use_rmul;      /* true if we may use 'rmul'         */
850
851         /*
852          * These data correspond to the state of the idcok() and idlok()
853          * functions.  A caveat is in order here:  the XSI and SVr4
854          * documentation specify that these functions apply to the window which
855          * is given as an argument.  However, ncurses implements this logic
856          * only for the newscr/curscr update process, _not_ per-window.
857          */
858         bool            _nc_sp_idlok;
859         bool            _nc_sp_idcok;
860 #define _nc_idlok SP->_nc_sp_idlok
861 #define _nc_idcok SP->_nc_sp_idcok
862
863         /*
864          * These are the data that support the mouse interface.
865          */
866         bool            _mouse_initialized;
867         MouseType       _mouse_type;
868         int             _maxclick;
869         bool            (*_mouse_event) (SCREEN *);
870         bool            (*_mouse_inline)(SCREEN *);
871         bool            (*_mouse_parse) (SCREEN *, int);
872         void            (*_mouse_resume)(SCREEN *);
873         void            (*_mouse_wrap)  (SCREEN *);
874         int             _mouse_fd;      /* file-descriptor, if any */
875         bool            _mouse_active;  /* true if initialized */
876         mmask_t         _mouse_mask;
877         NCURSES_CONST char *_mouse_xtermcap; /* string to enable/disable mouse */
878         MEVENT          _mouse_events[EV_MAX];  /* hold the last mouse event seen */
879         MEVENT          *_mouse_eventp; /* next free slot in event queue */
880
881 #if USE_GPM_SUPPORT
882         bool            _mouse_gpm_loaded;
883         bool            _mouse_gpm_found;
884 #ifdef HAVE_LIBDL
885         void            *_dlopen_gpm;
886         TYPE_gpm_fd     _mouse_gpm_fd;
887         TYPE_Gpm_Open   _mouse_Gpm_Open;
888         TYPE_Gpm_Close  _mouse_Gpm_Close;
889         TYPE_Gpm_GetEvent _mouse_Gpm_GetEvent;
890 #endif
891         Gpm_Connect     _mouse_gpm_connect;
892 #endif /* USE_GPM_SUPPORT */
893
894 #if USE_EMX_MOUSE
895         int             _emxmouse_wfd;
896         int             _emxmouse_thread;
897         int             _emxmouse_activated;
898         char            _emxmouse_buttons[4];
899 #endif
900
901 #if USE_SYSMOUSE
902         MEVENT          _sysmouse_fifo[FIFO_SIZE];
903         int             _sysmouse_head;
904         int             _sysmouse_tail;
905         int             _sysmouse_char_width;   /* character width */
906         int             _sysmouse_char_height;  /* character height */
907         int             _sysmouse_old_buttons;
908         int             _sysmouse_new_buttons;
909 #endif
910
911         /*
912          * This supports automatic resizing
913          */
914 #if USE_SIZECHANGE
915         int             (*_resize)(int,int);
916 #endif
917
918         /*
919          * These are data that support the proper handling of the panel stack on an
920          * per screen basis.
921          */
922         struct panelhook _panelHook;
923
924         bool            _sig_winch;
925         SCREEN          *_next_screen;
926
927         /* hashes for old and new lines */
928         unsigned long   *oldhash, *newhash;
929         HASHMAP         *hashtab;
930         int             hashtab_len;
931         int             *_oldnum_list;
932         int             _oldnum_size;
933
934         bool            _cleanup;       /* cleanup after int/quit signal */
935         int             (*_outch)(int); /* output handler if not putc */
936
937         int             _legacy_coding; /* see use_legacy_coding() */
938
939 #if USE_REENTRANT
940         char            _ttytype[NAMESIZE];
941         int             _ESCDELAY;
942         int             _TABSIZE;
943         int             _LINES;
944         int             _COLS;
945 #ifdef TRACE
946         long            _outchars;
947         const char      *_tputs_trace;
948 #endif
949 #endif
950
951 #ifdef TRACE
952         char            tracechr_buf[40];
953         char            tracemse_buf[TRACEMSE_MAX];
954 #endif
955         /*
956          * ncurses/ncursesw are the same up to this point.
957          */
958 #if USE_WIDEC_SUPPORT
959         /* recent versions of 'screen' have partially-working support for
960          * UTF-8, but do not permit ACS at the same time (see tty_update.c).
961          */
962         bool            _screen_acs_fix;
963 #endif
964 };
965
966 extern NCURSES_EXPORT_VAR(SCREEN *) _nc_screen_chain;
967 extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
968
969         WINDOWLIST {
970         WINDOW  win;            /* first, so WINDOW_EXT() works */
971         WINDOWLIST *next;
972         SCREEN *screen;         /* screen containing the window */
973 #ifdef _XOPEN_SOURCE_EXTENDED
974         char addch_work[(MB_LEN_MAX * 9) + 1];
975         unsigned addch_used;    /* number of bytes in addch_work[] */
976         int addch_x;            /* x-position for addch_work[] */
977         int addch_y;            /* y-position for addch_work[] */
978 #endif
979 };
980
981 #define WINDOW_EXT(win,field) (((WINDOWLIST *)(win))->field)
982
983 #define SP_PRE_INIT(sp)                         \
984     sp->_cursrow = -1;                          \
985     sp->_curscol = -1;                          \
986     sp->_nl = TRUE;                             \
987     sp->_raw = FALSE;                           \
988     sp->_cbreak = 0;                            \
989     sp->_echo = TRUE;                           \
990     sp->_fifohead = -1;                         \
991     sp->_endwin = TRUE;                         \
992     sp->_cursor = -1;                           \
993     sp->_windowlist = 0;                        \
994     sp->_outch = NCURSES_SP_NAME(_nc_outch);    \
995     sp->jump = 0                                \
996
997 /* usually in <limits.h> */
998 #ifndef UCHAR_MAX
999 #define UCHAR_MAX 255
1000 #endif
1001
1002 /* The terminfo source is assumed to be 7-bit ASCII */
1003 #define is7bits(c)      ((unsigned)(c) < 128)
1004
1005 /* Checks for isprint() should be done on 8-bit characters (non-wide) */
1006 #define is8bits(c)      ((unsigned)(c) <= UCHAR_MAX)
1007
1008 #ifndef min
1009 #define min(a,b)        ((a) > (b)  ?  (b)  :  (a))
1010 #endif
1011
1012 #ifndef max
1013 #define max(a,b)        ((a) < (b)  ?  (b)  :  (a))
1014 #endif
1015
1016 /* usually in <unistd.h> */
1017 #ifndef STDIN_FILENO
1018 #define STDIN_FILENO 0
1019 #endif
1020
1021 #ifndef STDOUT_FILENO
1022 #define STDOUT_FILENO 1
1023 #endif
1024
1025 #ifndef STDERR_FILENO
1026 #define STDERR_FILENO 2
1027 #endif
1028
1029 #ifndef EXIT_SUCCESS
1030 #define EXIT_SUCCESS 0
1031 #endif
1032
1033 #ifndef EXIT_FAILURE
1034 #define EXIT_FAILURE 1
1035 #endif
1036
1037 #ifndef R_OK
1038 #define R_OK    4               /* Test for read permission.  */
1039 #endif
1040 #ifndef W_OK
1041 #define W_OK    2               /* Test for write permission.  */
1042 #endif
1043 #ifndef X_OK
1044 #define X_OK    1               /* Test for execute permission.  */
1045 #endif
1046 #ifndef F_OK
1047 #define F_OK    0               /* Test for existence.  */
1048 #endif
1049
1050 #if HAVE_FCNTL_H
1051 #include <fcntl.h>              /* may define O_BINARY  */
1052 #endif
1053
1054 #ifndef O_BINARY
1055 #define O_BINARY 0
1056 #endif
1057
1058 #ifdef TRACE
1059 #if USE_REENTRANT
1060 #define COUNT_OUTCHARS(n) _nc_count_outchars(n);
1061 #else
1062 #define COUNT_OUTCHARS(n) _nc_outchars += (n);
1063 #endif
1064 #else
1065 #define COUNT_OUTCHARS(n) /* nothing */
1066 #endif
1067
1068 #define RESET_OUTCHARS() COUNT_OUTCHARS(-_nc_outchars)
1069
1070 #define UChar(c)        ((unsigned char)(c))
1071 #define ChCharOf(c)     ((c) & (chtype)A_CHARTEXT)
1072 #define ChAttrOf(c)     ((c) & (chtype)A_ATTRIBUTES)
1073
1074 #ifndef MB_LEN_MAX
1075 #define MB_LEN_MAX 8 /* should be >= MB_CUR_MAX, but that may be a function */
1076 #endif
1077
1078 #if USE_WIDEC_SUPPORT /* { */
1079 #define isEILSEQ(status) (((size_t)status == (size_t)-1) && (errno == EILSEQ))
1080
1081 #define init_mb(state)  memset(&state, 0, sizeof(state))
1082
1083 #if NCURSES_EXT_COLORS
1084 #define NulColor        , 0
1085 #else
1086 #define NulColor        /* nothing */
1087 #endif
1088
1089 #define NulChar         0,0,0,0 /* FIXME: see CCHARW_MAX */
1090 #define CharOf(c)       ((c).chars[0])
1091 #define AttrOf(c)       ((c).attr)
1092
1093 #define AddAttr(c,a)    AttrOf(c) |=  ((a) & A_ATTRIBUTES)
1094 #define RemAttr(c,a)    AttrOf(c) &= ~((a) & A_ATTRIBUTES)
1095 #define SetAttr(c,a)    AttrOf(c) =   ((a) & A_ATTRIBUTES) | WidecExt(c)
1096
1097 #define NewChar2(c,a)   { a, { c, NulChar } NulColor }
1098 #define NewChar(ch)     NewChar2(ChCharOf(ch), ChAttrOf(ch))
1099
1100 #if CCHARW_MAX == 5
1101 #define CharEq(a,b)     (((a).attr == (b).attr) \
1102                        && (a).chars[0] == (b).chars[0] \
1103                        && (a).chars[1] == (b).chars[1] \
1104                        && (a).chars[2] == (b).chars[2] \
1105                        && (a).chars[3] == (b).chars[3] \
1106                        && (a).chars[4] == (b).chars[4] \
1107                         if_EXT_COLORS(&& (a).ext_color == (b).ext_color))
1108 #else
1109 #define CharEq(a,b)     (!memcmp(&(a), &(b), sizeof(a)))
1110 #endif
1111
1112 #define SetChar(ch,c,a) do {                                                        \
1113                             NCURSES_CH_T *_cp = &ch;                                \
1114                             memset(_cp, 0, sizeof(ch));                             \
1115                             _cp->chars[0] = (c);                                            \
1116                             _cp->attr = (a);                                        \
1117                             if_EXT_COLORS(SetPair(ch, PAIR_NUMBER(a)));             \
1118                         } while (0)
1119 #define CHREF(wch)      (&wch)
1120 #define CHDEREF(wch)    (*wch)
1121 #define ARG_CH_T        NCURSES_CH_T *
1122 #define CARG_CH_T       const NCURSES_CH_T *
1123 #define PUTC_DATA       char PUTC_buf[MB_LEN_MAX]; int PUTC_i, PUTC_n; \
1124                         mbstate_t PUT_st; wchar_t PUTC_ch
1125 #define PUTC_INIT       init_mb (PUT_st)
1126 #define PUTC(ch,b)      do { if(!isWidecExt(ch)) {                                  \
1127                         if (Charable(ch)) {                                         \
1128                             fputc(CharOf(ch), b);                                   \
1129                             COUNT_OUTCHARS(1);                                      \
1130                         } else {                                                    \
1131                             PUTC_INIT;                                              \
1132                             for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {       \
1133                                 PUTC_ch = (ch).chars[PUTC_i];                       \
1134                                 if (PUTC_ch == L'\0')                               \
1135                                     break;                                          \
1136                                 PUTC_n = wcrtomb(PUTC_buf,                          \
1137                                                  (ch).chars[PUTC_i], &PUT_st);      \
1138                                 if (PUTC_n <= 0) {                                  \
1139                                     if (PUTC_ch && is8bits(PUTC_ch) && PUTC_i == 0) \
1140                                         putc(PUTC_ch,b);                            \
1141                                     break;                                          \
1142                                 }                                                   \
1143                                 fwrite(PUTC_buf, (unsigned) PUTC_n, 1, b);          \
1144                             }                                                       \
1145                             COUNT_OUTCHARS(PUTC_i);                                 \
1146                         } } } while (0)
1147
1148 #define BLANK           NewChar2(' ', WA_NORMAL)
1149 #define ZEROS           NewChar2('\0', WA_NORMAL)
1150 #define ISBLANK(ch)     ((ch).chars[0] == L' ' && (ch).chars[1] == L'\0')
1151
1152         /*
1153          * Wide characters cannot be represented in the A_CHARTEXT mask of
1154          * attr_t's but an application might have set a narrow character there.
1155          * But even in that case, it would only be a printable character, or
1156          * zero.  Otherwise we can use those bits to tell if a cell is the
1157          * first or extension part of a wide character.
1158          */
1159 #define WidecExt(ch)    (AttrOf(ch) & A_CHARTEXT)
1160 #define isWidecBase(ch) (WidecExt(ch) == 1)
1161 #define isWidecExt(ch)  (WidecExt(ch) > 1 && WidecExt(ch) < 32)
1162 #define SetWidecExt(dst, ext)   AttrOf(dst) &= ~A_CHARTEXT,             \
1163                                 AttrOf(dst) |= (ext + 1)
1164
1165 #define if_WIDEC(code)  code
1166 #define Charable(ch)    ((SP_PARM != 0 && SP_PARM->_legacy_coding)      \
1167                          || (AttrOf(ch) & A_ALTCHARSET)                 \
1168                          || (!isWidecExt(ch) &&                         \
1169                              (ch).chars[1] == L'\0' &&                  \
1170                              _nc_is_charable(CharOf(ch))))
1171
1172 #define L(ch)           L ## ch
1173 #else /* }{ */
1174 #define CharOf(c)       ChCharOf(c)
1175 #define AttrOf(c)       ChAttrOf(c)
1176 #define AddAttr(c,a)    c |= (a)
1177 #define RemAttr(c,a)    c &= ~((a) & A_ATTRIBUTES)
1178 #define SetAttr(c,a)    c = ((c) & ~A_ATTRIBUTES) | (a)
1179 #define NewChar(ch)     (ch)
1180 #define NewChar2(c,a)   ((c) | (a))
1181 #define CharEq(a,b)     ((a) == (b))
1182 #define SetChar(ch,c,a) ch = (c) | (a)
1183 #define CHREF(wch)      wch
1184 #define CHDEREF(wch)    wch
1185 #define ARG_CH_T        NCURSES_CH_T
1186 #define CARG_CH_T       NCURSES_CH_T
1187 #define PUTC_DATA       int data = 0
1188 #define PUTC(ch,b)      do { data = CharOf(ch); putc(data,b); } while (0)
1189
1190 #define BLANK           (' '|A_NORMAL)
1191 #define ZEROS           ('\0'|A_NORMAL)
1192 #define ISBLANK(ch)     (CharOf(ch) == ' ')
1193
1194 #define isWidecExt(ch)  (0)
1195 #define if_WIDEC(code) /* nothing */
1196
1197 #define L(ch)           ch
1198 #endif /* } */
1199
1200 #define AttrOfD(ch)     AttrOf(CHDEREF(ch))
1201 #define CharOfD(ch)     CharOf(CHDEREF(ch))
1202 #define SetChar2(wch,ch)    SetChar(wch,ChCharOf(ch),ChAttrOf(ch))
1203
1204 #define BLANK_ATTR      A_NORMAL
1205 #define BLANK_TEXT      L(' ')
1206
1207 #define CHANGED     -1
1208
1209 #define LEGALYX(w, y, x) \
1210               ((w) != 0 && \
1211                 ((x) >= 0 && (x) <= (w)->_maxx && \
1212                  (y) >= 0 && (y) <= (w)->_maxy))
1213
1214 #define CHANGED_CELL(line,col) \
1215         if (line->firstchar == _NOCHANGE) \
1216                 line->firstchar = line->lastchar = col; \
1217         else if ((col) < line->firstchar) \
1218                 line->firstchar = col; \
1219         else if ((col) > line->lastchar) \
1220                 line->lastchar = col
1221
1222 #define CHANGED_RANGE(line,start,end) \
1223         if (line->firstchar == _NOCHANGE \
1224          || line->firstchar > (start)) \
1225                 line->firstchar = start; \
1226         if (line->lastchar == _NOCHANGE \
1227          || line->lastchar < (end)) \
1228                 line->lastchar = end
1229
1230 #define CHANGED_TO_EOL(line,start,end) \
1231         if (line->firstchar == _NOCHANGE \
1232          || line->firstchar > (start)) \
1233                 line->firstchar = start; \
1234         line->lastchar = end
1235
1236 #define SIZEOF(v) (sizeof(v)/sizeof(v[0]))
1237
1238 #define FreeIfNeeded(p)  if ((p) != 0) free(p)
1239
1240 /* FreeAndNull() is not a comma-separated expression because some compilers
1241  * do not accept a mixture of void with values.
1242  */
1243 #define FreeAndNull(p)   free(p); p = 0
1244
1245 #include <nc_alloc.h>
1246
1247 /*
1248  * TTY bit definition for converting tabs to spaces.
1249  */
1250 #ifdef TAB3
1251 # define OFLAGS_TABS TAB3       /* POSIX specifies TAB3 */
1252 #else
1253 # ifdef XTABS
1254 #  define OFLAGS_TABS XTABS     /* XTABS is usually the "same" */
1255 # else
1256 #  ifdef OXTABS
1257 #   define OFLAGS_TABS OXTABS   /* the traditional BSD equivalent */
1258 #  else
1259 #   define OFLAGS_TABS 0
1260 #  endif
1261 # endif
1262 #endif
1263
1264 /*
1265  * Standardize/simplify common loops
1266  */
1267 #define each_screen(p) p = _nc_screen_chain; p != 0; p = (p)->_next_screen
1268 #define each_window(p) p = _nc_windows; p != 0; p = (p)->next
1269 #define each_ripoff(p) p = ripoff_stack; (p - ripoff_stack) < N_RIPS; ++p
1270
1271 /*
1272  * Prefixes for call/return points of library function traces.  We use these to
1273  * instrument the public functions so that the traces can be easily transformed
1274  * into regression scripts.
1275  */
1276 #define T_CALLED(fmt) "called {" fmt
1277 #define T_CREATE(fmt) "create :" fmt
1278 #define T_RETURN(fmt) "return }" fmt
1279
1280 #ifdef TRACE
1281
1282 #if USE_REENTRANT
1283 #define TPUTS_TRACE(s)  _nc_set_tputs_trace(s);
1284 #else
1285 #define TPUTS_TRACE(s)  _nc_tputs_trace = s;
1286 #endif
1287
1288 #define START_TRACE() \
1289         if ((_nc_tracing & TRACE_MAXIMUM) == 0) { \
1290             int t = _nc_getenv_num("NCURSES_TRACE"); \
1291             if (t >= 0) \
1292                 trace((unsigned) t); \
1293         }
1294
1295 /*
1296  * Many of the _tracef() calls use static buffers; lock the trace state before
1297  * trying to fill them.
1298  */
1299 #if USE_REENTRANT
1300 #define USE_TRACEF(mask) _nc_use_tracef(mask)
1301 extern NCURSES_EXPORT(int)      _nc_use_tracef (unsigned);
1302 extern NCURSES_EXPORT(void)     _nc_locked_tracef (const char *, ...) GCC_PRINTFLIKE(1,2);
1303 #else
1304 #define USE_TRACEF(mask) (_nc_tracing & (mask))
1305 #define _nc_locked_tracef _tracef
1306 #endif
1307
1308 #define TR(n, a)        if (USE_TRACEF(n)) _nc_locked_tracef a
1309 #define T(a)            TR(TRACE_CALLS, a)
1310 #define TRACE_RETURN(value,type) return _nc_retrace_##type(value)
1311
1312 #define returnAttr(code)        TRACE_RETURN(code,attr_t)
1313 #define returnBits(code)        TRACE_RETURN(code,unsigned)
1314 #define returnBool(code)        TRACE_RETURN(code,bool)
1315 #define returnCPtr(code)        TRACE_RETURN(code,cptr)
1316 #define returnCVoidPtr(code)    TRACE_RETURN(code,cvoid_ptr)
1317 #define returnChtype(code)      TRACE_RETURN(code,chtype)
1318 #define returnCode(code)        TRACE_RETURN(code,int)
1319 #define returnPtr(code)         TRACE_RETURN(code,ptr)
1320 #define returnSP(code)          TRACE_RETURN(code,sp)
1321 #define returnVoid              T((T_RETURN(""))); return
1322 #define returnVoidPtr(code)     TRACE_RETURN(code,void_ptr)
1323 #define returnWin(code)         TRACE_RETURN(code,win)
1324
1325 extern NCURSES_EXPORT(NCURSES_BOOL)     _nc_retrace_bool (NCURSES_BOOL);
1326 extern NCURSES_EXPORT(NCURSES_CONST void *) _nc_retrace_cvoid_ptr (NCURSES_CONST void *);
1327 extern NCURSES_EXPORT(SCREEN *)         _nc_retrace_sp (SCREEN *);
1328 extern NCURSES_EXPORT(WINDOW *)         _nc_retrace_win (WINDOW *);
1329 extern NCURSES_EXPORT(attr_t)           _nc_retrace_attr_t (attr_t);
1330 extern NCURSES_EXPORT(char *)           _nc_retrace_ptr (char *);
1331 extern NCURSES_EXPORT(char *)           _nc_trace_ttymode(TTY *tty);
1332 extern NCURSES_EXPORT(char *)           _nc_varargs (const char *, va_list);
1333 extern NCURSES_EXPORT(chtype)           _nc_retrace_chtype (chtype);
1334 extern NCURSES_EXPORT(const char *)     _nc_altcharset_name(attr_t, chtype);
1335 extern NCURSES_EXPORT(const char *)     _nc_retrace_cptr (const char *);
1336 extern NCURSES_EXPORT(int)              _nc_retrace_int (int);
1337 extern NCURSES_EXPORT(unsigned)         _nc_retrace_unsigned (unsigned);
1338 extern NCURSES_EXPORT(void *)           _nc_retrace_void_ptr (void *);
1339 extern NCURSES_EXPORT(void)             _nc_fifo_dump (SCREEN *);
1340
1341 #if USE_REENTRANT
1342 NCURSES_WRAPPED_VAR(long, _nc_outchars);
1343 NCURSES_WRAPPED_VAR(const char *, _nc_tputs_trace);
1344 #define _nc_outchars       NCURSES_PUBLIC_VAR(_nc_outchars())
1345 #define _nc_tputs_trace    NCURSES_PUBLIC_VAR(_nc_tputs_trace())
1346 extern NCURSES_EXPORT(void)             _nc_set_tputs_trace (const char *);
1347 extern NCURSES_EXPORT(void)             _nc_count_outchars (long);
1348 #else
1349 extern NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace;
1350 extern NCURSES_EXPORT_VAR(long)         _nc_outchars;
1351 #endif
1352
1353 extern NCURSES_EXPORT_VAR(unsigned)     _nc_tracing;
1354
1355 #if USE_WIDEC_SUPPORT
1356 extern NCURSES_EXPORT(const char *) _nc_viswbuf2 (int, const wchar_t *);
1357 extern NCURSES_EXPORT(const char *) _nc_viswbufn (const wchar_t *, int);
1358 #endif
1359
1360 extern NCURSES_EXPORT(const char *) _nc_viscbuf2 (int, const NCURSES_CH_T *, int);
1361 extern NCURSES_EXPORT(const char *) _nc_viscbuf (const NCURSES_CH_T *, int);
1362
1363 #else /* !TRACE */
1364
1365 #define START_TRACE() /* nothing */
1366
1367 #define T(a)
1368 #define TR(n, a)
1369 #define TPUTS_TRACE(s)
1370
1371 #define returnAttr(code)        return code
1372 #define returnBits(code)        return code
1373 #define returnBool(code)        return code
1374 #define returnCPtr(code)        return code
1375 #define returnCVoidPtr(code)    return code
1376 #define returnChtype(code)      return code
1377 #define returnCode(code)        return code
1378 #define returnPtr(code)         return code
1379 #define returnSP(code)          return code
1380 #define returnVoid              return
1381 #define returnVoidPtr(code)     return code
1382 #define returnWin(code)         return code
1383
1384 #endif /* TRACE/!TRACE */
1385
1386 /*
1387  * Return-codes for tgetent() and friends.
1388  */
1389 #define TGETENT_YES  1          /* entry is found */
1390 #define TGETENT_NO   0          /* entry is not found */
1391 #define TGETENT_ERR -1          /* an error occurred */
1392
1393 extern NCURSES_EXPORT(const char *) _nc_visbuf2 (int, const char *);
1394 extern NCURSES_EXPORT(const char *) _nc_visbufn (const char *, int);
1395
1396 #define EMPTY_MODULE(name) \
1397 extern  NCURSES_EXPORT(void) name (void); \
1398         NCURSES_EXPORT(void) name (void) { }
1399
1400 #define ALL_BUT_COLOR ((chtype)~(A_COLOR))
1401 #define NONBLANK_ATTR (A_NORMAL|A_BOLD|A_DIM|A_BLINK)
1402 #define XMC_CHANGES(c) ((c) & SP->_xmc_suppress)
1403
1404 #define toggle_attr_on(S,at) {\
1405    if (PAIR_NUMBER(at) > 0) {\
1406       (S) = ((S) & ALL_BUT_COLOR) | (at);\
1407    } else {\
1408       (S) |= (at);\
1409    }\
1410    TR(TRACE_ATTRS, ("new attribute is %s", _traceattr((S))));}
1411
1412
1413 #define toggle_attr_off(S,at) {\
1414    if (PAIR_NUMBER(at) > 0) {\
1415       (S) &= ~(at|A_COLOR);\
1416    } else {\
1417       (S) &= ~(at);\
1418    }\
1419    TR(TRACE_ATTRS, ("new attribute is %s", _traceattr((S))));}
1420
1421 #define DelCharCost(sp,count) \
1422                 ((parm_dch != 0) \
1423                 ? sp->_dch_cost \
1424                 : ((delete_character != 0) \
1425                         ? (sp->_dch1_cost * count) \
1426                         : INFINITY))
1427
1428 #define InsCharCost(sp,count) \
1429                 ((parm_ich != 0) \
1430                 ? sp->_ich_cost \
1431                 : ((enter_insert_mode && exit_insert_mode) \
1432                   ? sp->_smir_cost + sp->_rmir_cost + (sp->_ip_cost * count) \
1433                   : ((insert_character != 0) \
1434                     ? ((sp->_ich1_cost + sp->_ip_cost) * count) \
1435                     : INFINITY)))
1436
1437 #if USE_XMC_SUPPORT
1438 #define UpdateAttrs(sp,c)       if (!SameAttrOf(SCREEN_ATTRS(sp), c)) { \
1439                                 attr_t chg = AttrOf(SCREEN_ATTRS(sp)); \
1440                                 VIDATTR(AttrOf(c), GetPair(c)); \
1441                                 if (magic_cookie_glitch > 0 \
1442                                  && XMC_CHANGES((chg ^ AttrOf(SCREEN_ATTRS(sp))))) { \
1443                                         T(("%s @%d before glitch %d,%d", \
1444                                                 __FILE__, __LINE__, \
1445                                                 sp->_cursrow, \
1446                                                 sp->_curscol)); \
1447                                         _nc_do_xmc_glitch(chg); \
1448                                 } \
1449                         }
1450 #else
1451 #define UpdateAttrs(sp,c)       if (!SameAttrOf(SCREEN_ATTRS(sp), c)) \
1452                                 VIDATTR(AttrOf(c), GetPair(c));
1453 #endif
1454
1455 /*
1456  * Macros to make additional parameter to implement wgetch_events()
1457  */
1458 #ifdef NCURSES_WGETCH_EVENTS
1459 #define EVENTLIST_0th(param) param
1460 #define EVENTLIST_1st(param) param
1461 #define EVENTLIST_2nd(param) , param
1462 #else
1463 #define EVENTLIST_0th(param) void
1464 #define EVENTLIST_1st(param) /* nothing */
1465 #define EVENTLIST_2nd(param) /* nothing */
1466 #endif
1467
1468 #if NCURSES_EXPANDED && NCURSES_EXT_FUNCS
1469
1470 #undef  toggle_attr_on
1471 #define toggle_attr_on(S,at) _nc_toggle_attr_on(&(S), at)
1472 extern NCURSES_EXPORT(void) _nc_toggle_attr_on (attr_t *, attr_t);
1473
1474 #undef  toggle_attr_off
1475 #define toggle_attr_off(S,at) _nc_toggle_attr_off(&(S), at)
1476 extern NCURSES_EXPORT(void) _nc_toggle_attr_off (attr_t *, attr_t);
1477
1478 #undef  DelCharCost
1479 #define DelCharCost(count) _nc_DelCharCost(count)
1480 extern NCURSES_EXPORT(int) _nc_DelCharCost (int);
1481
1482 #undef  InsCharCost
1483 #define InsCharCost(count) _nc_InsCharCost(count)
1484 extern NCURSES_EXPORT(int) _nc_InsCharCost (int);
1485
1486 #undef  UpdateAttrs
1487 #define UpdateAttrs(c) _nc_UpdateAttrs(c)
1488 extern NCURSES_EXPORT(void) _nc_UpdateAttrs (NCURSES_CH_T);
1489
1490 #else
1491
1492 extern NCURSES_EXPORT(void) _nc_expanded (void);
1493
1494 #endif
1495
1496 #if !NCURSES_EXT_FUNCS
1497 #define set_escdelay(value) ESCDELAY = value
1498 #endif
1499
1500 #if !HAVE_GETCWD
1501 #define getcwd(buf,len) getwd(buf)
1502 #endif
1503
1504 /* charable.c */
1505 #if USE_WIDEC_SUPPORT
1506 extern NCURSES_EXPORT(bool) _nc_is_charable(wchar_t);
1507 extern NCURSES_EXPORT(int) _nc_to_char(wint_t);
1508 extern NCURSES_EXPORT(wint_t) _nc_to_widechar(int);
1509 #endif
1510
1511 /* comp_captab.c */
1512 typedef struct {
1513         short   nte_name;       /* offset of name to hash on */
1514         int     nte_type;       /* BOOLEAN, NUMBER or STRING */
1515         short   nte_index;      /* index of associated variable in its array */
1516         short   nte_link;       /* index in table of next hash, or -1 */
1517 } name_table_data;
1518
1519 typedef struct
1520 {
1521         short   from;
1522         short   to;
1523         short   source;
1524 } alias_table_data;
1525
1526 /* doupdate.c */
1527 #if USE_XMC_SUPPORT
1528 extern NCURSES_EXPORT(void) _nc_do_xmc_glitch (attr_t);
1529 #endif
1530
1531 /* hardscroll.c */
1532 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
1533 extern NCURSES_EXPORT(void) _nc_linedump (void);
1534 #endif
1535
1536 /* lib_acs.c */
1537 extern NCURSES_EXPORT(void) _nc_init_acs (void);        /* corresponds to traditional 'init_acs()' */
1538 extern NCURSES_EXPORT(int) _nc_msec_cost (const char *const, int);  /* used by 'tack' program */
1539
1540 /* lib_addch.c */
1541 #if USE_WIDEC_SUPPORT
1542 NCURSES_EXPORT(int) _nc_build_wch(WINDOW *win, ARG_CH_T ch);
1543 #endif
1544
1545 /* lib_addstr.c */
1546 #if USE_WIDEC_SUPPORT && !defined(USE_TERMLIB)
1547 extern NCURSES_EXPORT(int) _nc_wchstrlen(const cchar_t *);
1548 #endif
1549
1550 /* lib_color.c */
1551 extern NCURSES_EXPORT(bool) _nc_reset_colors(void);
1552
1553 /* lib_getch.c */
1554 extern NCURSES_EXPORT(int) _nc_wgetch(WINDOW *, unsigned long *, int EVENTLIST_2nd(_nc_eventlist *));
1555
1556 /* lib_insch.c */
1557 extern NCURSES_EXPORT(int) _nc_insert_ch(WINDOW *, chtype);
1558
1559 /* lib_mvcur.c */
1560 #define INFINITY        1000000 /* cost: too high to use */
1561
1562 extern NCURSES_EXPORT(void) _nc_mvcur_init (void);
1563 extern NCURSES_EXPORT(void) _nc_mvcur_resume (void);
1564 extern NCURSES_EXPORT(void) _nc_mvcur_wrap (void);
1565
1566 extern NCURSES_EXPORT(int) _nc_scrolln (int, int, int, int);
1567
1568 extern NCURSES_EXPORT(void) _nc_screen_init (void);
1569 extern NCURSES_EXPORT(void) _nc_screen_resume (void);
1570 extern NCURSES_EXPORT(void) _nc_screen_wrap (void);
1571
1572 /* lib_mouse.c */
1573 extern NCURSES_EXPORT(bool) _nc_has_mouse (SCREEN *);
1574
1575 /* lib_mvcur.c */
1576 #define INFINITY        1000000 /* cost: too high to use */
1577 #define BAUDBYTE        9       /* 9 = 7 bits + 1 parity + 1 stop */
1578
1579 /* lib_setup.c */
1580 extern NCURSES_EXPORT(char *) _nc_get_locale(void);
1581 extern NCURSES_EXPORT(int) _nc_unicode_locale(void);
1582 extern NCURSES_EXPORT(int) _nc_locale_breaks_acs(TERMINAL *);
1583 extern NCURSES_EXPORT(int) _nc_setupterm(NCURSES_CONST char *, int, int *, bool);
1584 extern NCURSES_EXPORT(void) _nc_get_screensize(SCREEN *, int *, int *);
1585
1586 /* lib_tstp.c */
1587 #if USE_SIGWINCH
1588 extern NCURSES_EXPORT(int) _nc_handle_sigwinch(SCREEN *);
1589 #else
1590 #define _nc_handle_sigwinch(a) /* nothing */
1591 #endif
1592
1593 /* lib_wacs.c */
1594 #if USE_WIDEC_SUPPORT
1595 extern NCURSES_EXPORT(void) _nc_init_wacs(void);
1596 #endif
1597
1598 typedef struct {
1599     char *s_head;       /* beginning of the string (may be null) */
1600     char *s_tail;       /* end of the string (may be null) */
1601     size_t s_size;      /* current remaining size available */
1602     size_t s_init;      /* total size available */
1603 } string_desc;
1604
1605 /* strings.c */
1606 extern NCURSES_EXPORT(string_desc *) _nc_str_init (string_desc *, char *, size_t);
1607 extern NCURSES_EXPORT(string_desc *) _nc_str_null (string_desc *, size_t);
1608 extern NCURSES_EXPORT(string_desc *) _nc_str_copy (string_desc *, string_desc *);
1609 extern NCURSES_EXPORT(bool) _nc_safe_strcat (string_desc *, const char *);
1610 extern NCURSES_EXPORT(bool) _nc_safe_strcpy (string_desc *, const char *);
1611
1612 #if !HAVE_STRSTR
1613 #define strstr _nc_strstr
1614 extern NCURSES_EXPORT(char *) _nc_strstr (const char *, const char *);
1615 #endif
1616
1617 /* safe_sprintf.c */
1618 extern NCURSES_EXPORT(char *) _nc_printf_string (const char *, va_list);
1619
1620 /* tries.c */
1621 extern NCURSES_EXPORT(int) _nc_add_to_try (TRIES **, const char *, unsigned);
1622 extern NCURSES_EXPORT(char *) _nc_expand_try (TRIES *, unsigned, int *, size_t);
1623 extern NCURSES_EXPORT(int) _nc_remove_key (TRIES **, unsigned);
1624 extern NCURSES_EXPORT(int) _nc_remove_string (TRIES **, const char *);
1625
1626 /* elsewhere ... */
1627 extern NCURSES_EXPORT(ENTRY *) _nc_delink_entry (ENTRY *, TERMTYPE *);
1628 extern NCURSES_EXPORT(NCURSES_CONST char *) _nc_keyname (SCREEN *, int);
1629 extern NCURSES_EXPORT(SCREEN *) _nc_screen_of (WINDOW *);
1630 extern NCURSES_EXPORT(WINDOW *) _nc_makenew (int, int, int, int, int);
1631 extern NCURSES_EXPORT(char *) _nc_trace_buf (int, size_t);
1632 extern NCURSES_EXPORT(char *) _nc_trace_bufcat (int, const char *);
1633 extern NCURSES_EXPORT(char *) _nc_tracechar (SCREEN *, int);
1634 extern NCURSES_EXPORT(char *) _nc_tracemouse (SCREEN *, MEVENT const *);
1635 extern NCURSES_EXPORT(int) _nc_access (const char *, int);
1636 extern NCURSES_EXPORT(int) _nc_baudrate (int);
1637 extern NCURSES_EXPORT(int) _nc_freewin (WINDOW *);
1638 extern NCURSES_EXPORT(int) _nc_getenv_num (const char *);
1639 extern NCURSES_EXPORT(int) _nc_keypad (SCREEN *, bool);
1640 extern NCURSES_EXPORT(int) _nc_ospeed (int);
1641 extern NCURSES_EXPORT(int) _nc_outch (int);
1642 extern NCURSES_EXPORT(int) _nc_putp(const char *, const char *);
1643 extern NCURSES_EXPORT(int) _nc_putp_flush(const char *, const char *);
1644 extern NCURSES_EXPORT(int) _nc_read_termcap_entry (const char *const, TERMTYPE *const);
1645 extern NCURSES_EXPORT(int) _nc_setupscreen (int, int, FILE *, bool, int);
1646 extern NCURSES_EXPORT(int) _nc_timed_wait (SCREEN *, int, int, int * EVENTLIST_2nd(_nc_eventlist *));
1647 extern NCURSES_EXPORT(void) _nc_do_color (short, short, bool, int (*)(int));
1648 extern NCURSES_EXPORT(void) _nc_flush (void);
1649 extern NCURSES_EXPORT(void) _nc_free_and_exit (int);
1650 extern NCURSES_EXPORT(void) _nc_free_entry (ENTRY *, TERMTYPE *);
1651 extern NCURSES_EXPORT(void) _nc_freeall (void);
1652 extern NCURSES_EXPORT(void) _nc_hash_map (void);
1653 extern NCURSES_EXPORT(void) _nc_init_keytry (SCREEN *);
1654 extern NCURSES_EXPORT(void) _nc_keep_tic_dir (const char *);
1655 extern NCURSES_EXPORT(void) _nc_make_oldhash (int i);
1656 extern NCURSES_EXPORT(void) _nc_scroll_oldhash (int n, int top, int bot);
1657 extern NCURSES_EXPORT(void) _nc_scroll_optimize (void);
1658 extern NCURSES_EXPORT(void) _nc_set_buffer (FILE *, bool);
1659 extern NCURSES_EXPORT(void) _nc_signal_handler (bool);
1660 extern NCURSES_EXPORT(void) _nc_synchook (WINDOW *);
1661 extern NCURSES_EXPORT(void) _nc_trace_tries (TRIES *);
1662
1663 #if NO_LEAKS
1664 extern NCURSES_EXPORT(void) _nc_alloc_entry_leaks(void);
1665 extern NCURSES_EXPORT(void) _nc_captoinfo_leaks(void);
1666 extern NCURSES_EXPORT(void) _nc_codes_leaks(void);
1667 extern NCURSES_EXPORT(void) _nc_comp_captab_leaks(void);
1668 extern NCURSES_EXPORT(void) _nc_comp_scan_leaks(void);
1669 extern NCURSES_EXPORT(void) _nc_keyname_leaks(void);
1670 extern NCURSES_EXPORT(void) _nc_names_leaks(void);
1671 extern NCURSES_EXPORT(void) _nc_tgetent_leaks(void);
1672 #endif
1673
1674 #ifndef USE_TERMLIB
1675 extern NCURSES_EXPORT(NCURSES_CH_T) _nc_render (WINDOW *, NCURSES_CH_T);
1676 extern NCURSES_EXPORT(int) _nc_waddch_nosync (WINDOW *, const NCURSES_CH_T);
1677 extern NCURSES_EXPORT(void) _nc_scroll_window (WINDOW *, int const, NCURSES_SIZE_T const, NCURSES_SIZE_T const, NCURSES_CH_T);
1678 #endif
1679
1680 #if USE_WIDEC_SUPPORT && !defined(USE_TERMLIB)
1681 extern NCURSES_EXPORT(size_t) _nc_wcrtomb (char *, wchar_t, mbstate_t *);
1682 #endif
1683
1684 #if USE_SIZECHANGE
1685 extern NCURSES_EXPORT(void) _nc_update_screensize (SCREEN *);
1686 #endif
1687
1688 #if HAVE_RESIZETERM
1689 extern NCURSES_EXPORT(void) _nc_resize_margins (WINDOW *);
1690 #else
1691 #define _nc_resize_margins(wp) /* nothing */
1692 #endif
1693
1694 #ifdef NCURSES_WGETCH_EVENTS
1695 extern NCURSES_EXPORT(int) _nc_eventlist_timeout(_nc_eventlist *);
1696 #else
1697 #define wgetch_events(win, evl) wgetch(win)
1698 #define wgetnstr_events(win, str, maxlen, evl) wgetnstr(win, str, maxlen)
1699 #endif
1700
1701 /*
1702  * Not everyone has vsscanf(), but we'd like to use it for scanw().
1703  */
1704 #if !HAVE_VSSCANF
1705 extern int vsscanf(const char *str, const char *format, va_list __arg);
1706 #endif
1707
1708 /* scroll indices */
1709 extern NCURSES_EXPORT_VAR(int *) _nc_oldnums;
1710
1711 #define USE_SETBUF_0 0
1712
1713 #define NC_BUFFERED(flag) _nc_set_buffer(SP->_ofp, flag)
1714
1715 #define NC_OUTPUT ((SP_PARM != 0) ? SP_PARM->_ofp : stdout)
1716
1717 /*
1718  * On systems with a broken linker, define 'SP' as a function to force the
1719  * linker to pull in the data-only module with 'SP'.
1720  */
1721 #if BROKEN_LINKER
1722 #define SP _nc_screen()
1723 extern NCURSES_EXPORT(SCREEN *) _nc_screen (void);
1724 extern NCURSES_EXPORT(int)      _nc_alloc_screen (void);
1725 extern NCURSES_EXPORT(void)     _nc_set_screen (SCREEN *);
1726 #else
1727 /* current screen is private data; avoid possible linking conflicts too */
1728 extern NCURSES_EXPORT_VAR(SCREEN *) SP;
1729 #define _nc_alloc_screen()      ((SP = typeCalloc(SCREEN, 1)) != 0)
1730 #define _nc_set_screen(sp)      SP = sp
1731 #endif
1732
1733 #define CURRENT_SCREEN SP
1734
1735 #define CURRENT_SCREEN_PRE      (IsPreScreen(CURRENT_SCREEN) ? CURRENT_SCREEN : new_prescr())
1736
1737 /*
1738  * We don't want to use the lines or columns capabilities internally, because
1739  * if the application is running multiple screens under X, it's quite possible
1740  * they could all have type xterm but have different sizes!  So...
1741  */
1742 #define screen_lines(sp)        (sp)->_lines
1743 #define screen_columns(sp)      (sp)->_columns
1744
1745 extern NCURSES_EXPORT(int) _nc_slk_initialize (WINDOW *, int);
1746
1747 /*
1748  * Some constants related to SLK's
1749  */
1750 #define MAX_SKEY_OLD       8    /* count of soft keys */
1751 #define MAX_SKEY_LEN_OLD   8    /* max length of soft key text */
1752 #define MAX_SKEY_PC       12    /* This is what most PC's have */
1753 #define MAX_SKEY_LEN_PC    5
1754
1755 /* Macro to check whether or not we use a standard format */
1756 #define SLK_STDFMT(fmt) (fmt < 3)
1757 /* Macro to determine height of label window */
1758 #define SLK_LINES(fmt)  (SLK_STDFMT(fmt) ? 1 : ((fmt) - 2))
1759
1760 #define MAX_SKEY(fmt)     (SLK_STDFMT(fmt)? MAX_SKEY_OLD : MAX_SKEY_PC)
1761 #define MAX_SKEY_LEN(fmt) (SLK_STDFMT(fmt)? MAX_SKEY_LEN_OLD : MAX_SKEY_LEN_PC)
1762
1763 extern NCURSES_EXPORT(int) _nc_ripoffline (int line, int (*init)(WINDOW *,int));
1764
1765 /*
1766  * Common error messages
1767  */
1768 #define MSG_NO_MEMORY "Out of memory"
1769 #define MSG_NO_INPUTS "Premature EOF"
1770
1771 /* timed_wait flag definitions */
1772 #define TW_NONE    0
1773 #define TW_INPUT   1
1774 #define TW_MOUSE   2
1775 #define TW_ANY     (TW_INPUT | TW_MOUSE)
1776 #define TW_EVENT   4
1777
1778 #define IsTermInfo(sp) TRUE
1779 #define HasTInfoTerminal(sp) ((0!=TerminalOf(sp)) && IsTermInfo(sp))
1780 #define IsValidTIScreen(sp)  (HasTInfoTerminal(sp))
1781
1782 /*
1783  * Exported entrypoints beyond the published API
1784  */
1785 #if NCURSES_SP_FUNCS
1786 extern NCURSES_EXPORT(WINDOW *) _nc_curscr_of(SCREEN*);
1787 extern NCURSES_EXPORT(WINDOW *) _nc_newscr_of(SCREEN*);
1788 extern NCURSES_EXPORT(WINDOW *) _nc_stdscr_of(SCREEN*);
1789 extern NCURSES_EXPORT(int)      _nc_outc_wrapper(SCREEN*,int);
1790
1791 extern NCURSES_EXPORT(TERMINAL*) NCURSES_SP_NAME(_nc_set_curterm)(SCREEN*,TERMINAL*);
1792
1793 #if NCURSES_EXT_FUNCS
1794 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_set_tabsize)(SCREEN*, int);
1795 #endif
1796
1797 extern NCURSES_EXPORT(chtype)   NCURSES_SP_NAME(_nc_acs_char)(SCREEN*,int);
1798
1799 /*
1800  * We put the safe versions of various calls here as they are not published
1801  * part of the API up to now
1802  */
1803 extern NCURSES_EXPORT(SCREEN*)  _nc_SP(void);
1804
1805 extern NCURSES_EXPORT(WINDOW *) NCURSES_SP_NAME(_nc_makenew) (SCREEN*, int, int, int, int, int);
1806 extern NCURSES_EXPORT(bool)     NCURSES_SP_NAME(_nc_is_term_resized)(SCREEN*,int,int);
1807 extern NCURSES_EXPORT(char *)   NCURSES_SP_NAME(_nc_printf_string)(SCREEN*, const char *, va_list);
1808 extern NCURSES_EXPORT(chtype)   NCURSES_SP_NAME(_nc_acs_char)(SCREEN*,int);
1809 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_curs_set)(SCREEN*,int);
1810 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_get_tty_mode)(SCREEN*,TTY*);
1811 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_mcprint)(SCREEN*,char*, int);
1812 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_msec_cost)(SCREEN*, const char *, int);
1813 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_outch)(SCREEN*, int);
1814 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_putp)(SCREEN*, const char *, const char *);
1815 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_putp_flush)(SCREEN*, const char *, const char *);
1816 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_resetty)(SCREEN*);
1817 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_resize_term)(SCREEN*,int,int);
1818 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_resizeterm)(SCREEN*,int,int);
1819 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_restartterm)(SCREEN*,NCURSES_CONST char*,int,int*);
1820 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_ripoffline)(SCREEN*, int, int (*)(WINDOW *,int));
1821 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_savetty)(SCREEN*);
1822 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scr_init)(SCREEN*,const char*);
1823 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scr_restore)(SCREEN*, const char*);
1824 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scrolln)(SCREEN*, int, int, int, int);
1825 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_set_tty_mode)(SCREEN*,TTY*);
1826 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_setupscreen)(SCREEN**, int, int, FILE *, bool, int);
1827 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tgetent)(SCREEN*,char*,const char *);
1828 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tigetnum)(SCREEN*,NCURSES_CONST char*);
1829 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tputs)(SCREEN*,const char*,int,int(*)(SCREEN*, int));
1830 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_vid_attr)(SCREEN *, attr_t, short, void *);
1831 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_vidattr)(SCREEN *, chtype);
1832 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_vidputs)(SCREEN*,chtype,int(*) (SCREEN*, int));
1833 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_do_color)(SCREEN*, short, short, bool, int (*)(SCREEN*,int));
1834 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_do_xmc_glitch)(SCREEN*, attr_t);
1835 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_flush)(SCREEN*);
1836 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_free_and_exit)(SCREEN*,int) GCC_NORETURN;
1837 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_freeall)(SCREEN*);
1838 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_hash_map)(SCREEN*);
1839 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_init_acs)(SCREEN*);
1840 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_make_oldhash)(SCREEN*, int i);
1841 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_mvcur_init)(SCREEN*);
1842 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_mvcur_resume)(SCREEN*);
1843 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_mvcur_wrap)(SCREEN*);
1844 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_screen_init)(SCREEN*);
1845 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_screen_resume)(SCREEN*);
1846 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_screen_wrap)(SCREEN*);
1847 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_scroll_oldhash)(SCREEN*, int n, int top, int bot);
1848 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_scroll_optimize)(SCREEN*);
1849 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_set_buffer)(SCREEN*, FILE *, bool);
1850
1851 extern NCURSES_EXPORT(void)     _nc_cookie_init(SCREEN *sp);
1852 extern NCURSES_EXPORT(int)      _nc_tinfo_doupdate(SCREEN *sp);
1853 extern NCURSES_EXPORT(int)      _nc_tinfo_mcprint(SCREEN*,char*,int);
1854
1855 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
1856 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_linedump)(SCREEN*);
1857 #endif
1858
1859 #if USE_WIDEC_SUPPORT
1860 extern NCURSES_EXPORT(wchar_t *) NCURSES_SP_NAME(_nc_wunctrl)(SCREEN*, cchar_t *);
1861 #endif
1862
1863 #endif /* NCURSES_SP_FUNCS */
1864
1865 #if NCURSES_SP_FUNCS
1866
1867 #define safe_keyname NCURSES_SP_NAME(keyname)
1868 #define safe_unctrl  NCURSES_SP_NAME(unctrl)
1869 #define safe_ungetch NCURSES_SP_NAME(ungetch)
1870
1871 #else
1872
1873 #define safe_keyname _nc_keyname
1874 #define safe_unctrl  _nc_unctrl
1875 #define safe_ungetch _nc_ungetch
1876
1877 extern NCURSES_EXPORT(int) _nc_ungetch (SCREEN *, int);
1878 extern NCURSES_EXPORT(NCURSES_CONST char *) _nc_unctrl (SCREEN *, chtype);
1879
1880 #endif
1881
1882 #ifdef __cplusplus
1883 }
1884 #endif
1885
1886 #endif /* CURSES_PRIV_H */