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