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