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