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