xref: /freebsd/contrib/ncurses/ncurses/tty/tty_update.c (revision 68ad2b0d7af2a3571c4abac9afa712f9b09b721c)
1 /****************************************************************************
2  * Copyright 2018-2024,2025 Thomas E. Dickey                                *
3  * Copyright 1998-2016,2017 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                         2009                    *
35  ****************************************************************************/
36 
37 /*-----------------------------------------------------------------
38  *
39  *	lib_doupdate.c
40  *
41  * 	The routine doupdate() and its dependents.
42  * 	All physical output is concentrated here (except _nc_outch()
43  *	in lib_tputs.c).
44  *
45  *-----------------------------------------------------------------*/
46 
47 #define NEW_PAIR_INTERNAL 1
48 
49 #include <curses.priv.h>
50 
51 #ifndef CUR
52 #define CUR SP_TERMTYPE
53 #endif
54 
55 #if defined __HAIKU__ && defined __BEOS__
56 #undef __BEOS__
57 #endif
58 
59 #ifdef __BEOS__
60 #undef false
61 #undef true
62 #include <OS.h>
63 #endif
64 
65 #if defined(TRACE) && HAVE_SYS_TIMES_H && HAVE_TIMES
66 #define USE_TRACE_TIMES 1
67 #else
68 #define USE_TRACE_TIMES 0
69 #endif
70 
71 #if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
72 #include <sys/time.h>
73 #endif
74 
75 #if USE_TRACE_TIMES
76 #include <sys/times.h>
77 #endif
78 
79 #if USE_FUNC_POLL
80 #elif HAVE_SELECT
81 #if HAVE_SYS_SELECT_H
82 #include <sys/select.h>
83 #endif
84 #endif
85 
86 #include <ctype.h>
87 
88 #if USE_WIDEC_SUPPORT
89 #if HAVE_WCTYPE_H
90 #include <wctype.h>
91 #endif
92 #endif
93 
94 MODULE_ID("$Id: tty_update.c,v 1.323 2025/12/27 12:34:03 tom Exp $")
95 
96 /*
97  * This define controls the line-breakout optimization.  Every once in a
98  * while during screen refresh, we want to check for input and abort the
99  * update if there's some waiting.  CHECK_INTERVAL controls the number of
100  * changed lines to be emitted between input checks.
101  *
102  * Note: Input-check-and-abort is no longer done if the screen is being
103  * updated from scratch.  This is a feature, not a bug.
104  */
105 #define CHECK_INTERVAL	5
106 
107 #define FILL_BCE(sp) (sp->_coloron && !sp->_default_color && !back_color_erase)
108 
109 static const NCURSES_CH_T blankchar = NewChar(BLANK_TEXT);
110 static NCURSES_CH_T normal = NewChar(BLANK_TEXT);
111 
112 /*
113  * Enable checking to see if doupdate and friends are tracking the true
114  * cursor position correctly.  NOTE: this is a debugging hack which will
115  * work ONLY on ANSI-compatible terminals!
116  */
117 /* #define POSITION_DEBUG */
118 
119 static NCURSES_INLINE NCURSES_CH_T ClrBlank(NCURSES_SP_DCLx WINDOW *win);
120 
121 #if NCURSES_SP_FUNCS
122 static int ClrBottom(SCREEN *, int total);
123 static void ClearScreen(SCREEN *, NCURSES_CH_T blank);
124 static void ClrUpdate(SCREEN *);
125 static void DelChar(SCREEN *, int count);
126 static void InsStr(SCREEN *, NCURSES_CH_T *line, int count);
127 static void TransformLine(SCREEN *, int const lineno);
128 #else
129 static int ClrBottom(int total);
130 static void ClearScreen(NCURSES_CH_T blank);
131 static void ClrUpdate(void);
132 static void DelChar(int count);
133 static void InsStr(NCURSES_CH_T *line, int count);
134 static void TransformLine(int const lineno);
135 #endif
136 
137 #ifdef POSITION_DEBUG
138 /****************************************************************************
139  *
140  * Debugging code.  Only works on ANSI-standard terminals.
141  *
142  ****************************************************************************/
143 
144 static void
position_check(NCURSES_SP_DCLx int expected_y,int expected_x,const char * legend)145 position_check(NCURSES_SP_DCLx int expected_y, int expected_x, const char *legend)
146 /* check to see if the real cursor position matches the virtual */
147 {
148     char buf[20];
149     char *s;
150     int y, x;
151 
152     if (!_nc_tracing || (expected_y < 0 && expected_x < 0))
153 	return;
154 
155     NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
156     memset(buf, '\0', sizeof(buf));
157     NCURSES_PUTP2_FLUSH("cpr", "\033[6n");	/* only works on ANSI-compatibles */
158     *(s = buf) = 0;
159     do {
160 	int ask = sizeof(buf) - 1 - (s - buf);
161 	int got = read(0, s, ask);
162 	if (got == 0)
163 	    break;
164 	s += got;
165     } while (strchr(buf, 'R') == 0);
166     _tracef("probe returned %s", _nc_visbuf(buf));
167 
168     /* try to interpret as a position report */
169     if (sscanf(buf, "\033[%d;%dR", &y, &x) != 2) {
170 	_tracef("position probe failed in %s", legend);
171     } else {
172 	if (expected_x < 0)
173 	    expected_x = x - 1;
174 	if (expected_y < 0)
175 	    expected_y = y - 1;
176 	if (y - 1 != expected_y || x - 1 != expected_x) {
177 	    NCURSES_SP_NAME(beep) (NCURSES_SP_ARG);
178 	    NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
179 				    TIPARM_2("\033[%d;%dH",
180 					     expected_y + 1,
181 					     expected_x + 1),
182 				    1, NCURSES_SP_NAME(_nc_outch));
183 	    _tracef("position seen (%d, %d) doesn't match expected one (%d, %d) in %s",
184 		    y - 1, x - 1, expected_y, expected_x, legend);
185 	} else {
186 	    _tracef("position matches OK in %s", legend);
187 	}
188     }
189 }
190 #else
191 #define position_check(expected_y, expected_x, legend)	/* nothing */
192 #endif /* POSITION_DEBUG */
193 
194 /****************************************************************************
195  *
196  * Optimized update code
197  *
198  ****************************************************************************/
199 
200 static NCURSES_INLINE void
GoTo(NCURSES_SP_DCLx int const row,int const col)201 GoTo(NCURSES_SP_DCLx int const row, int const col)
202 {
203     TR(TRACE_MOVE, ("GoTo(%p, %d, %d) from (%d, %d)",
204 		    (void *) SP_PARM, row, col, SP_PARM->_cursrow, SP_PARM->_curscol));
205 
206     position_check(NCURSES_SP_ARGx
207 		   SP_PARM->_cursrow,
208 		   SP_PARM->_curscol, "GoTo");
209 
210     TINFO_MVCUR(NCURSES_SP_ARGx
211 		SP_PARM->_cursrow,
212 		SP_PARM->_curscol,
213 		row, col);
214     position_check(NCURSES_SP_ARGx
215 		   SP_PARM->_cursrow,
216 		   SP_PARM->_curscol, "GoTo2");
217 }
218 
219 #if !NCURSES_WCWIDTH_GRAPHICS
220 #define is_wacs_value(ch) (_nc_wacs_width((wchar_t) ch) == 1 && wcwidth(ch) > 1)
221 #endif /* !NCURSES_WCWIDTH_GRAPHICS */
222 
223 static NCURSES_INLINE void
PutAttrChar(NCURSES_SP_DCLx CARG_CH_T ch)224 PutAttrChar(NCURSES_SP_DCLx CARG_CH_T ch)
225 {
226     int chlen = 1;
227     NCURSES_CH_T my_ch;
228 #if USE_WIDEC_SUPPORT
229     PUTC_DATA;
230 #endif
231     NCURSES_CH_T tilde;
232     NCURSES_CH_T attr = CHDEREF(ch);
233 
234     TR(TRACE_CHARPUT, ("PutAttrChar(%s) at (%d, %d)",
235 		       _tracech_t(ch),
236 		       SP_PARM->_cursrow, SP_PARM->_curscol));
237 #if USE_WIDEC_SUPPORT
238     /*
239      * If this is not a valid character, there is nothing more to do.
240      */
241     if (isWidecExt(CHDEREF(ch))) {
242 	TR(TRACE_CHARPUT, ("...skip"));
243 	return;
244     }
245     /*
246      * Determine the number of character cells which the 'ch' value will use
247      * on the screen.  It should be at least one.
248      */
249     if ((chlen = _nc_wacs_width(CharOf(CHDEREF(ch)))) <= 0) {
250 	static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
251 
252 	/*
253 	 * If the character falls into any of these special cases, do
254 	 * not force the result to a blank:
255 	 *
256 	 * a) it is printable (this works around a bug in wcwidth()).
257 	 * b) use_legacy_coding() has been called to modify the treatment
258 	 *    of codes 128-255.
259 	 * c) the acs_map[] has been initialized to allow codes 0-31
260 	 *    to be rendered.  This supports Linux console's "PC"
261 	 *    characters.  Codes 128-255 are allowed though this is
262 	 *    not checked.
263 	 */
264 	if (is8bits(CharOf(CHDEREF(ch)))
265 	    && (isprint(CharOf(CHDEREF(ch)))
266 #if USE_WIDEC_SUPPORT
267 		|| iswprint((wint_t) CharOf(CHDEREF(ch)))
268 #endif
269 		|| (SP_PARM->_legacy_coding > 0 && CharOf(CHDEREF(ch)) >= 160)
270 		|| (SP_PARM->_legacy_coding > 1 && CharOf(CHDEREF(ch)) >= 128)
271 		|| (AttrOf(attr) & A_ALTCHARSET
272 		    && ((CharOfD(ch) < ACS_LEN
273 			 && SP_PARM->_acs_map != NULL
274 			 && SP_PARM->_acs_map[CharOfD(ch)] != 0)
275 			|| (CharOfD(ch) >= 128))))) {
276 	    ;
277 	} else {
278 	    ch = CHREF(blank);
279 	    TR(TRACE_CHARPUT, ("forced to blank"));
280 	}
281 	chlen = 1;
282     }
283 #endif
284 
285     if ((AttrOf(attr) & A_ALTCHARSET)
286 	&& SP_PARM->_acs_map != NULL
287 	&& ((CharOfD(ch) < ACS_LEN)
288 #if !NCURSES_WCWIDTH_GRAPHICS
289 	    || is_wacs_value(CharOfD(ch))
290 #endif
291 	)) {
292 	int c8;
293 	my_ch = CHDEREF(ch);	/* work around const param */
294 	c8 = (int) CharOf(my_ch);
295 #if USE_WIDEC_SUPPORT
296 	/*
297 	 * This is crude & ugly, but works most of the time.  It checks if the
298 	 * acs_chars string specified that we have a mapping for this
299 	 * character, and uses the wide-character mapping when we expect the
300 	 * normal one to be broken (by mis-design ;-).
301 	 */
302 	if (SP_PARM->_screen_unicode
303 	    && _nc_wacs[CharOf(my_ch)].chars[0]) {
304 	    if (SP_PARM->_screen_acs_map[CharOf(my_ch)]) {
305 		if (SP_PARM->_screen_acs_fix) {
306 		    RemAttr(attr, A_ALTCHARSET);
307 		    my_ch = _nc_wacs[CharOf(my_ch)];
308 		}
309 	    } else {
310 		RemAttr(attr, A_ALTCHARSET);
311 		my_ch = _nc_wacs[CharOf(my_ch)];
312 	    }
313 #if !NCURSES_WCWIDTH_GRAPHICS
314 	    if (!(AttrOf(attr) & A_ALTCHARSET)) {
315 		chlen = 1;
316 	    }
317 #endif /* !NCURSES_WCWIDTH_GRAPHICS */
318 	} else
319 #endif
320 	if (!SP_PARM->_screen_acs_map[c8]) {
321 	    /*
322 	     * If we found no mapping for a given alternate-character set item
323 	     * in the terminal description, attempt to use the ASCII fallback
324 	     * code which is populated in the _acs_map[] array.  If that did
325 	     * not correspond to a line-drawing, etc., graphics character, the
326 	     * array entry would be empty.
327 	     */
328 	    chtype temp = UChar(SP_PARM->_acs_map[c8]);
329 	    if (temp) {
330 		RemAttr(attr, A_ALTCHARSET);
331 		SetChar(my_ch, temp, AttrOf(attr));
332 	    }
333 	}
334 
335 	/*
336 	 * If we (still) have alternate character set, it is the normal 8bit
337 	 * flavor.  The _screen_acs_map[] array tells if the character was
338 	 * really in acs_chars, needed because of the way wide/normal line
339 	 * drawing flavors are integrated.
340 	 */
341 	if (AttrOf(attr) & A_ALTCHARSET) {
342 	    int j = (int) CharOfD(ch);
343 	    chtype temp = UChar(SP_PARM->_acs_map[j]);
344 
345 	    if (temp != 0) {
346 		SetChar(my_ch, temp, AttrOf(attr));
347 	    } else {
348 		my_ch = CHDEREF(ch);
349 		RemAttr(attr, A_ALTCHARSET);
350 	    }
351 	}
352 	ch = CHREF(my_ch);
353     }
354 #if USE_WIDEC_SUPPORT && !NCURSES_WCWIDTH_GRAPHICS
355     else if (chlen > 1 && is_wacs_value(CharOfD(ch))) {
356 	chlen = 1;
357     }
358 #endif
359     if (tilde_glitch && (CharOfD(ch) == L('~'))) {
360 	SetChar(tilde, L('`'), AttrOf(attr));
361 	ch = CHREF(tilde);
362     }
363 
364     UpdateAttrs(SP_PARM, attr);
365     PUTC(CHDEREF(ch));
366 #if !USE_WIDEC_SUPPORT
367     COUNT_OUTCHARS(1);
368 #endif
369     SP_PARM->_curscol += chlen;
370     if (char_padding) {
371 	NCURSES_PUTP2("char_padding", char_padding);
372     }
373 }
374 
375 static bool
check_pending(NCURSES_SP_DCL0)376 check_pending(NCURSES_SP_DCL0)
377 /* check for pending input */
378 {
379     bool have_pending = FALSE;
380 
381     /*
382      * Only carry out this check when the flag is zero, otherwise we'll
383      * have the refreshing slow down drastically (or stop) if there's an
384      * unread character available.
385      */
386     if (SP_PARM->_fifohold != 0)
387 	return FALSE;
388 
389     if (SP_PARM->_checkfd >= 0) {
390 #if USE_FUNC_POLL
391 	struct pollfd fds[1];
392 	fds[0].fd = SP_PARM->_checkfd;
393 	fds[0].events = POLLIN;
394 	if (poll(fds, (size_t) 1, 0) > 0) {
395 	    have_pending = TRUE;
396 	}
397 #elif defined(__BEOS__)
398 	/*
399 	 * BeOS's select() is declared in socket.h, so the configure script does
400 	 * not see it.  That's just as well, since that function works only for
401 	 * sockets.  This (using snooze and ioctl) was distilled from Be's patch
402 	 * for ncurses which uses a separate thread to simulate select().
403 	 *
404 	 * FIXME: the return values from the ioctl aren't very clear if we get
405 	 * interrupted.
406 	 */
407 	int n = 0;
408 	int howmany = ioctl(0, 'ichr', &n);
409 	if (howmany >= 0 && n > 0) {
410 	    have_pending = TRUE;
411 	}
412 #elif HAVE_SELECT
413 	fd_set fdset;
414 	struct timeval ktimeout;
415 
416 	ktimeout.tv_sec =
417 	    ktimeout.tv_usec = 0;
418 
419 	FD_ZERO(&fdset);
420 	FD_SET(SP_PARM->_checkfd, &fdset);
421 	if (select(SP_PARM->_checkfd + 1, &fdset, NULL, NULL, &ktimeout) != 0) {
422 	    have_pending = TRUE;
423 	}
424 #endif
425     }
426     if (have_pending) {
427 	SP_PARM->_fifohold = 5;
428 	NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
429     }
430     return FALSE;
431 }
432 
433 /* put char at lower right corner */
434 static void
PutCharLR(NCURSES_SP_DCLx const ARG_CH_T ch)435 PutCharLR(NCURSES_SP_DCLx const ARG_CH_T ch)
436 {
437     if (!auto_right_margin) {
438 	/* we can put the char directly */
439 	PutAttrChar(NCURSES_SP_ARGx ch);
440     } else if (enter_am_mode && exit_am_mode) {
441 	int oldcol = SP_PARM->_curscol;
442 	/* we can suppress automargin */
443 	NCURSES_PUTP2("exit_am_mode", exit_am_mode);
444 
445 	PutAttrChar(NCURSES_SP_ARGx ch);
446 	SP_PARM->_curscol = oldcol;
447 	position_check(NCURSES_SP_ARGx
448 		       SP_PARM->_cursrow,
449 		       SP_PARM->_curscol,
450 		       "exit_am_mode");
451 
452 	NCURSES_PUTP2("enter_am_mode", enter_am_mode);
453     } else if ((enter_insert_mode && exit_insert_mode)
454 	       || insert_character || parm_ich) {
455 	GoTo(NCURSES_SP_ARGx
456 	     screen_lines(SP_PARM) - 1,
457 	     screen_columns(SP_PARM) - 2);
458 	PutAttrChar(NCURSES_SP_ARGx ch);
459 	GoTo(NCURSES_SP_ARGx
460 	     screen_lines(SP_PARM) - 1,
461 	     screen_columns(SP_PARM) - 2);
462 	InsStr(NCURSES_SP_ARGx
463 	       NewScreen(SP_PARM)->_line[screen_lines(SP_PARM) - 1].text +
464 	       screen_columns(SP_PARM) - 2, 1);
465     }
466 }
467 
468 /*
469  * Wrap the cursor position, i.e., advance to the beginning of the next line.
470  */
471 static void
wrap_cursor(NCURSES_SP_DCL0)472 wrap_cursor(NCURSES_SP_DCL0)
473 {
474     if (eat_newline_glitch) {
475 	/*
476 	 * xenl can manifest two different ways.  The vt100 way is that, when
477 	 * you'd expect the cursor to wrap, it stays hung at the right margin
478 	 * (on top of the character just emitted) and doesn't wrap until the
479 	 * *next* graphic char is emitted.  The c100 way is to ignore LF
480 	 * received just after an am wrap.
481 	 *
482 	 * An aggressive way to handle this would be to emit CR/LF after the
483 	 * char and then assume the wrap is done, you're on the first position
484 	 * of the next line, and the terminal out of its weird state.  Here
485 	 * it is safe to just tell the code that the cursor is in hyperspace and
486 	 * let the next mvcur() call straighten things out.
487 	 */
488 	SP_PARM->_curscol = -1;
489 	SP_PARM->_cursrow = -1;
490     } else if (auto_right_margin) {
491 	SP_PARM->_curscol = 0;
492 	SP_PARM->_cursrow++;
493 	/*
494 	 * We've actually moved - but may have to work around problems with
495 	 * video attributes not working.
496 	 */
497 	if (!move_standout_mode && AttrOf(SCREEN_ATTRS(SP_PARM))) {
498 	    TR(TRACE_CHARPUT, ("turning off (%#lx) %s before wrapping",
499 			       (unsigned long) AttrOf(SCREEN_ATTRS(SP_PARM)),
500 			       _traceattr(AttrOf(SCREEN_ATTRS(SP_PARM)))));
501 	    VIDPUTS(SP_PARM, A_NORMAL, 0);
502 	}
503     } else {
504 	SP_PARM->_curscol--;
505     }
506     position_check(NCURSES_SP_ARGx
507 		   SP_PARM->_cursrow,
508 		   SP_PARM->_curscol,
509 		   "wrap_cursor");
510 }
511 
512 static NCURSES_INLINE void
PutChar(NCURSES_SP_DCLx const ARG_CH_T ch)513 PutChar(NCURSES_SP_DCLx const ARG_CH_T ch)
514 /* insert character, handling automargin stuff */
515 {
516     if (SP_PARM->_cursrow == screen_lines(SP_PARM) - 1 &&
517 	SP_PARM->_curscol == screen_columns(SP_PARM) - 1) {
518 	PutCharLR(NCURSES_SP_ARGx ch);
519     } else {
520 	PutAttrChar(NCURSES_SP_ARGx ch);
521     }
522 
523     if (SP_PARM->_curscol >= screen_columns(SP_PARM))
524 	wrap_cursor(NCURSES_SP_ARG);
525 
526     position_check(NCURSES_SP_ARGx
527 		   SP_PARM->_cursrow,
528 		   SP_PARM->_curscol, "PutChar");
529 }
530 
531 /*
532  * Check whether the given character can be output by clearing commands.  This
533  * includes test for being a space and not including any 'bad' attributes, such
534  * as A_REVERSE.  All attribute flags which don't affect appearance of a space
535  * or can be output by clearing (A_COLOR in case of bce-terminal) are excluded.
536  */
537 static NCURSES_INLINE bool
can_clear_with(NCURSES_SP_DCLx ARG_CH_T ch)538 can_clear_with(NCURSES_SP_DCLx ARG_CH_T ch)
539 {
540     if (!back_color_erase && SP_PARM->_coloron) {
541 #if NCURSES_EXT_FUNCS
542 	int pair;
543 
544 	if (!SP_PARM->_default_color)
545 	    return FALSE;
546 	if (!(isDefaultColor(SP_PARM->_default_fg) &&
547 	      isDefaultColor(SP_PARM->_default_bg)))
548 	    return FALSE;
549 	if ((pair = GetPair(CHDEREF(ch))) != 0) {
550 	    NCURSES_COLOR_T fg, bg;
551 	    if (NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
552 					       (short) pair,
553 					       &fg, &bg) == ERR
554 		|| !(isDefaultColor(fg) && isDefaultColor(bg))) {
555 		return FALSE;
556 	    }
557 	}
558 #else
559 	if (AttrOfD(ch) & A_COLOR)
560 	    return FALSE;
561 #endif
562     }
563     return (ISBLANK(CHDEREF(ch)) &&
564 	    (AttrOfD(ch) & ~(NONBLANK_ATTR | A_COLOR)) == BLANK_ATTR);
565 }
566 
567 /*
568  * Issue a given span of characters from an array.
569  * Must be functionally equivalent to:
570  *	for (i = 0; i < num; i++)
571  *	    PutChar(ntext[i]);
572  * but can leave the cursor positioned at the middle of the interval.
573  *
574  * Returns: 0 - cursor is at the end of interval
575  *	    1 - cursor is somewhere in the middle
576  *
577  * This code is optimized using ech and rep.
578  */
579 static int
EmitRange(NCURSES_SP_DCLx const NCURSES_CH_T * ntext,int num)580 EmitRange(NCURSES_SP_DCLx const NCURSES_CH_T *ntext, int num)
581 {
582     int i;
583 
584     TR(TRACE_CHARPUT, ("EmitRange %d:%s", num, _nc_viscbuf(ntext, num)));
585 
586     if (erase_chars || repeat_char) {
587 	while (num > 0) {
588 	    int runcount;
589 	    NCURSES_CH_T ntext0;
590 
591 	    while (num > 1 && !CharEq(ntext[0], ntext[1])) {
592 		PutChar(NCURSES_SP_ARGx CHREF(ntext[0]));
593 		ntext++;
594 		num--;
595 	    }
596 	    ntext0 = ntext[0];
597 	    if (num == 1) {
598 		PutChar(NCURSES_SP_ARGx CHREF(ntext0));
599 		return 0;
600 	    }
601 	    runcount = 2;
602 
603 	    while (runcount < num && CharEq(ntext[runcount], ntext0))
604 		runcount++;
605 
606 	    /*
607 	     * The cost expression in the middle isn't exactly right.
608 	     * _cup_ch_cost is an upper bound on the cost for moving to the
609 	     * end of the erased area, but not the cost itself (which we
610 	     * can't compute without emitting the move).  This may result
611 	     * in erase_chars not getting used in some situations for
612 	     * which it would be marginally advantageous.
613 	     */
614 	    if (erase_chars
615 		&& runcount > SP_PARM->_ech_cost + SP_PARM->_cup_ch_cost
616 		&& can_clear_with(NCURSES_SP_ARGx CHREF(ntext0))) {
617 		UpdateAttrs(SP_PARM, ntext0);
618 		NCURSES_PUTP2("erase_chars", TIPARM_1(erase_chars, runcount));
619 
620 		/*
621 		 * If this is the last part of the given interval,
622 		 * don't bother moving cursor, since it can be the
623 		 * last update on the line.
624 		 */
625 		if (runcount < num) {
626 		    GoTo(NCURSES_SP_ARGx
627 			 SP_PARM->_cursrow,
628 			 SP_PARM->_curscol + runcount);
629 		} else {
630 		    return 1;	/* cursor stays in the middle */
631 		}
632 	    } else if (repeat_char != NULL &&
633 #if BSD_TPUTS
634 		       !isdigit(UChar(CharOf(ntext0))) &&
635 #endif
636 #if USE_WIDEC_SUPPORT
637 		       (!SP_PARM->_screen_unicode &&
638 			(CharOf(ntext0) < ((AttrOf(ntext0) & A_ALTCHARSET)
639 					   ? ACS_LEN
640 					   : 256))) &&
641 #endif
642 		       runcount > SP_PARM->_rep_cost) {
643 		NCURSES_CH_T temp;
644 		bool wrap_possible = (SP_PARM->_curscol + runcount >=
645 				      screen_columns(SP_PARM));
646 		int rep_count = runcount;
647 
648 		if (wrap_possible)
649 		    rep_count--;
650 
651 		UpdateAttrs(SP_PARM, ntext0);
652 		temp = ntext0;
653 		if ((AttrOf(temp) & A_ALTCHARSET) &&
654 		    SP_PARM->_acs_map != NULL &&
655 		    (SP_PARM->_acs_map[CharOf(temp)] & A_CHARTEXT) != 0) {
656 		    SetChar(temp,
657 			    (SP_PARM->_acs_map[CharOf(ntext0)] & A_CHARTEXT),
658 			    AttrOf(ntext0) | A_ALTCHARSET);
659 		}
660 		NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
661 					TIPARM_2(repeat_char,
662 						 CharOf(temp),
663 						 rep_count),
664 					1,
665 					NCURSES_SP_NAME(_nc_outch));
666 		SP_PARM->_curscol += rep_count;
667 
668 		if (wrap_possible)
669 		    PutChar(NCURSES_SP_ARGx CHREF(ntext0));
670 	    } else {
671 		for (i = 0; i < runcount; i++)
672 		    PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
673 	    }
674 	    ntext += runcount;
675 	    num -= runcount;
676 	}
677 	return 0;
678     }
679 
680     for (i = 0; i < num; i++)
681 	PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
682     return 0;
683 }
684 
685 /*
686  * Output the line in the given range [first .. last]
687  *
688  * If there's a run of identical characters that's long enough to justify
689  * cursor movement, use that also.
690  *
691  * Returns: same as EmitRange
692  */
693 static int
PutRange(NCURSES_SP_DCLx const NCURSES_CH_T * otext,const NCURSES_CH_T * ntext,int row,int first,int last)694 PutRange(NCURSES_SP_DCLx
695 	 const NCURSES_CH_T *otext,
696 	 const NCURSES_CH_T *ntext,
697 	 int row,
698 	 int first, int last)
699 {
700     int rc;
701 
702     TR(TRACE_CHARPUT, ("PutRange(%p, %p, %p, %d, %d, %d)",
703 		       (void *) SP_PARM,
704 		       (const void *) otext,
705 		       (const void *) ntext,
706 		       row, first, last));
707 
708     if (otext != ntext
709 	&& (last - first + 1) > SP_PARM->_inline_cost) {
710 	int i, j, same;
711 
712 	for (j = first, same = 0; j <= last; j++) {
713 	    if (!same && isWidecExt(otext[j]))
714 		continue;
715 	    if (CharEq(otext[j], ntext[j])) {
716 		same++;
717 	    } else {
718 		if (same > SP_PARM->_inline_cost) {
719 		    EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
720 		    GoTo(NCURSES_SP_ARGx row, first = j);
721 		}
722 		same = 0;
723 	    }
724 	}
725 	i = EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
726 	/*
727 	 * Always return 1 for the next GoTo() after a PutRange() if we found
728 	 * identical characters at end of interval
729 	 */
730 	rc = (same == 0 ? i : 1);
731     } else {
732 	rc = EmitRange(NCURSES_SP_ARGx ntext + first, last - first + 1);
733     }
734     return rc;
735 }
736 
737 /* leave unbracketed here so 'indent' works */
738 #define MARK_NOCHANGE(win,row) \
739 		win->_line[row].firstchar = _NOCHANGE; \
740 		win->_line[row].lastchar = _NOCHANGE; \
741 		if_USE_SCROLL_HINTS(win->_line[row].oldindex = row)
742 
743 NCURSES_EXPORT(int)
TINFO_DOUPDATE(NCURSES_SP_DCL0)744 TINFO_DOUPDATE(NCURSES_SP_DCL0)
745 {
746     int i;
747     int nonempty;
748 #if USE_TRACE_TIMES
749     struct tms before, after;
750 #endif /* USE_TRACE_TIMES */
751 
752     T((T_CALLED("_nc_tinfo:doupdate(%p)"), (void *) SP_PARM));
753 
754     _nc_lock_global(update);
755 
756     if (SP_PARM == NULL) {
757 	_nc_unlock_global(update);
758 	returnCode(ERR);
759     }
760 #if !USE_REENTRANT
761     /*
762      * It is "legal" but unlikely that an application could assign a new
763      * value to one of the standard windows.  Check for that possibility
764      * and try to recover.
765      *
766      * We do not allow applications to assign new values in the reentrant
767      * model.
768      */
769 #if NCURSES_SP_FUNCS
770     if (SP_PARM == CURRENT_SCREEN) {
771 #endif
772 #define SyncScreens(internal,exported) \
773 	if (internal == NULL) internal = exported; \
774 	if (internal != exported) exported = internal
775 
776 	SyncScreens(CurScreen(SP_PARM), curscr);
777 	SyncScreens(NewScreen(SP_PARM), newscr);
778 	SyncScreens(StdScreen(SP_PARM), stdscr);
779 #if NCURSES_SP_FUNCS
780     }
781 #endif
782 #endif /* !USE_REENTRANT */
783 
784     if (CurScreen(SP_PARM) == NULL
785 	|| NewScreen(SP_PARM) == NULL
786 	|| StdScreen(SP_PARM) == NULL) {
787 	_nc_unlock_global(update);
788 	returnCode(ERR);
789     }
790 #ifdef TRACE
791     if (USE_TRACEF(TRACE_UPDATE)) {
792 	if (CurScreen(SP_PARM)->_clear)
793 	    _tracef("curscr is clear");
794 	else
795 	    _tracedump("curscr", CurScreen(SP_PARM));
796 	_tracedump("newscr", NewScreen(SP_PARM));
797 	_nc_unlock_global(tracef);
798     }
799 #endif /* TRACE */
800 
801     _nc_signal_handler(FALSE);
802 
803     if (SP_PARM->_fifohold)
804 	SP_PARM->_fifohold--;
805 
806 #if USE_SIZECHANGE
807     if ((SP_PARM->_endwin == ewSuspend)
808 	|| _nc_handle_sigwinch(SP_PARM)) {
809 	/*
810 	 * This is a transparent extension:  XSI does not address it,
811 	 * and applications need not know that ncurses can do it.
812 	 *
813 	 * Check if the terminal size has changed while curses was off
814 	 * (this can happen in an xterm, for example), and resize the
815 	 * ncurses data structures accordingly.
816 	 */
817 	_nc_update_screensize(SP_PARM);
818     }
819 #endif
820 
821     if (SP_PARM->_endwin == ewSuspend) {
822 
823 	T(("coming back from shell mode"));
824 	NCURSES_SP_NAME(reset_prog_mode) (NCURSES_SP_ARG);
825 
826 	NCURSES_SP_NAME(_nc_mvcur_resume) (NCURSES_SP_ARG);
827 	NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_ARG);
828 	SP_PARM->_mouse_resume(SP_PARM);
829 
830 	SP_PARM->_endwin = ewRunning;
831     }
832 #if USE_TRACE_TIMES
833     /* zero the metering machinery */
834     RESET_OUTCHARS();
835     (void) times(&before);
836 #endif /* USE_TRACE_TIMES */
837 
838     /*
839      * This is the support for magic-cookie terminals.  The theory:  we scan
840      * the virtual screen looking for attribute turnons.  Where we find one,
841      * check to make sure it is realizable by seeing if the required number of
842      * un-attributed blanks are present before and after the attributed range;
843      * try to shift the range boundaries over blanks (not changing the screen
844      * display) so this becomes true.  If it is, shift the beginning attribute
845      * change appropriately (the end one, if we've gotten this far, is
846      * guaranteed room for its cookie).  If not, nuke the added attributes out
847      * of the span.
848      */
849 #if USE_XMC_SUPPORT
850     if (magic_cookie_glitch > 0) {
851 	int j, k;
852 	attr_t rattr = A_NORMAL;
853 
854 	for (i = 0; i < screen_lines(SP_PARM); i++) {
855 	    for (j = 0; j < screen_columns(SP_PARM); j++) {
856 		bool failed = FALSE;
857 		NCURSES_CH_T *thisline = NewScreen(SP_PARM)->_line[i].text;
858 		attr_t thisattr = AttrOf(thisline[j]) & SP_PARM->_xmc_triggers;
859 		attr_t turnon = thisattr & ~rattr;
860 
861 		/* is an attribute turned on here? */
862 		if (turnon == 0) {
863 		    rattr = thisattr;
864 		    continue;
865 		}
866 
867 		TR(TRACE_ATTRS, ("At (%d, %d): from %s...", i, j, _traceattr(rattr)));
868 		TR(TRACE_ATTRS, ("...to %s", _traceattr(turnon)));
869 
870 		/*
871 		 * If the attribute change location is a blank with a "safe"
872 		 * attribute, undo the attribute turnon.  This may ensure
873 		 * there's enough room to set the attribute before the first
874 		 * non-blank in the run.
875 		 */
876 #define SAFE(scr,a)	(!((a) & (scr)->_xmc_triggers))
877 		if (ISBLANK(thisline[j]) && SAFE(SP_PARM, turnon)) {
878 		    RemAttr(thisline[j], turnon);
879 		    continue;
880 		}
881 
882 		/* check that there's enough room at start of span */
883 		for (k = 1; k <= magic_cookie_glitch; k++) {
884 		    if (j - k < 0
885 			|| !ISBLANK(thisline[j - k])
886 			|| !SAFE(SP_PARM, AttrOf(thisline[j - k]))) {
887 			failed = TRUE;
888 			TR(TRACE_ATTRS, ("No room at start in %d,%d%s%s",
889 					 i, j - k,
890 					 (ISBLANK(thisline[j - k])
891 					  ? ""
892 					  : ":nonblank"),
893 					 (SAFE(SP_PARM, AttrOf(thisline[j - k]))
894 					  ? ""
895 					  : ":unsafe")));
896 			break;
897 		    }
898 		}
899 		if (!failed) {
900 		    bool end_onscreen = FALSE;
901 		    int m, n = j;
902 
903 		    /* find end of span, if it is onscreen */
904 		    for (m = i; m < screen_lines(SP_PARM); m++) {
905 			for (; n < screen_columns(SP_PARM); n++) {
906 			    attr_t testattr =
907 			    AttrOf(NewScreen(SP_PARM)->_line[m].text[n]);
908 			    if ((testattr & SP_PARM->_xmc_triggers) == rattr) {
909 				end_onscreen = TRUE;
910 				TR(TRACE_ATTRS,
911 				   ("Range attributed with %s ends at (%d, %d)",
912 				    _traceattr(turnon), m, n));
913 				goto foundit;
914 			    }
915 			}
916 			n = 0;
917 		    }
918 		    TR(TRACE_ATTRS,
919 		       ("Range attributed with %s ends offscreen",
920 			_traceattr(turnon)));
921 		  foundit:;
922 
923 		    if (end_onscreen) {
924 			NCURSES_CH_T *lastline =
925 			NewScreen(SP_PARM)->_line[m].text;
926 
927 			/*
928 			 * If there are safely-attributed blanks at the end of
929 			 * the range, shorten the range.  This will help ensure
930 			 * that there is enough room at end of span.
931 			 */
932 			while (n >= 0
933 			       && ISBLANK(lastline[n])
934 			       && SAFE(SP_PARM, AttrOf(lastline[n]))) {
935 			    RemAttr(lastline[n--], turnon);
936 			}
937 
938 			/* check that there's enough room at end of span */
939 			for (k = 1; k <= magic_cookie_glitch; k++) {
940 			    if (n + k >= screen_columns(SP_PARM)
941 				|| !ISBLANK(lastline[n + k])
942 				|| !SAFE(SP_PARM, AttrOf(lastline[n + k]))) {
943 				failed = TRUE;
944 				TR(TRACE_ATTRS,
945 				   ("No room at end in %d,%d%s%s",
946 				    i, j - k,
947 				    (ISBLANK(lastline[n + k])
948 				     ? ""
949 				     : ":nonblank"),
950 				    (SAFE(SP_PARM, AttrOf(lastline[n + k]))
951 				     ? ""
952 				     : ":unsafe")));
953 				break;
954 			    }
955 			}
956 		    }
957 		}
958 
959 		if (failed) {
960 		    int p, q = j;
961 
962 		    TR(TRACE_ATTRS,
963 		       ("Clearing %s beginning at (%d, %d)",
964 			_traceattr(turnon), i, j));
965 
966 		    /* turn off new attributes over span */
967 		    for (p = i; p < screen_lines(SP_PARM); p++) {
968 			for (; q < screen_columns(SP_PARM); q++) {
969 			    attr_t testattr = AttrOf(newscr->_line[p].text[q]);
970 			    if ((testattr & SP_PARM->_xmc_triggers) == rattr)
971 				goto foundend;
972 			    RemAttr(NewScreen(SP_PARM)->_line[p].text[q], turnon);
973 			}
974 			q = 0;
975 		    }
976 		  foundend:;
977 		} else {
978 		    TR(TRACE_ATTRS,
979 		       ("Cookie space for %s found before (%d, %d)",
980 			_traceattr(turnon), i, j));
981 
982 		    /*
983 		     * Back up the start of range so there's room for cookies
984 		     * before the first nonblank character.
985 		     */
986 		    for (k = 1; k <= magic_cookie_glitch; k++)
987 			AddAttr(thisline[j - k], turnon);
988 		}
989 
990 		rattr = thisattr;
991 	    }
992 	}
993 
994 #ifdef TRACE
995 	/* show altered highlights after magic-cookie check */
996 	if (USE_TRACEF(TRACE_UPDATE)) {
997 	    _tracef("After magic-cookie check...");
998 	    _tracedump("newscr", NewScreen(SP_PARM));
999 	    _nc_unlock_global(tracef);
1000 	}
1001 #endif /* TRACE */
1002     }
1003 #endif /* USE_XMC_SUPPORT */
1004 
1005     nonempty = 0;
1006     if (CurScreen(SP_PARM)->_clear || NewScreen(SP_PARM)->_clear) {	/* force refresh ? */
1007 	ClrUpdate(NCURSES_SP_ARG);
1008 	CurScreen(SP_PARM)->_clear = FALSE;	/* reset flag */
1009 	NewScreen(SP_PARM)->_clear = FALSE;	/* reset flag */
1010     } else {
1011 	int changedlines = CHECK_INTERVAL;
1012 
1013 	if (check_pending(NCURSES_SP_ARG))
1014 	    goto cleanup;
1015 
1016 	nonempty = Min(screen_lines(SP_PARM), NewScreen(SP_PARM)->_maxy + 1);
1017 
1018 	if (SP_PARM->_scrolling) {
1019 	    NCURSES_SP_NAME(_nc_scroll_optimize) (NCURSES_SP_ARG);
1020 	}
1021 
1022 	nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
1023 
1024 	TR(TRACE_UPDATE, ("Transforming lines, nonempty %d", nonempty));
1025 	for (i = 0; i < nonempty; i++) {
1026 	    /*
1027 	     * Here is our line-breakout optimization.
1028 	     */
1029 	    if (changedlines == CHECK_INTERVAL) {
1030 		if (check_pending(NCURSES_SP_ARG))
1031 		    goto cleanup;
1032 		changedlines = 0;
1033 	    }
1034 
1035 	    /*
1036 	     * newscr->line[i].firstchar is normally set
1037 	     * by wnoutrefresh.  curscr->line[i].firstchar
1038 	     * is normally set by _nc_scroll_window in the
1039 	     * vertical-movement optimization code,
1040 	     */
1041 	    if (NewScreen(SP_PARM)->_line[i].firstchar != _NOCHANGE
1042 		|| CurScreen(SP_PARM)->_line[i].firstchar != _NOCHANGE) {
1043 		TransformLine(NCURSES_SP_ARGx i);
1044 		changedlines++;
1045 	    }
1046 
1047 	    /* mark line changed successfully */
1048 	    if (i <= NewScreen(SP_PARM)->_maxy) {
1049 		MARK_NOCHANGE(NewScreen(SP_PARM), i);
1050 	    }
1051 	    if (i <= CurScreen(SP_PARM)->_maxy) {
1052 		MARK_NOCHANGE(CurScreen(SP_PARM), i);
1053 	    }
1054 	}
1055     }
1056 
1057     /* put everything back in sync */
1058     for (i = nonempty; i <= NewScreen(SP_PARM)->_maxy; i++) {
1059 	MARK_NOCHANGE(NewScreen(SP_PARM), i);
1060     }
1061     for (i = nonempty; i <= CurScreen(SP_PARM)->_maxy; i++) {
1062 	MARK_NOCHANGE(CurScreen(SP_PARM), i);
1063     }
1064 
1065     if (!NewScreen(SP_PARM)->_leaveok) {
1066 	CurScreen(SP_PARM)->_curx = NewScreen(SP_PARM)->_curx;
1067 	CurScreen(SP_PARM)->_cury = NewScreen(SP_PARM)->_cury;
1068 
1069 	GoTo(NCURSES_SP_ARGx CurScreen(SP_PARM)->_cury, CurScreen(SP_PARM)->_curx);
1070     }
1071 
1072   cleanup:
1073     /*
1074      * We would like to keep the physical screen in normal mode in case we get
1075      * other processes writing to the screen.  This goal cannot be met for
1076      * magic cookies since it interferes with attributes that may propagate
1077      * past the current position.
1078      */
1079 #if USE_XMC_SUPPORT
1080     if (magic_cookie_glitch != 0)
1081 #endif
1082 	UpdateAttrs(SP_PARM, normal);
1083 
1084     NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
1085     WINDOW_ATTRS(CurScreen(SP_PARM)) = WINDOW_ATTRS(NewScreen(SP_PARM));
1086 
1087 #if USE_TRACE_TIMES
1088     (void) times(&after);
1089     TR(TRACE_TIMES,
1090        ("Update cost: %ld chars, %ld clocks system time, %ld clocks user time",
1091 	_nc_outchars,
1092 	(long) (after.tms_stime - before.tms_stime),
1093 	(long) (after.tms_utime - before.tms_utime)));
1094 #endif /* USE_TRACE_TIMES */
1095 
1096     _nc_signal_handler(TRUE);
1097 
1098     _nc_unlock_global(update);
1099     returnCode(OK);
1100 }
1101 
1102 #if NCURSES_SP_FUNCS && !USE_TERM_DRIVER
1103 NCURSES_EXPORT(int)
doupdate(void)1104 doupdate(void)
1105 {
1106     return TINFO_DOUPDATE(CURRENT_SCREEN);
1107 }
1108 #endif
1109 
1110 /*
1111  *	ClrBlank(win)
1112  *
1113  *	Returns the attributed character that corresponds to the "cleared"
1114  *	screen.  If the terminal has the back-color-erase feature, this will be
1115  *	colored according to the wbkgd() call.
1116  *
1117  *	We treat 'curscr' specially because it isn't supposed to be set directly
1118  *	in the wbkgd() call.  Assume 'stdscr' for this case.
1119  */
1120 #define BCE_ATTRS (A_NORMAL|A_COLOR)
1121 #define BCE_BKGD(sp,win) (((win) == CurScreen(sp) ? StdScreen(sp) : (win))->_nc_bkgd)
1122 
1123 static NCURSES_INLINE NCURSES_CH_T
ClrBlank(NCURSES_SP_DCLx WINDOW * win)1124 ClrBlank(NCURSES_SP_DCLx WINDOW *win)
1125 {
1126     NCURSES_CH_T blank = blankchar;
1127     if (back_color_erase)
1128 	AddAttr(blank, (AttrOf(BCE_BKGD(SP_PARM, win)) & BCE_ATTRS));
1129     return blank;
1130 }
1131 
1132 /*
1133 **	ClrUpdate()
1134 **
1135 **	Update by clearing and redrawing the entire screen.
1136 **
1137 */
1138 
1139 static void
ClrUpdate(NCURSES_SP_DCL0)1140 ClrUpdate(NCURSES_SP_DCL0)
1141 {
1142     TR(TRACE_UPDATE, (T_CALLED("ClrUpdate")));
1143     if (NULL != SP_PARM) {
1144 	int i;
1145 	NCURSES_CH_T blank = ClrBlank(NCURSES_SP_ARGx StdScreen(SP_PARM));
1146 	int nonempty = Min(screen_lines(SP_PARM),
1147 			   NewScreen(SP_PARM)->_maxy + 1);
1148 
1149 	ClearScreen(NCURSES_SP_ARGx blank);
1150 
1151 	TR(TRACE_UPDATE, ("updating screen from scratch"));
1152 
1153 	nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
1154 
1155 	for (i = 0; i < nonempty; i++)
1156 	    TransformLine(NCURSES_SP_ARGx i);
1157     }
1158     TR(TRACE_UPDATE, (T_RETURN("")));
1159 }
1160 
1161 /*
1162 **	ClrToEOL(blank)
1163 **
1164 **	Clear to end of current line, starting at the cursor position
1165 */
1166 
1167 static void
ClrToEOL(NCURSES_SP_DCLx NCURSES_CH_T blank,int needclear)1168 ClrToEOL(NCURSES_SP_DCLx NCURSES_CH_T blank, int needclear)
1169 {
1170     if (CurScreen(SP_PARM) != NULL
1171 	&& SP_PARM->_cursrow >= 0) {
1172 	int j;
1173 
1174 	for (j = SP_PARM->_curscol; j < screen_columns(SP_PARM); j++) {
1175 	    if (j >= 0) {
1176 		NCURSES_CH_T *cp =
1177 		&(CurScreen(SP_PARM)->_line[SP_PARM->_cursrow].text[j]);
1178 
1179 		if (!CharEq(*cp, blank)) {
1180 		    *cp = blank;
1181 		    needclear = TRUE;
1182 		}
1183 	    }
1184 	}
1185     }
1186 
1187     if (needclear) {
1188 	UpdateAttrs(SP_PARM, blank);
1189 	if (clr_eol && SP_PARM->_el_cost <= (screen_columns(SP_PARM) - SP_PARM->_curscol)) {
1190 	    NCURSES_PUTP2("clr_eol", clr_eol);
1191 	} else {
1192 	    int count = (screen_columns(SP_PARM) - SP_PARM->_curscol);
1193 	    while (count-- > 0)
1194 		PutChar(NCURSES_SP_ARGx CHREF(blank));
1195 	}
1196     }
1197 }
1198 
1199 /*
1200 **	ClrToEOS(blank)
1201 **
1202 **	Clear to end of screen, starting at the cursor position
1203 */
1204 
1205 static void
ClrToEOS(NCURSES_SP_DCLx NCURSES_CH_T blank)1206 ClrToEOS(NCURSES_SP_DCLx NCURSES_CH_T blank)
1207 {
1208     int row, col;
1209 
1210     row = SP_PARM->_cursrow;
1211     col = SP_PARM->_curscol;
1212 
1213     if (row < 0)
1214 	row = 0;
1215     if (col < 0)
1216 	col = 0;
1217 
1218     UpdateAttrs(SP_PARM, blank);
1219     TPUTS_TRACE("clr_eos");
1220     NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1221 			    clr_eos,
1222 			    screen_lines(SP_PARM) - row,
1223 			    NCURSES_SP_NAME(_nc_outch));
1224 
1225     while (col < screen_columns(SP_PARM))
1226 	CurScreen(SP_PARM)->_line[row].text[col++] = blank;
1227 
1228     for (row++; row < screen_lines(SP_PARM); row++) {
1229 	for (col = 0; col < screen_columns(SP_PARM); col++)
1230 	    CurScreen(SP_PARM)->_line[row].text[col] = blank;
1231     }
1232 }
1233 
1234 /*
1235  *	ClrBottom(total)
1236  *
1237  *	Test if clearing the end of the screen would satisfy part of the
1238  *	screen-update.  Do this by scanning backwards through the lines in the
1239  *	screen, checking if each is blank, and one or more are changed.
1240  */
1241 static int
ClrBottom(NCURSES_SP_DCLx int total)1242 ClrBottom(NCURSES_SP_DCLx int total)
1243 {
1244     int top = total;
1245     int last = Min(screen_columns(SP_PARM), NewScreen(SP_PARM)->_maxx + 1);
1246     NCURSES_CH_T blank = NewScreen(SP_PARM)->_line[total - 1].text[last - 1];
1247 
1248     if (clr_eos && can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1249 	int row;
1250 
1251 	for (row = total - 1; row >= 0; row--) {
1252 	    int col;
1253 	    bool ok;
1254 
1255 	    for (col = 0, ok = TRUE; ok && col < last; col++) {
1256 		ok = (CharEq(NewScreen(SP_PARM)->_line[row].text[col], blank));
1257 	    }
1258 	    if (!ok)
1259 		break;
1260 
1261 	    for (col = 0; ok && col < last; col++) {
1262 		ok = (CharEq(CurScreen(SP_PARM)->_line[row].text[col], blank));
1263 	    }
1264 	    if (!ok)
1265 		top = row;
1266 	}
1267 
1268 	/* don't use clr_eos for just one line if clr_eol available */
1269 	if (top < total) {
1270 	    GoTo(NCURSES_SP_ARGx top, 0);
1271 	    ClrToEOS(NCURSES_SP_ARGx blank);
1272 	    if (SP_PARM->oldhash && SP_PARM->newhash) {
1273 		for (row = top; row < screen_lines(SP_PARM); row++)
1274 		    SP_PARM->oldhash[row] = SP_PARM->newhash[row];
1275 	    }
1276 	}
1277     }
1278     return top;
1279 }
1280 
1281 #if USE_XMC_SUPPORT
1282 #if USE_WIDEC_SUPPORT
1283 #define check_xmc_transition(sp, a, b)					\
1284     ((((a)->attr ^ (b)->attr) & ~((a)->attr) & (sp)->_xmc_triggers) != 0)
1285 #define xmc_turn_on(sp,a,b) check_xmc_transition(sp,&(a), &(b))
1286 #else
1287 #define xmc_turn_on(sp,a,b) ((((a)^(b)) & ~(a) & (sp)->_xmc_triggers) != 0)
1288 #endif
1289 
1290 #define xmc_new(sp,r,c) NewScreen(sp)->_line[r].text[c]
1291 #define xmc_turn_off(sp,a,b) xmc_turn_on(sp,b,a)
1292 #endif /* USE_XMC_SUPPORT */
1293 
1294 /*
1295 **	TransformLine(lineno)
1296 **
1297 **	Transform the given line in curscr to the one in newscr, using
1298 **	Insert/Delete Character if idcok && has_ic().
1299 **
1300 **		firstChar = position of first different character in line
1301 **		oLastChar = position of last different character in old line
1302 **		nLastChar = position of last different character in new line
1303 **
1304 **		move to firstChar
1305 **		overwrite chars up to Min(oLastChar, nLastChar)
1306 **		if oLastChar < nLastChar
1307 **			insert newLine[oLastChar+1..nLastChar]
1308 **		else
1309 **			delete oLastChar - nLastChar spaces
1310 */
1311 
1312 static void
TransformLine(NCURSES_SP_DCLx int const lineno)1313 TransformLine(NCURSES_SP_DCLx int const lineno)
1314 {
1315     int firstChar, oLastChar, nLastChar;
1316     NCURSES_CH_T *newLine = NewScreen(SP_PARM)->_line[lineno].text;
1317     NCURSES_CH_T *oldLine = CurScreen(SP_PARM)->_line[lineno].text;
1318     int n;
1319     bool attrchanged = FALSE;
1320 
1321     TR(TRACE_UPDATE, (T_CALLED("TransformLine(%p, %d)"), (void *) SP_PARM, lineno));
1322 
1323     /* copy new hash value to old one */
1324     if (SP_PARM->oldhash && SP_PARM->newhash)
1325 	SP_PARM->oldhash[lineno] = SP_PARM->newhash[lineno];
1326 
1327     /*
1328      * If we have colors, there is the possibility of having two color pairs
1329      * that display as the same colors.  For instance, Lynx does this.  Check
1330      * for this case, and update the old line with the new line's colors when
1331      * they are equivalent.
1332      */
1333     if (SP_PARM->_coloron) {
1334 	int oldPair;
1335 	int newPair;
1336 
1337 	for (n = 0; n < screen_columns(SP_PARM); n++) {
1338 	    if (!CharEq(newLine[n], oldLine[n])) {
1339 		oldPair = GetPair(oldLine[n]);
1340 		newPair = GetPair(newLine[n]);
1341 		if (oldPair != newPair
1342 		    && unColor(oldLine[n]) == unColor(newLine[n])) {
1343 		    if (oldPair < SP_PARM->_pair_alloc
1344 			&& newPair < SP_PARM->_pair_alloc
1345 			&& (isSamePair(SP_PARM->_color_pairs[oldPair],
1346 				       SP_PARM->_color_pairs[newPair]))) {
1347 			SetPair(oldLine[n], GetPair(newLine[n]));
1348 		    }
1349 		}
1350 	    }
1351 	}
1352     }
1353 
1354     if (ceol_standout_glitch && clr_eol) {
1355 	firstChar = 0;
1356 	while (firstChar < screen_columns(SP_PARM)) {
1357 	    if (!SameAttrOf(newLine[firstChar], oldLine[firstChar])) {
1358 		attrchanged = TRUE;
1359 		break;
1360 	    }
1361 	    firstChar++;
1362 	}
1363     }
1364 
1365     firstChar = 0;
1366 
1367     if (attrchanged) {		/* we may have to disregard the whole line */
1368 	GoTo(NCURSES_SP_ARGx lineno, firstChar);
1369 	ClrToEOL(NCURSES_SP_ARGx
1370 		 ClrBlank(NCURSES_SP_ARGx
1371 			  CurScreen(SP_PARM)), FALSE);
1372 	PutRange(NCURSES_SP_ARGx
1373 		 oldLine, newLine, lineno, 0,
1374 		 screen_columns(SP_PARM) - 1);
1375 #if USE_XMC_SUPPORT
1376 
1377 	/*
1378 	 * This is a very simple loop to paint characters which may have the
1379 	 * magic cookie glitch embedded.  It doesn't know much about video
1380 	 * attributes which are continued from one line to the next.  It
1381 	 * assumes that we have filtered out requests for attribute changes
1382 	 * that do not get mapped to blank positions.
1383 	 *
1384 	 * FIXME: we are not keeping track of where we put the cookies, so this
1385 	 * will work properly only once, since we may overwrite a cookie in a
1386 	 * following operation.
1387 	 */
1388     } else if (magic_cookie_glitch > 0) {
1389 	GoTo(NCURSES_SP_ARGx lineno, firstChar);
1390 	for (n = 0; n < screen_columns(SP_PARM); n++) {
1391 	    int m = n + magic_cookie_glitch;
1392 
1393 	    /* check for turn-on:
1394 	     * If we are writing an attributed blank, where the
1395 	     * previous cell is not attributed.
1396 	     */
1397 	    if (ISBLANK(newLine[n])
1398 		&& ((n > 0
1399 		     && xmc_turn_on(SP_PARM, newLine[n - 1], newLine[n]))
1400 		    || (n == 0
1401 			&& lineno > 0
1402 			&& xmc_turn_on(SP_PARM,
1403 				       xmc_new(SP_PARM, lineno - 1,
1404 					       screen_columns(SP_PARM) - 1),
1405 				       newLine[n])))) {
1406 		n = m;
1407 	    }
1408 
1409 	    PutChar(NCURSES_SP_ARGx CHREF(newLine[n]));
1410 
1411 	    /* check for turn-off:
1412 	     * If we are writing an attributed non-blank, where the
1413 	     * next cell is blank, and not attributed.
1414 	     */
1415 	    if (!ISBLANK(newLine[n])
1416 		&& ((n + 1 < screen_columns(SP_PARM)
1417 		     && xmc_turn_off(SP_PARM, newLine[n], newLine[n + 1]))
1418 		    || (n + 1 >= screen_columns(SP_PARM)
1419 			&& lineno + 1 < screen_lines(SP_PARM)
1420 			&& xmc_turn_off(SP_PARM,
1421 					newLine[n],
1422 					xmc_new(SP_PARM, lineno + 1, 0))))) {
1423 		n = m;
1424 	    }
1425 
1426 	}
1427 #endif
1428     } else {
1429 	NCURSES_CH_T blank;
1430 
1431 	/* it may be cheap to clear leading whitespace with clr_bol */
1432 	blank = newLine[0];
1433 	if (clr_bol && can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1434 	    int oFirstChar, nFirstChar;
1435 
1436 	    for (oFirstChar = 0;
1437 		 oFirstChar < screen_columns(SP_PARM);
1438 		 oFirstChar++)
1439 		if (!CharEq(oldLine[oFirstChar], blank))
1440 		    break;
1441 	    for (nFirstChar = 0;
1442 		 nFirstChar < screen_columns(SP_PARM);
1443 		 nFirstChar++)
1444 		if (!CharEq(newLine[nFirstChar], blank))
1445 		    break;
1446 
1447 	    if (nFirstChar == oFirstChar) {
1448 		firstChar = nFirstChar;
1449 		/* find the first differing character */
1450 		while (firstChar < screen_columns(SP_PARM)
1451 		       && CharEq(newLine[firstChar], oldLine[firstChar]))
1452 		    firstChar++;
1453 	    } else if (oFirstChar > nFirstChar) {
1454 		firstChar = nFirstChar;
1455 	    } else {		/* oFirstChar < nFirstChar */
1456 		firstChar = oFirstChar;
1457 		if (SP_PARM->_el1_cost < nFirstChar - oFirstChar) {
1458 		    if (nFirstChar >= screen_columns(SP_PARM)
1459 			&& SP_PARM->_el_cost <= SP_PARM->_el1_cost) {
1460 			GoTo(NCURSES_SP_ARGx lineno, 0);
1461 			UpdateAttrs(SP_PARM, blank);
1462 			NCURSES_PUTP2("clr_eol", clr_eol);
1463 		    } else {
1464 			GoTo(NCURSES_SP_ARGx lineno, nFirstChar - 1);
1465 			UpdateAttrs(SP_PARM, blank);
1466 			NCURSES_PUTP2("clr_bol", clr_bol);
1467 		    }
1468 
1469 		    while (firstChar < nFirstChar)
1470 			oldLine[firstChar++] = blank;
1471 		}
1472 	    }
1473 	} else {
1474 	    /* find the first differing character */
1475 	    while (firstChar < screen_columns(SP_PARM)
1476 		   && CharEq(newLine[firstChar], oldLine[firstChar]))
1477 		firstChar++;
1478 	}
1479 	/* if there wasn't one, we're done */
1480 	if (firstChar >= screen_columns(SP_PARM)) {
1481 	    TR(TRACE_UPDATE, (T_RETURN("")));
1482 	    return;
1483 	}
1484 
1485 	blank = newLine[screen_columns(SP_PARM) - 1];
1486 
1487 	if (!can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1488 	    /* find the last differing character */
1489 	    nLastChar = screen_columns(SP_PARM) - 1;
1490 
1491 	    while (nLastChar > firstChar
1492 		   && CharEq(newLine[nLastChar], oldLine[nLastChar]))
1493 		nLastChar--;
1494 
1495 	    if (nLastChar >= firstChar) {
1496 		GoTo(NCURSES_SP_ARGx lineno, firstChar);
1497 		PutRange(NCURSES_SP_ARGx
1498 			 oldLine,
1499 			 newLine,
1500 			 lineno,
1501 			 firstChar,
1502 			 nLastChar);
1503 		memcpy(oldLine + firstChar,
1504 		       newLine + firstChar,
1505 		       (unsigned) (nLastChar - firstChar + 1) * sizeof(NCURSES_CH_T));
1506 	    }
1507 	    TR(TRACE_UPDATE, (T_RETURN("")));
1508 	    return;
1509 	}
1510 
1511 	/* find last non-blank character on old line */
1512 	oLastChar = screen_columns(SP_PARM) - 1;
1513 	while (oLastChar > firstChar && CharEq(oldLine[oLastChar], blank))
1514 	    oLastChar--;
1515 
1516 	/* find last non-blank character on new line */
1517 	nLastChar = screen_columns(SP_PARM) - 1;
1518 	while (nLastChar > firstChar && CharEq(newLine[nLastChar], blank))
1519 	    nLastChar--;
1520 
1521 	if ((nLastChar == firstChar)
1522 	    && (SP_PARM->_el_cost < (oLastChar - nLastChar))) {
1523 	    GoTo(NCURSES_SP_ARGx lineno, firstChar);
1524 	    if (!CharEq(newLine[firstChar], blank))
1525 		PutChar(NCURSES_SP_ARGx CHREF(newLine[firstChar]));
1526 	    ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1527 	} else if ((nLastChar != oLastChar)
1528 		   && (!CharEq(newLine[nLastChar], oldLine[oLastChar])
1529 		       || !(SP_PARM->_nc_sp_idcok
1530 			    && NCURSES_SP_NAME(has_ic) (NCURSES_SP_ARG)))) {
1531 	    GoTo(NCURSES_SP_ARGx lineno, firstChar);
1532 	    if ((oLastChar - nLastChar) > SP_PARM->_el_cost) {
1533 		if (PutRange(NCURSES_SP_ARGx
1534 			     oldLine,
1535 			     newLine,
1536 			     lineno,
1537 			     firstChar,
1538 			     nLastChar)) {
1539 		    GoTo(NCURSES_SP_ARGx lineno, nLastChar + 1);
1540 		}
1541 		ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1542 	    } else {
1543 		n = Max(nLastChar, oLastChar);
1544 		PutRange(NCURSES_SP_ARGx
1545 			 oldLine,
1546 			 newLine,
1547 			 lineno,
1548 			 firstChar,
1549 			 n);
1550 	    }
1551 	} else {
1552 	    int nLastNonblank = nLastChar;
1553 	    int oLastNonblank = oLastChar;
1554 
1555 	    /* find the last characters that really differ */
1556 	    /* can be -1 if no characters differ */
1557 	    while (CharEq(newLine[nLastChar], oldLine[oLastChar])) {
1558 		/* don't split a wide char */
1559 		if (isWidecExt(newLine[nLastChar]) &&
1560 		    !CharEq(newLine[nLastChar - 1], oldLine[oLastChar - 1]))
1561 		    break;
1562 		nLastChar--;
1563 		oLastChar--;
1564 		if (nLastChar == -1 || oLastChar == -1)
1565 		    break;
1566 	    }
1567 
1568 	    n = Min(oLastChar, nLastChar);
1569 	    if (n >= firstChar) {
1570 		GoTo(NCURSES_SP_ARGx lineno, firstChar);
1571 		PutRange(NCURSES_SP_ARGx
1572 			 oldLine,
1573 			 newLine,
1574 			 lineno,
1575 			 firstChar,
1576 			 n);
1577 	    }
1578 
1579 	    if (oLastChar < nLastChar) {
1580 		int m = Max(nLastNonblank, oLastNonblank);
1581 #if USE_WIDEC_SUPPORT
1582 		if (n) {
1583 		    while (isWidecExt(newLine[n + 1]) && n) {
1584 			--n;
1585 			--oLastChar;	/* increase cost */
1586 		    }
1587 		} else if (n >= firstChar &&
1588 			   isWidecBase(newLine[n])) {
1589 		    while (isWidecExt(newLine[n + 1])) {
1590 			++n;
1591 			++oLastChar;	/* decrease cost */
1592 		    }
1593 		}
1594 #endif
1595 		GoTo(NCURSES_SP_ARGx lineno, n + 1);
1596 		if ((nLastChar < nLastNonblank)
1597 		    || InsCharCost(SP_PARM, nLastChar - oLastChar) > (m - n)) {
1598 		    PutRange(NCURSES_SP_ARGx
1599 			     oldLine,
1600 			     newLine,
1601 			     lineno,
1602 			     n + 1,
1603 			     m);
1604 		} else {
1605 		    InsStr(NCURSES_SP_ARGx &newLine[n + 1], nLastChar - oLastChar);
1606 		}
1607 	    } else if (oLastChar > nLastChar) {
1608 		GoTo(NCURSES_SP_ARGx lineno, n + 1);
1609 		if (DelCharCost(SP_PARM, oLastChar - nLastChar)
1610 		    > SP_PARM->_el_cost + nLastNonblank - (n + 1)) {
1611 		    if (PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1612 				 n + 1, nLastNonblank)) {
1613 			GoTo(NCURSES_SP_ARGx lineno, nLastNonblank + 1);
1614 		    }
1615 		    ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1616 		} else {
1617 		    /*
1618 		     * The delete-char sequence will
1619 		     * effectively shift in blanks from the
1620 		     * right margin of the screen.  Ensure
1621 		     * that they are the right color by
1622 		     * setting the video attributes from
1623 		     * the last character on the row.
1624 		     */
1625 		    UpdateAttrs(SP_PARM, blank);
1626 		    DelChar(NCURSES_SP_ARGx oLastChar - nLastChar);
1627 		}
1628 	    }
1629 	}
1630     }
1631 
1632     /* update the code's internal representation */
1633     if (screen_columns(SP_PARM) > firstChar)
1634 	memcpy(oldLine + firstChar,
1635 	       newLine + firstChar,
1636 	       (unsigned) (screen_columns(SP_PARM) - firstChar) * sizeof(NCURSES_CH_T));
1637     TR(TRACE_UPDATE, (T_RETURN("")));
1638     return;
1639 }
1640 
1641 /*
1642 **	ClearScreen(blank)
1643 **
1644 **	Clear the physical screen and put cursor at home
1645 **
1646 */
1647 
1648 static void
ClearScreen(NCURSES_SP_DCLx NCURSES_CH_T blank)1649 ClearScreen(NCURSES_SP_DCLx NCURSES_CH_T blank)
1650 {
1651     int i, j;
1652     bool fast_clear = (clear_screen || clr_eos || clr_eol);
1653 
1654     TR(TRACE_UPDATE, ("ClearScreen() called"));
1655 
1656 #if NCURSES_EXT_FUNCS
1657     if (SP_PARM->_coloron
1658 	&& !SP_PARM->_default_color) {
1659 	NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx
1660 				       (short) GET_SCREEN_PAIR(SP_PARM),
1661 				       0,
1662 				       FALSE,
1663 				       NCURSES_SP_NAME(_nc_outch));
1664 	if (!back_color_erase) {
1665 	    fast_clear = FALSE;
1666 	}
1667     }
1668 #endif
1669 
1670     if (fast_clear) {
1671 	if (clear_screen) {
1672 	    UpdateAttrs(SP_PARM, blank);
1673 	    NCURSES_PUTP2("clear_screen", clear_screen);
1674 	    SP_PARM->_cursrow = SP_PARM->_curscol = 0;
1675 	    position_check(NCURSES_SP_ARGx
1676 			   SP_PARM->_cursrow,
1677 			   SP_PARM->_curscol,
1678 			   "ClearScreen");
1679 	} else if (clr_eos) {
1680 	    SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1681 	    GoTo(NCURSES_SP_ARGx 0, 0);
1682 	    UpdateAttrs(SP_PARM, blank);
1683 	    TPUTS_TRACE("clr_eos");
1684 	    NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1685 				    clr_eos,
1686 				    screen_lines(SP_PARM),
1687 				    NCURSES_SP_NAME(_nc_outch));
1688 	} else if (clr_eol) {
1689 	    SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1690 	    UpdateAttrs(SP_PARM, blank);
1691 	    for (i = 0; i < screen_lines(SP_PARM); i++) {
1692 		GoTo(NCURSES_SP_ARGx i, 0);
1693 		NCURSES_PUTP2("clr_eol", clr_eol);
1694 	    }
1695 	    GoTo(NCURSES_SP_ARGx 0, 0);
1696 	}
1697     } else {
1698 	UpdateAttrs(SP_PARM, blank);
1699 	for (i = 0; i < screen_lines(SP_PARM); i++) {
1700 	    GoTo(NCURSES_SP_ARGx i, 0);
1701 	    for (j = 0; j < screen_columns(SP_PARM); j++)
1702 		PutChar(NCURSES_SP_ARGx CHREF(blank));
1703 	}
1704 	GoTo(NCURSES_SP_ARGx 0, 0);
1705     }
1706 
1707     for (i = 0; i < screen_lines(SP_PARM); i++) {
1708 	for (j = 0; j < screen_columns(SP_PARM); j++)
1709 	    CurScreen(SP_PARM)->_line[i].text[j] = blank;
1710     }
1711 
1712     TR(TRACE_UPDATE, ("screen cleared"));
1713 }
1714 
1715 /*
1716 **	InsStr(line, count)
1717 **
1718 **	Insert the count characters pointed to by line.
1719 **
1720 */
1721 
1722 static void
InsStr(NCURSES_SP_DCLx NCURSES_CH_T * line,int count)1723 InsStr(NCURSES_SP_DCLx NCURSES_CH_T *line, int count)
1724 {
1725     TR(TRACE_UPDATE, ("InsStr(%p, %p,%d) called",
1726 		      (void *) SP_PARM,
1727 		      (void *) line, count));
1728 
1729     /* Prefer parm_ich as it has the smallest cost - no need to shift
1730      * the whole line on each character. */
1731     /* The order must match that of InsCharCost. */
1732     if (parm_ich) {
1733 	TPUTS_TRACE("parm_ich");
1734 	NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1735 				TIPARM_1(parm_ich, count),
1736 				1,
1737 				NCURSES_SP_NAME(_nc_outch));
1738 	while (count > 0) {
1739 	    PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1740 	    line++;
1741 	    count--;
1742 	}
1743     } else if (enter_insert_mode && exit_insert_mode) {
1744 	NCURSES_PUTP2("enter_insert_mode", enter_insert_mode);
1745 	while (count > 0) {
1746 	    PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1747 	    if (insert_padding) {
1748 		NCURSES_PUTP2("insert_padding", insert_padding);
1749 	    }
1750 	    line++;
1751 	    count--;
1752 	}
1753 	NCURSES_PUTP2("exit_insert_mode", exit_insert_mode);
1754     } else {
1755 	while (count > 0) {
1756 	    NCURSES_PUTP2("insert_character", insert_character);
1757 	    PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1758 	    if (insert_padding) {
1759 		NCURSES_PUTP2("insert_padding", insert_padding);
1760 	    }
1761 	    line++;
1762 	    count--;
1763 	}
1764     }
1765     position_check(NCURSES_SP_ARGx
1766 		   SP_PARM->_cursrow,
1767 		   SP_PARM->_curscol, "InsStr");
1768 }
1769 
1770 /*
1771 **	DelChar(count)
1772 **
1773 **	Delete count characters at current position
1774 **
1775 */
1776 
1777 static void
DelChar(NCURSES_SP_DCLx int count)1778 DelChar(NCURSES_SP_DCLx int count)
1779 {
1780     TR(TRACE_UPDATE, ("DelChar(%p, %d) called, position = (%ld,%ld)",
1781 		      (void *) SP_PARM, count,
1782 		      (long) NewScreen(SP_PARM)->_cury,
1783 		      (long) NewScreen(SP_PARM)->_curx));
1784 
1785     if (parm_dch) {
1786 	TPUTS_TRACE("parm_dch");
1787 	NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1788 				TIPARM_1(parm_dch, count),
1789 				1,
1790 				NCURSES_SP_NAME(_nc_outch));
1791     } else {
1792 	int n;
1793 
1794 	for (n = 0; n < count; n++) {
1795 	    NCURSES_PUTP2("delete_character", delete_character);
1796 	}
1797     }
1798 }
1799 
1800 /*
1801  * Physical-scrolling support
1802  *
1803  * This code was adapted from Keith Bostic's hardware scrolling
1804  * support for 4.4BSD curses.  I (esr) translated it to use terminfo
1805  * capabilities, narrowed the call interface slightly, and cleaned
1806  * up some convoluted tests.  I also added support for the memory_above
1807  * memory_below, and non_dest_scroll_region capabilities.
1808  *
1809  * For this code to work, we must have either
1810  * change_scroll_region and scroll forward/reverse commands, or
1811  * insert and delete line capabilities.
1812  * When the scrolling region has been set, the cursor has to
1813  * be at the last line of the region to make the scroll up
1814  * happen, or on the first line of region to scroll down.
1815  *
1816  * This code makes one aesthetic decision in the opposite way from
1817  * BSD curses.  BSD curses preferred pairs of il/dl operations
1818  * over scrolls, allegedly because il/dl looked faster.  We, on
1819  * the other hand, prefer scrolls because (a) they're just as fast
1820  * on many terminals and (b) using them avoids bouncing an
1821  * unchanged bottom section of the screen up and down, which is
1822  * visually nasty.
1823  *
1824  * (lav): added more cases, used dl/il when bot==maxy and in csr case.
1825  *
1826  * I used assumption that capabilities il/il1/dl/dl1 work inside
1827  * changed scroll region not shifting screen contents outside of it.
1828  * If there are any terminals behaving different way, it would be
1829  * necessary to add some conditions to scroll_csr_forward/backward.
1830  */
1831 
1832 /* Try to scroll up assuming given csr (miny, maxy). Returns ERR on failure */
1833 static int
scroll_csr_forward(NCURSES_SP_DCLx int n,int top,int bot,int miny,int maxy,NCURSES_CH_T blank)1834 scroll_csr_forward(NCURSES_SP_DCLx
1835 		   int n,
1836 		   int top,
1837 		   int bot,
1838 		   int miny,
1839 		   int maxy,
1840 		   NCURSES_CH_T blank)
1841 {
1842     int i;
1843 
1844     if (n == 1 && scroll_forward && top == miny && bot == maxy) {
1845 	GoTo(NCURSES_SP_ARGx bot, 0);
1846 	UpdateAttrs(SP_PARM, blank);
1847 	NCURSES_PUTP2("scroll_forward", scroll_forward);
1848     } else if (n == 1 && delete_line && bot == maxy) {
1849 	GoTo(NCURSES_SP_ARGx top, 0);
1850 	UpdateAttrs(SP_PARM, blank);
1851 	NCURSES_PUTP2("delete_line", delete_line);
1852     } else if (parm_index && top == miny && bot == maxy) {
1853 	GoTo(NCURSES_SP_ARGx bot, 0);
1854 	UpdateAttrs(SP_PARM, blank);
1855 	TPUTS_TRACE("parm_index");
1856 	NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1857 				TIPARM_1(parm_index, n),
1858 				n,
1859 				NCURSES_SP_NAME(_nc_outch));
1860     } else if (parm_delete_line && bot == maxy) {
1861 	GoTo(NCURSES_SP_ARGx top, 0);
1862 	UpdateAttrs(SP_PARM, blank);
1863 	TPUTS_TRACE("parm_delete_line");
1864 	NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1865 				TIPARM_1(parm_delete_line, n),
1866 				n,
1867 				NCURSES_SP_NAME(_nc_outch));
1868     } else if (scroll_forward && top == miny && bot == maxy) {
1869 	GoTo(NCURSES_SP_ARGx bot, 0);
1870 	UpdateAttrs(SP_PARM, blank);
1871 	for (i = 0; i < n; i++) {
1872 	    NCURSES_PUTP2("scroll_forward", scroll_forward);
1873 	}
1874     } else if (delete_line && bot == maxy) {
1875 	GoTo(NCURSES_SP_ARGx top, 0);
1876 	UpdateAttrs(SP_PARM, blank);
1877 	for (i = 0; i < n; i++) {
1878 	    NCURSES_PUTP2("delete_line", delete_line);
1879 	}
1880     } else
1881 	return ERR;
1882 
1883 #if NCURSES_EXT_FUNCS
1884     if (FILL_BCE(SP_PARM)) {
1885 	int j;
1886 	for (i = 0; i < n; i++) {
1887 	    GoTo(NCURSES_SP_ARGx bot - i, 0);
1888 	    for (j = 0; j < screen_columns(SP_PARM); j++)
1889 		PutChar(NCURSES_SP_ARGx CHREF(blank));
1890 	}
1891     }
1892 #endif
1893     return OK;
1894 }
1895 
1896 /* Try to scroll down assuming given csr (miny, maxy). Returns ERR on failure */
1897 /* n > 0 */
1898 static int
scroll_csr_backward(NCURSES_SP_DCLx int n,int top,int bot,int miny,int maxy,NCURSES_CH_T blank)1899 scroll_csr_backward(NCURSES_SP_DCLx
1900 		    int n,
1901 		    int top,
1902 		    int bot,
1903 		    int miny,
1904 		    int maxy,
1905 		    NCURSES_CH_T blank)
1906 {
1907     int i;
1908 
1909     if (n == 1 && scroll_reverse && top == miny && bot == maxy) {
1910 	GoTo(NCURSES_SP_ARGx top, 0);
1911 	UpdateAttrs(SP_PARM, blank);
1912 	NCURSES_PUTP2("scroll_reverse", scroll_reverse);
1913     } else if (n == 1 && insert_line && bot == maxy) {
1914 	GoTo(NCURSES_SP_ARGx top, 0);
1915 	UpdateAttrs(SP_PARM, blank);
1916 	NCURSES_PUTP2("insert_line", insert_line);
1917     } else if (parm_rindex && top == miny && bot == maxy) {
1918 	GoTo(NCURSES_SP_ARGx top, 0);
1919 	UpdateAttrs(SP_PARM, blank);
1920 	TPUTS_TRACE("parm_rindex");
1921 	NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1922 				TIPARM_1(parm_rindex, n),
1923 				n,
1924 				NCURSES_SP_NAME(_nc_outch));
1925     } else if (parm_insert_line && bot == maxy) {
1926 	GoTo(NCURSES_SP_ARGx top, 0);
1927 	UpdateAttrs(SP_PARM, blank);
1928 	TPUTS_TRACE("parm_insert_line");
1929 	NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1930 				TIPARM_1(parm_insert_line, n),
1931 				n,
1932 				NCURSES_SP_NAME(_nc_outch));
1933     } else if (scroll_reverse && top == miny && bot == maxy) {
1934 	GoTo(NCURSES_SP_ARGx top, 0);
1935 	UpdateAttrs(SP_PARM, blank);
1936 	for (i = 0; i < n; i++) {
1937 	    NCURSES_PUTP2("scroll_reverse", scroll_reverse);
1938 	}
1939     } else if (insert_line && bot == maxy) {
1940 	GoTo(NCURSES_SP_ARGx top, 0);
1941 	UpdateAttrs(SP_PARM, blank);
1942 	for (i = 0; i < n; i++) {
1943 	    NCURSES_PUTP2("insert_line", insert_line);
1944 	}
1945     } else
1946 	return ERR;
1947 
1948 #if NCURSES_EXT_FUNCS
1949     if (FILL_BCE(SP_PARM)) {
1950 	int j;
1951 	for (i = 0; i < n; i++) {
1952 	    GoTo(NCURSES_SP_ARGx top + i, 0);
1953 	    for (j = 0; j < screen_columns(SP_PARM); j++)
1954 		PutChar(NCURSES_SP_ARGx CHREF(blank));
1955 	}
1956     }
1957 #endif
1958     return OK;
1959 }
1960 
1961 /* scroll by using delete_line at del and insert_line at ins */
1962 /* n > 0 */
1963 static int
scroll_idl(NCURSES_SP_DCLx int n,int del,int ins,NCURSES_CH_T blank)1964 scroll_idl(NCURSES_SP_DCLx int n, int del, int ins, NCURSES_CH_T blank)
1965 {
1966     int i;
1967 
1968     if (!((parm_delete_line || delete_line) && (parm_insert_line || insert_line)))
1969 	return ERR;
1970 
1971     GoTo(NCURSES_SP_ARGx del, 0);
1972     UpdateAttrs(SP_PARM, blank);
1973     if (n == 1 && delete_line) {
1974 	NCURSES_PUTP2("delete_line", delete_line);
1975     } else if (parm_delete_line) {
1976 	TPUTS_TRACE("parm_delete_line");
1977 	NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1978 				TIPARM_1(parm_delete_line, n),
1979 				n,
1980 				NCURSES_SP_NAME(_nc_outch));
1981     } else {			/* if (delete_line) */
1982 	for (i = 0; i < n; i++) {
1983 	    NCURSES_PUTP2("delete_line", delete_line);
1984 	}
1985     }
1986 
1987     GoTo(NCURSES_SP_ARGx ins, 0);
1988     UpdateAttrs(SP_PARM, blank);
1989     if (n == 1 && insert_line) {
1990 	NCURSES_PUTP2("insert_line", insert_line);
1991     } else if (parm_insert_line) {
1992 	TPUTS_TRACE("parm_insert_line");
1993 	NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1994 				TIPARM_1(parm_insert_line, n),
1995 				n,
1996 				NCURSES_SP_NAME(_nc_outch));
1997     } else {			/* if (insert_line) */
1998 	for (i = 0; i < n; i++) {
1999 	    NCURSES_PUTP2("insert_line", insert_line);
2000 	}
2001     }
2002 
2003     return OK;
2004 }
2005 
2006 /*
2007  * Note:  some terminals require the cursor to be within the scrolling margins
2008  * before setting them.  Generally, the cursor must be at the appropriate end
2009  * of the scrolling margins when issuing an indexing operation (it is not
2010  * apparent whether it must also be at the left margin; we do this just to be
2011  * safe).  To make the related cursor movement a little faster, we use the
2012  * save/restore cursor capabilities if the terminal has them.
2013  */
2014 NCURSES_EXPORT(int)
NCURSES_SP_NAME(_nc_scrolln)2015 NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_DCLx
2016 			      int n,
2017 			      int top,
2018 			      int bot,
2019 			      int maxy)
2020 /* scroll region from top to bot by n lines */
2021 {
2022     NCURSES_CH_T blank;
2023     int i;
2024     bool cursor_saved = FALSE;
2025     int res;
2026 
2027     TR(TRACE_MOVE, ("_nc_scrolln(%p, %d, %d, %d, %d)",
2028 		    (void *) SP_PARM, n, top, bot, maxy));
2029 
2030     if (!IsValidScreen(SP_PARM))
2031 	return (ERR);
2032 
2033     blank = ClrBlank(NCURSES_SP_ARGx StdScreen(SP_PARM));
2034 
2035 #if USE_XMC_SUPPORT
2036     /*
2037      * If we scroll, we might remove a cookie.
2038      */
2039     if (magic_cookie_glitch > 0) {
2040 	return (ERR);
2041     }
2042 #endif
2043 
2044     if (n > 0) {		/* scroll up (forward) */
2045 	/*
2046 	 * Explicitly clear if stuff pushed off top of region might
2047 	 * be saved by the terminal.
2048 	 */
2049 	res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, 0, maxy, blank);
2050 
2051 	if (res == ERR && change_scroll_region) {
2052 	    if ((((n == 1 && scroll_forward) || parm_index)
2053 		 && (SP_PARM->_cursrow == bot || SP_PARM->_cursrow == bot - 1))
2054 		&& save_cursor && restore_cursor) {
2055 		cursor_saved = TRUE;
2056 		NCURSES_PUTP2("save_cursor", save_cursor);
2057 	    }
2058 	    NCURSES_PUTP2("change_scroll_region",
2059 			  TIPARM_2(change_scroll_region, top, bot));
2060 	    if (cursor_saved) {
2061 		NCURSES_PUTP2("restore_cursor", restore_cursor);
2062 	    } else {
2063 		SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2064 	    }
2065 
2066 	    res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, top, bot, blank);
2067 
2068 	    NCURSES_PUTP2("change_scroll_region",
2069 			  TIPARM_2(change_scroll_region, 0, maxy));
2070 	    SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2071 	}
2072 
2073 	if (res == ERR && SP_PARM->_nc_sp_idlok)
2074 	    res = scroll_idl(NCURSES_SP_ARGx n, top, bot - n + 1, blank);
2075 
2076 	/*
2077 	 * Clear the newly shifted-in text.
2078 	 */
2079 	if (res != ERR
2080 	    && (non_dest_scroll_region || (memory_below && bot == maxy))) {
2081 	    static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
2082 	    if (bot == maxy && clr_eos) {
2083 		GoTo(NCURSES_SP_ARGx bot - n + 1, 0);
2084 		ClrToEOS(NCURSES_SP_ARGx blank2);
2085 	    } else {
2086 		for (i = 0; i < n; i++) {
2087 		    GoTo(NCURSES_SP_ARGx bot - i, 0);
2088 		    ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
2089 		}
2090 	    }
2091 	}
2092 
2093     } else {			/* (n < 0) - scroll down (backward) */
2094 	res = scroll_csr_backward(NCURSES_SP_ARGx -n, top, bot, 0, maxy, blank);
2095 
2096 	if (res == ERR && change_scroll_region) {
2097 	    if (top != 0
2098 		&& (SP_PARM->_cursrow == top ||
2099 		    SP_PARM->_cursrow == top - 1)
2100 		&& save_cursor && restore_cursor) {
2101 		cursor_saved = TRUE;
2102 		NCURSES_PUTP2("save_cursor", save_cursor);
2103 	    }
2104 	    NCURSES_PUTP2("change_scroll_region",
2105 			  TIPARM_2(change_scroll_region, top, bot));
2106 	    if (cursor_saved) {
2107 		NCURSES_PUTP2("restore_cursor", restore_cursor);
2108 	    } else {
2109 		SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2110 	    }
2111 
2112 	    res = scroll_csr_backward(NCURSES_SP_ARGx
2113 				      -n, top, bot, top, bot, blank);
2114 
2115 	    NCURSES_PUTP2("change_scroll_region",
2116 			  TIPARM_2(change_scroll_region, 0, maxy));
2117 	    SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2118 	}
2119 
2120 	if (res == ERR && SP_PARM->_nc_sp_idlok)
2121 	    res = scroll_idl(NCURSES_SP_ARGx -n, bot + n + 1, top, blank);
2122 
2123 	/*
2124 	 * Clear the newly shifted-in text.
2125 	 */
2126 	if (res != ERR
2127 	    && (non_dest_scroll_region || (memory_above && top == 0))) {
2128 	    static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
2129 	    for (i = 0; i < -n; i++) {
2130 		GoTo(NCURSES_SP_ARGx i + top, 0);
2131 		ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
2132 	    }
2133 	}
2134     }
2135 
2136     if (res == ERR)
2137 	return (ERR);
2138 
2139     _nc_scroll_window(CurScreen(SP_PARM), n,
2140 		      (NCURSES_SIZE_T) top,
2141 		      (NCURSES_SIZE_T) bot,
2142 		      blank);
2143 
2144     /* shift hash values too - they can be reused */
2145     NCURSES_SP_NAME(_nc_scroll_oldhash) (NCURSES_SP_ARGx n, top, bot);
2146 
2147     return (OK);
2148 }
2149 
2150 #if NCURSES_SP_FUNCS
2151 NCURSES_EXPORT(int)
_nc_scrolln(int n,int top,int bot,int maxy)2152 _nc_scrolln(int n, int top, int bot, int maxy)
2153 {
2154     return NCURSES_SP_NAME(_nc_scrolln) (CURRENT_SCREEN, n, top, bot, maxy);
2155 }
2156 #endif
2157 
2158 NCURSES_EXPORT(void)
NCURSES_SP_NAME(_nc_screen_resume)2159 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_DCL0)
2160 {
2161     assert(SP_PARM);
2162 
2163     /* make sure terminal is in a sane known state */
2164     SetAttr(SCREEN_ATTRS(SP_PARM), A_NORMAL);
2165     NewScreen(SP_PARM)->_clear = TRUE;
2166 
2167     /* reset color pairs and definitions */
2168     if (SP_PARM->_coloron || SP_PARM->_color_defs)
2169 	NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_ARG);
2170 
2171     /* restore user-defined colors, if any */
2172     if (SP_PARM->_color_defs < 0 && !SP_PARM->_direct_color.value) {
2173 	int n;
2174 	SP_PARM->_color_defs = -(SP_PARM->_color_defs);
2175 	for (n = 0; n < SP_PARM->_color_defs; ++n) {
2176 	    if (SP_PARM->_color_table[n].init) {
2177 		_nc_init_color(SP_PARM,
2178 			       n,
2179 			       SP_PARM->_color_table[n].r,
2180 			       SP_PARM->_color_table[n].g,
2181 			       SP_PARM->_color_table[n].b);
2182 	    }
2183 	}
2184     }
2185 
2186     if (exit_attribute_mode)
2187 	NCURSES_PUTP2("exit_attribute_mode", exit_attribute_mode);
2188     else {
2189 	/* turn off attributes */
2190 	if (exit_alt_charset_mode)
2191 	    NCURSES_PUTP2("exit_alt_charset_mode", exit_alt_charset_mode);
2192 	if (exit_standout_mode)
2193 	    NCURSES_PUTP2("exit_standout_mode", exit_standout_mode);
2194 	if (exit_underline_mode)
2195 	    NCURSES_PUTP2("exit_underline_mode", exit_underline_mode);
2196     }
2197     if (exit_insert_mode)
2198 	NCURSES_PUTP2("exit_insert_mode", exit_insert_mode);
2199     if (enter_am_mode && exit_am_mode) {
2200 	if (auto_right_margin) {
2201 	    NCURSES_PUTP2("enter_am_mode", enter_am_mode);
2202 	} else {
2203 	    NCURSES_PUTP2("exit_am_mode", exit_am_mode);
2204 	}
2205     }
2206 }
2207 
2208 #if NCURSES_SP_FUNCS
2209 NCURSES_EXPORT(void)
_nc_screen_resume(void)2210 _nc_screen_resume(void)
2211 {
2212     NCURSES_SP_NAME(_nc_screen_resume) (CURRENT_SCREEN);
2213 }
2214 #endif
2215 
2216 NCURSES_EXPORT(void)
NCURSES_SP_NAME(_nc_screen_init)2217 NCURSES_SP_NAME(_nc_screen_init) (NCURSES_SP_DCL0)
2218 {
2219     NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_ARG);
2220 }
2221 
2222 #if NCURSES_SP_FUNCS
2223 NCURSES_EXPORT(void)
_nc_screen_init(void)2224 _nc_screen_init(void)
2225 {
2226     NCURSES_SP_NAME(_nc_screen_init) (CURRENT_SCREEN);
2227 }
2228 #endif
2229 
2230 /* wrap up screen handling */
2231 NCURSES_EXPORT(void)
NCURSES_SP_NAME(_nc_screen_wrap)2232 NCURSES_SP_NAME(_nc_screen_wrap) (NCURSES_SP_DCL0)
2233 {
2234     if (SP_PARM != NULL) {
2235 
2236 	UpdateAttrs(SP_PARM, normal);
2237 #if NCURSES_EXT_FUNCS
2238 	if (SP_PARM->_coloron
2239 	    && !SP_PARM->_default_color) {
2240 	    static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
2241 	    SP_PARM->_default_color = TRUE;
2242 	    NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx
2243 					   -1,
2244 					   0,
2245 					   FALSE,
2246 					   NCURSES_SP_NAME(_nc_outch));
2247 	    SP_PARM->_default_color = FALSE;
2248 
2249 	    TINFO_MVCUR(NCURSES_SP_ARGx
2250 			SP_PARM->_cursrow,
2251 			SP_PARM->_curscol,
2252 			screen_lines(SP_PARM) - 1,
2253 			0);
2254 
2255 	    ClrToEOL(NCURSES_SP_ARGx blank, TRUE);
2256 	}
2257 #endif
2258 	if (SP_PARM->_color_defs) {
2259 	    NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_ARG);
2260 	}
2261     }
2262 }
2263 
2264 #if NCURSES_SP_FUNCS
2265 NCURSES_EXPORT(void)
_nc_screen_wrap(void)2266 _nc_screen_wrap(void)
2267 {
2268     NCURSES_SP_NAME(_nc_screen_wrap) (CURRENT_SCREEN);
2269 }
2270 #endif
2271 
2272 #if USE_XMC_SUPPORT
2273 NCURSES_EXPORT(void)
NCURSES_SP_NAME(_nc_do_xmc_glitch)2274 NCURSES_SP_NAME(_nc_do_xmc_glitch) (NCURSES_SP_DCLx attr_t previous)
2275 {
2276     if (SP_PARM != NULL) {
2277 	attr_t chg = XMC_CHANGES(previous ^ AttrOf(SCREEN_ATTRS(SP_PARM)));
2278 
2279 	while (chg != 0) {
2280 	    if (chg & 1) {
2281 		SP_PARM->_curscol += magic_cookie_glitch;
2282 		if (SP_PARM->_curscol >= SP_PARM->_columns)
2283 		    wrap_cursor(NCURSES_SP_ARG);
2284 		TR(TRACE_UPDATE, ("bumped to %d,%d after cookie",
2285 				  SP_PARM->_cursrow, SP_PARM->_curscol));
2286 	    }
2287 	    chg >>= 1;
2288 	}
2289     }
2290 }
2291 
2292 #if NCURSES_SP_FUNCS
2293 NCURSES_EXPORT(void)
_nc_do_xmc_glitch(attr_t previous)2294 _nc_do_xmc_glitch(attr_t previous)
2295 {
2296     NCURSES_SP_NAME(_nc_do_xmc_glitch) (CURRENT_SCREEN, previous);
2297 }
2298 #endif
2299 
2300 #endif /* USE_XMC_SUPPORT */
2301