xref: /freebsd/contrib/ncurses/ncurses/curses.priv.h (revision 68ad2b0d7af2a3571c4abac9afa712f9b09b721c)
1 /****************************************************************************
2  * Copyright 2018-2024,2025 Thomas E. Dickey                                *
3  * Copyright 1998-2017,2018 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *     and: Thomas E. Dickey                        1996-on                 *
34  *     and: Juergen Pfeifer                                                 *
35  ****************************************************************************/
36 
37 /*
38  * $Id: curses.priv.h,v 1.741 2025/12/27 21:46:04 tom Exp $
39  *
40  *	curses.priv.h
41  *
42  *	Header file for curses library objects which are private to
43  *	the library.
44  *
45  */
46 
47 #ifndef CURSES_PRIV_H
48 #define CURSES_PRIV_H 1
49 /* *INDENT-OFF* */
50 
51 #include <ncurses_dll.h>
52 
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
57 #include <ncurses_cfg.h>
58 
59 #if USE_RCS_IDS
60 #define MODULE_ID(id) static const char Ident[] = id;
61 #else
62 #define MODULE_ID(id)		/* nothing */
63 #endif
64 
65 #include <stddef.h>		/* for offsetof */
66 #include <stdlib.h>
67 #include <string.h>
68 #include <sys/types.h>
69 #include <sys/stat.h>
70 
71 #if HAVE_UNISTD_H
72 #include <unistd.h>
73 #endif
74 
75 #include <assert.h>
76 #include <stdio.h>
77 
78 #include <errno.h>
79 
80 #if defined __hpux
81 #  ifndef EILSEQ
82 #    define EILSEQ 47
83 #  endif
84 #endif
85 
86 #if DECL_ERRNO
87 extern int errno;
88 #endif
89 
90 /* Some Windows related defines */
91 
92 #undef _NC_WINDOWS
93 #if (defined(_WIN32) || defined(_WIN64__) || defined(__MSYS__))
94 #define _NC_WINDOWS
95 #endif
96 
97 #undef _NC_CYGWIN
98 #if (defined(__MSYS__) || defined(__CYGWIN__))
99 #define _NC_CYGWIN
100 #endif
101 
102 #undef _NC_WINDOWS_NATIVE
103 #if (defined(_WIN32) || defined(_WIN64))
104 #define _NC_WINDOWS_NATIVE
105 #endif
106 
107 #undef _NC_MINGW
108 #if (defined(__MINGW32__) || defined(__MINGW64__))
109 #define _NC_MINGW
110 #endif
111 
112 #undef _NC_MSC
113 #ifdef _MSC_VER
114 #define _NC_MSC
115 #endif
116 
117 /* Some systems have a broken 'select()', but workable 'poll()'.  Use that */
118 #if HAVE_WORKING_POLL
119 #define USE_FUNC_POLL 1
120 #if HAVE_POLL_H
121 #include <poll.h>
122 #else
123 #include <sys/poll.h>
124 #endif
125 #else
126 #define USE_FUNC_POLL 0
127 #endif
128 
129 #if HAVE_INTTYPES_H
130 # include <inttypes.h>
131 #else
132 # if HAVE_STDINT_H
133 #  include <stdint.h>
134 # endif
135 #endif
136 
137 #if (defined(__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO != 0) && (defined(__GNUC__) && (__GNUC__ < 12))
138 # undef PRIxPTR		/* gcc bug fixed in 12.x */
139 # define PRIxPTR	"lX"
140 # define CASTxPTR(n)    (unsigned long)(intptr_t)(void*)(n)
141 #else
142 # define CASTxPTR(n)    (intptr_t)(n)
143 #endif
144 
145 #ifndef PRIxPTR
146 # define PRIxPTR	"lx"
147 # undef CASTxPTR
148 # define CASTxPTR(n)    (long)(void*)(n)
149 #endif
150 
151 /* include signal.h before curses.h to work-around defect in glibc 2.1.3 */
152 #include <signal.h>
153 
154 /* Alessandro Rubini's GPM (general-purpose mouse) */
155 #if HAVE_LIBGPM && HAVE_GPM_H
156 #define USE_GPM_SUPPORT 1
157 #else
158 #define USE_GPM_SUPPORT 0
159 #endif
160 
161 /* QNX mouse support */
162 #if defined(__QNX__) && !defined(__QNXNTO__)
163 #define USE_QNX_MOUSE 1
164 #else
165 #define USE_QNX_MOUSE 0
166 #endif
167 
168 /* EMX mouse support */
169 #ifdef __EMX__
170 #define USE_EMX_MOUSE 1
171 #else
172 #define USE_EMX_MOUSE 0
173 #endif
174 
175 /* kLIBC keyboard/mouse support */
176 #if defined(__OS2__) && defined(__KLIBC__)
177 #define USE_KLIBC_KBD   1
178 #define USE_KLIBC_MOUSE 1
179 #else
180 #define USE_KLIBC_KBD   0
181 #define USE_KLIBC_MOUSE 0
182 #endif
183 
184 #define DEFAULT_MAXCLICK 166
185 #define EV_MAX		8	/* size of mouse circular event queue */
186 
187 /*
188  * If we don't have signals to support it, don't add a sigwinch handler.
189  * In any case, resizing is an extended feature.  Use it if we've got it.
190  */
191 #if !NCURSES_EXT_FUNCS
192 #undef HAVE_SIZECHANGE
193 #define HAVE_SIZECHANGE 0
194 #endif
195 
196 #if HAVE_SIZECHANGE && USE_SIGWINCH && defined(SIGWINCH)
197 #define USE_SIZECHANGE 1
198 #else
199 #define USE_SIZECHANGE 0
200 #undef USE_SIGWINCH
201 #define USE_SIGWINCH 0
202 #endif
203 
204 /*
205  * When building in the MSYS2 environment, the automatic discovery of
206  * the path separator in configure doesn't work properly. So, if building
207  * for MinGW, we enforce the correct Windows PATH separator
208  */
209 #if defined(_NC_WINDOWS_NATIVE) || defined(__DJGPP__)
210 #  ifdef NCURSES_PATHSEP
211 #    undef NCURSES_PATHSEP
212 #  endif
213 #  define NCURSES_PATHSEP ';'
214 #endif
215 
216 /*
217  * When the standard handles have been redirected (such as inside a text editor
218  * or the less utility), keystrokes must be read from the console rather than
219  * the redirected handle.  The standard output handle suffers from a similar
220  * problem.  Both handles are not closed once opened.  The console shall be
221  * considered reachable throughout the process.
222  */
223 #if defined(_NC_WINDOWS)
224 #define GetDirectHandle(fileName, shareMode) \
225 	CreateFile(TEXT(fileName), \
226 		   GENERIC_READ | GENERIC_WRITE, \
227 		   shareMode, \
228 		   0, \
229 		   OPEN_EXISTING, \
230 		   0, \
231 		   0)
232 #endif
233 
234 /*
235  * The stricmp() function is in MSVCRT, and cannot be called or linked from
236  * Cygwin runtime-dependent binaries currently. Use the POSIX strcaseccmp()
237  * function instead which is pretty much the same.
238  */
239 #if defined(_NC_CYGWIN)
240 #define stricmp(s1, s2) strcasecmp(s1, s2)
241 #endif
242 
243 /*
244  * Not all platforms have memmove; some have an equivalent bcopy.  (Some may
245  * have neither).
246  */
247 #if USE_OK_BCOPY
248 #define memmove(d,s,n) bcopy(s,d,n)
249 #elif USE_MY_MEMMOVE
250 #define memmove(d,s,n) _nc_memmove(d,s,n)
251 extern NCURSES_EXPORT(void *) _nc_memmove (void *, const void *, size_t);
252 #endif
253 
254 /*
255  * If we have va_copy(), use it for assigning va_list's.
256  */
257 #if defined(HAVE___VA_COPY)
258 #define begin_va_copy(dst,src)	__va_copy(dst, src)
259 #define end_va_copy(dst)	va_end(dst)
260 #elif defined(va_copy) || defined(HAVE_VA_COPY)
261 #define begin_va_copy(dst,src)	va_copy(dst, src)
262 #define end_va_copy(dst)	va_end(dst)
263 #else
264 #define begin_va_copy(dst,src) (dst) = (src)
265 #define end_va_copy(dst)	/* nothing */
266 #endif
267 
268 /*
269  * Either/both S_ISxxx and/or S_IFxxx are defined in sys/types.h; some systems
270  * lack one or the other.
271  */
272 #ifndef S_ISDIR
273 #define S_ISDIR(mode) ((mode & S_IFMT) == S_IFDIR)
274 #endif
275 
276 #ifndef S_ISREG
277 #define S_ISREG(mode) ((mode & S_IFMT) == S_IFREG)
278 #endif
279 
280 /*
281  * POSIX ignores the "b", which c89 specified.  Some very old systems do not
282  * accept it.
283  */
284 #if USE_FOPEN_BIN_R
285 #define BIN_R	"rb"
286 #define BIN_W	"wb"
287 #else
288 #define BIN_R	"r"
289 #define BIN_W	"w"
290 #endif
291 
292 /*
293  * Scroll hints are useless when hashmap is used
294  */
295 #if !USE_SCROLL_HINTS
296 #if !USE_HASHMAP
297 #define USE_SCROLL_HINTS 1
298 #else
299 #define USE_SCROLL_HINTS 0
300 #endif
301 #endif
302 
303 #if USE_SCROLL_HINTS
304 #define if_USE_SCROLL_HINTS(stmt) stmt
305 #else
306 #define if_USE_SCROLL_HINTS(stmt) /*nothing*/
307 #endif
308 
309 #include <nc_string.h>
310 
311 /*
312  * Options for terminal drivers, etc...
313  */
314 #if USE_TERM_DRIVER
315 #define NO_TERMINAL "unknown"
316 #define USE_SP_RIPOFF     1
317 #define USE_SP_TERMTYPE   1
318 #else
319 #define NO_TERMINAL 0
320 #endif
321 
322 #define VALID_TERM_ENV(term_env, no_terminal) \
323 	(term_env = (NonEmpty(term_env) \
324 		     ? term_env \
325 		     : no_terminal), \
326 	 NonEmpty(term_env))
327 
328 /*
329  * Originally a terminal-driver option, the window-list is per-screen to allow
330  * freeing memory used for windows when a screen is deleted.
331  */
332 #define USE_SP_WINDOWLIST 1
333 
334 /*
335  * Note:  ht/cbt expansion flakes out randomly under Linux 1.1.47, but only
336  * when we're throwing control codes at the screen at high volume.  To see
337  * this, re-enable USE_HARD_TABS and run worm for a while.  Other systems
338  * probably don't want to define this either due to uncertainties about tab
339  * delays and expansion in raw mode.
340  */
341 
342 #define TRIES struct tries
343 typedef TRIES {
344 	TRIES    *child;            /* ptr to child.  NULL if none          */
345 	TRIES    *sibling;          /* ptr to sibling.  NULL if none        */
346 	unsigned char    ch;        /* character at this node               */
347 	unsigned short   value;     /* code of string so far.  0 if none.   */
348 #undef TRIES
349 } TRIES;
350 
351 /*
352  * Common/troublesome character definitions
353  */
354 #define StringOf(ch) {ch, 0}
355 
356 #define CSI_CHR 0x9b
357 #define ESC_CHR 0x1b
358 
359 #define L_BLOCK '['
360 #define R_BLOCK ']'
361 #define L_BRACE '{'
362 #define R_BRACE '}'
363 #define S_QUOTE '\''
364 #define D_QUOTE '"'
365 
366 #define VT_ACSC "``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~"
367 
368 /*
369  * To make them easier to find, user-defined capabilities used within ncurses
370  * should be tagged with this macro:
371  */
372 #define UserCap(name) #name
373 
374 /*
375  * Structure for palette tables
376  */
377 
378 #define MAXCOLUMNS    135
379 #define MAXLINES      66
380 #define FIFO_SIZE     MAXCOLUMNS+2  /* for nocbreak mode input */
381 
382 #define ACS_LEN       128
383 
384 #define WINDOWLIST struct _win_list
385 
386 #if USE_WIDEC_SUPPORT
387 #define _nc_bkgd    _bkgrnd
388 #else
389 #undef _XOPEN_SOURCE_EXTENDED
390 #undef _XPG5
391 #define _nc_bkgd    _bkgd
392 #define wgetbkgrnd(win, wch)	((*wch = win->_bkgd) != 0 ? OK : ERR)
393 #define wbkgrnd	    wbkgd
394 #endif
395 
396 #undef NCURSES_OPAQUE
397 #define NCURSES_INTERNALS 1
398 #define NCURSES_OPAQUE 0
399 
400 #include <curses.h>	/* we'll use -Ipath directive to get the right one! */
401 
402 #if !(defined(NCURSES_WGETCH_EVENTS) && defined(NEED_KEY_EVENT))
403 #undef KEY_EVENT		/* reduce compiler-warnings with Visual C++ */
404 #endif
405 
406 /*
407  * Macros to make additional parameter to implement wgetch_events()
408  */
409 #ifdef NCURSES_WGETCH_EVENTS
410 #define EVENTLIST_0th(param) param
411 #define EVENTLIST_1st(param) param
412 #define EVENTLIST_2nd(param) , param
413 #define TWAIT_MASK (TW_ANY | TW_EVENT)
414 #else
415 #define EVENTLIST_0th(param) void
416 #define EVENTLIST_1st(param) /* nothing */
417 #define EVENTLIST_2nd(param) /* nothing */
418 #define TWAIT_MASK TW_ANY
419 #endif
420 
421 #include <term.priv.h>		/* defines TERMIOS via term.h */
422 
423 #if USE_TERM_DRIVER
424 #if defined(TERMIOS)
425 #undef  USE_NAMED_PIPES
426 #define USE_NAMED_PIPES 0
427 #undef  USE_WIN32CON_DRIVER
428 #elif defined(_NC_WINDOWS)
429 #include <nc_win32.h>
430 #endif /* TERMIOS */
431 #endif /* USE_TERM_DRIVER */
432 
433 #ifndef FixupPathname
434 #define FixupPathname(path) /* nothing */
435 #define FixupPathname2(path,buffer) /* nothing */
436 #endif
437 
438 typedef struct
439 {
440     int red, green, blue;	/* what color_content() returns */
441     int r, g, b;		/* params to init_color() */
442     int init;			/* true if we called init_color() */
443 }
444 color_t;
445 
446 typedef union {
447     struct {
448 	unsigned char red;
449 	unsigned char green;
450 	unsigned char blue;
451     } bits;			/* bits per color-value in RGB */
452     unsigned value;
453 } rgb_bits_t;
454 
455 /*
456  * If curses.h did not expose the SCREEN-functions, then we do not need the
457  * parameter in the corresponding unextended functions.
458  */
459 
460 #define USE_SP_FUNC_SUPPORT     NCURSES_SP_FUNCS
461 #define USE_EXT_SP_FUNC_SUPPORT (NCURSES_SP_FUNCS && NCURSES_EXT_FUNCS)
462 
463 #if NCURSES_SP_FUNCS
464 #define SP_PARM         sp	/* use parameter */
465 #define NCURSES_SP_ARG          SP_PARM
466 #define NCURSES_SP_DCL  SCREEN *NCURSES_SP_ARG
467 #define NCURSES_SP_DCL0 NCURSES_SP_DCL
468 #define NCURSES_SP_ARGx         NCURSES_SP_ARG,
469 #define NCURSES_SP_DCLx SCREEN *NCURSES_SP_ARGx
470 #else
471 #define SP_PARM         SP	/* use global variable */
472 #define NCURSES_SP_ARG
473 #define NCURSES_SP_DCL
474 #define NCURSES_SP_DCL0 void
475 #define NCURSES_SP_ARGx
476 #define NCURSES_SP_DCLx
477 #endif
478 
479 #include <nc_panel.h>
480 
481 #include <nc_termios.h>
482 
483 #define IsPreScreen(sp)      (((sp) != NULL) && sp->_prescreen)
484 #define HasTerminal(sp)      (((sp) != NULL) && (NULL != ((sp)->_term)))
485 #define IsValidScreen(sp)    (HasTerminal(sp) && !IsPreScreen(sp))
486 
487 #if USE_REENTRANT
488 #define CurTerm              _nc_prescreen._cur_term
489 #else
490 #define CurTerm              cur_term
491 #endif
492 
493 #if NCURSES_SP_FUNCS
494 #define TerminalOf(sp)       ((sp) ? ((sp)->_term ? (sp)->_term : CurTerm) : CurTerm)
495 #else
496 #define TerminalOf(sp)       CurTerm
497 #endif
498 
499 /*
500  * The legacy layout for TERMTYPE uses "short" for all of the numbers.  Moving
501  * past that, numeric capabilities can be "int" by using a TERMTYPE2 structure
502  * in TERMINAL, and doing most of the internal work using TERMTYPE2.  There are
503  * a few places (mostly to expose the legacy layout) where the distinction
504  * needs attention.
505  */
506 #if NCURSES_EXT_COLORS && HAVE_INIT_EXTENDED_COLOR
507 #define NCURSES_EXT_NUMBERS  1
508 #define NCURSES_INT2         int
509 #define SIZEOF_INT2          4
510 #define TerminalType(tp)     (tp)->type2
511 #else
512 #define NCURSES_EXT_NUMBERS  0
513 #define NCURSES_INT2         short
514 #define SIZEOF_INT2          2
515 #define TerminalType(tp)     (tp)->type
516 #endif
517 
518 #define SIZEOF_SHORT         2
519 
520 #ifdef CUR
521 #undef CUR
522 #define CUR TerminalType(cur_term).
523 #endif
524 
525 /*
526  * Reduce dependency on cur_term global by using terminfo data from SCREEN's
527  * pointer to this data.
528  */
529 #ifdef USE_SP_TERMTYPE
530 #undef CUR
531 #endif
532 
533 #define SP_TERMTYPE TerminalType(TerminalOf(sp)).
534 
535 #include <term_entry.h>
536 
537 #include <nc_tparm.h>
538 
539 /*
540  * Simplify ifdef's for the "*_ATTR" macros in case italics are not configured.
541  */
542 #if defined(A_ITALIC) && defined(exit_italics_mode)
543 #define USE_ITALIC 1
544 #else
545 #define USE_ITALIC 0
546 #undef  A_ITALIC
547 #define A_ITALIC 0
548 #endif
549 
550 /*
551  * Use these macros internally, to make tracing less verbose.  But leave the
552  * option for compiling the tracing into the library.
553  */
554 #if 1
555 #define ColorPair(n)		(NCURSES_BITS(n, 0) & A_COLOR)
556 #define PairNumber(a)		(NCURSES_CAST(int,(((unsigned long)(a) & A_COLOR) >> NCURSES_ATTR_SHIFT)))
557 #else
558 #define ColorPair(pair)		COLOR_PAIR(pair)
559 #define PairNumber(attr)	PAIR_NUMBER(attr)
560 #endif
561 
562 #define unColor(n)		unColor2(AttrOf(n))
563 #define unColor2(a)		((a) & ALL_BUT_COLOR)
564 
565 /*
566  * Extended-colors stores the color pair in a separate struct-member than the
567  * attributes.  But for compatibility, we handle most cases where a program
568  * written for non-extended colors stores the color in the attributes by
569  * checking for a color pair in both places.
570  */
571 #if NCURSES_EXT_COLORS
572 #define if_EXT_COLORS(stmt)	stmt
573 #define SetPair(value,p)	SetPair2((value).ext_color, AttrOf(value), p)
574 #define SetPair2(c,a,p)		c = (p), \
575 				a = (unColor2(a) | ColorPair(oldColor(c)))
576 #define GetPair(value)		GetPair2((value).ext_color, AttrOf(value))
577 #define GetPair2(c,a)		((c) ? (c) : PairNumber(a))
578 #define oldColor(p)		(((p) > 255) ? 255 : (p))
579 #define GET_WINDOW_PAIR(w)	GetPair2((w)->_color, (w)->_attrs)
580 #define SET_WINDOW_PAIR(w,p)	(w)->_color = (p)
581 #define SameAttrOf(a,b)		(AttrOf(a) == AttrOf(b) && GetPair(a) == GetPair(b))
582 
583 #define VIDPUTS(sp,attr,pair)	do { \
584 				    int vid_pair = pair; \
585 				    NCURSES_SP_NAME(vid_puts)( \
586 					NCURSES_SP_ARGx attr, \
587 					(NCURSES_PAIRS_T) pair, \
588 					&vid_pair, \
589 					NCURSES_OUTC_FUNC); \
590 				} while (0)
591 
592 #else /* !NCURSES_EXT_COLORS */
593 
594 #define if_EXT_COLORS(stmt)	/* nothing */
595 #define SetPair(value,p)	RemAttr(value, A_COLOR), \
596 				SetAttr(value, AttrOf(value) | ColorPair(p))
597 #define GetPair(value)		PairNumber(AttrOf(value))
598 #define GET_WINDOW_PAIR(w)	PairNumber(WINDOW_ATTRS(w))
599 #define SET_WINDOW_PAIR(w,p)	WINDOW_ATTRS(w) &= ALL_BUT_COLOR, \
600 				WINDOW_ATTRS(w) |= ColorPair(p)
601 #define SameAttrOf(a,b)		(AttrOf(a) == AttrOf(b))
602 
603 #define VIDPUTS(sp,attr,pair)	NCURSES_SP_NAME(vidputs)(NCURSES_SP_ARGx attr, NCURSES_OUTC_FUNC)
604 
605 #endif /* NCURSES_EXT_COLORS */
606 
607 #define NCURSES_OUTC_FUNC       NCURSES_SP_NAME(_nc_outch)
608 #define NCURSES_PUTP2(name,value)    NCURSES_SP_NAME(_nc_putp)(NCURSES_SP_ARGx name, value)
609 #define NCURSES_PUTP2_FLUSH(name,value)    NCURSES_SP_NAME(_nc_putp_flush)(NCURSES_SP_ARGx name, value)
610 
611 #if NCURSES_NO_PADDING
612 #define GetNoPadding(sp)	((sp) ? (sp)->_no_padding : _nc_prescreen._no_padding)
613 #define SetNoPadding(sp)	_nc_set_no_padding(sp)
614 extern NCURSES_EXPORT(void)     _nc_set_no_padding(SCREEN *);
615 #else
616 #define GetNoPadding(sp)	FALSE
617 #define SetNoPadding(sp)	/*nothing*/
618 #endif
619 
620 #define WINDOW_ATTRS(w)		((w)->_attrs)
621 
622 #define SCREEN_ATTRS(s)		(*((s)->_current_attr))
623 #define GET_SCREEN_PAIR(s)	GetPair(SCREEN_ATTRS(s))
624 #define SET_SCREEN_PAIR(s,p)	SetPair(SCREEN_ATTRS(s), p)
625 
626 #if USE_REENTRANT || NCURSES_SP_FUNCS
627 extern NCURSES_EXPORT(int *)    _nc_ptr_Lines (SCREEN *);
628 extern NCURSES_EXPORT(int *)    _nc_ptr_Cols (SCREEN *);
629 extern NCURSES_EXPORT(int *)    _nc_ptr_Tabsize (SCREEN *);
630 extern NCURSES_EXPORT(int *)    _nc_ptr_Escdelay (SCREEN *);
631 #endif
632 
633 #if USE_REENTRANT
634 
635 #define ptrLines(sp)         (sp ? &(sp->_LINES) : &(_nc_prescreen._LINES))
636 #define ptrCols(sp)          (sp ? &(sp->_COLS) : &(_nc_prescreen._COLS))
637 #define ptrTabsize(sp)       (sp ? &(sp->_TABSIZE) : &(_nc_prescreen._TABSIZE))
638 #define ptrEscdelay(sp)      (sp ? &(sp->_ESCDELAY) : &(_nc_prescreen._ESCDELAY))
639 
640 #define SET_LINES(value)     *_nc_ptr_Lines(SP_PARM) = value
641 #define SET_COLS(value)      *_nc_ptr_Cols(SP_PARM) = value
642 #define SET_TABSIZE(value)   *_nc_ptr_Tabsize(SP_PARM) = value
643 #define SET_ESCDELAY(value)  *_nc_ptr_Escdelay(SP_PARM) = value
644 
645 #else
646 
647 #define ptrLines(sp)         &LINES
648 #define ptrCols(sp)          &COLS
649 #define ptrTabsize(sp)       &TABSIZE
650 #define ptrEscdelay(sp)      &ESCDELAY
651 
652 #define SET_LINES(value)     LINES = value
653 #define SET_COLS(value)      COLS = value
654 #define SET_TABSIZE(value)   TABSIZE = value
655 #define SET_ESCDELAY(value)  ESCDELAY = value
656 
657 #endif
658 
659 #define IS_SUBWIN(w)         ((w)->_flags & _SUBWIN)
660 #define IS_PAD(w)            ((w)->_flags & _ISPAD)
661 #define IS_WRAPPED(w)        ((w)->_flags & _WRAPPED)
662 
663 #define HasHardTabs()	(NonEmpty(clear_all_tabs) && NonEmpty(set_tab))
664 
665 #define TR_MUTEX(data) _tracef("%s@%d: me:%08lX COUNT:%2u/%2d/%6d/%2d/%s%9u: " #data, \
666 	    __FILE__, __LINE__, \
667 	    (unsigned long) (pthread_self()), \
668 	    data.__data.__lock, \
669 	    data.__data.__count, \
670 	    data.__data.__owner, \
671 	    data.__data.__kind, \
672 	    (data.__data.__nusers > 5) ? " OOPS " : "", \
673 	    data.__data.__nusers)
674 #define TR_GLOBAL_MUTEX(name) TR_MUTEX(_nc_globals.mutex_##name)
675 
676 #if USE_WEAK_SYMBOLS
677 #if defined(__GNUC__)
678 #  if defined __USE_ISOC99
679 #    define _cat_pragma(exp)	_Pragma(#exp)
680 #    define _weak_pragma(exp)	_cat_pragma(weak name)
681 #  else
682 #    define _weak_pragma(exp)
683 #  endif
684 #  define _declare(name)	__extension__ extern __typeof__(name) name
685 #  define weak_symbol(name)	_weak_pragma(name) _declare(name) __attribute__((weak))
686 #else
687 #  undef USE_WEAK_SYMBOLS
688 #  define USE_WEAK_SYMBOLS 0
689 #endif
690 #endif
691 
692 #ifdef USE_PTHREADS
693 
694 #if USE_REENTRANT
695 #include <pthread.h>
696 extern NCURSES_EXPORT(void) _nc_init_pthreads(void);
697 extern NCURSES_EXPORT(void) _nc_mutex_init(pthread_mutex_t *);
698 extern NCURSES_EXPORT(int) _nc_mutex_lock(pthread_mutex_t *);
699 extern NCURSES_EXPORT(int) _nc_mutex_trylock(pthread_mutex_t *);
700 extern NCURSES_EXPORT(int) _nc_mutex_unlock(pthread_mutex_t *);
701 #define _nc_lock_global(name)	_nc_mutex_lock(&_nc_globals.mutex_##name)
702 #define _nc_try_global(name)    _nc_mutex_trylock(&_nc_globals.mutex_##name)
703 #define _nc_unlock_global(name)	_nc_mutex_unlock(&_nc_globals.mutex_##name)
704 
705 #else
706 #error POSIX threads requires --enable-reentrant option
707 #endif
708 
709 #ifdef USE_PTHREADS
710 #  if USE_WEAK_SYMBOLS
711 weak_symbol(pthread_sigmask);
712 weak_symbol(pthread_kill);
713 weak_symbol(pthread_self);
714 weak_symbol(pthread_equal);
715 weak_symbol(pthread_mutex_init);
716 weak_symbol(pthread_mutex_lock);
717 weak_symbol(pthread_mutex_unlock);
718 weak_symbol(pthread_mutex_trylock);
719 weak_symbol(pthread_mutexattr_settype);
720 weak_symbol(pthread_mutexattr_init);
721 extern NCURSES_EXPORT(int) _nc_sigprocmask(int, const sigset_t *, sigset_t *);
722 #    undef  sigprocmask
723 #    define sigprocmask(a, b, c) _nc_sigprocmask(a, b, c)
724 #    define GetThreadID() (((pthread_self)) ? pthread_self() : (pthread_t) getpid())
725 #  else
726 #    define GetThreadID() pthread_self()
727 #  endif
728 #endif
729 
730 #if HAVE_NANOSLEEP
731 #undef HAVE_NANOSLEEP
732 #define HAVE_NANOSLEEP 0	/* nanosleep suspends all threads */
733 #endif
734 
735 #else /* !USE_PTHREADS */
736 
737 #if USE_PTHREADS_EINTR
738 #  if USE_WEAK_SYMBOLS
739 #include <pthread.h>
740 weak_symbol(pthread_sigmask);
741 weak_symbol(pthread_kill);
742 weak_symbol(pthread_self);
743 weak_symbol(pthread_equal);
744 extern NCURSES_EXPORT(int) _nc_sigprocmask(int, const sigset_t *, sigset_t *);
745 #    undef  sigprocmask
746 #    define sigprocmask(a, b, c) _nc_sigprocmask(a, b, c)
747 #  endif
748 #endif /* USE_PTHREADS_EINTR */
749 
750 #define _nc_init_pthreads()	/* nothing */
751 #define _nc_mutex_init(obj)	/* nothing */
752 
753 #define _nc_lock_global(name)	/* nothing */
754 #define _nc_try_global(name)    0
755 #define _nc_unlock_global(name)	/* nothing */
756 #endif /* USE_PTHREADS */
757 
758 #if USE_PTHREADS_EINTR
759 extern NCURSES_EXPORT(void) _nc_set_read_thread(bool);
760 #else
761 #define _nc_set_read_thread(enable)	/* nothing */
762 #endif
763 
764 /*
765  * When using sp-funcs, locks are targeted to SCREEN-level granularity.
766  * So the locking is done in the non-sp-func (which calls the sp-func) rather
767  * than in the sp-func itself.
768  *
769  * Use the _nc_nonsp_XXX functions in the function using "NCURSES_SP_NAME()".
770  * Use the _nc_sp_XXX functions in the function using "#if NCURSES_SP_FUNCS".
771  */
772 #if NCURSES_SP_FUNCS
773 
774 #define _nc_nonsp_lock_global(name)	/* nothing */
775 #define _nc_nonsp_try_global(name)    0
776 #define _nc_nonsp_unlock_global(name)	/* nothing */
777 
778 #define _nc_sp_lock_global(name)	_nc_lock_global(name)
779 #define _nc_sp_try_global(name)         _nc_try_global(name)
780 #define _nc_sp_unlock_global(name)	_nc_unlock_global(name)
781 
782 #else
783 
784 #define _nc_nonsp_lock_global(name)	_nc_lock_global(name)
785 #define _nc_nonsp_try_global(name)      _nc_try_global(name)
786 #define _nc_nonsp_unlock_global(name)	_nc_unlock_global(name)
787 
788 #define _nc_sp_lock_global(name)	/* nothing */
789 #define _nc_sp_try_global(name)    0
790 #define _nc_sp_unlock_global(name)	/* nothing */
791 
792 #endif
793 
794 #if HAVE_CLOCK_GETTIME
795 # define PRECISE_GETTIME 1
796 # define GetClockTime(t) clock_gettime(CLOCK_REALTIME, t)
797 # define TimeType struct timespec
798 # define TimeScale 1000000000L		/* 1e9 */
799 # define sub_secs tv_nsec
800 #elif HAVE_GETTIMEOFDAY
801 # define PRECISE_GETTIME 1
802 # define GetClockTime(t) gettimeofday(t, 0)
803 # define TimeType struct timeval
804 # define TimeScale 1000000L		/* 1e6 */
805 # define sub_secs tv_usec
806 #else
807 # define PRECISE_GETTIME 0
808 # define TimeType time_t
809 #endif
810 
811 /*
812  * Definitions for color pairs
813  */
814 
815 #define MAX_OF_TYPE(t)   (int)(((unsigned t)(~0))>>1)
816 
817 #include <new_pair.h>
818 
819 #define isDefaultColor(c)	((c) < 0)
820 #define COLOR_DEFAULT		-1
821 
822 #if defined(USE_BUILD_CC) || (defined(USE_TERMLIB) && !defined(NEED_NCURSES_CH_T))
823 
824 #undef NCURSES_CH_T		/* this is not a termlib feature */
825 #define NCURSES_CH_T void	/* ...but we need a pointer in SCREEN */
826 
827 #endif	/* USE_TERMLIB */
828 
829 #ifndef USE_TERMLIB
830 struct ldat
831 {
832 	NCURSES_CH_T	*text;		/* text of the line */
833 	NCURSES_SIZE_T	firstchar;	/* first changed character in the line */
834 	NCURSES_SIZE_T	lastchar;	/* last changed character in the line */
835 	NCURSES_SIZE_T	oldindex;	/* index of the line at last update */
836 };
837 #endif	/* USE_TERMLIB */
838 
839 typedef enum {
840 	M_XTERM	= -1		/* use xterm's mouse tracking? */
841 	,M_NONE = 0		/* no mouse device */
842 #if USE_GPM_SUPPORT
843 	,M_GPM			/* use GPM */
844 #endif
845 #if USE_SYSMOUSE
846 	,M_SYSMOUSE		/* FreeBSD sysmouse on console */
847 #endif
848 #if USE_TERM_DRIVER
849 	,M_TERM_DRIVER		/* Win32 console, etc */
850 #endif
851 } MouseType;
852 
853 typedef enum {
854     	MF_X10 = 0		/* conventional 3-byte format */
855 	, MF_SGR1006		/* xterm private mode 1006, SGR-style */
856 #ifdef EXP_XTERM_1005
857 	, MF_XTERM_1005		/* xterm UTF-8 private mode 1005 */
858 #endif
859 } MouseFormat;
860 
861 /*
862  * Structures for scrolling.
863  */
864 
865 typedef struct {
866 	unsigned long hashval;
867 	int oldcount, newcount;
868 	int oldindex, newindex;
869 } HASHMAP;
870 
871 /*
872  * Structures for soft labels.
873  */
874 
875 struct _SLK;
876 
877 #if !(defined(USE_TERMLIB) || defined(USE_BUILD_CC))
878 
879 typedef struct
880 {
881 	char *ent_text;		/* text for the label */
882 	char *form_text;	/* formatted text (left/center/...) */
883 	int ent_x;		/* x coordinate of this field */
884 	char dirty;		/* this label has changed */
885 	char visible;		/* field is visible */
886 } slk_ent;
887 
888 typedef struct _SLK {
889 	bool    dirty;		/* all labels have changed */
890 	bool    hidden;		/* soft labels are hidden */
891 	WINDOW  *win;
892 	slk_ent *ent;
893 	short   maxlab;		/* number of available labels */
894 	short   labcnt;		/* number of allocated labels */
895 	short   maxlen;		/* length of labels */
896 	NCURSES_CH_T attr;	/* soft label attribute */
897 } SLK;
898 
899 #endif	/* USE_TERMLIB */
900 
901 #if USE_GPM_SUPPORT
902 #undef buttons			/* term.h defines this, and gpm uses it! */
903 #include <gpm.h>
904 #if USE_WEAK_SYMBOLS
905 weak_symbol(Gpm_Wgetch);
906 #endif
907 
908 #ifdef HAVE_LIBDL
909 /* link dynamically to GPM */
910 typedef int *TYPE_gpm_fd;
911 typedef int (*TYPE_Gpm_Open) (Gpm_Connect *, int);
912 typedef int (*TYPE_Gpm_Close) (void);
913 typedef int (*TYPE_Gpm_GetEvent) (Gpm_Event *);
914 
915 #define my_gpm_fd       SP_PARM->_mouse_gpm_fd
916 #define my_Gpm_Open     SP_PARM->_mouse_Gpm_Open
917 #define my_Gpm_Close    SP_PARM->_mouse_Gpm_Close
918 #define my_Gpm_GetEvent SP_PARM->_mouse_Gpm_GetEvent
919 #else
920 /* link statically to GPM */
921 #define my_gpm_fd       &gpm_fd
922 #define my_Gpm_Open     Gpm_Open
923 #define my_Gpm_Close    Gpm_Close
924 #define my_Gpm_GetEvent Gpm_GetEvent
925 #endif /* HAVE_LIBDL */
926 #endif /* USE_GPM_SUPPORT */
927 
928 /*
929  * Limit delay-times to 30 seconds, which is consistent with signed 16-bit
930  * numbers in legacy terminfo.
931  */
932 #define MAX_DELAY_MSECS 30000
933 
934 /*
935  * Limit screen dimensions read from environment variables.
936  */
937 #define MAX_ENV_LINES	512
938 #define MAX_ENV_COLUMNS	512
939 
940 /*
941  * When converting from terminfo to termcap, check for cases where we can trim
942  * octal escapes down to 2-character form.  It is useful for terminfo format
943  * also, but not as important.
944  */
945 #define MAX_TC_FIXUPS	10
946 #define MIN_TC_FIXUPS	4
947 
948 #define isoctal(c) ((c) >= '0' && (c) <= '7')
949 
950 /*
951  * The filesystem database normally uses a single-letter for the lower level
952  * of directories.  Use a hexadecimal code for filesystems which do not
953  * preserve mixed-case names.
954  *
955  * The leading "./" is used in _nc_to_dospath() to decide whether to prefix
956  * the converted path with the MinGW32 base directory.
957  */
958 #if MIXEDCASE_FILENAMES
959 #define LEAF_FMT "./%c"
960 #define LEAF_LEN 3
961 #else
962 #define LEAF_FMT "./%02x"
963 #define LEAF_LEN 4
964 #endif
965 
966 /*
967  * TRACEMSE_FMT is no longer than 80 columns, there are 5 numbers that
968  * could at most have 10 digits, and the mask contains no more than 32 bits
969  * with each bit representing less than 15 characters.  Usually the whole
970  * string is less than 80 columns, but this buffer size is an absolute
971  * limit.
972  */
973 #define TRACECHR_BUF	40
974 #define TRACEMSE_MAX	(80 + (5 * 10) + (32 * 15))
975 #define TRACEMSE_FMT	"id %2d  at (%2d, %2d, %2d) state %4lx = {" /* } */
976 
977 #if USE_TERM_DRIVER
978 struct DriverTCB; /* Terminal Control Block forward declaration */
979 #define INIT_TERM_DRIVER()	_nc_globals.term_driver = _nc_get_driver
980 #else
981 #define INIT_TERM_DRIVER()	/* nothing */
982 #endif
983 
984 extern NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals;
985 
986 /* The limit reserves one byte for a terminating NUL */
987 #define my_getstr_limit	(_nc_globals.getstr_limit - 1)
988 #define _nc_getstr_limit(n) \
989 	(((n) < 0) \
990 	 ? my_getstr_limit \
991 	 : (((n) > my_getstr_limit) \
992 	    ? my_getstr_limit \
993 	    : (n)))
994 
995 /*
996  * Use screen-specific ripoff data (for softkeys) rather than global.
997  */
998 #ifdef USE_SP_RIPOFF
999 #define safe_ripoff_sp     (sp)->rsp
1000 #define safe_ripoff_stack  (sp)->rippedoff
1001 #else
1002 #define safe_ripoff_sp	   _nc_prescreen.rsp
1003 #define safe_ripoff_stack  _nc_prescreen.rippedoff
1004 #endif
1005 
1006 extern NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen;
1007 
1008 typedef enum {
1009     ewUnknown = 0,
1010     ewInitial,
1011     ewRunning,
1012     ewSuspend
1013 } ENDWIN;
1014 
1015 typedef struct {
1016 	int		_nl;		/* True if NL -> CR/NL is on	    */
1017 	int		_raw;		/* True if in raw mode		    */
1018 	int		_cbreak;	/* 1 if in cbreak mode		    */
1019 					/* > 1 if in halfdelay mode	    */
1020 	int		_echo;		/* True if echo on		    */
1021 } TTY_FLAGS;
1022 
1023 #define IsNl(sp)        (sp)->_tty_flags._nl
1024 #define IsRaw(sp)       (sp)->_tty_flags._raw
1025 #define IsCbreak(sp)    (sp)->_tty_flags._cbreak
1026 #define IsEcho(sp)      (sp)->_tty_flags._echo
1027 
1028 /*
1029  * The SCREEN structure.
1030  */
1031 typedef struct screen {
1032 	int		_ifd;		/* input file descriptor for screen */
1033 	int		_ofd;		/* output file descriptor for screen */
1034 	FILE		*_ofp;		/* output file ptr for screen	    */
1035 	char		*out_buffer;	/* output buffer		    */
1036 	size_t		out_limit;	/* output buffer size		    */
1037 	size_t		out_inuse;	/* output buffer current use	    */
1038 	bool		_filtered;	/* filter() was called		    */
1039 	bool		_prescreen;	/* is in prescreen phase	    */
1040 	bool		_use_env;	/* LINES & COLS from environment?   */
1041 	int		_checkfd;	/* filedesc for typeahead check	    */
1042 	TERMINAL	*_term;		/* terminal type information	    */
1043 	TTY		_saved_tty;	/* savetty/resetty information	    */
1044 	NCURSES_SIZE_T	_lines;		/* screen lines			    */
1045 	NCURSES_SIZE_T	_columns;	/* screen columns		    */
1046 
1047 	NCURSES_SIZE_T	_lines_avail;	/* lines available for stdscr	    */
1048 	NCURSES_SIZE_T	_topstolen;	/* lines stolen from top	    */
1049 
1050 	WINDOW		*_curscr;	/* current screen		    */
1051 	WINDOW		*_newscr;	/* virtual screen to be updated to  */
1052 	WINDOW		*_stdscr;	/* screen's full-window context	    */
1053 
1054 #define CurScreen(sp)  (sp)->_curscr
1055 #define NewScreen(sp)  (sp)->_newscr
1056 #define StdScreen(sp)  (sp)->_stdscr
1057 
1058 	TRIES		*_keytry;	/* "Try" for use with keypad mode   */
1059 	TRIES		*_key_ok;	/* Disabled keys via keyok(,FALSE)  */
1060 	bool		_tried;		/* keypad mode was initialized	    */
1061 	bool		_keypad_on;	/* keypad mode is currently on	    */
1062 
1063 	bool		_called_wgetch;	/* check for recursion in wgetch()  */
1064 	int		_fifo[FIFO_SIZE];	/* input push-back buffer   */
1065 	short		_fifohead,	/* head of fifo queue		    */
1066 			_fifotail,	/* tail of fifo queue		    */
1067 			_fifopeek,	/* where to peek for next char	    */
1068 			_fifohold;	/* set if breakout marked	    */
1069 
1070 	int		_endwin;	/* are we out of window mode?	    */
1071 	NCURSES_CH_T	*_current_attr; /* holds current attributes set	    */
1072 	int		_coloron;	/* is color enabled?		    */
1073 	int		_color_defs;	/* are colors modified		    */
1074 	int		_cursor;	/* visibility of the cursor	    */
1075 	int		_cursrow;	/* physical cursor row		    */
1076 	int		_curscol;	/* physical cursor column	    */
1077 	bool		_notty;		/* true if we cannot switch non-tty */
1078 	TTY_FLAGS	_tty_flags;
1079 	int		_use_meta;	/* use the meta key?		    */
1080 	struct _SLK	*_slk;		/* ptr to soft key struct / NULL    */
1081 	int		slk_format;	/* selected format for this screen  */
1082 	/* cursor movement costs; units are 10ths of milliseconds */
1083 	int		_char_padding;	/* cost of character put	    */
1084 	int		_cr_cost;	/* cost of (carriage_return)	    */
1085 	int		_cup_cost;	/* cost of (cursor_address)	    */
1086 	int		_home_cost;	/* cost of (cursor_home)	    */
1087 	int		_ll_cost;	/* cost of (cursor_to_ll)	    */
1088 	int		_cub1_cost;	/* cost of (cursor_left)	    */
1089 	int		_cuf1_cost;	/* cost of (cursor_right)	    */
1090 	int		_cud1_cost;	/* cost of (cursor_down)	    */
1091 	int		_cuu1_cost;	/* cost of (cursor_up)		    */
1092 	int		_cub_cost;	/* cost of (parm_cursor_left)	    */
1093 	int		_cuf_cost;	/* cost of (parm_cursor_right)	    */
1094 	int		_cud_cost;	/* cost of (parm_cursor_down)	    */
1095 	int		_cuu_cost;	/* cost of (parm_cursor_up)	    */
1096 	int		_hpa_cost;	/* cost of (column_address)	    */
1097 	int		_vpa_cost;	/* cost of (row_address)	    */
1098 	/* used in tty_update.c, must be chars */
1099 	int		_ed_cost;	/* cost of (clr_eos)		    */
1100 	int		_el_cost;	/* cost of (clr_eol)		    */
1101 	int		_el1_cost;	/* cost of (clr_bol)		    */
1102 	int		_dch1_cost;	/* cost of (delete_character)	    */
1103 	int		_ich1_cost;	/* cost of (insert_character)	    */
1104 	int		_dch_cost;	/* cost of (parm_dch)		    */
1105 	int		_ich_cost;	/* cost of (parm_ich)		    */
1106 	int		_ech_cost;	/* cost of (erase_chars)	    */
1107 	int		_rep_cost;	/* cost of (repeat_char)	    */
1108 	int		_hpa_ch_cost;	/* cost of (column_address)	    */
1109 	int		_cup_ch_cost;	/* cost of (cursor_address)	    */
1110 	int		_cuf_ch_cost;	/* cost of (parm_cursor_right)	    */
1111 	int		_inline_cost;	/* cost of inline-move		    */
1112 	int		_smir_cost;	/* cost of (enter_insert_mode)	    */
1113 	int		_rmir_cost;	/* cost of (exit_insert_mode)	    */
1114 	int		_ip_cost;	/* cost of (insert_padding)	    */
1115 	/* used in lib_mvcur.c */
1116 	char *		_address_cursor;
1117 	/* used in tty_update.c */
1118 	int		_scrolling;	/* 1 if terminal's smart enough to  */
1119 
1120 	/* used in lib_color.c */
1121 	rgb_bits_t	_direct_color;	/* RGB overrides color-table	     */
1122 	color_t		*_color_table;	/* screen's color palette	     */
1123 	int		_color_count;	/* count of colors in palette	     */
1124 	colorpair_t	*_color_pairs;	/* screen's color pair list	     */
1125 	int		_pair_count;	/* same as COLOR_PAIRS               */
1126 	int		_pair_limit;	/* actual limit of color-pairs       */
1127 	int		_pair_alloc;	/* current table-size of color-pairs */
1128 	chtype		_ok_attributes; /* valid attributes for terminal     */
1129 	chtype		_xmc_suppress;	/* attributes to suppress if xmc     */
1130 	chtype		_xmc_triggers;	/* attributes to process if xmc	     */
1131 	chtype *	_acs_map;	/* the real alternate-charset map    */
1132 	bool *		_screen_acs_map;
1133 
1134 
1135 	/* used in lib_vidattr.c */
1136 	bool		_use_rmso;	/* true if we may use 'rmso'	     */
1137 	bool		_use_rmul;	/* true if we may use 'rmul'	     */
1138 
1139 	/*
1140 	 * These data correspond to the state of the idcok() and idlok()
1141 	 * functions.  A caveat is in order here:  the XSI and SVr4
1142 	 * documentation specify that these functions apply to the window which
1143 	 * is given as an argument.  However, ncurses implements this logic
1144 	 * only for the newscr/curscr update process, _not_ per-window.
1145 	 */
1146 	bool		_nc_sp_idlok;
1147 	bool		_nc_sp_idcok;
1148 
1149 	/*
1150 	 * These are the data that support the mouse interface.
1151 	 */
1152 	bool		_mouse_initialized;
1153 	MouseType	_mouse_type;
1154 	int		_maxclick;
1155 	bool		(*_mouse_event) (SCREEN *);
1156 	bool		(*_mouse_inline)(SCREEN *);
1157 	bool		(*_mouse_parse) (SCREEN *, int);
1158 	void		(*_mouse_resume)(SCREEN *);
1159 	void		(*_mouse_wrap)	(SCREEN *);
1160 	int		_mouse_fd;	/* file-descriptor, if any */
1161 	bool		_mouse_active;	/* true if initialized */
1162 	mmask_t		_mouse_mask;	/* set via mousemask() */
1163 	mmask_t		_mouse_mask2;	/* OR's in press/release bits */
1164 	mmask_t		_mouse_bstate;
1165 	MouseFormat	_mouse_format;	/* type of xterm mouse protocol */
1166 	NCURSES_CONST char *_mouse_xtermcap; /* string to enable/disable mouse */
1167 	MEVENT		_mouse_events[EV_MAX];	/* hold the last mouse event seen */
1168 	MEVENT		*_mouse_readp;	/* read pointer into event queue */
1169 	MEVENT		*_mouse_writep;	/* write pointer into event queue */
1170 
1171 	/*
1172 	 * These are data that support the proper handling of the panel stack on an
1173 	 * per screen basis.
1174 	 */
1175 	struct panelhook _panelHook;
1176 
1177 	bool		_sig_winch;
1178 	SCREEN		*_next_screen;
1179 
1180 	/* hashes for old and new lines */
1181 	unsigned long	*oldhash, *newhash;
1182 	HASHMAP		*hashtab;
1183 	int		hashtab_len;
1184 	int		*_oldnum_list;
1185 	int		_oldnum_size;
1186 
1187 	NCURSES_SP_OUTC	_outch;		/* output handler if not putc */
1188 	NCURSES_OUTC	jump;
1189 
1190 	ripoff_t	rippedoff[N_RIPS];
1191 	ripoff_t	*rsp;
1192 
1193 	int		_legacy_coding;	/* see use_legacy_coding() */
1194 
1195 #if NCURSES_NO_PADDING
1196 	bool		_no_padding;	/* flag to set if padding disabled  */
1197 #endif
1198 
1199 #if USE_HARD_TABS
1200 	int		_ht_cost;	/* cost of (tab)		    */
1201 	int		_cbt_cost;	/* cost of (backtab)		    */
1202 #endif /* USE_HARD_TABS */
1203 
1204 	/* used in lib_vidattr.c */
1205 #if USE_ITALIC
1206 	bool		_use_ritm;	/* true if we may use 'ritm'	     */
1207 #endif
1208 
1209 	/* used in getch/twait */
1210 #if USE_KLIBC_KBD
1211 	bool		_extended_key;	/* true if an extended key	     */
1212 #endif
1213 
1214 	/* used in lib_color.c */
1215 #if NCURSES_EXT_FUNCS
1216 	bool		_assumed_color; /* use assumed colors		     */
1217 	bool		_default_color; /* use default colors		     */
1218 	bool		_has_sgr_39_49; /* has ECMA default color support    */
1219 	int		_default_fg;	/* assumed default foreground	     */
1220 	int		_default_bg;	/* assumed default background	     */
1221 	int		_default_pairs;	/* count pairs using default color   */
1222 #endif
1223 
1224 	/* system-dependent mouse data */
1225 #if USE_GPM_SUPPORT
1226 	bool		_mouse_gpm_loaded;
1227 	bool		_mouse_gpm_found;
1228 #ifdef HAVE_LIBDL
1229 	void		*_dlopen_gpm;
1230 	TYPE_gpm_fd	_mouse_gpm_fd;
1231 	TYPE_Gpm_Open	_mouse_Gpm_Open;
1232 	TYPE_Gpm_Close	_mouse_Gpm_Close;
1233 	TYPE_Gpm_GetEvent _mouse_Gpm_GetEvent;
1234 #endif
1235 	Gpm_Connect	_mouse_gpm_connect;
1236 #endif /* USE_GPM_SUPPORT */
1237 
1238 #if USE_EMX_MOUSE
1239 	int		_emxmouse_wfd;
1240 	int		_emxmouse_thread;
1241 	int		_emxmouse_activated;
1242 	char		_emxmouse_buttons[4];
1243 #endif
1244 
1245 #if USE_SYSMOUSE
1246 	MEVENT		_sysmouse_fifo[FIFO_SIZE];
1247 	int		_sysmouse_head;
1248 	int		_sysmouse_tail;
1249 	int		_sysmouse_char_width;	/* character width */
1250 	int		_sysmouse_char_height;	/* character height */
1251 	int		_sysmouse_old_buttons;
1252 	int		_sysmouse_new_buttons;
1253 #endif
1254 
1255 #if USE_TERM_DRIVER || USE_NAMED_PIPES
1256 	MEVENT		_drv_mouse_fifo[FIFO_SIZE];
1257 	int		_drv_mouse_head;
1258 	int		_drv_mouse_tail;
1259 	int		_drv_mouse_old_buttons;
1260 	int		_drv_mouse_new_buttons;
1261 #endif
1262 	/*
1263 	 * This supports automatic resizing
1264 	 */
1265 #if USE_SIZECHANGE
1266 	int		(*_resize)(NCURSES_SP_DCLx int y, int x);
1267 	int		(*_ungetch)(SCREEN *, int);
1268 #endif
1269 
1270 #ifdef USE_SP_WINDOWLIST
1271 	WINDOWLIST*	_windowlist;
1272 #define WindowList(sp)  (sp)->_windowlist
1273 #endif
1274 
1275 #if USE_REENTRANT
1276 	char		_ttytype[NAMESIZE];
1277 	int		_ESCDELAY;
1278 	int		_TABSIZE;
1279 	int		_LINES;
1280 	int		_COLS;
1281 #endif
1282 
1283 #if NCURSES_SP_FUNCS
1284 	bool		use_tioctl;
1285 #endif
1286 
1287 	/*
1288 	 * ncurses/ncursesw are the same up to this point.
1289 	 */
1290 #if USE_WIDEC_SUPPORT
1291 	/* recent versions of 'screen' have partially-working support for
1292 	 * UTF-8, but do not permit ACS at the same time (see tty_update.c).
1293 	 */
1294 	bool		_screen_acs_fix;
1295 	bool		_screen_unicode;
1296 #endif
1297 
1298 #if NCURSES_EXT_FUNCS && NCURSES_EXT_COLORS
1299 	void		*_ordered_pairs; /* index used by alloc_pair()	     */
1300 	int		_pairs_used;	/* actual number of color-pairs used */
1301 	int		_recent_pair;	/* number for most recent free-pair  */
1302 #endif
1303 
1304 #ifdef TRACE
1305 	char		tracechr_buf[TRACECHR_BUF];
1306 	char		tracemse_buf[TRACEMSE_MAX];
1307 #if USE_REENTRANT
1308 	long		_outchars;
1309 	const char	*_tputs_trace;
1310 #endif
1311 #endif
1312 #undef SCREEN
1313 } SCREEN;
1314 
1315 extern NCURSES_EXPORT_VAR(SCREEN *) _nc_screen_chain;
1316 extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
1317 
1318 	WINDOWLIST {
1319 	WINDOWLIST *next;
1320 	SCREEN *screen;		/* screen containing the window */
1321 	WINDOW	win;		/* WINDOW_EXT() needs to account for offset */
1322 #if NCURSES_WIDECHAR
1323 	char addch_work[(MB_LEN_MAX * 9) + 1];
1324 	unsigned addch_used;	/* number of bytes in addch_work[] */
1325 	int addch_x;		/* x-position for addch_work[] */
1326 	int addch_y;		/* y-position for addch_work[] */
1327 #endif
1328 };
1329 
1330 #define WINDOW_EXT(w,m) (((WINDOWLIST *)((void *)((char *)(w) - offsetof(WINDOWLIST, win))))->m)
1331 
1332 #ifdef USE_SP_WINDOWLIST
1333 #define SP_INIT_WINDOWLIST(sp)	WindowList(sp) = NULL
1334 #else
1335 #define SP_INIT_WINDOWLIST(sp)	/* nothing */
1336 #endif
1337 
1338 #define SP_PRE_INIT(sp)                         \
1339     sp->_cursrow           = -1;                \
1340     sp->_curscol           = -1;                \
1341     IsNl(sp)               = TRUE;              \
1342     IsRaw(sp)              = FALSE;             \
1343     IsCbreak(sp)           = 0;                 \
1344     IsEcho(sp)             = TRUE;              \
1345     sp->_fifohead          = -1;                \
1346     sp->_endwin            = ewSuspend;         \
1347     sp->_cursor            = -1;                \
1348     SP_INIT_WINDOWLIST(sp);                     \
1349     sp->_outch             = NCURSES_OUTC_FUNC; \
1350     sp->jump               = NULL               \
1351 
1352 /* usually in <limits.h> */
1353 #ifndef UCHAR_MAX
1354 #define UCHAR_MAX 255
1355 #endif
1356 
1357 /* The terminfo source is assumed to be 7-bit ASCII */
1358 #define is7bits(c)	((unsigned)(c) < 128)
1359 
1360 /* Checks for isprint() should be done on 8-bit characters (non-wide) */
1361 #define is8bits(c)	((unsigned)(c) <= UCHAR_MAX)
1362 
1363 #ifndef Min
1364 #define Min(a,b)	((a) > (b)  ?  (b)  :  (a))
1365 #endif
1366 
1367 #ifndef Max
1368 #define Max(a,b)	((a) < (b)  ?  (b)  :  (a))
1369 #endif
1370 
1371 /* usually in <unistd.h> */
1372 #ifndef STDIN_FILENO
1373 #define STDIN_FILENO 0
1374 #endif
1375 
1376 #ifndef STDOUT_FILENO
1377 #define STDOUT_FILENO 1
1378 #endif
1379 
1380 #ifndef STDERR_FILENO
1381 #define STDERR_FILENO 2
1382 #endif
1383 
1384 #ifndef EXIT_SUCCESS
1385 #define EXIT_SUCCESS 0
1386 #endif
1387 
1388 #ifndef EXIT_FAILURE
1389 #define EXIT_FAILURE 1
1390 #endif
1391 
1392 #ifndef R_OK
1393 #define	R_OK	4		/* Test for read permission.  */
1394 #endif
1395 #ifndef W_OK
1396 #define	W_OK	2		/* Test for write permission.  */
1397 #endif
1398 #ifndef X_OK
1399 #define	X_OK	1		/* Test for execute permission.  */
1400 #endif
1401 #ifndef F_OK
1402 #define	F_OK	0		/* Test for existence.  */
1403 #endif
1404 
1405 #if HAVE_FCNTL_H
1406 #include <fcntl.h>		/* may define O_BINARY	*/
1407 #endif
1408 
1409 #ifndef O_BINARY
1410 #define O_BINARY 0
1411 #endif
1412 
1413 #ifdef TRACE
1414 #if USE_REENTRANT
1415 #define COUNT_OUTCHARS(n) _nc_count_outchars(n);
1416 #else
1417 #define COUNT_OUTCHARS(n) _nc_outchars += (n);
1418 #endif
1419 #else
1420 #define COUNT_OUTCHARS(n) /* nothing */
1421 #endif
1422 
1423 #define RESET_OUTCHARS() COUNT_OUTCHARS(-_nc_outchars)
1424 
1425 #define UChar(c)	((unsigned char)(c))
1426 #define UShort(c)	((unsigned short)(c))
1427 #define ChCharOf(c)	((chtype)(c) & (chtype)A_CHARTEXT)
1428 #define ChAttrOf(c)	((chtype)(c) & (chtype)A_ATTRIBUTES)
1429 
1430 #define TR_PUTC(c)	TR(TRACE_CHARPUT, ("PUTC %#x", UChar(c)))
1431 
1432 #if USE_WIDEC_SUPPORT /* { */
1433 /* true if the status/errno indicate an illegal multibyte sequence */
1434 #define isEILSEQ(status) (((size_t)status == (size_t)-1) && (errno == EILSEQ))
1435 
1436 #define init_mb(state)	memset(&(state), 0, sizeof(state))
1437 
1438 #if NCURSES_EXT_COLORS
1439 #define NulColor	, 0
1440 #else
1441 #define NulColor	/* nothing */
1442 #endif
1443 
1444 #if NCURSES_RGB_COLORS
1445 #define NulDirect	, 0
1446 #else
1447 #define NulDirect	/* nothing */
1448 #endif
1449 
1450 #if CCHARW_MAX > 6
1451 #define NulChar		0,0,0,0,0
1452 #elif CCHARW_MAX > 5
1453 #define NulChar		0,0,0,0		/* ncurses7 */
1454 #elif CCHARW_MAX > 4
1455 #define NulChar		0,0,0		/* ncurses6 */
1456 #elif CCHARW_MAX > 3
1457 #define NulChar		0,0
1458 #elif CCHARW_MAX > 2
1459 #define NulChar		0
1460 #else
1461 #define NulChar		/* nothing */
1462 #endif
1463 
1464 #define CharOf(c)	((c).chars[0])
1465 #define AttrOf(c)	((c).attr)
1466 
1467 #define AddAttr(c,a)	AttrOf(c) |=  ((a) & A_ATTRIBUTES)
1468 #define RemAttr(c,a)	AttrOf(c) &= ~((a) & A_ATTRIBUTES)
1469 #define SetAttr(c,a)	AttrOf(c) =   ((a) & A_ATTRIBUTES) | WidecExt(c)
1470 
1471 #define NewChar2(c,a)	{ a, { c, NulChar } NulColor NulDirect }
1472 #define NewChar(ch)	NewChar2(ChCharOf(ch), ChAttrOf(ch))
1473 
1474 #if CCHARW_MAX > 1
1475 #define CharEq(a,b)	(((a).attr == (b).attr) \
1476 			 && !memcmp((a).chars, \
1477 				    (b).chars, \
1478 				    CCHARW_MAX * sizeof((b).chars[0])) \
1479 			if_EXT_COLORS(&& (a).ext_color == (b).ext_color))
1480 #else
1481 #define CharEq(a,b)	(!memcmp(&(a), &(b), sizeof(a)))
1482 #endif
1483 
1484 #define SetChar(ch,c,a) do {							    \
1485 			    NCURSES_CH_T *_cp = &(ch);				    \
1486 			    memset(_cp, 0, sizeof(ch));				    \
1487 			    _cp->chars[0] = (wchar_t) (c);			    \
1488 			    _cp->attr = (a);					    \
1489 			    if_EXT_COLORS(SetPair(ch, PairNumber(a)));		    \
1490 			} while (0)
1491 #define CHREF(wch)	(&(wch))
1492 #define CHDEREF(wch)	(*(wch))
1493 #define ARG_CH_T	NCURSES_CH_T *
1494 #define CARG_CH_T	const NCURSES_CH_T *
1495 #define PUTC_DATA	char PUTC_buf[MB_LEN_MAX]; int PUTC_i, PUTC_n; \
1496 			mbstate_t PUT_st; wchar_t PUTC_ch
1497 #define PUTC_INIT	init_mb (PUT_st)
1498 #define PUTC(ch)	do { if(!isWidecExt(ch)) {				    \
1499 			if (Charable(ch)) {					    \
1500 			    TR_PUTC(CharOf(ch));				    \
1501 			    NCURSES_OUTC_FUNC (NCURSES_SP_ARGx (int) CharOf(ch));	    \
1502 			    COUNT_OUTCHARS(1);					    \
1503 			} else {						    \
1504 			    for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {	    \
1505 				PUTC_ch = (ch).chars[PUTC_i];			    \
1506 				if (PUTC_ch == L'\0')				    \
1507 				    break;					    \
1508 				PUTC_INIT;					    \
1509 				PUTC_n = (int) wcrtomb(PUTC_buf,		    \
1510 						       (ch).chars[PUTC_i], &PUT_st); \
1511 				if (PUTC_n <= 0) {				    \
1512 				    if (PUTC_ch && is8bits(PUTC_ch) && PUTC_i == 0) { \
1513 					TR_PUTC(CharOf(ch));			    \
1514 					NCURSES_OUTC_FUNC (NCURSES_SP_ARGx (int) CharOf(ch)); \
1515 				    }						    \
1516 				    break;					    \
1517 				} else if (PUTC_n > 1 || !is8bits(PUTC_ch)) {	    \
1518 				    int PUTC_j;					    \
1519 				    for (PUTC_j = 0; PUTC_j < PUTC_n; ++PUTC_j) {   \
1520 					TR_PUTC(PUTC_buf[PUTC_j]);		    \
1521 					NCURSES_OUTC_FUNC (NCURSES_SP_ARGx PUTC_buf[PUTC_j]); \
1522 				    }						    \
1523 				} else {					    \
1524 				    PUTC_buf[0] = (char) PUTC_ch;			    \
1525 				    TR_PUTC(PUTC_buf[0]);			    \
1526 				    NCURSES_OUTC_FUNC (NCURSES_SP_ARGx PUTC_buf[0]); \
1527 				}						    \
1528 			    }							    \
1529 			    COUNT_OUTCHARS(PUTC_i);				    \
1530 			} } } while (0)
1531 
1532 #define BLANK		NewChar2(' ', WA_NORMAL)
1533 #define ZEROS		NewChar2('\0', WA_NORMAL)
1534 #define ISBLANK(ch)	((ch).chars[0] == L' ' && (ch).chars[1] == L'\0')
1535 
1536 	/*
1537 	 * Wide characters cannot be represented in the A_CHARTEXT mask of
1538 	 * attr_t's but an application might have set a narrow character there.
1539 	 * But even in that case, it would only be a printable character, or
1540 	 * zero.  Otherwise we can use those bits to tell if a cell is the
1541 	 * first or extension part of a wide character.
1542 	 */
1543 #define WidecExt(ch)	(int) (AttrOf(ch) & A_CHARTEXT)
1544 #define isWidecBase(ch)	(WidecExt(ch) == 1)
1545 #define isWidecExt(ch)	(WidecExt(ch) > 1 && WidecExt(ch) < 32)
1546 #define SetWidecExt(dst, ext)	AttrOf(dst) &= ~A_CHARTEXT,		\
1547 				AttrOf(dst) |= (attr_t) (ext + 1)
1548 
1549 #define if_WIDEC(code)  code
1550 #define Charable(ch)	(((SP_PARM->_legacy_coding)			\
1551 			 || (AttrOf(ch) & A_ALTCHARSET)			\
1552 			 || (!isWidecExt(ch))) &&			\
1553 			     (ch).chars[1] == L'\0' &&			\
1554 			     _nc_is_charable(CharOf(ch)))
1555 
1556 #define L(ch)		L ## ch
1557 #else /* }{ */
1558 #define CharOf(c)	ChCharOf(c)
1559 #define AttrOf(c)	ChAttrOf(c)
1560 #define AddAttr(c,a)	c |= (a)
1561 #define RemAttr(c,a)	c &= ~((a) & A_ATTRIBUTES)
1562 #define SetAttr(c,a)	c = ((c) & ~A_ATTRIBUTES) | (a)
1563 #define NewChar(ch)	(ch)
1564 #define NewChar2(c,a)	((c) | (a))
1565 #define CharEq(a,b)	((a) == (b))
1566 #define SetChar(ch,c,a)	ch = (c) | (a)
1567 #define CHREF(wch)	wch
1568 #define CHDEREF(wch)	wch
1569 #define ARG_CH_T	NCURSES_CH_T
1570 #define CARG_CH_T	NCURSES_CH_T
1571 #define PUTC_DATA	/* nothing */
1572 #define PUTC(ch)	{ \
1573 			    TR_PUTC(ch); \
1574 			    NCURSES_OUTC_FUNC (NCURSES_SP_ARGx (int) ch); \
1575 			}
1576 
1577 #define BLANK		(' '|A_NORMAL)
1578 #define ZEROS		('\0'|A_NORMAL)
1579 #define ISBLANK(ch)	(CharOf(ch) == ' ')
1580 
1581 #define isWidecExt(ch)	(0)
1582 #define if_WIDEC(code) /* nothing */
1583 
1584 #define Charable(ch)	(CharOf(ch) >= ' ' && CharOf(ch) <= '~')
1585 #define L(ch)		ch
1586 #endif /* } */
1587 
1588 #define AttrOfD(ch)	AttrOf(CHDEREF(ch))
1589 #define CharOfD(ch)	CharOf(CHDEREF(ch))
1590 #define SetChar2(wch,ch)    SetChar(wch,ChCharOf(ch),ChAttrOf(ch))
1591 
1592 #define BLANK_ATTR	A_NORMAL
1593 #define BLANK_TEXT	L(' ')
1594 
1595 #define CHANGED     -1
1596 
1597 #define LEGALYX(w, y, x) \
1598 	      ((w) != NULL && \
1599 		((x) >= 0 && (x) <= (w)->_maxx && \
1600 		 (y) >= 0 && (y) <= (w)->_maxy))
1601 
1602 #define OK_DIMENSION(n)	((NCURSES_SIZE_T)(n) == (n) && (n) > 0)
1603 
1604 #define CHANGED_CELL(line,col) \
1605 	if (line->firstchar == _NOCHANGE) \
1606 		line->firstchar = line->lastchar = (NCURSES_SIZE_T) (col); \
1607 	else if ((col) < line->firstchar) \
1608 		line->firstchar = (NCURSES_SIZE_T) (col); \
1609 	else if ((col) > line->lastchar) \
1610 		line->lastchar = (NCURSES_SIZE_T) (col)
1611 
1612 #define CHANGED_RANGE(line,start,end) \
1613 	if (line->firstchar == _NOCHANGE \
1614 	 || line->firstchar > (start)) \
1615 		line->firstchar = (NCURSES_SIZE_T) (start); \
1616 	if (line->lastchar == _NOCHANGE \
1617 	 || line->lastchar < (end)) \
1618 		line->lastchar = (NCURSES_SIZE_T) (end)
1619 
1620 #define CHANGED_TO_EOL(line,start,end) \
1621 	if (line->firstchar == _NOCHANGE \
1622 	 || line->firstchar > (start)) \
1623 		line->firstchar = (NCURSES_SIZE_T) (start); \
1624 	line->lastchar = (NCURSES_SIZE_T) (end)
1625 
1626 #define SIZEOF(v) (sizeof(v)/sizeof(v[0]))
1627 
1628 #include <nc_alloc.h>
1629 #include <nc_access.h>
1630 
1631 #define FreeIfNeeded(p)  if ((p) != NULL) free(p)
1632 
1633 /* FreeAndNull() is not a comma-separated expression because some compilers
1634  * do not accept a mixture of void with values.
1635  */
1636 #define FreeAndNull(p)   do { free(p); p = NULL; } while (0)
1637 
1638 #ifdef EXP_OOM_TESTING
1639 extern NCURSES_EXPORT(void *)	_nc_oom_malloc(size_t size);
1640 extern NCURSES_EXPORT(void *)	_nc_oom_calloc(size_t nmemb, size_t size);
1641 extern NCURSES_EXPORT(void *)	_nc_oom_realloc(void *ptr, size_t size);
1642 extern NCURSES_EXPORT(void)	_nc_oom_free(void *ptr);
1643 extern NCURSES_EXPORT(char *)	_nc_oom_strdup(const char *ptr);
1644 #define malloc(size)		_nc_oom_malloc(size)
1645 #define calloc(nmemb, size)	_nc_oom_calloc(nmemb, size)
1646 #define realloc(ptr, size)	_nc_oom_realloc(ptr, size)
1647 #define free(ptr)		_nc_oom_free(ptr)
1648 #define strdup(ptr)		_nc_oom_strdup(ptr)
1649 #endif
1650 
1651 /*
1652  * Use these for tic/infocmp malloc failures.  Generally the ncurses library
1653  * tries to limp along after a failure.
1654  */
1655 #define TYPE_MALLOC(type, size, name) \
1656 	do { \
1657 	    name = typeMalloc(type, size); \
1658 	    if (name == NULL) \
1659 		_nc_err_abort(MSG_NO_MEMORY); \
1660 	} while (0)
1661 
1662 #define TYPE_CALLOC(type, size, name) \
1663 	do { \
1664 	    name = typeCalloc(type, size); \
1665 	    if (name == NULL) \
1666 		_nc_err_abort(MSG_NO_MEMORY); \
1667 	} while (0)
1668 
1669 #define TYPE_REALLOC(type, size, name) \
1670 	do { \
1671 	    name = typeRealloc(type, size, name); \
1672 	    if (name == NULL) \
1673 		_nc_err_abort(MSG_NO_MEMORY); \
1674 	} while (0)
1675 
1676 /*
1677  * TTY bit definition for converting tabs to spaces.
1678  */
1679 #ifdef TAB3
1680 # define OFLAGS_TABS TAB3	/* POSIX specifies TAB3 */
1681 #else
1682 # ifdef XTABS
1683 #  define OFLAGS_TABS XTABS	/* XTABS is usually the "same" */
1684 # else
1685 #  ifdef OXTABS
1686 #   define OFLAGS_TABS OXTABS	/* the traditional BSD equivalent */
1687 #  else
1688 #   define OFLAGS_TABS 0
1689 #  endif
1690 # endif
1691 #endif
1692 
1693 #ifdef __TANDEM
1694 #define ROOT_UID 65535
1695 #endif
1696 
1697 #ifndef ROOT_UID
1698 #define ROOT_UID 0
1699 #endif
1700 
1701 /*
1702  * Standardize/simplify common loops
1703  */
1704 #define each_screen(p) p = _nc_screen_chain; p != NULL; p = (p)->_next_screen
1705 #define each_window(sp,p) p = WindowList(sp); p != NULL; p = (p)->next
1706 #define each_ripoff(p) p = safe_ripoff_stack; (p - safe_ripoff_stack) < N_RIPS; ++p
1707 
1708 /*
1709  * Prefixes for call/return points of library function traces.  We use these to
1710  * instrument the public functions so that the traces can be easily transformed
1711  * into regression scripts.
1712  */
1713 #define T_CALLED(fmt) "called {" fmt
1714 #define T_CREATE(fmt) "create :" fmt
1715 #define T_RETURN(fmt) "return }" fmt
1716 
1717 #define NonNull(s)              ((s) != NULL ? s : "<null>")
1718 #define NonEmpty(s)             ((s) != NULL && *(s) != '\0')
1719 
1720 #ifdef TRACE
1721 
1722 #if USE_REENTRANT
1723 #define TPUTS_TRACE(s)	_nc_set_tputs_trace(s);
1724 #else
1725 #define TPUTS_TRACE(s)	_nc_tputs_trace = s;
1726 #endif
1727 
1728 #ifdef HAVE_CONSISTENT_GETENV
1729 #define START_TRACE() \
1730 	if ((_nc_tracing & TRACE_MAXIMUM) == 0) { \
1731 	    int t = _nc_getenv_num("NCURSES_TRACE"); \
1732 	    if (t >= 0) \
1733 		curses_trace((unsigned) t); \
1734 	}
1735 #else
1736 #define START_TRACE() /* nothing */
1737 #endif
1738 
1739 /*
1740  * Many of the _tracef() calls use static buffers; lock the trace state before
1741  * trying to fill them.
1742  */
1743 #if USE_REENTRANT
1744 #define USE_TRACEF(mask) _nc_use_tracef(mask)
1745 extern NCURSES_EXPORT(int)	_nc_use_tracef (unsigned);
1746 extern NCURSES_EXPORT(void)	_nc_locked_tracef (const char *, ...) GCC_PRINTFLIKE(1,2);
1747 #else
1748 #define USE_TRACEF(mask) (_nc_tracing & (mask))
1749 #define _nc_locked_tracef _tracef
1750 #endif
1751 
1752 #define TR(n, a)	if (USE_TRACEF(n)) _nc_locked_tracef a
1753 #define T(a)		TR(TRACE_CALLS, a)
1754 #define TRACE_RETURN(value,type)     return _nc_retrace_##type((type)(value))
1755 #define TRACE_RETURN1(value,dst)     return _nc_retrace_##dst(value)
1756 #define TRACE_RETURN2(value,dst,src) return _nc_retrace_##dst##_##src(value)
1757 #define TRACE_RETURN_SP(value,type)  return _nc_retrace_##type(SP_PARM, value)
1758 
1759 typedef void VoidFunc(void);
1760 
1761 #define TR_FUNC_LEN		((sizeof(void *) + sizeof(void (*)(void))) * 2 + 4)
1762 #define TR_FUNC_BFR(max)	char tr_func_data[max][TR_FUNC_LEN]
1763 #define TR_FUNC_ARG(num,func)	_nc_fmt_funcptr(&tr_func_data[num][0], (const char *)&(func), sizeof((func)))
1764 
1765 #define returnAttr(code)	TRACE_RETURN(code,attr_t)
1766 #define returnBits(code)	TRACE_RETURN(code,unsigned)
1767 #define returnBool(code)	TRACE_RETURN(code,bool)
1768 #define returnCPtr(code)	TRACE_RETURN1(code,cptr)
1769 #define returnCVoidPtr(code)	TRACE_RETURN1(code,cvoid_ptr)
1770 #define returnChar(code)	TRACE_RETURN(code,char)
1771 #define returnChtype(code)	TRACE_RETURN(code,chtype)
1772 #define returnCode(code)	TRACE_RETURN(code,int)
1773 #define returnIntAttr(code)	TRACE_RETURN2(code,int,attr_t)
1774 #define returnMMask(code)	TRACE_RETURN_SP(code,mmask_t)
1775 #define returnPtr(code)		TRACE_RETURN1(code,ptr)
1776 #define returnSP(code)		TRACE_RETURN1(code,sp)
1777 #define returnVoid		{ T((T_RETURN(""))); return; }
1778 #define returnVoidPtr(code)	TRACE_RETURN1(code,void_ptr)
1779 #define returnWin(code)		TRACE_RETURN1(code,win)
1780 
1781 #define returnDB(rc)		do { TR(TRACE_DATABASE,(T_RETURN("code %d"), (rc))); return (rc); } while (0)
1782 #define returnPtrDB(rc)		do { TR(TRACE_DATABASE,(T_RETURN("%p"), (rc))); return (rc); } while (0)
1783 #define returnVoidDB		do { TR(TRACE_DATABASE,(T_RETURN(""))); return; } while (0)
1784 
1785 extern NCURSES_EXPORT(bool)             _nc_retrace_bool (bool);
1786 extern NCURSES_EXPORT(NCURSES_CONST void *) _nc_retrace_cvoid_ptr (NCURSES_CONST void *);
1787 extern NCURSES_EXPORT(SCREEN *)         _nc_retrace_sp (SCREEN *);
1788 extern NCURSES_EXPORT(WINDOW *)         _nc_retrace_win (WINDOW *);
1789 extern NCURSES_EXPORT(attr_t)           _nc_retrace_attr_t (attr_t);
1790 extern NCURSES_EXPORT(char *)           _nc_retrace_ptr (char *);
1791 extern NCURSES_EXPORT(char *)           _nc_trace_ttymode(const TTY *tty);
1792 extern NCURSES_EXPORT(char *)           _nc_varargs (const char *, va_list);
1793 extern NCURSES_EXPORT(chtype)           _nc_retrace_chtype (chtype);
1794 extern NCURSES_EXPORT(const char *)     _nc_altcharset_name(attr_t, chtype);
1795 extern NCURSES_EXPORT(const char *)     _nc_retrace_cptr (const char *);
1796 extern NCURSES_EXPORT(char)             _nc_retrace_char (int);
1797 extern NCURSES_EXPORT(int)              _nc_retrace_int (int);
1798 extern NCURSES_EXPORT(int)              _nc_retrace_int_attr_t (attr_t);
1799 extern NCURSES_EXPORT(mmask_t)          _nc_retrace_mmask_t (SCREEN *, mmask_t);
1800 extern NCURSES_EXPORT(unsigned)         _nc_retrace_unsigned (unsigned);
1801 extern NCURSES_EXPORT(void *)           _nc_retrace_void_ptr (void *);
1802 extern NCURSES_EXPORT(void)             _nc_fifo_dump (SCREEN *);
1803 
1804 extern NCURSES_EXPORT(char *)           _nc_fmt_funcptr(char *, const char *, size_t);
1805 
1806 #if USE_REENTRANT
1807 NCURSES_WRAPPED_VAR(long, _nc_outchars);
1808 NCURSES_WRAPPED_VAR(const char *, _nc_tputs_trace);
1809 #define _nc_outchars       NCURSES_PUBLIC_VAR(_nc_outchars())
1810 #define _nc_tputs_trace    NCURSES_PUBLIC_VAR(_nc_tputs_trace())
1811 extern NCURSES_EXPORT(void)		_nc_set_tputs_trace (const char *);
1812 extern NCURSES_EXPORT(void)		_nc_count_outchars (long);
1813 #else
1814 extern NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace;
1815 extern NCURSES_EXPORT_VAR(long)         _nc_outchars;
1816 #endif
1817 
1818 extern NCURSES_EXPORT_VAR(unsigned)     _nc_tracing;
1819 
1820 extern NCURSES_EXPORT(char *) _nc_tracebits (void);
1821 extern NCURSES_EXPORT(char *) _tracemouse (const MEVENT *);
1822 extern NCURSES_EXPORT(void) _tracedump (const char *, WINDOW *);
1823 
1824 #if USE_WIDEC_SUPPORT
1825 extern NCURSES_EXPORT(const char *) _nc_viswbuf2 (int, const wchar_t *);
1826 extern NCURSES_EXPORT(const char *) _nc_viswbufn (const wchar_t *, int);
1827 #endif
1828 
1829 extern NCURSES_EXPORT(const char *) _nc_viscbuf2 (int, const NCURSES_CH_T *, int);
1830 extern NCURSES_EXPORT(const char *) _nc_viscbuf (const NCURSES_CH_T *, int);
1831 
1832 #else /* !TRACE */
1833 
1834 #define START_TRACE() /* nothing */
1835 
1836 #define T(a)
1837 #define TR(n, a)
1838 #define TPUTS_TRACE(s)
1839 #define TR_FUNC_BFR(max)
1840 
1841 #define returnAttr(code)	return code
1842 #define returnBits(code)	return code
1843 #define returnBool(code)	return code
1844 #define returnCPtr(code)	return code
1845 #define returnCVoidPtr(code)	return code
1846 #define returnChar(code)	return ((char) code)
1847 #define returnChtype(code)	return code
1848 #define returnCode(code)	return code
1849 #define returnIntAttr(code)	return code
1850 #define returnMMask(code)	return code
1851 #define returnPtr(code)		return code
1852 #define returnSP(code)		return code
1853 #define returnVoid		return
1854 #define returnVoidPtr(code)	return code
1855 #define returnWin(code)		return code
1856 
1857 #define returnDB(code)		return code
1858 #define returnPtrDB(rc)		return rc
1859 #define returnVoidDB		return
1860 
1861 #endif /* TRACE/!TRACE */
1862 
1863 /*
1864  * Workaround for defective implementation of gcc attribute warn_unused_result
1865  */
1866 #if defined(__GNUC__) && defined(_FORTIFY_SOURCE)
1867 #define IGNORE_RC(func) errno = (int) func
1868 #else
1869 #define IGNORE_RC(func) (void) func
1870 #endif /* gcc workarounds */
1871 
1872 /*
1873  * Return-codes for tgetent() and friends.
1874  */
1875 #define TGETENT_YES  1		/* entry is found */
1876 #define TGETENT_NO   0		/* entry is not found */
1877 #define TGETENT_ERR -1		/* an error occurred */
1878 
1879 extern NCURSES_EXPORT(const char *) _nc_visbuf2 (int, const char *);
1880 extern NCURSES_EXPORT(const char *) _nc_visbufn (const char *, int);
1881 
1882 #define EMPTY_MODULE(name) \
1883 extern	NCURSES_EXPORT(void) name (void); \
1884 	NCURSES_EXPORT(void) name (void) { }
1885 
1886 #define ALL_BUT_COLOR ((chtype)~(A_COLOR))
1887 #define NONBLANK_ATTR (A_BOLD | A_DIM | A_BLINK | A_ITALIC)
1888 #define TPARM_ATTR    (A_STANDOUT | A_UNDERLINE | A_REVERSE | A_BLINK | A_DIM | A_BOLD | A_ALTCHARSET | A_INVIS | A_PROTECT)
1889 #define XMC_CONFLICT  (A_STANDOUT | A_UNDERLINE | A_REVERSE | A_BLINK | A_DIM | A_BOLD | A_INVIS | A_PROTECT | A_ITALIC)
1890 #define XMC_CHANGES(c) ((c) & SP_PARM->_xmc_suppress)
1891 
1892 #define toggle_attr_on(S,at) {\
1893    if (PairNumber(at) > 0) {\
1894       (S) = ((S) & ALL_BUT_COLOR) | (attr_t) (at);\
1895    } else {\
1896       (S) |= (attr_t) (at);\
1897    }\
1898    TR(TRACE_ATTRS, ("new attribute is %s", _traceattr((S))));}
1899 
1900 
1901 #define toggle_attr_off(S,at) {\
1902    if (PairNumber(at) > 0) {\
1903       (S) &= ~(at|A_COLOR);\
1904    } else {\
1905       (S) &= ~(at);\
1906    }\
1907    TR(TRACE_ATTRS, ("new attribute is %s", _traceattr((S))));}
1908 
1909 #define DelCharCost(sp,count) \
1910 		((parm_dch != NULL) \
1911 		? sp->_dch_cost \
1912 		: ((delete_character != NULL) \
1913 			? (sp->_dch1_cost * count) \
1914 			: NC_INFINITY))
1915 
1916 #define InsCharCost(sp,count) \
1917 		((parm_ich != NULL) \
1918 		? sp->_ich_cost \
1919 		: ((enter_insert_mode && exit_insert_mode) \
1920 		  ? sp->_smir_cost + sp->_rmir_cost + (sp->_ip_cost * count) \
1921 		  : ((insert_character != NULL) \
1922 		    ? ((sp->_ich1_cost + sp->_ip_cost) * count) \
1923 		    : NC_INFINITY)))
1924 
1925 #if USE_XMC_SUPPORT
1926 #define UpdateAttrs(sp,c) if (!SameAttrOf(SCREEN_ATTRS(sp), c)) { \
1927 				attr_t chg = AttrOf(SCREEN_ATTRS(sp)); \
1928 				VIDPUTS(sp, AttrOf(c), GetPair(c)); \
1929 				if (magic_cookie_glitch > 0 \
1930 				 && XMC_CHANGES((chg ^ AttrOf(SCREEN_ATTRS(sp))))) { \
1931 					T(("%s @%d before glitch %d,%d", \
1932 						__FILE__, __LINE__, \
1933 						sp->_cursrow, \
1934 						sp->_curscol)); \
1935 					NCURSES_SP_NAME(_nc_do_xmc_glitch)(NCURSES_SP_ARGx chg); \
1936 				} \
1937 			}
1938 #else
1939 #define UpdateAttrs(sp,c) if (!SameAttrOf(SCREEN_ATTRS(sp), c)) { \
1940 				    VIDPUTS(sp, AttrOf(c), GetPair(c)); \
1941 			}
1942 #endif
1943 
1944 #if NCURSES_EXPANDED && NCURSES_EXT_FUNCS
1945 
1946 #undef  toggle_attr_on
1947 #define toggle_attr_on(S,at) _nc_toggle_attr_on(&(S), at)
1948 extern NCURSES_EXPORT(void) _nc_toggle_attr_on (attr_t *, attr_t);
1949 
1950 #undef  toggle_attr_off
1951 #define toggle_attr_off(S,at) _nc_toggle_attr_off(&(S), at)
1952 extern NCURSES_EXPORT(void) _nc_toggle_attr_off (attr_t *, attr_t);
1953 
1954 #undef  DelCharCost
1955 #define DelCharCost(sp, count) NCURSES_SP_NAME(_nc_DelCharCost)(NCURSES_SP_ARGx count)
1956 
1957 #undef  InsCharCost
1958 #define InsCharCost(sp, count) NCURSES_SP_NAME(_nc_InsCharCost)(NCURSES_SP_ARGx count)
1959 
1960 extern NCURSES_EXPORT(int) NCURSES_SP_NAME(_nc_DelCharCost) (NCURSES_SP_DCLx int _c);
1961 extern NCURSES_EXPORT(int) NCURSES_SP_NAME(_nc_InsCharCost) (NCURSES_SP_DCLx int _c);
1962 
1963 #undef  UpdateAttrs
1964 #define UpdateAttrs(sp,c) NCURSES_SP_NAME(_nc_UpdateAttrs)(NCURSES_SP_ARGx CHREF(c))
1965 
1966 #if USE_WIDEC_SUPPORT || defined(NEED_NCURSES_CH_T)
1967 extern NCURSES_EXPORT(void) NCURSES_SP_NAME(_nc_UpdateAttrs) (NCURSES_SP_DCLx CARG_CH_T _c);
1968 #else
1969 extern NCURSES_EXPORT(void) NCURSES_SP_NAME(_nc_UpdateAttrs) (NCURSES_SP_DCLx chtype c);
1970 #endif
1971 
1972 #if NCURSES_SP_FUNCS
1973 extern NCURSES_EXPORT(int) _nc_DelCharCost (int);
1974 extern NCURSES_EXPORT(int) _nc_InsCharCost (int);
1975 extern NCURSES_EXPORT(void) _nc_UpdateAttrs (CARG_CH_T);
1976 #endif /* NCURSES_SP_FUNCS */
1977 
1978 #else
1979 
1980 extern NCURSES_EXPORT(void) _nc_expanded (void);
1981 
1982 #endif
1983 
1984 #if !NCURSES_EXT_FUNCS
1985 #define set_escdelay(value) ESCDELAY = value
1986 #endif
1987 
1988 #if !HAVE_GETCWD
1989 #define getcwd(buf,len) getwd(buf)
1990 #endif
1991 
1992 #define save_ttytype(termp) \
1993 	if (TerminalType(termp).term_names != NULL) { \
1994 	    _nc_STRNCPY(ttytype, \
1995 	    		TerminalType(termp).term_names, \
1996 			NAMESIZE - 1); \
1997 	    ttytype[NAMESIZE - 1] = '\0'; \
1998 	}
1999 
2000 #if !NCURSES_WCWIDTH_GRAPHICS
2001 extern NCURSES_EXPORT(int) _nc_wacs_width(wchar_t);
2002 #else
2003 #define _nc_wacs_width(ch) wcwidth(ch)
2004 #endif
2005 
2006 /* charable.c */
2007 #if USE_WIDEC_SUPPORT
2008 extern NCURSES_EXPORT(bool) _nc_is_charable(wchar_t);
2009 extern NCURSES_EXPORT(int) _nc_to_char(wint_t);
2010 extern NCURSES_EXPORT(wint_t) _nc_to_widechar(int);
2011 #endif
2012 
2013 /* comp_captab.c */
2014 typedef struct {
2015 	short	nte_name;	/* offset of name to hash on */
2016 	int	nte_type;	/* BOOLEAN, NUMBER or STRING */
2017 	short	nte_index;	/* index of associated variable in its array */
2018 	short	nte_link;	/* index in table of next hash, or -1 */
2019 } name_table_data;
2020 
2021 typedef struct
2022 {
2023 	short	from;
2024 	short	to;
2025 	short	source;
2026 } alias_table_data;
2027 
2028 /* comp_userdefs.c */
2029 typedef struct {
2030 	short	ute_name;	/* offset of name to hash on */
2031 	unsigned ute_type;	/* mask (BOOLEAN, NUMBER, STRING) */
2032 	unsigned ute_argc;	/* number of parameters */
2033 	unsigned ute_args;	/* bit-mask for string parameters */
2034 	short	ute_index;	/* index of associated variable in its array */
2035 	short	ute_link;	/* index in table of next hash, or -1 */
2036 } user_table_data;
2037 
2038 /* doupdate.c */
2039 #if USE_XMC_SUPPORT
2040 extern NCURSES_EXPORT(void) _nc_do_xmc_glitch (attr_t);
2041 #endif
2042 
2043 /* hardscroll.c */
2044 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
2045 extern NCURSES_EXPORT(void) _nc_linedump (void);
2046 #endif
2047 
2048 /* lib_acs.c */
2049 extern NCURSES_EXPORT(void) _nc_init_acs (void); /* corresponds to traditional 'init_acs()' */
2050 extern NCURSES_EXPORT(int)  _nc_msec_cost (const char *const, int);
2051 
2052 /* lib_addch.c */
2053 #if USE_WIDEC_SUPPORT
2054 extern NCURSES_EXPORT(int) _nc_build_wch(WINDOW *win, ARG_CH_T ch);
2055 #endif
2056 
2057 /* lib_addstr.c */
2058 #if USE_WIDEC_SUPPORT && !(defined(USE_TERMLIB) || defined(USE_BUILD_CC))
2059 extern NCURSES_EXPORT(int) _nc_wchstrlen(const cchar_t *);
2060 #endif
2061 
2062 /* lib_color.c */
2063 extern NCURSES_EXPORT(int) _nc_init_color(SCREEN *, int, int, int, int);
2064 extern NCURSES_EXPORT(int) _nc_init_pair(SCREEN *, int, int, int);
2065 extern NCURSES_EXPORT(int) _nc_pair_content(SCREEN *, int, int *, int *);
2066 extern NCURSES_EXPORT(bool) _nc_reset_colors(void);
2067 extern NCURSES_EXPORT(void) _nc_reserve_pairs(SCREEN *, int);
2068 extern NCURSES_EXPORT(void) _nc_change_pair(SCREEN *, int);
2069 
2070 #define ReservePairs(sp,want) \
2071 	    if ((sp->_color_pairs == NULL) || (want >= sp->_pair_alloc)) \
2072 		_nc_reserve_pairs(sp, want)
2073 
2074 /* lib_getch.c */
2075 extern NCURSES_EXPORT(int) _nc_wgetch(WINDOW *, int *, int EVENTLIST_2nd(_nc_eventlist *));
2076 
2077 /* lib_insch.c */
2078 extern NCURSES_EXPORT(int) _nc_insert_ch(SCREEN *, WINDOW *, chtype);
2079 
2080 /* lib_mouse.c */
2081 extern NCURSES_EXPORT(bool) _nc_has_mouse (const SCREEN *);
2082 
2083 /* lib_mvcur.c */
2084 #define NC_INFINITY	1000000	/* cost: too high to use */
2085 #define BAUDBYTE	9	/* 9 = 7 bits + 1 parity + 1 stop */
2086 
2087 extern NCURSES_EXPORT(int) _nc_mvcur(int yold, int xold, int ynew, int xnew);
2088 
2089 extern NCURSES_EXPORT(void) _nc_mvcur_init (void);
2090 extern NCURSES_EXPORT(void) _nc_mvcur_resume (void);
2091 extern NCURSES_EXPORT(void) _nc_mvcur_wrap (void);
2092 
2093 extern NCURSES_EXPORT(int) _nc_scrolln (int, int, int, int);
2094 
2095 extern NCURSES_EXPORT(void) _nc_screen_init (void);
2096 extern NCURSES_EXPORT(void) _nc_screen_resume (void);
2097 extern NCURSES_EXPORT(void) _nc_screen_wrap (void);
2098 
2099 /* lib_setup.c */
2100 extern NCURSES_EXPORT(char *) _nc_get_locale(void);
2101 extern NCURSES_EXPORT(bool)   _nc_unicode_locale(void);
2102 extern NCURSES_EXPORT(int)    _nc_locale_breaks_acs(TERMINAL *);
2103 extern NCURSES_EXPORT(int)    _nc_setupterm(const char *, int, int *, int);
2104 extern NCURSES_EXPORT(void)   _nc_tinfo_cmdch(TERMINAL *, int);
2105 
2106 #ifdef USE_PTHREADS
2107 extern NCURSES_EXPORT(SCREEN *) _nc_find_prescr(void);
2108 extern NCURSES_EXPORT(void)   _nc_forget_prescr(void);
2109 #else
2110 #define _nc_find_prescr()     _nc_prescreen.allocated
2111 #define _nc_forget_prescr()   _nc_prescreen.allocated = NULL
2112 #endif
2113 
2114 /* lib_set_term.c */
2115 extern NCURSES_EXPORT(int)    _nc_ripoffline(int, int(*)(WINDOW*, int));
2116 
2117 /* lib_setup.c */
2118 #define ExitTerminfo(code)    exit_terminfo(code)
2119 
2120 #define SETUP_FAIL ERR
2121 
2122 #define ret_error(rc, fmt, p, q)	if (errret) {\
2123 					    *errret = rc;\
2124 					    q;\
2125 					    returnCode(SETUP_FAIL);\
2126 					} else {\
2127 					    fprintf(stderr, fmt, p);\
2128 					    q;\
2129 					    ExitTerminfo(EXIT_FAILURE);\
2130 					}
2131 
2132 #define ret_error1(rc, fmt, p, q)	ret_error(rc, "'%s': " fmt, p, q)
2133 
2134 #define ret_error0(rc, msg)		if (errret) {\
2135 					    *errret = rc;\
2136 					    returnCode(SETUP_FAIL);\
2137 					} else {\
2138 					    fprintf(stderr, msg);\
2139 					    ExitTerminfo(EXIT_FAILURE);\
2140 					}
2141 
2142 /* lib_tstp.c */
2143 #if USE_SIGWINCH
2144 extern NCURSES_EXPORT(int) _nc_handle_sigwinch(SCREEN *);
2145 #else
2146 #define _nc_handle_sigwinch(a) /* nothing */
2147 #endif
2148 
2149 /* lib_wacs.c */
2150 #if USE_WIDEC_SUPPORT
2151 extern NCURSES_EXPORT(void) _nc_init_wacs(void);
2152 #endif
2153 
2154 typedef struct {
2155     char *s_head;	/* beginning of the string (may be null) */
2156     char *s_tail;	/* end of the string (may be null) */
2157     size_t s_size;	/* current remaining size available */
2158     size_t s_init;	/* total size available */
2159 } string_desc;
2160 
2161 /* strings.c */
2162 extern NCURSES_EXPORT(string_desc *) _nc_str_init (string_desc *, char *, size_t);
2163 extern NCURSES_EXPORT(string_desc *) _nc_str_null (string_desc *, size_t);
2164 extern NCURSES_EXPORT(string_desc *) _nc_str_copy (string_desc *, const string_desc *);
2165 extern NCURSES_EXPORT(bool) _nc_safe_strcat (string_desc *, const char *);
2166 extern NCURSES_EXPORT(bool) _nc_safe_strcpy (string_desc *, const char *);
2167 
2168 #if !HAVE_STRSTR
2169 #define strstr _nc_strstr
2170 extern NCURSES_EXPORT(char *) _nc_strstr (const char *, const char *);
2171 #endif
2172 
2173 /* safe_sprintf.c */
2174 extern NCURSES_EXPORT(char *) _nc_printf_string (const char *, va_list);
2175 
2176 /* tries.c */
2177 extern NCURSES_EXPORT(int) _nc_add_to_try (TRIES **, const char *, unsigned);
2178 extern NCURSES_EXPORT(char *) _nc_expand_try (TRIES *, unsigned, int *, size_t);
2179 extern NCURSES_EXPORT(int) _nc_remove_key (TRIES **, unsigned);
2180 extern NCURSES_EXPORT(int) _nc_remove_string (TRIES **, const char *);
2181 
2182 /* elsewhere ... */
2183 extern NCURSES_EXPORT(SCREEN *) _nc_screen_of (WINDOW *);
2184 extern NCURSES_EXPORT(TERMINAL*) _nc_get_cur_term (void);
2185 extern NCURSES_EXPORT(WINDOW *) _nc_makenew (int, int, int, int, int);
2186 extern NCURSES_EXPORT(char *) _nc_trace_buf (int, size_t);
2187 extern NCURSES_EXPORT(char *) _nc_trace_bufcat (int, const char *);
2188 extern NCURSES_EXPORT(char *) _nc_tracechar (SCREEN *, int);
2189 extern NCURSES_EXPORT(char *) _nc_tracemouse (SCREEN *, MEVENT const *);
2190 extern NCURSES_EXPORT(char *) _nc_trace_mmask_t (SCREEN *, mmask_t);
2191 extern NCURSES_EXPORT(int) _nc_access (const char *, int);
2192 extern NCURSES_EXPORT(int) _nc_baudrate (int);
2193 extern NCURSES_EXPORT(int) _nc_freewin (WINDOW *);
2194 extern NCURSES_EXPORT(int) _nc_getenv_num (const char *);
2195 extern NCURSES_EXPORT(int) _nc_keypad (SCREEN *, bool);
2196 extern NCURSES_EXPORT(int) _nc_ospeed (int);
2197 extern NCURSES_EXPORT(int) _nc_outch (int);
2198 extern NCURSES_EXPORT(int) _nc_putchar (int);
2199 extern NCURSES_EXPORT(int) _nc_putp(const char *, const char *);
2200 extern NCURSES_EXPORT(int) _nc_putp_flush(const char *, const char *);
2201 extern NCURSES_EXPORT(int) _nc_read_termcap_entry (const char *const, TERMTYPE2 *const);
2202 extern NCURSES_EXPORT(int) _nc_setup_tinfo(const char *, TERMTYPE2 *);
2203 extern NCURSES_EXPORT(int) _nc_setupscreen (int, int, FILE *, bool, int);
2204 extern NCURSES_EXPORT(int) _nc_timed_wait (const SCREEN *, int, int, int * EVENTLIST_2nd(_nc_eventlist *));
2205 extern NCURSES_EXPORT(int) _nc_trans_string (char *, const char *);
2206 extern NCURSES_EXPORT(void) _nc_init_termtype (TERMTYPE2 *const);
2207 extern NCURSES_EXPORT(void) _nc_do_color (int, int, int, NCURSES_OUTC);
2208 extern NCURSES_EXPORT(void) _nc_flush (void);
2209 extern NCURSES_EXPORT(void) _nc_free_entry (ENTRY *, const TERMTYPE2 *);
2210 extern NCURSES_EXPORT(void) _nc_freeall (void);
2211 extern NCURSES_EXPORT(void) _nc_hash_map (void);
2212 extern NCURSES_EXPORT(void) _nc_init_keytry (SCREEN *);
2213 extern NCURSES_EXPORT(void) _nc_keep_tic_dir (const char *);
2214 extern NCURSES_EXPORT(void) _nc_make_oldhash (int i);
2215 extern NCURSES_EXPORT(void) _nc_reset_input (FILE *, char *);
2216 extern NCURSES_EXPORT(void) _nc_scroll_oldhash (int n, int top, int bot);
2217 extern NCURSES_EXPORT(void) _nc_scroll_optimize (void);
2218 extern NCURSES_EXPORT(void) _nc_set_buffer (FILE *, int);
2219 extern NCURSES_EXPORT(void) _nc_setenv_num (const char *, int);
2220 extern NCURSES_EXPORT(void) _nc_signal_handler (int);
2221 extern NCURSES_EXPORT(void) _nc_synchook (WINDOW *);
2222 extern NCURSES_EXPORT(void) _nc_trace_tries (TRIES *);
2223 
2224 #if NCURSES_EXT_NUMBERS
2225 extern NCURSES_EXPORT(const TERMTYPE2 *) _nc_fallback2 (const char *);
2226 #else
2227 #define _nc_fallback2(tp) _nc_fallback(tp)
2228 #endif
2229 
2230 extern NCURSES_EXPORT(void) _nc_copy_termtype(TERMTYPE *, const TERMTYPE *);
2231 
2232 #if NCURSES_EXT_NUMBERS
2233 extern NCURSES_EXPORT(void) _nc_copy_termtype2 (TERMTYPE2 *, const TERMTYPE2 *);
2234 extern NCURSES_EXPORT(void) _nc_export_termtype2(TERMTYPE *, const TERMTYPE2 *);
2235 #else
2236 #define _nc_copy_termtype2(dst,src) _nc_copy_termtype((dst),(src))
2237 #define _nc_export_termtype2(dst,src) /* nothing */
2238 #define _nc_free_termtype2(t) _nc_free_termtype(t)
2239 /* also... */
2240 #define _nc_read_entry2 _nc_read_entry
2241 #endif
2242 
2243 #if NO_LEAKS
2244 extern NCURSES_EXPORT(void) _nc_alloc_entry_leaks(void);
2245 extern NCURSES_EXPORT(void) _nc_captoinfo_leaks(void);
2246 extern NCURSES_EXPORT(void) _nc_codes_leaks(void);
2247 extern NCURSES_EXPORT(void) _nc_comp_captab_leaks(void);
2248 extern NCURSES_EXPORT(void) _nc_comp_error_leaks(void);
2249 extern NCURSES_EXPORT(void) _nc_comp_scan_leaks(void);
2250 extern NCURSES_EXPORT(void) _nc_comp_userdefs_leaks(void);
2251 extern NCURSES_EXPORT(void) _nc_db_iterator_leaks(void);
2252 extern NCURSES_EXPORT(void) _nc_keyname_leaks(void);
2253 extern NCURSES_EXPORT(void) _nc_names_leaks(void);
2254 extern NCURSES_EXPORT(void) _nc_tgetent_leak(const TERMINAL *);
2255 extern NCURSES_EXPORT(void) _nc_tgetent_leaks(void);
2256 #endif
2257 
2258 #if !(defined(USE_TERMLIB) || defined(USE_BUILD_CC))
2259 extern NCURSES_EXPORT(NCURSES_CH_T) _nc_render (WINDOW *, NCURSES_CH_T);
2260 extern NCURSES_EXPORT(int) _nc_waddch_nosync (WINDOW *, const NCURSES_CH_T);
2261 extern NCURSES_EXPORT(void) _nc_scroll_window (WINDOW *, int const, int const, int const, NCURSES_CH_T);
2262 #endif
2263 
2264 #if USE_WIDEC_SUPPORT
2265 extern NCURSES_EXPORT(int) _nc_insert_wch(WINDOW *, const cchar_t *);
2266 #endif
2267 
2268 #if USE_WIDEC_SUPPORT && !(defined(USE_TERMLIB) || defined(USE_BUILD_CC))
2269 extern NCURSES_EXPORT(size_t) _nc_wcrtomb (char *, wchar_t, mbstate_t *);
2270 #endif
2271 
2272 #if USE_SIZECHANGE
2273 extern NCURSES_EXPORT(void) _nc_update_screensize (SCREEN *);
2274 #endif
2275 
2276 #if HAVE_RESIZETERM
2277 extern NCURSES_EXPORT(void) _nc_resize_margins (WINDOW *);
2278 #else
2279 #define _nc_resize_margins(wp) /* nothing */
2280 #endif
2281 
2282 #ifdef NCURSES_WGETCH_EVENTS
2283 extern NCURSES_EXPORT(int) _nc_eventlist_timeout(_nc_eventlist *);
2284 #else
2285 #define wgetch_events(win, evl) wgetch(win)
2286 #define wgetnstr_events(win, str, maxlen, evl) wgetnstr(win, str, maxlen)
2287 #endif
2288 
2289 /*
2290  * Wide-character macros to hide some platform-differences.
2291  */
2292 #if USE_WIDEC_SUPPORT
2293 
2294 #if defined(_NC_WINDOWS_NATIVE) && !defined(_NC_MSC) && !USE_NAMED_PIPES
2295 /*
2296  * MinGW has wide-character functions, but they do not work correctly.
2297  */
2298 extern int __MINGW_NOTHROW _nc_wctomb(char *, wchar_t);
2299 #define wctomb(s,wc) _nc_wctomb(s,wc)
2300 #define wcrtomb(s,wc,n) _nc_wctomb(s,wc)
2301 
2302 extern int __MINGW_NOTHROW _nc_mbtowc(wchar_t *, const char *, size_t);
2303 #define mbtowc(pwc,s,n) _nc_mbtowc(pwc,s,n)
2304 
2305 extern int __MINGW_NOTHROW _nc_mblen(const char *, size_t);
2306 #define mblen(s,n) _nc_mblen(s, n)
2307 
2308 #endif /* _NC_WINDOWS_NATIVE && !_NC_MSC */
2309 
2310 #if HAVE_MBTOWC && HAVE_MBLEN
2311 #define reset_mbytes(state) IGNORE_RC(mblen(NULL, (size_t) 0)), IGNORE_RC(mbtowc(NULL, NULL, (size_t) 0))
2312 #define count_mbytes(buffer,length,state) mblen(buffer,length)
2313 #define check_mbytes(wch,buffer,length,state) \
2314 	(int) mbtowc(&(wch), buffer, length)
2315 #define state_unused
2316 #elif HAVE_MBRTOWC && HAVE_MBRLEN
2317 #define reset_mbytes(state) init_mb(state)
2318 #define count_mbytes(buffer,length,state) mbrlen(buffer,length,&(state))
2319 #define check_mbytes(wch,buffer,length,state) \
2320 	(int) mbrtowc(&(wch), buffer, length, &(state))
2321 #else
2322 make an error
2323 #endif
2324 
2325 #endif /* USE_WIDEC_SUPPORT */
2326 
2327 /*
2328  * Not everyone has vsscanf(), but we'd like to use it for scanw().
2329  */
2330 #if !HAVE_VSSCANF
2331 extern int vsscanf(const char *str, const char *format, va_list __arg);
2332 #endif
2333 
2334 /* scroll indices */
2335 extern NCURSES_EXPORT_VAR(int *) _nc_oldnums;
2336 
2337 #define USE_SETBUF_0 0
2338 
2339 #define NC_OUTPUT(sp) ((sp != NULL && sp->_ofp != NULL) ? sp->_ofp : stdout)
2340 
2341 /*
2342  * On systems with a broken linker, define 'SP' as a function to force the
2343  * linker to pull in the data-only module with 'SP'.
2344  */
2345 #define _nc_alloc_screen_sp() typeCalloc(SCREEN, 1)
2346 
2347 #if BROKEN_LINKER
2348 #define SP _nc_screen()
2349 extern NCURSES_EXPORT(SCREEN *) _nc_screen (void);
2350 extern NCURSES_EXPORT(int)      _nc_alloc_screen (void);
2351 extern NCURSES_EXPORT(void)     _nc_set_screen (SCREEN *);
2352 #define CURRENT_SCREEN          _nc_screen()
2353 #else
2354 /* current screen is private data; avoid possible linking conflicts too */
2355 extern NCURSES_EXPORT_VAR(SCREEN *) SP;
2356 #define CURRENT_SCREEN SP
2357 #define _nc_alloc_screen()      ((SP = _nc_alloc_screen_sp()) != NULL)
2358 #define _nc_set_screen(sp)      SP = sp
2359 #endif
2360 
2361 #if NCURSES_SP_FUNCS
2362 #define CURRENT_SCREEN_PRE      (IsPreScreen(CURRENT_SCREEN) ? CURRENT_SCREEN : new_prescr())
2363 #else
2364 #define CURRENT_SCREEN_PRE      CURRENT_SCREEN
2365 #endif
2366 
2367 /*
2368  * We don't want to use the lines or columns capabilities internally, because
2369  * if the application is running multiple screens under X, it is quite possible
2370  * they could all have type xterm but have different sizes!  So...
2371  */
2372 #define screen_lines(sp)        (sp)->_lines
2373 #define screen_columns(sp)      (sp)->_columns
2374 
2375 extern NCURSES_EXPORT(int) _nc_slk_initialize (WINDOW *, int);
2376 extern NCURSES_EXPORT(int) _nc_format_slks (NCURSES_SP_DCLx int _c);
2377 
2378 /*
2379  * Some constants related to SLK's
2380  */
2381 #define MAX_SKEY_OLD	   8	/* count of soft keys */
2382 #define MAX_SKEY_LEN_OLD   8	/* max length of soft key text */
2383 #define MAX_SKEY_PC       12    /* This is what most PC's have */
2384 #define MAX_SKEY_LEN_PC    5
2385 
2386 /* Macro to check whether or not we use a standard format */
2387 #define SLK_STDFMT(fmt) (fmt < 3)
2388 /* Macro to determine height of label window */
2389 #define SLK_LINES(fmt)  (SLK_STDFMT(fmt) ? 1 : ((fmt) - 2))
2390 
2391 #define MAX_SKEY(fmt)     (SLK_STDFMT(fmt)? MAX_SKEY_OLD : MAX_SKEY_PC)
2392 #define MAX_SKEY_LEN(fmt) (SLK_STDFMT(fmt)? MAX_SKEY_LEN_OLD : MAX_SKEY_LEN_PC)
2393 
2394 /*
2395  * Common error messages
2396  */
2397 #define MSG_NO_MEMORY "Out of memory"
2398 #define MSG_NO_INPUTS "Premature EOF"
2399 
2400 extern NCURSES_EXPORT(int) _nc_set_tty_mode(TTY *);
2401 extern NCURSES_EXPORT(int) _nc_get_tty_mode(TTY *);
2402 
2403 /* timed_wait flag definitions */
2404 #define TW_NONE    0
2405 #define TW_INPUT   1
2406 #define TW_MOUSE   2
2407 #define TW_ANY     (TW_INPUT | TW_MOUSE)
2408 #define TW_EVENT   4
2409 
2410 #define SetSafeOutcWrapper(outc)	    \
2411     SCREEN* sp = CURRENT_SCREEN;            \
2412     struct screen outc_wrapper;		    \
2413     if (sp == NULL) {                       \
2414 	sp = &outc_wrapper;                 \
2415 	memset(sp,0,sizeof(struct screen)); \
2416 	sp->_outch = _nc_outc_wrapper;      \
2417     }\
2418     sp->jump = outc
2419 
2420 #if USE_TERM_DRIVER
2421 
2422 typedef struct _termInfo
2423 {
2424     bool caninit;
2425 
2426     bool hascolor;
2427     bool initcolor;
2428     bool canchange;
2429 
2430     int  tabsize;
2431 
2432     int  maxcolors;
2433     int  maxpairs;
2434     int  nocolorvideo;
2435 
2436     int  numbuttons;
2437     int  numlabels;
2438     int  labelwidth;
2439     int  labelheight;
2440 
2441     const color_t* defaultPalette;
2442 } TerminalInfo;
2443 
2444 typedef struct term_driver {
2445     bool   isTerminfo;
2446     const char* (*td_name)(struct DriverTCB*);
2447     bool   (*td_CanHandle)(struct DriverTCB*, const char*, int*);
2448     void   (*td_init)(struct DriverTCB*);
2449     void   (*td_release)(struct DriverTCB*);
2450     int    (*td_size)(struct DriverTCB*, int* Line, int *Cols);
2451     int    (*td_sgmode)(struct DriverTCB*, int setFlag, TTY*);
2452     chtype (*td_conattr)(struct DriverTCB*);
2453     int    (*td_hwcur)(struct DriverTCB*, int yold, int xold, int y, int x);
2454     int    (*td_mode)(struct DriverTCB*, int progFlag, int defFlag);
2455     bool   (*td_rescol)(struct DriverTCB*);
2456     bool   (*td_rescolors)(struct DriverTCB*);
2457     void   (*td_color)(struct DriverTCB*, int fore, int color, int(*)(SCREEN*, int));
2458     int    (*td_doBeepOrFlash)(struct DriverTCB*, int);
2459     void   (*td_initpair)(struct DriverTCB*, int, int, int);
2460     void   (*td_initcolor)(struct DriverTCB*, int, int, int, int);
2461     void   (*td_docolor)(struct DriverTCB*, int, int, int, int(*)(SCREEN*, int));
2462     void   (*td_initmouse)(struct DriverTCB*);
2463     int    (*td_testmouse)(struct DriverTCB*, int EVENTLIST_2nd(_nc_eventlist*));
2464     void   (*td_setfilter)(struct DriverTCB*);
2465     void   (*td_hwlabel)(struct DriverTCB*, int, char*);
2466     void   (*td_hwlabelOnOff)(struct DriverTCB*, int);
2467     int    (*td_update)(struct DriverTCB*);
2468     int    (*td_defaultcolors)(struct DriverTCB*, int, int);
2469     int    (*td_print)(struct DriverTCB*, char*, int);
2470     int    (*td_getsize)(struct DriverTCB*, int*, int*);
2471     int    (*td_setsize)(struct DriverTCB*, int, int);
2472     void   (*td_initacs)(struct DriverTCB*, chtype*, chtype*);
2473     void   (*td_scinit)(SCREEN *);
2474     void   (*td_scexit)(SCREEN *);
2475     int    (*td_twait)(struct DriverTCB*, int, int, int* EVENTLIST_2nd(_nc_eventlist*));
2476     int    (*td_read)(struct DriverTCB*, int*);
2477     int    (*td_nap)(struct DriverTCB*, int);
2478     int    (*td_kpad)(struct DriverTCB*, int);
2479     int    (*td_kyOk)(struct DriverTCB*, int, int);
2480     bool   (*td_kyExist)(struct DriverTCB*, int);
2481     int    (*td_cursorSet)(struct DriverTCB*, int);
2482 } TERM_DRIVER;
2483 
2484 typedef struct DriverTCB
2485 {
2486     TERMINAL      term;   /* needs to be the first Element !!! */
2487     TERM_DRIVER*  drv;    /* The driver for that Terminal */
2488     SCREEN*       csp;    /* The screen that owns that Terminal */
2489     TerminalInfo  info;   /* Driver independent core capabilities of the Terminal */
2490     void*         prop;   /* Driver dependent property storage to be used by the Driver */
2491     long          magic;
2492 } TERMINAL_CONTROL_BLOCK;
2493 
2494 #define NCDRV_MAGIC(id) (0x47110000 | (id&0xffff))
2495 #define NCDRV_TINFO      0x01
2496 #define NCDRV_WINCONSOLE 0x02
2497 
2498 #define TCBOf(sp)    ((TERMINAL_CONTROL_BLOCK*)(TerminalOf(sp)))
2499 #define InfoOf(sp)   TCBOf(sp)->info
2500 #define CallDriver(sp,method)                        TCBOf(sp)->drv->method(TCBOf(sp))
2501 #define CallDriver_1(sp,method,arg1)                 TCBOf(sp)->drv->method(TCBOf(sp),arg1)
2502 #define CallDriver_2(sp,method,arg1,arg2)            TCBOf(sp)->drv->method(TCBOf(sp),arg1,arg2)
2503 #define CallDriver_3(sp,method,arg1,arg2,arg3)       TCBOf(sp)->drv->method(TCBOf(sp),arg1,arg2,arg3)
2504 #define CallDriver_4(sp,method,arg1,arg2,arg3,arg4)  TCBOf(sp)->drv->method(TCBOf(sp),arg1,arg2,arg3,arg4)
2505 
2506 extern NCURSES_EXPORT_VAR(const color_t*) _nc_cga_palette;
2507 extern NCURSES_EXPORT_VAR(const color_t*) _nc_hls_palette;
2508 
2509 extern NCURSES_EXPORT(int)      _nc_get_driver(TERMINAL_CONTROL_BLOCK*, const char*, int*);
2510 extern NCURSES_EXPORT(void)     _nc_get_screensize_ex(SCREEN *, TERMINAL *, int *, int *);
2511 #endif /* USE_TERM_DRIVER */
2512 
2513 /*
2514  * Entrypoints which are actually provided in the terminal driver, which would
2515  * be an sp-name otherwise.
2516  */
2517 #if USE_TERM_DRIVER
2518 #define TINFO_HAS_KEY           _nc_tinfo_has_key
2519 #define TINFO_DOUPDATE          _nc_tinfo_doupdate
2520 #define TINFO_MVCUR             _nc_tinfo_mvcur
2521 extern NCURSES_EXPORT(int)      TINFO_HAS_KEY(SCREEN*, int);
2522 extern NCURSES_EXPORT(int)      TINFO_DOUPDATE(SCREEN *);
2523 extern NCURSES_EXPORT(int)      TINFO_MVCUR(SCREEN*, int, int, int, int);
2524 #else
2525 #define TINFO_HAS_KEY           NCURSES_SP_NAME(has_key)
2526 #define TINFO_DOUPDATE          NCURSES_SP_NAME(doupdate)
2527 #define TINFO_MVCUR             NCURSES_SP_NAME(_nc_mvcur)
2528 #endif
2529 
2530 #ifdef _NC_WINDOWS
2531 #if USE_WIDEC_SUPPORT
2532 #include <wchar.h>
2533 #endif
2534 #include <tchar.h>
2535 #include <io.h>
2536 #elif defined(_NC_WINDOWS_NATIVE)
2537 #include <tchar.h>
2538 #include <io.h>
2539 #elif defined(__EMX__)
2540 #include <io.h>
2541 #endif
2542 
2543 /*
2544  * Entrypoints using an extra parameter with the terminal driver.
2545  */
2546 #if USE_TERM_DRIVER
2547 extern NCURSES_EXPORT(void)   _nc_get_screensize(SCREEN *, TERMINAL *, int *, int *);
2548 extern NCURSES_EXPORT(int)    _nc_setupterm_ex(TERMINAL **, const char *, int , int *, int);
2549 #define TINFO_GET_SIZE(sp, tp, lp, cp) \
2550 	_nc_get_screensize(sp, tp, lp, cp)
2551 #define TINFO_SET_CURTERM(sp, tp) \
2552 	NCURSES_SP_NAME(set_curterm)(sp, tp)
2553 #define TINFO_SETUP_TERM(tpp, name, fd, err, reuse) \
2554 	_nc_setupterm_ex(tpp, name, fd, err, reuse)
2555 #else /* !USE_TERM_DRIVER */
2556 extern NCURSES_EXPORT(void)   _nc_get_screensize(SCREEN *, int *, int *);
2557 #define TINFO_GET_SIZE(sp, tp, lp, cp) \
2558 	_nc_get_screensize(sp, lp, cp)
2559 #define TINFO_SET_CURTERM(sp, tp) \
2560 	set_curterm(tp)
2561 #define TINFO_SETUP_TERM(tpp, name, fd, err, reuse) \
2562 	_nc_setupterm(name, fd, err, reuse)
2563 #endif /* !USE_TERM_DRIVER */
2564 
2565 #if USE_TERM_DRIVER
2566 extern NCURSES_EXPORT_VAR(TERM_DRIVER) _nc_WIN_DRIVER;
2567 extern NCURSES_EXPORT_VAR(TERM_DRIVER) _nc_TINFO_DRIVER;
2568 #endif /* USE_TERM_DRIVER */
2569 
2570 #ifdef TERMIOS
2571 #define USE_WINCONMODE 0
2572 #elif defined(USE_WIN32CON_DRIVER)
2573 #define USE_WINCONMODE 1
2574 extern NCURSES_EXPORT(int)  _nc_console_setmode(void* handle, const ConsoleMode* arg);
2575 extern NCURSES_EXPORT(int)  _nc_console_getmode(void* handle, ConsoleMode* arg);
2576 extern NCURSES_EXPORT(bool)  _nc_console_checkinit(bool assumeTermInfo);
2577 extern NCURSES_EXPORT(void*) _nc_console_fd2handle(int fd);
2578 extern NCURSES_EXPORT(WORD) _nc_console_MapColor(bool fore, int color);
2579 extern NCURSES_EXPORT(int)  _nc_console_flush(void* handle);
2580 extern NCURSES_EXPORT(bool) _nc_console_get_SBI(void);
2581 extern NCURSES_EXPORT(HANDLE) _nc_console_handle(int fd);
2582 extern NCURSES_EXPORT(int)  _nc_console_isatty(int fd);
2583 extern NCURSES_EXPORT(bool) _nc_console_keyExist(int keycode);
2584 extern NCURSES_EXPORT(int)  _nc_console_keyok(int keycode, int flag);
2585 extern NCURSES_EXPORT(int)  _nc_console_read(SCREEN *sp, HANDLE fd, int *buf);
2586 extern NCURSES_EXPORT(bool) _nc_console_restore(void);
2587 extern NCURSES_EXPORT(void) _nc_console_selectActiveHandle(void);
2588 extern NCURSES_EXPORT(void) _nc_console_set_scrollback(bool normal, CONSOLE_SCREEN_BUFFER_INFO * info);
2589 extern NCURSES_EXPORT(void) _nc_console_size(int *Lines, int *Cols);
2590 extern NCURSES_EXPORT(int)  _nc_console_test(int fd);
2591 extern NCURSES_EXPORT(int)  _nc_console_testmouse(const SCREEN *sp, HANDLE fd, int delay EVENTLIST_2nd(_nc_eventlist*));
2592 extern NCURSES_EXPORT(int)  _nc_console_twait(const SCREEN *sp, HANDLE hdl,int mode,int msec,int *left EVENTLIST_2nd(_nc_eventlist * evl));
2593 extern NCURSES_EXPORT(int)  _nc_console_vt_supported(void);
2594 
2595 #ifdef _NC_CHECK_MINTTY
2596 extern NCURSES_EXPORT(int)    _nc_console_checkmintty(int fd, LPHANDLE pMinTTY);
2597 #endif
2598 
2599 #else
2600 #error unsupported driver configuration
2601 #endif /* USE_WIN32CON_DRIVER */
2602 
2603 #if USE_TERM_DRIVER && defined(USE_WIN32CON_DRIVER)
2604 #define NC_ISATTY(fd) (0 != _nc_console_isatty(fd))
2605 #else
2606 #define NC_ISATTY(fd) isatty(fd)
2607 #endif
2608 
2609 /*
2610  * Perhaps not "real" but possibly not "fake".
2611  */
2612 #define IsRealTty(fd,value) \
2613 	(NC_ISATTY(fd) \
2614 	 && (value = ttyname(fd)) != NULL \
2615 	 && strncmp(value, "/dev/pts/", 9))
2616 
2617 #if USE_TERM_DRIVER
2618 #  define IsTermInfo(sp)       ((TCBOf(sp) != NULL) && ((TCBOf(sp)->drv != NULL)) && ((TCBOf(sp)->drv->isTerminfo)))
2619 #  define HasTInfoTerminal(sp) ((NULL != TerminalOf(sp)) && IsTermInfo(sp))
2620 #  if USE_NAMED_PIPES
2621 #    define IsTermInfoOnConsole(sp) (IsTermInfo(sp) && _nc_console_test(TerminalOf(sp)->Filedes))
2622 #  elif defined(USE_WIN32CON_DRIVER)
2623 #    define IsTermInfoOnConsole(sp) (IsTermInfo(sp) && _nc_console_test(TerminalOf(sp)->Filedes))
2624 #  else
2625 #    define IsTermInfoOnConsole(sp) FALSE
2626 #  endif
2627 #else
2628 #  define IsTermInfo(sp)       TRUE
2629 #  define HasTInfoTerminal(sp) (NULL != TerminalOf(sp))
2630 #  if USE_NAMED_PIPES
2631 #    define IsTermInfoOnConsole(sp) _nc_console_test(TerminalOf(sp)->Filedes)
2632 #  else
2633 #    define IsTermInfoOnConsole(sp) FALSE
2634 #  endif
2635 #endif
2636 
2637 #define IsValidTIScreen(sp)  (HasTInfoTerminal(sp))
2638 
2639 /*
2640  * Exported entrypoints beyond the published API
2641  */
2642 #if NCURSES_SP_FUNCS
2643 extern NCURSES_EXPORT(WINDOW *) _nc_curscr_of(SCREEN*);
2644 extern NCURSES_EXPORT(WINDOW *) _nc_newscr_of(SCREEN*);
2645 extern NCURSES_EXPORT(WINDOW *) _nc_stdscr_of(SCREEN*);
2646 extern NCURSES_EXPORT(int)      _nc_outc_wrapper(SCREEN*,int);
2647 
2648 #if USE_REENTRANT
2649 extern NCURSES_EXPORT(int)       NCURSES_SP_NAME(_nc_TABSIZE)(SCREEN*);
2650 extern NCURSES_EXPORT(char *)    NCURSES_SP_NAME(longname)(SCREEN*);
2651 #endif
2652 
2653 #if NCURSES_EXT_FUNCS
2654 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_set_tabsize)(SCREEN*, int);
2655 #endif
2656 
2657 /*
2658  * We put the safe versions of various calls here as they are not published
2659  * part of the API up to now
2660  */
2661 extern NCURSES_EXPORT(TERMINAL*) NCURSES_SP_NAME(_nc_get_cur_term) (SCREEN *sp);
2662 extern NCURSES_EXPORT(WINDOW *) NCURSES_SP_NAME(_nc_makenew) (SCREEN*, int, int, int, int, int);
2663 extern NCURSES_EXPORT(bool)     NCURSES_SP_NAME(_nc_reset_colors)(SCREEN*);
2664 extern NCURSES_EXPORT(char *)   NCURSES_SP_NAME(_nc_printf_string)(SCREEN*, const char *, va_list);
2665 extern NCURSES_EXPORT(chtype)   NCURSES_SP_NAME(_nc_acs_char)(SCREEN*,int);
2666 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_get_tty_mode)(SCREEN*,TTY*);
2667 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_mcprint)(SCREEN*,char*, int);
2668 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_msec_cost)(SCREEN*, const char *, int);
2669 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_mvcur)(SCREEN*, int, int, int, int);
2670 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_outch)(SCREEN*, int);
2671 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_putchar)(SCREEN*, int);
2672 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_putp)(SCREEN*, const char *, const char*);
2673 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_putp_flush)(SCREEN*, const char *, const char *);
2674 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_ripoffline)(SCREEN*, int, int (*)(WINDOW *,int));
2675 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scrolln)(SCREEN*, int, int, int, int);
2676 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_set_tty_mode)(SCREEN*, TTY*);
2677 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_setupscreen)(SCREEN**, int, int, FILE *, bool, int);
2678 extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tgetent)(SCREEN*,char*,const char *);
2679 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_do_color)(SCREEN*, int, int, int, NCURSES_SP_OUTC);
2680 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_do_xmc_glitch)(SCREEN*, attr_t);
2681 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_flush)(SCREEN*);
2682 extern GCC_NORETURN NCURSES_EXPORT(void) NCURSES_SP_NAME(_nc_free_and_exit)(SCREEN*, int);
2683 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_freeall)(SCREEN*);
2684 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_hash_map)(SCREEN*);
2685 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_init_acs)(SCREEN*);
2686 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_make_oldhash)(SCREEN*, int i);
2687 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_mvcur_init)(SCREEN*);
2688 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_mvcur_resume)(SCREEN*);
2689 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_mvcur_wrap)(SCREEN*);
2690 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_screen_init)(SCREEN*);
2691 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_screen_resume)(SCREEN*);
2692 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_screen_wrap)(SCREEN*);
2693 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_scroll_oldhash)(SCREEN*, int n, int top, int bot);
2694 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_scroll_optimize)(SCREEN*);
2695 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_set_buffer)(SCREEN*, FILE *, int);
2696 
2697 extern NCURSES_EXPORT(void)     _nc_cookie_init(SCREEN *sp);
2698 
2699 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
2700 extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_linedump)(SCREEN*);
2701 #endif
2702 
2703 #if USE_WIDEC_SUPPORT
2704 extern NCURSES_EXPORT(wchar_t *) NCURSES_SP_NAME(_nc_wunctrl)(SCREEN*, cchar_t *);
2705 #endif
2706 
2707 #endif /* NCURSES_SP_FUNCS */
2708 
2709 #if NCURSES_SP_FUNCS
2710 
2711 #define safe_keyname NCURSES_SP_NAME(keyname)
2712 #define safe_unctrl  NCURSES_SP_NAME(unctrl)
2713 #define safe_ungetch NCURSES_SP_NAME(ungetch)
2714 
2715 #else
2716 
2717 #define safe_keyname _nc_keyname
2718 #define safe_unctrl  _nc_unctrl
2719 #define safe_ungetch _nc_ungetch
2720 
2721 extern NCURSES_EXPORT(NCURSES_CONST char *) _nc_keyname (SCREEN *, int);
2722 extern NCURSES_EXPORT(int) _nc_ungetch (SCREEN *, int);
2723 extern NCURSES_EXPORT(NCURSES_CONST char *) _nc_unctrl (SCREEN *, chtype);
2724 
2725 #endif
2726 
2727 #ifdef EXP_XTERM_1005
2728 extern NCURSES_EXPORT(int) _nc_conv_to_utf8(unsigned char *, unsigned, unsigned);
2729 extern NCURSES_EXPORT(int) _nc_conv_to_utf32(unsigned *, const char *, unsigned);
2730 #endif
2731 
2732 #ifdef _NC_WINDOWS
2733 #if USE_WIDEC_SUPPORT
2734 #define write_screen WriteConsoleOutputW
2735 #define read_screen  ReadConsoleOutputW
2736 #define read_keycode ReadConsoleInputW
2737 #define KeyEventChar KeyEvent.uChar.UnicodeChar
2738 #define CharInfoChar Char.UnicodeChar
2739 #else
2740 #define write_screen WriteConsoleOutput
2741 #define read_screen  ReadConsoleOutput
2742 #define read_keycode ReadConsoleInput
2743 #define KeyEventChar KeyEvent.uChar.AsciiChar
2744 #define CharInfoChar Char.AsciiChar
2745 #endif
2746 #endif /* _NC_WINDOWS */
2747 
2748 #ifdef __cplusplus
2749 }
2750 #endif
2751 
2752 /* *INDENT-ON* */
2753 
2754 #endif /* CURSES_PRIV_H */
2755