xref: /netbsd-src/lib/libcurses/refresh.c (revision 0920b4f20b78ab1ccd9f2312fbe10deaf000cbf3)
1 /*	$NetBSD: refresh.c,v 1.67 2007/05/29 19:07:19 veego Exp $	*/
2 
3 /*
4  * Copyright (c) 1981, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)refresh.c	8.7 (Berkeley) 8/13/94";
36 #else
37 __RCSID("$NetBSD: refresh.c,v 1.67 2007/05/29 19:07:19 veego Exp $");
38 #endif
39 #endif				/* not lint */
40 
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "curses.h"
45 #include "curses_private.h"
46 
47 static void	domvcur(int, int, int, int);
48 static int	makech(int);
49 static void	quickch(void);
50 static void	scrolln(int, int, int, int, int);
51 
52 static int _cursesi_wnoutrefresh(SCREEN *, WINDOW *,
53 				 int, int, int, int, int, int);
54 
55 #ifdef HAVE_WCHAR
56 int cellcmp( __LDATA *, __LDATA * );
57 int linecmp( __LDATA *, __LDATA *, size_t );
58 #endif /* HAVE_WCHAR */
59 
60 #ifndef _CURSES_USE_MACROS
61 
62 /*
63  * refresh --
64  *	Make the current screen look like "stdscr" over the area covered by
65  *	stdscr.
66  */
67 int
68 refresh(void)
69 {
70 	return wrefresh(stdscr);
71 }
72 
73 #endif
74 
75 /*
76  * wnoutrefresh --
77  *	Add the contents of "win" to the virtual window.
78  */
79 int
80 wnoutrefresh(WINDOW *win)
81 {
82 #ifdef DEBUG
83 	__CTRACE(__CTRACE_REFRESH, "wnoutrefresh: win %p\n", win);
84 #endif
85 
86 	return _cursesi_wnoutrefresh(_cursesi_screen, win, 0, 0, win->begy,
87 	    win->begx, win->maxy, win->maxx);
88 }
89 
90 /*
91  * pnoutrefresh --
92  *	Add the contents of "pad" to the virtual window.
93  */
94 int
95 pnoutrefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
96 	int smaxy, int smaxx)
97 {
98 	int pmaxy, pmaxx;
99 
100 #ifdef DEBUG
101 	__CTRACE(__CTRACE_REFRESH, "pnoutrefresh: pad %p, flags 0x%08x\n",
102 	    pad, pad->flags);
103 	__CTRACE(__CTRACE_REFRESH,
104 	    "pnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n",
105 	    pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
106 #endif
107 
108 	/* SUS says if these are negative, they should be treated as zero */
109 	if (pbegy < 0)
110 		pbegy = 0;
111 	if (pbegx < 0)
112 		pbegx = 0;
113 	if (sbegy < 0)
114 		sbegy = 0;
115 	if (sbegx < 0)
116 		sbegx = 0;
117 
118 	/* Calculate rectangle on pad - used by _cursesi_wnoutrefresh */
119 	pmaxy = pbegy + smaxy - sbegy + 1;
120 	pmaxx = pbegx + smaxx - sbegx + 1;
121 
122 	/* Check rectangle fits in pad */
123 	if (pmaxy > pad->maxy - pad->begy)
124 		pmaxy = pad->maxy - pad->begy;
125 	if (pmaxx > pad->maxx - pad->begx)
126 		pmaxx = pad->maxx - pad->begx;
127 
128 	if (smaxy - sbegy < 0 || smaxx - sbegx < 0 )
129 		return ERR;
130 
131 	return _cursesi_wnoutrefresh(_cursesi_screen, pad,
132 	    pad->begy + pbegy, pad->begx + pbegx, pad->begy + sbegy,
133 	    pad->begx + sbegx, pmaxy, pmaxx);
134 }
135 
136 /*
137  * _cursesi_wnoutrefresh --
138  *	Does the grunt work for wnoutrefresh to the given screen.
139  *	Copies the part of the window given by the rectangle
140  *	(begy, begx) to (maxy, maxx) at screen position (wbegy, wbegx).
141  */
142 int
143 _cursesi_wnoutrefresh(SCREEN *screen, WINDOW *win, int begy, int begx,
144 	int wbegy, int wbegx, int maxy, int maxx)
145 {
146 
147 	short	sy, wy, wx, y_off, x_off, mx;
148 	__LINE	*wlp, *vlp;
149 	WINDOW	*sub_win, *orig;
150 
151 #ifdef DEBUG
152 	__CTRACE(__CTRACE_REFRESH, "_wnoutrefresh: win %p, flags 0x%08x\n",
153 	    win, win->flags);
154 	__CTRACE(__CTRACE_REFRESH,
155 	    "_wnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n",
156 	    begy, begx, wbegy, wbegx, maxy, maxx);
157 #endif
158 
159 	if (screen->curwin)
160 		return OK;
161 
162 	/*
163 	 * Recurse through any sub-windows, mark as dirty lines on the parent
164 	 * window that are dirty on the sub-window and clear the dirty flag on
165 	 * the sub-window.
166 	 */
167 	if (win->orig == 0) {
168 		orig = win;
169 		for (sub_win = win->nextp; sub_win != win;
170 		    sub_win = sub_win->nextp) {
171 #ifdef DEBUG
172 			__CTRACE(__CTRACE_REFRESH,
173 			    "wnout_refresh: win %p, sub_win %p\n",
174 			    orig, sub_win);
175 #endif
176 			for (sy = 0; sy < sub_win->maxy; sy++) {
177 				if (sub_win->lines[sy]->flags == __ISDIRTY) {
178 					orig->lines[sy + sub_win->begy - orig->begy]->flags
179 					    |= __ISDIRTY;
180 					sub_win->lines[sy]->flags
181 					    &= ~__ISDIRTY;
182 				}
183 			}
184 		}
185 	}
186 
187 	/* Check that cursor position on "win" is valid for "__virtscr" */
188 	if (win->cury + wbegy - begy < screen->__virtscr->maxy &&
189 	    win->cury + wbegy - begy >= 0 && win->cury < maxy - begy)
190 		screen->__virtscr->cury = win->cury + wbegy - begy;
191 	if (win->curx + wbegx - begx < screen->__virtscr->maxx &&
192 	    win->curx + wbegx - begx >= 0 && win->curx < maxx - begx)
193 		screen->__virtscr->curx = win->curx + wbegx - begx;
194 
195 	/* Copy the window flags from "win" to "__virtscr" */
196 	if (win->flags & __CLEAROK) {
197 		if (win->flags & __FULLWIN)
198 			screen->__virtscr->flags |= __CLEAROK;
199 		win->flags &= ~__CLEAROK;
200 	}
201 	screen->__virtscr->flags &= ~__LEAVEOK;
202 	screen->__virtscr->flags |= win->flags;
203 
204 	for (wy = begy, y_off = wbegy; wy < maxy &&
205 	    y_off < screen->__virtscr->maxy; wy++, y_off++) {
206 		wlp = win->lines[wy];
207 #ifdef DEBUG
208 		__CTRACE(__CTRACE_REFRESH,
209 		    "_wnoutrefresh: wy %d\tf %d\tl %d\tflags %x\n",
210 		    wy, *wlp->firstchp, *wlp->lastchp, wlp->flags);
211 #endif
212 		if ((wlp->flags & __ISDIRTY) == 0)
213 			continue;
214 		vlp = screen->__virtscr->lines[y_off];
215 
216 		if (*wlp->firstchp < maxx + win->ch_off &&
217 		    *wlp->lastchp >= win->ch_off) {
218 			/* Set start column */
219 			wx = begx;
220 			x_off = wbegx;
221 			if (*wlp->firstchp - win->ch_off > 0) {
222 				wx += *wlp->firstchp - win->ch_off;
223 				x_off += *wlp->firstchp - win->ch_off;
224 			}
225 			/* Set finish column */
226 			mx = maxx;
227 			if (mx > *wlp->lastchp - win->ch_off + 1)
228 				mx = *wlp->lastchp - win->ch_off + 1;
229 			if (x_off + (mx - wx) > __virtscr->maxx)
230 				mx -= (x_off + maxx) - __virtscr->maxx;
231 			/* Copy line from "win" to "__virtscr". */
232 			while (wx < mx) {
233 #ifdef DEBUG
234 				__CTRACE(__CTRACE_REFRESH,
235 				    "_wnoutrefresh: copy from %d, "
236 				    "%d to %d, %d\n",
237 				    wy, wx, y_off, x_off);
238 #endif
239 				/* Copy character */
240 				vlp->line[x_off].ch = wlp->line[wx].ch;
241 				/* Copy attributes  */
242 				vlp->line[x_off].attr = wlp->line[wx].attr;
243 				/* Check for nca conflict with colour */
244 				if ((vlp->line[x_off].attr & __COLOR) &&
245 				    (vlp->line[x_off].attr &
246 				    _cursesi_screen->nca))
247 					vlp->line[x_off].attr &= ~__COLOR;
248 #ifdef HAVE_WCHAR
249 				if (wlp->line[wx].ch
250 				    == (wchar_t)btowc((int) win->bch)) {
251 					vlp->line[x_off].ch = win->bch;
252 					SET_WCOL( vlp->line[x_off], 1 );
253 					if (_cursesi_copy_nsp(win->bnsp,
254 							      &vlp->line[x_off])
255 					    == ERR)
256 						return ERR;
257 				}
258 #endif /* HAVE_WCHAR */
259 				wx++;
260 				x_off++;
261 			}
262 
263 			/* Set flags on "__virtscr" and unset on "win". */
264 			if (wlp->flags & __ISPASTEOL)
265 				vlp->flags |= __ISPASTEOL;
266 			else
267 				vlp->flags &= ~__ISPASTEOL;
268 			if (wlp->flags & __ISDIRTY)
269 				vlp->flags |= __ISDIRTY;
270 
271 #ifdef DEBUG
272 			__CTRACE(__CTRACE_REFRESH,
273 			    "win: firstch = %d, lastch = %d\n",
274 			    *wlp->firstchp, *wlp->lastchp);
275 #endif
276 			/* Set change pointers on "__virtscr". */
277 			if (*vlp->firstchp >
278 			    *wlp->firstchp + wbegx - win->ch_off)
279 				*vlp->firstchp =
280 				    *wlp->firstchp + wbegx - win->ch_off;
281 			if (*vlp->lastchp <
282 			    *wlp->lastchp + wbegx - win->ch_off)
283 				*vlp->lastchp =
284 				    *wlp->lastchp + wbegx - win->ch_off;
285 #ifdef DEBUG
286 			__CTRACE(__CTRACE_REFRESH,
287 			    "__virtscr: firstch = %d, lastch = %d\n",
288 			    *vlp->firstchp, *vlp->lastchp);
289 #endif
290 			/*
291 			 * Unset change pointers only if a window, as a pad
292 			 * can be displayed again without any of the contents
293 			 * changing.
294 			 */
295 			if (!(win->flags & __ISPAD)) {
296 				/* Set change pointers on "win". */
297 				if (*wlp->firstchp >= win->ch_off)
298 					*wlp->firstchp = maxx + win->ch_off;
299 				if (*wlp->lastchp < maxx + win->ch_off)
300 					*wlp->lastchp = win->ch_off;
301 				if ((*wlp->lastchp < *wlp->firstchp) ||
302 				    (*wlp->firstchp >= maxx + win->ch_off) ||
303 				    (*wlp->lastchp <= win->ch_off)) {
304 #ifdef DEBUG
305 					__CTRACE(__CTRACE_REFRESH,
306 					    "_wnoutrefresh: "
307 					    "line %d notdirty\n", wy);
308 #endif
309 					wlp->flags &= ~__ISDIRTY;
310 				}
311 			}
312 		}
313 	}
314 	return OK;
315 }
316 
317 /*
318  * wrefresh --
319  *	Make the current screen look like "win" over the area covered by
320  *	win.
321  */
322 int
323 wrefresh(WINDOW *win)
324 {
325 	int retval;
326 
327 #ifdef DEBUG
328 	__CTRACE(__CTRACE_REFRESH, "wrefresh: win %p\n", win);
329 #endif
330 
331 	_cursesi_screen->curwin = (win == _cursesi_screen->curscr);
332 	if (!_cursesi_screen->curwin)
333 		retval = _cursesi_wnoutrefresh(_cursesi_screen, win, 0, 0,
334 		    win->begy, win->begx, win->maxy, win->maxx);
335 	else
336 		retval = OK;
337 	if (retval == OK) {
338 		retval = doupdate();
339 		if (!(win->flags & __LEAVEOK)) {
340 			win->cury = max(0, curscr->cury - win->begy);
341 			win->curx = max(0, curscr->curx - win->begx);
342 		}
343 	}
344 	_cursesi_screen->curwin = 0;
345 	return(retval);
346 }
347 
348  /*
349  * prefresh --
350  *	Make the current screen look like "pad" over the area coverd by
351  *	the specified area of pad.
352  */
353 int
354 prefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
355 	int smaxy, int smaxx)
356 {
357 	int retval;
358 
359 #ifdef DEBUG
360 	__CTRACE(__CTRACE_REFRESH, "prefresh: pad %p, flags 0x%08x\n",
361 	    pad, pad->flags);
362 #endif
363 	/* Retain values in case pechochar() is called. */
364 	pad->pbegy = pbegy;
365 	pad->pbegx = pbegx;
366 	pad->sbegy = sbegy;
367 	pad->sbegx = sbegx;
368 	pad->smaxy = smaxy;
369 	pad->smaxx = smaxx;
370 
371 	/* Use pnoutrefresh() to avoid duplicating code here */
372 	retval = pnoutrefresh(pad, pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
373 	if (retval == OK) {
374 		retval = doupdate();
375 		if (!(pad->flags & __LEAVEOK)) {
376 			pad->cury = max(0, curscr->cury - pad->begy);
377 			pad->curx = max(0, curscr->curx - pad->begx);
378 		}
379 	}
380 	return(retval);
381 }
382 
383 /*
384  * doupdate --
385  *	Make the current screen look like the virtual window "__virtscr".
386  */
387 int
388 doupdate(void)
389 {
390 	WINDOW	*win;
391 	__LINE	*wlp;
392 	short	 wy;
393 	int	 dnum;
394 #ifdef HAVE_WCHAR
395 	__LDATA *lp;
396 	nschar_t *np;
397 	int x;
398 #endif /* HAVE_WCHAR */
399 
400 	/* Check if we need to restart ... */
401 	if (_cursesi_screen->endwin)
402 		__restartwin();
403 
404 	if (_cursesi_screen->curwin)
405 		win = curscr;
406 	else
407 		win = _cursesi_screen->__virtscr;
408 
409 	/* Initialize loop parameters. */
410 	_cursesi_screen->ly = curscr->cury;
411 	_cursesi_screen->lx = curscr->curx;
412 	wy = 0;
413 
414 	if (!_cursesi_screen->curwin) {
415 		for (wy = 0; wy < win->maxy; wy++) {
416 			wlp = win->lines[wy];
417 			if (wlp->flags & __ISDIRTY) {
418 #ifndef HAVE_WCHAR
419 				wlp->hash = __hash(wlp->line,
420 				    (size_t)(win->maxx * __LDATASIZE));
421 #else
422 				wlp->hash = 0;
423 				for ( x = 0; x < win->maxx; x++ ) {
424 					lp = &wlp->line[ x ];
425 					wlp->hash = __hash_more( &lp->ch,
426 						sizeof( wchar_t ), wlp->hash );
427 					wlp->hash = __hash_more( &lp->attr,
428 						sizeof( attr_t ), wlp->hash );
429 					np = lp->nsp;
430 					if (np) {
431 						while ( np ) {
432 							wlp->hash
433 							    = __hash_more(
434 								&np->ch,
435 								sizeof(wchar_t),
436 								wlp->hash );
437 							np = np->next;
438 						}
439 					}
440 				}
441 #endif /* HAVE_WCHAR */
442 			}
443 		}
444 	}
445 
446 	if ((win->flags & __CLEAROK) || (curscr->flags & __CLEAROK) ||
447 	    _cursesi_screen->curwin) {
448 		if (curscr->wattr & __COLOR)
449 			__unsetattr(0);
450 		tputs(__tc_cl, 0, __cputchar);
451 		_cursesi_screen->ly = 0;
452 		_cursesi_screen->lx = 0;
453 		if (!_cursesi_screen->curwin) {
454 			curscr->flags &= ~__CLEAROK;
455 			curscr->cury = 0;
456 			curscr->curx = 0;
457 			werase(curscr);
458 		}
459 		__touchwin(win);
460 		win->flags &= ~__CLEAROK;
461 	}
462 	if (!__CA) {
463 		if (win->curx != 0)
464 			__cputchar('\n');
465 		if (!_cursesi_screen->curwin)
466 			werase(curscr);
467 	}
468 #ifdef DEBUG
469 	__CTRACE(__CTRACE_REFRESH, "doupdate: (%p): curwin = %d\n", win,
470 	    _cursesi_screen->curwin);
471 	__CTRACE(__CTRACE_REFRESH, "doupdate: \tfirstch\tlastch\n");
472 #endif
473 
474 	if (!_cursesi_screen->curwin) {
475 		/*
476 		 * Invoke quickch() only if more than a quarter of the lines
477 		 * in the window are dirty.
478 		 */
479 		for (wy = 0, dnum = 0; wy < win->maxy; wy++)
480 			if (win->lines[wy]->flags & __ISDIRTY)
481 				dnum++;
482 		if (!__noqch && dnum > (int) win->maxy / 4)
483 			quickch();
484 	}
485 
486 #ifdef DEBUG
487 	{
488 		int	i, j;
489 
490 		__CTRACE(__CTRACE_REFRESH,
491 		    "#####################################\n");
492 		__CTRACE(__CTRACE_REFRESH,
493 		    "stdscr(%p)-curscr(%p)-__virtscr(%p)\n",
494 		    stdscr, curscr, _cursesi_screen->__virtscr);
495 		for (i = 0; i < curscr->maxy; i++) {
496 			__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
497 			__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
498 			    curscr->lines[i]->hash);
499 			for (j = 0; j < curscr->maxx; j++)
500 				__CTRACE(__CTRACE_REFRESH, "%c",
501 				    curscr->lines[i]->line[j].ch);
502 			__CTRACE(__CTRACE_REFRESH, "\n");
503 			__CTRACE(__CTRACE_REFRESH, " attr:");
504 			for (j = 0; j < curscr->maxx; j++)
505 				__CTRACE(__CTRACE_REFRESH, " %x",
506 				    curscr->lines[i]->line[j].attr);
507 			__CTRACE(__CTRACE_REFRESH, "\n");
508 			__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
509 			__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
510 			    win->lines[i]->hash);
511 			__CTRACE(__CTRACE_REFRESH, " 0x%x ",
512 			    win->lines[i]->flags);
513 			for (j = 0; j < win->maxx; j++)
514 				__CTRACE(__CTRACE_REFRESH, "%c",
515 				    win->lines[i]->line[j].ch);
516 			__CTRACE(__CTRACE_REFRESH, "\n");
517 			__CTRACE(__CTRACE_REFRESH, " attr:");
518 			for (j = 0; j < win->maxx; j++)
519 				__CTRACE(__CTRACE_REFRESH, " %x",
520 				    win->lines[i]->line[j].attr);
521 			__CTRACE(__CTRACE_REFRESH, "\n");
522 #ifdef HAVE_WCHAR
523 			__CTRACE(__CTRACE_REFRESH, " nsp:");
524 			for (j = 0; j < curscr->maxx; j++)
525 				__CTRACE(__CTRACE_REFRESH, " %p",
526 				    win->lines[i]->line[j].nsp);
527 			__CTRACE(__CTRACE_REFRESH, "\n");
528 			__CTRACE(__CTRACE_REFRESH, " bnsp:");
529 			for (j = 0; j < curscr->maxx; j++)
530 				__CTRACE(__CTRACE_REFRESH, " %p",
531 				    win->bnsp);
532 			__CTRACE(__CTRACE_REFRESH, "\n");
533 #endif /* HAVE_WCHAR */
534 		}
535 	}
536 #endif /* DEBUG */
537 
538 	for (wy = 0; wy < win->maxy; wy++) {
539 		wlp = win->lines[wy];
540 /* XXX: remove this debug */
541 #ifdef DEBUG
542 		__CTRACE(__CTRACE_REFRESH,
543 		    "doupdate: wy %d\tf: %d\tl:%d\tflags %x\n",
544 		    wy, *wlp->firstchp, *wlp->lastchp, wlp->flags);
545 #endif /* DEBUG */
546 		if (!_cursesi_screen->curwin)
547 			curscr->lines[wy]->hash = wlp->hash;
548 		if (wlp->flags & __ISDIRTY) {
549 #ifdef DEBUG
550 			__CTRACE(__CTRACE_REFRESH,
551 			    "doupdate: [ISDIRTY]wy:%d\tf:%d\tl:%d\n", wy,
552 			    *wlp->firstchp, *wlp->lastchp);
553 #endif /* DEBUG */
554 			if (makech(wy) == ERR)
555 				return (ERR);
556 			else {
557 				if (*wlp->firstchp >= 0)
558 					*wlp->firstchp = win->maxx;
559 				if (*wlp->lastchp < win->maxx)
560 					*wlp->lastchp = 0;
561 				if (*wlp->lastchp < *wlp->firstchp) {
562 #ifdef DEBUG
563 					__CTRACE(__CTRACE_REFRESH,
564 					    "doupdate: line %d notdirty\n", wy);
565 #endif /* DEBUG */
566 					wlp->flags &= ~__ISDIRTY;
567 				}
568 			}
569 
570 		}
571 #ifdef DEBUG
572 		__CTRACE(__CTRACE_REFRESH, "\t%d\t%d\n",
573 		    *wlp->firstchp, *wlp->lastchp);
574 #endif /* DEBUG */
575 	}
576 
577 #ifdef DEBUG
578 	__CTRACE(__CTRACE_REFRESH, "doupdate: ly=%d, lx=%d\n",
579 	    _cursesi_screen->ly, _cursesi_screen->lx);
580 #endif /* DEBUG */
581 
582 	if (_cursesi_screen->curwin)
583 		domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
584 			(int) win->cury, (int) win->curx);
585 	else {
586 		if (win->flags & __LEAVEOK) {
587 			curscr->cury = _cursesi_screen->ly;
588 			curscr->curx = _cursesi_screen->lx;
589 		} else {
590 			domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
591 				win->cury, win->curx);
592 			curscr->cury = win->cury;
593 			curscr->curx = win->curx;
594 		}
595 	}
596 
597 	/* Don't leave the screen with attributes set. */
598 	__unsetattr(0);
599 #ifdef DEBUG
600 #ifdef HAVE_WCHAR
601 	{
602 		int	i, j;
603 
604 		__CTRACE(__CTRACE_REFRESH,
605 		    "***********after*****************\n");
606 		__CTRACE(__CTRACE_REFRESH,
607 		    "stdscr(%p)-curscr(%p)-__virtscr(%p)\n",
608 		    stdscr, curscr, _cursesi_screen->__virtscr);
609 		for (i = 0; i < curscr->maxy; i++) {
610 			for (j = 0; j < curscr->maxx; j++)
611 				__CTRACE(__CTRACE_REFRESH,
612 				    "[%d,%d](%x,%x,%p)-(%x,%x,%p)\n",
613 				    i, j,
614 				    curscr->lines[i]->line[j].ch,
615 				    curscr->lines[i]->line[j].attr,
616 				    curscr->lines[i]->line[j].nsp,
617 				    _cursesi_screen->__virtscr->lines[i]->line[j].ch,
618 				    _cursesi_screen->__virtscr->lines[i]->line[j].attr,
619 				    _cursesi_screen->__virtscr->lines[i]->line[j].nsp);
620 		}
621 	}
622 #endif /* HAVE_WCHAR */
623 #endif /* DEBUG */
624 	return fflush(_cursesi_screen->outfd) == EOF ? ERR : OK;
625 }
626 
627 /*
628  * makech --
629  *	Make a change on the screen.
630  */
631 static int
632 makech(int wy)
633 {
634 	WINDOW	*win;
635 	static __LDATA blank;
636 	__LDATA *nsp, *csp, *cp, *cep;
637 	size_t	clsp, nlsp;	/* Last space in lines. */
638 	int	lch, wx;
639 	char	*ce;
640 	attr_t	lspc;		/* Last space colour */
641 	attr_t	off, on;
642 
643 #ifdef __GNUC__
644 	nlsp = lspc = 0;	/* XXX gcc -Wuninitialized */
645 #endif
646 	if (_cursesi_screen->curwin)
647 		win = curscr;
648 	else
649 		win = __virtscr;
650 #ifdef HAVE_WCHAR
651 	blank.ch = ( wchar_t )btowc(( int ) win->bch );
652 	blank.attr = 0;
653 	if (_cursesi_copy_nsp(win->bnsp, &blank) == ERR)
654 		return ERR;
655 	SET_WCOL( blank, 1 );
656 #endif /* HAVE_WCHAR */
657 #ifdef DEBUG
658 #if HAVE_WCHAR
659 	{
660 		int x;
661 		__LDATA *lp, *vlp;
662 
663 		__CTRACE(__CTRACE_REFRESH,
664 		    "[makech-before]wy=%d,curscr(%p)-__virtscr(%p)\n",
665 		    wy, curscr, __virtscr);
666 		for (x = 0; x < curscr->maxx; x++) {
667 			lp = &curscr->lines[wy]->line[x];
668 			vlp = &__virtscr->lines[wy]->line[x];
669 			__CTRACE(__CTRACE_REFRESH,
670 			    "[%d,%d](%x,%x,%x,%x,%p)-"
671 			    "(%x,%x,%x,%x,%p)\n",
672 			    wy, x, lp->ch, lp->attr,
673 			    win->bch, win->battr, lp->nsp,
674 			    vlp->ch, vlp->attr,
675 			    win->bch, win->battr, vlp->nsp);
676 		}
677 	}
678 #endif /* HAVE_WCHAR */
679 #endif /* DEBUG */
680 	/* Is the cursor still on the end of the last line? */
681 	if (wy > 0 && curscr->lines[wy - 1]->flags & __ISPASTEOL) {
682 		domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
683 			_cursesi_screen->ly + 1, 0);
684 		_cursesi_screen->ly++;
685 		_cursesi_screen->lx = 0;
686 	}
687 	wx = *win->lines[wy]->firstchp;
688 	if (wx < 0)
689 		wx = 0;
690 	else
691 		if (wx >= win->maxx)
692 			return (OK);
693 	lch = *win->lines[wy]->lastchp;
694 	if (lch < 0)
695 		return (OK);
696 	else
697 		if (lch >= (int) win->maxx)
698 			lch = win->maxx - 1;
699 
700 	if (_cursesi_screen->curwin) {
701 		csp = &blank;
702 #ifdef DEBUG
703 		__CTRACE(__CTRACE_REFRESH, "makech: csp is blank\n");
704 #endif /* DEBUG */
705 	} else {
706 		csp = &curscr->lines[wy]->line[wx];
707 #ifdef DEBUG
708 		__CTRACE(__CTRACE_REFRESH,
709 		    "makech: csp is on curscr:(%d,%d)\n", wy, wx);
710 #endif /* DEBUG */
711 	}
712 
713 	nsp = &win->lines[wy]->line[wx];
714 #ifdef DEBUG
715 	if ( _cursesi_screen->curwin )
716 		__CTRACE(__CTRACE_REFRESH,
717 		    "makech: nsp is at curscr:(%d,%d)\n", wy, wx);
718 	else
719 		__CTRACE(__CTRACE_REFRESH,
720 		    "makech: nsp is at __virtscr:(%d,%d)\n", wy, wx);
721 #endif /* DEBUG */
722 	if (__tc_ce && !_cursesi_screen->curwin) {
723 		cp = &win->lines[wy]->line[win->maxx - 1];
724 		lspc = cp->attr & __COLOR;
725 #ifndef HAVE_WCHAR
726 		while (cp->ch == ' ' && cp->attr == lspc) /* XXX */
727 			if (cp-- <= win->lines[wy]->line)
728 				break;
729 #else
730 		while (cp->ch == ( wchar_t )btowc(( int )' ' )
731 				&& ( cp->attr & WA_ATTRIBUTES ) == lspc)
732 			if (cp-- <= win->lines[wy]->line)
733 				break;
734 #endif /* HAVE_WCHAR */
735 		if (win->lines[wy]->line > cp)
736 			nlsp = 0;
737 		else
738 			nlsp = cp - win->lines[wy]->line;
739 	}
740 	if (!_cursesi_screen->curwin)
741 		ce = __tc_ce;
742 	else
743 		ce = NULL;
744 
745 	while (wx <= lch) {
746 #ifdef DEBUG
747 		__CTRACE(__CTRACE_REFRESH, "makech: wx=%d,lch=%d\n", wx, lch);
748 #endif /* DEBUG */
749 #ifndef HAVE_WCHAR
750 		if (memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
751 			if (wx <= lch) {
752 				while (wx <= lch &&
753 				    memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
754 					nsp++;
755 					if (!_cursesi_screen->curwin)
756 						++csp;
757 					++wx;
758 				}
759 				continue;
760 			}
761 			break;
762 		}
763 #else
764 #ifdef DEBUG
765 		__CTRACE(__CTRACE_REFRESH, "makech: nsp=(%x,%x,%x,%x,%p)\n",
766 			nsp->ch, nsp->attr, win->bch, win->battr, nsp->nsp);
767 		__CTRACE(__CTRACE_REFRESH, "makech: csp=(%x,%x,%x,%x,%p)\n",
768 			csp->ch, csp->attr, win->bch, win->battr, csp->nsp);
769 #endif /* DEBUG */
770 		if (((nsp->attr & __WCWIDTH) != __WCWIDTH) &&
771 		    cellcmp(nsp, csp)) {
772 			if (wx <= lch) {
773 				while (wx <= lch && cellcmp( csp, nsp )) {
774 					nsp++;
775 					if (!_cursesi_screen->curwin)
776 						++csp;
777 					++wx;
778 				}
779 				continue;
780 			}
781 			break;
782 		}
783 #endif /* HAVE_WCHAR */
784 		domvcur(_cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
785 
786 #ifdef DEBUG
787 		__CTRACE(__CTRACE_REFRESH, "makech: 1: wx = %d, ly= %d, "
788 		    "lx = %d, newy = %d, newx = %d\n",
789 		    wx, _cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
790 #endif
791 		_cursesi_screen->ly = wy;
792 		_cursesi_screen->lx = wx;
793 #ifndef HAVE_WCHAR
794 		while (wx <= lch && memcmp(nsp, csp, sizeof(__LDATA)) != 0) {
795 			if (ce != NULL &&
796 			    wx >= nlsp && nsp->ch == ' ' && nsp->attr == lspc) {
797 				/* Are we continuing a multircell character? */
798 				if ((nsp->attr & __WCWIDTH) == __WCWIDTH) {
799 #ifdef DEBUG
800 					__CTRACE(__CTRACE_REFRESH,
801 						"Skipping continuation cell\n");
802 					wx++;
803 					csp->ch = nsp->ch;
804 					csp->attr = nsp->attr;
805 					nsp++;
806 					csp++;
807 					continue;
808 				}
809 #endif
810 #else
811 		while (!cellcmp(nsp, csp) && wx <= lch) {
812 			if (ce != NULL && wx >= nlsp
813 			   && nsp->ch == (wchar_t)btowc((int)' ') /* XXX */
814 			   && (nsp->attr & WA_ATTRIBUTES) == lspc) {
815 
816 #endif
817 				/* Check for clear to end-of-line. */
818 				cep = &curscr->lines[wy]->line[win->maxx - 1];
819 #ifndef HAVE_WCHAR
820 				while (cep->ch == ' ' && cep->attr == lspc) /* XXX */
821 #else
822 				while (cep->ch == (wchar_t)btowc((int)' ')
823 				       && (cep->attr & WA_ATTRIBUTES) == lspc)
824 #endif /* HAVE_WCHAR */
825 					if (cep-- <= csp)
826 						break;
827 				clsp = cep - curscr->lines[wy]->line -
828 				    win->begx * __LDATASIZE;
829 #ifdef DEBUG
830 				__CTRACE(__CTRACE_REFRESH,
831 				    "makech: clsp = %zu, nlsp = %zu\n",
832 				    clsp, nlsp);
833 #endif
834 				if (((clsp - nlsp >= strlen(__tc_ce) &&
835 				    clsp < win->maxx * __LDATASIZE) ||
836 				    wy == win->maxy - 1) &&
837 				    (!(lspc & __COLOR) ||
838 				    ((lspc & __COLOR) && __tc_ut))) {
839 					__unsetattr(0);
840 					if (__using_color &&
841 					    ((lspc & __COLOR) !=
842 					    (curscr->wattr & __COLOR)))
843 						__set_color(curscr, lspc &
844 						    __COLOR);
845 					tputs(__tc_ce, 0, __cputchar);
846 					_cursesi_screen->lx = wx + win->begx;
847 					while (wx++ <= clsp) {
848 						csp->attr = lspc;
849 #ifndef HAVE_WCHAR
850 						csp->ch = ' '; /* XXX */
851 #else
852 						csp->ch = (wchar_t)btowc((int)' ');
853 						SET_WCOL( *csp, 1 );
854 #endif /* HAVE_WCHAR */
855 						csp++;
856 					}
857 					return (OK);
858 				}
859 				ce = NULL;
860 			}
861 
862 #ifdef DEBUG
863 				__CTRACE(__CTRACE_REFRESH,
864 				    "makech: have attr %08x, need attr %08x\n",
865 				    curscr->wattr & WA_ATTRIBUTES,
866 				    nsp->attr & WA_ATTRIBUTES);
867 #endif
868 
869 			off = (~nsp->attr & curscr->wattr) & WA_ATTRIBUTES;
870 
871 			/*
872 			 * Unset attributes as appropriate.  Unset first
873 			 * so that the relevant attributes can be reset
874 			 * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
875 			 * 'mp' and 'mr').  Check to see if we also turn off
876 			 * standout, attributes and colour.
877 			 */
878 			if (off & __TERMATTR && __tc_me != NULL) {
879 				tputs(__tc_me, 0, __cputchar);
880 				curscr->wattr &= __mask_me;
881 				off &= __mask_me;
882 			}
883 
884 			/*
885 			 * Exit underscore mode if appropriate.
886 			 * Check to see if we also turn off standout,
887 			 * attributes and colour.
888 			 */
889 			if (off & __UNDERSCORE && __tc_ue != NULL) {
890 				tputs(__tc_ue, 0, __cputchar);
891 				curscr->wattr &= __mask_ue;
892 				off &= __mask_ue;
893 			}
894 
895 			/*
896 			 * Exit standout mode as appropriate.
897 			 * Check to see if we also turn off underscore,
898 			 * attributes and colour.
899 			 * XXX
900 			 * Should use uc if so/se not available.
901 			 */
902 			if (off & __STANDOUT && __tc_se != NULL) {
903 				tputs(__tc_se, 0, __cputchar);
904 				curscr->wattr &= __mask_se;
905 				off &= __mask_se;
906 			}
907 
908 #ifndef HAVE_WCHAR
909 			if (off & __ALTCHARSET && __tc_ae != NULL) {
910 				tputs(__tc_ae, 0, __cputchar);
911 				curscr->wattr &= ~__ALTCHARSET;
912 			}
913 #else
914 			if (off & WA_ALTCHARSET && __tc_ae != NULL) {
915 				tputs(__tc_ae, 0, __cputchar);
916 				curscr->wattr &= ~WA_ALTCHARSET;
917 			}
918 #endif /* HAVE_WCHAR */
919 
920 			/* Set/change colour as appropriate. */
921 			if (__using_color)
922 				__set_color(curscr, nsp->attr & __COLOR);
923 
924 			on = (nsp->attr & ~curscr->wattr) & WA_ATTRIBUTES;
925 
926 			/*
927 			 * Enter standout mode if appropriate.
928 			 */
929 			if (on & __STANDOUT && __tc_so != NULL && __tc_se
930 			    != NULL) {
931 				tputs(__tc_so, 0, __cputchar);
932 				curscr->wattr |= __STANDOUT;
933 			}
934 
935 			/*
936 			 * Enter underscore mode if appropriate.
937 			 * XXX
938 			 * Should use uc if us/ue not available.
939 			 */
940 			if (on & __UNDERSCORE && __tc_us != NULL &&
941 			    __tc_ue != NULL) {
942 				tputs(__tc_us, 0, __cputchar);
943 				curscr->wattr |= __UNDERSCORE;
944 			}
945 
946 			/*
947 			 * Set other attributes as appropriate.
948 			 */
949 			if (__tc_me != NULL) {
950 				if (on & __BLINK && __tc_mb != NULL) {
951 					tputs(__tc_mb, 0, __cputchar);
952 					curscr->wattr |= __BLINK;
953 				}
954 				if (on & __BOLD && __tc_md != NULL) {
955 					tputs(__tc_md, 0, __cputchar);
956 					curscr->wattr |= __BOLD;
957 				}
958 				if (on & __DIM && __tc_mh != NULL) {
959 					tputs(__tc_mh, 0, __cputchar);
960 					curscr->wattr |= __DIM;
961 				}
962 				if (on & __BLANK && __tc_mk != NULL) {
963 					tputs(__tc_mk, 0, __cputchar);
964 					curscr->wattr |= __BLANK;
965 				}
966 				if (on & __PROTECT && __tc_mp != NULL) {
967 					tputs(__tc_mp, 0, __cputchar);
968 					curscr->wattr |= __PROTECT;
969 				}
970 				if (on & __REVERSE && __tc_mr != NULL) {
971 					tputs(__tc_mr, 0, __cputchar);
972 					curscr->wattr |= __REVERSE;
973 				}
974 #ifdef HAVE_WCHAR
975 				if (on & WA_TOP && __tc_Xt != NULL) {
976 					tputs(__tc_Xt, 0, __cputchar);
977 					curscr->wattr |= WA_TOP;
978 				}
979 				if (on & WA_LOW && __tc_Xo != NULL) {
980 					tputs(__tc_Xo, 0, __cputchar);
981 					curscr->wattr |= WA_LOW;
982 				}
983 				if (on & WA_LEFT && __tc_Xl != NULL) {
984 					tputs(__tc_Xl, 0, __cputchar);
985 					curscr->wattr |= WA_LEFT;
986 				}
987 				if (on & WA_RIGHT && __tc_Xl != NULL) {
988 					tputs(__tc_Xl, 0, __cputchar);
989 					curscr->wattr |= WA_RIGHT;
990 				}
991 				if (on & WA_HORIZONTAL && __tc_Xh != NULL) {
992 					tputs(__tc_Xh, 0, __cputchar);
993 					curscr->wattr |= WA_HORIZONTAL;
994 				}
995 				if (on & WA_VERTICAL && __tc_Xv != NULL) {
996 					tputs(__tc_Xv, 0, __cputchar);
997 					curscr->wattr |= WA_VERTICAL;
998 				}
999 #endif /* HAVE_WCHAR */
1000 			}
1001 
1002 			/* Enter/exit altcharset mode as appropriate. */
1003 #ifndef HAVE_WCHAR
1004 			if (on & __ALTCHARSET && __tc_as != NULL &&
1005 			    __tc_ae != NULL) {
1006 				tputs(__tc_as, 0, __cputchar);
1007 				curscr->wattr |= __ALTCHARSET;
1008 			}
1009 #else
1010 			if (on & WA_ALTCHARSET && __tc_as != NULL &&
1011 			    __tc_ae != NULL) {
1012 				tputs(__tc_as, 0, __cputchar);
1013 				curscr->wattr |= WA_ALTCHARSET;
1014 			}
1015 #endif /* HAVE_WCHAR */
1016 
1017 			wx++;
1018 			if (wx >= win->maxx &&
1019 			    wy == win->maxy - 1 && !_cursesi_screen->curwin) {
1020 				if (win->flags & __SCROLLOK) {
1021 					if (win->flags & __ENDLINE)
1022 						__unsetattr(1);
1023 					if (!(win->flags & __SCROLLWIN)) {
1024 						if (!_cursesi_screen->curwin) {
1025 							csp->attr = nsp->attr;
1026 							csp->ch = nsp->ch;
1027 #ifdef HAVE_WCHAR
1028 							if (_cursesi_copy_nsp(nsp->nsp, csp) == ERR)
1029 								return ERR;
1030 #endif /* HAVE_WCHAR */
1031 						}
1032 #ifndef HAVE_WCHAR
1033 						__cputchar((int) nsp->ch);
1034 #else
1035 						if ( WCOL( *nsp ) > 0 ) {
1036 							__cputwchar((int)nsp->ch);
1037 #ifdef DEBUG
1038 							__CTRACE(__CTRACE_REFRESH,
1039 							    "makech: (%d,%d)putwchar(0x%x)\n",
1040 								wy, wx - 1,
1041 								nsp->ch );
1042 #endif /* DEBUG */
1043 							/*
1044 							 * Output non-spacing
1045 							 * characters for the
1046 							 * cell.
1047 							 */
1048 							__cursesi_putnsp(nsp->nsp,
1049 									 wy, wx);
1050 
1051 						}
1052 #endif /* HAVE_WCHAR */
1053 					}
1054 					if (wx < curscr->maxx) {
1055 						domvcur(_cursesi_screen->ly, wx,
1056 						    (int) (win->maxy - 1),
1057 						    (int) (win->maxx - 1));
1058 					}
1059 					_cursesi_screen->ly = win->maxy - 1;
1060 					_cursesi_screen->lx = win->maxx - 1;
1061 					return (OK);
1062 				}
1063 			}
1064 			if (wx < win->maxx || wy < win->maxy - 1 ||
1065 			    !(win->flags & __SCROLLWIN)) {
1066 				if (!_cursesi_screen->curwin) {
1067 					csp->attr = nsp->attr;
1068 					csp->ch = nsp->ch;
1069 #ifdef HAVE_WCHAR
1070 					if (_cursesi_copy_nsp(nsp->nsp,
1071 							      csp) == ERR)
1072 						return ERR;
1073 #endif /* HAVE_WCHAR */
1074 					csp++;
1075 				}
1076 #ifndef HAVE_WCHAR
1077 				__cputchar((int) nsp->ch);
1078 #ifdef DEBUG
1079 				__CTRACE(__CTRACE_REFRESH,
1080 				    "makech: putchar(%c)\n", nsp->ch & 0177);
1081 #endif
1082 #else
1083 				if (WCOL(*nsp) > 0) {
1084 					__cputwchar((int) nsp->ch);
1085 #ifdef DEBUG
1086 					__CTRACE(__CTRACE_REFRESH,
1087 					    "makech:(%d,%d) putwchar(%x)\n",
1088 					    wy, wx - 1, nsp->ch);
1089 					__cursesi_putnsp(nsp->nsp, wy, wx);
1090 #endif /* DEBUG */
1091 				}
1092 #endif /* HAVE_WCHAR */
1093 			}
1094 			if (__tc_uc && ((nsp->attr & __STANDOUT) ||
1095 			    (nsp->attr & __UNDERSCORE))) {
1096 				__cputchar('\b');
1097 				tputs(__tc_uc, 0, __cputchar);
1098 			}
1099 			nsp++;
1100 #ifdef DEBUG
1101 			__CTRACE(__CTRACE_REFRESH,
1102 			    "makech: 2: wx = %d, lx = %d\n",
1103 			    wx, _cursesi_screen->lx);
1104 #endif
1105 		}
1106 		if (_cursesi_screen->lx == wx)	/* If no change. */
1107 			break;
1108 		_cursesi_screen->lx = wx;
1109 		if (_cursesi_screen->lx >= COLS && __tc_am)
1110 			_cursesi_screen->lx = COLS - 1;
1111 		else
1112 			if (wx >= win->maxx) {
1113 				domvcur(_cursesi_screen->ly,
1114 					_cursesi_screen->lx,
1115 					_cursesi_screen->ly,
1116 					(int) (win->maxx - 1));
1117 				_cursesi_screen->lx = win->maxx - 1;
1118 			}
1119 #ifdef DEBUG
1120 		__CTRACE(__CTRACE_REFRESH, "makech: 3: wx = %d, lx = %d\n",
1121 		    wx, _cursesi_screen->lx);
1122 #endif
1123 	}
1124 #ifdef DEBUG
1125 #if HAVE_WCHAR
1126 	{
1127 		int x;
1128 		__LDATA *lp, *vlp;
1129 
1130 		__CTRACE(__CTRACE_REFRESH,
1131 		    "makech-after: curscr(%p)-__virtscr(%p)\n",
1132 		    curscr, __virtscr );
1133 		for (x = 0; x < curscr->maxx; x++) {
1134 			lp = &curscr->lines[wy]->line[x];
1135 			vlp = &__virtscr->lines[wy]->line[x];
1136 			__CTRACE(__CTRACE_REFRESH,
1137 			    "[%d,%d](%x,%x,%x,%x,%p)-"
1138 			    "(%x,%x,%x,%x,%p)\n",
1139 			    wy, x, lp->ch, lp->attr,
1140 			    win->bch, win->battr, lp->nsp,
1141 			    vlp->ch, vlp->attr,
1142 			    win->bch, win->battr, vlp->nsp);
1143 		}
1144 	}
1145 #endif /* HAVE_WCHAR */
1146 #endif /* DEBUG */
1147 
1148 	return (OK);
1149 }
1150 
1151 /*
1152  * domvcur --
1153  *	Do a mvcur, leaving attributes if necessary.
1154  */
1155 static void
1156 domvcur(oy, ox, ny, nx)
1157 	int	oy, ox, ny, nx;
1158 {
1159 #ifdef DEBUG
1160 	__CTRACE(__CTRACE_REFRESH, "domvcur: (%x,%d)=>(%d,%d)\n",
1161 	    oy, ox, ny, nx );
1162 #endif /* DEBUG */
1163 	__unsetattr(1);
1164 	if ( oy == ny && ox == nx )
1165 		return;
1166 	__mvcur(oy, ox, ny, nx, 1);
1167 }
1168 
1169 /*
1170  * Quickch() attempts to detect a pattern in the change of the window
1171  * in order to optimize the change, e.g., scroll n lines as opposed to
1172  * repainting the screen line by line.
1173  */
1174 
1175 static __LDATA buf[128];
1176 static  u_int last_hash;
1177 static  size_t last_hash_len;
1178 #define BLANKSIZE (sizeof(buf) / sizeof(buf[0]))
1179 
1180 static void
1181 quickch(void)
1182 {
1183 #define THRESH		(int) __virtscr->maxy / 4
1184 
1185 	__LINE *clp, *tmp1, *tmp2;
1186 	int	bsize, curs, curw, starts, startw, i, j;
1187 	int	n, target, cur_period, bot, top, sc_region;
1188 	u_int	blank_hash;
1189 	attr_t	bcolor;
1190 
1191 #ifdef __GNUC__
1192 	curs = curw = starts = startw = 0;	/* XXX gcc -Wuninitialized */
1193 #endif
1194 	/*
1195 	 * Find how many lines from the top of the screen are unchanged.
1196 	 */
1197 	for (top = 0; top < __virtscr->maxy; top++)
1198 #ifndef HAVE_WCHAR
1199 		if (__virtscr->lines[top]->flags & __ISDIRTY &&
1200 		    (__virtscr->lines[top]->hash != curscr->lines[top]->hash ||
1201 		    memcmp(__virtscr->lines[top]->line,
1202 		    curscr->lines[top]->line,
1203 		    (size_t) __virtscr->maxx * __LDATASIZE)
1204 		    != 0))
1205 			break;
1206 #else
1207 		if (__virtscr->lines[top]->flags & __ISDIRTY &&
1208 		    (__virtscr->lines[top]->hash != curscr->lines[top]->hash ||
1209 		    !linecmp(__virtscr->lines[top]->line,
1210 		    curscr->lines[top]->line,
1211 	(size_t) __virtscr->maxx )))
1212 			break;
1213 #endif /* HAVE_WCHAR */
1214 		else
1215 			__virtscr->lines[top]->flags &= ~__ISDIRTY;
1216 	/*
1217 	 * Find how many lines from bottom of screen are unchanged.
1218 	 */
1219 	for (bot = __virtscr->maxy - 1; bot >= 0; bot--)
1220 #ifndef HAVE_WCHAR
1221 		if (__virtscr->lines[bot]->flags & __ISDIRTY &&
1222 		    (__virtscr->lines[bot]->hash != curscr->lines[bot]->hash ||
1223 		    memcmp(__virtscr->lines[bot]->line,
1224 		    curscr->lines[bot]->line,
1225 		    (size_t) __virtscr->maxx * __LDATASIZE)
1226 		    != 0))
1227 			break;
1228 #else
1229 		if (__virtscr->lines[bot]->flags & __ISDIRTY &&
1230 		    (__virtscr->lines[bot]->hash != curscr->lines[bot]->hash ||
1231 		    !linecmp(__virtscr->lines[bot]->line,
1232 		    curscr->lines[bot]->line,
1233 		    (size_t) __virtscr->maxx )))
1234 			break;
1235 #endif /* HAVE_WCHAR */
1236 		else
1237 			__virtscr->lines[bot]->flags &= ~__ISDIRTY;
1238 
1239 	/*
1240 	 * Work round an xterm bug where inserting lines causes all the
1241 	 * inserted lines to be covered with the background colour we
1242 	 * set on the first line (even if we unset it for subsequent
1243 	 * lines).
1244 	 */
1245 	bcolor = __virtscr->lines[min(top,
1246 	    __virtscr->maxy - 1)]->line[0].attr & __COLOR;
1247 	for (i = top + 1, j = 0; i < bot; i++) {
1248 		if ((__virtscr->lines[i]->line[0].attr & __COLOR) != bcolor) {
1249 			bcolor = __virtscr->lines[i]->line[__virtscr->maxx].
1250 			    attr & __COLOR;
1251 			j = i - top;
1252 		} else
1253 			break;
1254 	}
1255 	top += j;
1256 
1257 #ifdef NO_JERKINESS
1258 	/*
1259 	 * If we have a bottom unchanged region return.  Scrolling the
1260 	 * bottom region up and then back down causes a screen jitter.
1261 	 * This will increase the number of characters sent to the screen
1262 	 * but it looks better.
1263 	 */
1264 	if (bot < __virtscr->maxy - 1)
1265 		return;
1266 #endif				/* NO_JERKINESS */
1267 
1268 	/*
1269 	 * Search for the largest block of text not changed.
1270 	 * Invariants of the loop:
1271 	 * - Startw is the index of the beginning of the examined block in
1272 	 *   __virtscr.
1273 	 * - Starts is the index of the beginning of the examined block in
1274 	 *   curscr.
1275 	 * - Curw is the index of one past the end of the exmined block in
1276 	 *   __virtscr.
1277 	 * - Curs is the index of one past the end of the exmined block in
1278 	 *   curscr.
1279 	 * - bsize is the current size of the examined block.
1280 	*/
1281 
1282 	for (bsize = bot - top; bsize >= THRESH; bsize--) {
1283 		for (startw = top; startw <= bot - bsize; startw++)
1284 			for (starts = top; starts <= bot - bsize;
1285 				starts++) {
1286 				for (curw = startw, curs = starts;
1287 				    curs < starts + bsize; curw++, curs++)
1288 					if (__virtscr->lines[curw]->hash !=
1289 					    curscr->lines[curs]->hash)
1290 						break;
1291 				if (curs != starts + bsize)
1292 					continue;
1293 				for (curw = startw, curs = starts;
1294 					curs < starts + bsize; curw++, curs++)
1295 #ifndef HAVE_WCHAR
1296 					if (memcmp(__virtscr->lines[curw]->line,
1297 					    curscr->lines[curs]->line,
1298 					    (size_t) __virtscr->maxx *
1299 					    __LDATASIZE) != 0)
1300 						break;
1301 #else
1302 					if (!linecmp(__virtscr->lines[curw]->line,
1303 					    curscr->lines[curs]->line,
1304 					    (size_t) __virtscr->maxx))
1305 						break;
1306 #endif /* HAVE_WCHAR */
1307 				if (curs == starts + bsize)
1308 					goto done;
1309 			}
1310 	}
1311 done:
1312 
1313 	/* Did not find anything */
1314 	if (bsize < THRESH)
1315 		return;
1316 
1317 #ifdef DEBUG
1318 	__CTRACE(__CTRACE_REFRESH, "quickch:bsize=%d, starts=%d, startw=%d, "
1319 	    "curw=%d, curs=%d, top=%d, bot=%d\n",
1320 	    bsize, starts, startw, curw, curs, top, bot);
1321 #endif
1322 
1323 	/*
1324 	 * Make sure that there is no overlap between the bottom and top
1325 	 * regions and the middle scrolled block.
1326 	 */
1327 	if (bot < curs)
1328 		bot = curs - 1;
1329 	if (top > starts)
1330 		top = starts;
1331 
1332 	n = startw - starts;
1333 
1334 #ifdef DEBUG
1335 	__CTRACE(__CTRACE_REFRESH, "#####################################\n");
1336 	for (i = 0; i < curscr->maxy; i++) {
1337 		__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
1338 		__CTRACE(__CTRACE_REFRESH, " 0x%x \n", curscr->lines[i]->hash);
1339 		for (j = 0; j < curscr->maxx; j++)
1340 			__CTRACE(__CTRACE_REFRESH, "%c",
1341 			    curscr->lines[i]->line[j].ch);
1342 		__CTRACE(__CTRACE_REFRESH, "\n");
1343 		__CTRACE(__CTRACE_REFRESH, " attr:");
1344 		for (j = 0; j < curscr->maxx; j++)
1345 			__CTRACE(__CTRACE_REFRESH, " %x",
1346 			    curscr->lines[i]->line[j].attr);
1347 		__CTRACE(__CTRACE_REFRESH, "\n");
1348 		__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
1349 		__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
1350 		    __virtscr->lines[i]->hash);
1351 		__CTRACE(__CTRACE_REFRESH, " 0x%x ",
1352 		    __virtscr->lines[i]->flags);
1353 		for (j = 0; j < __virtscr->maxx; j++)
1354 			__CTRACE(__CTRACE_REFRESH, "%c",
1355 			    __virtscr->lines[i]->line[j].ch);
1356 		__CTRACE(__CTRACE_REFRESH, "\n");
1357 		__CTRACE(__CTRACE_REFRESH, " attr:");
1358 		for (j = 0; j < __virtscr->maxx; j++)
1359 			__CTRACE(__CTRACE_REFRESH, " %x",
1360 			    __virtscr->lines[i]->line[j].attr);
1361 		__CTRACE(__CTRACE_REFRESH, "\n");
1362 	}
1363 #endif
1364 
1365 #ifndef HAVE_WCHAR
1366 	if (buf[0].ch != ' ') {
1367 		for (i = 0; i < BLANKSIZE; i++) {
1368 			buf[i].ch = ' ';
1369 			buf[i].attr = 0;
1370 		}
1371 	}
1372 #else
1373 	if (buf[0].ch != ( wchar_t )btowc(( int ) curscr->bch )) {
1374 		for (i = 0; i < BLANKSIZE; i++) {
1375 			buf[i].ch = ( wchar_t )btowc(( int ) curscr->bch );
1376 			if (_cursesi_copy_nsp(curscr->bnsp, &buf[i]) == ERR)
1377 				return;
1378 			buf[i].attr = 0;
1379 			SET_WCOL( buf[ i ], 1 );
1380 		}
1381 	}
1382 #endif /* HAVE_WCHAR */
1383 
1384 	if (__virtscr->maxx != last_hash_len) {
1385 		blank_hash = 0;
1386 		for (i = __virtscr->maxx; i > BLANKSIZE; i -= BLANKSIZE) {
1387 			blank_hash = __hash_more(buf, sizeof(buf), blank_hash);
1388 		}
1389 		blank_hash = __hash_more((char *)(void *)buf,
1390 		    i * sizeof(buf[0]), blank_hash);
1391 		/* cache result in static data - screen width doesn't change often */
1392 		last_hash_len = __virtscr->maxx;
1393 		last_hash = blank_hash;
1394 	} else
1395 		blank_hash = last_hash;
1396 
1397 	/*
1398 	 * Perform the rotation to maintain the consistency of curscr.
1399 	 * This is hairy since we are doing an *in place* rotation.
1400 	 * Invariants of the loop:
1401 	 * - I is the index of the current line.
1402 	 * - Target is the index of the target of line i.
1403 	 * - Tmp1 points to current line (i).
1404 	 * - Tmp2 and points to target line (target);
1405 	 * - Cur_period is the index of the end of the current period.
1406 	 *   (see below).
1407 	 *
1408 	 * There are 2 major issues here that make this rotation non-trivial:
1409 	 * 1.  Scrolling in a scrolling region bounded by the top
1410 	 *     and bottom regions determined (whose size is sc_region).
1411 	 * 2.  As a result of the use of the mod function, there may be a
1412 	 *     period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
1413 	 *     0 to 2, which then causes all odd lines not to be rotated.
1414 	 *     To remedy this, an index of the end ( = beginning) of the
1415 	 *     current 'period' is kept, cur_period, and when it is reached,
1416 	 *     the next period is started from cur_period + 1 which is
1417 	 *     guaranteed not to have been reached since that would mean that
1418 	 *     all records would have been reached. (think about it...).
1419 	 *
1420 	 * Lines in the rotation can have 3 attributes which are marked on the
1421 	 * line so that curscr is consistent with the visual screen.
1422 	 * 1.  Not dirty -- lines inside the scrolled block, top region or
1423 	 *                  bottom region.
1424 	 * 2.  Blank lines -- lines in the differential of the scrolling
1425 	 *		      region adjacent to top and bot regions
1426 	 *                    depending on scrolling direction.
1427 	 * 3.  Dirty line -- all other lines are marked dirty.
1428 	 */
1429 	sc_region = bot - top + 1;
1430 	i = top;
1431 	tmp1 = curscr->lines[top];
1432 	cur_period = top;
1433 	for (j = top; j <= bot; j++) {
1434 		target = (i - top + n + sc_region) % sc_region + top;
1435 		tmp2 = curscr->lines[target];
1436 		curscr->lines[target] = tmp1;
1437 		/* Mark block as clean and blank out scrolled lines. */
1438 		clp = curscr->lines[target];
1439 #ifdef DEBUG
1440 		__CTRACE(__CTRACE_REFRESH,
1441 		    "quickch: n=%d startw=%d curw=%d i = %d target=%d ",
1442 		    n, startw, curw, i, target);
1443 #endif
1444 		if ((target >= startw && target < curw) || target < top
1445 		    || target > bot) {
1446 #ifdef DEBUG
1447 			__CTRACE(__CTRACE_REFRESH, " notdirty\n");
1448 #endif
1449 			__virtscr->lines[target]->flags &= ~__ISDIRTY;
1450 		} else
1451 			if ((n > 0 && target >= top && target < top + n) ||
1452 			    (n < 0 && target <= bot && target > bot + n)) {
1453 #ifndef HAVE_WCHAR
1454 				if (clp->hash != blank_hash ||
1455 				    memcmp(clp->line, clp->line + 1,
1456 				    (__virtscr->maxx - 1)
1457 				    * __LDATASIZE) ||
1458 				    memcmp(clp->line, buf, __LDATASIZE)) {
1459 #else
1460 				if (clp->hash != blank_hash
1461 				    || linecmp(clp->line, clp->line + 1,
1462 				    (unsigned int) (__virtscr->maxx - 1))
1463 				    || cellcmp(clp->line, buf)) {
1464 #endif /* HAVE_WCHAR */
1465 					for (i = __virtscr->maxx;
1466 					    i > BLANKSIZE;
1467 					    i -= BLANKSIZE) {
1468 						(void) memcpy(clp->line + i -
1469 						    BLANKSIZE, buf, sizeof(buf));
1470 					}
1471 					(void) memcpy(clp->line , buf, i *
1472 					    sizeof(buf[0]));
1473 #ifdef DEBUG
1474 					__CTRACE(__CTRACE_REFRESH,
1475 					    " blanked out: dirty\n");
1476 #endif
1477 					clp->hash = blank_hash;
1478 					__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
1479 				} else {
1480 #ifdef DEBUG
1481 					__CTRACE(__CTRACE_REFRESH,
1482 					    " -- blank line already: dirty\n");
1483 #endif
1484 					__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
1485 				}
1486 			} else {
1487 #ifdef DEBUG
1488 				__CTRACE(__CTRACE_REFRESH, " -- dirty\n");
1489 #endif
1490 				__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
1491 			}
1492 		if (target == cur_period) {
1493 			i = target + 1;
1494 			tmp1 = curscr->lines[i];
1495 			cur_period = i;
1496 		} else {
1497 			tmp1 = tmp2;
1498 			i = target;
1499 		}
1500 	}
1501 #ifdef DEBUG
1502 	__CTRACE(__CTRACE_REFRESH, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
1503 	for (i = 0; i < curscr->maxy; i++) {
1504 		__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
1505 		for (j = 0; j < curscr->maxx; j++)
1506 			__CTRACE(__CTRACE_REFRESH, "%c",
1507 			    curscr->lines[i]->line[j].ch);
1508 		__CTRACE(__CTRACE_REFRESH, "\n");
1509 		__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
1510 		for (j = 0; j < __virtscr->maxx; j++)
1511 			__CTRACE(__CTRACE_REFRESH, "%c",
1512 			    __virtscr->lines[i]->line[j].ch);
1513 		__CTRACE(__CTRACE_REFRESH, "\n");
1514 	}
1515 #endif
1516 	if (n != 0)
1517 		scrolln(starts, startw, curs, bot, top);
1518 }
1519 
1520 /*
1521  * scrolln --
1522  *	Scroll n lines, where n is starts - startw.
1523  */
1524 static void /* ARGSUSED */
1525 scrolln(starts, startw, curs, bot, top)
1526 	int	starts, startw, curs, bot, top;
1527 {
1528 	int	i, oy, ox, n;
1529 
1530 	oy = curscr->cury;
1531 	ox = curscr->curx;
1532 	n = starts - startw;
1533 
1534 	/*
1535 	 * XXX
1536 	 * The initial tests that set __noqch don't let us reach here unless
1537 	 * we have either cs + ho + SF/sf/SR/sr, or AL + DL.  SF/sf and SR/sr
1538 	 * scrolling can only shift the entire scrolling region, not just a
1539 	 * part of it, which means that the quickch() routine is going to be
1540 	 * sadly disappointed in us if we don't have cs as well.
1541 	 *
1542 	 * If cs, ho and SF/sf are set, can use the scrolling region.  Because
1543 	 * the cursor position after cs is undefined, we need ho which gives us
1544 	 * the ability to move to somewhere without knowledge of the current
1545 	 * location of the cursor.  Still call __mvcur() anyway, to update its
1546 	 * idea of where the cursor is.
1547 	 *
1548 	 * When the scrolling region has been set, the cursor has to be at the
1549 	 * last line of the region to make the scroll happen.
1550 	 *
1551 	 * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
1552 	 * or AL/DL, and, some terminals have AL/DL, sf/sr, and cs, but not
1553 	 * SF/SR.  So, if we're scrolling almost all of the screen, try and use
1554 	 * AL/DL, otherwise use the scrolling region.  The "almost all" is a
1555 	 * shameless hack for vi.
1556 	 */
1557 	if (n > 0) {
1558 		if (__tc_cs != NULL && __tc_ho != NULL && (__tc_SF != NULL ||
1559 		    ((__tc_AL == NULL || __tc_DL == NULL ||
1560 		    top > 3 || bot + 3 < __virtscr->maxy) &&
1561 		    __tc_sf != NULL))) {
1562 			tputs(__tscroll(__tc_cs, top, bot + 1), 0, __cputchar);
1563 			__mvcur(oy, ox, 0, 0, 1);
1564 			tputs(__tc_ho, 0, __cputchar);
1565 			__mvcur(0, 0, bot, 0, 1);
1566 			if (__tc_SF != NULL)
1567 				tputs(__tscroll(__tc_SF, n, 0), 0, __cputchar);
1568 			else
1569 				for (i = 0; i < n; i++)
1570 					tputs(__tc_sf, 0, __cputchar);
1571 			tputs(__tscroll(__tc_cs, 0, (int) __virtscr->maxy), 0,
1572 			    __cputchar);
1573 			__mvcur(bot, 0, 0, 0, 1);
1574 			tputs(__tc_ho, 0, __cputchar);
1575 			__mvcur(0, 0, oy, ox, 1);
1576 			return;
1577 		}
1578 
1579 		/* Scroll up the block. */
1580 		if (__tc_SF != NULL && top == 0) {
1581 			__mvcur(oy, ox, bot, 0, 1);
1582 			tputs(__tscroll(__tc_SF, n, 0), 0, __cputchar);
1583 		} else
1584 			if (__tc_DL != NULL) {
1585 				__mvcur(oy, ox, top, 0, 1);
1586 				tputs(__tscroll(__tc_DL, n, 0), 0, __cputchar);
1587 			} else
1588 				if (__tc_dl != NULL) {
1589 					__mvcur(oy, ox, top, 0, 1);
1590 					for (i = 0; i < n; i++)
1591 						tputs(__tc_dl, 0, __cputchar);
1592 				} else
1593 					if (__tc_sf != NULL && top == 0) {
1594 						__mvcur(oy, ox, bot, 0, 1);
1595 						for (i = 0; i < n; i++)
1596 							tputs(__tc_sf, 0,
1597 							    __cputchar);
1598 					} else
1599 						abort();
1600 
1601 		/* Push down the bottom region. */
1602 		__mvcur(top, 0, bot - n + 1, 0, 1);
1603 		if (__tc_AL != NULL)
1604 			tputs(__tscroll(__tc_AL, n, 0), 0, __cputchar);
1605 		else
1606 			if (__tc_al != NULL)
1607 				for (i = 0; i < n; i++)
1608 					tputs(__tc_al, 0, __cputchar);
1609 			else
1610 				abort();
1611 		__mvcur(bot - n + 1, 0, oy, ox, 1);
1612 	} else {
1613 		/*
1614 		 * !!!
1615 		 * n < 0
1616 		 *
1617 		 * If cs, ho and SR/sr are set, can use the scrolling region.
1618 		 * See the above comments for details.
1619 		 */
1620 		if (__tc_cs != NULL && __tc_ho != NULL && (__tc_SR != NULL ||
1621 		    ((__tc_AL == NULL || __tc_DL == NULL || top > 3 ||
1622 		    bot + 3 < __virtscr->maxy) && __tc_sr != NULL))) {
1623 			tputs(__tscroll(__tc_cs, top, bot + 1), 0, __cputchar);
1624 			__mvcur(oy, ox, 0, 0, 1);
1625 			tputs(__tc_ho, 0, __cputchar);
1626 			__mvcur(0, 0, top, 0, 1);
1627 
1628 			if (__tc_SR != NULL)
1629 				tputs(__tscroll(__tc_SR, -n, 0), 0, __cputchar);
1630 			else
1631 				for (i = n; i < 0; i++)
1632 					tputs(__tc_sr, 0, __cputchar);
1633 			tputs(__tscroll(__tc_cs, 0, (int) __virtscr->maxy), 0,
1634 			    __cputchar);
1635 			__mvcur(top, 0, 0, 0, 1);
1636 			tputs(__tc_ho, 0, __cputchar);
1637 			__mvcur(0, 0, oy, ox, 1);
1638 			return;
1639 		}
1640 
1641 		/* Preserve the bottom lines. */
1642 		__mvcur(oy, ox, bot + n + 1, 0, 1);
1643 		if (__tc_SR != NULL && bot == __virtscr->maxy)
1644 			tputs(__tscroll(__tc_SR, -n, 0), 0, __cputchar);
1645 		else
1646 			if (__tc_DL != NULL)
1647 				tputs(__tscroll(__tc_DL, -n, 0), 0, __cputchar);
1648 			else
1649 				if (__tc_dl != NULL)
1650 					for (i = n; i < 0; i++)
1651 						tputs(__tc_dl, 0, __cputchar);
1652 				else
1653 					if (__tc_sr != NULL &&
1654 					    bot == __virtscr->maxy)
1655 						for (i = n; i < 0; i++)
1656 							tputs(__tc_sr, 0,
1657 							    __cputchar);
1658 					else
1659 						abort();
1660 
1661 		/* Scroll the block down. */
1662 		__mvcur(bot + n + 1, 0, top, 0, 1);
1663 		if (__tc_AL != NULL)
1664 			tputs(__tscroll(__tc_AL, -n, 0), 0, __cputchar);
1665 		else
1666 			if (__tc_al != NULL)
1667 				for (i = n; i < 0; i++)
1668 					tputs(__tc_al, 0, __cputchar);
1669 			else
1670 				abort();
1671 		__mvcur(top, 0, oy, ox, 1);
1672 	}
1673 }
1674 
1675 /*
1676  * __unsetattr --
1677  *	Unset attributes on curscr.  Leave standout, attribute and colour
1678  *	modes if necessary (!ms).  Always leave altcharset (xterm at least
1679  *	ignores a cursor move if we don't).
1680  */
1681 void /* ARGSUSED */
1682 __unsetattr(int checkms)
1683 {
1684 	int	isms;
1685 
1686 	if (checkms)
1687 		if (!__tc_ms) {
1688 			isms = 1;
1689 		} else {
1690 			isms = 0;
1691 		}
1692 	else
1693 		isms = 1;
1694 #ifdef DEBUG
1695 	__CTRACE(__CTRACE_REFRESH,
1696 	    "__unsetattr: checkms = %d, ms = %s, wattr = %08x\n",
1697 	    checkms, __tc_ms ? "TRUE" : "FALSE", curscr->wattr);
1698 #endif
1699 
1700 	/*
1701 	 * Don't leave the screen in standout mode (check against ms).  Check
1702 	 * to see if we also turn off underscore, attributes and colour.
1703 	 */
1704 	if (curscr->wattr & __STANDOUT && isms) {
1705 		tputs(__tc_se, 0, __cputchar);
1706 		curscr->wattr &= __mask_se;
1707 	}
1708 	/*
1709 	 * Don't leave the screen in underscore mode (check against ms).
1710 	 * Check to see if we also turn off attributes.  Assume that we
1711 	 * also turn off colour.
1712 	 */
1713 	if (curscr->wattr & __UNDERSCORE && isms) {
1714 		tputs(__tc_ue, 0, __cputchar);
1715 		curscr->wattr &= __mask_ue;
1716 	}
1717 	/*
1718 	 * Don't leave the screen with attributes set (check against ms).
1719 	 * Assume that also turn off colour.
1720 	 */
1721 	if (curscr->wattr & __TERMATTR && isms) {
1722 		tputs(__tc_me, 0, __cputchar);
1723 		curscr->wattr &= __mask_me;
1724 	}
1725 	/* Don't leave the screen with altcharset set (don't check ms). */
1726 #ifndef HAVE_WCHAR
1727 	if (curscr->wattr & __ALTCHARSET) {
1728 		tputs(__tc_ae, 0, __cputchar);
1729 		curscr->wattr &= ~__ALTCHARSET;
1730 	}
1731 #else
1732 	if (curscr->wattr & WA_ALTCHARSET) {
1733 		tputs(__tc_ae, 0, __cputchar);
1734 		curscr->wattr &= ~WA_ALTCHARSET;
1735 	}
1736 #endif /* HAVE_WCHAR */
1737 	/* Don't leave the screen with colour set (check against ms). */
1738 	if (__using_color && isms)
1739 		__unset_color(curscr);
1740 }
1741 
1742 #ifdef HAVE_WCHAR
1743 /* compare two cells on screen, must have the same forground/background,
1744  * and the same sequence of non-spacing characters */
1745 int
1746 cellcmp( __LDATA *x, __LDATA *y )
1747 {
1748 	nschar_t *xnp = x->nsp, *ynp = y->nsp;
1749 	int ret = ( x->ch == y->ch ) & ( x->attr == y->attr );
1750 
1751 	if ( !ret )
1752 		return 0;
1753 	if ( !xnp && !ynp )
1754 		return 1;
1755 	if (( xnp && !ynp ) || ( !xnp && ynp ))
1756 		return 0;
1757 
1758 	while ( xnp && ynp ) {
1759 		if ( xnp->ch != ynp->ch )
1760 			return 0;
1761 		xnp = xnp->next;
1762 		ynp = ynp->next;
1763 	}
1764 	return ( !xnp && !ynp );
1765 }
1766 
1767 /* compare two line segments */
1768 int
1769 linecmp( __LDATA *xl, __LDATA *yl, size_t len )
1770 {
1771 	int i = 0;
1772 	__LDATA *xp = xl, *yp = yl;
1773 
1774 	for ( i = 0; i < len; i++, xp++, yp++ ) {
1775 		if ( !cellcmp( xp, yp ))
1776 			return 0;
1777 	}
1778 	return 1;
1779 }
1780 
1781 /*
1782  * Output the non-spacing characters associated with the given character
1783  * cell to the screen.
1784  */
1785 
1786 void
1787 __cursesi_putnsp(nschar_t *nsp, const int wy, const int wx)
1788 {
1789 	nschar_t *p;
1790 
1791 	/* this shuts up gcc warnings about wx and wy not being used */
1792 	if (wx > wy) {
1793 	}
1794 
1795 	p = nsp;
1796 	while (p != NULL) {
1797 		__cputwchar((int) p->ch);
1798 #ifdef DEBUG
1799 		__CTRACE(__CTRACE_REFRESH,
1800 		       "_cursesi_putnsp: (%d,%d) non-spacing putwchar(0x%x)\n",
1801 			 wy, wx - 1, p->ch);
1802 #endif
1803 		p = p->next;
1804 	}
1805 }
1806 
1807 #endif /* HAVE_WCHAR */
1808