Home | History | Annotate | Line # | Download | only in libcurses
refresh.c revision 1.50
      1 /*	$NetBSD: refresh.c,v 1.50 2002/12/23 12:17:55 jdc 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)refresh.c	8.7 (Berkeley) 8/13/94";
     40 #else
     41 __RCSID("$NetBSD: refresh.c,v 1.50 2002/12/23 12:17:55 jdc Exp $");
     42 #endif
     43 #endif				/* not lint */
     44 
     45 #include <stdlib.h>
     46 #include <string.h>
     47 
     48 #include "curses.h"
     49 #include "curses_private.h"
     50 
     51 static void	domvcur __P((int, int, int, int));
     52 static int	makech __P((int));
     53 static void	quickch __P((void));
     54 static void	scrolln __P((int, int, int, int, int));
     55 
     56 #ifndef _CURSES_USE_MACROS
     57 
     58 /*
     59  * refresh --
     60  *	Make the current screen look like "stdscr" over the area covered by
     61  *	stdscr.
     62  */
     63 int
     64 refresh(void)
     65 {
     66 	return wrefresh(stdscr);
     67 }
     68 
     69 #endif
     70 
     71 /*
     72  * wnoutrefresh --
     73  *	Add the contents of "win" to the virtual window.
     74  */
     75 int
     76 wnoutrefresh(WINDOW *win)
     77 {
     78 #ifdef DEBUG
     79 	__CTRACE("wnoutrefresh: win %0.2o\n", win);
     80 #endif
     81 
     82 	return _cursesi_wnoutrefresh(_cursesi_screen, win, 0, 0, win->begy,
     83 	    win->begx, win->maxy, win->maxx);
     84 }
     85 
     86 /*
     87  * pnoutrefresh --
     88  *	Add the contents of "pad" to the virtual window.
     89  */
     90 int
     91 pnoutrefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
     92     int smaxy, int smaxx)
     93 {
     94 	int pmaxy, pmaxx;
     95 
     96 #ifdef DEBUG
     97 	__CTRACE("pnoutrefresh: pad %0.2o, flags 0x%08x\n", pad, pad->flags);
     98 	__CTRACE("pnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n", pbegy, pbegx,
     99 	    sbegy, sbegx, smaxy, smaxx);
    100 #endif
    101 
    102 	/* SUS says if these are negative, they should be treated as zero */
    103 	if (pbegy < 0)
    104 		pbegy = 0;
    105 	if (pbegx < 0)
    106 		pbegx = 0;
    107 	if (sbegy < 0)
    108 		sbegy = 0;
    109 	if (sbegx < 0)
    110 		sbegx = 0;
    111 
    112 	/* Calculate rectangle on pad - used bu _cursesi_wnoutrefresh */
    113 	pmaxy = pbegy + smaxy - sbegy + 1;
    114 	pmaxx = pbegx + smaxx - sbegx + 1;
    115 
    116 	/* Check rectangle fits in pad */
    117 	if (pmaxy > pad->maxy - pad->begy)
    118 		pmaxy = pad->maxy - pad->begy;
    119 	if (pmaxx > pad->maxx - pad->begx)
    120 		pmaxx = pad->maxx - pad->begx;
    121 
    122 	if (smaxy - sbegy < 0 || smaxx - sbegx < 0 )
    123 		return ERR;
    124 
    125 	return _cursesi_wnoutrefresh(_cursesi_screen, pad,
    126 	    pad->begy + pbegy, pad->begx + pbegx, pad->begy + sbegy,
    127 	    pad->begx + sbegx, pmaxy, pmaxx);
    128 }
    129 
    130 /*
    131  * _cursesi_wnoutrefresh --
    132  *      Does the grunt work for wnoutrefresh to the given screen.
    133  *	Copies the part of the window given by the rectangle
    134  *	(begy, begx) to (maxy, maxx) at screen position (wbegy, wbegx).
    135  */
    136 int
    137 _cursesi_wnoutrefresh(SCREEN *screen, WINDOW *win, int begy, int begx,
    138     int wbegy, int wbegx, int maxy, int maxx)
    139 {
    140 
    141 	short	wy, wx, y_off, x_off;
    142 	__LINE	*wlp, *vlp;
    143 
    144 #ifdef DEBUG
    145 	__CTRACE("wnoutrefresh: win %0.2o, flags 0x%08x\n", win, win->flags);
    146 	__CTRACE("wnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n", begy, begx,
    147 		wbegy, wbegx, maxy, maxx);
    148 #endif
    149 
    150 	if (screen->curwin)
    151 		return(OK);
    152 
    153 	/* Check that cursor position on "win" is valid for "__virtscr" */
    154 	if (win->cury + wbegy - begy < screen->__virtscr->maxy &&
    155 	    win->cury + wbegy - begy >= 0 && win->cury < maxy - begy)
    156 		screen->__virtscr->cury = win->cury + wbegy - begy;
    157 	if (win->curx + wbegx - begx < screen->__virtscr->maxx &&
    158 	    win->curx + wbegx - begx >= 0 && win->curx < maxx - begx)
    159 		screen->__virtscr->curx = win->curx + wbegx - begx;
    160 
    161 	/* Copy the window flags from "win" to "__virtscr" */
    162 	if (win->flags & __CLEAROK) {
    163 		if (win->flags & __FULLWIN) {
    164 			screen->__virtscr->flags |= __CLEAROK;
    165 			werase(screen->__virtscr);
    166 		}
    167 		win->flags &= ~__CLEAROK;
    168 	}
    169 	screen->__virtscr->flags &= ~__LEAVEOK;
    170 	screen->__virtscr->flags |= win->flags;
    171 
    172 	for (wy = begy, y_off = wbegy; wy < maxy &&
    173 	    y_off < screen->__virtscr->maxy; wy++, y_off++) {
    174  		wlp = win->lines[wy];
    175 #ifdef DEBUG
    176 		__CTRACE("wnoutrefresh: wy %d\tf: %d\tl:%d\tflags %x\n", wy,
    177 		    *wlp->firstchp, *wlp->lastchp, wlp->flags);
    178 #endif
    179 		if ((wlp->flags & __ISDIRTY) == 0)
    180 			continue;
    181 		vlp = screen->__virtscr->lines[y_off];
    182 
    183 		if (*wlp->firstchp < maxx + win->ch_off &&
    184 		    *wlp->lastchp >= win->ch_off) {
    185 			/* Copy line from "win" to "__virtscr". */
    186 			for (wx = begx + *wlp->firstchp - win->ch_off,
    187 			    x_off = wbegx + *wlp->firstchp - win->ch_off;
    188 			    wx <= *wlp->lastchp && wx < maxx &&
    189 			    x_off < screen->__virtscr->maxx; wx++, x_off++) {
    190 				vlp->line[x_off].attr = wlp->line[wx].attr;
    191 				if (wlp->line[wx].attr & __COLOR)
    192 					vlp->line[x_off].attr |=
    193 					    wlp->line[wx].battr & ~__COLOR;
    194 				else
    195 					vlp->line[x_off].attr |=
    196 					    wlp->line[wx].battr;
    197 				if (wlp->line[wx].ch == ' ' &&
    198 				    wlp->line[wx].bch != ' ')
    199 					vlp->line[x_off].ch
    200 					    = wlp->line[wx].bch;
    201 				else
    202 					vlp->line[x_off].ch
    203 					    = wlp->line[wx].ch;
    204 			}
    205 
    206 			/* Set flags on "__virtscr" and unset on "win". */
    207 			if (wlp->flags & __ISPASTEOL)
    208 				vlp->flags |= __ISPASTEOL;
    209 			else
    210 				vlp->flags &= ~__ISPASTEOL;
    211 			if (wlp->flags & __ISDIRTY)
    212 				vlp->flags |= __ISDIRTY;
    213 
    214 #ifdef DEBUG
    215 			__CTRACE("win: firstch = %d, lastch = %d\n",
    216 			    *wlp->firstchp, *wlp->lastchp);
    217 #endif
    218 			/* Set change pointers on "__virtscr". */
    219 			if (*vlp->firstchp >
    220 			    *wlp->firstchp + wbegx - win->ch_off)
    221 				*vlp->firstchp =
    222 				    *wlp->firstchp + wbegx - win->ch_off;
    223 			if (*vlp->lastchp <
    224 			    *wlp->lastchp + wbegx - win->ch_off)
    225 				*vlp->lastchp =
    226 				    *wlp->lastchp + wbegx - win->ch_off;
    227 #ifdef DEBUG
    228 			__CTRACE("__virtscr: firstch = %d, lastch = %d\n",
    229 			    *vlp->firstchp, *vlp->lastchp);
    230 #endif
    231 			/*
    232 			 * Unset change pointers only if a window, as a pad
    233 			 * can be displayed again without any of the contents
    234 			 * changing.
    235 			 */
    236 			if (!(win->flags & __ISPAD)) {
    237 				/* Set change pointers on "win". */
    238 				if (*wlp->firstchp >= win->ch_off)
    239 					*wlp->firstchp = maxx + win->ch_off;
    240 				if (*wlp->lastchp < maxx + win->ch_off)
    241 					*wlp->lastchp = win->ch_off;
    242 				if (*wlp->lastchp < *wlp->firstchp) {
    243 #ifdef DEBUG
    244 					__CTRACE("wnoutrefresh: line %d notdirty\n", wy);
    245 #endif
    246 					wlp->flags &= ~__ISDIRTY;
    247 				}
    248 			}
    249 		}
    250 	}
    251 
    252 	return (OK);
    253 }
    254 
    255 /*
    256  * wrefresh --
    257  *	Make the current screen look like "win" over the area covered by
    258  *	win.
    259  */
    260 int
    261 wrefresh(WINDOW *win)
    262 {
    263 	int retval;
    264 
    265 #ifdef DEBUG
    266 	__CTRACE("wrefresh: win %0.2o\n", win);
    267 #endif
    268 
    269 	_cursesi_screen->curwin = (win == _cursesi_screen->curscr);
    270 	if (!_cursesi_screen->curwin)
    271 		retval = _cursesi_wnoutrefresh(_cursesi_screen, win, 0, 0,
    272 		    win->begy, win->begx, win->maxy, win->maxx);
    273 	else
    274 		retval = OK;
    275 	if (retval == OK) {
    276 		retval = doupdate();
    277 		if (!win->flags & __LEAVEOK) {
    278 			win->cury = max(0, curscr->cury - win->begy);
    279 			win->curx = max(0, curscr->curx - win->begx);
    280 		}
    281 	}
    282 	_cursesi_screen->curwin = 0;
    283 	return(retval);
    284 }
    285 
    286  /*
    287  * prefresh --
    288  *	Make the current screen look like "pad" over the area coverd by
    289  *	the specified area of pad.
    290  */
    291 int
    292 prefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
    293     int smaxy, int smaxx)
    294 {
    295 	int retval;
    296 
    297 #ifdef DEBUG
    298 	__CTRACE("prefresh: pad %0.2o, flags 0x%08x\n", pad, pad->flags);
    299 #endif
    300 
    301 	/* Use pnoutrefresh() to avoid duplicating code here */
    302 	retval = pnoutrefresh(pad, pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
    303 	if (retval == OK) {
    304 		retval = doupdate();
    305 		if (!pad->flags & __LEAVEOK) {
    306 			pad->cury = max(0, curscr->cury - pad->begy);
    307 			pad->curx = max(0, curscr->curx - pad->begx);
    308 		}
    309 	}
    310 	return(retval);
    311 }
    312 
    313 /*
    314  * doupdate --
    315  *	Make the current screen look like the virtual window "__virtscr".
    316  */
    317 int
    318 doupdate(void)
    319 {
    320 	WINDOW	*win;
    321 	__LINE	*wlp;
    322 	short	 wy;
    323 	int	 dnum;
    324 
    325 	/* Check if we need to restart ... */
    326 	if (_cursesi_screen->endwin)
    327 		__restartwin();
    328 
    329 	if (_cursesi_screen->curwin)
    330 		win = curscr;
    331 	else
    332 		win = _cursesi_screen->__virtscr;
    333 
    334 	/* Initialize loop parameters. */
    335 	_cursesi_screen->ly = curscr->cury;
    336 	_cursesi_screen->lx = curscr->curx;
    337 	wy = 0;
    338 
    339 	if (!_cursesi_screen->curwin)
    340 		for (wy = 0; wy < win->maxy; wy++) {
    341 			wlp = win->lines[wy];
    342 			if (wlp->flags & __ISDIRTY)
    343 				wlp->hash = __hash((char *)(void *)wlp->line,
    344 				    (size_t) (win->maxx * __LDATASIZE));
    345 		}
    346 
    347 	if ((win->flags & __CLEAROK) || (curscr->flags & __CLEAROK) ||
    348 	    _cursesi_screen->curwin) {
    349 		if (curscr->wattr & __COLOR)
    350 			__unsetattr(0);
    351 		tputs(__tc_cl, 0, __cputchar);
    352 		_cursesi_screen->ly = 0;
    353 		_cursesi_screen->lx = 0;
    354 		if (!_cursesi_screen->curwin) {
    355 			curscr->flags &= ~__CLEAROK;
    356 			curscr->cury = 0;
    357 			curscr->curx = 0;
    358 			werase(curscr);
    359 		}
    360 		__touchwin(win);
    361 		win->flags &= ~__CLEAROK;
    362 	}
    363 	if (!__CA) {
    364 		if (win->curx != 0)
    365 			__cputchar('\n');
    366 		if (!_cursesi_screen->curwin)
    367 			werase(curscr);
    368 	}
    369 #ifdef DEBUG
    370 	__CTRACE("doupdate: (%0.2o): curwin = %d\n", win,
    371 		 _cursesi_screen->curwin);
    372 	__CTRACE("doupdate: \tfirstch\tlastch\n");
    373 #endif
    374 
    375 	if (!_cursesi_screen->curwin) {
    376 		/*
    377 		 * Invoke quickch() only if more than a quarter of the lines
    378 		 * in the window are dirty.
    379 		 */
    380 		for (wy = 0, dnum = 0; wy < win->maxy; wy++)
    381 			if (win->lines[wy]->flags & __ISDIRTY)
    382 				dnum++;
    383 		if (!__noqch && dnum > (int) win->maxy / 4)
    384 			quickch();
    385 	}
    386 
    387 #ifdef DEBUG
    388 	{
    389 		int	i, j;
    390 
    391 		__CTRACE("#####################################\n");
    392 		for (i = 0; i < curscr->maxy; i++) {
    393 			__CTRACE("C: %d:", i);
    394 			__CTRACE(" 0x%x \n", curscr->lines[i]->hash);
    395 			for (j = 0; j < curscr->maxx; j++)
    396 				__CTRACE("%c", curscr->lines[i]->line[j].ch);
    397 			__CTRACE("\n");
    398 			__CTRACE(" attr:");
    399 			for (j = 0; j < curscr->maxx; j++)
    400 				__CTRACE(" %x",
    401 					 curscr->lines[i]->line[j].attr);
    402 			__CTRACE("\n");
    403 			__CTRACE("W: %d:", i);
    404 			__CTRACE(" 0x%x \n", win->lines[i]->hash);
    405 			__CTRACE(" 0x%x ", win->lines[i]->flags);
    406 			for (j = 0; j < win->maxx; j++)
    407 				__CTRACE("%c", win->lines[i]->line[j].ch);
    408 			__CTRACE("\n");
    409 			__CTRACE(" attr:");
    410 			for (j = 0; j < win->maxx; j++)
    411 				__CTRACE(" %x",
    412 				    win->lines[i]->line[j].attr);
    413 			__CTRACE("\n");
    414 		}
    415 	}
    416 #endif				/* DEBUG */
    417 
    418 	for (wy = 0; wy < win->maxy; wy++) {
    419 		wlp = win->lines[wy];
    420 /* XXX: remove this debug */
    421 #ifdef DEBUG
    422 		__CTRACE("doupdate: wy %d\tf: %d\tl:%d\tflags %x\n", wy,
    423 		    *wlp->firstchp, *wlp->lastchp, wlp->flags);
    424 #endif
    425 		if (!_cursesi_screen->curwin)
    426 			curscr->lines[wy]->hash = wlp->hash;
    427 		if (wlp->flags & __ISDIRTY) {
    428 			if (makech(wy) == ERR)
    429 				return (ERR);
    430 			else {
    431 				if (*wlp->firstchp >= 0)
    432 					*wlp->firstchp = win->maxx;
    433 				if (*wlp->lastchp < win->maxx)
    434 					*wlp->lastchp = 0;
    435 				if (*wlp->lastchp < *wlp->firstchp) {
    436 #ifdef DEBUG
    437 					__CTRACE("doupdate: line %d notdirty\n", wy);
    438 #endif
    439 					wlp->flags &= ~__ISDIRTY;
    440 				}
    441 			}
    442 
    443 		}
    444 #ifdef DEBUG
    445 		__CTRACE("\t%d\t%d\n", *wlp->firstchp, *wlp->lastchp);
    446 #endif
    447 	}
    448 
    449 #ifdef DEBUG
    450 	__CTRACE("doupdate: ly=%d, lx=%d\n", _cursesi_screen->ly,
    451 		 _cursesi_screen->lx);
    452 #endif
    453 
    454 	if (_cursesi_screen->curwin)
    455 		domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
    456 			(int) win->cury, (int) win->curx);
    457 	else {
    458 		if (win->flags & __LEAVEOK) {
    459 			curscr->cury = _cursesi_screen->ly;
    460 			curscr->curx = _cursesi_screen->lx;
    461 		} else {
    462 			domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
    463 				win->cury, win->curx);
    464 			curscr->cury = win->cury;
    465 			curscr->curx = win->curx;
    466 		}
    467 	}
    468 
    469 	/* Don't leave the screen with attributes set. */
    470 	__unsetattr(0);
    471 	(void) fflush(_cursesi_screen->outfd);
    472 	return (OK);
    473 }
    474 
    475 /*
    476  * makech --
    477  *	Make a change on the screen.
    478  */
    479 static int
    480 makech(wy)
    481 	int	wy;
    482 {
    483 	WINDOW	*win;
    484 	static __LDATA blank = {' ', 0, ' ', 0};
    485 	__LDATA *nsp, *csp, *cp, *cep;
    486 	int	clsp, nlsp;	/* Last space in lines. */
    487 	int	lch, wx;
    488 	char	*ce;
    489 	attr_t	lspc;		/* Last space colour */
    490 	attr_t	off, on;
    491 
    492 #ifdef __GNUC__
    493 	nlsp = lspc = 0;	/* XXX gcc -Wuninitialized */
    494 #endif
    495 	if (_cursesi_screen->curwin)
    496 		win = curscr;
    497 	else
    498 		win = __virtscr;
    499 	/* Is the cursor still on the end of the last line? */
    500 	if (wy > 0 && curscr->lines[wy - 1]->flags & __ISPASTEOL) {
    501 		domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
    502 			_cursesi_screen->ly + 1, 0);
    503 		_cursesi_screen->ly++;
    504 		_cursesi_screen->lx = 0;
    505 	}
    506 	wx = *win->lines[wy]->firstchp;
    507 	if (wx < 0)
    508 		wx = 0;
    509 	else
    510 		if (wx >= win->maxx)
    511 			return (OK);
    512 	lch = *win->lines[wy]->lastchp;
    513 	if (lch < 0)
    514 		return (OK);
    515 	else
    516 		if (lch >= (int) win->maxx)
    517 			lch = win->maxx - 1;
    518 
    519 	if (_cursesi_screen->curwin)
    520 		csp = &blank;
    521 	else
    522 		csp = &curscr->lines[wy]->line[wx];
    523 
    524 	nsp = &win->lines[wy]->line[wx];
    525 	if (__tc_ce && !_cursesi_screen->curwin) {
    526 		cp = &win->lines[wy]->line[win->maxx - 1];
    527 		lspc = cp->attr & __COLOR;
    528 		while (cp->ch == ' ' && cp->attr == lspc)
    529 			if (cp-- <= win->lines[wy]->line)
    530 				break;
    531 		nlsp = cp - win->lines[wy]->line;
    532 		if (nlsp < 0)
    533 			nlsp = 0;
    534 	}
    535 	if (!_cursesi_screen->curwin)
    536 		ce = __tc_ce;
    537 	else
    538 		ce = NULL;
    539 
    540 	while (wx <= lch) {
    541 		if (memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
    542 			if (wx <= lch) {
    543 				while (wx <= lch &&
    544 				    memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
    545 					nsp++;
    546 					if (!_cursesi_screen->curwin)
    547 						++csp;
    548 					++wx;
    549 				}
    550 				continue;
    551 			}
    552 			break;
    553 		}
    554 		domvcur(_cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
    555 
    556 #ifdef DEBUG
    557 		__CTRACE("makech: 1: wx = %d, ly= %d, lx = %d, newy = %d, newx = %d\n",
    558 		    wx, _cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
    559 #endif
    560 		_cursesi_screen->ly = wy;
    561 		_cursesi_screen->lx = wx;
    562 		while (memcmp(nsp, csp, sizeof(__LDATA)) != 0 && wx <= lch) {
    563 			if (ce != NULL &&
    564 			    wx >= nlsp && nsp->ch == ' ' && nsp->attr == lspc) {
    565 				/* Check for clear to end-of-line. */
    566 				cep = &curscr->lines[wy]->line[win->maxx - 1];
    567 				while (cep->ch == ' ' && cep->attr == lspc)
    568 					if (cep-- <= csp)
    569 						break;
    570 				clsp = cep - curscr->lines[wy]->line -
    571 				    win->begx * __LDATASIZE;
    572 #ifdef DEBUG
    573 				__CTRACE("makech: clsp = %d, nlsp = %d\n",
    574 				    clsp, nlsp);
    575 #endif
    576 				if (((clsp - nlsp >= strlen(__tc_ce) &&
    577 				    clsp < win->maxx * __LDATASIZE) ||
    578 				    wy == win->maxy - 1) &&
    579 				    (!(lspc & __COLOR) ||
    580 				    ((lspc & __COLOR) && __tc_ut))) {
    581 					__unsetattr(0);
    582 					if (__using_color &&
    583 					    ((lspc & __COLOR) !=
    584 					    (curscr->wattr & __COLOR)))
    585 						__set_color(curscr, lspc &
    586 						    __COLOR);
    587 					tputs(__tc_ce, 0, __cputchar);
    588 					_cursesi_screen->lx = wx + win->begx;
    589 					while (wx++ <= clsp) {
    590 						csp->ch = ' ';
    591 						csp->attr = lspc;
    592 						csp++;
    593 					}
    594 					return (OK);
    595 				}
    596 				ce = NULL;
    597 			}
    598 
    599 #ifdef DEBUG
    600 				__CTRACE("makech: have attributes %08x, need attributes %08x\n", curscr->wattr, nsp->attr);
    601 #endif
    602 
    603 			off = ~nsp->attr & curscr->wattr;
    604 
    605 			/*
    606 			 * Unset attributes as appropriate.  Unset first
    607 			 * so that the relevant attributes can be reset
    608 			 * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
    609 			 * 'mp' and 'mr').  Check to see if we also turn off
    610 			 * standout, attributes and colour.
    611 			 */
    612 			if (off & __TERMATTR && __tc_me != NULL) {
    613 				tputs(__tc_me, 0, __cputchar);
    614 				curscr->wattr &= __mask_me;
    615 				off &= __mask_me;
    616 			}
    617 
    618 			/*
    619 			 * Exit underscore mode if appropriate.
    620 			 * Check to see if we also turn off standout,
    621 			 * attributes and colour.
    622 			 */
    623 			if (off & __UNDERSCORE && __tc_ue != NULL) {
    624 				tputs(__tc_ue, 0, __cputchar);
    625 				curscr->wattr &= __mask_ue;
    626 				off &= __mask_ue;
    627 			}
    628 
    629 			/*
    630 			 * Exit standout mode as appropriate.
    631 			 * Check to see if we also turn off underscore,
    632 			 * attributes and colour.
    633 			 * XXX
    634 			 * Should use uc if so/se not available.
    635 			 */
    636 			if (off & __STANDOUT && __tc_se != NULL) {
    637 				tputs(__tc_se, 0, __cputchar);
    638 				curscr->wattr &= __mask_se;
    639 				off &= __mask_se;
    640 			}
    641 
    642 			if (off & __ALTCHARSET && __tc_ae != NULL) {
    643 				tputs(__tc_ae, 0, __cputchar);
    644 				curscr->wattr &= ~__ALTCHARSET;
    645 			}
    646 
    647 			/* Set/change colour as appropriate. */
    648 			if (__using_color)
    649 				__set_color(curscr, nsp->attr & __COLOR);
    650 
    651 			on = nsp->attr & ~curscr->wattr;
    652 
    653 			/*
    654 			 * Enter standout mode if appropriate.
    655 			 */
    656 			if (on & __STANDOUT && __tc_so != NULL && __tc_se
    657 			    != NULL) {
    658 				tputs(__tc_so, 0, __cputchar);
    659 				curscr->wattr |= __STANDOUT;
    660 			}
    661 
    662 			/*
    663 			 * Enter underscore mode if appropriate.
    664 			 * XXX
    665 			 * Should use uc if us/ue not available.
    666 			 */
    667 			if (on & __UNDERSCORE && __tc_us != NULL &&
    668 			    __tc_ue != NULL) {
    669 				tputs(__tc_us, 0, __cputchar);
    670 				curscr->wattr |= __UNDERSCORE;
    671 			}
    672 
    673 			/*
    674 			 * Set other attributes as appropriate.
    675 			 */
    676 			if (__tc_me != NULL) {
    677 				if (on & __BLINK && __tc_mb != NULL) {
    678 					tputs(__tc_mb, 0, __cputchar);
    679 					curscr->wattr |= __BLINK;
    680 				}
    681 				if (on & __BOLD && __tc_md != NULL) {
    682 					tputs(__tc_md, 0, __cputchar);
    683 					curscr->wattr |= __BOLD;
    684 				}
    685 				if (on & __DIM && __tc_mh != NULL) {
    686 					tputs(__tc_mh, 0, __cputchar);
    687 					curscr->wattr |= __DIM;
    688 				}
    689 				if (on & __BLANK && __tc_mk != NULL) {
    690 					tputs(__tc_mk, 0, __cputchar);
    691 					curscr->wattr |= __BLANK;
    692 				}
    693 				if (on & __PROTECT && __tc_mp != NULL) {
    694 					tputs(__tc_mp, 0, __cputchar);
    695 					curscr->wattr |= __PROTECT;
    696 				}
    697 				if (on & __REVERSE && __tc_mr != NULL) {
    698 					tputs(__tc_mr, 0, __cputchar);
    699 					curscr->wattr |= __REVERSE;
    700 				}
    701 			}
    702 
    703 			/* Enter/exit altcharset mode as appropriate. */
    704 			if (on & __ALTCHARSET && __tc_as != NULL &&
    705 			    __tc_ae != NULL) {
    706 				tputs(__tc_as, 0, __cputchar);
    707 				curscr->wattr |= __ALTCHARSET;
    708 			}
    709 
    710 			wx++;
    711 			if (wx >= win->maxx &&
    712 			    wy == win->maxy - 1 && !_cursesi_screen->curwin)
    713 				if (win->flags & __SCROLLOK) {
    714 					if (win->flags & __ENDLINE)
    715 						__unsetattr(1);
    716 					if (!(win->flags & __SCROLLWIN)) {
    717 						if (!_cursesi_screen->curwin) {
    718 							csp->attr = nsp->attr;
    719 							__cputchar((int)
    720 							    (csp->ch =
    721 							    nsp->ch));
    722 						} else
    723 							__cputchar((int) nsp->ch);
    724 					}
    725 					if (wx < curscr->maxx) {
    726 						domvcur(_cursesi_screen->ly, wx,
    727 						    (int) (win->maxy - 1),
    728 						    (int) (win->maxx - 1));
    729 					}
    730 					_cursesi_screen->ly = win->maxy - 1;
    731 					_cursesi_screen->lx = win->maxx - 1;
    732 					return (OK);
    733 				}
    734 			if (wx < win->maxx || wy < win->maxy - 1 ||
    735 			    !(win->flags & __SCROLLWIN)) {
    736 				if (!_cursesi_screen->curwin) {
    737 					csp->attr = nsp->attr;
    738 					__cputchar((int) (csp->ch = nsp->ch));
    739 					csp++;
    740 				} else
    741 					__cputchar((int) nsp->ch);
    742 			}
    743 #ifdef DEBUG
    744 			__CTRACE("makech: putchar(%c)\n", nsp->ch & 0177);
    745 #endif
    746 			if (__tc_uc && ((nsp->attr & __STANDOUT) ||
    747 			    (nsp->attr & __UNDERSCORE))) {
    748 				__cputchar('\b');
    749 				tputs(__tc_uc, 0, __cputchar);
    750 			}
    751 			nsp++;
    752 #ifdef DEBUG
    753 			__CTRACE("makech: 2: wx = %d, lx = %d\n", wx, _cursesi_screen->lx);
    754 #endif
    755 		}
    756 		if (_cursesi_screen->lx == wx)	/* If no change. */
    757 			break;
    758 		_cursesi_screen->lx = wx;
    759 		if (_cursesi_screen->lx >= COLS && __tc_am)
    760 			_cursesi_screen->lx = COLS - 1;
    761 		else
    762 			if (wx >= win->maxx) {
    763 				domvcur(_cursesi_screen->ly,
    764 					_cursesi_screen->lx,
    765 					_cursesi_screen->ly,
    766 					(int) (win->maxx - 1));
    767 				_cursesi_screen->lx = win->maxx - 1;
    768 			}
    769 #ifdef DEBUG
    770 		__CTRACE("makech: 3: wx = %d, lx = %d\n", wx,
    771 			 _cursesi_screen->lx);
    772 #endif
    773 	}
    774 
    775 	return (OK);
    776 }
    777 
    778 /*
    779  * domvcur --
    780  *	Do a mvcur, leaving attributes if necessary.
    781  */
    782 static void
    783 domvcur(oy, ox, ny, nx)
    784 	int	oy, ox, ny, nx;
    785 {
    786 	__unsetattr(1);
    787 	__mvcur(oy, ox, ny, nx, 1);
    788 }
    789 
    790 /*
    791  * Quickch() attempts to detect a pattern in the change of the window
    792  * in order to optimize the change, e.g., scroll n lines as opposed to
    793  * repainting the screen line by line.
    794  */
    795 
    796 static __LDATA buf[128];
    797 static  u_int last_hash;
    798 static  size_t last_hash_len;
    799 #define BLANKSIZE (sizeof(buf) / sizeof(buf[0]))
    800 
    801 static void
    802 quickch(void)
    803 {
    804 #define THRESH		(int) __virtscr->maxy / 4
    805 
    806 	__LINE *clp, *tmp1, *tmp2;
    807 	int	bsize, curs, curw, starts, startw, i, j;
    808 	int	n, target, cur_period, bot, top, sc_region;
    809 	u_int	blank_hash;
    810 	attr_t	bcolor;
    811 
    812 #ifdef __GNUC__
    813 	curs = curw = starts = startw = 0;	/* XXX gcc -Wuninitialized */
    814 #endif
    815 	/*
    816 	 * Find how many lines from the top of the screen are unchanged.
    817 	 */
    818 	for (top = 0; top < __virtscr->maxy; top++)
    819 		if (__virtscr->lines[top]->flags & __ISDIRTY &&
    820 		    (__virtscr->lines[top]->hash != curscr->lines[top]->hash ||
    821 		     memcmp(__virtscr->lines[top]->line,
    822 			  curscr->lines[top]->line,
    823 			  (size_t) __virtscr->maxx * __LDATASIZE) != 0))
    824 			break;
    825 		else
    826 			__virtscr->lines[top]->flags &= ~__ISDIRTY;
    827 	/*
    828 	 * Find how many lines from bottom of screen are unchanged.
    829 	 */
    830 	for (bot = __virtscr->maxy - 1; bot >= 0; bot--)
    831 		if (__virtscr->lines[bot]->flags & __ISDIRTY &&
    832 		    (__virtscr->lines[bot]->hash != curscr->lines[bot]->hash ||
    833 		     memcmp(__virtscr->lines[bot]->line,
    834 			  curscr->lines[bot]->line,
    835 			  (size_t) __virtscr->maxx * __LDATASIZE) != 0))
    836 			break;
    837 		else
    838 			__virtscr->lines[bot]->flags &= ~__ISDIRTY;
    839 
    840 	/*
    841 	 * Work round an xterm bug where inserting lines causes all the
    842 	 * inserted lines to be covered with the background colour we
    843 	 * set on the first line (even if we unset it for subsequent
    844 	 * lines).
    845 	 */
    846 	bcolor = __virtscr->lines[min(top,
    847 	    __virtscr->maxy - 1)]->line[0].attr & __COLOR;
    848 	for (i = top + 1, j = 0; i < bot; i++) {
    849 		if ((__virtscr->lines[i]->line[0].attr & __COLOR) != bcolor) {
    850 			bcolor = __virtscr->lines[i]->line[__virtscr->maxx].
    851 			    attr & __COLOR;
    852 			j = i - top;
    853 		} else
    854 			break;
    855 	}
    856 	top += j;
    857 
    858 #ifdef NO_JERKINESS
    859 	/*
    860 	 * If we have a bottom unchanged region return.  Scrolling the
    861 	 * bottom region up and then back down causes a screen jitter.
    862 	 * This will increase the number of characters sent to the screen
    863 	 * but it looks better.
    864 	 */
    865 	if (bot < __virtscr->maxy - 1)
    866 		return;
    867 #endif				/* NO_JERKINESS */
    868 
    869 	/*
    870 	 * Search for the largest block of text not changed.
    871 	 * Invariants of the loop:
    872 	 * - Startw is the index of the beginning of the examined block in
    873 	 *   __virtscr.
    874 	 * - Starts is the index of the beginning of the examined block in
    875 	 *   curscr.
    876 	 * - Curw is the index of one past the end of the exmined block in
    877 	 *   __virtscr.
    878 	 * - Curs is the index of one past the end of the exmined block in
    879 	 *   curscr.
    880 	 * - bsize is the current size of the examined block.
    881 	*/
    882 
    883 	for (bsize = bot - top; bsize >= THRESH; bsize--) {
    884 		for (startw = top; startw <= bot - bsize; startw++)
    885 			for (starts = top; starts <= bot - bsize;
    886 			    starts++) {
    887 				for (curw = startw, curs = starts;
    888 				    curs < starts + bsize; curw++, curs++)
    889 					if (__virtscr->lines[curw]->hash !=
    890 					    curscr->lines[curs]->hash)
    891 						break;
    892 				if (curs != starts + bsize)
    893 					continue;
    894 				for (curw = startw, curs = starts;
    895 				    curs < starts + bsize; curw++, curs++)
    896 					if (memcmp(__virtscr->lines[curw]->line,
    897 					    curscr->lines[curs]->line,
    898 					    (size_t) __virtscr->maxx *
    899 					    __LDATASIZE) != 0)
    900 						break;
    901 				if (curs == starts + bsize)
    902 					goto done;
    903 			}
    904 	}
    905 done:
    906 
    907 	/* Did not find anything */
    908 	if (bsize < THRESH)
    909 		return;
    910 
    911 #ifdef DEBUG
    912 	__CTRACE("quickch:bsize=%d,starts=%d,startw=%d,curw=%d,curs=%d,top=%d,bot=%d\n",
    913 	    bsize, starts, startw, curw, curs, top, bot);
    914 #endif
    915 
    916 	/*
    917 	 * Make sure that there is no overlap between the bottom and top
    918 	 * regions and the middle scrolled block.
    919 	 */
    920 	if (bot < curs)
    921 		bot = curs - 1;
    922 	if (top > starts)
    923 		top = starts;
    924 
    925 	n = startw - starts;
    926 
    927 #ifdef DEBUG
    928 	__CTRACE("#####################################\n");
    929 	for (i = 0; i < curscr->maxy; i++) {
    930 		__CTRACE("C: %d:", i);
    931 		__CTRACE(" 0x%x \n", curscr->lines[i]->hash);
    932 		for (j = 0; j < curscr->maxx; j++)
    933 			__CTRACE("%c", curscr->lines[i]->line[j].ch);
    934 		__CTRACE("\n");
    935 		__CTRACE(" attr:");
    936 		for (j = 0; j < curscr->maxx; j++)
    937 			__CTRACE(" %x", curscr->lines[i]->line[j].attr);
    938 		__CTRACE("\n");
    939 		__CTRACE("W: %d:", i);
    940 		__CTRACE(" 0x%x \n", __virtscr->lines[i]->hash);
    941 		__CTRACE(" 0x%x ", __virtscr->lines[i]->flags);
    942 		for (j = 0; j < __virtscr->maxx; j++)
    943 			__CTRACE("%c", __virtscr->lines[i]->line[j].ch);
    944 		__CTRACE("\n");
    945 		__CTRACE(" attr:");
    946 		for (j = 0; j < __virtscr->maxx; j++)
    947 			__CTRACE(" %x", __virtscr->lines[i]->line[j].attr);
    948 		__CTRACE("\n");
    949 	}
    950 #endif
    951 
    952 	if (buf[0].ch != ' ') {
    953 		for (i = 0; i < BLANKSIZE; i++) {
    954 			buf[i].ch = ' ';
    955 			buf[i].bch = ' ';
    956 			buf[i].attr = 0;
    957 			buf[i].battr = 0;
    958 		}
    959 	}
    960 
    961 	if (__virtscr->maxx != last_hash_len) {
    962 		blank_hash = 0;
    963 		for (i = __virtscr->maxx; i > BLANKSIZE; i -= BLANKSIZE) {
    964 			blank_hash = __hash_more((char *)(void *)buf, sizeof(buf),
    965 			    blank_hash);
    966 		}
    967 		blank_hash = __hash_more((char *)(void *)buf,
    968 		    i * sizeof(buf[0]), blank_hash);
    969 		/* cache result in static data - screen width doesn't change often */
    970 		last_hash_len = __virtscr->maxx;
    971 		last_hash = blank_hash;
    972 	} else
    973 		blank_hash = last_hash;
    974 
    975 	/*
    976 	 * Perform the rotation to maintain the consistency of curscr.
    977 	 * This is hairy since we are doing an *in place* rotation.
    978 	 * Invariants of the loop:
    979 	 * - I is the index of the current line.
    980 	 * - Target is the index of the target of line i.
    981 	 * - Tmp1 points to current line (i).
    982 	 * - Tmp2 and points to target line (target);
    983 	 * - Cur_period is the index of the end of the current period.
    984 	 *   (see below).
    985 	 *
    986 	 * There are 2 major issues here that make this rotation non-trivial:
    987 	 * 1.  Scrolling in a scrolling region bounded by the top
    988 	 *     and bottom regions determined (whose size is sc_region).
    989 	 * 2.  As a result of the use of the mod function, there may be a
    990 	 *     period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
    991 	 *     0 to 2, which then causes all odd lines not to be rotated.
    992 	 *     To remedy this, an index of the end ( = beginning) of the
    993 	 *     current 'period' is kept, cur_period, and when it is reached,
    994 	 *     the next period is started from cur_period + 1 which is
    995 	 *     guaranteed not to have been reached since that would mean that
    996 	 *     all records would have been reached. (think about it...).
    997 	 *
    998 	 * Lines in the rotation can have 3 attributes which are marked on the
    999 	 * line so that curscr is consistent with the visual screen.
   1000 	 * 1.  Not dirty -- lines inside the scrolled block, top region or
   1001 	 *                  bottom region.
   1002 	 * 2.  Blank lines -- lines in the differential of the scrolling
   1003 	 *		      region adjacent to top and bot regions
   1004 	 *                    depending on scrolling direction.
   1005 	 * 3.  Dirty line -- all other lines are marked dirty.
   1006 	 */
   1007 	sc_region = bot - top + 1;
   1008 	i = top;
   1009 	tmp1 = curscr->lines[top];
   1010 	cur_period = top;
   1011 	for (j = top; j <= bot; j++) {
   1012 		target = (i - top + n + sc_region) % sc_region + top;
   1013 		tmp2 = curscr->lines[target];
   1014 		curscr->lines[target] = tmp1;
   1015 		/* Mark block as clean and blank out scrolled lines. */
   1016 		clp = curscr->lines[target];
   1017 #ifdef DEBUG
   1018 		__CTRACE("quickch: n=%d startw=%d curw=%d i = %d target=%d ",
   1019 		    n, startw, curw, i, target);
   1020 #endif
   1021 		if ((target >= startw && target < curw) || target < top
   1022 		    || target > bot) {
   1023 #ifdef DEBUG
   1024 			__CTRACE("-- notdirty\n");
   1025 #endif
   1026 			__virtscr->lines[target]->flags &= ~__ISDIRTY;
   1027 		} else
   1028 			if ((n > 0 && target >= top && target < top + n) ||
   1029 			    (n < 0 && target <= bot && target > bot + n)) {
   1030 				if (clp->hash != blank_hash || memcmp(clp->line,
   1031 				    clp->line + 1, (__virtscr->maxx - 1) *
   1032 				    __LDATASIZE) || memcmp(clp->line, buf,
   1033 				    __LDATASIZE)) {
   1034 					for (i = __virtscr->maxx; i > BLANKSIZE;
   1035 					    i -= BLANKSIZE) {
   1036 						(void)memcpy(clp->line + i -
   1037 						    BLANKSIZE, buf, sizeof(buf));
   1038 					}
   1039 					(void)memcpy(clp->line , buf, i *
   1040 					    sizeof(buf[0]));
   1041 #ifdef DEBUG
   1042 					__CTRACE("-- blanked out: dirty\n");
   1043 #endif
   1044 					clp->hash = blank_hash;
   1045 					__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
   1046 				} else {
   1047 #ifdef DEBUG
   1048 					__CTRACE(" -- blank line already: dirty\n");
   1049 #endif
   1050 					__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
   1051 				}
   1052 			} else {
   1053 #ifdef DEBUG
   1054 				__CTRACE(" -- dirty\n");
   1055 #endif
   1056 				__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
   1057 			}
   1058 		if (target == cur_period) {
   1059 			i = target + 1;
   1060 			tmp1 = curscr->lines[i];
   1061 			cur_period = i;
   1062 		} else {
   1063 			tmp1 = tmp2;
   1064 			i = target;
   1065 		}
   1066 	}
   1067 #ifdef DEBUG
   1068 	__CTRACE("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
   1069 	for (i = 0; i < curscr->maxy; i++) {
   1070 		__CTRACE("C: %d:", i);
   1071 		for (j = 0; j < curscr->maxx; j++)
   1072 			__CTRACE("%c", curscr->lines[i]->line[j].ch);
   1073 		__CTRACE("\n");
   1074 		__CTRACE("W: %d:", i);
   1075 		for (j = 0; j < __virtscr->maxx; j++)
   1076 			__CTRACE("%c", __virtscr->lines[i]->line[j].ch);
   1077 		__CTRACE("\n");
   1078 	}
   1079 #endif
   1080 	if (n != 0)
   1081 		scrolln(starts, startw, curs, bot, top);
   1082 }
   1083 
   1084 /*
   1085  * scrolln --
   1086  *	Scroll n lines, where n is starts - startw.
   1087  */
   1088 static void /* ARGSUSED */
   1089 scrolln(starts, startw, curs, bot, top)
   1090 	int	starts, startw, curs, bot, top;
   1091 {
   1092 	int	i, oy, ox, n;
   1093 
   1094 	oy = curscr->cury;
   1095 	ox = curscr->curx;
   1096 	n = starts - startw;
   1097 
   1098 	/*
   1099 	 * XXX
   1100 	 * The initial tests that set __noqch don't let us reach here unless
   1101 	 * we have either cs + ho + SF/sf/SR/sr, or AL + DL.  SF/sf and SR/sr
   1102 	 * scrolling can only shift the entire scrolling region, not just a
   1103 	 * part of it, which means that the quickch() routine is going to be
   1104 	 * sadly disappointed in us if we don't have cs as well.
   1105 	 *
   1106 	 * If cs, ho and SF/sf are set, can use the scrolling region.  Because
   1107 	 * the cursor position after cs is undefined, we need ho which gives us
   1108 	 * the ability to move to somewhere without knowledge of the current
   1109 	 * location of the cursor.  Still call __mvcur() anyway, to update its
   1110 	 * idea of where the cursor is.
   1111 	 *
   1112 	 * When the scrolling region has been set, the cursor has to be at the
   1113 	 * last line of the region to make the scroll happen.
   1114 	 *
   1115 	 * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
   1116 	 * or AL/DL, and, some terminals have AL/DL, sf/sr, and cs, but not
   1117 	 * SF/SR.  So, if we're scrolling almost all of the screen, try and use
   1118 	 * AL/DL, otherwise use the scrolling region.  The "almost all" is a
   1119 	 * shameless hack for vi.
   1120 	 */
   1121 	if (n > 0) {
   1122 		if (__tc_cs != NULL && __tc_ho != NULL && (__tc_SF != NULL ||
   1123 		    ((__tc_AL == NULL || __tc_DL == NULL ||
   1124 		    top > 3 || bot + 3 < __virtscr->maxy) &&
   1125 		    __tc_sf != NULL))) {
   1126 			tputs(__tscroll(__tc_cs, top, bot + 1), 0, __cputchar);
   1127 			__mvcur(oy, ox, 0, 0, 1);
   1128 			tputs(__tc_ho, 0, __cputchar);
   1129 			__mvcur(0, 0, bot, 0, 1);
   1130 			if (__tc_SF != NULL)
   1131 				tputs(__tscroll(__tc_SF, n, 0), 0, __cputchar);
   1132 			else
   1133 				for (i = 0; i < n; i++)
   1134 					tputs(__tc_sf, 0, __cputchar);
   1135 			tputs(__tscroll(__tc_cs, 0, (int) __virtscr->maxy), 0,
   1136 			    __cputchar);
   1137 			__mvcur(bot, 0, 0, 0, 1);
   1138 			tputs(__tc_ho, 0, __cputchar);
   1139 			__mvcur(0, 0, oy, ox, 1);
   1140 			return;
   1141 		}
   1142 
   1143 		/* Scroll up the block. */
   1144 		if (__tc_SF != NULL && top == 0) {
   1145 			__mvcur(oy, ox, bot, 0, 1);
   1146 			tputs(__tscroll(__tc_SF, n, 0), 0, __cputchar);
   1147 		} else
   1148 			if (__tc_DL != NULL) {
   1149 				__mvcur(oy, ox, top, 0, 1);
   1150 				tputs(__tscroll(__tc_DL, n, 0), 0, __cputchar);
   1151 			} else
   1152 				if (__tc_dl != NULL) {
   1153 					__mvcur(oy, ox, top, 0, 1);
   1154 					for (i = 0; i < n; i++)
   1155 						tputs(__tc_dl, 0, __cputchar);
   1156 				} else
   1157 					if (__tc_sf != NULL && top == 0) {
   1158 						__mvcur(oy, ox, bot, 0, 1);
   1159 						for (i = 0; i < n; i++)
   1160 							tputs(__tc_sf, 0,
   1161 							    __cputchar);
   1162 					} else
   1163 						abort();
   1164 
   1165 		/* Push down the bottom region. */
   1166 		__mvcur(top, 0, bot - n + 1, 0, 1);
   1167 		if (__tc_AL != NULL)
   1168 			tputs(__tscroll(__tc_AL, n, 0), 0, __cputchar);
   1169 		else
   1170 			if (__tc_al != NULL)
   1171 				for (i = 0; i < n; i++)
   1172 					tputs(__tc_al, 0, __cputchar);
   1173 			else
   1174 				abort();
   1175 		__mvcur(bot - n + 1, 0, oy, ox, 1);
   1176 	} else {
   1177 		/*
   1178 		 * !!!
   1179 		 * n < 0
   1180 		 *
   1181 		 * If cs, ho and SR/sr are set, can use the scrolling region.
   1182 		 * See the above comments for details.
   1183 		 */
   1184 		if (__tc_cs != NULL && __tc_ho != NULL && (__tc_SR != NULL ||
   1185 		    ((__tc_AL == NULL || __tc_DL == NULL || top > 3 ||
   1186 		    bot + 3 < __virtscr->maxy) && __tc_sr != NULL))) {
   1187 			tputs(__tscroll(__tc_cs, top, bot + 1), 0, __cputchar);
   1188 			__mvcur(oy, ox, 0, 0, 1);
   1189 			tputs(__tc_ho, 0, __cputchar);
   1190 			__mvcur(0, 0, top, 0, 1);
   1191 
   1192 			if (__tc_SR != NULL)
   1193 				tputs(__tscroll(__tc_SR, -n, 0), 0, __cputchar);
   1194 			else
   1195 				for (i = n; i < 0; i++)
   1196 					tputs(__tc_sr, 0, __cputchar);
   1197 			tputs(__tscroll(__tc_cs, 0, (int) __virtscr->maxy), 0,
   1198 			    __cputchar);
   1199 			__mvcur(top, 0, 0, 0, 1);
   1200 			tputs(__tc_ho, 0, __cputchar);
   1201 			__mvcur(0, 0, oy, ox, 1);
   1202 			return;
   1203 		}
   1204 
   1205 		/* Preserve the bottom lines. */
   1206 		__mvcur(oy, ox, bot + n + 1, 0, 1);
   1207 		if (__tc_SR != NULL && bot == __virtscr->maxy)
   1208 			tputs(__tscroll(__tc_SR, -n, 0), 0, __cputchar);
   1209 		else
   1210 			if (__tc_DL != NULL)
   1211 				tputs(__tscroll(__tc_DL, -n, 0), 0, __cputchar);
   1212 			else
   1213 				if (__tc_dl != NULL)
   1214 					for (i = n; i < 0; i++)
   1215 						tputs(__tc_dl, 0, __cputchar);
   1216 				else
   1217 					if (__tc_sr != NULL &&
   1218 					    bot == __virtscr->maxy)
   1219 						for (i = n; i < 0; i++)
   1220 							tputs(__tc_sr, 0,
   1221 							    __cputchar);
   1222 					else
   1223 						abort();
   1224 
   1225 		/* Scroll the block down. */
   1226 		__mvcur(bot + n + 1, 0, top, 0, 1);
   1227 		if (__tc_AL != NULL)
   1228 			tputs(__tscroll(__tc_AL, -n, 0), 0, __cputchar);
   1229 		else
   1230 			if (__tc_al != NULL)
   1231 				for (i = n; i < 0; i++)
   1232 					tputs(__tc_al, 0, __cputchar);
   1233 			else
   1234 				abort();
   1235 		__mvcur(top, 0, oy, ox, 1);
   1236 	}
   1237 }
   1238 
   1239 /*
   1240  * __unsetattr --
   1241  *	Unset attributes on curscr.  Leave standout, attribute and colour
   1242  *	modes if necessary (!ms).  Always leave altcharset (xterm at least
   1243  *	ignores a cursor move if we don't).
   1244  */
   1245 void /* ARGSUSED */
   1246 __unsetattr(int checkms)
   1247 {
   1248 	int	isms;
   1249 
   1250 	if (checkms)
   1251 		if (!__tc_ms) {
   1252 			isms = 1;
   1253 		} else {
   1254 			isms = 0;
   1255 		}
   1256 	else
   1257 		isms = 1;
   1258 #ifdef DEBUG
   1259 	__CTRACE("__unsetattr: checkms = %d, ms = %s, wattr = %08x\n",
   1260 	    checkms, __tc_ms ? "TRUE" : "FALSE", curscr->wattr);
   1261 #endif
   1262 
   1263 	/*
   1264          * Don't leave the screen in standout mode (check against ms).  Check
   1265 	 * to see if we also turn off underscore, attributes and colour.
   1266 	 */
   1267 	if (curscr->wattr & __STANDOUT && isms) {
   1268 		tputs(__tc_se, 0, __cputchar);
   1269 		curscr->wattr &= __mask_se;
   1270 	}
   1271 	/*
   1272 	 * Don't leave the screen in underscore mode (check against ms).
   1273 	 * Check to see if we also turn off attributes.  Assume that we
   1274 	 * also turn off colour.
   1275 	 */
   1276 	if (curscr->wattr & __UNDERSCORE && isms) {
   1277 		tputs(__tc_ue, 0, __cputchar);
   1278 		curscr->wattr &= __mask_ue;
   1279 	}
   1280 	/*
   1281 	 * Don't leave the screen with attributes set (check against ms).
   1282 	 * Assume that also turn off colour.
   1283 	 */
   1284 	if (curscr->wattr & __TERMATTR && isms) {
   1285 		tputs(__tc_me, 0, __cputchar);
   1286 		curscr->wattr &= __mask_me;
   1287 	}
   1288 	/* Don't leave the screen with altcharset set (don't check ms). */
   1289 	if (curscr->wattr & __ALTCHARSET) {
   1290 		tputs(__tc_ae, 0, __cputchar);
   1291 		curscr->wattr &= ~__ALTCHARSET;
   1292 	}
   1293 	/* Don't leave the screen with colour set (check against ms). */
   1294 	if (__using_color && isms)
   1295 		__unset_color(curscr);
   1296 }
   1297