17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include	<sys/types.h>
437c478bd9Sstevel@tonic-gate #include	<stdlib.h>
447c478bd9Sstevel@tonic-gate #include	<string.h>
457c478bd9Sstevel@tonic-gate #include	"curses_inc.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Make the screen look like "win" over the area covered by win.
497c478bd9Sstevel@tonic-gate  * This routine may use insert/delete char/line and scrolling-region.
507c478bd9Sstevel@tonic-gate  * win : the window being updated
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate extern int	outchcount;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static void	_updateln(int), _turn_off_background(void),
567c478bd9Sstevel@tonic-gate 		_setmark1(int, int, chtype *),
577c478bd9Sstevel@tonic-gate 		_setmark2(int, int, chtype *), _rmargin(int),
587c478bd9Sstevel@tonic-gate 		_useceod(int, int);
597c478bd9Sstevel@tonic-gate static int	_useidch(chtype *, chtype *, int, int, int *),
607c478bd9Sstevel@tonic-gate 		_prefix(chtype *, chtype *, int, int, int *),
617c478bd9Sstevel@tonic-gate 		_getceod(int, int);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static	short	cy, cx,		/* current cursor coord */
647c478bd9Sstevel@tonic-gate 		scrli,		/* actual screen lines */
657c478bd9Sstevel@tonic-gate 		scrco;		/* actual screen columns */
667c478bd9Sstevel@tonic-gate static	char	**marks;	/* the mark table for cookie terminals */
677c478bd9Sstevel@tonic-gate static	char	**color_marks;	/* color mark table for cookie terminals */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	_ISMARK1(y, x)	(marks[y][x / BITSPERBYTE] & (1 << (x % BITSPERBYTE)))
707c478bd9Sstevel@tonic-gate #define	_ISMARK2(y, x)	(color_marks ? (color_marks[y][x / BITSPERBYTE] & \
717c478bd9Sstevel@tonic-gate 			(1 << (x % BITSPERBYTE))) : FALSE)
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	_VIDEO(c)	((c) & A_ATTRIBUTES & ~A_COLOR)
747c478bd9Sstevel@tonic-gate #define	_COLOR(c)	((c) & A_COLOR)
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #ifdef	_VR2_COMPAT_CODE
777c478bd9Sstevel@tonic-gate extern	char	_endwin;
787c478bd9Sstevel@tonic-gate #endif	/* _VR2_COMPAT_CODE */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate int
wrefresh(WINDOW * win)817c478bd9Sstevel@tonic-gate wrefresh(WINDOW *win)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	short	*bnsch, *ensch;
857c478bd9Sstevel@tonic-gate 	SLK_MAP	*slk;
867c478bd9Sstevel@tonic-gate 	int	wx, wy, nc, boty, clby, idby, *hs, curwin;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	curwin = (win == curscr);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/* don't allow curscr refresh if the screen was just created */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (curwin && curscr->_sync)
937c478bd9Sstevel@tonic-gate 		return (OK);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	/* go thru _stdbody */
967c478bd9Sstevel@tonic-gate 	if (!curwin && (win != _virtscr))
977c478bd9Sstevel@tonic-gate 		(void) wnoutrefresh(win);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/* if there is typeahead */
1007c478bd9Sstevel@tonic-gate 	if ((_INPUTPENDING = _chkinput()) == TRUE) {
1017c478bd9Sstevel@tonic-gate 		if (curwin)
1027c478bd9Sstevel@tonic-gate 			curscr->_clear = TRUE;
1037c478bd9Sstevel@tonic-gate 		return (OK);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (curwin || curscr->_clear)
1077c478bd9Sstevel@tonic-gate 		_virtscr->_clear = TRUE;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/* save curscr cursor coordinates */
1107c478bd9Sstevel@tonic-gate 	cy = curscr->_cury;
1117c478bd9Sstevel@tonic-gate 	cx = curscr->_curx;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/* to simplify code in some cases */
1147c478bd9Sstevel@tonic-gate 	marks = _MARKS;
1157c478bd9Sstevel@tonic-gate 	color_marks = _COLOR_MARKS;
1167c478bd9Sstevel@tonic-gate 	scrli = curscr->_maxy;
1177c478bd9Sstevel@tonic-gate 	scrco = curscr->_maxx;
1187c478bd9Sstevel@tonic-gate 	slk = SP->slk;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	outchcount = 0;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	/* make sure we're in program mode */
1237c478bd9Sstevel@tonic-gate 	if (SP->fl_endwin) {
1247c478bd9Sstevel@tonic-gate 		/* If endwin is equal to 2 it means we just did a newscreen. */
1257c478bd9Sstevel@tonic-gate 		if (SP->fl_endwin == TRUE) {
1267c478bd9Sstevel@tonic-gate 			(void) reset_prog_mode();
1277c478bd9Sstevel@tonic-gate 			if (SP->kp_state)
1287c478bd9Sstevel@tonic-gate 				(void) tputs(keypad_xmit, 1, _outch);
1297c478bd9Sstevel@tonic-gate 			if (slk)
1307c478bd9Sstevel@tonic-gate 				(*_do_slk_tch)();
1317c478bd9Sstevel@tonic-gate 			if (SP->fl_meta)
1327c478bd9Sstevel@tonic-gate 				(void) tputs(meta_on, 1, _outch);
1337c478bd9Sstevel@tonic-gate 			if (cur_term->_cursorstate != 1)
1347c478bd9Sstevel@tonic-gate 				_PUTS(cur_term->cursor_seq[cur_term->
1357c478bd9Sstevel@tonic-gate 				    _cursorstate], 0);
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 		_PUTS(enter_ca_mode, 1);
1387c478bd9Sstevel@tonic-gate 		(void) tputs(ena_acs, 1, _outch);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 		if (exit_attribute_mode)
1417c478bd9Sstevel@tonic-gate 			_PUTS(tparm_p0(exit_attribute_mode), 1);
1427c478bd9Sstevel@tonic-gate 		else
1437c478bd9Sstevel@tonic-gate 			/*
1447c478bd9Sstevel@tonic-gate 			 * If there is no exit_attribute mode, then vidupdate
1457c478bd9Sstevel@tonic-gate 			 * could only possibly turn off one of the below three
1467c478bd9Sstevel@tonic-gate 			 * so that's all we ask it turn off.
147*e07d85f8SToomas Soome 			 */
1487c478bd9Sstevel@tonic-gate 			vidupdate(A_NORMAL, (A_ALTCHARSET | A_STANDOUT |
1497c478bd9Sstevel@tonic-gate 			    A_UNDERLINE), _outch);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		SP->fl_endwin = FALSE;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate #ifdef	_VR2_COMPAT_CODE
154*e07d85f8SToomas Soome 		_endwin = (char)FALSE;
1557c478bd9Sstevel@tonic-gate #endif	/* _VR2_COMPAT_CODE */
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/* clear the screen if required */
1597c478bd9Sstevel@tonic-gate 	if (_virtscr->_clear) {
1607c478bd9Sstevel@tonic-gate /* SS: colors */
1617c478bd9Sstevel@tonic-gate 		if (back_color_erase)
1627c478bd9Sstevel@tonic-gate 			_turn_off_background();
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		_PUTS(clear_screen, scrli);
1657c478bd9Sstevel@tonic-gate 		cy = cx = curscr->_curx = curscr->_cury = 0;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		/* _sync indicates that this a new screen */
1687c478bd9Sstevel@tonic-gate 		if (!curscr->_sync)
1697c478bd9Sstevel@tonic-gate 			(void) werase(curscr);
1707c478bd9Sstevel@tonic-gate 		else {
1717c478bd9Sstevel@tonic-gate 			nc = scrco / BITSPERBYTE - (scrco %
1727c478bd9Sstevel@tonic-gate 			    BITSPERBYTE ? 0 : 1);
1737c478bd9Sstevel@tonic-gate 			wy = scrli - 1;
1747c478bd9Sstevel@tonic-gate 			bnsch = _BEGNS; ensch = _ENDNS;
1757c478bd9Sstevel@tonic-gate 			hs = _CURHASH;
1767c478bd9Sstevel@tonic-gate 			for (; wy >= 0; --wy) {
1777c478bd9Sstevel@tonic-gate 				*bnsch++ = scrco;
1787c478bd9Sstevel@tonic-gate 				*ensch++ = -1;
1797c478bd9Sstevel@tonic-gate 				*hs++ = 0;
1807c478bd9Sstevel@tonic-gate 				if (marks)
1817c478bd9Sstevel@tonic-gate 					for (wx = nc; wx >= 0; --wx)
1827c478bd9Sstevel@tonic-gate 						marks[wy][wx] = 0;
1837c478bd9Sstevel@tonic-gate 			}
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		_virtscr->_clear = curscr->_sync = curscr->_clear = FALSE;
1877c478bd9Sstevel@tonic-gate 		if (slk)
1887c478bd9Sstevel@tonic-gate 			(*_do_slk_tch)();
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		/* pretend _virtscr has been totally changed */
1917c478bd9Sstevel@tonic-gate 		(void) wtouchln(_virtscr, 0, scrli, -1);
1927c478bd9Sstevel@tonic-gate 		_VIRTTOP = 0;
1937c478bd9Sstevel@tonic-gate 		_VIRTBOT = scrli - 1;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		/* will not do clear-eod or ins/del lines */
1967c478bd9Sstevel@tonic-gate 		clby = idby = scrli;
1977c478bd9Sstevel@tonic-gate 	} else
1987c478bd9Sstevel@tonic-gate 		clby = idby = -1;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/* Software soft labels; if _changed == 2, slk's are in clear mode. */
2017c478bd9Sstevel@tonic-gate 	if (slk && slk->_win && (slk->_changed == TRUE))
2027c478bd9Sstevel@tonic-gate 		(*_do_slk_noref)();
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/* do line updating */
2057c478bd9Sstevel@tonic-gate 	_virtscr->_clear = FALSE;
2067c478bd9Sstevel@tonic-gate 	wy = _VIRTTOP;
2077c478bd9Sstevel@tonic-gate 	boty = _VIRTBOT + 1;
2087c478bd9Sstevel@tonic-gate 	bnsch = _virtscr->_firstch + wy;
2097c478bd9Sstevel@tonic-gate 	ensch = _virtscr->_lastch + wy;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	for (; wy < boty; ++wy, ++bnsch, ++ensch) {
2127c478bd9Sstevel@tonic-gate 		/* this line is up-to-date */
2137c478bd9Sstevel@tonic-gate 		if (*bnsch >= scrco)
2147c478bd9Sstevel@tonic-gate 			goto next;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		/* there is type-ahead */
2177c478bd9Sstevel@tonic-gate 		if (!curwin && (_INPUTPENDING = _chkinput()) == TRUE) {
2187c478bd9Sstevel@tonic-gate 			/* LINTED */
219*e07d85f8SToomas Soome 			_VIRTTOP = (short)wy;
2207c478bd9Sstevel@tonic-gate 			goto done;
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		if (clby < 0) {
2247c478bd9Sstevel@tonic-gate 			/* now we have to work, check for ceod */
2257c478bd9Sstevel@tonic-gate 			clby = _getceod(wy, boty);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 			/* check for insert/delete lines */
2287c478bd9Sstevel@tonic-gate 			if (_virtscr->_use_idl)
2297c478bd9Sstevel@tonic-gate 				idby = (*_setidln)();
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		/* try clear-to-eod */
2337c478bd9Sstevel@tonic-gate 		if (wy == clby)
2347c478bd9Sstevel@tonic-gate 			_useceod(wy, boty);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		/* try ins/del lines */
2377c478bd9Sstevel@tonic-gate 		if (wy == idby) {
2387c478bd9Sstevel@tonic-gate 			curscr->_cury = cy;
2397c478bd9Sstevel@tonic-gate 			curscr->_curx = cx;
2407c478bd9Sstevel@tonic-gate 			(*_useidln)();
2417c478bd9Sstevel@tonic-gate 			cy = curscr->_cury;
2427c478bd9Sstevel@tonic-gate 			cx = curscr->_curx;
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		if (*bnsch < scrco)
2467c478bd9Sstevel@tonic-gate 			_updateln(wy);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate next:
2497c478bd9Sstevel@tonic-gate 		*bnsch = _INFINITY;
2507c478bd9Sstevel@tonic-gate 		*ensch = -1;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/* do hardware soft labels; if _changed == 2, */
2547c478bd9Sstevel@tonic-gate 	/* slk's are in clear mode. */
2557c478bd9Sstevel@tonic-gate 	if (slk && (slk->_changed == TRUE) && !(slk->_win))
2567c478bd9Sstevel@tonic-gate 		(*_do_slk_ref)();
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/* move cursor */
2597c478bd9Sstevel@tonic-gate 	wy = _virtscr->_cury;
2607c478bd9Sstevel@tonic-gate 	wx = _virtscr->_curx;
2617c478bd9Sstevel@tonic-gate 	if (wy != cy || wx != cx) {
2627c478bd9Sstevel@tonic-gate 		(void) mvcur(cy, cx, wy, wx);
2637c478bd9Sstevel@tonic-gate 		/* LINTED */
264*e07d85f8SToomas Soome 		cy = (short)wy;
2657c478bd9Sstevel@tonic-gate 		/* LINTED */
266*e07d85f8SToomas Soome 		cx = (short)wx;
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	/* reset the flags */
2707c478bd9Sstevel@tonic-gate 	curscr->_clear = FALSE;
2717c478bd9Sstevel@tonic-gate 	_virtscr->_use_idl = FALSE;
2727c478bd9Sstevel@tonic-gate 	_virtscr->_use_idc = TRUE;
2737c478bd9Sstevel@tonic-gate 	_INPUTPENDING = FALSE;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/* virtual image is now up-to-date */
2767c478bd9Sstevel@tonic-gate 	_VIRTTOP = scrli;
2777c478bd9Sstevel@tonic-gate 	_VIRTBOT = -1;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate done :
2807c478bd9Sstevel@tonic-gate 	curscr->_cury = cy;
2817c478bd9Sstevel@tonic-gate 	curscr->_curx = cx;
2827c478bd9Sstevel@tonic-gate 	(void) fflush(SP->term_file);
2837c478bd9Sstevel@tonic-gate 	return (outchcount);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /* Shift appropriate portions of a line to leave space for cookies. */
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static	chtype	*
_shove(int wy)2897c478bd9Sstevel@tonic-gate _shove(int wy)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate 	chtype		*wcp, *cp, prev;
2927c478bd9Sstevel@tonic-gate 	short		curx;
2937c478bd9Sstevel@tonic-gate 	int		x, cury, didshift;
2947c478bd9Sstevel@tonic-gate 	static	chtype	*line;
2957c478bd9Sstevel@tonic-gate 	static	int	length;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/* allocate space for shifted line */
2987c478bd9Sstevel@tonic-gate 	if (length < scrco) {
299*e07d85f8SToomas Soome 		free(line);
3007c478bd9Sstevel@tonic-gate 		line = (chtype *) malloc(scrco * sizeof (chtype));
3017c478bd9Sstevel@tonic-gate 		length = line ? scrco : 0;
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* no space to do it */
3057c478bd9Sstevel@tonic-gate 	if (!line)
3067c478bd9Sstevel@tonic-gate 		return (_virtscr->_y[wy]);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	prev = A_NORMAL;
3097c478bd9Sstevel@tonic-gate 	cp = line;
3107c478bd9Sstevel@tonic-gate 	wcp = _virtscr->_y[wy];
3117c478bd9Sstevel@tonic-gate 	curx = _virtscr->_curx;
3127c478bd9Sstevel@tonic-gate 	cury = _virtscr->_cury;
3137c478bd9Sstevel@tonic-gate 	didshift = FALSE;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	for (x = 0; x < scrco; ++x, ++wcp, ++cp) {
3167c478bd9Sstevel@tonic-gate 		if (_ATTR(*wcp) != prev) {
3177c478bd9Sstevel@tonic-gate 			/* use existing blank */
3187c478bd9Sstevel@tonic-gate 			if (_CHAR(*wcp) == ' ')
3197c478bd9Sstevel@tonic-gate 				*cp = ' ' | _ATTR(*(wcp + 1));
3207c478bd9Sstevel@tonic-gate 				/* use previous blank */
3217c478bd9Sstevel@tonic-gate 			else
3227c478bd9Sstevel@tonic-gate 				if ((x > 0) && _CHAR(*(cp - 1)) == ' ') {
3237c478bd9Sstevel@tonic-gate 					*(cp - 1) = ' ' | _ATTR(*wcp);
3247c478bd9Sstevel@tonic-gate 					*cp = *wcp;
3257c478bd9Sstevel@tonic-gate 				} else {
3267c478bd9Sstevel@tonic-gate 					if ((curx >= x) && (cury == wy))
3277c478bd9Sstevel@tonic-gate 						++curx;
3287c478bd9Sstevel@tonic-gate 					*cp = ' ' | _ATTR(*wcp);
3297c478bd9Sstevel@tonic-gate 					--wcp;
3307c478bd9Sstevel@tonic-gate 					didshift = TRUE;
3317c478bd9Sstevel@tonic-gate 				}
3327c478bd9Sstevel@tonic-gate 			prev = _ATTR(*cp);
3337c478bd9Sstevel@tonic-gate 		} else
3347c478bd9Sstevel@tonic-gate 			*cp = *wcp;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/* make sure that the end of the line is normal */
3387c478bd9Sstevel@tonic-gate 	cp = line + scrco - 1;
3397c478bd9Sstevel@tonic-gate 	if (didshift || (_ATTR(*cp) != A_NORMAL) ||
3407c478bd9Sstevel@tonic-gate 	    ((wy == scrli - 1) && (_ATTR(*(cp - 1)) != A_NORMAL))) {
3417c478bd9Sstevel@tonic-gate 		*cp = didshift ? ' ' : _CHAR(*cp);
3427c478bd9Sstevel@tonic-gate 		if (wy == scrli - 1)
3437c478bd9Sstevel@tonic-gate 			*(cp - 1) = didshift ? ' ' : _CHAR(*(cp - 1));
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (wy == cury)
3477c478bd9Sstevel@tonic-gate 		_virtscr->_curx = curx >= scrco ? scrco - 1 : curx;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	return (line);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate  * Update a line.
3547c478bd9Sstevel@tonic-gate  * Three schemes of coloring are allowed. The first is the usual
3557c478bd9Sstevel@tonic-gate  * pen-up/pen-down model. The second is the HP26*-like model.
3567c478bd9Sstevel@tonic-gate  * In this case, colorings are specified by intervals, the left
3577c478bd9Sstevel@tonic-gate  * side of the interval has the coloring mark, the right side
3587c478bd9Sstevel@tonic-gate  * has the end-coloring mark. We assume that clear sequences will
3597c478bd9Sstevel@tonic-gate  * clear ALL marks in the affected regions. The second case is
3607c478bd9Sstevel@tonic-gate  * signified by the boolean flag ceol_standout_glitch.
3617c478bd9Sstevel@tonic-gate  * The third case is for terminals that leave visible cookies on
3627c478bd9Sstevel@tonic-gate  * the screen. This last case is at most an approximation of what
3637c478bd9Sstevel@tonic-gate  * can be done right.
3647c478bd9Sstevel@tonic-gate  */
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate static	void
_updateln(int wy)3677c478bd9Sstevel@tonic-gate _updateln(int wy)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	chtype	*wcp, *scp, *wp, *sp, wc, sc;
3707c478bd9Sstevel@tonic-gate 	int	wx, lastx, x, mtch, idch, blnkx, idcx, video_attrx,
371*e07d85f8SToomas Soome 	    color_attrx, maxi, endns, begns, wx_sav, multi_col;
3727c478bd9Sstevel@tonic-gate 	bool	redraw, changed, didcolor, didvideo;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	redraw = (_virtscr->_firstch[wy] == _REDRAW);
3757c478bd9Sstevel@tonic-gate 	endns = _ENDNS[wy];
3767c478bd9Sstevel@tonic-gate 	begns = _BEGNS[wy];
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/* easy case */
3797c478bd9Sstevel@tonic-gate 	if (!redraw && (_virtscr->_lastch[wy] == _BLANK) && (begns >= scrco))
3807c478bd9Sstevel@tonic-gate 		return;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/* line images */
3837c478bd9Sstevel@tonic-gate 	wcp = magic_cookie_glitch <= 0 ? _virtscr->_y[wy] : _shove(wy);
3847c478bd9Sstevel@tonic-gate 	scp = curscr->_y[wy];
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/* the interval to be updated */
3877c478bd9Sstevel@tonic-gate 	if (redraw || magic_cookie_glitch >= 0) {
3887c478bd9Sstevel@tonic-gate 		wx = 0;
3897c478bd9Sstevel@tonic-gate 		lastx = scrco;
3907c478bd9Sstevel@tonic-gate 	} else {
3917c478bd9Sstevel@tonic-gate 		wx = _virtscr->_firstch[wy];
3927c478bd9Sstevel@tonic-gate 		lastx = _virtscr->_lastch[wy] == _BLANK ? scrco :
3937c478bd9Sstevel@tonic-gate 		    _virtscr->_lastch[wy] + 1;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/* skip equal parts */
3977c478bd9Sstevel@tonic-gate 	if (!redraw) {
3987c478bd9Sstevel@tonic-gate 		/* skip the starting equal part */
3997c478bd9Sstevel@tonic-gate 		wp = wcp + wx;
4007c478bd9Sstevel@tonic-gate 		sp = scp + wx;
4017c478bd9Sstevel@tonic-gate 		for (; wx < lastx; ++wx)
4027c478bd9Sstevel@tonic-gate 			if (*wp++ != *sp++)
4037c478bd9Sstevel@tonic-gate 				break;
4047c478bd9Sstevel@tonic-gate 		if (wx >= lastx)
4057c478bd9Sstevel@tonic-gate 			return;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		/* start update at an entire character */
4087c478bd9Sstevel@tonic-gate 		for (sp = scp+wx, wp = wcp+wx; wp > wcp; --wp, --sp, --wx)
4097c478bd9Sstevel@tonic-gate 			if (!ISCBIT(*wp) && !ISCBIT(*sp))
4107c478bd9Sstevel@tonic-gate 				break;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		/* skip the ending equal part */
4137c478bd9Sstevel@tonic-gate 		wp = wcp + lastx - 1;
4147c478bd9Sstevel@tonic-gate 		sp = scp + lastx - 1;
4157c478bd9Sstevel@tonic-gate 		for (; lastx > wx; --lastx)
4167c478bd9Sstevel@tonic-gate 			if (*wp-- != *sp--)
4177c478bd9Sstevel@tonic-gate 				break;
4187c478bd9Sstevel@tonic-gate 		++wp;
4197c478bd9Sstevel@tonic-gate 		++wp;
4207c478bd9Sstevel@tonic-gate 		++sp;
4217c478bd9Sstevel@tonic-gate 		++sp;
4227c478bd9Sstevel@tonic-gate 		for (; lastx < scrco; ++wp, ++sp, ++lastx)
4237c478bd9Sstevel@tonic-gate 			if (!ISCBIT(*wp) && !ISCBIT(*sp))
4247c478bd9Sstevel@tonic-gate 				break;
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/* place to do clear-eol */
4287c478bd9Sstevel@tonic-gate 	if (!clr_eol || endns >= lastx)
4297c478bd9Sstevel@tonic-gate 		blnkx = scrco;
4307c478bd9Sstevel@tonic-gate 	else
4317c478bd9Sstevel@tonic-gate 		if (_virtscr->_lastch[wy] == _BLANK)
4327c478bd9Sstevel@tonic-gate 			blnkx = -1;
4337c478bd9Sstevel@tonic-gate 		else {
4347c478bd9Sstevel@tonic-gate 			for (blnkx = lastx - 1, wp = wcp + blnkx;
4357c478bd9Sstevel@tonic-gate 			    blnkx >= wx; --blnkx, --wp)
4367c478bd9Sstevel@tonic-gate 				if (_DARKCHAR(*wp))
4377c478bd9Sstevel@tonic-gate 					break;
4387c478bd9Sstevel@tonic-gate 			for (sp = scp + blnkx + 1; blnkx < scrco - 1;
4397c478bd9Sstevel@tonic-gate 			    ++sp, ++blnkx)
4407c478bd9Sstevel@tonic-gate 				if (!ISCBIT(*sp))
4417c478bd9Sstevel@tonic-gate 					break;
4427c478bd9Sstevel@tonic-gate 			if (blnkx + _COST(Clr_eol) >= lastx)
4437c478bd9Sstevel@tonic-gate 				blnkx = scrco;
4447c478bd9Sstevel@tonic-gate 		}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/* on cookie terminals, we may need to do more work */
4477c478bd9Sstevel@tonic-gate 	if (marks) {
4487c478bd9Sstevel@tonic-gate 		/* video_attrx = color_attrx = scrco; */
4497c478bd9Sstevel@tonic-gate 		video_attrx = color_attrx = (lastx >= scrco) ? lastx - 1 :
4507c478bd9Sstevel@tonic-gate 		    lastx;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		/* find the last video attribute on the line	*/
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		wp = wcp + video_attrx;
4557c478bd9Sstevel@tonic-gate 		for (; video_attrx >= wx; --video_attrx, --wp)
4567c478bd9Sstevel@tonic-gate 			if (_VIDEO(*wp) != A_NORMAL)
4577c478bd9Sstevel@tonic-gate 				break;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		/* find the last color attribute on the line	*/
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		if (color_marks) {
4627c478bd9Sstevel@tonic-gate 			wp = wcp + color_attrx;
4637c478bd9Sstevel@tonic-gate 			for (; color_attrx >= wx; --color_attrx, --wp)
4647c478bd9Sstevel@tonic-gate 				if (_COLOR(*wp) != A_NORMAL)
4657c478bd9Sstevel@tonic-gate 					break;
4667c478bd9Sstevel@tonic-gate 			if (color_attrx < lastx)
4677c478bd9Sstevel@tonic-gate 				color_attrx++;
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 		if (video_attrx < lastx)
4707c478bd9Sstevel@tonic-gate 			video_attrx++;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		if (video_attrx >= scrco)
4737c478bd9Sstevel@tonic-gate 			--video_attrx;
4747c478bd9Sstevel@tonic-gate 		if (color_marks && color_attrx >= scrco)
4757c478bd9Sstevel@tonic-gate 			--color_attrx;
4767c478bd9Sstevel@tonic-gate 		if (magic_cookie_glitch > 0 && wy == scrli - 1 &&
4777c478bd9Sstevel@tonic-gate 		    video_attrx == scrco - 1)
4787c478bd9Sstevel@tonic-gate 			--video_attrx;
4797c478bd9Sstevel@tonic-gate 		if (color_marks && magic_cookie_glitch > 0 &&
4807c478bd9Sstevel@tonic-gate 		    wy == scrli - 1 && color_attrx == scrco - 1)
4817c478bd9Sstevel@tonic-gate 			--color_attrx;
4827c478bd9Sstevel@tonic-gate 		for (wp = wcp+video_attrx; wp >= wcp+wx; --wp)
4837c478bd9Sstevel@tonic-gate 			if (!ISCBIT(*wp))
4847c478bd9Sstevel@tonic-gate 				break;
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/* place for insert/delete chars */
4887c478bd9Sstevel@tonic-gate #define	SLACK	4
4897c478bd9Sstevel@tonic-gate 	if (redraw || (!SP->dchok && !SP->ichok) || !(_virtscr->_use_idc) ||
4907c478bd9Sstevel@tonic-gate 	    endns < wx || (endns >= lastx && (scrco - lastx) > SLACK)) {
4917c478bd9Sstevel@tonic-gate 		idcx = scrco;
4927c478bd9Sstevel@tonic-gate 	} else
4937c478bd9Sstevel@tonic-gate 		if (!marks)
4947c478bd9Sstevel@tonic-gate 			idcx = -1;
4957c478bd9Sstevel@tonic-gate 		else {
4967c478bd9Sstevel@tonic-gate 			/* on cookie term, only do idch where no attrs */
4977c478bd9Sstevel@tonic-gate 			/* are used */
4987c478bd9Sstevel@tonic-gate 			for (idcx = scrco - 1, wp = wcp + idcx; idcx >= wx;
4997c478bd9Sstevel@tonic-gate 			    --idcx, --wp)
5007c478bd9Sstevel@tonic-gate 				if (_ATTR(*wp) || _ISMARK1(wy, idcx) ||
5017c478bd9Sstevel@tonic-gate 				    _ISMARK2(wy, idcx))
5027c478bd9Sstevel@tonic-gate 					break;
5037c478bd9Sstevel@tonic-gate 			if (idcx >= scrco - SLACK)
5047c478bd9Sstevel@tonic-gate 				idcx = scrco;
5057c478bd9Sstevel@tonic-gate 		}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	if (idcx < lastx && endns >= lastx)
5087c478bd9Sstevel@tonic-gate 		lastx = scrco;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/* max amount of insert allow */
5117c478bd9Sstevel@tonic-gate 	if (idcx == scrco || !SP->ichok)
5127c478bd9Sstevel@tonic-gate 		maxi = 0;
5137c478bd9Sstevel@tonic-gate 	else
5147c478bd9Sstevel@tonic-gate 		if (lastx == scrco)
5157c478bd9Sstevel@tonic-gate 			maxi = scrco;
5167c478bd9Sstevel@tonic-gate 		else
5177c478bd9Sstevel@tonic-gate 			maxi = lastx - (endns + 1);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/* go */
5207c478bd9Sstevel@tonic-gate 	wcp += wx;
5217c478bd9Sstevel@tonic-gate 	scp += wx;
5227c478bd9Sstevel@tonic-gate 	didvideo = changed = FALSE;
5237c478bd9Sstevel@tonic-gate 	didcolor = (color_marks) ? FALSE : TRUE;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	while (wx < lastx) {
5267c478bd9Sstevel@tonic-gate 		/* skip things that are already right */
5277c478bd9Sstevel@tonic-gate 		if (!redraw) {
5287c478bd9Sstevel@tonic-gate 			multi_col = 0;
5297c478bd9Sstevel@tonic-gate 			wx_sav = wx;
5307c478bd9Sstevel@tonic-gate 			for (; wx < lastx; ++wx, ++wcp, ++scp)
5317c478bd9Sstevel@tonic-gate 				if (*wcp != *scp)
5327c478bd9Sstevel@tonic-gate 					break;
5337c478bd9Sstevel@tonic-gate 			if (wx >= lastx)
5347c478bd9Sstevel@tonic-gate 				goto done;
5357c478bd9Sstevel@tonic-gate 			for (; wx > wx_sav; --wx, --wcp, --scp) {
5367c478bd9Sstevel@tonic-gate 				if (!ISCBIT(*wcp) && !ISCBIT(*scp))
5377c478bd9Sstevel@tonic-gate 					break;
5387c478bd9Sstevel@tonic-gate 				multi_col = 1;
5397c478bd9Sstevel@tonic-gate 			}
5407c478bd9Sstevel@tonic-gate 		}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		/* try clear-bol, we'll assume exclusive clr_bol */
5437c478bd9Sstevel@tonic-gate 		if (!changed && !marks && clr_bol && blnkx > wx &&
5447c478bd9Sstevel@tonic-gate 		    begns >= wx) {
5457c478bd9Sstevel@tonic-gate 			for (x = wx, wp = wcp; x < lastx; ++x, ++wp)
5467c478bd9Sstevel@tonic-gate 				if (_DARKCHAR(*wp))
5477c478bd9Sstevel@tonic-gate 					break;
5487c478bd9Sstevel@tonic-gate 			/* clearing only whole screen characters */
5497c478bd9Sstevel@tonic-gate 			for (sp = scp+(x-wx); x >= wx; --x, --sp)
5507c478bd9Sstevel@tonic-gate 				if (!ISCBIT(*sp))
5517c478bd9Sstevel@tonic-gate 					break;
5527c478bd9Sstevel@tonic-gate 			x -= 1;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 			if ((x - (redraw ? 0 : begns)) > _COST(Clr_bol)) {
5557c478bd9Sstevel@tonic-gate 				(void) mvcur(cy, cx, wy, x);
5567c478bd9Sstevel@tonic-gate 				/* MORE?: colors - mvcur will shuts of */
5577c478bd9Sstevel@tonic-gate 				/* colors when msgr is not defined */
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate /* SS: colors */
5607c478bd9Sstevel@tonic-gate 				if (back_color_erase)
5617c478bd9Sstevel@tonic-gate 					_turn_off_background();
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 				_PUTS(clr_bol, 1);
5647c478bd9Sstevel@tonic-gate 				/* LINTED */
565*e07d85f8SToomas Soome 				cy = (short)wy;
5667c478bd9Sstevel@tonic-gate 				/* LINTED */
567*e07d85f8SToomas Soome 				cx = (short)x;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 				mtch = x - wx;
570*e07d85f8SToomas Soome 				(void) memcpy(scp, wcp,
5717c478bd9Sstevel@tonic-gate 				    (mtch * sizeof (chtype)));
5727c478bd9Sstevel@tonic-gate 				wcp += mtch;
5737c478bd9Sstevel@tonic-gate 				scp += mtch;
5747c478bd9Sstevel@tonic-gate 				wx = x;
5757c478bd9Sstevel@tonic-gate 			}
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 		/* screen image is changing */
5797c478bd9Sstevel@tonic-gate 		changed = TRUE;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		/* move to the point to start refresh */
5827c478bd9Sstevel@tonic-gate 		if (cy != wy || cx != wx)
5837c478bd9Sstevel@tonic-gate 			(void) mvcur(cy, cx, wy, wx);
5847c478bd9Sstevel@tonic-gate 		/* LINTED */
585*e07d85f8SToomas Soome 		cy = (short)wy;
5867c478bd9Sstevel@tonic-gate 		/* LINTED */
587*e07d85f8SToomas Soome 		cx = (short)wx;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		/* update screen image */
5907c478bd9Sstevel@tonic-gate 		while (wx < lastx) {
5917c478bd9Sstevel@tonic-gate 			wc = *wcp;
5927c478bd9Sstevel@tonic-gate 			sc = *scp;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 			if (!redraw && !multi_col && wc == sc)
5957c478bd9Sstevel@tonic-gate 				break;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 			/* real video attributes */
5987c478bd9Sstevel@tonic-gate 			if (marks)
5997c478bd9Sstevel@tonic-gate 				curscr->_attrs = _ATTR(sc);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 			/* blanks only */
6027c478bd9Sstevel@tonic-gate 			if (wx > blnkx) {
6037c478bd9Sstevel@tonic-gate /* SS: colors */
6047c478bd9Sstevel@tonic-gate 				if (back_color_erase)
6057c478bd9Sstevel@tonic-gate 					_turn_off_background();
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 				_PUTS(clr_eol, 1);
6087c478bd9Sstevel@tonic-gate 				/* LINTED */
609*e07d85f8SToomas Soome 				curscr->_curx = (short)wx;
610*e07d85f8SToomas Soome 				/* LINTED */
611*e07d85f8SToomas Soome 				curscr->_cury = (short)wy;
6127c478bd9Sstevel@tonic-gate 				(void) wclrtoeol(curscr);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 				if (marks && wx > 0 && _ATTR(*(scp - 1)) !=
6157c478bd9Sstevel@tonic-gate 				    A_NORMAL) {
6167c478bd9Sstevel@tonic-gate 					_VIDS(A_NORMAL, _ATTR(*(scp - 1)));
6177c478bd9Sstevel@tonic-gate 					if (_VIDEO(*scp - 1))
6187c478bd9Sstevel@tonic-gate 						_setmark1(wy, wx, NULL);
6197c478bd9Sstevel@tonic-gate 					if (_COLOR(*scp - 1))
6207c478bd9Sstevel@tonic-gate 						_setmark2(wy, wx, NULL);
6217c478bd9Sstevel@tonic-gate 				}
6227c478bd9Sstevel@tonic-gate 				goto done;
6237c478bd9Sstevel@tonic-gate 			}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 			/* try insert/delete chars */
6267c478bd9Sstevel@tonic-gate 			if (wx > idcx && !ISCBIT(*scp) &&
6277c478bd9Sstevel@tonic-gate 			    (mtch = _useidch(wcp, scp, lastx - wx,
6287c478bd9Sstevel@tonic-gate 			    maxi, &idch))) {
6297c478bd9Sstevel@tonic-gate 				maxi -= idch;
6307c478bd9Sstevel@tonic-gate 				wx += mtch;
6317c478bd9Sstevel@tonic-gate 				scp += mtch;
6327c478bd9Sstevel@tonic-gate 				wcp += mtch;
6337c478bd9Sstevel@tonic-gate 				break;
6347c478bd9Sstevel@tonic-gate 			}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 			/* about to output chars, make sure insert */
6377c478bd9Sstevel@tonic-gate 			/* mode is off */
6387c478bd9Sstevel@tonic-gate 			if (SP->phys_irm)
6397c478bd9Sstevel@tonic-gate 				_OFFINSERT();
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 			/* color and video attributes */
6427c478bd9Sstevel@tonic-gate 			if (_ATTR(wc) != curscr->_attrs) {
643*e07d85f8SToomas Soome 				bool  color_change = FALSE;
644*e07d85f8SToomas Soome 				bool  video_change = FALSE;
645*e07d85f8SToomas Soome 
646*e07d85f8SToomas Soome 				if (marks) {
647*e07d85f8SToomas Soome 					if (_VIDEO(wc) !=
648*e07d85f8SToomas Soome 					    _VIDEO(curscr->_attrs)) {
649*e07d85f8SToomas Soome 						video_change = TRUE;
650*e07d85f8SToomas Soome 					}
651*e07d85f8SToomas Soome 				}
652*e07d85f8SToomas Soome 				if (color_marks) {
653*e07d85f8SToomas Soome 					if (_COLOR(wc) !=
654*e07d85f8SToomas Soome 					    _COLOR(curscr->_attrs)) {
655*e07d85f8SToomas Soome 						color_change = TRUE;
656*e07d85f8SToomas Soome 					}
657*e07d85f8SToomas Soome 				}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 				/* the following may occurs when, for */
6607c478bd9Sstevel@tonic-gate 				/* example the application */
6617c478bd9Sstevel@tonic-gate 				/* is written for color terminal and then */
6627c478bd9Sstevel@tonic-gate 				/* run on a monocrome  */
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 				if (marks && !video_change && !color_change)
6657c478bd9Sstevel@tonic-gate 					goto no_change;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 				/* prevent spilling out of line */
6687c478bd9Sstevel@tonic-gate 				if (marks && !(didcolor && didvideo)) {
6697c478bd9Sstevel@tonic-gate 				    if ((video_change && !_ISMARK1(wy,
6707c478bd9Sstevel@tonic-gate 					video_attrx)) || (color_change &&
6717c478bd9Sstevel@tonic-gate 					!_ISMARK2(wy, color_attrx))) {
6727c478bd9Sstevel@tonic-gate 					    int    tempx;
6737c478bd9Sstevel@tonic-gate 					    chtype sa = curscr->_attrs;
6747c478bd9Sstevel@tonic-gate 					    bool   first  = FALSE;
6757c478bd9Sstevel@tonic-gate 					    bool   second = FALSE;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 					    if (!didvideo && video_change &&
6787c478bd9Sstevel@tonic-gate 						!_ISMARK1(wy, video_attrx)) {
6797c478bd9Sstevel@tonic-gate 						didvideo = TRUE;
6807c478bd9Sstevel@tonic-gate 						(void) mvcur(wy, wx,
6817c478bd9Sstevel@tonic-gate 						    wy, video_attrx);
6827c478bd9Sstevel@tonic-gate 						_VIDS(_VIDEO(_virtscr->_y[wy]
6837c478bd9Sstevel@tonic-gate 						    [video_attrx]),
6847c478bd9Sstevel@tonic-gate 						    _VIDEO(_virtscr->_y[wy]
6857c478bd9Sstevel@tonic-gate 						    [video_attrx-1]));
6867c478bd9Sstevel@tonic-gate 						_setmark1(wy, video_attrx,
6877c478bd9Sstevel@tonic-gate 						    NULL);
6887c478bd9Sstevel@tonic-gate 						first = TRUE;
6897c478bd9Sstevel@tonic-gate 					    }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 				if (!didcolor && color_change &&
6927c478bd9Sstevel@tonic-gate 				    !_ISMARK2(wy, color_attrx)) {
6937c478bd9Sstevel@tonic-gate 					didcolor = TRUE;
6947c478bd9Sstevel@tonic-gate 					tempx = first ? video_attrx : wx;
6957c478bd9Sstevel@tonic-gate 					if (tempx != color_attrx)
6967c478bd9Sstevel@tonic-gate 						(void) mvcur(wy, tempx, wy,
6977c478bd9Sstevel@tonic-gate 						    color_attrx);
6987c478bd9Sstevel@tonic-gate 				/*
6997c478bd9Sstevel@tonic-gate 				 * sc = _COLOR(curscr->_y[wy][color_attrx]);
700*e07d85f8SToomas Soome 				 * _VIDS(sc, (~sc & A_COLOR));
701*e07d85f8SToomas Soome 				 */
7027c478bd9Sstevel@tonic-gate 					_VIDS(_COLOR(_virtscr->_y[wy]
7037c478bd9Sstevel@tonic-gate 					    [color_attrx]),
7047c478bd9Sstevel@tonic-gate 					    _COLOR(_virtscr->_y[wy]
7057c478bd9Sstevel@tonic-gate 					    [color_attrx-1]));
7067c478bd9Sstevel@tonic-gate 					_setmark2(wy, color_attrx, NULL);
7077c478bd9Sstevel@tonic-gate 					second = TRUE;
7087c478bd9Sstevel@tonic-gate 				}
7097c478bd9Sstevel@tonic-gate 				(void) mvcur(wy, (second ? color_attrx :
7107c478bd9Sstevel@tonic-gate 				    video_attrx), wy, wx);
7117c478bd9Sstevel@tonic-gate 				curscr->_attrs = sa;
7127c478bd9Sstevel@tonic-gate 			    }
7137c478bd9Sstevel@tonic-gate 			}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 			_VIDS(_ATTR(wc), curscr->_attrs);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 			/* on cookie terminals mark the interval */
7187c478bd9Sstevel@tonic-gate 			if (video_change)
7197c478bd9Sstevel@tonic-gate 				_setmark1(wy, wx, scp);
7207c478bd9Sstevel@tonic-gate 			if (color_change)
7217c478bd9Sstevel@tonic-gate 				_setmark2(wy, wx, scp);
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		/* end-of-line */
7257c478bd9Sstevel@tonic-gate no_change:
7267c478bd9Sstevel@tonic-gate 		x = 1;
7277c478bd9Sstevel@tonic-gate 		if (_scrmax > 1)
7287c478bd9Sstevel@tonic-gate 			x = _curs_scrwidth[TYPE(RBYTE(wc))];
7297c478bd9Sstevel@tonic-gate 		if (wx == scrco - x) {
7307c478bd9Sstevel@tonic-gate 			_rmargin(wx);
7317c478bd9Sstevel@tonic-gate 			goto done;
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 		if (transparent_underline && erase_overstrike &&
7357c478bd9Sstevel@tonic-gate 		    _CHAR(wc) == '_') {
7367c478bd9Sstevel@tonic-gate 			(void) _outch(' ');
7377c478bd9Sstevel@tonic-gate 			(void) mvcur(wy, wx + 1, wy, wx);
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 		/* put out the character */
7417c478bd9Sstevel@tonic-gate 		(void) _outwch(tilde_glitch && _CHAR(wc) == '~' ? '`' : wc);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 		*scp++ = wc;
7447c478bd9Sstevel@tonic-gate 		wcp++;
7457c478bd9Sstevel@tonic-gate 		wx++;
7467c478bd9Sstevel@tonic-gate 		cx++;
7477c478bd9Sstevel@tonic-gate 		/* output entire multi-byte chars */
7487c478bd9Sstevel@tonic-gate 		while (wx < lastx && ISCBIT(*wcp)) {
7497c478bd9Sstevel@tonic-gate 			(void) _outwch(*wcp);
7507c478bd9Sstevel@tonic-gate 			*scp++ = *wcp++;
7517c478bd9Sstevel@tonic-gate 			wx++;
7527c478bd9Sstevel@tonic-gate 			cx++;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 		}
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate done:
7627c478bd9Sstevel@tonic-gate 	if (changed) {
7637c478bd9Sstevel@tonic-gate 		/* update the blank structure */
7647c478bd9Sstevel@tonic-gate 		for (wx = 0, scp = curscr->_y[wy]; wx < scrco; ++wx, ++scp)
7657c478bd9Sstevel@tonic-gate 			if (_DARKCHAR(*scp))
7667c478bd9Sstevel@tonic-gate 				break;
7677c478bd9Sstevel@tonic-gate 		/* LINTED */
768*e07d85f8SToomas Soome 		_BEGNS[wy] = (short)wx;
7697c478bd9Sstevel@tonic-gate 		if (wx == scrco)
7707c478bd9Sstevel@tonic-gate 			_ENDNS[wy] = -1;
7717c478bd9Sstevel@tonic-gate 		else {
7727c478bd9Sstevel@tonic-gate 			wx = scrco - 1;
7737c478bd9Sstevel@tonic-gate 			scp = curscr->_y[wy] + wx;
7747c478bd9Sstevel@tonic-gate 			for (; wx >= 0; --wx, --scp)
7757c478bd9Sstevel@tonic-gate 				if (_DARKCHAR(*scp))
7767c478bd9Sstevel@tonic-gate 					break;
7777c478bd9Sstevel@tonic-gate 			/* LINTED */
778*e07d85f8SToomas Soome 			_ENDNS[wy] = (short)wx;
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		/* update the hash structure */
7827c478bd9Sstevel@tonic-gate 		_CURHASH[wy] = _BEGNS[wy] < scrco ? _NOHASH : 0;
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate /*
7877c478bd9Sstevel@tonic-gate  * See if a left or right shift is apppropriate
7887c478bd9Sstevel@tonic-gate  * This routine is called only if !cookie_glitch or no video attributes
7897c478bd9Sstevel@tonic-gate  * are used in the affected part.
7907c478bd9Sstevel@tonic-gate  * The main idea is to find a longest common substring which is a
7917c478bd9Sstevel@tonic-gate  * prefix of one of 'wcp' or 'scp', then either delete or
7927c478bd9Sstevel@tonic-gate  * insert depending on where the prefix is.
7937c478bd9Sstevel@tonic-gate  *
7947c478bd9Sstevel@tonic-gate  * wcp : what we want the screen to look like
7957c478bd9Sstevel@tonic-gate  * scp : what the screen looks like now
7967c478bd9Sstevel@tonic-gate  * length: the length to be updated
7977c478bd9Sstevel@tonic-gate  * maxi: maximum possible insert amount
7987c478bd9Sstevel@tonic-gate  * id; *id returns the amount of insert/delete
7997c478bd9Sstevel@tonic-gate  *
8007c478bd9Sstevel@tonic-gate  * Return the number of chars matched after the shift.
8017c478bd9Sstevel@tonic-gate  */
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate static int
_useidch(chtype * wcp,chtype * scp,int length,int maxi,int * id)8047c478bd9Sstevel@tonic-gate _useidch(chtype *wcp, chtype *scp, int length, int maxi, int *id)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 	int	x1, x2, blnk, idch, cost, cost_ich1, match;
8077c478bd9Sstevel@tonic-gate 	chtype	wc;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	/* try deletion */
8107c478bd9Sstevel@tonic-gate 	if (SP->dchok && _CHAR(*wcp) != ' ') {
8117c478bd9Sstevel@tonic-gate 		if ((match = _prefix(wcp, scp, length, length / 2, &idch)) > 0)
8127c478bd9Sstevel@tonic-gate 			cost = _COST(dcfixed) + (parm_dch ? _COST(Parm_dch) :
8137c478bd9Sstevel@tonic-gate 			    _COST(Delete_character) * idch);
8147c478bd9Sstevel@tonic-gate 		else
8157c478bd9Sstevel@tonic-gate 			cost = _INFINITY;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 		if (match >= cost) {
8187c478bd9Sstevel@tonic-gate /* SS: colors */
8197c478bd9Sstevel@tonic-gate 			if (back_color_erase)
8207c478bd9Sstevel@tonic-gate 				_turn_off_background();
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 			if (SP->dmode) {
8237c478bd9Sstevel@tonic-gate 				if (SP->sid_equal) {
8247c478bd9Sstevel@tonic-gate 					if (!(SP->phys_irm))
8257c478bd9Sstevel@tonic-gate 						_ONINSERT();
8267c478bd9Sstevel@tonic-gate 				} else {
8277c478bd9Sstevel@tonic-gate 					if (SP->phys_irm)
8287c478bd9Sstevel@tonic-gate 						_OFFINSERT();
8297c478bd9Sstevel@tonic-gate 					_PUTS(enter_delete_mode, 1);
8307c478bd9Sstevel@tonic-gate 				}
8317c478bd9Sstevel@tonic-gate 			}
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 			if (parm_dch)
8347c478bd9Sstevel@tonic-gate 				_PUTS(tparm_p1(parm_dch, idch), 1);
8357c478bd9Sstevel@tonic-gate 			else
8367c478bd9Sstevel@tonic-gate 				for (x1 = 0; x1 < idch; ++x1)
8377c478bd9Sstevel@tonic-gate 					_PUTS(delete_character, 1);
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 			if (SP->dmode) {
8407c478bd9Sstevel@tonic-gate 				if (SP->eid_equal)
8417c478bd9Sstevel@tonic-gate 					SP->phys_irm = FALSE;
8427c478bd9Sstevel@tonic-gate 				_PUTS(exit_delete_mode, 1);
8437c478bd9Sstevel@tonic-gate 			}
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 			/* update screen image */
8467c478bd9Sstevel@tonic-gate 			for (x1 = 0, x2 = idch; x2 < length; ++x1, ++x2)
8477c478bd9Sstevel@tonic-gate 				scp[x1] = scp[x2];
8487c478bd9Sstevel@tonic-gate 			for (; x1 < length; ++x1)
8497c478bd9Sstevel@tonic-gate 				scp[x1] = ' ';
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 			*id = -idch;
8527c478bd9Sstevel@tonic-gate 			return (match);
8537c478bd9Sstevel@tonic-gate 		}
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	/* no insertion wanted or possible */
8577c478bd9Sstevel@tonic-gate 	if (!(SP->ichok) || _CHAR(*scp) == ' ')
8587c478bd9Sstevel@tonic-gate 		return (0);
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	/* see if insertion is worth it */
8617c478bd9Sstevel@tonic-gate 	maxi = (idch = length / 2) < maxi ? idch : maxi;
8627c478bd9Sstevel@tonic-gate 	if ((match = _prefix(scp, wcp, length, maxi, &idch)) <= 0)
8637c478bd9Sstevel@tonic-gate 		return (0);
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	/* see if inserting blanks only */
8667c478bd9Sstevel@tonic-gate 	for (blnk = 0; blnk < idch; ++blnk)
8677c478bd9Sstevel@tonic-gate 		if (wcp[blnk] != ' ') {
8687c478bd9Sstevel@tonic-gate 			blnk = 0;
8697c478bd9Sstevel@tonic-gate 			break;
8707c478bd9Sstevel@tonic-gate 		}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/* see if doing insertion is worth it */
8737c478bd9Sstevel@tonic-gate 	cost_ich1 = idch * _COST(Insert_character);
8747c478bd9Sstevel@tonic-gate 	if (SP->imode) {
8757c478bd9Sstevel@tonic-gate 		cost = SP->phys_irm ? 0 : _COST(icfixed);
8767c478bd9Sstevel@tonic-gate 		if (blnk > _COST(Parm_ich) && _COST(Parm_ich) < cost_ich1)
8777c478bd9Sstevel@tonic-gate 			cost += _COST(Parm_ich);
8787c478bd9Sstevel@tonic-gate 		else
8797c478bd9Sstevel@tonic-gate 			if (insert_character)
8807c478bd9Sstevel@tonic-gate 				cost += cost_ich1;
8817c478bd9Sstevel@tonic-gate 	} else {
8827c478bd9Sstevel@tonic-gate 		if (parm_ich && _COST(Parm_ich) < cost_ich1)
8837c478bd9Sstevel@tonic-gate 			cost = _COST(Parm_ich);
8847c478bd9Sstevel@tonic-gate 		else
8857c478bd9Sstevel@tonic-gate 			cost = cost_ich1;
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 	if ((cost - blnk) > match)
8887c478bd9Sstevel@tonic-gate 		return (0);
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/* perform the insertions */
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	/* SS: colors */
8937c478bd9Sstevel@tonic-gate 	if (back_color_erase)
8947c478bd9Sstevel@tonic-gate 		_turn_off_background();
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	if (SP->imode) {
8977c478bd9Sstevel@tonic-gate 		if (!SP->phys_irm)
8987c478bd9Sstevel@tonic-gate 			_ONINSERT();
8997c478bd9Sstevel@tonic-gate 		if (blnk > _COST(Parm_ich) && _COST(Parm_ich) < cost_ich1)
9007c478bd9Sstevel@tonic-gate 			_PUTS(tparm_p1(parm_ich, idch), 1);
9017c478bd9Sstevel@tonic-gate 		else
9027c478bd9Sstevel@tonic-gate 			if (insert_character)
9037c478bd9Sstevel@tonic-gate 				goto do_insert_char;
9047c478bd9Sstevel@tonic-gate 			else
9057c478bd9Sstevel@tonic-gate 				/* so that we'll do real char insertions */
9067c478bd9Sstevel@tonic-gate 				blnk = 0;
9077c478bd9Sstevel@tonic-gate 	} else {
9087c478bd9Sstevel@tonic-gate 		if (parm_ich && _COST(Parm_ich) < cost_ich1)
9097c478bd9Sstevel@tonic-gate 			_PUTS(tparm_p1(parm_ich, idch), 1);
9107c478bd9Sstevel@tonic-gate 		else {
9117c478bd9Sstevel@tonic-gate do_insert_char:
9127c478bd9Sstevel@tonic-gate 			for (x1 = 0; x1 < idch; ++x1)
9137c478bd9Sstevel@tonic-gate 				_PUTS(insert_character, 1);
9147c478bd9Sstevel@tonic-gate 		}
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	/* inserting desired characters */
9187c478bd9Sstevel@tonic-gate 	if (!blnk)
9197c478bd9Sstevel@tonic-gate 		for (x1 = 0; x1 < idch; ++x1) {
9207c478bd9Sstevel@tonic-gate 			wc = wcp[x1];
9217c478bd9Sstevel@tonic-gate 			if (_ATTR(wc) != curscr->_attrs)
9227c478bd9Sstevel@tonic-gate 				_VIDS(_ATTR(wc), curscr->_attrs);
9237c478bd9Sstevel@tonic-gate 			(void) _outwch(_CHAR(wc) == '~' &&
9247c478bd9Sstevel@tonic-gate 			    tilde_glitch ? '`' : wc);
9257c478bd9Sstevel@tonic-gate 			++cx;
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	/* update the screen image */
9297c478bd9Sstevel@tonic-gate 	for (x1 = length - 1, x2 = length - idch - 1; x2 >= 0; --x1, --x2)
9307c478bd9Sstevel@tonic-gate 		scp[x1] = scp[x2];
931*e07d85f8SToomas Soome 	(void) memcpy(scp, wcp, idch * sizeof (chtype));
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	*id = idch;
9347c478bd9Sstevel@tonic-gate 	return (match + idch);
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /*
9387c478bd9Sstevel@tonic-gate  * Find a substring of s2 that match a prefix of s1.
9397c478bd9Sstevel@tonic-gate  * The substring is such that:
9407c478bd9Sstevel@tonic-gate  * 	1. it does not start with an element
9417c478bd9Sstevel@tonic-gate  *	   that is in perfect alignment with one in s1 and
9427c478bd9Sstevel@tonic-gate  * 	2: it is at least as long as the displacement.
9437c478bd9Sstevel@tonic-gate  *
9447c478bd9Sstevel@tonic-gate  * length: the length of s1, s2.
9457c478bd9Sstevel@tonic-gate  * maxs: only search for match in [1,maxs]  of s2.
9467c478bd9Sstevel@tonic-gate  * begm: *begm returns where the match begins.
9477c478bd9Sstevel@tonic-gate  *
9487c478bd9Sstevel@tonic-gate  * Return the number of matches.
9497c478bd9Sstevel@tonic-gate  */
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate static int
_prefix(chtype * s1,chtype * s2,int length,int maxs,int * begm)9527c478bd9Sstevel@tonic-gate _prefix(chtype *s1, chtype *s2, int length, int maxs, int *begm)
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate 	int	m, n, k;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	n = 0;
9577c478bd9Sstevel@tonic-gate 	for (m = 1; m <= maxs; ++m)
9587c478bd9Sstevel@tonic-gate 		/* testing for s1[m] != s2[m] is condition 1 */
9597c478bd9Sstevel@tonic-gate 		if (s1[0] == s2[m] && s1[m] != s2[m]) {
9607c478bd9Sstevel@tonic-gate 			/* see if it's long enough (condition 2) */
9617c478bd9Sstevel@tonic-gate 			for (k = 2 * m - 1; k > m; --k)
9627c478bd9Sstevel@tonic-gate 				if (s1[k - m] != s2[k])
9637c478bd9Sstevel@tonic-gate 					break;
9647c478bd9Sstevel@tonic-gate 			/* found a match with a good length */
9657c478bd9Sstevel@tonic-gate 			if (k == m) {
9667c478bd9Sstevel@tonic-gate 				*begm = m;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 				/* count the # of matches */
9697c478bd9Sstevel@tonic-gate 				s2 += m;
9707c478bd9Sstevel@tonic-gate 				length -= m;
9717c478bd9Sstevel@tonic-gate 				for (n = m; n < length; ++n)
9727c478bd9Sstevel@tonic-gate 					if (s1[n] != s2[n])
9737c478bd9Sstevel@tonic-gate 						break;
9747c478bd9Sstevel@tonic-gate 				goto done;
9757c478bd9Sstevel@tonic-gate 			}
9767c478bd9Sstevel@tonic-gate 		}
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate done:
9797c478bd9Sstevel@tonic-gate 	return (n);
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate /* Set video markers for cookie terminal. */
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate static void
_setmark1(int y,int x,chtype * s)9857c478bd9Sstevel@tonic-gate _setmark1(int y, int x, chtype *s)
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate 	long	a;
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	/* set the mark map */
9907c478bd9Sstevel@tonic-gate 	marks[y][x / BITSPERBYTE] |= (1 << (x % BITSPERBYTE));
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	if (s) {
9937c478bd9Sstevel@tonic-gate 		a  = _VIDEO(curscr->_attrs);
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 		/* set the video attr of the first char here */
9967c478bd9Sstevel@tonic-gate 		/* LINTED */
9977c478bd9Sstevel@tonic-gate 		*s = _CHAR(*s) | _COLOR(*s) | a;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 		/* now the video attr of the rest of the affected interval */
10007c478bd9Sstevel@tonic-gate 		for (x += 1, s += 1; x < scrco; ++x, ++s)
10017c478bd9Sstevel@tonic-gate 			if (_ISMARK1(y, x))
10027c478bd9Sstevel@tonic-gate 				break;
10037c478bd9Sstevel@tonic-gate 			else
10047c478bd9Sstevel@tonic-gate 				/* LINTED */
10057c478bd9Sstevel@tonic-gate 				*s = _CHAR(*s) | _COLOR(*s) | a;
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate /* Set color markers for cookie terminal. */
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate static void
_setmark2(int y,int x,chtype * s)10127c478bd9Sstevel@tonic-gate _setmark2(int y, int x, chtype *s)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	long	a;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/* set the mark map */
10177c478bd9Sstevel@tonic-gate 	color_marks[y][x / BITSPERBYTE] |= (1 << (x % BITSPERBYTE));
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (s) {
10207c478bd9Sstevel@tonic-gate 		a  = _COLOR(curscr->_attrs);
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 		/* set the video attr of the first char here */
10237c478bd9Sstevel@tonic-gate 		/* LINTED */
10247c478bd9Sstevel@tonic-gate 		*s = _CHAR(*s) | _VIDEO(*s) | a;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 		/* now the video attr of the rest of the affected interval */
10277c478bd9Sstevel@tonic-gate 		for (x += 1, s += 1; x < scrco; ++x, ++s)
10287c478bd9Sstevel@tonic-gate 			if (_ISMARK2(y, x))
10297c478bd9Sstevel@tonic-gate 				break;
10307c478bd9Sstevel@tonic-gate 			else
10317c478bd9Sstevel@tonic-gate 				/* LINTED */
10327c478bd9Sstevel@tonic-gate 				*s = _CHAR(*s) | _VIDEO(*s) | a;
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate /* At the right margin various weird things can happen.  We treat them here. */
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate /* At the right margin various weird things can happen.  We treat them here. */
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate static void
_rmargin(int wx)10427c478bd9Sstevel@tonic-gate _rmargin(int wx)
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate 	int	x, w, ix;
10457c478bd9Sstevel@tonic-gate 	chtype	sc;
10467c478bd9Sstevel@tonic-gate 	chtype	*wcp =	_virtscr->_y[cy];
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	/* screen may scroll */
10497c478bd9Sstevel@tonic-gate 	if (cy == scrli - 1) {
10507c478bd9Sstevel@tonic-gate 		/* can't do anything */
10517c478bd9Sstevel@tonic-gate 		if (!SP->ichok)
10527c478bd9Sstevel@tonic-gate 			return;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 		/* the width of the new character */
10557c478bd9Sstevel@tonic-gate 		w = _curs_scrwidth[TYPE(RBYTE(wcp[wx]))];
10567c478bd9Sstevel@tonic-gate 		/* the place to put it without causing scrolling */
10577c478bd9Sstevel@tonic-gate 		for (x = wx - 1; x > 0; --x)
10587c478bd9Sstevel@tonic-gate 			if (!ISCBIT(wcp[x]))
10597c478bd9Sstevel@tonic-gate 				break;
10607c478bd9Sstevel@tonic-gate 		sc = curscr->_y[cy][x];
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 		(void) mvcur(cy, cx, cy, x);
10637c478bd9Sstevel@tonic-gate 		if (_ATTR(wcp[wx]) != curscr->_attrs)
10647c478bd9Sstevel@tonic-gate 			_VIDS(_ATTR(wcp[wx]), curscr->_attrs);
10657c478bd9Sstevel@tonic-gate 		(void) _outwch(tilde_glitch &&
10667c478bd9Sstevel@tonic-gate 		    _CHAR(wcp[wx]) == '~' ? '`' : wcp[wx]);
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 		for (ix = wx + 1; ix < scrco; ++ix) {
10697c478bd9Sstevel@tonic-gate 			(void) _outwch(wcp[ix]);
10707c478bd9Sstevel@tonic-gate 		}
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 		/* insert sc back in and push wcp[wx] right */
10737c478bd9Sstevel@tonic-gate 		(void) mvcur(cy, x+w, cy, x);
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 		/* SS: colors */
10767c478bd9Sstevel@tonic-gate 		if (back_color_erase)
10777c478bd9Sstevel@tonic-gate 			_turn_off_background();
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 		if (SP->imode && !SP->phys_irm)
10807c478bd9Sstevel@tonic-gate 			_ONINSERT();
10817c478bd9Sstevel@tonic-gate 		/* width of the old character that was overwritten */
10827c478bd9Sstevel@tonic-gate 		w = _curs_scrwidth[TYPE(RBYTE(curscr->_y[cy][x]))];
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 		if (insert_character)
10857c478bd9Sstevel@tonic-gate 			for (ix = 0; ix < w; ++ix)
10867c478bd9Sstevel@tonic-gate 				_PUTS(insert_character, 1);
10877c478bd9Sstevel@tonic-gate 		else
10887c478bd9Sstevel@tonic-gate 			if (parm_ich && !SP->imode)
10897c478bd9Sstevel@tonic-gate 				_PUTS(tparm_p1(parm_ich, w), 1);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 		if (_ATTR(sc) != curscr->_attrs)
10927c478bd9Sstevel@tonic-gate 			_VIDS(_ATTR(sc), curscr->_attrs);
10937c478bd9Sstevel@tonic-gate 		for (ix = x; w > 0; --w, ++ix)
10947c478bd9Sstevel@tonic-gate 			(void) _outwch(curscr->_y[cy][ix]);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		/* make sure the video attrs are ok */
10977c478bd9Sstevel@tonic-gate 		if (marks && (_ATTR(sc) || _ATTR(wcp[wx])))
10987c478bd9Sstevel@tonic-gate 			_VIDS(_ATTR(wcp[wx]), ~_ATTR(sc));
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 		/* update screen image */
11017c478bd9Sstevel@tonic-gate 		/* LINTED */
1102*e07d85f8SToomas Soome 		cx = (short)wx;
11037c478bd9Sstevel@tonic-gate 		curscr->_y[cy][wx] = wcp[wx];
11047c478bd9Sstevel@tonic-gate 		for (x = wx + 1; x < scrco; ++x) {
11057c478bd9Sstevel@tonic-gate 			(void) _outwch(wcp[x]);
11067c478bd9Sstevel@tonic-gate 			curscr->_y[cy][x] = wcp[x];
11077c478bd9Sstevel@tonic-gate 		}
11087c478bd9Sstevel@tonic-gate 		return;
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	/* put char out and update screen image */
11127c478bd9Sstevel@tonic-gate 	(void) _outwch(tilde_glitch && _CHAR(wcp[wx]) == '~' ? '`' : wcp[wx]);
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	curscr->_y[cy][wx] = wcp[wx];
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	for (x = wx + 1; x < scrco; ++x) {
11247c478bd9Sstevel@tonic-gate 		(void) _outwch(wcp[x]);
11257c478bd9Sstevel@tonic-gate 		curscr->_y[cy][x] = wcp[x];
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	/* make sure that wrap-around happens */
11297c478bd9Sstevel@tonic-gate 	if (!auto_right_margin || eat_newline_glitch) {
11307c478bd9Sstevel@tonic-gate 		(void) _outch('\r');
11317c478bd9Sstevel@tonic-gate 		(void) _outch('\n');
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 	cx = 0;
11347c478bd9Sstevel@tonic-gate 	++cy;
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate /*
11387c478bd9Sstevel@tonic-gate  * Find the top-most line to do clear-to-eod.
11397c478bd9Sstevel@tonic-gate  *
11407c478bd9Sstevel@tonic-gate  * topy, boty: the region to consider
11417c478bd9Sstevel@tonic-gate  */
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate static int
_getceod(int topy,int boty)11447c478bd9Sstevel@tonic-gate _getceod(int topy, int boty)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	chtype	*wcp, *ecp;
11477c478bd9Sstevel@tonic-gate 	int	wy;
11487c478bd9Sstevel@tonic-gate 	short	*begch, *endch, *begns;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	/* do nothing */
11517c478bd9Sstevel@tonic-gate 	if ((topy + 1) >= boty)
11527c478bd9Sstevel@tonic-gate 		return (boty);
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	wy = boty - 1;
11557c478bd9Sstevel@tonic-gate 	begch = _virtscr->_firstch + wy;
11567c478bd9Sstevel@tonic-gate 	endch = _virtscr->_lastch + wy;
11577c478bd9Sstevel@tonic-gate 	begns = _BEGNS + wy;
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	for (; wy >= topy; --wy, --begch, --endch, --begns) {
11607c478bd9Sstevel@tonic-gate 		if (*endch == _BLANK || (*begch >= scrco && *begns >= scrco))
11617c478bd9Sstevel@tonic-gate 			continue;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 		wcp = _virtscr->_y[wy];
11647c478bd9Sstevel@tonic-gate 		ecp = wcp + scrco;
11657c478bd9Sstevel@tonic-gate 		for (; wcp < ecp; ++wcp)
11667c478bd9Sstevel@tonic-gate 			if (_DARKCHAR(*wcp))
11677c478bd9Sstevel@tonic-gate 			break;
11687c478bd9Sstevel@tonic-gate 		if (wcp != ecp)
11697c478bd9Sstevel@tonic-gate 			break;
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 		*endch = _BLANK;
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	return (wy + 1);
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate /* Use hardware clear-to-bottom. */
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate static void
_useceod(int topy,int boty)11807c478bd9Sstevel@tonic-gate _useceod(int topy, int boty)
11817c478bd9Sstevel@tonic-gate {
11827c478bd9Sstevel@tonic-gate 	short	*begns, *begch;
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	/* skip lines already blanked */
11857c478bd9Sstevel@tonic-gate 	begch = _virtscr->_firstch + topy;
11867c478bd9Sstevel@tonic-gate 	begns = _BEGNS + topy;
11877c478bd9Sstevel@tonic-gate 	for (; topy < boty; ++topy, ++begns, ++begch)
11887c478bd9Sstevel@tonic-gate 		if (*begns < scrco || *begch == _REDRAW)
11897c478bd9Sstevel@tonic-gate 			break;
11907c478bd9Sstevel@tonic-gate 		else
11917c478bd9Sstevel@tonic-gate 			*begch = _INFINITY;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	/* nothing to do */
11947c478bd9Sstevel@tonic-gate 	if (topy + 1 >= boty)
11957c478bd9Sstevel@tonic-gate 		return;
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	/* see if bottom is clear */
11987c478bd9Sstevel@tonic-gate 	for (begns = _BEGNS + boty; boty < scrli; ++boty, ++begns)
11997c478bd9Sstevel@tonic-gate 		if (*begns < scrco)
12007c478bd9Sstevel@tonic-gate 			return;
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	/* use clear-screen if appropriate */
12037c478bd9Sstevel@tonic-gate 	if (topy == 0) {
12047c478bd9Sstevel@tonic-gate 		/* SS: colors */
12057c478bd9Sstevel@tonic-gate 		if (back_color_erase)
12067c478bd9Sstevel@tonic-gate 			_turn_off_background();
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 		_PUTS(clear_screen, scrli);
12097c478bd9Sstevel@tonic-gate 		cy = 0; cx = 0;
12107c478bd9Sstevel@tonic-gate 		(void) werase(curscr);
1211*e07d85f8SToomas Soome 	} else {
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 		/* use clear-to-end-of-display or delete lines */
12147c478bd9Sstevel@tonic-gate 		if (clr_eos || (parm_delete_line && !memory_below)) {
12157c478bd9Sstevel@tonic-gate 			(void) mvcur(cy, cx, topy, 0);
12167c478bd9Sstevel@tonic-gate 			/* LINTED */
1217*e07d85f8SToomas Soome 			cy = (short)topy;
12187c478bd9Sstevel@tonic-gate 			cx = 0;
12197c478bd9Sstevel@tonic-gate 			/* SS: colors */
12207c478bd9Sstevel@tonic-gate 			if (back_color_erase)
12217c478bd9Sstevel@tonic-gate 				_turn_off_background();
12227c478bd9Sstevel@tonic-gate 			_PUTS(clr_eos ? clr_eos : tparm_p1(parm_delete_line,
12237c478bd9Sstevel@tonic-gate 			    scrli - topy), scrli - topy);
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 			/* update curscr */
12267c478bd9Sstevel@tonic-gate 			/* LINTED */
1227*e07d85f8SToomas Soome 			curscr->_cury = (short)topy;
12287c478bd9Sstevel@tonic-gate 			curscr->_curx = 0;
12297c478bd9Sstevel@tonic-gate 			(void) wclrtobot(curscr);
1230*e07d85f8SToomas Soome 		} else {
12317c478bd9Sstevel@tonic-gate 			/* no hardware support */
12327c478bd9Sstevel@tonic-gate 			return;
1233*e07d85f8SToomas Soome 		}
1234*e07d85f8SToomas Soome 	}
12357c478bd9Sstevel@tonic-gate 
1236*e07d85f8SToomas Soome 	/* correct the update structure */
1237*e07d85f8SToomas Soome 	(void) wtouchln(_virtscr, topy, scrli, FALSE);
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate static void
_turn_off_background(void)12427c478bd9Sstevel@tonic-gate _turn_off_background(void)
12437c478bd9Sstevel@tonic-gate {
12447c478bd9Sstevel@tonic-gate 	/* this routine turn the background color to zero.  This need to be */
12457c478bd9Sstevel@tonic-gate 	/* done only in forllowing cases:				*/
12467c478bd9Sstevel@tonic-gate 	/*  1) We are using Tek type terminal (which has bce terminfo	*/
12477c478bd9Sstevel@tonic-gate 	/*	variable)  */
12487c478bd9Sstevel@tonic-gate 	/*  2) The current background is not already zero		*/
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	if (set_background && cur_term->_cur_pair.background > 0) {
12517c478bd9Sstevel@tonic-gate 		_PUTS(orig_pair, 1);
12527c478bd9Sstevel@tonic-gate 		cur_term->_cur_pair.foreground = -1;
12537c478bd9Sstevel@tonic-gate 		cur_term->_cur_pair.background = -1;
12547c478bd9Sstevel@tonic-gate 		curscr->_attrs &= ~A_COLOR;
12557c478bd9Sstevel@tonic-gate 	}
12567c478bd9Sstevel@tonic-gate }
1257