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