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