1 /****************************************************************************
2  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33 
34 
35 /*
36  * $Id: curses.priv.h,v 1.168 2000/10/08 01:24:59 tom Exp $
37  *
38  *	curses.priv.h
39  *
40  *	Header file for curses library objects which are private to
41  *	the library.
42  *
43  */
44 
45 #ifndef CURSES_PRIV_H
46 #define CURSES_PRIV_H 1
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 #include <ncurses_cfg.h>
53 
54 #if USE_RCS_IDS
55 #define MODULE_ID(id) static const char Ident[] = id;
56 #else
57 #define MODULE_ID(id) /*nothing*/
58 #endif
59 
60 #include <stdlib.h>
61 #include <string.h>
62 #include <sys/types.h>
63 
64 #if HAVE_UNISTD_H
65 #include <unistd.h>
66 #endif
67 
68 #if HAVE_SYS_BSDTYPES_H
69 #include <sys/bsdtypes.h>	/* needed for ISC */
70 #endif
71 
72 #if HAVE_LIMITS_H
73 # include <limits.h>
74 #elif HAVE_SYS_PARAM_H
75 # include <sys/param.h>
76 #endif
77 
78 #ifndef PATH_MAX
79 # if defined(_POSIX_PATH_MAX)
80 #  define PATH_MAX _POSIX_PATH_MAX
81 # elif defined(MAXPATHLEN)
82 #  define PATH_MAX MAXPATHLEN
83 # else
84 #  define PATH_MAX 255	/* the Posix minimum path-size */
85 # endif
86 #endif
87 
88 #include <assert.h>
89 #include <stdio.h>
90 
91 #include <errno.h>
92 
93 #if DECL_ERRNO
94 extern int errno;
95 #endif
96 
97 #include <nc_panel.h>
98 
99 /* Some systems have a broken 'select()', but workable 'poll()'.  Use that */
100 #if HAVE_WORKING_POLL
101 #define USE_FUNC_POLL 1
102 #if HAVE_POLL_H
103 #include <poll.h>
104 #else
105 #include <sys/poll.h>
106 #endif
107 #else
108 #define USE_FUNC_POLL 0
109 #endif
110 
111 /* Alessandro Rubini's GPM (general-purpose mouse) */
112 #if HAVE_LIBGPM && HAVE_GPM_H
113 #define USE_GPM_SUPPORT 1
114 #else
115 #define USE_GPM_SUPPORT 0
116 #endif
117 
118 /* QNX mouse support */
119 #if defined(__QNX__) && !defined(__QNXNTO__)
120 #define USE_QNX_MOUSE 1
121 #else
122 #define USE_QNX_MOUSE 0
123 #endif
124 
125 /* EMX mouse support */
126 #ifdef __EMX__
127 #define USE_EMX_MOUSE
128 #endif
129 
130 #define DEFAULT_MAXCLICK 166
131 #define EV_MAX		8	/* size of mouse circular event queue */
132 
133 /*
134  * If we don't have signals to support it, don't add a sigwinch handler.
135  * In any case, resizing is an extended feature.  Use it if we've got it.
136  */
137 #if !NCURSES_EXT_FUNCS
138 #undef HAVE_SIZECHANGE
139 #endif
140 
141 #if HAVE_SIZECHANGE
142 #define USE_SIZECHANGE 1
143 #else
144 #undef USE_SIGWINCH
145 #endif
146 
147 /*
148  * If desired, one can configure this, disabling environment variables that
149  * point to custom terminfo/termcap locations.
150  */
151 #ifdef USE_ROOT_ENVIRON
152 #define use_terminfo_vars() 1
153 #else
154 #define use_terminfo_vars() _nc_env_access()
155 extern int _nc_env_access(void);
156 #endif
157 
158 /*
159  * Not all platforms have memmove; some have an equivalent bcopy.  (Some may
160  * have neither).
161  */
162 #if USE_OK_BCOPY
163 #define memmove(d,s,n) bcopy(s,d,n)
164 #elif USE_MY_MEMMOVE
165 #define memmove(d,s,n) _nc_memmove(d,s,n)
166 extern void * _nc_memmove(void *, const void *, size_t);
167 #endif
168 
169 /*
170  * Scroll hints are useless when hashmap is used
171  */
172 #if !USE_SCROLL_HINTS
173 #if !USE_HASHMAP
174 #define USE_SCROLL_HINTS 1
175 #else
176 #define USE_SCROLL_HINTS 0
177 #endif
178 #endif
179 
180 #if USE_SCROLL_HINTS
181 #define if_USE_SCROLL_HINTS(stmt) stmt
182 #else
183 #define if_USE_SCROLL_HINTS(stmt) /*nothing*/
184 #endif
185 
186 /*
187  * Note:  ht/cbt expansion flakes out randomly under Linux 1.1.47, but only
188  * when we're throwing control codes at the screen at high volume.  To see
189  * this, re-enable USE_HARD_TABS and run worm for a while.  Other systems
190  * probably don't want to define this either due to uncertainties about tab
191  * delays and expansion in raw mode.
192  */
193 
194 struct tries {
195 	struct tries    *child;     /* ptr to child.  NULL if none          */
196 	struct tries    *sibling;   /* ptr to sibling.  NULL if none        */
197 	unsigned char    ch;        /* character at this node               */
198 	unsigned short   value;     /* code of string so far.  0 if none.   */
199 };
200 
201 /*
202  * Definitions for color pairs
203  */
204 #define C_SHIFT 8		/* we need more bits than there are colors */
205 #define C_MASK  ((1 << C_SHIFT) - 1)
206 
207 #define PAIR_OF(fg, bg) ((((fg) & C_MASK) << C_SHIFT) | ((bg) & C_MASK))
208 
209 /*
210  * Common/troublesome character definitions
211  */
212 #define L_BRACE '{'
213 #define R_BRACE '}'
214 #define S_QUOTE '\''
215 
216 /*
217  * Structure for palette tables
218  */
219 
220 typedef struct
221 {
222     short red, green, blue;
223 }
224 color_t;
225 
226 #define MAXCOLUMNS    135
227 #define MAXLINES      66
228 #define FIFO_SIZE     MAXCOLUMNS+2  /* for nocbreak mode input */
229 
230 #define ACS_LEN       128
231 
232 #define WINDOWLIST struct _win_list
233 
234 #include <curses.h>	/* we'll use -Ipath directive to get the right one! */
235 
236 /*
237  * Structure for soft labels.
238  */
239 
240 typedef struct
241 {
242 	char *text;             /* text for the label */
243 	char *form_text;        /* formatted text (left/center/...) */
244 	int x;                  /* x coordinate of this field */
245 	char dirty;             /* this label has changed */
246 	char visible;           /* field is visible */
247 } slk_ent;
248 
249 typedef struct {
250 	char dirty;             /* all labels have changed */
251 	char hidden;            /* soft labels are hidden */
252 	struct _win_st *win;
253 	slk_ent *ent;
254 	char*  buffer;           /* buffer for labels */
255 	short  maxlab;           /* number of available labels */
256 	short  labcnt;           /* number of allocated labels */
257 	short  maxlen;           /* length of labels */
258         chtype attr;             /* soft label attribute */
259 } SLK;
260 
261 struct screen {
262 	int             _ifd;           /* input file ptr for screen        */
263 	FILE            *_ofp;          /* output file ptr for screen       */
264 	char            *_setbuf;       /* buffered I/O for output          */
265 	int		_buffered;      /* setvbuf uses _setbuf data        */
266 	int             _checkfd;       /* filedesc for typeahead check     */
267 	struct term     *_term;         /* terminal type information        */
268 	short           _lines;         /* screen lines                     */
269 	short           _columns;       /* screen columns                   */
270 	short           _lines_avail;   /* lines available for stdscr       */
271 	short           _topstolen;     /* lines stolen from top            */
272 
273 	WINDOW          *_curscr;       /* current screen                   */
274 	WINDOW          *_newscr;       /* virtual screen to be updated to  */
275 	WINDOW          *_stdscr;       /* screen's full-window context     */
276 
277 	struct tries    *_keytry;       /* "Try" for use with keypad mode   */
278 	struct tries    *_key_ok;       /* Disabled keys via keyok(,FALSE)  */
279 	int             _tried;         /* keypad mode was initialized      */
280 
281 	unsigned int    _fifo[FIFO_SIZE];       /* input push-back buffer   */
282 	short           _fifohead,      /* head of fifo queue               */
283 	                _fifotail,      /* tail of fifo queue               */
284 	                _fifopeek,      /* where to peek for next char      */
285 	                _fifohold;      /* set if breakout marked           */
286 
287 	int             _endwin;        /* are we out of window mode?       */
288 	unsigned long   _current_attr;  /* terminal attribute current set   */
289 	int             _coloron;       /* is color enabled?                */
290 	int             _cursor;        /* visibility of the cursor         */
291 	int             _cursrow;       /* physical cursor row              */
292 	int             _curscol;       /* physical cursor column           */
293 	int             _nl;            /* True if NL -> CR/NL is on        */
294 	int             _raw;           /* True if in raw mode              */
295 	int             _cbreak;        /* 1 if in cbreak mode              */
296 	                                /* > 1 if in halfdelay mode         */
297 	int             _echo;          /* True if echo on                  */
298 	int             _use_meta;      /* use the meta key?                */
299 	SLK             *_slk;          /* ptr to soft key struct / NULL    */
300         int             slk_format;     /* selected format for this screen  */
301 	/* cursor movement costs; units are 10ths of milliseconds */
302 #if NCURSES_NO_PADDING
303 	int             _no_padding;    /* flag to set if padding disabled  */
304 #endif
305 	int             _char_padding;  /* cost of character put            */
306 	int             _cr_cost;       /* cost of (carriage_return)        */
307 	int             _cup_cost;      /* cost of (cursor_address)         */
308 	int             _home_cost;     /* cost of (cursor_home)            */
309 	int             _ll_cost;       /* cost of (cursor_to_ll)           */
310 #if USE_HARD_TABS
311 	int             _ht_cost;       /* cost of (tab)                    */
312 	int             _cbt_cost;      /* cost of (backtab)                */
313 #endif /* USE_HARD_TABS */
314 	int             _cub1_cost;     /* cost of (cursor_left)            */
315 	int             _cuf1_cost;     /* cost of (cursor_right)           */
316 	int             _cud1_cost;     /* cost of (cursor_down)            */
317 	int             _cuu1_cost;     /* cost of (cursor_up)              */
318 	int             _cub_cost;      /* cost of (parm_cursor_left)       */
319 	int             _cuf_cost;      /* cost of (parm_cursor_right)      */
320 	int             _cud_cost;      /* cost of (parm_cursor_down)       */
321 	int             _cuu_cost;      /* cost of (parm_cursor_up)         */
322 	int             _hpa_cost;      /* cost of (column_address)         */
323 	int             _vpa_cost;      /* cost of (row_address)            */
324 	/* used in tty_update.c, must be chars */
325 	int             _ed_cost;       /* cost of (clr_eos)                */
326 	int             _el_cost;       /* cost of (clr_eol)                */
327 	int             _el1_cost;      /* cost of (clr_bol)                */
328 	int             _dch1_cost;     /* cost of (delete_character)       */
329 	int             _ich1_cost;     /* cost of (insert_character)       */
330 	int             _dch_cost;      /* cost of (parm_dch)               */
331 	int             _ich_cost;      /* cost of (parm_ich)               */
332 	int             _ech_cost;      /* cost of (erase_chars)            */
333 	int             _rep_cost;      /* cost of (repeat_char)            */
334 	int             _hpa_ch_cost;   /* cost of (column_address)         */
335 	int             _cup_ch_cost;   /* cost of (cursor_address)         */
336 	int             _cuf_ch_cost;   /* cost of (parm_cursor_right)      */
337 	int             _inline_cost;   /* cost of inline-move              */
338 	int             _smir_cost;	/* cost of (enter_insert_mode)      */
339 	int             _rmir_cost;	/* cost of (exit_insert_mode)       */
340 	int             _ip_cost;       /* cost of (insert_padding)         */
341 	/* used in lib_mvcur.c */
342 	char *          _address_cursor;
343 	/* used in tty_update.c */
344 	int             _scrolling;     /* 1 if terminal's smart enough to  */
345 
346 	/* used in lib_color.c */
347 	color_t         *_color_table;  /* screen's color palette            */
348 	int             _color_count;   /* count of colors in palette        */
349 	unsigned short  *_color_pairs;  /* screen's color pair list          */
350 	int             _pair_count;    /* count of color pairs              */
351 #if NCURSES_EXT_FUNCS
352 	bool            _default_color; /* use default colors                */
353 	bool            _has_sgr_39_49; /* has ECMA default color support    */
354 	int             _default_fg;    /* assumed default foreground        */
355 	int             _default_bg;    /* assumed default background        */
356 #endif
357 	chtype          _xmc_suppress;  /* attributes to suppress if xmc     */
358 	chtype          _xmc_triggers;  /* attributes to process if xmc      */
359 	chtype          _acs_map[ACS_LEN];
360 
361 	/* used in lib_vidattr.c */
362 	bool            _use_rmso;	/* true if we may use 'rmso'         */
363 	bool            _use_rmul;	/* true if we may use 'rmul'         */
364 
365 	/*
366 	 * These data correspond to the state of the idcok() and idlok()
367 	 * functions.  A caveat is in order here:  the XSI and SVr4
368 	 * documentation specify that these functions apply to the window which
369 	 * is given as an argument.  However, ncurses implements this logic
370 	 * only for the newscr/curscr update process, _not_ per-window.
371 	 */
372 	bool            _nc_sp_idlok;
373 	bool            _nc_sp_idcok;
374 #define _nc_idlok SP->_nc_sp_idlok
375 #define _nc_idcok SP->_nc_sp_idcok
376 
377 	/*
378 	 * These are the data that support the mouse interface.
379 	 */
380 	int             _maxclick;
381 	bool            (*_mouse_event) (SCREEN *);
382 	bool            (*_mouse_inline)(SCREEN *);
383 	bool            (*_mouse_parse) (int);
384 	void            (*_mouse_resume)(SCREEN *);
385 	void            (*_mouse_wrap)  (SCREEN *);
386 	int             _mouse_fd;      /* file-descriptor, if any */
387 
388 	/*
389 	 * This supports automatic resizing
390 	 */
391 #if USE_SIZECHANGE
392 	int		(*_resize)(int,int);
393 #endif
394 
395         /*
396 	 * These are data that support the proper handling of the panel stack on an
397 	 * per screen basis.
398 	 */
399         struct panelhook _panelHook;
400 	/*
401 	 * Linked-list of all windows, to support '_nc_resizeall()' and
402 	 * '_nc_freeall()'
403 	 */
404 	WINDOWLIST      *_nc_sp_windows;
405 #define _nc_windows SP->_nc_sp_windows
406 
407 	bool            _sig_winch;
408 	SCREEN          *_next_screen;
409 
410 	/* hashes for old and new lines */
411 	unsigned long	*oldhash, *newhash;
412 
413 	bool            _cleanup;	/* cleanup after int/quit signal */
414 	int             (*_outch)(int);	/* output handler if not putc */
415 };
416 
417 extern SCREEN *_nc_screen_chain;
418 
419 #if NCURSES_NOMACROS
420 #include <nomacros.h>
421 #endif
422 
423 	WINDOWLIST {
424 	WINDOWLIST *next;
425 	WINDOW	*win;
426 };
427 
428 typedef	struct {
429 	int	line;                   /* lines to take, < 0 => from bottom*/
430 	int	(*hook)(struct _win_st *, int); /* callback for user        */
431 	struct _win_st *w;              /* maybe we need this for cleanup   */
432 } ripoff_t;
433 
434 /* The terminfo source is assumed to be 7-bit ASCII */
435 #define is7bits(c)	((unsigned)(c) < 128)
436 
437 #ifndef min
438 #define min(a,b)	((a) > (b)  ?  (b)  :  (a))
439 #endif
440 
441 #ifndef max
442 #define max(a,b)	((a) < (b)  ?  (b)  :  (a))
443 #endif
444 
445 /* usually in <unistd.h> */
446 #ifndef STDIN_FILENO
447 #define STDIN_FILENO 0
448 #endif
449 
450 #ifndef STDOUT_FILENO
451 #define STDOUT_FILENO 1
452 #endif
453 
454 #ifndef STDERR_FILENO
455 #define STDERR_FILENO 2
456 #endif
457 
458 #ifndef EXIT_SUCCESS
459 #define EXIT_SUCCESS 0
460 #endif
461 
462 #ifndef EXIT_FAILURE
463 #define EXIT_FAILURE 1
464 #endif
465 
466 #ifndef R_OK
467 #define	R_OK	4		/* Test for read permission.  */
468 #endif
469 #ifndef W_OK
470 #define	W_OK	2		/* Test for write permission.  */
471 #endif
472 #ifndef X_OK
473 #define	X_OK	1		/* Test for execute permission.  */
474 #endif
475 #ifndef F_OK
476 #define	F_OK	0		/* Test for existence.  */
477 #endif
478 
479 #if HAVE_FCNTL_H
480 #include <fcntl.h>		/* may define O_BINARY	*/
481 #endif
482 
483 #ifndef O_BINARY
484 #define O_BINARY 0
485 #endif
486 
487 #define TextOf(c)    ((c) & (chtype)A_CHARTEXT)
488 #define AttrOf(c)    ((c) & (chtype)A_ATTRIBUTES)
489 
490 #define BLANK        (' '|A_NORMAL)
491 
492 #define CHANGED     -1
493 
494 #define CHANGED_CELL(line,col) \
495 	if (line->firstchar == _NOCHANGE) \
496 		line->firstchar = line->lastchar = col; \
497 	else if ((col) < line->firstchar) \
498 		line->firstchar = col; \
499 	else if ((col) > line->lastchar) \
500 		line->lastchar = col
501 
502 #define CHANGED_RANGE(line,start,end) \
503 	if (line->firstchar == _NOCHANGE \
504 	 || line->firstchar > (start)) \
505 		line->firstchar = start; \
506 	if (line->lastchar == _NOCHANGE \
507 	 || line->lastchar < (end)) \
508 		line->lastchar = end
509 
510 #define CHANGED_TO_EOL(line,start,end) \
511 	if (line->firstchar == _NOCHANGE \
512 	 || line->firstchar > (start)) \
513 		line->firstchar = start; \
514 	line->lastchar = end
515 
516 #define SIZEOF(v) (sizeof(v)/sizeof(v[0]))
517 
518 #define FreeIfNeeded(p)  if ((p) != 0) free(p)
519 
520 /* FreeAndNull() is not a comma-separated expression because some compilers
521  * do not accept a mixture of void with values.
522  */
523 #define FreeAndNull(p)   free(p); p = 0
524 
525 #include <nc_alloc.h>
526 
527 /*
528  * Prefixes for call/return points of library function traces.  We use these to
529  * instrument the public functions so that the traces can be easily transformed
530  * into regression scripts.
531  */
532 #define T_CALLED(fmt) "called " fmt
533 #define T_CREATE(fmt) "create " fmt
534 #define T_RETURN(fmt) "return " fmt
535 
536 #ifdef TRACE
537 #define TR(n, a)	if (_nc_tracing & (n)) _tracef a
538 #define T(a)		TR(TRACE_CALLS, a)
539 #define TPUTS_TRACE(s)	_nc_tputs_trace = s;
540 #define TRACE_RETURN(value,type) return _nc_retrace_##type(value)
541 #define returnAttr(code) TRACE_RETURN(code,attr_t)
542 #define returnChar(code) TRACE_RETURN(code,chtype)
543 #define returnCode(code) TRACE_RETURN(code,int)
544 #define returnPtr(code)  TRACE_RETURN(code,ptr)
545 #define returnVoid       T((T_RETURN(""))); return
546 #define returnWin(code)  TRACE_RETURN(code,win)
547 extern WINDOW * _nc_retrace_win(WINDOW *);
548 extern attr_t _nc_retrace_attr_t(attr_t);
549 extern attr_t _nc_retrace_chtype(chtype);
550 extern char *_nc_retrace_ptr(char *);
551 extern const char *_nc_tputs_trace;
552 extern int _nc_retrace_int(int);
553 extern long _nc_outchars;
554 extern void _nc_fifo_dump(void);
555 #else
556 #define T(a)
557 #define TR(n, a)
558 #define TPUTS_TRACE(s)
559 #define returnAttr(code) return code
560 #define returnChar(code) return code
561 #define returnCode(code) return code
562 #define returnPtr(code)  return code
563 #define returnVoid       return
564 #define returnWin(code)  return code
565 #endif
566 
567 extern unsigned _nc_tracing;
568 extern const char *_nc_visbuf2(int, const char *);
569 
570 #define _trace_key(ch) ((ch > KEY_MIN) ? keyname(ch) : _tracechar((unsigned char)ch))
571 
572 #define ALL_BUT_COLOR ((chtype)~(A_COLOR))
573 #define IGNORE_COLOR_OFF FALSE
574 #define NONBLANK_ATTR (A_BOLD|A_DIM|A_BLINK)
575 #define XMC_CHANGES(c) ((c) & SP->_xmc_suppress)
576 
577 
578 #define toggle_attr_on(S,at) {\
579    if (PAIR_NUMBER(at) > 0)\
580       (S) = ((S) & ALL_BUT_COLOR) | (at);\
581    else\
582       (S) |= (at);\
583    TR(TRACE_ATTRS, ("new attribute is %s", _traceattr((S))));}
584 
585 
586 #define toggle_attr_off(S,at) {\
587    if (IGNORE_COLOR_OFF == TRUE) {\
588       if (PAIR_NUMBER(at) == 0xff) /* turn off color */\
589 	 (S) &= ~(at);\
590       else /* leave color alone */\
591 	 (S) &= ~((at)&ALL_BUT_COLOR);\
592    } else {\
593       if (PAIR_NUMBER(at) > 0x00) /* turn off color */\
594 	 (S) &= ~(at|A_COLOR);\
595       else /* leave color alone */\
596 	 (S) &= ~(at);\
597    }\
598    TR(TRACE_ATTRS, ("new attribute is %s", _traceattr((S))));}
599 
600 #define DelCharCost(count) \
601 		((parm_dch != 0) \
602 		? SP->_dch_cost \
603 		: ((delete_character != 0) \
604 			? (SP->_dch1_cost * count) \
605 			: INFINITY))
606 
607 #define InsCharCost(count) \
608 		((parm_ich != 0) \
609 		? SP->_ich_cost \
610 		: ((enter_insert_mode && exit_insert_mode) \
611 		  ? SP->_smir_cost + SP->_rmir_cost + (SP->_ip_cost * count) \
612 		  : ((insert_character != 0) \
613 		    ? (SP->_ich1_cost * count) \
614 		    : INFINITY)))
615 
616 #if USE_XMC_SUPPORT
617 #define UpdateAttrs(c)	if (SP->_current_attr != AttrOf(c)) { \
618 				attr_t chg = SP->_current_attr; \
619 				vidattr(AttrOf(c)); \
620 				if (magic_cookie_glitch > 0 \
621 				 && XMC_CHANGES((chg ^ SP->_current_attr))) { \
622 					TR(TRACE_ATTRS, \
623 						("%s @%d before glitch %d,%d", \
624 						__FILE__, __LINE__, \
625 						SP->_cursrow, \
626 						SP->_curscol)); \
627 					_nc_do_xmc_glitch(chg); \
628 				} \
629 			}
630 #else
631 #define UpdateAttrs(c)	if (SP->_current_attr != AttrOf(c)) \
632 				vidattr(AttrOf(c))
633 #endif
634 
635 #if NCURSES_EXPANDED && NCURSES_EXT_FUNCS
636 
637 #undef  toggle_attr_on
638 #define toggle_attr_on(S,at) _nc_toggle_attr_on(&(S), at)
639 extern void _nc_toggle_attr_on(attr_t *, attr_t);
640 
641 #undef  toggle_attr_off
642 #define toggle_attr_off(S,at) _nc_toggle_attr_off(&(S), at)
643 extern void _nc_toggle_attr_off(attr_t *, attr_t);
644 
645 #undef  DelCharCost
646 #define DelCharCost(count) _nc_DelCharCost(count)
647 extern int _nc_DelCharCost(int);
648 
649 #undef  InsCharCost
650 #define InsCharCost(count) _nc_InsCharCost(count)
651 extern int _nc_InsCharCost(int);
652 
653 #undef  UpdateAttrs
654 #define UpdateAttrs(c) _nc_UpdateAttrs(c)
655 extern void _nc_UpdateAttrs(chtype);
656 
657 #else
658 
659 extern void _nc_expanded(void);
660 
661 #endif
662 
663 #if !HAVE_GETCWD
664 #define getcwd(buf,len) getwd(buf)
665 #endif
666 
667 /* doupdate.c */
668 #if USE_XMC_SUPPORT
669 extern void _nc_do_xmc_glitch(attr_t);
670 #endif
671 
672 /* hardscroll.c */
673 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
674 extern void _nc_linedump(void);
675 #endif
676 
677 /* lib_acs.c */
678 extern void _nc_init_acs(void);	/* corresponds to traditional 'init_acs()' */
679 extern int _nc_msec_cost(const char *const, int);  /* used by 'tack' program */
680 
681 /* lib_mvcur.c */
682 #define INFINITY	1000000	/* cost: too high to use */
683 
684 extern void _nc_mvcur_init(void);
685 extern void _nc_mvcur_resume(void);
686 extern void _nc_mvcur_wrap(void);
687 
688 extern int _nc_scrolln(int, int, int, int);
689 
690 extern void _nc_screen_init(void);
691 extern void _nc_screen_resume(void);
692 extern void _nc_screen_wrap(void);
693 
694 /* lib_mouse.c */
695 extern int _nc_has_mouse(void);
696 
697 /* lib_mvcur.c */
698 #define INFINITY	1000000	/* cost: too high to use */
699 
700 typedef struct {
701     char *s_head;
702     char *s_tail;
703     size_t s_size;
704 } string_desc;
705 
706 /* strings.c */
707 extern string_desc *_nc_str_init(string_desc * dst, char *src, size_t len);
708 extern string_desc *_nc_str_null(string_desc * dst, size_t len);
709 extern string_desc *_nc_str_copy(string_desc * dst, string_desc * src);
710 extern bool _nc_safe_strcat(string_desc * dst, const char *src);
711 extern bool _nc_safe_strcpy(string_desc * dst, const char *src);
712 
713 extern void _nc_mvcur_init(void);
714 extern void _nc_mvcur_resume(void);
715 extern void _nc_mvcur_wrap(void);
716 
717 extern int _nc_scrolln(int, int, int, int);
718 
719 extern void _nc_screen_init(void);
720 extern void _nc_screen_resume(void);
721 extern void _nc_screen_wrap(void);
722 
723 #if !HAVE_STRSTR
724 #define strstr _nc_strstr
725 extern char *_nc_strstr(const char *, const char *);
726 #endif
727 
728 /* safe_sprintf.c */
729 extern char * _nc_printf_string(const char *fmt, va_list ap);
730 
731 /* tries.c */
732 extern void _nc_add_to_try(struct tries **tree, const char *str, unsigned short code);
733 extern char *_nc_expand_try(struct tries *tree, unsigned short code, int *count, size_t len);
734 extern int _nc_remove_key(struct tries **tree, unsigned short code);
735 extern int _nc_remove_string(struct tries **tree, char *string);
736 
737 /* elsewhere ... */
738 extern WINDOW *_nc_makenew(int, int, int, int, int);
739 extern char *_nc_home_terminfo(void);
740 extern char *_nc_trace_buf(int, size_t);
741 extern chtype _nc_background(WINDOW *);
742 extern chtype _nc_render(WINDOW *, chtype);
743 extern int _nc_access(const char *, int);
744 extern int _nc_baudrate(int);
745 extern int _nc_getenv_num(const char *);
746 extern int _nc_keypad(bool);
747 extern int _nc_ospeed(int);
748 extern int _nc_outch(int);
749 extern int _nc_setupscreen(short, short const, FILE *);
750 extern int _nc_timed_wait(int, int, int *);
751 extern int _nc_waddch_nosync(WINDOW *, const chtype);
752 extern void _nc_do_color(int, int, bool, int (*)(int));
753 extern void _nc_freeall(void);
754 extern void _nc_freewin(WINDOW *win);
755 extern void _nc_hash_map(void);
756 extern void _nc_init_keytry(void);
757 extern void _nc_keep_tic_dir(const char *);
758 extern void _nc_make_oldhash(int i);
759 extern void _nc_flush(void);
760 extern void _nc_outstr(const char *str);
761 extern void _nc_scroll_oldhash(int n, int top, int bot);
762 extern void _nc_scroll_optimize(void);
763 extern void _nc_scroll_window(WINDOW *, int const, short const, short const, chtype);
764 extern void _nc_set_buffer(FILE *, bool);
765 extern void _nc_signal_handler(bool);
766 extern void _nc_synchook(WINDOW *win);
767 extern void _nc_trace_tries(struct tries *tree);
768 
769 #if USE_SIZECHANGE
770 extern void _nc_update_screensize(void);
771 #endif
772 
773 #if USE_WIDEC_SUPPORT
774 extern int _nc_utf8_outch(int);
775 #endif
776 
777 /* scroll indices */
778 extern int *_nc_oldnums;
779 
780 #define USE_SETBUF_0 0
781 
782 #define NC_BUFFERED(flag) \
783 	if ((SP->_buffered != 0) != flag) \
784 		_nc_set_buffer(SP->_ofp, flag)
785 
786 #define NC_OUTPUT ((SP != 0) ? SP->_ofp : stdout)
787 
788 /*
789  * On systems with a broken linker, define 'SP' as a function to force the
790  * linker to pull in the data-only module with 'SP'.
791  */
792 #if BROKEN_LINKER
793 #define SP _nc_screen()
794 extern SCREEN *_nc_screen(void);
795 extern int _nc_alloc_screen(void);
796 extern void _nc_set_screen(SCREEN *);
797 #else
798 /* current screen is private data; avoid possible linking conflicts too */
799 extern SCREEN *SP;
800 #define _nc_alloc_screen() ((SP = typeCalloc(SCREEN, 1)) != 0)
801 #define _nc_set_screen(sp) SP = sp
802 #endif
803 
804 /*
805  * We don't want to use the lines or columns capabilities internally,
806  * because if the application is running multiple screens under
807  * X windows, it's quite possible they could all have type xterm
808  * but have different sizes!  So...
809  */
810 #define screen_lines	SP->_lines
811 #define screen_columns	SP->_columns
812 
813 extern int _nc_slk_format;  /* != 0 if slk_init() called */
814 extern int _nc_slk_initialize(WINDOW *, int);
815 
816 /*
817  * Some constants related to SLK's
818  */
819 #define MAX_SKEY_OLD	   8	/* count of soft keys */
820 #define MAX_SKEY_LEN_OLD   8	/* max length of soft key text */
821 #define MAX_SKEY_PC       12    /* This is what most PC's have */
822 #define MAX_SKEY_LEN_PC    5
823 
824 /* Macro to check whether or not we use a standard format */
825 #define SLK_STDFMT(fmt) (fmt < 3)
826 /* Macro to determine height of label window */
827 #define SLK_LINES(fmt)  (SLK_STDFMT(fmt) ? 1 : ((fmt) - 2))
828 
829 #define MAX_SKEY(fmt)     (SLK_STDFMT(fmt)? MAX_SKEY_OLD : MAX_SKEY_PC)
830 #define MAX_SKEY_LEN(fmt) (SLK_STDFMT(fmt)? MAX_SKEY_LEN_OLD : MAX_SKEY_LEN_PC)
831 
832 extern int _nc_ripoffline(int line, int (*init)(WINDOW *,int));
833 
834 #ifdef __cplusplus
835 }
836 #endif
837 
838 #endif /* CURSES_PRIV_H */
839