Home | History | Annotate | Line # | Download | only in libcurses
      1 /*	$NetBSD: refresh.c,v 1.138 2026/11/08 05:08:11 blymn 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.138 2026/11/08 05:08:11 blymn Exp $");
     38 #endif
     39 #endif				/* not lint */
     40 
     41 #include <poll.h>
     42 #include <stdlib.h>
     43 #include <string.h>
     44 #include <assert.h>
     45 
     46 #include "curses.h"
     47 #include "curses_private.h"
     48 
     49 static void	domvcur(WINDOW *, int, int, int, int);
     50 static void	putattr(__LDATA *);
     51 static void	putattr_out(__LDATA *);
     52 static int	putch(__LDATA *, __LDATA *, int, int);
     53 static int	putchbr(__LDATA *, __LDATA *, __LDATA *, int, int);
     54 static int	makech(int);
     55 static void	quickch(void);
     56 static void	scrolln(int, int, int, int, int);
     57 
     58 static int	_wnoutrefresh(WINDOW *, int, int, int, int, int, int);
     59 
     60 static int lineeq(__LDATA *, __LDATA *, size_t);
     61 
     62 #define	CHECK_INTERVAL		5 /* Change N lines before checking typeahead */
     63 
     64 #ifndef _CURSES_USE_MACROS
     65 
     66 /*
     67  * refresh --
     68  *	Make the current screen look like "stdscr" over the area covered by
     69  *	stdscr.
     70  */
     71 int
     72 refresh(void)
     73 {
     74 
     75 	return wrefresh(stdscr);
     76 }
     77 
     78 #endif
     79 
     80 /*
     81  * wnoutrefresh --
     82  *	Add the contents of "win" to the virtual window.
     83  */
     84 int
     85 wnoutrefresh(WINDOW *win)
     86 {
     87 
     88 	__CTRACE(__CTRACE_REFRESH,
     89 	    "wnoutrefresh: win %p, begy %d, begx %d, maxy %d, maxx %d\n",
     90 	    win, win->begy, win->begx, win->maxy, win->maxx);
     91 
     92 	return _wnoutrefresh(win, 0, 0, win->begy, win->begx,
     93 	    win->maxy, win->maxx);
     94 }
     95 
     96 /*
     97  * pnoutrefresh --
     98  *	Add the contents of "pad" to the virtual window.
     99  */
    100 int
    101 pnoutrefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
    102 	     int smaxy, int smaxx)
    103 {
    104 	int pmaxy, pmaxx;
    105 
    106 	__CTRACE(__CTRACE_REFRESH, "pnoutrefresh: pad %p, flags 0x%08x\n",
    107 	    pad, (pad != NULL) ? pad->flags : 0);
    108 	__CTRACE(__CTRACE_REFRESH,
    109 	    "pnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n",
    110 	    pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
    111 
    112 	if (__predict_false(pad == NULL))
    113 		return ERR;
    114 
    115 	/* SUS says if these are negative, they should be treated as zero */
    116 	if (pbegy < 0)
    117 		pbegy = 0;
    118 	if (pbegx < 0)
    119 		pbegx = 0;
    120 	if (sbegy < 0)
    121 		sbegy = 0;
    122 	if (sbegx < 0)
    123 		sbegx = 0;
    124 
    125 	/* Calculate rectangle on pad - used by _wnoutrefresh */
    126 	pmaxy = pbegy + smaxy - sbegy + 1;
    127 	pmaxx = pbegx + smaxx - sbegx + 1;
    128 
    129 	/* Check rectangle fits in pad */
    130 	if (pmaxy > pad->maxy - pad->begy)
    131 		pmaxy = pad->maxy - pad->begy;
    132 	if (pmaxx > pad->maxx - pad->begx)
    133 		pmaxx = pad->maxx - pad->begx;
    134 
    135 	if (smaxy - sbegy < 0 || smaxx - sbegx < 0 )
    136 		return ERR;
    137 
    138 	return _wnoutrefresh(pad,
    139 	    pad->begy + pbegy, pad->begx + pbegx, pad->begy + sbegy,
    140 	    pad->begx + sbegx, pmaxy, pmaxx);
    141 }
    142 
    143 /*
    144  * _wnoutrefresh --
    145  *	Does the grunt work for wnoutrefresh to the given screen.
    146  *	Copies the part of the window given by the rectangle
    147  *	(begy, begx) to (maxy, maxx) at screen position (wbegy, wbegx).
    148  */
    149 static int
    150 _wnoutrefresh(WINDOW *win, int begy, int begx, int wbegy, int wbegx,
    151               int maxy, int maxx)
    152 {
    153 	SCREEN *screen;
    154 	short	sy, wy, wx, y_off, x_off, mx, dy_off, dx_off, endy;
    155 	int newy, newx;
    156 #ifdef HAVE_WCHAR
    157 	int i, tx;
    158 	wchar_t ch;
    159 #endif
    160 	__LINE	*wlp, *vlp, *dwlp;
    161 	WINDOW	*sub_win, *orig, *swin, *dwin;
    162 
    163 	__CTRACE(__CTRACE_REFRESH, "_wnoutrefresh: win %p, flags 0x%08x\n",
    164 	    win, (win != NULL) ? win->flags : 0);
    165 	__CTRACE(__CTRACE_REFRESH,
    166 	    "_wnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n",
    167 	    begy, begx, wbegy, wbegx, maxy, maxx);
    168 
    169 	if (__predict_false(win == NULL))
    170 		return ERR;
    171 
    172 	screen = win->screen;
    173 
    174 	if (screen->curwin)
    175 		return OK;
    176 
    177 	swin = dwin = win;
    178 	if (win->flags & __ISDERWIN)
    179 		swin = win->orig;
    180 
    181 	/*
    182 	 * Recurse through any sub-windows, mark as dirty lines on the parent
    183 	 * window that are dirty on the sub-window and clear the dirty flag on
    184 	 * the sub-window.
    185 	 */
    186 	if (dwin->orig == 0) {
    187 		orig = dwin;
    188 		for (sub_win = dwin->nextp; sub_win != orig;
    189 		    sub_win = sub_win->nextp) {
    190 			if (sub_win->flags & __ISDERWIN)
    191 				continue;
    192 			__CTRACE(__CTRACE_REFRESH,
    193 			    "wnout_refresh: win %p, sub_win %p\n",
    194 			    orig, sub_win);
    195 			for (sy = 0; sy < sub_win->maxy; sy++) {
    196 				if (sub_win->alines[sy]->flags & __ISDIRTY) {
    197 					orig->alines[sy + sub_win->begy - orig->begy]->flags
    198 					    |= __ISDIRTY;
    199 					sub_win->alines[sy]->flags
    200 					    &= ~__ISDIRTY;
    201 				}
    202 				if (sub_win->alines[sy]->flags & __ISFORCED) {
    203 					orig->alines[sy + sub_win->begy - orig->begy]->flags
    204 					    |= __ISFORCED;
    205 					sub_win->alines[sy]->flags
    206 					    &= ~__ISFORCED;
    207 				}
    208 			}
    209 		}
    210 	}
    211 
    212 	/* Check that cursor position on "win" is valid for "__virtscr" */
    213 	newy = wbegy + dwin->cury - begy;
    214 	newx = wbegx + dwin->curx - begx;
    215 	if (begy <= dwin->cury && dwin->cury < maxy
    216 	    && 0 <= newy && newy < screen->__virtscr->maxy)
    217 		screen->__virtscr->cury = newy;
    218 	if (begx <= dwin->curx && dwin->curx < maxx
    219 	    && 0 <= newx && newx < screen->__virtscr->maxx)
    220 		screen->__virtscr->curx = newx;
    221 
    222 	/* Copy the window flags from "win" to "__virtscr" */
    223 	if (dwin->flags & __CLEAROK) {
    224 		if (dwin->flags & __FULLWIN)
    225 			screen->__virtscr->flags |= __CLEAROK;
    226 		dwin->flags &= ~__CLEAROK;
    227 	}
    228 	screen->__virtscr->flags &= ~__LEAVEOK;
    229 	screen->__virtscr->flags |= dwin->flags;
    230 
    231 	/* copy the background char and attributes from win to __virtscr */
    232 	screen->__virtscr->bch = win->bch;
    233 	screen->__virtscr->battr = win->battr;
    234 
    235 	if ((dwin->flags & __ISDERWIN) != 0)
    236 		endy = begy + maxy;
    237 	else
    238 		endy = maxy;
    239 
    240 	for (wy = begy, y_off = wbegy, dy_off = 0; wy < endy &&
    241 	    y_off < screen->__virtscr->maxy; wy++, y_off++, dy_off++)
    242 	{
    243 		wlp = swin->alines[wy];
    244 		dwlp = dwin->alines[dy_off];
    245 #ifdef DEBUG
    246 		__CTRACE(__CTRACE_REFRESH,
    247 		    "_wnoutrefresh: wy %d\tf %d\tl %d\tflags %x\n",
    248 		    wy, *wlp->firstchp, *wlp->lastchp, wlp->flags);
    249 
    250 		char *_wintype;
    251 
    252 		if ((dwin->flags & __ISDERWIN) != 0)
    253 			_wintype = "derwin";
    254 		else
    255 			_wintype = "dwin";
    256 
    257 		__CTRACE(__CTRACE_REFRESH,
    258 		    "_wnoutrefresh: %s wy %d\tf %d\tl %d\tflags %x\n",
    259 		    _wintype, dy_off, *dwlp->firstchp, *dwlp->lastchp,
    260 		    dwlp->flags);
    261 		__CTRACE(__CTRACE_REFRESH,
    262 		    "_wnoutrefresh: %s maxx %d\tch_off %d wlp %p\n",
    263 		    _wintype, dwin->maxx, dwin->ch_off, wlp);
    264 #endif
    265 		if (((wlp->flags & (__ISDIRTY | __ISFORCED)) == 0) &&
    266 		    ((dwlp->flags & (__ISDIRTY | __ISFORCED)) == 0))
    267 			continue;
    268 		__CTRACE(__CTRACE_REFRESH,
    269 		    "_wnoutrefresh: line y_off %d (dy_off %d) is dirty\n",
    270 		    y_off, dy_off);
    271 
    272 		wlp = swin->alines[wy];
    273 		vlp = screen->__virtscr->alines[y_off];
    274 
    275 		if ((*wlp->firstchp < maxx + swin->ch_off &&
    276 		    *wlp->lastchp >= swin->ch_off) ||
    277 		    ((((dwin->flags & __ISDERWIN) != 0) &&
    278 		     (*dwlp->firstchp < dwin->maxx + dwin->ch_off &&
    279 		      *dwlp->lastchp >= dwin->ch_off))))
    280 		{
    281 			/* Set start column */
    282 			wx = begx;
    283 			x_off = wbegx;
    284 			dx_off = 0;
    285 			/*
    286 			 * if a derwin then source change pointers aren't
    287 			 * relevant.
    288 			 */
    289 			if ((dwin->flags & __ISDERWIN) != 0)
    290 				mx = wx + maxx;
    291 			else {
    292 				if (*wlp->firstchp - swin->ch_off > 0) {
    293 					wx += *wlp->firstchp - swin->ch_off;
    294 					x_off += *wlp->firstchp - swin->ch_off;
    295 				}
    296 				mx = maxx;
    297 				if (mx > *wlp->lastchp - swin->ch_off + 1)
    298 					mx = *dwlp->lastchp - dwin->ch_off + 1;
    299 				if (x_off + (mx - wx) > screen->__virtscr->maxx)
    300 					mx -= (x_off + maxx) -
    301 					    screen->__virtscr->maxx;
    302 			}
    303 
    304 			/* Copy line from "win" to "__virtscr". */
    305 			while (wx < mx) {
    306 				__CTRACE(__CTRACE_REFRESH,
    307 				    "_wnoutrefresh: copy from %d, "
    308 				    "%d to %d, %d: '%s', 0x%x, 0x%x",
    309 				    wy, wx, y_off, x_off,
    310 				    unctrl(wlp->line[wx].ch),
    311 				    wlp->line[wx].attr, wlp->line[wx].cflags);
    312 				__CTRACE(__CTRACE_REFRESH,
    313 				    " (curdest %s, 0x%x, 0x%x)",
    314 				    unctrl(vlp->line[x_off].ch),
    315 				    vlp->line[x_off].attr,
    316 				    vlp->line[x_off].cflags);
    317 				/* Copy character */
    318 				vlp->line[x_off].ch = wlp->line[wx].ch;
    319 				/* Copy attributes  */
    320 				vlp->line[x_off].attr = wlp->line[wx].attr;
    321 				/* Copy character flags  */
    322 				vlp->line[x_off].cflags = wlp->line[wx].cflags;
    323 #ifdef HAVE_WCHAR
    324 				vlp->line[x_off].wcols = wlp->line[wx].wcols;
    325 
    326 				ch = wlp->line[wx].ch;
    327 				for (tx = x_off + 1, i = wlp->line[wx].wcols - 1;
    328 				    i > 0; i--, tx++) {
    329 					vlp->line[tx].ch = ch;
    330 					vlp->line[tx].wcols = i;
    331 					vlp->line[tx].cflags =
    332 					    CA_CONTINUATION;
    333 				}
    334 #endif /* HAVE_WCHAR */
    335 
    336 				if (win->flags & __ISDERWIN) {
    337 					dwlp->line[dx_off].ch =
    338 						wlp->line[wx].ch;
    339 					dwlp->line[dx_off].attr =
    340 						wlp->line[wx].attr;
    341 					dwlp->line[dx_off].cflags =
    342 						wlp->line[wx].cflags;
    343 #ifdef HAVE_WCHAR
    344 					dwlp->line[dx_off].wcols =
    345 						wlp->line[wx].wcols;
    346 
    347 					for (tx = dx_off + 1, i = wlp->line[wx].wcols - 1;
    348 					    i > 0; i--, tx++) {
    349 						dwlp->line[tx].ch = ch;
    350 						dwlp->line[tx].wcols = i;
    351 						dwlp->line[tx].cflags =
    352 						    CA_CONTINUATION;
    353 					}
    354 #endif /* HAVE_WCHAR */
    355 				}
    356 
    357 #ifdef HAVE_WCHAR
    358 				if (wlp->line[wx].ch == win->bch) {
    359 					vlp->line[x_off].ch = win->bch;
    360 					vlp->line[x_off].wcols = win->wcols;
    361 					vlp->line[x_off].cflags = CA_BACKGROUND;
    362 					if (_cursesi_copy_nsp(win->bnsp,
    363 							      &vlp->line[x_off])
    364 					    == ERR)
    365 						return ERR;
    366 					if (win->flags & __ISDERWIN) {
    367 						dwlp->line[dx_off].ch =
    368 							win->bch;
    369 						dwlp->line[dx_off].wcols =
    370 						    win->wcols;
    371 						dwlp->line[dx_off].cflags =
    372 							wlp->line[wx].cflags;
    373 						if (_cursesi_copy_nsp(win->bnsp,
    374 						     &dwlp->line[dx_off])
    375 						    == ERR)
    376 							return ERR;
    377 					}
    378 				}
    379 #endif /* HAVE_WCHAR */
    380 
    381 				__CTRACE(__CTRACE_REFRESH, " = '%s', 0x%x\n",
    382 				    unctrl(vlp->line[x_off].ch),
    383 				    vlp->line[x_off].attr);
    384 #ifdef HAVE_WCHAR
    385 				x_off += wlp->line[wx].wcols;
    386 				dx_off += wlp->line[wx].wcols;
    387 				wx += wlp->line[wx].wcols;
    388 #else
    389 				wx++;
    390 				x_off++;
    391 				dx_off++;
    392 #endif /* HAVE_WCHAR */
    393 			}
    394 
    395 			/* Set flags on "__virtscr" and unset on "win". */
    396 			if (wlp->flags & __ISPASTEOL)
    397 				vlp->flags |= __ISPASTEOL;
    398 			else
    399 				vlp->flags &= ~__ISPASTEOL;
    400 			if (wlp->flags & __ISDIRTY)
    401 				vlp->flags |= __ISDIRTY;
    402 			if (wlp->flags & __ISFORCED)
    403 				vlp->flags |= __ISFORCED;
    404 
    405 #ifdef DEBUG
    406 			__CTRACE(__CTRACE_REFRESH,
    407 			    "win: firstch = %d, lastch = %d\n",
    408 			    *wlp->firstchp, *wlp->lastchp);
    409 			if (win->flags & __ISDERWIN) {
    410 				__CTRACE(__CTRACE_REFRESH,
    411 				    "derwin: firstch = %d, lastch = %d\n",
    412 				    *dwlp->firstchp, *dwlp->lastchp);
    413 			}
    414 #endif
    415 			/* Set change pointers on "__virtscr". */
    416 			if (*vlp->firstchp >
    417 				*wlp->firstchp + wbegx - win->ch_off)
    418 					*vlp->firstchp =
    419 					    *wlp->firstchp + wbegx - win->ch_off;
    420 			if (*vlp->lastchp <
    421 				*wlp->lastchp + wbegx - win->ch_off)
    422 					*vlp->lastchp =
    423 					    *wlp->lastchp + wbegx - win->ch_off;
    424 
    425 			if (win->flags & __ISDERWIN) {
    426 				if (*vlp->firstchp >
    427 				    *dwlp->firstchp + wbegx - dwin->ch_off)
    428 				{
    429 					*vlp->firstchp =
    430 					    *dwlp->firstchp + wbegx
    431 						- dwin->ch_off;
    432 					vlp->flags |= __ISDIRTY;
    433 				}
    434 
    435 				if (*vlp->lastchp <
    436 				    *dwlp->lastchp + wbegx - dwin->ch_off)
    437 				{
    438 					*vlp->lastchp = *dwlp->lastchp
    439 					    + wbegx - dwin->ch_off;
    440 					vlp->flags |= __ISDIRTY;
    441 				}
    442 			}
    443 
    444 			__CTRACE(__CTRACE_REFRESH,
    445 			    "__virtscr: firstch = %d, lastch = %d, flags 0x%x\n",
    446 			    *vlp->firstchp, *vlp->lastchp, vlp->flags);
    447 			/*
    448 			 * Unset change pointers only if a window
    449 			 * A pad can be displayed again without any
    450 			 * of the contents changing.
    451 			 */
    452 			if (!(win->flags & __ISPAD))
    453 			{
    454 				/* Set change pointers on "win". */
    455 				if (*wlp->firstchp >= win->ch_off)
    456 					*wlp->firstchp = maxx + win->ch_off;
    457 				if (*wlp->lastchp < maxx + win->ch_off)
    458 					*wlp->lastchp = win->ch_off;
    459 				if ((*wlp->lastchp < *wlp->firstchp) ||
    460 				    (*wlp->firstchp >= maxx + win->ch_off) ||
    461 				    (*wlp->lastchp <= win->ch_off)) {
    462 					__CTRACE(__CTRACE_REFRESH,
    463 					    "_wnoutrefresh: "
    464 					    "line %d notdirty\n", wy);
    465 					wlp->flags &= ~(__ISDIRTY | __ISFORCED);
    466 				}
    467 			}
    468 		}
    469 	}
    470 	return OK;
    471 }
    472 
    473 /*
    474  * wrefresh --
    475  *	Make the current screen look like "win" over the area covered by
    476  *	win.
    477  */
    478 int
    479 wrefresh(WINDOW *win)
    480 {
    481 	int retval;
    482 	int pbegx, pbegy;
    483 
    484 	__CTRACE(__CTRACE_REFRESH, "wrefresh: win %p\n", win);
    485 
    486 	if (__predict_false(win == NULL))
    487 		return ERR;
    488 
    489 	_cursesi_screen->curwin = (win == _cursesi_screen->curscr);
    490 	if (!_cursesi_screen->curwin) {
    491 		pbegx = pbegy = 0;
    492 		if ((win->flags & __ISDERWIN) == __ISDERWIN) {
    493 			pbegx = win->derx;
    494 			pbegy = win->dery;
    495 			__CTRACE(__CTRACE_REFRESH, "wrefresh: derwin, "
    496 			    "begy = %d, begx = %x\n",
    497 			    pbegy, pbegx);
    498 		}
    499 		wsyncdown(win);
    500 		retval = _wnoutrefresh(win, pbegy, pbegx, win->begy, win->begx,
    501 		    win->maxy, win->maxx);
    502 	} else
    503 		retval = OK;
    504 	if (retval == OK) {
    505 		retval = doupdate();
    506 		if (!(win->flags & __LEAVEOK)) {
    507 			win->cury = max(0, curscr->cury - win->begy);
    508 			win->curx = max(0, curscr->curx - win->begx);
    509 		}
    510 	}
    511 	_cursesi_screen->curwin = 0;
    512 	return retval;
    513 }
    514 
    515  /*
    516  * prefresh --
    517  *	Make the current screen look like "pad" over the area coverd by
    518  *	the specified area of pad.
    519  */
    520 int
    521 prefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
    522 	int smaxy, int smaxx)
    523 {
    524 	int retval;
    525 
    526 	__CTRACE(__CTRACE_REFRESH, "prefresh: pad %p, flags 0x%08x\n",
    527 	    pad, (pad != NULL) ? pad->flags : 0);
    528 
    529 	if (__predict_false(pad == NULL))
    530 		return ERR;
    531 
    532 	/* Retain values in case pechochar() is called. */
    533 	pad->pbegy = pbegy;
    534 	pad->pbegx = pbegx;
    535 	pad->sbegy = sbegy;
    536 	pad->sbegx = sbegx;
    537 	pad->smaxy = smaxy;
    538 	pad->smaxx = smaxx;
    539 
    540 	/* Use pnoutrefresh() to avoid duplicating code here */
    541 	retval = pnoutrefresh(pad, pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
    542 	if (retval == OK) {
    543 		retval = doupdate();
    544 		if (!(pad->flags & __LEAVEOK)) {
    545 			pad->cury = max(0, pbegy + (curscr->cury - sbegy));
    546 			pad->curx = max(0, pbegx + (curscr->curx - sbegx));
    547 		}
    548 	}
    549 	return retval;
    550 }
    551 
    552 /*
    553  * doupdate --
    554  *	Make the current screen look like the virtual window "__virtscr".
    555  */
    556 int
    557 doupdate(void)
    558 {
    559 	WINDOW	*win;
    560 	__LINE	*wlp, *vlp;
    561 	short	 wy;
    562 	int	 dnum, was_cleared, changed;
    563 
    564 	/* Check if we need to restart ... */
    565 	if (_cursesi_screen->endwin)
    566 		__restartwin();
    567 
    568 	if (_cursesi_screen->curwin)
    569 		win = curscr;
    570 	else
    571 		win = _cursesi_screen->__virtscr;
    572 
    573 	/* Initialize loop parameters. */
    574 	_cursesi_screen->ly = curscr->cury;
    575 	_cursesi_screen->lx = curscr->curx;
    576 	wy = 0;
    577 
    578 	if (!_cursesi_screen->curwin) {
    579 		for (wy = 0; wy < win->maxy; wy++) {
    580 			wlp = curscr->alines[wy];
    581 			if (wlp->flags & __ISDIRTY)
    582 				wlp->hash = __hash_line(wlp->line, curscr->maxx);
    583 		}
    584 	}
    585 
    586 	was_cleared = 0;
    587 	if ((win->flags & __CLEAROK) || (curscr->flags & __CLEAROK) ||
    588 	    _cursesi_screen->curwin)
    589 	{
    590 		if (curscr->wattr & __COLOR)
    591 			__unsetattr(0);
    592 		tputs(clear_screen, 0, __cputchar);
    593 		_cursesi_screen->ly = 0;
    594 		_cursesi_screen->lx = 0;
    595 		if (!_cursesi_screen->curwin) {
    596 			curscr->flags &= ~__CLEAROK;
    597 			curscr->cury = 0;
    598 			curscr->curx = 0;
    599 			werase(curscr);
    600 		}
    601 		__touchwin(win, 0);
    602 		win->flags &= ~__CLEAROK;
    603 		/* note we cleared for later */
    604 		was_cleared = 1;
    605 	}
    606 	if (!cursor_address) {
    607 		if (win->curx != 0)
    608 			__cputchar('\n');
    609 		if (!_cursesi_screen->curwin)
    610 			werase(curscr);
    611 	}
    612 	__CTRACE(__CTRACE_REFRESH, "doupdate: (%p): curwin = %d\n", win,
    613 	    _cursesi_screen->curwin);
    614 	__CTRACE(__CTRACE_REFRESH, "doupdate: \tfirstch\tlastch\n");
    615 
    616 	/* curscr may have been cleared, rehash the dirty lines */
    617 	for (wy = 0; wy < win->maxy; wy++) {
    618 		wlp = curscr->alines[wy];
    619 		if (wlp->flags & __ISDIRTY)
    620 			wlp->hash = __hash_line(wlp->line, curscr->maxx);
    621 	}
    622 
    623 	/* and the same with virtscr... */
    624 	for (wy = 0; wy < __virtscr->maxy; wy++) {
    625 		wlp = __virtscr->alines[wy];
    626 		if (wlp->flags & __ISDIRTY)
    627 			wlp->hash = __hash_line(wlp->line, __virtscr->maxx);
    628 	}
    629 
    630 	if (!_cursesi_screen->curwin) {
    631 		/*
    632 		 * Invoke quickch() only if more than a quarter of the lines
    633 		 * in the window are dirty and we didn't just clear.
    634 		 */
    635 		for (wy = 0, dnum = 0; wy < win->maxy; wy++)
    636 			if (win->alines[wy]->flags & __ISDIRTY)
    637 				dnum++;
    638 		if ((!__noqch) && (was_cleared == 0)
    639 		    && (dnum > (int) win->maxy / 4))
    640 			quickch();
    641 	}
    642 
    643 #ifdef DEBUG
    644 	{
    645 		int	i, j;
    646 
    647 		__CTRACE(__CTRACE_REFRESH,
    648 		    "#####################################\n");
    649 		__CTRACE(__CTRACE_REFRESH,
    650 		    "stdscr(%p)-curscr(%p)-__virtscr(%p)\n",
    651 		    stdscr, curscr, _cursesi_screen->__virtscr);
    652 		for (i = 0; i < curscr->maxy; i++) {
    653 			__CTRACE(__CTRACE_REFRESH, "curscr: %d:", i);
    654 			__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
    655 			    curscr->alines[i]->hash);
    656 			for (j = 0; j < curscr->maxx; j++)
    657 				__CTRACE(__CTRACE_REFRESH, "%c",
    658 				    curscr->alines[i]->line[j].ch);
    659 			__CTRACE(__CTRACE_REFRESH, "\n");
    660 			__CTRACE(__CTRACE_REFRESH, " attr:");
    661 			for (j = 0; j < curscr->maxx; j++)
    662 				__CTRACE(__CTRACE_REFRESH, " %x",
    663 				    curscr->alines[i]->line[j].attr);
    664 			__CTRACE(__CTRACE_REFRESH, "\n");
    665 #ifdef HAVE_WCHAR
    666 			__CTRACE(__CTRACE_REFRESH, " wcols:");
    667 			for (j = 0; j < curscr->maxx; j++)
    668 				__CTRACE(__CTRACE_REFRESH, " %d",
    669 				    curscr->alines[i]->line[j].wcols);
    670 			__CTRACE(__CTRACE_REFRESH, "\n");
    671 
    672 			__CTRACE(__CTRACE_REFRESH, " cflags:");
    673 			for (j = 0; j < curscr->maxx; j++)
    674 				__CTRACE(__CTRACE_REFRESH, " 0x%x",
    675 				    curscr->alines[i]->line[j].cflags);
    676 			__CTRACE(__CTRACE_REFRESH, "\n");
    677 #endif /* HAVE_WCHAR */
    678 			__CTRACE(__CTRACE_REFRESH, "win %p: %d:", win, i);
    679 			__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
    680 			    win->alines[i]->hash);
    681 			__CTRACE(__CTRACE_REFRESH, " 0x%x ",
    682 			    win->alines[i]->flags);
    683 			for (j = 0; j < win->maxx; j++)
    684 				__CTRACE(__CTRACE_REFRESH, "%c",
    685 				    win->alines[i]->line[j].ch);
    686 			__CTRACE(__CTRACE_REFRESH, "\n");
    687 			__CTRACE(__CTRACE_REFRESH, " attr:");
    688 			for (j = 0; j < win->maxx; j++)
    689 				__CTRACE(__CTRACE_REFRESH, " %x",
    690 				    win->alines[i]->line[j].attr);
    691 			__CTRACE(__CTRACE_REFRESH, "\n");
    692 #ifdef HAVE_WCHAR
    693 			__CTRACE(__CTRACE_REFRESH, " wcols:");
    694 			for (j = 0; j < win->maxx; j++)
    695 				__CTRACE(__CTRACE_REFRESH, " %d",
    696 				    win->alines[i]->line[j].wcols);
    697 			__CTRACE(__CTRACE_REFRESH, "\n");
    698 			__CTRACE(__CTRACE_REFRESH, " cflags:");
    699 			for (j = 0; j < win->maxx; j++)
    700 				__CTRACE(__CTRACE_REFRESH, " 0x%x",
    701 				    win->alines[i]->line[j].cflags);
    702 			__CTRACE(__CTRACE_REFRESH, "\n");
    703 			__CTRACE(__CTRACE_REFRESH, " nsp:");
    704 			for (j = 0; j < curscr->maxx; j++)
    705 				__CTRACE(__CTRACE_REFRESH, " %p",
    706 				    win->alines[i]->line[j].nsp);
    707 			__CTRACE(__CTRACE_REFRESH, "\n");
    708 			__CTRACE(__CTRACE_REFRESH, " bnsp:");
    709 			for (j = 0; j < curscr->maxx; j++)
    710 				__CTRACE(__CTRACE_REFRESH, " %p",
    711 				    win->bnsp);
    712 			__CTRACE(__CTRACE_REFRESH, "\n");
    713 #endif /* HAVE_WCHAR */
    714 		}
    715 	}
    716 #endif /* DEBUG */
    717 
    718 	changed = 0;
    719 	for (wy = 0; wy < win->maxy; wy++) {
    720 		wlp = win->alines[wy];
    721 		vlp = _cursesi_screen->__virtscr->alines[win->begy + wy];
    722 /* XXX: remove this */
    723 		__CTRACE(__CTRACE_REFRESH,
    724 		    "doupdate: wy %d\tf: %d\tl:%d\tflags %x\n",
    725 		    wy, *wlp->firstchp, *wlp->lastchp, wlp->flags);
    726 		if (!_cursesi_screen->curwin)
    727 			curscr->alines[wy]->hash = wlp->hash;
    728 		if (wlp->flags & __ISDIRTY || wlp->flags & __ISFORCED) {
    729 			__CTRACE(__CTRACE_REFRESH,
    730 			    "doupdate: [ISDIRTY]wy:%d\tf:%d\tl:%d\n", wy,
    731 			    *wlp->firstchp, *wlp->lastchp);
    732 			/*
    733 			 * We have just cleared so don't force an update
    734 			 * otherwise we spray needless blanks to a cleared
    735 			 * screen.  That is, unless, we are using color,
    736 			 * in this case we need to force the background
    737 			 * color to default.
    738 			 */
    739 			if ((was_cleared == 1) && (__using_color == 0))
    740 				win->alines[wy]->flags &= ~ 0L;
    741 			/*if ((was_cleared == 1) && (__using_color == 0))
    742 				win->alines[wy]->flags &= ~__ISFORCED;*/
    743 
    744 			if (makech(wy) == ERR)
    745 				return ERR;
    746 			else {
    747 				if (*wlp->firstchp >= 0)
    748 					*wlp->firstchp = win->maxx;
    749 				if (*wlp->lastchp < win->maxx)
    750 					*wlp->lastchp = win->ch_off;
    751 				if (*wlp->lastchp < *wlp->firstchp) {
    752 					__CTRACE(__CTRACE_REFRESH,
    753 					    "doupdate: line %d notdirty\n", wy);
    754 					wlp->flags &= ~(__ISDIRTY | __ISFORCED);
    755 				}
    756 
    757 				/* Check if we have input after
    758 				 * changing N lines. */
    759 				if (_cursesi_screen->checkfd != -1 &&
    760 				    ++changed == CHECK_INTERVAL)
    761 				{
    762 					struct pollfd fds[1];
    763 
    764 					/* If we have input, abort. */
    765 					fds[0].fd = _cursesi_screen->checkfd;
    766 					fds[0].events = POLLIN;
    767 					if (poll(fds, 1, 0) > 0)
    768 						goto cleanup;
    769 					changed = 0;
    770 				}
    771 			}
    772 		}
    773 
    774 		/*
    775 		 * virtscr is now synced for the line, unset the change
    776 		 * pointers.
    777 		 */
    778 		if (*vlp->firstchp >= 0)
    779 			*vlp->firstchp = _cursesi_screen->__virtscr->maxx;
    780 		if (*vlp->lastchp <= _cursesi_screen->__virtscr->maxx)
    781 			*vlp->lastchp = 0;
    782 
    783 		__CTRACE(__CTRACE_REFRESH, "\t%d\t%d\n",
    784 		    *wlp->firstchp, *wlp->lastchp);
    785 	}
    786 
    787 	__CTRACE(__CTRACE_REFRESH, "doupdate: ly=%d, lx=%d\n",
    788 	    _cursesi_screen->ly, _cursesi_screen->lx);
    789 
    790 	if (_cursesi_screen->curwin)
    791 		domvcur(win, _cursesi_screen->ly, _cursesi_screen->lx,
    792 			win->cury, win->curx);
    793 	else {
    794 		if (win->flags & __LEAVEOK) {
    795 			curscr->cury = _cursesi_screen->ly;
    796 			curscr->curx = _cursesi_screen->lx;
    797 		} else {
    798 			domvcur(win, _cursesi_screen->ly, _cursesi_screen->lx,
    799 				win->cury, win->curx);
    800 			curscr->cury = win->cury;
    801 			curscr->curx = win->curx;
    802 		}
    803 	}
    804 
    805 cleanup:
    806 	/* Don't leave the screen with attributes set. */
    807 	__unsetattr(0);
    808 #ifdef DEBUG
    809 #ifdef HAVE_WCHAR
    810 	{
    811 		int	i, j;
    812 
    813 		__CTRACE(__CTRACE_REFRESH,
    814 		    "***********after*****************\n");
    815 		__CTRACE(__CTRACE_REFRESH,
    816 		    "stdscr(%p)-curscr(%p)-__virtscr(%p)\n",
    817 		    stdscr, curscr, _cursesi_screen->__virtscr);
    818 		for (i = 0; i < curscr->maxy; i++) {
    819 			for (j = 0; j < curscr->maxx; j++)
    820 				__CTRACE(__CTRACE_REFRESH,
    821 				    "[%d,%d](%x,%x,%d,%x,%p)-(%x,%x,%d,%x,%p)-(%x,%x,%d,%x,%p)\n",
    822 				    i, j,
    823 				    stdscr->alines[i]->line[j].ch,
    824 				    stdscr->alines[i]->line[j].attr,
    825 				    stdscr->alines[i]->line[j].wcols,
    826 				    stdscr->alines[i]->line[j].cflags,
    827 				    stdscr->alines[i]->line[j].nsp,
    828 				    curscr->alines[i]->line[j].ch,
    829 				    curscr->alines[i]->line[j].attr,
    830 				    curscr->alines[i]->line[j].wcols,
    831 				    curscr->alines[i]->line[j].cflags,
    832 				    curscr->alines[i]->line[j].nsp,
    833 				    _cursesi_screen->__virtscr->alines[i]->line[j].ch,
    834 				    _cursesi_screen->__virtscr->alines[i]->line[j].attr,
    835 				    _cursesi_screen->__virtscr->alines[i]->line[j].wcols,
    836 				    _cursesi_screen->__virtscr->alines[i]->line[j].cflags,
    837 				    _cursesi_screen->__virtscr->alines[i]->line[j].nsp);
    838 		}
    839 	}
    840 #endif /* HAVE_WCHAR */
    841 #endif /* DEBUG */
    842 	return fflush(_cursesi_screen->outfd) == EOF ? ERR : OK;
    843 }
    844 
    845 static void
    846 putattr(__LDATA *nsp)
    847 {
    848 	attr_t	off, on;
    849 
    850 	__CTRACE(__CTRACE_REFRESH,
    851 	    "putattr: have attr %08x, need attr %08x\n",
    852 	    curscr->wattr
    853 #ifndef HAVE_WCHAR
    854 	    & __ATTRIBUTES
    855 #else
    856 	    & WA_ATTRIBUTES
    857 #endif
    858 	    ,  nsp->attr
    859 #ifndef HAVE_WCHAR
    860 	    & __ATTRIBUTES
    861 #else
    862 	    & WA_ATTRIBUTES
    863 #endif
    864 	    );
    865 
    866 	off = (~nsp->attr & curscr->wattr)
    867 #ifndef HAVE_WCHAR
    868 	    & __ATTRIBUTES
    869 #else
    870 	    & WA_ATTRIBUTES
    871 #endif
    872 	    ;
    873 
    874 	/*
    875 	 * Unset attributes as appropriate.  Unset first
    876 	 * so that the relevant attributes can be reset
    877 	 * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
    878 	 * 'mp' and 'mr').  Check to see if we also turn off
    879 	 * standout, attributes and colour.
    880 	 */
    881 	if (off & __TERMATTR && exit_attribute_mode != NULL) {
    882 		tputs(exit_attribute_mode, 0, __cputchar);
    883 		/* exit_attribute_mode resets the color to terminal default */
    884 		_cursesi_screen->curpair = -1;
    885 		curscr->wattr &= __mask_me;
    886 		off &= __mask_me;
    887 	}
    888 
    889 	/*
    890 	 * Exit underscore mode if appropriate.
    891 	 * Check to see if we also turn off standout,
    892 	 * attributes and colour.
    893 	 */
    894 	if (off & __UNDERSCORE && exit_underline_mode != NULL) {
    895 		tputs(exit_underline_mode, 0, __cputchar);
    896 		curscr->wattr &= __mask_ue;
    897 		off &= __mask_ue;
    898 	}
    899 
    900 	/*
    901 	 * Exit standout mode as appropriate.
    902 	 * Check to see if we also turn off underscore,
    903 	 * attributes and colour.
    904 	 * XXX
    905 	 * Should use uc if so/se not available.
    906 	 */
    907 	if (off & __STANDOUT && exit_standout_mode != NULL) {
    908 		tputs(exit_standout_mode, 0, __cputchar);
    909 		curscr->wattr &= __mask_se;
    910 		off &= __mask_se;
    911 	}
    912 
    913 	if (off & __ALTCHARSET && exit_alt_charset_mode != NULL) {
    914 		tputs(exit_alt_charset_mode, 0, __cputchar);
    915 		curscr->wattr &= ~__ALTCHARSET;
    916 	}
    917 
    918 	/* Set/change colour as appropriate. */
    919 	if (__using_color)
    920 		__set_color(curscr, nsp->attr & __COLOR);
    921 
    922 	on = (nsp->attr & ~curscr->wattr)
    923 #ifndef HAVE_WCHAR
    924 	    & __ATTRIBUTES
    925 #else
    926 	    & WA_ATTRIBUTES
    927 #endif
    928 	    ;
    929 
    930 	/*
    931 	 * Enter standout mode if appropriate.
    932 	 */
    933 	if (on & __STANDOUT &&
    934 	    enter_standout_mode != NULL &&
    935 	    exit_standout_mode != NULL)
    936 	{
    937 		tputs(enter_standout_mode, 0, __cputchar);
    938 		curscr->wattr |= __STANDOUT;
    939 	}
    940 
    941 	/*
    942 	 * Enter underscore mode if appropriate.
    943 	 * XXX
    944 	 * Should use uc if us/ue not available.
    945 	 */
    946 	if (on & __UNDERSCORE &&
    947 	    enter_underline_mode != NULL &&
    948 	    exit_underline_mode != NULL)
    949 	{
    950 		tputs(enter_underline_mode, 0, __cputchar);
    951 		curscr->wattr |= __UNDERSCORE;
    952 	}
    953 
    954 	/*
    955 	 * Set other attributes as appropriate.
    956 	 */
    957 	if (exit_attribute_mode != NULL) {
    958 		if (on & __BLINK && enter_blink_mode != NULL)
    959 		{
    960 			tputs(enter_blink_mode, 0, __cputchar);
    961 			curscr->wattr |= __BLINK;
    962 		}
    963 		if (on & __BOLD && enter_bold_mode != NULL)
    964 		{
    965 			tputs(enter_bold_mode, 0, __cputchar);
    966 			curscr->wattr |= __BOLD;
    967 		}
    968 		if (on & __DIM && enter_dim_mode != NULL)
    969 		{
    970 			tputs(enter_dim_mode, 0, __cputchar);
    971 			curscr->wattr |= __DIM;
    972 		}
    973 		if (on & __BLANK && enter_secure_mode != NULL)
    974 		{
    975 			tputs(enter_secure_mode, 0, __cputchar);
    976 			curscr->wattr |= __BLANK;
    977 		}
    978 		if (on & __PROTECT && enter_protected_mode != NULL)
    979 		{
    980 			tputs(enter_protected_mode, 0, __cputchar);
    981 			curscr->wattr |= __PROTECT;
    982 		}
    983 		if (on & __REVERSE && enter_reverse_mode != NULL)
    984 		{
    985 			tputs(enter_reverse_mode, 0, __cputchar);
    986 			curscr->wattr |= __REVERSE;
    987 		}
    988 #ifdef HAVE_WCHAR
    989 		if (on & WA_TOP && enter_top_hl_mode != NULL)
    990 		{
    991 			tputs(enter_top_hl_mode, 0, __cputchar);
    992 			curscr->wattr |= WA_TOP;
    993 		}
    994 		if (on & WA_LOW && enter_low_hl_mode != NULL)
    995 		{
    996 			tputs(enter_low_hl_mode, 0, __cputchar);
    997 			curscr->wattr |= WA_LOW;
    998 		}
    999 		if (on & WA_LEFT && enter_left_hl_mode != NULL)
   1000 		{
   1001 			tputs(enter_left_hl_mode, 0, __cputchar);
   1002 			curscr->wattr |= WA_LEFT;
   1003 		}
   1004 		if (on & WA_RIGHT && enter_right_hl_mode != NULL)
   1005 		{
   1006 			tputs(enter_right_hl_mode, 0, __cputchar);
   1007 			curscr->wattr |= WA_RIGHT;
   1008 		}
   1009 		if (on & WA_HORIZONTAL && enter_horizontal_hl_mode != NULL)
   1010 		{
   1011 			tputs(enter_horizontal_hl_mode, 0, __cputchar);
   1012 			curscr->wattr |= WA_HORIZONTAL;
   1013 		}
   1014 		if (on & WA_VERTICAL && enter_vertical_hl_mode != NULL)
   1015 		{
   1016 			tputs(enter_vertical_hl_mode, 0, __cputchar);
   1017 			curscr->wattr |= WA_VERTICAL;
   1018 		}
   1019 #endif /* HAVE_WCHAR */
   1020 	}
   1021 
   1022 	/* Enter/exit altcharset mode as appropriate. */
   1023 	if (on & __ALTCHARSET && enter_alt_charset_mode != NULL &&
   1024 	    exit_alt_charset_mode != NULL) {
   1025 		tputs(enter_alt_charset_mode, 0, __cputchar);
   1026 		curscr->wattr |= __ALTCHARSET;
   1027 	}
   1028 }
   1029 
   1030 static void
   1031 putattr_out(__LDATA *nsp)
   1032 {
   1033 
   1034 	if (underline_char &&
   1035 	    ((nsp->attr & __STANDOUT) || (nsp->attr & __UNDERSCORE)))
   1036 	{
   1037 		__cputchar('\b');
   1038 		tputs(underline_char, 0, __cputchar);
   1039 	}
   1040 }
   1041 
   1042 static int
   1043 putch(__LDATA *nsp, __LDATA *csp, int wy, int wx)
   1044 {
   1045 #ifdef HAVE_WCHAR
   1046 	int i;
   1047 	__LDATA *tcsp;
   1048 #endif /* HAVE_WCHAR */
   1049 
   1050 	if (csp != NULL)
   1051 		putattr(nsp);
   1052 
   1053 	if (!_cursesi_screen->curwin && csp) {
   1054 		csp->attr = nsp->attr;
   1055 		csp->ch = nsp->ch;
   1056 		csp->cflags = nsp->cflags;
   1057 #ifdef HAVE_WCHAR
   1058 		if (_cursesi_copy_nsp(nsp->nsp, csp) == ERR)
   1059 			return ERR;
   1060 		csp->wcols = nsp->wcols;
   1061 
   1062 		if (nsp->wcols > 1) {
   1063 			tcsp = csp;
   1064 			tcsp++;
   1065 			for (i = nsp->wcols - 1; i > 0; i--) {
   1066 				tcsp->ch = csp->ch;
   1067 				tcsp->attr = csp->attr;
   1068 				tcsp->wcols = i;
   1069 				tcsp->cflags = CA_CONTINUATION;
   1070 				tcsp++;
   1071 			}
   1072 		}
   1073 #endif /* HAVE_WCHAR */
   1074 	}
   1075 
   1076 #ifndef HAVE_WCHAR
   1077 	__cputchar((int)nsp->ch);
   1078 #else
   1079 	if ((nsp->wcols <= 0) || (nsp->cflags & CA_CONTINUATION))
   1080 		goto out;
   1081 
   1082 	if (((_cursesi_screen->nca & nsp->attr) == 0) && (__using_color == 1) &&
   1083 	    csp == NULL)
   1084 		__set_color(curscr, nsp->attr & __COLOR);
   1085 	__cputwchar((int)nsp->ch);
   1086 	__CTRACE(__CTRACE_REFRESH,
   1087 	    "putch: (%d,%d)putwchar(0x%x)\n", wy, wx, nsp->ch);
   1088 
   1089 	/* Output non-spacing characters for the cell. */
   1090 	__cursesi_putnsp(nsp->nsp, wy, wx);
   1091 out:
   1092 #endif /* HAVE_WCHAR */
   1093 
   1094 	if (csp != NULL)
   1095 		putattr_out(nsp);
   1096 	return OK;
   1097 }
   1098 
   1099 static int
   1100 putchbr(__LDATA *nsp, __LDATA *csp, __LDATA *psp, int wy, int wx)
   1101 {
   1102 	int error, cw, pcw;
   1103 
   1104 	/* Can safely print to bottom right corner. */
   1105 	if (!auto_right_margin)
   1106 		return putch(nsp, csp, wy, wx);
   1107 
   1108 	/* Disable auto margins temporarily. */
   1109 	if (enter_am_mode && exit_am_mode) {
   1110 		tputs(exit_am_mode, 0, __cputchar);
   1111 		error = putch(nsp, csp, wy, wx);
   1112 		tputs(enter_am_mode, 0, __cputchar);
   1113 		return error;
   1114 	}
   1115 
   1116 	/* We need to insert characters. */
   1117 #ifdef HAVE_WCHAR
   1118 	cw = nsp->wcols;
   1119 	pcw = (psp == NULL) ? 0 : psp->wcols;
   1120 	if (cw < 1 || pcw < 1)
   1121 		return ERR; /* Nothing to insert */
   1122 
   1123 	/* When inserting a wide character, we need something other than
   1124 	 * insert_character. */
   1125 	if (pcw > 1 &&
   1126 	    !(parm_ich != NULL ||
   1127 	    (enter_insert_mode != NULL && exit_insert_mode != NULL)))
   1128 		return ERR;
   1129 #else
   1130 	cw = pcw = 1;
   1131 #endif /* HAVE_WCHAR */
   1132 
   1133 	/* Write the corner character at wx - pcw. */
   1134 	__mvcur(wy, wx, wy, wx - pcw, 1);
   1135 	if (putch(nsp, csp, wy, wx) == ERR)
   1136 		return ERR;
   1137 
   1138 	/* Move cursor back. */
   1139 	__mvcur(wy, wx - pcw + cw, wy, wx - cw, 1);
   1140 
   1141 	putattr(psp);
   1142 
   1143 	/* Enter insert mode. */
   1144 	if (pcw == 1 && insert_character != NULL)
   1145 		tputs(insert_character, 0, __cputchar);
   1146 	else if (parm_ich != NULL)
   1147 		tputs(tiparm(parm_ich, (long)pcw), 0, __cputchar);
   1148 	else if (enter_insert_mode != NULL && exit_insert_mode != NULL)
   1149 		tputs(enter_insert_mode, 0, __cputchar);
   1150 	else
   1151 		return ERR;
   1152 
   1153 	/* Insert the old character back. */
   1154 	error = putch(psp, NULL, wy, wx - pcw);
   1155 
   1156 	/* Exit insert mode. */
   1157 	if (insert_character != NULL || parm_ich != NULL)
   1158 		;
   1159 	else if (enter_insert_mode != NULL && exit_insert_mode != NULL)
   1160 		tputs(exit_insert_mode, 0, __cputchar);
   1161 
   1162 	putattr_out(psp);
   1163 
   1164 	return error;
   1165 }
   1166 
   1167 /*
   1168  * makech --
   1169  *	Make a change on the screen.
   1170  */
   1171 static int
   1172 makech(int wy)
   1173 {
   1174 	WINDOW	*win;
   1175 	static __LDATA blank;
   1176 	__LDATA *nsp, *csp, *cp, *fsp;
   1177 	__LINE *wlp;
   1178 	int	nlsp;	/* offset to first space at eol. */
   1179 	int	lch, wx, owx, chw;
   1180 	const char	*ce;
   1181 	attr_t	battr;		/* background attribute bits */
   1182 	attr_t	attr_mask;	/* attributes mask */
   1183 
   1184 #ifdef __GNUC__
   1185 	nlsp = 0;	/* XXX gcc -Wuninitialized */
   1186 #endif
   1187 	if (_cursesi_screen->curwin)
   1188 		win = curscr;
   1189 	else
   1190 		win = __virtscr;
   1191 
   1192 	blank.ch = (wchar_t) btowc((int) ' ');
   1193 	blank.attr = win->battr;
   1194 	blank.cflags = CA_BACKGROUND;
   1195 #ifdef HAVE_WCHAR
   1196 	if (_cursesi_copy_nsp(win->bnsp, &blank) == ERR)
   1197 		return ERR;
   1198 	blank.wcols = win->wcols;
   1199 	attr_mask = WA_ATTRIBUTES;
   1200 #else
   1201 	attr_mask = A_ATTRIBUTES;
   1202 #endif /* HAVE_WCHAR */
   1203 	battr = win->battr & attr_mask;
   1204 
   1205 #ifdef DEBUG
   1206 #if HAVE_WCHAR
   1207 	{
   1208 		int x;
   1209 		__LDATA *lp, *vlp;
   1210 
   1211 		__CTRACE(__CTRACE_REFRESH,
   1212 		    "[makech-before]wy=%d,curscr(%p)-__virtscr(%p)\n",
   1213 		    wy, curscr, __virtscr);
   1214 		for (x = 0; x < curscr->maxx; x++) {
   1215 			lp = &curscr->alines[wy]->line[x];
   1216 			vlp = &__virtscr->alines[wy]->line[x];
   1217 			__CTRACE(__CTRACE_REFRESH,
   1218 			    "[%d,%d](%x,%x,%d,%x,%x,%d,%p)-"
   1219 			    "(%x,%x,%d,%x,%x,%d,%p)\n",
   1220 			    wy, x, lp->ch, lp->attr, lp->wcols,
   1221 			    win->bch, win->battr, win->wcols, lp->nsp,
   1222 			    vlp->ch, vlp->attr, vlp->wcols,
   1223 			    win->bch, win->battr, win->wcols, vlp->nsp);
   1224 		}
   1225 	}
   1226 #endif /* HAVE_WCHAR */
   1227 #endif /* DEBUG */
   1228 
   1229 	/* Is the cursor still on the end of the last line? */
   1230 	if (wy > 0 && curscr->alines[wy - 1]->flags & __ISPASTEOL) {
   1231 		/* XXX this looks wrong - bad things will happen if ly
   1232 		   is at maxy */
   1233 		domvcur(win, _cursesi_screen->ly, _cursesi_screen->lx,
   1234 			_cursesi_screen->ly + 1, 0);
   1235 		_cursesi_screen->ly++;
   1236 		_cursesi_screen->lx = 0;
   1237 	}
   1238 	wlp = win->alines[wy];
   1239 	wx = *win->alines[wy]->firstchp;
   1240 	if (wx < 0)
   1241 		wx = 0;
   1242 	else
   1243 		if (wx >= win->maxx)
   1244 			return (OK);
   1245 	lch = *win->alines[wy]->lastchp;
   1246 	if (lch < 0)
   1247 		return OK;
   1248 	else
   1249 		if (lch >= (int) win->maxx)
   1250 			lch = win->maxx - 1;
   1251 
   1252 	while (win->alines[wy]->line[wx].cflags & CA_CONTINUATION) {
   1253 		wx--;
   1254 		if (wx <= 0) {
   1255 			wx = 0;
   1256 			break;
   1257 		}
   1258 	}
   1259 
   1260 	if (_cursesi_screen->curwin) {
   1261 		csp = &blank;
   1262 		__CTRACE(__CTRACE_REFRESH, "makech: csp is blank\n");
   1263 	} else {
   1264 		csp = &curscr->alines[wy]->line[wx];
   1265 		__CTRACE(__CTRACE_REFRESH,
   1266 		    "makech: csp is on virtscr:(%d,%d)\n", wy, wx);
   1267 	}
   1268 
   1269 	nsp = fsp = &win->alines[wy]->line[wx];
   1270 
   1271 #ifdef DEBUG
   1272 	if (_cursesi_screen->curwin)
   1273 		__CTRACE(__CTRACE_REFRESH,
   1274 		    "makech: nsp is at curscr:(%d,%d)\n", wy, wx);
   1275 	else
   1276 		__CTRACE(__CTRACE_REFRESH,
   1277 		    "makech: nsp is at __virtscr:(%d,%d)\n", wy, wx);
   1278 #endif /* DEBUG */
   1279 
   1280 	/*
   1281 	 * Work out if we can use a clear to end of line.  If we are
   1282 	 * using color then we can only erase the line if the terminal
   1283 	 * can erase to the background color.
   1284 	 */
   1285 	if (clr_eol && !_cursesi_screen->curwin && (!(__using_color)
   1286 	    || (__using_color && back_color_erase))) {
   1287 
   1288 		nlsp = win->maxx - 1;
   1289 		cp = &win->alines[wy]->line[win->maxx - 1];
   1290 #ifdef HAVE_WCHAR
   1291 		while (((_cursesi_celleq(cp, &blank) == 1) &&
   1292 		    (cp->attr & attr_mask) == battr)) {
   1293 			nlsp -= cp->wcols;
   1294 			cp -= cp->wcols;
   1295 
   1296 			if (nlsp <= 0)
   1297 				break;
   1298 		}
   1299 #else
   1300 		while (cp->ch == blank.ch &&
   1301 		    ((cp->attr & attr_mask) == battr)) {
   1302 			nlsp--;
   1303 			cp--;
   1304 
   1305 			if (nlsp <= 0)
   1306 				break;
   1307 		}
   1308 #endif /* HAVE_WCHAR */
   1309 
   1310 
   1311 		if (nlsp < 0)
   1312 			nlsp = 0;
   1313 	}
   1314 
   1315 	ce = clr_eol;
   1316 
   1317 	while (wx <= lch) {
   1318 		__CTRACE(__CTRACE_REFRESH, "makech: wx=%d,lch=%d, nlsp=%d\n", wx, lch, nlsp);
   1319 #ifdef HAVE_WCHAR
   1320 		__CTRACE(__CTRACE_REFRESH, "makech: farnarkle: flags 0x%x, cflags 0x%x, celleq %d\n",
   1321 			wlp->flags, nsp->cflags, _cursesi_celleq(nsp, csp));
   1322 		__CTRACE(__CTRACE_REFRESH, "makech: nsp=(%x,%x,%d,%x,%x,%d,%p)\n",
   1323 			nsp->ch, nsp->attr, nsp->wcols, win->bch, win->battr,
   1324 			win->wcols, nsp->nsp);
   1325 		__CTRACE(__CTRACE_REFRESH, "makech: csp=(%x,%x,%d,%x,%x,%d,%p)\n",
   1326 			csp->ch, csp->attr, csp->wcols, win->bch, win->battr,
   1327 			win->wcols, csp->nsp);
   1328 #endif
   1329 
   1330 		/*
   1331 		 * If the update is not being forced then skip over
   1332 		 * all the unchanged characters.
   1333 		 */
   1334 		if (!(wlp->flags & __ISFORCED) && _cursesi_celleq(nsp, csp))
   1335 		{
   1336 			if (wx <= lch) {
   1337 				while (wx <= lch && _cursesi_celleq(nsp, csp)) {
   1338 #ifdef HAVE_WCHAR
   1339 					wx += nsp->wcols;
   1340 					if (!_cursesi_screen->curwin)
   1341 						csp += nsp->wcols;
   1342 					nsp += nsp->wcols;
   1343 #else
   1344 					wx++;
   1345 					nsp++;
   1346 					if (!_cursesi_screen->curwin)
   1347 						++csp;
   1348 #endif
   1349 				}
   1350 				continue;
   1351 			}
   1352 			break;
   1353 		}
   1354 
   1355 		domvcur(win, _cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
   1356 
   1357 		__CTRACE(__CTRACE_REFRESH, "makech: 1: wx = %d, ly= %d, "
   1358 		    "lx = %d, newy = %d, newx = %d, lch = %d, nlsp = %d\n",
   1359 		    wx, _cursesi_screen->ly, _cursesi_screen->lx, wy, wx, lch,
   1360 		    nlsp);
   1361 
   1362 		_cursesi_screen->ly = wy;
   1363 		_cursesi_screen->lx = wx;
   1364 		owx = wx;
   1365 
   1366 		if (wx <= lch &&
   1367 		    ((wlp->flags & __ISFORCED) || !_cursesi_celleq(nsp, csp)))
   1368 		{
   1369 			/*
   1370 			 * Consider clearing the line, if:
   1371 			 *  - we have a clear to eol capability
   1372 			 *  - current x pos is past last non blank char
   1373 			 *  - the win char is blank
   1374 			 *  - either we are initing colour or the attributes
   1375 			 *    match.
   1376 			 *  - Or the character is marked background
   1377 			 */
   1378 			if ((clr_eol != NULL) && (wx >= nlsp)  &&
   1379 			    (nsp->ch == blank.ch) && (nsp->attr == blank.attr))
   1380 			{
   1381 				/*
   1382 				 * work out how to clear the line.  If:
   1383 				 *  - clear len is greater than clear_to_eol len
   1384 				 *  - background char == ' '
   1385 				 *  - we are not at EOL
   1386 				 *  - using color and term can erase to
   1387 				 *    background color
   1388 				 *  - if we are at the bottom of the window
   1389 				 *    (to prevent a scroll)
   1390 				 * then emit the ce string.
   1391 				 */
   1392 				if (((wy == win->maxy - 1) ||
   1393 				    (((win->maxx - 1) - wx) > strlen(ce))) &&
   1394 				     ((__using_color && back_color_erase) ||
   1395 				      (! __using_color))) {
   1396 					if (wlp->line[wx].attr & win->screen->nca) {
   1397 						__unsetattr(0);
   1398 					} else if (__using_color ) {
   1399 						__set_color(curscr,
   1400 						    blank.attr);
   1401 					}
   1402 					tputs(ce, 0, __cputchar);
   1403 					_cursesi_screen->lx = wx + win->begx;
   1404 					csp = &curscr->alines[wy]->line[wx + win->begx];
   1405 					wx = wx + win->begx;
   1406 					while (wx++ <= (curscr->maxx - 1)) {
   1407 						csp->attr = blank.attr;
   1408 						csp->ch = blank.ch;
   1409 						csp->cflags = CA_BACKGROUND;
   1410 #ifdef HAVE_WCHAR
   1411 						if (_cursesi_copy_nsp(blank.nsp, csp) == ERR)
   1412 							return ERR;
   1413 						csp->wcols = blank.wcols;
   1414 						csp += blank.wcols;
   1415 #else
   1416 						csp++;
   1417 #endif /* HAVE_WCHAR */
   1418 						assert(csp != &blank);
   1419 					}
   1420 
   1421 					/* We cleared the line, update the
   1422 					 * the hash
   1423 					 */
   1424 					win->alines[wy]->hash = __hash_line(
   1425 					   win->alines[wy]->line, win->maxx);
   1426 
   1427 					return OK;
   1428 				}
   1429 			}
   1430 
   1431 #ifdef HAVE_WCHAR
   1432 			chw = nsp->wcols;
   1433 			if (chw < 0)
   1434 				chw = 0; /* match putch() */
   1435 #else
   1436 			chw = 1;
   1437 #endif /* HAVE_WCHAR */
   1438 			owx = wx;
   1439 			if (wx + chw >= (win->maxx) &&
   1440 			    wy >= (win->maxy - 1) && !_cursesi_screen->curwin)
   1441 			{
   1442 				if (win->flags & __ENDLINE)
   1443 					__unsetattr(1);
   1444 				if (!(win->flags & __SCROLLWIN)) {
   1445 					int e;
   1446 
   1447 					if (win->flags & __SCROLLOK)
   1448 						e = putch(nsp, csp, wy, wx);
   1449 					else {
   1450 						e = putchbr(nsp, csp,
   1451 						    nsp == fsp ? NULL : nsp - 1,
   1452 						    wy, wx);
   1453 					}
   1454 					if (e == ERR)
   1455 						return ERR;
   1456 				}
   1457 				if (wx + chw < curscr->maxx) {
   1458 					domvcur(win,
   1459 					    _cursesi_screen->ly, wx,
   1460 					    (int)(win->maxy - 1),
   1461 					    (int)(win->maxx - 1));
   1462 				}
   1463 				_cursesi_screen->ly = win->maxy - 1;
   1464 				_cursesi_screen->lx = win->maxx - 1;
   1465 				return OK;
   1466 			}
   1467 			if (wx + chw < win->maxx || wy < win->maxy - 1 ||
   1468 			    !(win->flags & __SCROLLWIN))
   1469 			{
   1470 				if (putch(nsp, csp, wy, wx) == ERR)
   1471 					return ERR;
   1472 			} else {
   1473 				putattr(nsp);
   1474 				putattr_out(nsp);
   1475 			}
   1476 			wx += chw;
   1477 			nsp += chw;
   1478 			if (!_cursesi_screen->curwin)
   1479 				csp += chw;
   1480 
   1481 			__CTRACE(__CTRACE_REFRESH,
   1482 			    "makech: 2: wx = %d, lx = %d\n",
   1483 			    wx, _cursesi_screen->lx);
   1484 		}
   1485 		if (_cursesi_screen->lx == wx)	/* If no change. */
   1486 			break;
   1487 
   1488 		/*
   1489 		 * We need to work out if the cursor has been put in the
   1490 		 * middle of a wide character so check if curx is between
   1491 		 * where we were and where we are and we are on the right
   1492 		 * line.  If so, move the cursor now.
   1493 		 */
   1494 		if ((wy == win->cury) && (wx > win->curx) &&
   1495 		    (owx < win->curx)) {
   1496 			_cursesi_screen->lx = win->curx;
   1497 			domvcur(win, _cursesi_screen->ly, wx,
   1498 			    _cursesi_screen->ly, _cursesi_screen->lx);
   1499 		} else
   1500 			_cursesi_screen->lx = wx;
   1501 
   1502 		if (_cursesi_screen->lx >= COLS && auto_right_margin)
   1503 			_cursesi_screen->lx = COLS - 1;
   1504 		else
   1505 			if (wx >= win->maxx) {
   1506 				domvcur(win,
   1507 					_cursesi_screen->ly,
   1508 					_cursesi_screen->lx,
   1509 					_cursesi_screen->ly,
   1510 					(int)(win->maxx - 1));
   1511 				_cursesi_screen->lx = win->maxx - 1;
   1512 			}
   1513 		__CTRACE(__CTRACE_REFRESH, "makech: 3: wx = %d, lx = %d\n",
   1514 		    wx, _cursesi_screen->lx);
   1515 	}
   1516 #ifdef DEBUG
   1517 #if HAVE_WCHAR
   1518 	{
   1519 		int x;
   1520 		__LDATA *lp, *vlp;
   1521 
   1522 		__CTRACE(__CTRACE_REFRESH,
   1523 		    "makech-after: curscr(%p)-__virtscr(%p)\n",
   1524 		    curscr, __virtscr );
   1525 		for (x = 0; x < curscr->maxx; x++) {
   1526 			lp = &curscr->alines[wy]->line[x];
   1527 			vlp = &__virtscr->alines[wy]->line[x];
   1528 			__CTRACE(__CTRACE_REFRESH,
   1529 			    "[%d,%d](%x,%x,%d,%x,%x,%d,%p)-"
   1530 			    "(%x,%x,%d,%x,%x,%d,%p)\n",
   1531 			    wy, x, lp->ch, lp->attr, lp->wcols,
   1532 			    win->bch, win->battr, win->wcols, lp->nsp,
   1533 			    vlp->ch, vlp->attr, vlp->wcols,
   1534 			    win->bch, win->battr, win->wcols, vlp->nsp);
   1535 		}
   1536 	}
   1537 #endif /* HAVE_WCHAR */
   1538 #endif /* DEBUG */
   1539 
   1540 	return OK;
   1541 }
   1542 
   1543 /*
   1544  * domvcur --
   1545  *	Do a mvcur, leaving attributes if necessary.
   1546  */
   1547 static void
   1548 domvcur(WINDOW *win, int oy, int ox, int ny, int nx)
   1549 {
   1550 
   1551 	__CTRACE(__CTRACE_REFRESH, "domvcur: (%d,%d)=>(%d,%d) win %p\n",
   1552 	    oy, ox, ny, nx, win );
   1553 
   1554 	/* Don't move the cursor unless we need to. */
   1555 	if (oy == ny && ox == nx) {
   1556 		/* Check EOL. */
   1557 		if (!(win->alines[oy]->flags & __ISPASTEOL))
   1558 			return;
   1559 	}
   1560 
   1561 	__unsetattr(1);
   1562 
   1563 	/* Clear EOL flags. */
   1564 	win->alines[oy]->flags &= ~__ISPASTEOL;
   1565 	win->alines[ny]->flags &= ~__ISPASTEOL;
   1566 
   1567 	__mvcur(oy, ox, ny, nx, 1);
   1568 }
   1569 
   1570 /*
   1571  * Quickch() attempts to detect a pattern in the change of the window
   1572  * in order to optimize the change, e.g., scroll n lines as opposed to
   1573  * repainting the screen line by line.
   1574  */
   1575 
   1576 static __LDATA buf[128];
   1577 static  unsigned int last_hash;
   1578 static  size_t last_hash_len;
   1579 #define BLANKSIZE (sizeof(buf) / sizeof(buf[0]))
   1580 
   1581 static void
   1582 quickch(void)
   1583 {
   1584 #define THRESH		(int) __virtscr->maxy / 4
   1585 
   1586 	__LINE *clp, *tmp1, *tmp2;
   1587 	int	bsize, curs, curw, starts, startw, i, j;
   1588 	int	n, target, cur_period, bot, top, sc_region;
   1589 	unsigned int	blank_hash;
   1590 	attr_t	bcolor;
   1591 
   1592 #ifdef __GNUC__
   1593 	curs = curw = starts = startw = 0;	/* XXX gcc -Wuninitialized */
   1594 #endif
   1595 	/*
   1596 	 * Find how many lines from the top of the screen are unchanged.
   1597 	 */
   1598 	for (top = 0; top < __virtscr->maxy; top++) {
   1599 		if (__virtscr->alines[top]->flags & __ISDIRTY &&
   1600 		    (__virtscr->alines[top]->hash != curscr->alines[top]->hash ||
   1601 		     !lineeq(__virtscr->alines[top]->line,
   1602 			     curscr->alines[top]->line,
   1603 			     (size_t) __virtscr->maxx))) {
   1604 			break;
   1605 		} else
   1606 			__virtscr->alines[top]->flags &= ~__ISDIRTY;
   1607 	}
   1608 	/*
   1609 	 * Find how many lines from bottom of screen are unchanged.
   1610 	 */
   1611 	for (bot = __virtscr->maxy - 1; bot >= 0; bot--) {
   1612 		if (__virtscr->alines[bot]->flags & __ISDIRTY &&
   1613 		    (__virtscr->alines[bot]->hash != curscr->alines[bot]->hash ||
   1614 		     !lineeq(__virtscr->alines[bot]->line,
   1615 			     curscr->alines[bot]->line,
   1616 			     (size_t) __virtscr->maxx))) {
   1617 			break;
   1618 		} else
   1619 			__virtscr->alines[bot]->flags &= ~__ISDIRTY;
   1620 	}
   1621 
   1622 	/*
   1623 	 * Work round an xterm bug where inserting lines causes all the
   1624 	 * inserted lines to be covered with the background colour we
   1625 	 * set on the first line (even if we unset it for subsequent
   1626 	 * lines).
   1627 	 */
   1628 	bcolor = __virtscr->alines[min(top,
   1629 	    __virtscr->maxy - 1)]->line[0].attr & __COLOR;
   1630 	for (i = top + 1, j = 0; i < bot; i++) {
   1631 		if ((__virtscr->alines[i]->line[0].attr & __COLOR) != bcolor) {
   1632 			bcolor = __virtscr->alines[i]->line[__virtscr->maxx].
   1633 			    attr & __COLOR;
   1634 			j = i - top;
   1635 		} else
   1636 			break;
   1637 	}
   1638 	top += j;
   1639 
   1640 #ifdef NO_JERKINESS
   1641 	/*
   1642 	 * If we have a bottom unchanged region return.  Scrolling the
   1643 	 * bottom region up and then back down causes a screen jitter.
   1644 	 * This will increase the number of characters sent to the screen
   1645 	 * but it looks better.
   1646 	 */
   1647 	if (bot < __virtscr->maxy - 1)
   1648 		return;
   1649 #endif				/* NO_JERKINESS */
   1650 
   1651 	/*
   1652 	 * Search for the largest block of text not changed.
   1653 	 * Invariants of the loop:
   1654 	 * - Startw is the index of the beginning of the examined block in
   1655 	 *   __virtscr.
   1656 	 * - Starts is the index of the beginning of the examined block in
   1657 	 *   curscr.
   1658 	 * - Curw is the index of one past the end of the exmined block in
   1659 	 *   __virtscr.
   1660 	 * - Curs is the index of one past the end of the exmined block in
   1661 	 *   curscr.
   1662 	 * - bsize is the current size of the examined block.
   1663 	*/
   1664 
   1665 	for (bsize = bot - top; bsize >= THRESH; bsize--) {
   1666 		for (startw = top; startw <= bot - bsize; startw++)
   1667 			for (starts = top; starts <= bot - bsize; starts++) {
   1668 				for (curw = startw, curs = starts;
   1669 				    curs < starts + bsize; curw++, curs++)
   1670 					if (__virtscr->alines[curw]->hash !=
   1671 					    curscr->alines[curs]->hash)
   1672 						break;
   1673 				if (curs != starts + bsize)
   1674 					continue;
   1675 				for (curw = startw, curs = starts;
   1676 					curs < starts + bsize; curw++, curs++)
   1677 					if (!lineeq(__virtscr->alines[curw]->line,
   1678 						    curscr->alines[curs]->line,
   1679 						    (size_t) __virtscr->maxx)) {
   1680 						break;
   1681 					}
   1682 				if (curs == starts + bsize) {
   1683 					goto done;
   1684 				}
   1685 			}
   1686 	}
   1687 done:
   1688 
   1689 	__CTRACE(__CTRACE_REFRESH, "quickch:bsize=%d, THRESH=%d, starts=%d, "
   1690 	    "startw=%d, curw=%d, curs=%d, top=%d, bot=%d\n",
   1691 	    bsize, THRESH, starts, startw, curw, curs, top, bot);
   1692 
   1693 	/* Did not find anything */
   1694 	if (bsize < THRESH)
   1695 		return;
   1696 
   1697 	/*
   1698 	 * Make sure that there is no overlap between the bottom and top
   1699 	 * regions and the middle scrolled block.
   1700 	 */
   1701 	if (bot < curs)
   1702 		bot = curs - 1;
   1703 	if (top > starts)
   1704 		top = starts;
   1705 
   1706 	n = startw - starts;
   1707 
   1708 #ifdef DEBUG
   1709 	__CTRACE(__CTRACE_REFRESH, "#####################################\n");
   1710 	__CTRACE(__CTRACE_REFRESH, "quickch: n = %d\n", n);
   1711 	for (i = 0; i < curscr->maxy; i++) {
   1712 		__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
   1713 		__CTRACE(__CTRACE_REFRESH, " hash: 0x%x \n", curscr->alines[i]->hash);
   1714 		for (j = 0; j < curscr->maxx; j++)
   1715 			__CTRACE(__CTRACE_REFRESH, "%c",
   1716 			    curscr->alines[i]->line[j].ch);
   1717 		__CTRACE(__CTRACE_REFRESH, "\n");
   1718 		__CTRACE(__CTRACE_REFRESH, " attr:");
   1719 		for (j = 0; j < curscr->maxx; j++)
   1720 			__CTRACE(__CTRACE_REFRESH, " %x",
   1721 			    curscr->alines[i]->line[j].attr);
   1722 		__CTRACE(__CTRACE_REFRESH, "\n");
   1723 		__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
   1724 		__CTRACE(__CTRACE_REFRESH, " hash: 0x%x \n",
   1725 		    __virtscr->alines[i]->hash);
   1726 		__CTRACE(__CTRACE_REFRESH, " 0x%x ",
   1727 		    __virtscr->alines[i]->flags);
   1728 		for (j = 0; j < __virtscr->maxx; j++)
   1729 			__CTRACE(__CTRACE_REFRESH, "%c",
   1730 			    __virtscr->alines[i]->line[j].ch);
   1731 		__CTRACE(__CTRACE_REFRESH, "\n");
   1732 		__CTRACE(__CTRACE_REFRESH, " attr:");
   1733 		for (j = 0; j < __virtscr->maxx; j++)
   1734 			__CTRACE(__CTRACE_REFRESH, " %x",
   1735 			    __virtscr->alines[i]->line[j].attr);
   1736 		__CTRACE(__CTRACE_REFRESH, "\n");
   1737 	}
   1738 #endif
   1739 
   1740 #ifndef HAVE_WCHAR
   1741 	if (buf[0].ch != curscr->bch) {
   1742 		for (i = 0; i < BLANKSIZE; i++) {
   1743 			buf[i].ch = curscr->bch;
   1744 			buf[i].attr = curscr->battr;
   1745 			buf[i].cflags = CA_BACKGROUND;
   1746 		}
   1747 	}
   1748 #else
   1749 	if (buf[0].ch != curscr->bch) {
   1750 		for (i = 0; i < BLANKSIZE; i++) { /* XXXX: BLANKSIZE may not be valid if wcols > 1 */
   1751 			buf[i].ch = curscr->bch;
   1752 			if (_cursesi_copy_nsp(curscr->bnsp, &buf[i]) == ERR)
   1753 				return;
   1754 			buf[i].attr = curscr->battr;
   1755 			buf[i].cflags = CA_BACKGROUND;
   1756 			buf[i].wcols = curscr->wcols;
   1757 		}
   1758 	}
   1759 #endif /* HAVE_WCHAR */
   1760 
   1761 	if (__virtscr->maxx != last_hash_len) {
   1762 		blank_hash = 0;
   1763 		for (i = __virtscr->maxx; i > BLANKSIZE; i -= BLANKSIZE) {
   1764 			blank_hash = __hash_more(buf, sizeof(buf), blank_hash);
   1765 		}
   1766 		blank_hash = __hash_more((char *)(void *)buf,
   1767 		    i * sizeof(buf[0]), blank_hash);
   1768 		/* cache result in static data - screen width doesn't change often */
   1769 		last_hash_len = __virtscr->maxx;
   1770 		last_hash = blank_hash;
   1771 	} else
   1772 		blank_hash = last_hash;
   1773 
   1774 	/*
   1775 	 * Perform the rotation to maintain the consistency of curscr.
   1776 	 * This is hairy since we are doing an *in place* rotation.
   1777 	 * Invariants of the loop:
   1778 	 * - I is the index of the current line.
   1779 	 * - Target is the index of the target of line i.
   1780 	 * - Tmp1 points to current line (i).
   1781 	 * - Tmp2 and points to target line (target);
   1782 	 * - Cur_period is the index of the end of the current period.
   1783 	 *   (see below).
   1784 	 *
   1785 	 * There are 2 major issues here that make this rotation non-trivial:
   1786 	 * 1.  Scrolling in a scrolling region bounded by the top
   1787 	 *     and bottom regions determined (whose size is sc_region).
   1788 	 * 2.  As a result of the use of the mod function, there may be a
   1789 	 *     period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
   1790 	 *     0 to 2, which then causes all odd lines not to be rotated.
   1791 	 *     To remedy this, an index of the end ( = beginning) of the
   1792 	 *     current 'period' is kept, cur_period, and when it is reached,
   1793 	 *     the next period is started from cur_period + 1 which is
   1794 	 *     guaranteed not to have been reached since that would mean that
   1795 	 *     all records would have been reached. (think about it...).
   1796 	 *
   1797 	 * Lines in the rotation can have 3 attributes which are marked on the
   1798 	 * line so that curscr is consistent with the visual screen.
   1799 	 * 1.  Not dirty -- lines inside the scrolled block, top region or
   1800 	 *                  bottom region.
   1801 	 * 2.  Blank lines -- lines in the differential of the scrolling
   1802 	 *		      region adjacent to top and bot regions
   1803 	 *                    depending on scrolling direction.
   1804 	 * 3.  Dirty line -- all other lines are marked dirty.
   1805 	 */
   1806 	sc_region = bot - top + 1;
   1807 	i = top;
   1808 	tmp1 = curscr->alines[top];
   1809 	cur_period = top;
   1810 	for (j = top; j <= bot; j++) {
   1811 		target = (i - top + n + sc_region) % sc_region + top;
   1812 		tmp2 = curscr->alines[target];
   1813 		curscr->alines[target] = tmp1;
   1814 		/* Mark block as clean and blank out scrolled lines. */
   1815 		clp = curscr->alines[target];
   1816 		__CTRACE(__CTRACE_REFRESH,
   1817 		    "quickch: n=%d startw=%d curw=%d i = %d target=%d ",
   1818 		    n, startw, curw, i, target);
   1819 		if ((target >= startw && target < curw) || target < top
   1820 		    || target > bot)
   1821 		{
   1822 			__CTRACE(__CTRACE_REFRESH, " notdirty\n");
   1823 			__virtscr->alines[target]->flags &= ~__ISDIRTY;
   1824 		} else
   1825 			if ((n > 0 && target >= top && target < top + n) ||
   1826 			    (n < 0 && target <= bot && target > bot + n))
   1827 			{
   1828 				if (clp->hash != blank_hash ||
   1829 				    !lineeq(clp->line, clp->line + 1,
   1830 					    (__virtscr->maxx - 1)) ||
   1831 				    !_cursesi_celleq(clp->line, buf))
   1832 				{
   1833 					for (i = __virtscr->maxx;
   1834 					    i > BLANKSIZE;
   1835 					    i -= BLANKSIZE) {
   1836 						(void) memcpy(clp->line + i -
   1837 						    BLANKSIZE, buf, sizeof(buf));
   1838 					}
   1839 					(void)memcpy(clp->line, buf,
   1840 					    i * sizeof(buf[0]));
   1841 					__CTRACE(__CTRACE_REFRESH,
   1842 					    " blanked out: dirty\n");
   1843 					clp->hash = blank_hash;
   1844 					__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
   1845 				} else {
   1846 					__CTRACE(__CTRACE_REFRESH,
   1847 					    " -- blank line already: dirty\n");
   1848 					__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
   1849 				}
   1850 			} else {
   1851 				__CTRACE(__CTRACE_REFRESH, " -- dirty\n");
   1852 				__touchline(__virtscr, target, 0,
   1853 				    (int)__virtscr->maxx - 1);
   1854 			}
   1855 		if (target == cur_period) {
   1856 			i = target + 1;
   1857 			tmp1 = curscr->alines[i];
   1858 			cur_period = i;
   1859 		} else {
   1860 			tmp1 = tmp2;
   1861 			i = target;
   1862 		}
   1863 	}
   1864 #ifdef DEBUG
   1865 	__CTRACE(__CTRACE_REFRESH, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
   1866 	for (i = 0; i < curscr->maxy; i++) {
   1867 		__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
   1868 		for (j = 0; j < curscr->maxx; j++)
   1869 			__CTRACE(__CTRACE_REFRESH, "%c",
   1870 			    curscr->alines[i]->line[j].ch);
   1871 		__CTRACE(__CTRACE_REFRESH, "\n");
   1872 		__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
   1873 		for (j = 0; j < __virtscr->maxx; j++)
   1874 			__CTRACE(__CTRACE_REFRESH, "%c",
   1875 			    __virtscr->alines[i]->line[j].ch);
   1876 		__CTRACE(__CTRACE_REFRESH, "\n");
   1877 	}
   1878 #endif
   1879 	if (n != 0)
   1880 		scrolln(starts, startw, curs, bot, top);
   1881 }
   1882 
   1883 /*
   1884  * scrolln --
   1885  *	Scroll n lines, where n is starts - startw.
   1886  */
   1887 static void /* ARGSUSED */
   1888 scrolln(int starts, int startw, int curs, int bot, int top)
   1889 {
   1890 	int	i, oy, ox, n;
   1891 
   1892 	oy = curscr->cury;
   1893 	ox = curscr->curx;
   1894 	n = starts - startw;
   1895 
   1896 	/*
   1897 	 * XXX
   1898 	 * The initial tests that set __noqch don't let us reach here unless
   1899 	 * we have either cs + ho + SF/sf/SR/sr, or AL + DL.  SF/sf and SR/sr
   1900 	 * scrolling can only shift the entire scrolling region, not just a
   1901 	 * part of it, which means that the quickch() routine is going to be
   1902 	 * sadly disappointed in us if we don't have cs as well.
   1903 	 *
   1904 	 * If cs, ho and SF/sf are set, can use the scrolling region.  Because
   1905 	 * the cursor position after cs is undefined, we need ho which gives us
   1906 	 * the ability to move to somewhere without knowledge of the current
   1907 	 * location of the cursor.  Still call __mvcur() anyway, to update its
   1908 	 * idea of where the cursor is.
   1909 	 *
   1910 	 * When the scrolling region has been set, the cursor has to be at the
   1911 	 * last line of the region to make the scroll happen.
   1912 	 *
   1913 	 * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
   1914 	 * or AL/DL, and, some terminals have AL/DL, sf/sr, and cs, but not
   1915 	 * SF/SR.  So, if we're scrolling almost all of the screen, try and use
   1916 	 * AL/DL, otherwise use the scrolling region.  The "almost all" is a
   1917 	 * shameless hack for vi.
   1918 	 */
   1919 	if (n > 0) {
   1920 		if (change_scroll_region != NULL && cursor_home != NULL &&
   1921 		    (parm_index != NULL ||
   1922 		    ((parm_insert_line == NULL || parm_delete_line == NULL ||
   1923 		    top > 3 || bot + 3 < __virtscr->maxy) &&
   1924 		    scroll_forward != NULL)))
   1925 		{
   1926 			tputs(tiparm(change_scroll_region, top, bot),
   1927 			    0, __cputchar);
   1928 			__mvcur(oy, ox, 0, 0, 1);
   1929 			tputs(cursor_home, 0, __cputchar);
   1930 			__mvcur(0, 0, bot, 0, 1);
   1931 			if (parm_index != NULL)
   1932 				tputs(tiparm(parm_index, n),
   1933 				    0, __cputchar);
   1934 			else
   1935 				for (i = 0; i < n; i++)
   1936 					tputs(scroll_forward, 0, __cputchar);
   1937 			tputs(tiparm(change_scroll_region,
   1938 			    0, (int)__virtscr->maxy - 1), 0, __cputchar);
   1939 			__mvcur(bot, 0, 0, 0, 1);
   1940 			tputs(cursor_home, 0, __cputchar);
   1941 			__mvcur(0, 0, oy, ox, 1);
   1942 			return;
   1943 		}
   1944 
   1945 		/* Scroll up the block. */
   1946 		if (parm_index != NULL && top == 0) {
   1947 			__mvcur(oy, ox, bot, 0, 1);
   1948 			tputs(tiparm(parm_index, n), 0, __cputchar);
   1949 		} else
   1950 			if (parm_delete_line != NULL) {
   1951 				__mvcur(oy, ox, top, 0, 1);
   1952 				tputs(tiparm(parm_delete_line, n),
   1953 				    0, __cputchar);
   1954 			} else
   1955 				if (delete_line != NULL) {
   1956 					__mvcur(oy, ox, top, 0, 1);
   1957 					for (i = 0; i < n; i++)
   1958 						tputs(delete_line, 0,
   1959 						    __cputchar);
   1960 				} else
   1961 					if (scroll_forward != NULL && top == 0) {
   1962 						__mvcur(oy, ox, bot, 0, 1);
   1963 						for (i = 0; i < n; i++)
   1964 							tputs(scroll_forward, 0,
   1965 							    __cputchar);
   1966 					} else
   1967 						abort();
   1968 
   1969 		/* Push down the bottom region. */
   1970 		__mvcur(top, 0, bot - n + 1, 0, 1);
   1971 		if (parm_insert_line != NULL)
   1972 			tputs(tiparm(parm_insert_line, n), 0, __cputchar);
   1973 		else {
   1974 			if (insert_line != NULL) {
   1975 				for (i = 0; i < n; i++)
   1976 					tputs(insert_line, 0, __cputchar);
   1977 			} else
   1978 				abort();
   1979 		}
   1980 		__mvcur(bot - n + 1, 0, oy, ox, 1);
   1981 	} else {
   1982 		/*
   1983 		 * !!!
   1984 		 * n < 0
   1985 		 *
   1986 		 * If cs, ho and SR/sr are set, can use the scrolling region.
   1987 		 * See the above comments for details.
   1988 		 */
   1989 		if (change_scroll_region != NULL && cursor_home != NULL &&
   1990 		    (parm_rindex != NULL ||
   1991 		    ((parm_insert_line == NULL || parm_delete_line == NULL ||
   1992 		    top > 3 ||
   1993 		    bot + 3 < __virtscr->maxy) && scroll_reverse != NULL)))
   1994 		{
   1995 			tputs(tiparm(change_scroll_region, top, bot),
   1996 			    0, __cputchar);
   1997 			__mvcur(oy, ox, 0, 0, 1);
   1998 			tputs(cursor_home, 0, __cputchar);
   1999 			__mvcur(0, 0, top, 0, 1);
   2000 
   2001 			if (parm_rindex != NULL)
   2002 				tputs(tiparm(parm_rindex, -n),
   2003 				    0, __cputchar);
   2004 			else
   2005 				for (i = n; i < 0; i++)
   2006 					tputs(scroll_reverse, 0, __cputchar);
   2007 			tputs(tiparm(change_scroll_region,
   2008 			    0, (int) __virtscr->maxy - 1), 0, __cputchar);
   2009 			__mvcur(top, 0, 0, 0, 1);
   2010 			tputs(cursor_home, 0, __cputchar);
   2011 			__mvcur(0, 0, oy, ox, 1);
   2012 			return;
   2013 		}
   2014 
   2015 		/* Preserve the bottom lines. */
   2016 		__mvcur(oy, ox, bot + n + 1, 0, 1);
   2017 		if (parm_rindex != NULL && bot == __virtscr->maxy)
   2018 			tputs(tiparm(parm_rindex, -n), 0, __cputchar);
   2019 		else {
   2020 			if (parm_delete_line != NULL)
   2021 				tputs(tiparm(parm_delete_line, -n),
   2022 				    0, __cputchar);
   2023 			else {
   2024 				if (delete_line != NULL)
   2025 					for (i = n; i < 0; i++)
   2026 						tputs(delete_line,
   2027 						    0, __cputchar);
   2028 				else {
   2029 					if (scroll_reverse != NULL &&
   2030 					    bot == __virtscr->maxy)
   2031 						for (i = n; i < 0; i++)
   2032 							tputs(scroll_reverse, 0,
   2033 							    __cputchar);
   2034 					else
   2035 						abort();
   2036 				}
   2037 			}
   2038 		}
   2039 		/* Scroll the block down. */
   2040 		__mvcur(bot + n + 1, 0, top, 0, 1);
   2041 		if (parm_insert_line != NULL)
   2042 			tputs(tiparm(parm_insert_line, -n), 0, __cputchar);
   2043 		else
   2044 			if (insert_line != NULL)
   2045 				for (i = n; i < 0; i++)
   2046 					tputs(insert_line, 0, __cputchar);
   2047 			else
   2048 				abort();
   2049 		__mvcur(top, 0, oy, ox, 1);
   2050 	}
   2051 }
   2052 
   2053 /*
   2054  * __unsetattr --
   2055  *	Unset attributes on curscr.  Leave standout, attribute and colour
   2056  *	modes if necessary (!ms).  Always leave altcharset (xterm at least
   2057  *	ignores a cursor move if we don't).
   2058  */
   2059 void /* ARGSUSED */
   2060 __unsetattr(int checkms)
   2061 {
   2062 	int	isms;
   2063 
   2064 	if (checkms) {
   2065 		if (!move_standout_mode)
   2066 			isms = 1;
   2067 		else
   2068 			isms = 0;
   2069 	} else
   2070 		isms = 1;
   2071 	__CTRACE(__CTRACE_REFRESH,
   2072 	    "__unsetattr: checkms = %d, ms = %s, wattr = %08x\n",
   2073 	    checkms, move_standout_mode ? "TRUE" : "FALSE", curscr->wattr);
   2074 
   2075 	/*
   2076 	 * Don't leave the screen in standout mode (check against ms).  Check
   2077 	 * to see if we also turn off underscore, attributes and colour.
   2078 	 */
   2079 	if (curscr->wattr & __STANDOUT && isms) {
   2080 		tputs(exit_standout_mode, 0, __cputchar);
   2081 		curscr->wattr &= __mask_se;
   2082 	}
   2083 	/*
   2084 	 * Don't leave the screen in underscore mode (check against ms).
   2085 	 * Check to see if we also turn off attributes.  Assume that we
   2086 	 * also turn off colour.
   2087 	 */
   2088 	if (curscr->wattr & __UNDERSCORE && isms) {
   2089 		tputs(exit_underline_mode, 0, __cputchar);
   2090 		curscr->wattr &= __mask_ue;
   2091 	}
   2092 	/*
   2093 	 * Don't leave the screen with attributes set (check against ms).
   2094 	 * Assume that also turn off colour.
   2095 	 */
   2096 	if (curscr->wattr & __TERMATTR && isms) {
   2097 		tputs(exit_attribute_mode, 0, __cputchar);
   2098 		_cursesi_screen->curpair = -1;  /* tputs just reset it */
   2099 		curscr->wattr &= __mask_me;
   2100 	}
   2101 	/* Don't leave the screen with altcharset set (don't check ms). */
   2102 	if (curscr->wattr & __ALTCHARSET) {
   2103 		tputs(exit_alt_charset_mode, 0, __cputchar);
   2104 		curscr->wattr &= ~__ALTCHARSET;
   2105 	}
   2106 
   2107 	/* Don't leave the screen with colour set (check against ms). */
   2108 	if (__using_color && isms)
   2109 		__unset_color(curscr);
   2110 
   2111 }
   2112 
   2113 /* compare two line segments */
   2114 static int
   2115 lineeq(__LDATA *xl, __LDATA *yl, size_t len)
   2116 {
   2117 	int i = 0;
   2118 	__LDATA *xp = xl, *yp = yl;
   2119 
   2120 	for (i = 0; i < len; i++, xp++, yp++) {
   2121 		if (!_cursesi_celleq(xp, yp))
   2122 			return 0;
   2123 	}
   2124 	return 1;
   2125 }
   2126 
   2127 #ifdef HAVE_WCHAR
   2128 /*
   2129  * Output the non-spacing characters associated with the given character
   2130  * cell to the screen.
   2131  */
   2132 
   2133 void
   2134 __cursesi_putnsp(nschar_t *nsp, const int wy, const int wx)
   2135 {
   2136 	nschar_t *p;
   2137 
   2138 	/* this shuts up gcc warnings about wx and wy not being used */
   2139 	if (wx > wy) {
   2140 	}
   2141 
   2142 	p = nsp;
   2143 	while (p != NULL) {
   2144 		__cputwchar((int)p->ch);
   2145 		__CTRACE(__CTRACE_REFRESH,
   2146 		    "_cursesi_putnsp: (%d,%d) non-spacing putwchar(0x%x)\n",
   2147 		    wy, wx - 1, p->ch);
   2148 		p = p->next;
   2149 	}
   2150 }
   2151 
   2152 #endif /* HAVE_WCHAR */
   2153