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