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