]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/curses.priv.h
ncurses 5.7 - patch 20090411
[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.403 2009/02/28 20:55:48 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->_LINES) : &(_nc_prescreen._LINES))
339 #define ptrCols()  (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() &LINES
344 #define ptrCols()  &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->_mouse_gpm_fd
530 #define my_Gpm_Open     SP->_mouse_Gpm_Open
531 #define my_Gpm_Close    SP->_mouse_Gpm_Close
532 #define my_Gpm_GetEvent SP->_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 /* usually in <limits.h> */
984 #ifndef UCHAR_MAX
985 #define UCHAR_MAX 255
986 #endif
987
988 /* The terminfo source is assumed to be 7-bit ASCII */
989 #define is7bits(c)      ((unsigned)(c) < 128)
990
991 /* Checks for isprint() should be done on 8-bit characters (non-wide) */
992 #define is8bits(c)      ((unsigned)(c) <= UCHAR_MAX)
993
994 #ifndef min
995 #define min(a,b)        ((a) > (b)  ?  (b)  :  (a))
996 #endif
997
998 #ifndef max
999 #define max(a,b)        ((a) < (b)  ?  (b)  :  (a))
1000 #endif
1001
1002 /* usually in <unistd.h> */
1003 #ifndef STDIN_FILENO
1004 #define STDIN_FILENO 0
1005 #endif
1006
1007 #ifndef STDOUT_FILENO
1008 #define STDOUT_FILENO 1
1009 #endif
1010
1011 #ifndef STDERR_FILENO
1012 #define STDERR_FILENO 2
1013 #endif
1014
1015 #ifndef EXIT_SUCCESS
1016 #define EXIT_SUCCESS 0
1017 #endif
1018
1019 #ifndef EXIT_FAILURE
1020 #define EXIT_FAILURE 1
1021 #endif
1022
1023 #ifndef R_OK
1024 #define R_OK    4               /* Test for read permission.  */
1025 #endif
1026 #ifndef W_OK
1027 #define W_OK    2               /* Test for write permission.  */
1028 #endif
1029 #ifndef X_OK
1030 #define X_OK    1               /* Test for execute permission.  */
1031 #endif
1032 #ifndef F_OK
1033 #define F_OK    0               /* Test for existence.  */
1034 #endif
1035
1036 #if HAVE_FCNTL_H
1037 #include <fcntl.h>              /* may define O_BINARY  */
1038 #endif
1039
1040 #ifndef O_BINARY
1041 #define O_BINARY 0
1042 #endif
1043
1044 #ifdef TRACE
1045 #if USE_REENTRANT
1046 #define COUNT_OUTCHARS(n) _nc_count_outchars(n);
1047 #else
1048 #define COUNT_OUTCHARS(n) _nc_outchars += (n);
1049 #endif
1050 #else
1051 #define COUNT_OUTCHARS(n) /* nothing */
1052 #endif
1053
1054 #define RESET_OUTCHARS() COUNT_OUTCHARS(-_nc_outchars)
1055
1056 #define UChar(c)        ((unsigned char)(c))
1057 #define ChCharOf(c)     ((c) & (chtype)A_CHARTEXT)
1058 #define ChAttrOf(c)     ((c) & (chtype)A_ATTRIBUTES)
1059
1060 #ifndef MB_LEN_MAX
1061 #define MB_LEN_MAX 8 /* should be >= MB_CUR_MAX, but that may be a function */
1062 #endif
1063
1064 #if USE_WIDEC_SUPPORT /* { */
1065 #define isEILSEQ(status) (((size_t)status == (size_t)-1) && (errno == EILSEQ))
1066
1067 #define init_mb(state)  memset(&state, 0, sizeof(state))
1068
1069 #if NCURSES_EXT_COLORS
1070 #define NulColor        , 0
1071 #else
1072 #define NulColor        /* nothing */
1073 #endif
1074
1075 #define NulChar         0,0,0,0 /* FIXME: see CCHARW_MAX */
1076 #define CharOf(c)       ((c).chars[0])
1077 #define AttrOf(c)       ((c).attr)
1078
1079 #define AddAttr(c,a)    AttrOf(c) |=  ((a) & A_ATTRIBUTES)
1080 #define RemAttr(c,a)    AttrOf(c) &= ~((a) & A_ATTRIBUTES)
1081 #define SetAttr(c,a)    AttrOf(c) =   ((a) & A_ATTRIBUTES) | WidecExt(c)
1082
1083 #define NewChar2(c,a)   { a, { c, NulChar } NulColor }
1084 #define NewChar(ch)     NewChar2(ChCharOf(ch), ChAttrOf(ch))
1085
1086 #if CCHARW_MAX == 5
1087 #define CharEq(a,b)     (((a).attr == (b).attr) \
1088                        && (a).chars[0] == (b).chars[0] \
1089                        && (a).chars[1] == (b).chars[1] \
1090                        && (a).chars[2] == (b).chars[2] \
1091                        && (a).chars[3] == (b).chars[3] \
1092                        && (a).chars[4] == (b).chars[4] \
1093                         if_EXT_COLORS(&& (a).ext_color == (b).ext_color))
1094 #else
1095 #define CharEq(a,b)     (!memcmp(&(a), &(b), sizeof(a)))
1096 #endif
1097
1098 #define SetChar(ch,c,a) do {                                                        \
1099                             NCURSES_CH_T *_cp = &ch;                                \
1100                             memset(_cp, 0, sizeof(ch));                             \
1101                             _cp->chars[0] = (c);                                            \
1102                             _cp->attr = (a);                                        \
1103                             if_EXT_COLORS(SetPair(ch, PAIR_NUMBER(a)));             \
1104                         } while (0)
1105 #define CHREF(wch)      (&wch)
1106 #define CHDEREF(wch)    (*wch)
1107 #define ARG_CH_T        NCURSES_CH_T *
1108 #define CARG_CH_T       const NCURSES_CH_T *
1109 #define PUTC_DATA       char PUTC_buf[MB_LEN_MAX]; int PUTC_i, PUTC_n; \
1110                         mbstate_t PUT_st; wchar_t PUTC_ch
1111 #define PUTC_INIT       init_mb (PUT_st)
1112 #define PUTC(ch,b)      do { if(!isWidecExt(ch)) {                                  \
1113                         if (Charable(ch)) {                                         \
1114                             fputc(CharOf(ch), b);                                   \
1115                             COUNT_OUTCHARS(1);                                      \
1116                         } else {                                                    \
1117                             PUTC_INIT;                                              \
1118                             for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {       \
1119                                 PUTC_ch = (ch).chars[PUTC_i];                       \
1120                                 if (PUTC_ch == L'\0')                               \
1121                                     break;                                          \
1122                                 PUTC_n = wcrtomb(PUTC_buf,                          \
1123                                                  (ch).chars[PUTC_i], &PUT_st);      \
1124                                 if (PUTC_n <= 0) {                                  \
1125                                     if (PUTC_ch && is8bits(PUTC_ch) && PUTC_i == 0) \
1126                                         putc(PUTC_ch,b);                            \
1127                                     break;                                          \
1128                                 }                                                   \
1129                                 fwrite(PUTC_buf, (unsigned) PUTC_n, 1, b);          \
1130                             }                                                       \
1131                             COUNT_OUTCHARS(PUTC_i);                                 \
1132                         } } } while (0)
1133
1134 #define BLANK           NewChar2(' ', WA_NORMAL)
1135 #define ZEROS           NewChar2('\0', WA_NORMAL)
1136 #define ISBLANK(ch)     ((ch).chars[0] == L' ' && (ch).chars[1] == L'\0')
1137
1138         /*
1139          * Wide characters cannot be represented in the A_CHARTEXT mask of
1140          * attr_t's but an application might have set a narrow character there.
1141          * But even in that case, it would only be a printable character, or
1142          * zero.  Otherwise we can use those bits to tell if a cell is the
1143          * first or extension part of a wide character.
1144          */
1145 #define WidecExt(ch)    (AttrOf(ch) & A_CHARTEXT)
1146 #define isWidecBase(ch) (WidecExt(ch) == 1)
1147 #define isWidecExt(ch)  (WidecExt(ch) > 1 && WidecExt(ch) < 32)
1148 #define SetWidecExt(dst, ext)   AttrOf(dst) &= ~A_CHARTEXT,             \
1149                                 AttrOf(dst) |= (ext + 1)
1150
1151 #define if_WIDEC(code)  code
1152 #define Charable(ch)    ((SP != 0 && SP->_legacy_coding)                \
1153                          || (AttrOf(ch) & A_ALTCHARSET)                 \
1154                          || (!isWidecExt(ch) &&                         \
1155                              (ch).chars[1] == L'\0' &&                  \
1156                              _nc_is_charable(CharOf(ch))))
1157
1158 #define L(ch)           L ## ch
1159 #else /* }{ */
1160 #define CharOf(c)       ChCharOf(c)
1161 #define AttrOf(c)       ChAttrOf(c)
1162 #define AddAttr(c,a)    c |= (a)
1163 #define RemAttr(c,a)    c &= ~((a) & A_ATTRIBUTES)
1164 #define SetAttr(c,a)    c = ((c) & ~A_ATTRIBUTES) | (a)
1165 #define NewChar(ch)     (ch)
1166 #define NewChar2(c,a)   ((c) | (a))
1167 #define CharEq(a,b)     ((a) == (b))
1168 #define SetChar(ch,c,a) ch = (c) | (a)
1169 #define CHREF(wch)      wch
1170 #define CHDEREF(wch)    wch
1171 #define ARG_CH_T        NCURSES_CH_T
1172 #define CARG_CH_T       NCURSES_CH_T
1173 #define PUTC_DATA       int data = 0
1174 #define PUTC(ch,b)      do { data = CharOf(ch); putc(data,b); } while (0)
1175
1176 #define BLANK           (' '|A_NORMAL)
1177 #define ZEROS           ('\0'|A_NORMAL)
1178 #define ISBLANK(ch)     (CharOf(ch) == ' ')
1179
1180 #define isWidecExt(ch)  (0)
1181 #define if_WIDEC(code) /* nothing */
1182
1183 #define L(ch)           ch
1184 #endif /* } */
1185
1186 #define AttrOfD(ch)     AttrOf(CHDEREF(ch))
1187 #define CharOfD(ch)     CharOf(CHDEREF(ch))
1188 #define SetChar2(wch,ch)    SetChar(wch,ChCharOf(ch),ChAttrOf(ch))
1189
1190 #define BLANK_ATTR      A_NORMAL
1191 #define BLANK_TEXT      L(' ')
1192
1193 #define CHANGED     -1
1194
1195 #define LEGALYX(w, y, x) \
1196               ((w) != 0 && \
1197                 ((x) >= 0 && (x) <= (w)->_maxx && \
1198                  (y) >= 0 && (y) <= (w)->_maxy))
1199
1200 #define CHANGED_CELL(line,col) \
1201         if (line->firstchar == _NOCHANGE) \
1202                 line->firstchar = line->lastchar = col; \
1203         else if ((col) < line->firstchar) \
1204                 line->firstchar = col; \
1205         else if ((col) > line->lastchar) \
1206                 line->lastchar = col
1207
1208 #define CHANGED_RANGE(line,start,end) \
1209         if (line->firstchar == _NOCHANGE \
1210          || line->firstchar > (start)) \
1211                 line->firstchar = start; \
1212         if (line->lastchar == _NOCHANGE \
1213          || line->lastchar < (end)) \
1214                 line->lastchar = end
1215
1216 #define CHANGED_TO_EOL(line,start,end) \
1217         if (line->firstchar == _NOCHANGE \
1218          || line->firstchar > (start)) \
1219                 line->firstchar = start; \
1220         line->lastchar = end
1221
1222 #define SIZEOF(v) (sizeof(v)/sizeof(v[0]))
1223
1224 #define FreeIfNeeded(p)  if ((p) != 0) free(p)
1225
1226 /* FreeAndNull() is not a comma-separated expression because some compilers
1227  * do not accept a mixture of void with values.
1228  */
1229 #define FreeAndNull(p)   free(p); p = 0
1230
1231 #include <nc_alloc.h>
1232
1233 /*
1234  * TTY bit definition for converting tabs to spaces.
1235  */
1236 #ifdef TAB3
1237 # define OFLAGS_TABS TAB3       /* POSIX specifies TAB3 */
1238 #else
1239 # ifdef XTABS
1240 #  define OFLAGS_TABS XTABS     /* XTABS is usually the "same" */
1241 # else
1242 #  ifdef OXTABS
1243 #   define OFLAGS_TABS OXTABS   /* the traditional BSD equivalent */
1244 #  else
1245 #   define OFLAGS_TABS 0
1246 #  endif
1247 # endif
1248 #endif
1249
1250 /*
1251  * Standardize/simplify common loops
1252  */
1253 #define each_screen(p) p = _nc_screen_chain; p != 0; p = (p)->_next_screen
1254 #define each_window(p) p = _nc_windows; p != 0; p = (p)->next
1255 #define each_ripoff(p) p = ripoff_stack; (p - ripoff_stack) < N_RIPS; ++p
1256
1257 /*
1258  * Prefixes for call/return points of library function traces.  We use these to
1259  * instrument the public functions so that the traces can be easily transformed
1260  * into regression scripts.
1261  */
1262 #define T_CALLED(fmt) "called {" fmt
1263 #define T_CREATE(fmt) "create :" fmt
1264 #define T_RETURN(fmt) "return }" fmt
1265
1266 #ifdef TRACE
1267
1268 #if USE_REENTRANT
1269 #define TPUTS_TRACE(s)  _nc_set_tputs_trace(s);
1270 #else
1271 #define TPUTS_TRACE(s)  _nc_tputs_trace = s;
1272 #endif
1273
1274 #define START_TRACE() \
1275         if ((_nc_tracing & TRACE_MAXIMUM) == 0) { \
1276             int t = _nc_getenv_num("NCURSES_TRACE"); \
1277             if (t >= 0) \
1278                 trace((unsigned) t); \
1279         }
1280
1281 /*
1282  * Many of the _tracef() calls use static buffers; lock the trace state before
1283  * trying to fill them.
1284  */
1285 #if USE_REENTRANT
1286 #define USE_TRACEF(mask) _nc_use_tracef(mask)
1287 extern NCURSES_EXPORT(int)      _nc_use_tracef (unsigned);
1288 extern NCURSES_EXPORT(void)     _nc_locked_tracef (const char *, ...) GCC_PRINTFLIKE(1,2);
1289 #else
1290 #define USE_TRACEF(mask) (_nc_tracing & (mask))
1291 #define _nc_locked_tracef _tracef
1292 #endif
1293
1294 #define TR(n, a)        if (USE_TRACEF(n)) _nc_locked_tracef a
1295 #define T(a)            TR(TRACE_CALLS, a)
1296 #define TRACE_RETURN(value,type) return _nc_retrace_##type(value)
1297
1298 #define returnAttr(code)        TRACE_RETURN(code,attr_t)
1299 #define returnBits(code)        TRACE_RETURN(code,unsigned)
1300 #define returnBool(code)        TRACE_RETURN(code,bool)
1301 #define returnCPtr(code)        TRACE_RETURN(code,cptr)
1302 #define returnCVoidPtr(code)    TRACE_RETURN(code,cvoid_ptr)
1303 #define returnChtype(code)      TRACE_RETURN(code,chtype)
1304 #define returnCode(code)        TRACE_RETURN(code,int)
1305 #define returnPtr(code)         TRACE_RETURN(code,ptr)
1306 #define returnSP(code)          TRACE_RETURN(code,sp)
1307 #define returnVoid              T((T_RETURN(""))); return
1308 #define returnVoidPtr(code)     TRACE_RETURN(code,void_ptr)
1309 #define returnWin(code)         TRACE_RETURN(code,win)
1310
1311 extern NCURSES_EXPORT(NCURSES_BOOL)     _nc_retrace_bool (NCURSES_BOOL);
1312 extern NCURSES_EXPORT(NCURSES_CONST void *) _nc_retrace_cvoid_ptr (NCURSES_CONST void *);
1313 extern NCURSES_EXPORT(SCREEN *)         _nc_retrace_sp (SCREEN *);
1314 extern NCURSES_EXPORT(WINDOW *)         _nc_retrace_win (WINDOW *);
1315 extern NCURSES_EXPORT(attr_t)           _nc_retrace_attr_t (attr_t);
1316 extern NCURSES_EXPORT(char *)           _nc_retrace_ptr (char *);
1317 extern NCURSES_EXPORT(char *)           _nc_trace_ttymode(TTY *tty);
1318 extern NCURSES_EXPORT(char *)           _nc_varargs (const char *, va_list);
1319 extern NCURSES_EXPORT(chtype)           _nc_retrace_chtype (chtype);
1320 extern NCURSES_EXPORT(const char *)     _nc_altcharset_name(attr_t, chtype);
1321 extern NCURSES_EXPORT(const char *)     _nc_retrace_cptr (const char *);
1322 extern NCURSES_EXPORT(int)              _nc_retrace_int (int);
1323 extern NCURSES_EXPORT(unsigned)         _nc_retrace_unsigned (unsigned);
1324 extern NCURSES_EXPORT(void *)           _nc_retrace_void_ptr (void *);
1325 extern NCURSES_EXPORT(void)             _nc_fifo_dump (SCREEN *);
1326
1327 #if USE_REENTRANT
1328 NCURSES_WRAPPED_VAR(long, _nc_outchars);
1329 NCURSES_WRAPPED_VAR(const char *, _nc_tputs_trace);
1330 #define _nc_outchars       NCURSES_PUBLIC_VAR(_nc_outchars())
1331 #define _nc_tputs_trace    NCURSES_PUBLIC_VAR(_nc_tputs_trace())
1332 extern NCURSES_EXPORT(void)             _nc_set_tputs_trace (const char *);
1333 extern NCURSES_EXPORT(void)             _nc_count_outchars (long);
1334 #else
1335 extern NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace;
1336 extern NCURSES_EXPORT_VAR(long)         _nc_outchars;
1337 #endif
1338
1339 extern NCURSES_EXPORT_VAR(unsigned)     _nc_tracing;
1340
1341 #if USE_WIDEC_SUPPORT
1342 extern NCURSES_EXPORT(const char *) _nc_viswbuf2 (int, const wchar_t *);
1343 extern NCURSES_EXPORT(const char *) _nc_viswbufn (const wchar_t *, int);
1344 #endif
1345
1346 extern NCURSES_EXPORT(const char *) _nc_viscbuf2 (int, const NCURSES_CH_T *, int);
1347 extern NCURSES_EXPORT(const char *) _nc_viscbuf (const NCURSES_CH_T *, int);
1348
1349 #else /* !TRACE */
1350
1351 #define START_TRACE() /* nothing */
1352
1353 #define T(a)
1354 #define TR(n, a)
1355 #define TPUTS_TRACE(s)
1356
1357 #define returnAttr(code)        return code
1358 #define returnBits(code)        return code
1359 #define returnBool(code)        return code
1360 #define returnCPtr(code)        return code
1361 #define returnCVoidPtr(code)    return code
1362 #define returnChtype(code)      return code
1363 #define returnCode(code)        return code
1364 #define returnPtr(code)         return code
1365 #define returnSP(code)          return code
1366 #define returnVoid              return
1367 #define returnVoidPtr(code)     return code
1368 #define returnWin(code)         return code
1369
1370 #endif /* TRACE/!TRACE */
1371
1372 /*
1373  * Return-codes for tgetent() and friends.
1374  */
1375 #define TGETENT_YES  1          /* entry is found */
1376 #define TGETENT_NO   0          /* entry is not found */
1377 #define TGETENT_ERR -1          /* an error occurred */
1378
1379 extern NCURSES_EXPORT(const char *) _nc_visbuf2 (int, const char *);
1380 extern NCURSES_EXPORT(const char *) _nc_visbufn (const char *, int);
1381
1382 #define EMPTY_MODULE(name) \
1383 extern  NCURSES_EXPORT(void) name (void); \
1384         NCURSES_EXPORT(void) name (void) { }
1385
1386 #define ALL_BUT_COLOR ((chtype)~(A_COLOR))
1387 #define NONBLANK_ATTR (A_NORMAL|A_BOLD|A_DIM|A_BLINK)
1388 #define XMC_CHANGES(c) ((c) & SP->_xmc_suppress)
1389
1390 #define toggle_attr_on(S,at) {\
1391    if (PAIR_NUMBER(at) > 0) {\
1392       (S) = ((S) & ALL_BUT_COLOR) | (at);\
1393    } else {\
1394       (S) |= (at);\
1395    }\
1396    TR(TRACE_ATTRS, ("new attribute is %s", _traceattr((S))));}
1397
1398
1399 #define toggle_attr_off(S,at) {\
1400    if (PAIR_NUMBER(at) > 0) {\
1401       (S) &= ~(at|A_COLOR);\
1402    } else {\
1403       (S) &= ~(at);\
1404    }\
1405    TR(TRACE_ATTRS, ("new attribute is %s", _traceattr((S))));}
1406
1407 #define DelCharCost(count) \
1408                 ((parm_dch != 0) \
1409                 ? SP->_dch_cost \
1410                 : ((delete_character != 0) \
1411                         ? (SP->_dch1_cost * count) \
1412                         : INFINITY))
1413
1414 #define InsCharCost(count) \
1415                 ((parm_ich != 0) \
1416                 ? SP->_ich_cost \
1417                 : ((enter_insert_mode && exit_insert_mode) \
1418                   ? SP->_smir_cost + SP->_rmir_cost + (SP->_ip_cost * count) \
1419                   : ((insert_character != 0) \
1420                     ? ((SP->_ich1_cost + SP->_ip_cost) * count) \
1421                     : INFINITY)))
1422
1423 #if USE_XMC_SUPPORT
1424 #define UpdateAttrs(c)  if (!SameAttrOf(SCREEN_ATTRS(SP), c)) { \
1425                                 attr_t chg = AttrOf(SCREEN_ATTRS(SP)); \
1426                                 VIDATTR(AttrOf(c), GetPair(c)); \
1427                                 if (magic_cookie_glitch > 0 \
1428                                  && XMC_CHANGES((chg ^ AttrOf(SCREEN_ATTRS(SP))))) { \
1429                                         T(("%s @%d before glitch %d,%d", \
1430                                                 __FILE__, __LINE__, \
1431                                                 SP->_cursrow, \
1432                                                 SP->_curscol)); \
1433                                         _nc_do_xmc_glitch(chg); \
1434                                 } \
1435                         }
1436 #else
1437 #define UpdateAttrs(c)  if (!SameAttrOf(SCREEN_ATTRS(SP), c)) \
1438                                 VIDATTR(AttrOf(c), GetPair(c));
1439 #endif
1440
1441 /*
1442  * Macros to make additional parameter to implement wgetch_events()
1443  */
1444 #ifdef NCURSES_WGETCH_EVENTS
1445 #define EVENTLIST_0th(param) param
1446 #define EVENTLIST_1st(param) param
1447 #define EVENTLIST_2nd(param) , param
1448 #else
1449 #define EVENTLIST_0th(param) void
1450 #define EVENTLIST_1st(param) /* nothing */
1451 #define EVENTLIST_2nd(param) /* nothing */
1452 #endif
1453
1454 #if NCURSES_EXPANDED && NCURSES_EXT_FUNCS
1455
1456 #undef  toggle_attr_on
1457 #define toggle_attr_on(S,at) _nc_toggle_attr_on(&(S), at)
1458 extern NCURSES_EXPORT(void) _nc_toggle_attr_on (attr_t *, attr_t);
1459
1460 #undef  toggle_attr_off
1461 #define toggle_attr_off(S,at) _nc_toggle_attr_off(&(S), at)
1462 extern NCURSES_EXPORT(void) _nc_toggle_attr_off (attr_t *, attr_t);
1463
1464 #undef  DelCharCost
1465 #define DelCharCost(count) _nc_DelCharCost(count)
1466 extern NCURSES_EXPORT(int) _nc_DelCharCost (int);
1467
1468 #undef  InsCharCost
1469 #define InsCharCost(count) _nc_InsCharCost(count)
1470 extern NCURSES_EXPORT(int) _nc_InsCharCost (int);
1471
1472 #undef  UpdateAttrs
1473 #define UpdateAttrs(c) _nc_UpdateAttrs(c)
1474 extern NCURSES_EXPORT(void) _nc_UpdateAttrs (NCURSES_CH_T);
1475
1476 #else
1477
1478 extern NCURSES_EXPORT(void) _nc_expanded (void);
1479
1480 #endif
1481
1482 #if !NCURSES_EXT_FUNCS
1483 #define set_escdelay(value) ESCDELAY = value
1484 #endif
1485
1486 #if !HAVE_GETCWD
1487 #define getcwd(buf,len) getwd(buf)
1488 #endif
1489
1490 /* charable.c */
1491 #if USE_WIDEC_SUPPORT
1492 extern NCURSES_EXPORT(bool) _nc_is_charable(wchar_t);
1493 extern NCURSES_EXPORT(int) _nc_to_char(wint_t);
1494 extern NCURSES_EXPORT(wint_t) _nc_to_widechar(int);
1495 #endif
1496
1497 /* comp_captab.c */
1498 typedef struct {
1499         short   nte_name;       /* offset of name to hash on */
1500         int     nte_type;       /* BOOLEAN, NUMBER or STRING */
1501         short   nte_index;      /* index of associated variable in its array */
1502         short   nte_link;       /* index in table of next hash, or -1 */
1503 } name_table_data;
1504
1505 typedef struct
1506 {
1507         short   from;
1508         short   to;
1509         short   source;
1510 } alias_table_data;
1511
1512 /* doupdate.c */
1513 #if USE_XMC_SUPPORT
1514 extern NCURSES_EXPORT(void) _nc_do_xmc_glitch (attr_t);
1515 #endif
1516
1517 /* hardscroll.c */
1518 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
1519 extern NCURSES_EXPORT(void) _nc_linedump (void);
1520 #endif
1521
1522 /* lib_acs.c */
1523 extern NCURSES_EXPORT(void) _nc_init_acs (void);        /* corresponds to traditional 'init_acs()' */
1524 extern NCURSES_EXPORT(int) _nc_msec_cost (const char *const, int);  /* used by 'tack' program */
1525
1526 /* lib_addch.c */
1527 #if USE_WIDEC_SUPPORT
1528 NCURSES_EXPORT(int) _nc_build_wch(WINDOW *win, ARG_CH_T ch);
1529 #endif
1530
1531 /* lib_addstr.c */
1532 #if USE_WIDEC_SUPPORT && !defined(USE_TERMLIB)
1533 extern NCURSES_EXPORT(int) _nc_wchstrlen(const cchar_t *);
1534 #endif
1535
1536 /* lib_color.c */
1537 extern NCURSES_EXPORT(bool) _nc_reset_colors(void);
1538
1539 /* lib_getch.c */
1540 extern NCURSES_EXPORT(int) _nc_wgetch(WINDOW *, unsigned long *, int EVENTLIST_2nd(_nc_eventlist *));
1541
1542 /* lib_insch.c */
1543 extern NCURSES_EXPORT(int) _nc_insert_ch(WINDOW *, chtype);
1544
1545 /* lib_mvcur.c */
1546 #define INFINITY        1000000 /* cost: too high to use */
1547
1548 extern NCURSES_EXPORT(void) _nc_mvcur_init (void);
1549 extern NCURSES_EXPORT(void) _nc_mvcur_resume (void);
1550 extern NCURSES_EXPORT(void) _nc_mvcur_wrap (void);
1551
1552 extern NCURSES_EXPORT(int) _nc_scrolln (int, int, int, int);
1553
1554 extern NCURSES_EXPORT(void) _nc_screen_init (void);
1555 extern NCURSES_EXPORT(void) _nc_screen_resume (void);
1556 extern NCURSES_EXPORT(void) _nc_screen_wrap (void);
1557
1558 /* lib_mouse.c */
1559 extern NCURSES_EXPORT(bool) _nc_has_mouse (SCREEN *);
1560
1561 /* lib_mvcur.c */
1562 #define INFINITY        1000000 /* cost: too high to use */
1563 #define BAUDBYTE        9       /* 9 = 7 bits + 1 parity + 1 stop */
1564
1565 /* lib_setup.c */
1566 extern NCURSES_EXPORT(char *) _nc_get_locale(void);
1567 extern NCURSES_EXPORT(int) _nc_unicode_locale(void);
1568 extern NCURSES_EXPORT(int) _nc_locale_breaks_acs(TERMINAL *);
1569 extern NCURSES_EXPORT(int) _nc_setupterm(NCURSES_CONST char *, int, int *, bool);
1570 extern NCURSES_EXPORT(void) _nc_get_screensize(SCREEN *, int *, int *);
1571
1572 /* lib_tstp.c */
1573 #if USE_SIGWINCH
1574 extern NCURSES_EXPORT(int) _nc_handle_sigwinch(SCREEN *);
1575 #else
1576 #define _nc_handle_sigwinch(a) /* nothing */
1577 #endif
1578
1579 /* lib_ungetch.c */
1580 extern NCURSES_EXPORT(int) _nc_ungetch (SCREEN *, int);
1581
1582 /* lib_wacs.c */
1583 #if USE_WIDEC_SUPPORT
1584 extern NCURSES_EXPORT(void) _nc_init_wacs(void);
1585 #endif
1586
1587 typedef struct {
1588     char *s_head;       /* beginning of the string (may be null) */
1589     char *s_tail;       /* end of the string (may be null) */
1590     size_t s_size;      /* current remaining size available */
1591     size_t s_init;      /* total size available */
1592 } string_desc;
1593
1594 /* strings.c */
1595 extern NCURSES_EXPORT(string_desc *) _nc_str_init (string_desc *, char *, size_t);
1596 extern NCURSES_EXPORT(string_desc *) _nc_str_null (string_desc *, size_t);
1597 extern NCURSES_EXPORT(string_desc *) _nc_str_copy (string_desc *, string_desc *);
1598 extern NCURSES_EXPORT(bool) _nc_safe_strcat (string_desc *, const char *);
1599 extern NCURSES_EXPORT(bool) _nc_safe_strcpy (string_desc *, const char *);
1600
1601 #if !HAVE_STRSTR
1602 #define strstr _nc_strstr
1603 extern NCURSES_EXPORT(char *) _nc_strstr (const char *, const char *);
1604 #endif
1605
1606 /* safe_sprintf.c */
1607 extern NCURSES_EXPORT(char *) _nc_printf_string (const char *, va_list);
1608
1609 /* tries.c */
1610 extern NCURSES_EXPORT(int) _nc_add_to_try (TRIES **, const char *, unsigned);
1611 extern NCURSES_EXPORT(char *) _nc_expand_try (TRIES *, unsigned, int *, size_t);
1612 extern NCURSES_EXPORT(int) _nc_remove_key (TRIES **, unsigned);
1613 extern NCURSES_EXPORT(int) _nc_remove_string (TRIES **, const char *);
1614
1615 /* elsewhere ... */
1616 extern NCURSES_EXPORT(ENTRY *) _nc_delink_entry (ENTRY *, TERMTYPE *);
1617 extern NCURSES_EXPORT(NCURSES_CONST char *) _nc_keyname (SCREEN *, int);
1618 extern NCURSES_EXPORT(NCURSES_CONST char *) _nc_unctrl (SCREEN *, chtype);
1619 extern NCURSES_EXPORT(SCREEN *) _nc_screen_of (WINDOW *);
1620 extern NCURSES_EXPORT(WINDOW *) _nc_makenew (int, int, int, int, int);
1621 extern NCURSES_EXPORT(char *) _nc_trace_buf (int, size_t);
1622 extern NCURSES_EXPORT(char *) _nc_trace_bufcat (int, const char *);
1623 extern NCURSES_EXPORT(char *) _nc_tracechar (SCREEN *, int);
1624 extern NCURSES_EXPORT(char *) _nc_tracemouse (SCREEN *, MEVENT const *);
1625 extern NCURSES_EXPORT(int) _nc_access (const char *, int);
1626 extern NCURSES_EXPORT(int) _nc_baudrate (int);
1627 extern NCURSES_EXPORT(int) _nc_freewin (WINDOW *);
1628 extern NCURSES_EXPORT(int) _nc_getenv_num (const char *);
1629 extern NCURSES_EXPORT(int) _nc_keypad (SCREEN *, bool);
1630 extern NCURSES_EXPORT(int) _nc_ospeed (int);
1631 extern NCURSES_EXPORT(int) _nc_outch (int);
1632 extern NCURSES_EXPORT(int) _nc_putp(const char *, const char *);
1633 extern NCURSES_EXPORT(int) _nc_putp_flush(const char *, const char *);
1634 extern NCURSES_EXPORT(int) _nc_read_termcap_entry (const char *const, TERMTYPE *const);
1635 extern NCURSES_EXPORT(int) _nc_setupscreen (int, int, FILE *, bool, int);
1636 extern NCURSES_EXPORT(int) _nc_timed_wait (SCREEN *, int, int, int * EVENTLIST_2nd(_nc_eventlist *));
1637 extern NCURSES_EXPORT(void) _nc_do_color (short, short, bool, int (*)(int));
1638 extern NCURSES_EXPORT(void) _nc_flush (void);
1639 extern NCURSES_EXPORT(void) _nc_free_and_exit (int);
1640 extern NCURSES_EXPORT(void) _nc_free_entry (ENTRY *, TERMTYPE *);
1641 extern NCURSES_EXPORT(void) _nc_freeall (void);
1642 extern NCURSES_EXPORT(void) _nc_hash_map (void);
1643 extern NCURSES_EXPORT(void) _nc_init_keytry (SCREEN *);
1644 extern NCURSES_EXPORT(void) _nc_keep_tic_dir (const char *);
1645 extern NCURSES_EXPORT(void) _nc_make_oldhash (int i);
1646 extern NCURSES_EXPORT(void) _nc_scroll_oldhash (int n, int top, int bot);
1647 extern NCURSES_EXPORT(void) _nc_scroll_optimize (void);
1648 extern NCURSES_EXPORT(void) _nc_set_buffer (FILE *, bool);
1649 extern NCURSES_EXPORT(void) _nc_signal_handler (bool);
1650 extern NCURSES_EXPORT(void) _nc_synchook (WINDOW *);
1651 extern NCURSES_EXPORT(void) _nc_trace_tries (TRIES *);
1652
1653 #if NO_LEAKS
1654 extern NCURSES_EXPORT(void) _nc_alloc_entry_leaks(void);
1655 extern NCURSES_EXPORT(void) _nc_captoinfo_leaks(void);
1656 extern NCURSES_EXPORT(void) _nc_codes_leaks(void);
1657 extern NCURSES_EXPORT(void) _nc_comp_captab_leaks(void);
1658 extern NCURSES_EXPORT(void) _nc_comp_scan_leaks(void);
1659 extern NCURSES_EXPORT(void) _nc_keyname_leaks(void);
1660 extern NCURSES_EXPORT(void) _nc_names_leaks(void);
1661 extern NCURSES_EXPORT(void) _nc_tgetent_leaks(void);
1662 #endif
1663
1664 #ifndef USE_TERMLIB
1665 extern NCURSES_EXPORT(NCURSES_CH_T) _nc_render (WINDOW *, NCURSES_CH_T);
1666 extern NCURSES_EXPORT(int) _nc_waddch_nosync (WINDOW *, const NCURSES_CH_T);
1667 extern NCURSES_EXPORT(void) _nc_scroll_window (WINDOW *, int const, NCURSES_SIZE_T const, NCURSES_SIZE_T const, NCURSES_CH_T);
1668 #endif
1669
1670 #if USE_WIDEC_SUPPORT && !defined(USE_TERMLIB)
1671 extern NCURSES_EXPORT(size_t) _nc_wcrtomb (char *, wchar_t, mbstate_t *);
1672 #endif
1673
1674 #if USE_SIZECHANGE
1675 extern NCURSES_EXPORT(void) _nc_update_screensize (SCREEN *);
1676 #endif
1677
1678 #if HAVE_RESIZETERM
1679 extern NCURSES_EXPORT(void) _nc_resize_margins (WINDOW *);
1680 #else
1681 #define _nc_resize_margins(wp) /* nothing */
1682 #endif
1683
1684 #ifdef NCURSES_WGETCH_EVENTS
1685 extern NCURSES_EXPORT(int) _nc_eventlist_timeout(_nc_eventlist *);
1686 #else
1687 #define wgetch_events(win, evl) wgetch(win)
1688 #define wgetnstr_events(win, str, maxlen, evl) wgetnstr(win, str, maxlen)
1689 #endif
1690
1691 /*
1692  * Not everyone has vsscanf(), but we'd like to use it for scanw().
1693  */
1694 #if !HAVE_VSSCANF
1695 extern int vsscanf(const char *str, const char *format, va_list __arg);
1696 #endif
1697
1698 /* scroll indices */
1699 extern NCURSES_EXPORT_VAR(int *) _nc_oldnums;
1700
1701 #define USE_SETBUF_0 0
1702
1703 #define NC_BUFFERED(flag) _nc_set_buffer(SP->_ofp, flag)
1704
1705 #define NC_OUTPUT ((SP_PARM != 0) ? SP_PARM->_ofp : stdout)
1706
1707 /*
1708  * On systems with a broken linker, define 'SP' as a function to force the
1709  * linker to pull in the data-only module with 'SP'.
1710  */
1711 #if BROKEN_LINKER
1712 #define SP _nc_screen()
1713 extern NCURSES_EXPORT(SCREEN *) _nc_screen (void);
1714 extern NCURSES_EXPORT(int) _nc_alloc_screen (void);
1715 extern NCURSES_EXPORT(void) _nc_set_screen (SCREEN *);
1716 #else
1717 /* current screen is private data; avoid possible linking conflicts too */
1718 extern NCURSES_EXPORT_VAR(SCREEN *) SP;
1719 #define _nc_alloc_screen() ((SP = typeCalloc(SCREEN, 1)) != 0)
1720 #define _nc_set_screen(sp) SP = sp
1721 #endif
1722
1723 #define CURRENT_SCREEN SP
1724
1725 /*
1726  * We don't want to use the lines or columns capabilities internally, because
1727  * if the application is running multiple screens under X, it's quite possible
1728  * they could all have type xterm but have different sizes!  So...
1729  */
1730 #define screen_lines    SP->_lines
1731 #define screen_columns  SP->_columns
1732
1733 extern NCURSES_EXPORT(int) _nc_slk_initialize (WINDOW *, int);
1734
1735 /*
1736  * Some constants related to SLK's
1737  */
1738 #define MAX_SKEY_OLD       8    /* count of soft keys */
1739 #define MAX_SKEY_LEN_OLD   8    /* max length of soft key text */
1740 #define MAX_SKEY_PC       12    /* This is what most PC's have */
1741 #define MAX_SKEY_LEN_PC    5
1742
1743 /* Macro to check whether or not we use a standard format */
1744 #define SLK_STDFMT(fmt) (fmt < 3)
1745 /* Macro to determine height of label window */
1746 #define SLK_LINES(fmt)  (SLK_STDFMT(fmt) ? 1 : ((fmt) - 2))
1747
1748 #define MAX_SKEY(fmt)     (SLK_STDFMT(fmt)? MAX_SKEY_OLD : MAX_SKEY_PC)
1749 #define MAX_SKEY_LEN(fmt) (SLK_STDFMT(fmt)? MAX_SKEY_LEN_OLD : MAX_SKEY_LEN_PC)
1750
1751 extern NCURSES_EXPORT(int) _nc_ripoffline (int line, int (*init)(WINDOW *,int));
1752
1753 /*
1754  * Common error messages
1755  */
1756 #define MSG_NO_MEMORY "Out of memory"
1757 #define MSG_NO_INPUTS "Premature EOF"
1758
1759 /* timed_wait flag definitions */
1760 #define TW_NONE    0
1761 #define TW_INPUT   1
1762 #define TW_MOUSE   2
1763 #define TW_ANY     (TW_INPUT | TW_MOUSE)
1764 #define TW_EVENT   4
1765
1766 /*
1767  * Exported entrypoints beyond the published API
1768  */
1769 #if NCURSES_SP_FUNCS
1770 extern NCURSES_EXPORT(WINDOW *) _nc_curscr_of(SCREEN*);
1771 extern NCURSES_EXPORT(WINDOW *) _nc_newscr_of(SCREEN*);
1772 extern NCURSES_EXPORT(WINDOW *) _nc_stdscr_of(SCREEN*);
1773 extern NCURSES_EXPORT(int)      _nc_outc_wrapper(SCREEN*,int);
1774
1775 extern NCURSES_EXPORT(TERMINAL*) NCURSES_SP_NAME(_nc_set_curterm)(SCREEN*,TERMINAL*);
1776 extern NCURSES_EXPORT(int)       NCURSES_SP_NAME(_nc_del_curterm)(SCREEN*,TERMINAL*);
1777
1778 #if NCURSES_EXT_FUNCS
1779 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_set_tabsize)(SCREEN*, int);
1780 #endif
1781
1782 extern NCURSES_EXPORT(chtype)   NCURSES_SP_NAME(_nc_acs_char)(SCREEN*,int);
1783
1784 /*
1785  * We put the safe versions of various calls here as they are not published
1786  * part of the API up to now
1787  */
1788 extern NCURSES_EXPORT(SCREEN*)  _nc_SP(void);
1789
1790 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_vidputs)(SCREEN*,chtype,int(*) (SCREEN*, int));
1791 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_vidattr)(SCREEN *, chtype);
1792
1793 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scr_restore)(SCREEN*, const char*);
1794 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scr_init)(SCREEN*,const char*);
1795
1796 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_restartterm)(SCREEN*,NCURSES_CONST char*,int,int*);
1797 extern NCURSES_EXPORT(bool)     NCURSES_SP_NAME(_nc_is_term_resized)(SCREEN*,int,int);
1798 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_resize_term)(SCREEN*,int,int);
1799 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_resizeterm)(SCREEN*,int,int);
1800
1801 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_outch)(SCREEN*, int ch);
1802 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_flush)(SCREEN*);
1803 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_putp)(SCREEN*, const char *, const char *);
1804 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_putp_flush)(SCREEN*, const char *, const char *);
1805 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tgetent)(SCREEN*,char*,const char *);
1806 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tputs)(SCREEN*,const char*,int,int(*)(SCREEN*, int));
1807 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_savetty)(SCREEN*);
1808 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_resetty)(SCREEN*);
1809
1810 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_mcprint)(SCREEN*,char*, int);
1811 extern NCURSES_EXPORT(char *)   NCURSES_SP_NAME(_nc_tgetstr)(SCREEN*,NCURSES_CONST char*,char**);
1812 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tgetflag)(SCREEN*,NCURSES_CONST char*);
1813 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tgetnum)(SCREEN*,NCURSES_CONST char*);
1814 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tigetflag)(SCREEN*,NCURSES_CONST char*);
1815 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tigetnum)(SCREEN*,NCURSES_CONST char*);
1816 extern NCURSES_EXPORT(char *)   NCURSES_SP_NAME(_nc_tigetstr)(SCREEN*, NCURSES_CONST char*);
1817 extern NCURSES_EXPORT(void)     _nc_cookie_init(SCREEN *sp);
1818 extern NCURSES_EXPORT(int)      _nc_tinfo_doupdate(SCREEN *sp);
1819 extern NCURSES_EXPORT(int)      _nc_tinfo_mcprint(SCREEN*,char*,int);
1820 #if USE_WIDEC_SUPPORT
1821 extern NCURSES_EXPORT(wchar_t *) NCURSES_SP_NAME(_nc_wunctrl)(SCREEN*, cchar_t *);
1822 #endif
1823
1824 /* FIXME - move these to curses.h.in */
1825 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(getmouse) (SCREEN*, MEVENT *);
1826 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(ungetmouse) (SCREEN*, MEVENT *);
1827 extern NCURSES_EXPORT(mmask_t)  NCURSES_SP_NAME(mousemask) (SCREEN*, mmask_t, mmask_t *);
1828 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(mouseinterval) (SCREEN*, int);
1829 extern NCURSES_EXPORT(bool)     NCURSES_SP_NAME(has_mouse) (SCREEN*);
1830 #endif /* NCURSES_SP_FUNCS */
1831
1832 #ifdef __cplusplus
1833 }
1834 #endif
1835
1836 #endif /* CURSES_PRIV_H */