xref: /onnv-gate/usr/src/lib/libcurses/screen/wrefresh.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*LINTLIBRARY*/
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include	<sys/types.h>
45*0Sstevel@tonic-gate #include	<stdlib.h>
46*0Sstevel@tonic-gate #include	<string.h>
47*0Sstevel@tonic-gate #include	"curses_inc.h"
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate  * Make the screen look like "win" over the area covered by win.
51*0Sstevel@tonic-gate  * This routine may use insert/delete char/line and scrolling-region.
52*0Sstevel@tonic-gate  * win : the window being updated
53*0Sstevel@tonic-gate  */
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate extern int	outchcount;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate static void	_updateln(int), _turn_off_background(void),
58*0Sstevel@tonic-gate 		_setmark1(int, int, chtype *),
59*0Sstevel@tonic-gate 		_setmark2(int, int, chtype *), _rmargin(int),
60*0Sstevel@tonic-gate 		_useceod(int, int);
61*0Sstevel@tonic-gate static int	_useidch(chtype *, chtype *, int, int, int *),
62*0Sstevel@tonic-gate 		_prefix(chtype *, chtype *, int, int, int *),
63*0Sstevel@tonic-gate 		_getceod(int, int);
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate static	short	cy, cx,		/* current cursor coord */
66*0Sstevel@tonic-gate 		scrli,		/* actual screen lines */
67*0Sstevel@tonic-gate 		scrco;		/* actual screen columns */
68*0Sstevel@tonic-gate static	char	**marks;	/* the mark table for cookie terminals */
69*0Sstevel@tonic-gate static	char	**color_marks;	/* color mark table for cookie terminals */
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate #define	_ISMARK1(y, x)	(marks[y][x / BITSPERBYTE] & (1 << (x % BITSPERBYTE)))
72*0Sstevel@tonic-gate #define	_ISMARK2(y, x)	(color_marks ? (color_marks[y][x / BITSPERBYTE] & \
73*0Sstevel@tonic-gate 			(1 << (x % BITSPERBYTE))) : FALSE)
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate #define	_VIDEO(c)	((c) & A_ATTRIBUTES & ~A_COLOR)
76*0Sstevel@tonic-gate #define	_COLOR(c)	((c) & A_COLOR)
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate #ifdef	_VR2_COMPAT_CODE
79*0Sstevel@tonic-gate extern	char	_endwin;
80*0Sstevel@tonic-gate #endif	/* _VR2_COMPAT_CODE */
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate int
wrefresh(WINDOW * win)83*0Sstevel@tonic-gate wrefresh(WINDOW *win)
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	short	*bnsch, *ensch;
87*0Sstevel@tonic-gate 	SLK_MAP	*slk;
88*0Sstevel@tonic-gate 	int	wx, wy, nc, boty, clby, idby, *hs, curwin;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	curwin = (win == curscr);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	/* don't allow curscr refresh if the screen was just created */
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	if (curwin && curscr->_sync)
95*0Sstevel@tonic-gate 		return (OK);
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	/* go thru _stdbody */
98*0Sstevel@tonic-gate 	if (!curwin && (win != _virtscr))
99*0Sstevel@tonic-gate 		(void) wnoutrefresh(win);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	/* if there is typeahead */
102*0Sstevel@tonic-gate 	if ((_INPUTPENDING = _chkinput()) == TRUE) {
103*0Sstevel@tonic-gate 		if (curwin)
104*0Sstevel@tonic-gate 			curscr->_clear = TRUE;
105*0Sstevel@tonic-gate 		return (OK);
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	if (curwin || curscr->_clear)
109*0Sstevel@tonic-gate 		_virtscr->_clear = TRUE;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	/* save curscr cursor coordinates */
112*0Sstevel@tonic-gate 	cy = curscr->_cury;
113*0Sstevel@tonic-gate 	cx = curscr->_curx;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	/* to simplify code in some cases */
116*0Sstevel@tonic-gate 	marks = _MARKS;
117*0Sstevel@tonic-gate 	color_marks = _COLOR_MARKS;
118*0Sstevel@tonic-gate 	scrli = curscr->_maxy;
119*0Sstevel@tonic-gate 	scrco = curscr->_maxx;
120*0Sstevel@tonic-gate 	slk = SP->slk;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	outchcount = 0;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	/* make sure we're in program mode */
125*0Sstevel@tonic-gate 	if (SP->fl_endwin) {
126*0Sstevel@tonic-gate 		/* If endwin is equal to 2 it means we just did a newscreen. */
127*0Sstevel@tonic-gate 		if (SP->fl_endwin == TRUE) {
128*0Sstevel@tonic-gate 			(void) reset_prog_mode();
129*0Sstevel@tonic-gate 			if (SP->kp_state)
130*0Sstevel@tonic-gate 				(void) tputs(keypad_xmit, 1, _outch);
131*0Sstevel@tonic-gate 			if (slk)
132*0Sstevel@tonic-gate 				(*_do_slk_tch)();
133*0Sstevel@tonic-gate 			if (SP->fl_meta)
134*0Sstevel@tonic-gate 				(void) tputs(meta_on, 1, _outch);
135*0Sstevel@tonic-gate 			if (cur_term->_cursorstate != 1)
136*0Sstevel@tonic-gate 				_PUTS(cur_term->cursor_seq[cur_term->
137*0Sstevel@tonic-gate 				    _cursorstate], 0);
138*0Sstevel@tonic-gate 		}
139*0Sstevel@tonic-gate 		_PUTS(enter_ca_mode, 1);
140*0Sstevel@tonic-gate 		(void) tputs(ena_acs, 1, _outch);
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 		if (exit_attribute_mode)
143*0Sstevel@tonic-gate 			_PUTS(tparm_p0(exit_attribute_mode), 1);
144*0Sstevel@tonic-gate 		else
145*0Sstevel@tonic-gate 			/*
146*0Sstevel@tonic-gate 			 * If there is no exit_attribute mode, then vidupdate
147*0Sstevel@tonic-gate 			 * could only possibly turn off one of the below three
148*0Sstevel@tonic-gate 			 * so that's all we ask it turn off.
149*0Sstevel@tonic-gate 			*/
150*0Sstevel@tonic-gate 			vidupdate(A_NORMAL, (A_ALTCHARSET | A_STANDOUT |
151*0Sstevel@tonic-gate 			    A_UNDERLINE), _outch);
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 		SP->fl_endwin = FALSE;
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate #ifdef	_VR2_COMPAT_CODE
156*0Sstevel@tonic-gate 		_endwin = (char) FALSE;
157*0Sstevel@tonic-gate #endif	/* _VR2_COMPAT_CODE */
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	/* clear the screen if required */
161*0Sstevel@tonic-gate 	if (_virtscr->_clear) {
162*0Sstevel@tonic-gate /* SS: colors */
163*0Sstevel@tonic-gate 		if (back_color_erase)
164*0Sstevel@tonic-gate 			_turn_off_background();
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 		_PUTS(clear_screen, scrli);
167*0Sstevel@tonic-gate 		cy = cx = curscr->_curx = curscr->_cury = 0;
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 		/* _sync indicates that this a new screen */
170*0Sstevel@tonic-gate 		if (!curscr->_sync)
171*0Sstevel@tonic-gate 			(void) werase(curscr);
172*0Sstevel@tonic-gate 		else {
173*0Sstevel@tonic-gate 			nc = scrco / BITSPERBYTE - (scrco %
174*0Sstevel@tonic-gate 			    BITSPERBYTE ? 0 : 1);
175*0Sstevel@tonic-gate 			wy = scrli - 1;
176*0Sstevel@tonic-gate 			bnsch = _BEGNS; ensch = _ENDNS;
177*0Sstevel@tonic-gate 			hs = _CURHASH;
178*0Sstevel@tonic-gate 			for (; wy >= 0; --wy) {
179*0Sstevel@tonic-gate 				*bnsch++ = scrco;
180*0Sstevel@tonic-gate 				*ensch++ = -1;
181*0Sstevel@tonic-gate 				*hs++ = 0;
182*0Sstevel@tonic-gate 				if (marks)
183*0Sstevel@tonic-gate 					for (wx = nc; wx >= 0; --wx)
184*0Sstevel@tonic-gate 						marks[wy][wx] = 0;
185*0Sstevel@tonic-gate 			}
186*0Sstevel@tonic-gate 		}
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 		_virtscr->_clear = curscr->_sync = curscr->_clear = FALSE;
189*0Sstevel@tonic-gate 		if (slk)
190*0Sstevel@tonic-gate 			(*_do_slk_tch)();
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 		/* pretend _virtscr has been totally changed */
193*0Sstevel@tonic-gate 		(void) wtouchln(_virtscr, 0, scrli, -1);
194*0Sstevel@tonic-gate 		_VIRTTOP = 0;
195*0Sstevel@tonic-gate 		_VIRTBOT = scrli - 1;
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 		/* will not do clear-eod or ins/del lines */
198*0Sstevel@tonic-gate 		clby = idby = scrli;
199*0Sstevel@tonic-gate 	} else
200*0Sstevel@tonic-gate 		clby = idby = -1;
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	/* Software soft labels; if _changed == 2, slk's are in clear mode. */
203*0Sstevel@tonic-gate 	if (slk && slk->_win && (slk->_changed == TRUE))
204*0Sstevel@tonic-gate 		(*_do_slk_noref)();
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	/* do line updating */
207*0Sstevel@tonic-gate 	_virtscr->_clear = FALSE;
208*0Sstevel@tonic-gate 	wy = _VIRTTOP;
209*0Sstevel@tonic-gate 	boty = _VIRTBOT + 1;
210*0Sstevel@tonic-gate 	bnsch = _virtscr->_firstch + wy;
211*0Sstevel@tonic-gate 	ensch = _virtscr->_lastch + wy;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	for (; wy < boty; ++wy, ++bnsch, ++ensch) {
214*0Sstevel@tonic-gate 		/* this line is up-to-date */
215*0Sstevel@tonic-gate 		if (*bnsch >= scrco)
216*0Sstevel@tonic-gate 			goto next;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 		/* there is type-ahead */
219*0Sstevel@tonic-gate 		if (!curwin && (_INPUTPENDING = _chkinput()) == TRUE) {
220*0Sstevel@tonic-gate 			/* LINTED */
221*0Sstevel@tonic-gate 			_VIRTTOP = (short) wy;
222*0Sstevel@tonic-gate 			goto done;
223*0Sstevel@tonic-gate 		}
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 		if (clby < 0) {
226*0Sstevel@tonic-gate 			/* now we have to work, check for ceod */
227*0Sstevel@tonic-gate 			clby = _getceod(wy, boty);
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 			/* check for insert/delete lines */
230*0Sstevel@tonic-gate 			if (_virtscr->_use_idl)
231*0Sstevel@tonic-gate 				idby = (*_setidln)();
232*0Sstevel@tonic-gate 		}
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 		/* try clear-to-eod */
235*0Sstevel@tonic-gate 		if (wy == clby)
236*0Sstevel@tonic-gate 			_useceod(wy, boty);
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 		/* try ins/del lines */
239*0Sstevel@tonic-gate 		if (wy == idby) {
240*0Sstevel@tonic-gate 			curscr->_cury = cy;
241*0Sstevel@tonic-gate 			curscr->_curx = cx;
242*0Sstevel@tonic-gate 			(*_useidln)();
243*0Sstevel@tonic-gate 			cy = curscr->_cury;
244*0Sstevel@tonic-gate 			cx = curscr->_curx;
245*0Sstevel@tonic-gate 		}
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 		if (*bnsch < scrco)
248*0Sstevel@tonic-gate 			_updateln(wy);
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate next:
251*0Sstevel@tonic-gate 		*bnsch = _INFINITY;
252*0Sstevel@tonic-gate 		*ensch = -1;
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	/* do hardware soft labels; if _changed == 2, */
256*0Sstevel@tonic-gate 	/* slk's are in clear mode. */
257*0Sstevel@tonic-gate 	if (slk && (slk->_changed == TRUE) && !(slk->_win))
258*0Sstevel@tonic-gate 		(*_do_slk_ref)();
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	/* move cursor */
261*0Sstevel@tonic-gate 	wy = _virtscr->_cury;
262*0Sstevel@tonic-gate 	wx = _virtscr->_curx;
263*0Sstevel@tonic-gate 	if (wy != cy || wx != cx) {
264*0Sstevel@tonic-gate 		(void) mvcur(cy, cx, wy, wx);
265*0Sstevel@tonic-gate 		/* LINTED */
266*0Sstevel@tonic-gate 		cy = (short) wy;
267*0Sstevel@tonic-gate 		/* LINTED */
268*0Sstevel@tonic-gate 		cx = (short) wx;
269*0Sstevel@tonic-gate 	}
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	/* reset the flags */
272*0Sstevel@tonic-gate 	curscr->_clear = FALSE;
273*0Sstevel@tonic-gate 	_virtscr->_use_idl = FALSE;
274*0Sstevel@tonic-gate 	_virtscr->_use_idc = TRUE;
275*0Sstevel@tonic-gate 	_INPUTPENDING = FALSE;
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	/* virtual image is now up-to-date */
278*0Sstevel@tonic-gate 	_VIRTTOP = scrli;
279*0Sstevel@tonic-gate 	_VIRTBOT = -1;
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate done :
282*0Sstevel@tonic-gate 	curscr->_cury = cy;
283*0Sstevel@tonic-gate 	curscr->_curx = cx;
284*0Sstevel@tonic-gate 	(void) fflush(SP->term_file);
285*0Sstevel@tonic-gate 	return (outchcount);
286*0Sstevel@tonic-gate }
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate /* Shift appropriate portions of a line to leave space for cookies. */
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate static	chtype	*
_shove(int wy)291*0Sstevel@tonic-gate _shove(int wy)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate 	chtype		*wcp, *cp, prev;
294*0Sstevel@tonic-gate 	short		curx;
295*0Sstevel@tonic-gate 	int		x, cury, didshift;
296*0Sstevel@tonic-gate 	static	chtype	*line;
297*0Sstevel@tonic-gate 	static	int	length;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	/* allocate space for shifted line */
300*0Sstevel@tonic-gate 	if (length < scrco) {
301*0Sstevel@tonic-gate 		if (line)
302*0Sstevel@tonic-gate 			free((char *) line);
303*0Sstevel@tonic-gate 		line = (chtype *) malloc(scrco * sizeof (chtype));
304*0Sstevel@tonic-gate 		length = line ? scrco : 0;
305*0Sstevel@tonic-gate 	}
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	/* no space to do it */
308*0Sstevel@tonic-gate 	if (!line)
309*0Sstevel@tonic-gate 		return (_virtscr->_y[wy]);
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	prev = A_NORMAL;
312*0Sstevel@tonic-gate 	cp = line;
313*0Sstevel@tonic-gate 	wcp = _virtscr->_y[wy];
314*0Sstevel@tonic-gate 	curx = _virtscr->_curx;
315*0Sstevel@tonic-gate 	cury = _virtscr->_cury;
316*0Sstevel@tonic-gate 	didshift = FALSE;
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	for (x = 0; x < scrco; ++x, ++wcp, ++cp) {
319*0Sstevel@tonic-gate 		if (_ATTR(*wcp) != prev) {
320*0Sstevel@tonic-gate 			/* use existing blank */
321*0Sstevel@tonic-gate 			if (_CHAR(*wcp) == ' ')
322*0Sstevel@tonic-gate 				*cp = ' ' | _ATTR(*(wcp + 1));
323*0Sstevel@tonic-gate 				/* use previous blank */
324*0Sstevel@tonic-gate 			else
325*0Sstevel@tonic-gate 				if ((x > 0) && _CHAR(*(cp - 1)) == ' ') {
326*0Sstevel@tonic-gate 					*(cp - 1) = ' ' | _ATTR(*wcp);
327*0Sstevel@tonic-gate 					*cp = *wcp;
328*0Sstevel@tonic-gate 				} else {
329*0Sstevel@tonic-gate 					if ((curx >= x) && (cury == wy))
330*0Sstevel@tonic-gate 						++curx;
331*0Sstevel@tonic-gate 					*cp = ' ' | _ATTR(*wcp);
332*0Sstevel@tonic-gate 					--wcp;
333*0Sstevel@tonic-gate 					didshift = TRUE;
334*0Sstevel@tonic-gate 				}
335*0Sstevel@tonic-gate 			prev = _ATTR(*cp);
336*0Sstevel@tonic-gate 		} else
337*0Sstevel@tonic-gate 			*cp = *wcp;
338*0Sstevel@tonic-gate 	}
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	/* make sure that the end of the line is normal */
341*0Sstevel@tonic-gate 	cp = line + scrco - 1;
342*0Sstevel@tonic-gate 	if (didshift || (_ATTR(*cp) != A_NORMAL) ||
343*0Sstevel@tonic-gate 	    ((wy == scrli - 1) && (_ATTR(*(cp - 1)) != A_NORMAL))) {
344*0Sstevel@tonic-gate 		*cp = didshift ? ' ' : _CHAR(*cp);
345*0Sstevel@tonic-gate 		if (wy == scrli - 1)
346*0Sstevel@tonic-gate 			*(cp - 1) = didshift ? ' ' : _CHAR(*(cp - 1));
347*0Sstevel@tonic-gate 	}
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	if (wy == cury)
350*0Sstevel@tonic-gate 		_virtscr->_curx = curx >= scrco ? scrco - 1 : curx;
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	return (line);
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate /*
356*0Sstevel@tonic-gate  * Update a line.
357*0Sstevel@tonic-gate  * Three schemes of coloring are allowed. The first is the usual
358*0Sstevel@tonic-gate  * pen-up/pen-down model. The second is the HP26*-like model.
359*0Sstevel@tonic-gate  * In this case, colorings are specified by intervals, the left
360*0Sstevel@tonic-gate  * side of the interval has the coloring mark, the right side
361*0Sstevel@tonic-gate  * has the end-coloring mark. We assume that clear sequences will
362*0Sstevel@tonic-gate  * clear ALL marks in the affected regions. The second case is
363*0Sstevel@tonic-gate  * signified by the boolean flag ceol_standout_glitch.
364*0Sstevel@tonic-gate  * The third case is for terminals that leave visible cookies on
365*0Sstevel@tonic-gate  * the screen. This last case is at most an approximation of what
366*0Sstevel@tonic-gate  * can be done right.
367*0Sstevel@tonic-gate  */
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate static	void
_updateln(int wy)370*0Sstevel@tonic-gate _updateln(int wy)
371*0Sstevel@tonic-gate {
372*0Sstevel@tonic-gate 	chtype	*wcp, *scp, *wp, *sp, wc, sc;
373*0Sstevel@tonic-gate 	int	wx, lastx, x, mtch, idch, blnkx, idcx, video_attrx,
374*0Sstevel@tonic-gate 		color_attrx, maxi, endns, begns, wx_sav, multi_col;
375*0Sstevel@tonic-gate 	bool	redraw, changed, didcolor, didvideo;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	redraw = (_virtscr->_firstch[wy] == _REDRAW);
378*0Sstevel@tonic-gate 	endns = _ENDNS[wy];
379*0Sstevel@tonic-gate 	begns = _BEGNS[wy];
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	/* easy case */
382*0Sstevel@tonic-gate 	if (!redraw && (_virtscr->_lastch[wy] == _BLANK) && (begns >= scrco))
383*0Sstevel@tonic-gate 		return;
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 	/* line images */
386*0Sstevel@tonic-gate 	wcp = magic_cookie_glitch <= 0 ? _virtscr->_y[wy] : _shove(wy);
387*0Sstevel@tonic-gate 	scp = curscr->_y[wy];
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	/* the interval to be updated */
390*0Sstevel@tonic-gate 	if (redraw || magic_cookie_glitch >= 0) {
391*0Sstevel@tonic-gate 		wx = 0;
392*0Sstevel@tonic-gate 		lastx = scrco;
393*0Sstevel@tonic-gate 	} else {
394*0Sstevel@tonic-gate 		wx = _virtscr->_firstch[wy];
395*0Sstevel@tonic-gate 		lastx = _virtscr->_lastch[wy] == _BLANK ? scrco :
396*0Sstevel@tonic-gate 		    _virtscr->_lastch[wy] + 1;
397*0Sstevel@tonic-gate 	}
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	/* skip equal parts */
400*0Sstevel@tonic-gate 	if (!redraw) {
401*0Sstevel@tonic-gate 		/* skip the starting equal part */
402*0Sstevel@tonic-gate 		wp = wcp + wx;
403*0Sstevel@tonic-gate 		sp = scp + wx;
404*0Sstevel@tonic-gate 		for (; wx < lastx; ++wx)
405*0Sstevel@tonic-gate 			if (*wp++ != *sp++)
406*0Sstevel@tonic-gate 				break;
407*0Sstevel@tonic-gate 		if (wx >= lastx)
408*0Sstevel@tonic-gate 			return;
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 		/* start update at an entire character */
411*0Sstevel@tonic-gate 		for (sp = scp+wx, wp = wcp+wx; wp > wcp; --wp, --sp, --wx)
412*0Sstevel@tonic-gate 			if (!ISCBIT(*wp) && !ISCBIT(*sp))
413*0Sstevel@tonic-gate 				break;
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 		/* skip the ending equal part */
416*0Sstevel@tonic-gate 		wp = wcp + lastx - 1;
417*0Sstevel@tonic-gate 		sp = scp + lastx - 1;
418*0Sstevel@tonic-gate 		for (; lastx > wx; --lastx)
419*0Sstevel@tonic-gate 			if (*wp-- != *sp--)
420*0Sstevel@tonic-gate 				break;
421*0Sstevel@tonic-gate 		++wp;
422*0Sstevel@tonic-gate 		++wp;
423*0Sstevel@tonic-gate 		++sp;
424*0Sstevel@tonic-gate 		++sp;
425*0Sstevel@tonic-gate 		for (; lastx < scrco; ++wp, ++sp, ++lastx)
426*0Sstevel@tonic-gate 			if (!ISCBIT(*wp) && !ISCBIT(*sp))
427*0Sstevel@tonic-gate 				break;
428*0Sstevel@tonic-gate 	}
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	/* place to do clear-eol */
431*0Sstevel@tonic-gate 	if (!clr_eol || endns >= lastx)
432*0Sstevel@tonic-gate 		blnkx = scrco;
433*0Sstevel@tonic-gate 	else
434*0Sstevel@tonic-gate 		if (_virtscr->_lastch[wy] == _BLANK)
435*0Sstevel@tonic-gate 			blnkx = -1;
436*0Sstevel@tonic-gate 		else {
437*0Sstevel@tonic-gate 			for (blnkx = lastx - 1, wp = wcp + blnkx;
438*0Sstevel@tonic-gate 			    blnkx >= wx; --blnkx, --wp)
439*0Sstevel@tonic-gate 				if (_DARKCHAR(*wp))
440*0Sstevel@tonic-gate 					break;
441*0Sstevel@tonic-gate 			for (sp = scp + blnkx + 1; blnkx < scrco - 1;
442*0Sstevel@tonic-gate 			    ++sp, ++blnkx)
443*0Sstevel@tonic-gate 				if (!ISCBIT(*sp))
444*0Sstevel@tonic-gate 					break;
445*0Sstevel@tonic-gate 			if (blnkx + _COST(Clr_eol) >= lastx)
446*0Sstevel@tonic-gate 				blnkx = scrco;
447*0Sstevel@tonic-gate 		}
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 	/* on cookie terminals, we may need to do more work */
450*0Sstevel@tonic-gate 	if (marks) {
451*0Sstevel@tonic-gate 		/* video_attrx = color_attrx = scrco; */
452*0Sstevel@tonic-gate 		video_attrx = color_attrx = (lastx >= scrco) ? lastx - 1 :
453*0Sstevel@tonic-gate 		    lastx;
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 		/* find the last video attribute on the line	*/
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 		wp = wcp + video_attrx;
458*0Sstevel@tonic-gate 		for (; video_attrx >= wx; --video_attrx, --wp)
459*0Sstevel@tonic-gate 			if (_VIDEO(*wp) != A_NORMAL)
460*0Sstevel@tonic-gate 				break;
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 		/* find the last color attribute on the line	*/
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 		if (color_marks) {
465*0Sstevel@tonic-gate 			wp = wcp + color_attrx;
466*0Sstevel@tonic-gate 			for (; color_attrx >= wx; --color_attrx, --wp)
467*0Sstevel@tonic-gate 				if (_COLOR(*wp) != A_NORMAL)
468*0Sstevel@tonic-gate 					break;
469*0Sstevel@tonic-gate 			if (color_attrx < lastx)
470*0Sstevel@tonic-gate 				color_attrx++;
471*0Sstevel@tonic-gate 		}
472*0Sstevel@tonic-gate 		if (video_attrx < lastx)
473*0Sstevel@tonic-gate 			video_attrx++;
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate 		if (video_attrx >= scrco)
476*0Sstevel@tonic-gate 			--video_attrx;
477*0Sstevel@tonic-gate 		if (color_marks && color_attrx >= scrco)
478*0Sstevel@tonic-gate 			--color_attrx;
479*0Sstevel@tonic-gate 		if (magic_cookie_glitch > 0 && wy == scrli - 1 &&
480*0Sstevel@tonic-gate 		    video_attrx == scrco - 1)
481*0Sstevel@tonic-gate 			--video_attrx;
482*0Sstevel@tonic-gate 		if (color_marks && magic_cookie_glitch > 0 &&
483*0Sstevel@tonic-gate 		    wy == scrli - 1 && color_attrx == scrco - 1)
484*0Sstevel@tonic-gate 			--color_attrx;
485*0Sstevel@tonic-gate 		for (wp = wcp+video_attrx; wp >= wcp+wx; --wp)
486*0Sstevel@tonic-gate 			if (!ISCBIT(*wp))
487*0Sstevel@tonic-gate 				break;
488*0Sstevel@tonic-gate 	}
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 	/* place for insert/delete chars */
491*0Sstevel@tonic-gate #define	SLACK	4
492*0Sstevel@tonic-gate 	if (redraw || (!SP->dchok && !SP->ichok) || !(_virtscr->_use_idc) ||
493*0Sstevel@tonic-gate 	    endns < wx || (endns >= lastx && (scrco - lastx) > SLACK)) {
494*0Sstevel@tonic-gate 		idcx = scrco;
495*0Sstevel@tonic-gate 	} else
496*0Sstevel@tonic-gate 		if (!marks)
497*0Sstevel@tonic-gate 			idcx = -1;
498*0Sstevel@tonic-gate 		else {
499*0Sstevel@tonic-gate 			/* on cookie term, only do idch where no attrs */
500*0Sstevel@tonic-gate 			/* are used */
501*0Sstevel@tonic-gate 			for (idcx = scrco - 1, wp = wcp + idcx; idcx >= wx;
502*0Sstevel@tonic-gate 			    --idcx, --wp)
503*0Sstevel@tonic-gate 				if (_ATTR(*wp) || _ISMARK1(wy, idcx) ||
504*0Sstevel@tonic-gate 				    _ISMARK2(wy, idcx))
505*0Sstevel@tonic-gate 					break;
506*0Sstevel@tonic-gate 			if (idcx >= scrco - SLACK)
507*0Sstevel@tonic-gate 				idcx = scrco;
508*0Sstevel@tonic-gate 		}
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	if (idcx < lastx && endns >= lastx)
511*0Sstevel@tonic-gate 		lastx = scrco;
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	/* max amount of insert allow */
514*0Sstevel@tonic-gate 	if (idcx == scrco || !SP->ichok)
515*0Sstevel@tonic-gate 		maxi = 0;
516*0Sstevel@tonic-gate 	else
517*0Sstevel@tonic-gate 		if (lastx == scrco)
518*0Sstevel@tonic-gate 			maxi = scrco;
519*0Sstevel@tonic-gate 		else
520*0Sstevel@tonic-gate 			maxi = lastx - (endns + 1);
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 	/* go */
523*0Sstevel@tonic-gate 	wcp += wx;
524*0Sstevel@tonic-gate 	scp += wx;
525*0Sstevel@tonic-gate 	didvideo = changed = FALSE;
526*0Sstevel@tonic-gate 	didcolor = (color_marks) ? FALSE : TRUE;
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 	while (wx < lastx) {
529*0Sstevel@tonic-gate 		/* skip things that are already right */
530*0Sstevel@tonic-gate 		if (!redraw) {
531*0Sstevel@tonic-gate 			multi_col = 0;
532*0Sstevel@tonic-gate 			wx_sav = wx;
533*0Sstevel@tonic-gate 			for (; wx < lastx; ++wx, ++wcp, ++scp)
534*0Sstevel@tonic-gate 				if (*wcp != *scp)
535*0Sstevel@tonic-gate 					break;
536*0Sstevel@tonic-gate 			if (wx >= lastx)
537*0Sstevel@tonic-gate 				goto done;
538*0Sstevel@tonic-gate 			for (; wx > wx_sav; --wx, --wcp, --scp) {
539*0Sstevel@tonic-gate 				if (!ISCBIT(*wcp) && !ISCBIT(*scp))
540*0Sstevel@tonic-gate 					break;
541*0Sstevel@tonic-gate 				multi_col = 1;
542*0Sstevel@tonic-gate 			}
543*0Sstevel@tonic-gate 		}
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate 		/* try clear-bol, we'll assume exclusive clr_bol */
546*0Sstevel@tonic-gate 		if (!changed && !marks && clr_bol && blnkx > wx &&
547*0Sstevel@tonic-gate 		    begns >= wx) {
548*0Sstevel@tonic-gate 			for (x = wx, wp = wcp; x < lastx; ++x, ++wp)
549*0Sstevel@tonic-gate 				if (_DARKCHAR(*wp))
550*0Sstevel@tonic-gate 					break;
551*0Sstevel@tonic-gate 			/* clearing only whole screen characters */
552*0Sstevel@tonic-gate 			for (sp = scp+(x-wx); x >= wx; --x, --sp)
553*0Sstevel@tonic-gate 				if (!ISCBIT(*sp))
554*0Sstevel@tonic-gate 					break;
555*0Sstevel@tonic-gate 			x -= 1;
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 			if ((x - (redraw ? 0 : begns)) > _COST(Clr_bol)) {
558*0Sstevel@tonic-gate 				(void) mvcur(cy, cx, wy, x);
559*0Sstevel@tonic-gate 				/* MORE?: colors - mvcur will shuts of */
560*0Sstevel@tonic-gate 				/* colors when msgr is not defined */
561*0Sstevel@tonic-gate 
562*0Sstevel@tonic-gate /* SS: colors */
563*0Sstevel@tonic-gate 				if (back_color_erase)
564*0Sstevel@tonic-gate 					_turn_off_background();
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 				_PUTS(clr_bol, 1);
567*0Sstevel@tonic-gate 				/* LINTED */
568*0Sstevel@tonic-gate 				cy = (short) wy;
569*0Sstevel@tonic-gate 				/* LINTED */
570*0Sstevel@tonic-gate 				cx = (short) x;
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 				mtch = x - wx;
573*0Sstevel@tonic-gate 				(void) memcpy((char *) scp, (char *) wcp,
574*0Sstevel@tonic-gate 				    (mtch * sizeof (chtype)));
575*0Sstevel@tonic-gate 				wcp += mtch;
576*0Sstevel@tonic-gate 				scp += mtch;
577*0Sstevel@tonic-gate 				wx = x;
578*0Sstevel@tonic-gate 			}
579*0Sstevel@tonic-gate 		}
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 		/* screen image is changing */
582*0Sstevel@tonic-gate 		changed = TRUE;
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate 		/* move to the point to start refresh */
585*0Sstevel@tonic-gate 		if (cy != wy || cx != wx)
586*0Sstevel@tonic-gate 			(void) mvcur(cy, cx, wy, wx);
587*0Sstevel@tonic-gate 		/* LINTED */
588*0Sstevel@tonic-gate 		cy = (short) wy;
589*0Sstevel@tonic-gate 		/* LINTED */
590*0Sstevel@tonic-gate 		cx = (short) wx;
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 		/* update screen image */
593*0Sstevel@tonic-gate 		while (wx < lastx) {
594*0Sstevel@tonic-gate 			wc = *wcp;
595*0Sstevel@tonic-gate 			sc = *scp;
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 			if (!redraw && !multi_col && wc == sc)
598*0Sstevel@tonic-gate 				break;
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate 			/* real video attributes */
601*0Sstevel@tonic-gate 			if (marks)
602*0Sstevel@tonic-gate 				curscr->_attrs = _ATTR(sc);
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 			/* blanks only */
605*0Sstevel@tonic-gate 			if (wx > blnkx) {
606*0Sstevel@tonic-gate /* SS: colors */
607*0Sstevel@tonic-gate 				if (back_color_erase)
608*0Sstevel@tonic-gate 					_turn_off_background();
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate 				_PUTS(clr_eol, 1);
611*0Sstevel@tonic-gate 				/* LINTED */
612*0Sstevel@tonic-gate 				curscr->_curx = (short) wx;
613*0Sstevel@tonic-gate 		 		/* LINTED */
614*0Sstevel@tonic-gate 				curscr->_cury = (short) wy;
615*0Sstevel@tonic-gate 				(void) wclrtoeol(curscr);
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 				if (marks && wx > 0 && _ATTR(*(scp - 1)) !=
618*0Sstevel@tonic-gate 				    A_NORMAL) {
619*0Sstevel@tonic-gate 					_VIDS(A_NORMAL, _ATTR(*(scp - 1)));
620*0Sstevel@tonic-gate 					if (_VIDEO(*scp - 1))
621*0Sstevel@tonic-gate 						_setmark1(wy, wx, NULL);
622*0Sstevel@tonic-gate 					if (_COLOR(*scp - 1))
623*0Sstevel@tonic-gate 						_setmark2(wy, wx, NULL);
624*0Sstevel@tonic-gate 				}
625*0Sstevel@tonic-gate 				goto done;
626*0Sstevel@tonic-gate 			}
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate 			/* try insert/delete chars */
629*0Sstevel@tonic-gate 			if (wx > idcx && !ISCBIT(*scp) &&
630*0Sstevel@tonic-gate 			    (mtch = _useidch(wcp, scp, lastx - wx,
631*0Sstevel@tonic-gate 			    maxi, &idch))) {
632*0Sstevel@tonic-gate 				maxi -= idch;
633*0Sstevel@tonic-gate 				wx += mtch;
634*0Sstevel@tonic-gate 				scp += mtch;
635*0Sstevel@tonic-gate 				wcp += mtch;
636*0Sstevel@tonic-gate 				break;
637*0Sstevel@tonic-gate 			}
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 			/* about to output chars, make sure insert */
640*0Sstevel@tonic-gate 			/* mode is off */
641*0Sstevel@tonic-gate 			if (SP->phys_irm)
642*0Sstevel@tonic-gate 				_OFFINSERT();
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 			/* color and video attributes */
645*0Sstevel@tonic-gate 			if (_ATTR(wc) != curscr->_attrs) {
646*0Sstevel@tonic-gate 			    bool  color_change = FALSE;
647*0Sstevel@tonic-gate 			    bool  video_change = FALSE;
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 			    if (marks)
650*0Sstevel@tonic-gate 				if (_VIDEO(wc) != _VIDEO(curscr->_attrs))
651*0Sstevel@tonic-gate 					video_change = TRUE;
652*0Sstevel@tonic-gate 				if (color_marks)
653*0Sstevel@tonic-gate 				    if (_COLOR(wc) != _COLOR(curscr->_attrs))
654*0Sstevel@tonic-gate 					color_change = TRUE;
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate 				/* the following may occurs when, for */
657*0Sstevel@tonic-gate 				/* example the application */
658*0Sstevel@tonic-gate 				/* is written for color terminal and then */
659*0Sstevel@tonic-gate 				/* run on a monocrome  */
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate 				if (marks && !video_change && !color_change)
662*0Sstevel@tonic-gate 					goto no_change;
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 				/* prevent spilling out of line */
665*0Sstevel@tonic-gate 				if (marks && !(didcolor && didvideo)) {
666*0Sstevel@tonic-gate 				    if ((video_change && !_ISMARK1(wy,
667*0Sstevel@tonic-gate 					video_attrx)) || (color_change &&
668*0Sstevel@tonic-gate 					!_ISMARK2(wy, color_attrx))) {
669*0Sstevel@tonic-gate 					    int    tempx;
670*0Sstevel@tonic-gate 					    chtype sa = curscr->_attrs;
671*0Sstevel@tonic-gate 					    bool   first  = FALSE;
672*0Sstevel@tonic-gate 					    bool   second = FALSE;
673*0Sstevel@tonic-gate 
674*0Sstevel@tonic-gate 					    if (!didvideo && video_change &&
675*0Sstevel@tonic-gate 						!_ISMARK1(wy, video_attrx)) {
676*0Sstevel@tonic-gate 						didvideo = TRUE;
677*0Sstevel@tonic-gate 						(void) mvcur(wy, wx,
678*0Sstevel@tonic-gate 						    wy, video_attrx);
679*0Sstevel@tonic-gate 						_VIDS(_VIDEO(_virtscr->_y[wy]
680*0Sstevel@tonic-gate 						    [video_attrx]),
681*0Sstevel@tonic-gate 						    _VIDEO(_virtscr->_y[wy]
682*0Sstevel@tonic-gate 						    [video_attrx-1]));
683*0Sstevel@tonic-gate 						_setmark1(wy, video_attrx,
684*0Sstevel@tonic-gate 						    NULL);
685*0Sstevel@tonic-gate 						first = TRUE;
686*0Sstevel@tonic-gate 					    }
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 				if (!didcolor && color_change &&
689*0Sstevel@tonic-gate 				    !_ISMARK2(wy, color_attrx)) {
690*0Sstevel@tonic-gate 					didcolor = TRUE;
691*0Sstevel@tonic-gate 					tempx = first ? video_attrx : wx;
692*0Sstevel@tonic-gate 					if (tempx != color_attrx)
693*0Sstevel@tonic-gate 						(void) mvcur(wy, tempx, wy,
694*0Sstevel@tonic-gate 						    color_attrx);
695*0Sstevel@tonic-gate 				/*
696*0Sstevel@tonic-gate 				 * sc = _COLOR(curscr->_y[wy][color_attrx]);
697*0Sstevel@tonic-gate 				 *_VIDS(sc, (~sc & A_COLOR));
698*0Sstevel@tonic-gate 				*/
699*0Sstevel@tonic-gate 					_VIDS(_COLOR(_virtscr->_y[wy]
700*0Sstevel@tonic-gate 					    [color_attrx]),
701*0Sstevel@tonic-gate 					    _COLOR(_virtscr->_y[wy]
702*0Sstevel@tonic-gate 					    [color_attrx-1]));
703*0Sstevel@tonic-gate 					_setmark2(wy, color_attrx, NULL);
704*0Sstevel@tonic-gate 					second = TRUE;
705*0Sstevel@tonic-gate 				}
706*0Sstevel@tonic-gate 				(void) mvcur(wy, (second ? color_attrx :
707*0Sstevel@tonic-gate 				    video_attrx), wy, wx);
708*0Sstevel@tonic-gate 				curscr->_attrs = sa;
709*0Sstevel@tonic-gate 			    }
710*0Sstevel@tonic-gate 			}
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 			_VIDS(_ATTR(wc), curscr->_attrs);
713*0Sstevel@tonic-gate 
714*0Sstevel@tonic-gate 			/* on cookie terminals mark the interval */
715*0Sstevel@tonic-gate 			if (video_change)
716*0Sstevel@tonic-gate 				_setmark1(wy, wx, scp);
717*0Sstevel@tonic-gate 			if (color_change)
718*0Sstevel@tonic-gate 				_setmark2(wy, wx, scp);
719*0Sstevel@tonic-gate 		}
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 		/* end-of-line */
722*0Sstevel@tonic-gate no_change:
723*0Sstevel@tonic-gate 		x = 1;
724*0Sstevel@tonic-gate 		if (_scrmax > 1)
725*0Sstevel@tonic-gate 			x = _curs_scrwidth[TYPE(RBYTE(wc))];
726*0Sstevel@tonic-gate 		if (wx == scrco - x) {
727*0Sstevel@tonic-gate 			_rmargin(wx);
728*0Sstevel@tonic-gate 			goto done;
729*0Sstevel@tonic-gate 		}
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 		if (transparent_underline && erase_overstrike &&
732*0Sstevel@tonic-gate 		    _CHAR(wc) == '_') {
733*0Sstevel@tonic-gate 			(void) _outch(' ');
734*0Sstevel@tonic-gate 			(void) mvcur(wy, wx + 1, wy, wx);
735*0Sstevel@tonic-gate 		}
736*0Sstevel@tonic-gate 
737*0Sstevel@tonic-gate 		/* put out the character */
738*0Sstevel@tonic-gate 		(void) _outwch(tilde_glitch && _CHAR(wc) == '~' ? '`' : wc);
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 		*scp++ = wc;
741*0Sstevel@tonic-gate 		wcp++;
742*0Sstevel@tonic-gate 		wx++;
743*0Sstevel@tonic-gate 		cx++;
744*0Sstevel@tonic-gate 		/* output entire multi-byte chars */
745*0Sstevel@tonic-gate 		while (wx < lastx && ISCBIT(*wcp)) {
746*0Sstevel@tonic-gate 			(void) _outwch(*wcp);
747*0Sstevel@tonic-gate 			*scp++ = *wcp++;
748*0Sstevel@tonic-gate 			wx++;
749*0Sstevel@tonic-gate 			cx++;
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 		}
755*0Sstevel@tonic-gate 		}
756*0Sstevel@tonic-gate 	}
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate done:
759*0Sstevel@tonic-gate 	if (changed) {
760*0Sstevel@tonic-gate 		/* update the blank structure */
761*0Sstevel@tonic-gate 		for (wx = 0, scp = curscr->_y[wy]; wx < scrco; ++wx, ++scp)
762*0Sstevel@tonic-gate 			if (_DARKCHAR(*scp))
763*0Sstevel@tonic-gate 				break;
764*0Sstevel@tonic-gate 		/* LINTED */
765*0Sstevel@tonic-gate 		_BEGNS[wy] = (short) wx;
766*0Sstevel@tonic-gate 		if (wx == scrco)
767*0Sstevel@tonic-gate 			_ENDNS[wy] = -1;
768*0Sstevel@tonic-gate 		else {
769*0Sstevel@tonic-gate 			wx = scrco - 1;
770*0Sstevel@tonic-gate 			scp = curscr->_y[wy] + wx;
771*0Sstevel@tonic-gate 			for (; wx >= 0; --wx, --scp)
772*0Sstevel@tonic-gate 				if (_DARKCHAR(*scp))
773*0Sstevel@tonic-gate 					break;
774*0Sstevel@tonic-gate 			/* LINTED */
775*0Sstevel@tonic-gate 			_ENDNS[wy] = (short) wx;
776*0Sstevel@tonic-gate 		}
777*0Sstevel@tonic-gate 
778*0Sstevel@tonic-gate 		/* update the hash structure */
779*0Sstevel@tonic-gate 		_CURHASH[wy] = _BEGNS[wy] < scrco ? _NOHASH : 0;
780*0Sstevel@tonic-gate 	}
781*0Sstevel@tonic-gate }
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate /*
784*0Sstevel@tonic-gate  * See if a left or right shift is apppropriate
785*0Sstevel@tonic-gate  * This routine is called only if !cookie_glitch or no video attributes
786*0Sstevel@tonic-gate  * are used in the affected part.
787*0Sstevel@tonic-gate  * The main idea is to find a longest common substring which is a
788*0Sstevel@tonic-gate  * prefix of one of 'wcp' or 'scp', then either delete or
789*0Sstevel@tonic-gate  * insert depending on where the prefix is.
790*0Sstevel@tonic-gate  *
791*0Sstevel@tonic-gate  * wcp : what we want the screen to look like
792*0Sstevel@tonic-gate  * scp : what the screen looks like now
793*0Sstevel@tonic-gate  * length: the length to be updated
794*0Sstevel@tonic-gate  * maxi: maximum possible insert amount
795*0Sstevel@tonic-gate  * id; *id returns the amount of insert/delete
796*0Sstevel@tonic-gate  *
797*0Sstevel@tonic-gate  * Return the number of chars matched after the shift.
798*0Sstevel@tonic-gate  */
799*0Sstevel@tonic-gate 
800*0Sstevel@tonic-gate static int
_useidch(chtype * wcp,chtype * scp,int length,int maxi,int * id)801*0Sstevel@tonic-gate _useidch(chtype *wcp, chtype *scp, int length, int maxi, int *id)
802*0Sstevel@tonic-gate {
803*0Sstevel@tonic-gate 	int	x1, x2, blnk, idch, cost, cost_ich1, match;
804*0Sstevel@tonic-gate 	chtype	wc;
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate 	/* try deletion */
807*0Sstevel@tonic-gate 	if (SP->dchok && _CHAR(*wcp) != ' ') {
808*0Sstevel@tonic-gate 		if ((match = _prefix(wcp, scp, length, length / 2, &idch)) > 0)
809*0Sstevel@tonic-gate 			cost = _COST(dcfixed) + (parm_dch ? _COST(Parm_dch) :
810*0Sstevel@tonic-gate 			    _COST(Delete_character) * idch);
811*0Sstevel@tonic-gate 		else
812*0Sstevel@tonic-gate 			cost = _INFINITY;
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate 		if (match >= cost) {
815*0Sstevel@tonic-gate /* SS: colors */
816*0Sstevel@tonic-gate 			if (back_color_erase)
817*0Sstevel@tonic-gate 				_turn_off_background();
818*0Sstevel@tonic-gate 
819*0Sstevel@tonic-gate 			if (SP->dmode) {
820*0Sstevel@tonic-gate 				if (SP->sid_equal) {
821*0Sstevel@tonic-gate 					if (!(SP->phys_irm))
822*0Sstevel@tonic-gate 						_ONINSERT();
823*0Sstevel@tonic-gate 				} else {
824*0Sstevel@tonic-gate 					if (SP->phys_irm)
825*0Sstevel@tonic-gate 						_OFFINSERT();
826*0Sstevel@tonic-gate 					_PUTS(enter_delete_mode, 1);
827*0Sstevel@tonic-gate 				}
828*0Sstevel@tonic-gate 			}
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 			if (parm_dch)
831*0Sstevel@tonic-gate 				_PUTS(tparm_p1(parm_dch, idch), 1);
832*0Sstevel@tonic-gate 			else
833*0Sstevel@tonic-gate 				for (x1 = 0; x1 < idch; ++x1)
834*0Sstevel@tonic-gate 					_PUTS(delete_character, 1);
835*0Sstevel@tonic-gate 
836*0Sstevel@tonic-gate 			if (SP->dmode) {
837*0Sstevel@tonic-gate 				if (SP->eid_equal)
838*0Sstevel@tonic-gate 					SP->phys_irm = FALSE;
839*0Sstevel@tonic-gate 				_PUTS(exit_delete_mode, 1);
840*0Sstevel@tonic-gate 			}
841*0Sstevel@tonic-gate 
842*0Sstevel@tonic-gate 			/* update screen image */
843*0Sstevel@tonic-gate 			for (x1 = 0, x2 = idch; x2 < length; ++x1, ++x2)
844*0Sstevel@tonic-gate 				scp[x1] = scp[x2];
845*0Sstevel@tonic-gate 			for (; x1 < length; ++x1)
846*0Sstevel@tonic-gate 				scp[x1] = ' ';
847*0Sstevel@tonic-gate 
848*0Sstevel@tonic-gate 			*id = -idch;
849*0Sstevel@tonic-gate 			return (match);
850*0Sstevel@tonic-gate 		}
851*0Sstevel@tonic-gate 	}
852*0Sstevel@tonic-gate 
853*0Sstevel@tonic-gate 	/* no insertion wanted or possible */
854*0Sstevel@tonic-gate 	if (!(SP->ichok) || _CHAR(*scp) == ' ')
855*0Sstevel@tonic-gate 		return (0);
856*0Sstevel@tonic-gate 
857*0Sstevel@tonic-gate 	/* see if insertion is worth it */
858*0Sstevel@tonic-gate 	maxi = (idch = length / 2) < maxi ? idch : maxi;
859*0Sstevel@tonic-gate 	if ((match = _prefix(scp, wcp, length, maxi, &idch)) <= 0)
860*0Sstevel@tonic-gate 		return (0);
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate 	/* see if inserting blanks only */
863*0Sstevel@tonic-gate 	for (blnk = 0; blnk < idch; ++blnk)
864*0Sstevel@tonic-gate 		if (wcp[blnk] != ' ') {
865*0Sstevel@tonic-gate 			blnk = 0;
866*0Sstevel@tonic-gate 			break;
867*0Sstevel@tonic-gate 		}
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate 	/* see if doing insertion is worth it */
870*0Sstevel@tonic-gate 	cost_ich1 = idch * _COST(Insert_character);
871*0Sstevel@tonic-gate 	if (SP->imode) {
872*0Sstevel@tonic-gate 		cost = SP->phys_irm ? 0 : _COST(icfixed);
873*0Sstevel@tonic-gate 		if (blnk > _COST(Parm_ich) && _COST(Parm_ich) < cost_ich1)
874*0Sstevel@tonic-gate 			cost += _COST(Parm_ich);
875*0Sstevel@tonic-gate 		else
876*0Sstevel@tonic-gate 			if (insert_character)
877*0Sstevel@tonic-gate 				cost += cost_ich1;
878*0Sstevel@tonic-gate 	} else {
879*0Sstevel@tonic-gate 		if (parm_ich && _COST(Parm_ich) < cost_ich1)
880*0Sstevel@tonic-gate 			cost = _COST(Parm_ich);
881*0Sstevel@tonic-gate 		else
882*0Sstevel@tonic-gate 			cost = cost_ich1;
883*0Sstevel@tonic-gate 	}
884*0Sstevel@tonic-gate 	if ((cost - blnk) > match)
885*0Sstevel@tonic-gate 		return (0);
886*0Sstevel@tonic-gate 
887*0Sstevel@tonic-gate 	/* perform the insertions */
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate 	/* SS: colors */
890*0Sstevel@tonic-gate 	if (back_color_erase)
891*0Sstevel@tonic-gate 		_turn_off_background();
892*0Sstevel@tonic-gate 
893*0Sstevel@tonic-gate 	if (SP->imode) {
894*0Sstevel@tonic-gate 		if (!SP->phys_irm)
895*0Sstevel@tonic-gate 			_ONINSERT();
896*0Sstevel@tonic-gate 		if (blnk > _COST(Parm_ich) && _COST(Parm_ich) < cost_ich1)
897*0Sstevel@tonic-gate 			_PUTS(tparm_p1(parm_ich, idch), 1);
898*0Sstevel@tonic-gate 		else
899*0Sstevel@tonic-gate 			if (insert_character)
900*0Sstevel@tonic-gate 				goto do_insert_char;
901*0Sstevel@tonic-gate 			else
902*0Sstevel@tonic-gate 				/* so that we'll do real char insertions */
903*0Sstevel@tonic-gate 				blnk = 0;
904*0Sstevel@tonic-gate 	} else {
905*0Sstevel@tonic-gate 		if (parm_ich && _COST(Parm_ich) < cost_ich1)
906*0Sstevel@tonic-gate 			_PUTS(tparm_p1(parm_ich, idch), 1);
907*0Sstevel@tonic-gate 		else {
908*0Sstevel@tonic-gate do_insert_char:
909*0Sstevel@tonic-gate 			for (x1 = 0; x1 < idch; ++x1)
910*0Sstevel@tonic-gate 				_PUTS(insert_character, 1);
911*0Sstevel@tonic-gate 		}
912*0Sstevel@tonic-gate 	}
913*0Sstevel@tonic-gate 
914*0Sstevel@tonic-gate 	/* inserting desired characters */
915*0Sstevel@tonic-gate 	if (!blnk)
916*0Sstevel@tonic-gate 		for (x1 = 0; x1 < idch; ++x1) {
917*0Sstevel@tonic-gate 			wc = wcp[x1];
918*0Sstevel@tonic-gate 			if (_ATTR(wc) != curscr->_attrs)
919*0Sstevel@tonic-gate 				_VIDS(_ATTR(wc), curscr->_attrs);
920*0Sstevel@tonic-gate 			(void) _outwch(_CHAR(wc) == '~' &&
921*0Sstevel@tonic-gate 			    tilde_glitch ? '`' : wc);
922*0Sstevel@tonic-gate 			++cx;
923*0Sstevel@tonic-gate 		}
924*0Sstevel@tonic-gate 
925*0Sstevel@tonic-gate 	/* update the screen image */
926*0Sstevel@tonic-gate 	for (x1 = length - 1, x2 = length - idch - 1; x2 >= 0; --x1, --x2)
927*0Sstevel@tonic-gate 		scp[x1] = scp[x2];
928*0Sstevel@tonic-gate 	(void) memcpy((char *) scp, (char *) wcp, idch * sizeof (chtype));
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate 	*id = idch;
931*0Sstevel@tonic-gate 	return (match + idch);
932*0Sstevel@tonic-gate }
933*0Sstevel@tonic-gate 
934*0Sstevel@tonic-gate /*
935*0Sstevel@tonic-gate  * Find a substring of s2 that match a prefix of s1.
936*0Sstevel@tonic-gate  * The substring is such that:
937*0Sstevel@tonic-gate  * 	1. it does not start with an element
938*0Sstevel@tonic-gate  *	   that is in perfect alignment with one in s1 and
939*0Sstevel@tonic-gate  * 	2: it is at least as long as the displacement.
940*0Sstevel@tonic-gate  *
941*0Sstevel@tonic-gate  * length: the length of s1, s2.
942*0Sstevel@tonic-gate  * maxs: only search for match in [1,maxs]  of s2.
943*0Sstevel@tonic-gate  * begm: *begm returns where the match begins.
944*0Sstevel@tonic-gate  *
945*0Sstevel@tonic-gate  * Return the number of matches.
946*0Sstevel@tonic-gate  */
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate static int
_prefix(chtype * s1,chtype * s2,int length,int maxs,int * begm)949*0Sstevel@tonic-gate _prefix(chtype *s1, chtype *s2, int length, int maxs, int *begm)
950*0Sstevel@tonic-gate {
951*0Sstevel@tonic-gate 	int	m, n, k;
952*0Sstevel@tonic-gate 
953*0Sstevel@tonic-gate 	n = 0;
954*0Sstevel@tonic-gate 	for (m = 1; m <= maxs; ++m)
955*0Sstevel@tonic-gate 		/* testing for s1[m] != s2[m] is condition 1 */
956*0Sstevel@tonic-gate 		if (s1[0] == s2[m] && s1[m] != s2[m]) {
957*0Sstevel@tonic-gate 			/* see if it's long enough (condition 2) */
958*0Sstevel@tonic-gate 			for (k = 2 * m - 1; k > m; --k)
959*0Sstevel@tonic-gate 				if (s1[k - m] != s2[k])
960*0Sstevel@tonic-gate 					break;
961*0Sstevel@tonic-gate 			/* found a match with a good length */
962*0Sstevel@tonic-gate 			if (k == m) {
963*0Sstevel@tonic-gate 				*begm = m;
964*0Sstevel@tonic-gate 
965*0Sstevel@tonic-gate 				/* count the # of matches */
966*0Sstevel@tonic-gate 				s2 += m;
967*0Sstevel@tonic-gate 				length -= m;
968*0Sstevel@tonic-gate 				for (n = m; n < length; ++n)
969*0Sstevel@tonic-gate 					if (s1[n] != s2[n])
970*0Sstevel@tonic-gate 						break;
971*0Sstevel@tonic-gate 				goto done;
972*0Sstevel@tonic-gate 			}
973*0Sstevel@tonic-gate 		}
974*0Sstevel@tonic-gate 
975*0Sstevel@tonic-gate done:
976*0Sstevel@tonic-gate 	return (n);
977*0Sstevel@tonic-gate }
978*0Sstevel@tonic-gate 
979*0Sstevel@tonic-gate /* Set video markers for cookie terminal. */
980*0Sstevel@tonic-gate 
981*0Sstevel@tonic-gate static void
_setmark1(int y,int x,chtype * s)982*0Sstevel@tonic-gate _setmark1(int y, int x, chtype *s)
983*0Sstevel@tonic-gate {
984*0Sstevel@tonic-gate 	long	a;
985*0Sstevel@tonic-gate 
986*0Sstevel@tonic-gate 	/* set the mark map */
987*0Sstevel@tonic-gate 	marks[y][x / BITSPERBYTE] |= (1 << (x % BITSPERBYTE));
988*0Sstevel@tonic-gate 
989*0Sstevel@tonic-gate 	if (s) {
990*0Sstevel@tonic-gate 		a  = _VIDEO(curscr->_attrs);
991*0Sstevel@tonic-gate 
992*0Sstevel@tonic-gate 		/* set the video attr of the first char here */
993*0Sstevel@tonic-gate 		/* LINTED */
994*0Sstevel@tonic-gate 		*s = _CHAR(*s) | _COLOR(*s) | a;
995*0Sstevel@tonic-gate 
996*0Sstevel@tonic-gate 		/* now the video attr of the rest of the affected interval */
997*0Sstevel@tonic-gate 		for (x += 1, s += 1; x < scrco; ++x, ++s)
998*0Sstevel@tonic-gate 			if (_ISMARK1(y, x))
999*0Sstevel@tonic-gate 				break;
1000*0Sstevel@tonic-gate 			else
1001*0Sstevel@tonic-gate 				/* LINTED */
1002*0Sstevel@tonic-gate 				*s = _CHAR(*s) | _COLOR(*s) | a;
1003*0Sstevel@tonic-gate 	}
1004*0Sstevel@tonic-gate }
1005*0Sstevel@tonic-gate 
1006*0Sstevel@tonic-gate /* Set color markers for cookie terminal. */
1007*0Sstevel@tonic-gate 
1008*0Sstevel@tonic-gate static void
_setmark2(int y,int x,chtype * s)1009*0Sstevel@tonic-gate _setmark2(int y, int x, chtype *s)
1010*0Sstevel@tonic-gate {
1011*0Sstevel@tonic-gate 	long	a;
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 	/* set the mark map */
1014*0Sstevel@tonic-gate 	color_marks[y][x / BITSPERBYTE] |= (1 << (x % BITSPERBYTE));
1015*0Sstevel@tonic-gate 
1016*0Sstevel@tonic-gate 	if (s) {
1017*0Sstevel@tonic-gate 		a  = _COLOR(curscr->_attrs);
1018*0Sstevel@tonic-gate 
1019*0Sstevel@tonic-gate 		/* set the video attr of the first char here */
1020*0Sstevel@tonic-gate 		/* LINTED */
1021*0Sstevel@tonic-gate 		*s = _CHAR(*s) | _VIDEO(*s) | a;
1022*0Sstevel@tonic-gate 
1023*0Sstevel@tonic-gate 		/* now the video attr of the rest of the affected interval */
1024*0Sstevel@tonic-gate 		for (x += 1, s += 1; x < scrco; ++x, ++s)
1025*0Sstevel@tonic-gate 			if (_ISMARK2(y, x))
1026*0Sstevel@tonic-gate 				break;
1027*0Sstevel@tonic-gate 			else
1028*0Sstevel@tonic-gate 				/* LINTED */
1029*0Sstevel@tonic-gate 				*s = _CHAR(*s) | _VIDEO(*s) | a;
1030*0Sstevel@tonic-gate 	}
1031*0Sstevel@tonic-gate }
1032*0Sstevel@tonic-gate 
1033*0Sstevel@tonic-gate 
1034*0Sstevel@tonic-gate /* At the right margin various weird things can happen.  We treat them here. */
1035*0Sstevel@tonic-gate 
1036*0Sstevel@tonic-gate /* At the right margin various weird things can happen.  We treat them here. */
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate static void
_rmargin(int wx)1039*0Sstevel@tonic-gate _rmargin(int wx)
1040*0Sstevel@tonic-gate {
1041*0Sstevel@tonic-gate 	int	x, w, ix;
1042*0Sstevel@tonic-gate 	chtype	sc;
1043*0Sstevel@tonic-gate 	chtype	*wcp =	_virtscr->_y[cy];
1044*0Sstevel@tonic-gate 
1045*0Sstevel@tonic-gate 	/* screen may scroll */
1046*0Sstevel@tonic-gate 	if (cy == scrli - 1) {
1047*0Sstevel@tonic-gate 		/* can't do anything */
1048*0Sstevel@tonic-gate 		if (!SP->ichok)
1049*0Sstevel@tonic-gate 			return;
1050*0Sstevel@tonic-gate 
1051*0Sstevel@tonic-gate 		/* the width of the new character */
1052*0Sstevel@tonic-gate 		w = _curs_scrwidth[TYPE(RBYTE(wcp[wx]))];
1053*0Sstevel@tonic-gate 		/* the place to put it without causing scrolling */
1054*0Sstevel@tonic-gate 		for (x = wx - 1; x > 0; --x)
1055*0Sstevel@tonic-gate 			if (!ISCBIT(wcp[x]))
1056*0Sstevel@tonic-gate 				break;
1057*0Sstevel@tonic-gate 		sc = curscr->_y[cy][x];
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate 		(void) mvcur(cy, cx, cy, x);
1060*0Sstevel@tonic-gate 		if (_ATTR(wcp[wx]) != curscr->_attrs)
1061*0Sstevel@tonic-gate 			_VIDS(_ATTR(wcp[wx]), curscr->_attrs);
1062*0Sstevel@tonic-gate 		(void) _outwch(tilde_glitch &&
1063*0Sstevel@tonic-gate 		    _CHAR(wcp[wx]) == '~' ? '`' : wcp[wx]);
1064*0Sstevel@tonic-gate 
1065*0Sstevel@tonic-gate 		for (ix = wx + 1; ix < scrco; ++ix) {
1066*0Sstevel@tonic-gate 			(void) _outwch(wcp[ix]);
1067*0Sstevel@tonic-gate 		}
1068*0Sstevel@tonic-gate 
1069*0Sstevel@tonic-gate 		/* insert sc back in and push wcp[wx] right */
1070*0Sstevel@tonic-gate 		(void) mvcur(cy, x+w, cy, x);
1071*0Sstevel@tonic-gate 
1072*0Sstevel@tonic-gate 		/* SS: colors */
1073*0Sstevel@tonic-gate 		if (back_color_erase)
1074*0Sstevel@tonic-gate 			_turn_off_background();
1075*0Sstevel@tonic-gate 
1076*0Sstevel@tonic-gate 		if (SP->imode && !SP->phys_irm)
1077*0Sstevel@tonic-gate 			_ONINSERT();
1078*0Sstevel@tonic-gate 		/* width of the old character that was overwritten */
1079*0Sstevel@tonic-gate 		w = _curs_scrwidth[TYPE(RBYTE(curscr->_y[cy][x]))];
1080*0Sstevel@tonic-gate 
1081*0Sstevel@tonic-gate 		if (insert_character)
1082*0Sstevel@tonic-gate 			for (ix = 0; ix < w; ++ix)
1083*0Sstevel@tonic-gate 				_PUTS(insert_character, 1);
1084*0Sstevel@tonic-gate 		else
1085*0Sstevel@tonic-gate 			if (parm_ich && !SP->imode)
1086*0Sstevel@tonic-gate 				_PUTS(tparm_p1(parm_ich, w), 1);
1087*0Sstevel@tonic-gate 
1088*0Sstevel@tonic-gate 		if (_ATTR(sc) != curscr->_attrs)
1089*0Sstevel@tonic-gate 			_VIDS(_ATTR(sc), curscr->_attrs);
1090*0Sstevel@tonic-gate 		for (ix = x; w > 0; --w, ++ix)
1091*0Sstevel@tonic-gate 			(void) _outwch(curscr->_y[cy][ix]);
1092*0Sstevel@tonic-gate 
1093*0Sstevel@tonic-gate 		/* make sure the video attrs are ok */
1094*0Sstevel@tonic-gate 		if (marks && (_ATTR(sc) || _ATTR(wcp[wx])))
1095*0Sstevel@tonic-gate 			_VIDS(_ATTR(wcp[wx]), ~_ATTR(sc));
1096*0Sstevel@tonic-gate 
1097*0Sstevel@tonic-gate 		/* update screen image */
1098*0Sstevel@tonic-gate 		/* LINTED */
1099*0Sstevel@tonic-gate 		cx = (short) wx;
1100*0Sstevel@tonic-gate 		curscr->_y[cy][wx] = wcp[wx];
1101*0Sstevel@tonic-gate 		for (x = wx + 1; x < scrco; ++x) {
1102*0Sstevel@tonic-gate 			(void) _outwch(wcp[x]);
1103*0Sstevel@tonic-gate 			curscr->_y[cy][x] = wcp[x];
1104*0Sstevel@tonic-gate 		}
1105*0Sstevel@tonic-gate 		return;
1106*0Sstevel@tonic-gate 	}
1107*0Sstevel@tonic-gate 
1108*0Sstevel@tonic-gate 	/* put char out and update screen image */
1109*0Sstevel@tonic-gate 	(void) _outwch(tilde_glitch && _CHAR(wcp[wx]) == '~' ? '`' : wcp[wx]);
1110*0Sstevel@tonic-gate 
1111*0Sstevel@tonic-gate 
1112*0Sstevel@tonic-gate 
1113*0Sstevel@tonic-gate 
1114*0Sstevel@tonic-gate 
1115*0Sstevel@tonic-gate 
1116*0Sstevel@tonic-gate 
1117*0Sstevel@tonic-gate 
1118*0Sstevel@tonic-gate 	curscr->_y[cy][wx] = wcp[wx];
1119*0Sstevel@tonic-gate 
1120*0Sstevel@tonic-gate 	for (x = wx + 1; x < scrco; ++x) {
1121*0Sstevel@tonic-gate 		(void) _outwch(wcp[x]);
1122*0Sstevel@tonic-gate 		curscr->_y[cy][x] = wcp[x];
1123*0Sstevel@tonic-gate 	}
1124*0Sstevel@tonic-gate 
1125*0Sstevel@tonic-gate 	/* make sure that wrap-around happens */
1126*0Sstevel@tonic-gate 	if (!auto_right_margin || eat_newline_glitch) {
1127*0Sstevel@tonic-gate 		(void) _outch('\r');
1128*0Sstevel@tonic-gate 		(void) _outch('\n');
1129*0Sstevel@tonic-gate 	}
1130*0Sstevel@tonic-gate 	cx = 0;
1131*0Sstevel@tonic-gate 	++cy;
1132*0Sstevel@tonic-gate }
1133*0Sstevel@tonic-gate 
1134*0Sstevel@tonic-gate /*
1135*0Sstevel@tonic-gate  * Find the top-most line to do clear-to-eod.
1136*0Sstevel@tonic-gate  *
1137*0Sstevel@tonic-gate  * topy, boty: the region to consider
1138*0Sstevel@tonic-gate  */
1139*0Sstevel@tonic-gate 
1140*0Sstevel@tonic-gate static int
_getceod(int topy,int boty)1141*0Sstevel@tonic-gate _getceod(int topy, int boty)
1142*0Sstevel@tonic-gate {
1143*0Sstevel@tonic-gate 	chtype	*wcp, *ecp;
1144*0Sstevel@tonic-gate 	int	wy;
1145*0Sstevel@tonic-gate 	short	*begch, *endch, *begns;
1146*0Sstevel@tonic-gate 
1147*0Sstevel@tonic-gate 	/* do nothing */
1148*0Sstevel@tonic-gate 	if ((topy + 1) >= boty)
1149*0Sstevel@tonic-gate 		return (boty);
1150*0Sstevel@tonic-gate 
1151*0Sstevel@tonic-gate 	wy = boty - 1;
1152*0Sstevel@tonic-gate 	begch = _virtscr->_firstch + wy;
1153*0Sstevel@tonic-gate 	endch = _virtscr->_lastch + wy;
1154*0Sstevel@tonic-gate 	begns = _BEGNS + wy;
1155*0Sstevel@tonic-gate 
1156*0Sstevel@tonic-gate 	for (; wy >= topy; --wy, --begch, --endch, --begns) {
1157*0Sstevel@tonic-gate 		if (*endch == _BLANK || (*begch >= scrco && *begns >= scrco))
1158*0Sstevel@tonic-gate 			continue;
1159*0Sstevel@tonic-gate 
1160*0Sstevel@tonic-gate 		wcp = _virtscr->_y[wy];
1161*0Sstevel@tonic-gate 		ecp = wcp + scrco;
1162*0Sstevel@tonic-gate 		for (; wcp < ecp; ++wcp)
1163*0Sstevel@tonic-gate 			if (_DARKCHAR(*wcp))
1164*0Sstevel@tonic-gate 			break;
1165*0Sstevel@tonic-gate 		if (wcp != ecp)
1166*0Sstevel@tonic-gate 			break;
1167*0Sstevel@tonic-gate 
1168*0Sstevel@tonic-gate 		*endch = _BLANK;
1169*0Sstevel@tonic-gate 	}
1170*0Sstevel@tonic-gate 
1171*0Sstevel@tonic-gate 	return (wy + 1);
1172*0Sstevel@tonic-gate }
1173*0Sstevel@tonic-gate 
1174*0Sstevel@tonic-gate /* Use hardware clear-to-bottom. */
1175*0Sstevel@tonic-gate 
1176*0Sstevel@tonic-gate static void
_useceod(int topy,int boty)1177*0Sstevel@tonic-gate _useceod(int topy, int boty)
1178*0Sstevel@tonic-gate {
1179*0Sstevel@tonic-gate 	short	*begns, *begch;
1180*0Sstevel@tonic-gate 
1181*0Sstevel@tonic-gate 	/* skip lines already blanked */
1182*0Sstevel@tonic-gate 	begch = _virtscr->_firstch + topy;
1183*0Sstevel@tonic-gate 	begns = _BEGNS + topy;
1184*0Sstevel@tonic-gate 	for (; topy < boty; ++topy, ++begns, ++begch)
1185*0Sstevel@tonic-gate 		if (*begns < scrco || *begch == _REDRAW)
1186*0Sstevel@tonic-gate 			break;
1187*0Sstevel@tonic-gate 		else
1188*0Sstevel@tonic-gate 			*begch = _INFINITY;
1189*0Sstevel@tonic-gate 
1190*0Sstevel@tonic-gate 	/* nothing to do */
1191*0Sstevel@tonic-gate 	if (topy + 1 >= boty)
1192*0Sstevel@tonic-gate 		return;
1193*0Sstevel@tonic-gate 
1194*0Sstevel@tonic-gate 	/* see if bottom is clear */
1195*0Sstevel@tonic-gate 	for (begns = _BEGNS + boty; boty < scrli; ++boty, ++begns)
1196*0Sstevel@tonic-gate 		if (*begns < scrco)
1197*0Sstevel@tonic-gate 			return;
1198*0Sstevel@tonic-gate 
1199*0Sstevel@tonic-gate 	/* use clear-screen if appropriate */
1200*0Sstevel@tonic-gate 	if (topy == 0) {
1201*0Sstevel@tonic-gate 		/* SS: colors */
1202*0Sstevel@tonic-gate 		if (back_color_erase)
1203*0Sstevel@tonic-gate 			_turn_off_background();
1204*0Sstevel@tonic-gate 
1205*0Sstevel@tonic-gate 		_PUTS(clear_screen, scrli);
1206*0Sstevel@tonic-gate 		cy = 0; cx = 0;
1207*0Sstevel@tonic-gate 		(void) werase(curscr);
1208*0Sstevel@tonic-gate 	} else
1209*0Sstevel@tonic-gate 
1210*0Sstevel@tonic-gate 		/* use clear-to-end-of-display or delete lines */
1211*0Sstevel@tonic-gate 		if (clr_eos || (parm_delete_line && !memory_below)) {
1212*0Sstevel@tonic-gate 			(void) mvcur(cy, cx, topy, 0);
1213*0Sstevel@tonic-gate 			/* LINTED */
1214*0Sstevel@tonic-gate 			cy = (short) topy;
1215*0Sstevel@tonic-gate 			cx = 0;
1216*0Sstevel@tonic-gate 			/* SS: colors */
1217*0Sstevel@tonic-gate 			if (back_color_erase)
1218*0Sstevel@tonic-gate 				_turn_off_background();
1219*0Sstevel@tonic-gate 			_PUTS(clr_eos ? clr_eos : tparm_p1(parm_delete_line,
1220*0Sstevel@tonic-gate 			    scrli - topy), scrli - topy);
1221*0Sstevel@tonic-gate 
1222*0Sstevel@tonic-gate 			/* update curscr */
1223*0Sstevel@tonic-gate 			/* LINTED */
1224*0Sstevel@tonic-gate 			curscr->_cury = (short) topy;
1225*0Sstevel@tonic-gate 			curscr->_curx = 0;
1226*0Sstevel@tonic-gate 			(void) wclrtobot(curscr);
1227*0Sstevel@tonic-gate 		} else
1228*0Sstevel@tonic-gate 			/* no hardware support */
1229*0Sstevel@tonic-gate 			return;
1230*0Sstevel@tonic-gate 
1231*0Sstevel@tonic-gate 		/* correct the update structure */
1232*0Sstevel@tonic-gate 		(void) wtouchln(_virtscr, topy, scrli, FALSE);
1233*0Sstevel@tonic-gate }
1234*0Sstevel@tonic-gate 
1235*0Sstevel@tonic-gate 
1236*0Sstevel@tonic-gate static void
_turn_off_background(void)1237*0Sstevel@tonic-gate _turn_off_background(void)
1238*0Sstevel@tonic-gate {
1239*0Sstevel@tonic-gate 	/* this routine turn the background color to zero.  This need to be */
1240*0Sstevel@tonic-gate 	/* done only in forllowing cases:				*/
1241*0Sstevel@tonic-gate 	/*  1) We are using Tek type terminal (which has bce terminfo	*/
1242*0Sstevel@tonic-gate 	/*	variable)  */
1243*0Sstevel@tonic-gate 	/*  2) The current background is not already zero		*/
1244*0Sstevel@tonic-gate 
1245*0Sstevel@tonic-gate 	if (set_background && cur_term->_cur_pair.background > 0) {
1246*0Sstevel@tonic-gate 		_PUTS(orig_pair, 1);
1247*0Sstevel@tonic-gate 		cur_term->_cur_pair.foreground = -1;
1248*0Sstevel@tonic-gate 		cur_term->_cur_pair.background = -1;
1249*0Sstevel@tonic-gate 		curscr->_attrs &= ~A_COLOR;
1250*0Sstevel@tonic-gate 	}
1251*0Sstevel@tonic-gate }
1252