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