1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
43*7c478bd9Sstevel@tonic-gate #include	"curses_inc.h"
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate  * Insert to 'win' at most n chars of a string
47*7c478bd9Sstevel@tonic-gate  * starting at (cury, curx). However, if n <= 0,
48*7c478bd9Sstevel@tonic-gate  * insert the entire string.
49*7c478bd9Sstevel@tonic-gate  * \n, \t, \r, \b are treated in the same way
50*7c478bd9Sstevel@tonic-gate  * as other control chars.
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate int
winsnstr(WINDOW * win,char * tsp,int n)54*7c478bd9Sstevel@tonic-gate winsnstr(WINDOW *win, char *tsp, int n)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	chtype		*wcp;
57*7c478bd9Sstevel@tonic-gate 	int		x, cury, endx, maxx, len;
58*7c478bd9Sstevel@tonic-gate 	bool		savscrl, savsync, savimmed;
59*7c478bd9Sstevel@tonic-gate 	short		savx, savy;
60*7c478bd9Sstevel@tonic-gate 	unsigned char	*sp = (unsigned char *)tsp;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	/* only insert at the start of a character */
63*7c478bd9Sstevel@tonic-gate 	win->_nbyte = -1;
64*7c478bd9Sstevel@tonic-gate 	win->_insmode = TRUE;
65*7c478bd9Sstevel@tonic-gate 	if (_scrmax > 1 && _mbvalid(win) == ERR)
66*7c478bd9Sstevel@tonic-gate 		return (ERR);
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	if (n < 0)
69*7c478bd9Sstevel@tonic-gate 		n = MAXINT;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	/* compute total length of the insertion */
72*7c478bd9Sstevel@tonic-gate 	endx = win->_curx;
73*7c478bd9Sstevel@tonic-gate 	maxx = win->_maxx;
74*7c478bd9Sstevel@tonic-gate 	for (x = 0; sp[x] != '\0' && x < n && endx < maxx; ++x) {
75*7c478bd9Sstevel@tonic-gate 		len = (sp[x] < ' '|| sp[x] == _CTRL('?')) ? 2 : 1;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 		if (ISMBIT(sp[x])) {
78*7c478bd9Sstevel@tonic-gate 			int	m, k, ty;
79*7c478bd9Sstevel@tonic-gate 			chtype	c;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 			/* see if the entire character is defined */
82*7c478bd9Sstevel@tonic-gate 			c = RBYTE(sp[x]);
83*7c478bd9Sstevel@tonic-gate 			ty = TYPE(c);
84*7c478bd9Sstevel@tonic-gate 			m = x + cswidth[ty] - (ty == 0 ? 1 : 0);
85*7c478bd9Sstevel@tonic-gate 			for (k = x + 1; sp[k] != '\0' && k <= m; ++k) {
86*7c478bd9Sstevel@tonic-gate 				c = RBYTE(sp[k]);
87*7c478bd9Sstevel@tonic-gate 				if (TYPE(c) != 0)
88*7c478bd9Sstevel@tonic-gate 					break;
89*7c478bd9Sstevel@tonic-gate 			}
90*7c478bd9Sstevel@tonic-gate 			if (k <= m)
91*7c478bd9Sstevel@tonic-gate 				break;
92*7c478bd9Sstevel@tonic-gate 			/* recompute # of columns used */
93*7c478bd9Sstevel@tonic-gate 			len = _curs_scrwidth[ty];
94*7c478bd9Sstevel@tonic-gate 			/* skip an appropriate number of bytes */
95*7c478bd9Sstevel@tonic-gate 			x = m;
96*7c478bd9Sstevel@tonic-gate 		}
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 		if ((endx += len) > maxx) {
99*7c478bd9Sstevel@tonic-gate 			endx -= len;
100*7c478bd9Sstevel@tonic-gate 			break;
101*7c478bd9Sstevel@tonic-gate 		}
102*7c478bd9Sstevel@tonic-gate 	}
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	/* length of chars to be shifted */
105*7c478bd9Sstevel@tonic-gate 	if ((len = endx - win->_curx) <= 0)
106*7c478bd9Sstevel@tonic-gate 		return (ERR);
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	/* number of chars insertible */
109*7c478bd9Sstevel@tonic-gate 	n = x;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	/* shift data */
112*7c478bd9Sstevel@tonic-gate 	cury = win->_cury;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	if (_mbinsshift(win, len) == ERR)
115*7c478bd9Sstevel@tonic-gate 		return (ERR);
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	/* insert new data */
118*7c478bd9Sstevel@tonic-gate 	wcp = win->_y[cury] + win->_curx;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	/* act as if we are adding characters */
121*7c478bd9Sstevel@tonic-gate 	savx = win->_curx;
122*7c478bd9Sstevel@tonic-gate 	savy = win->_cury;
123*7c478bd9Sstevel@tonic-gate 	win->_insmode = FALSE;
124*7c478bd9Sstevel@tonic-gate 	savscrl = win->_scroll;
125*7c478bd9Sstevel@tonic-gate 	savimmed = win->_immed;
126*7c478bd9Sstevel@tonic-gate 	savsync = win->_sync;
127*7c478bd9Sstevel@tonic-gate 	win->_scroll = win->_sync;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	for (; n > 0; --n, ++sp) {
130*7c478bd9Sstevel@tonic-gate 		/* multi-byte characters */
131*7c478bd9Sstevel@tonic-gate 		if (_mbtrue && ISMBIT(*sp)) {
132*7c478bd9Sstevel@tonic-gate 			(void) _mbaddch(win, A_NORMAL, RBYTE(*sp));
133*7c478bd9Sstevel@tonic-gate 			wcp = win->_y[cury] + win->_curx;
134*7c478bd9Sstevel@tonic-gate 			continue;
135*7c478bd9Sstevel@tonic-gate 		}
136*7c478bd9Sstevel@tonic-gate 		if (_scrmax > 1 && ISMBIT(*wcp))
137*7c478bd9Sstevel@tonic-gate 			(void) _mbclrch(win, cury, win->_curx);
138*7c478bd9Sstevel@tonic-gate 		/* single byte character */
139*7c478bd9Sstevel@tonic-gate 		win->_nbyte = -1;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 		if (*sp < ' ' || *sp == _CTRL('?')) {
142*7c478bd9Sstevel@tonic-gate 			*wcp++ = _CHAR('^') | win->_attrs;
143*7c478bd9Sstevel@tonic-gate 			*wcp = _CHAR(_UNCTRL(*sp)) | win->_attrs;
144*7c478bd9Sstevel@tonic-gate 		} else
145*7c478bd9Sstevel@tonic-gate 			*wcp = _CHAR(*sp) | win->_attrs;
146*7c478bd9Sstevel@tonic-gate 		win->_curx += (*sp < ' ' || *sp == _CTRL('?')) ? 2 : 1;
147*7c478bd9Sstevel@tonic-gate 		++wcp;
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 	win->_curx = savx;
150*7c478bd9Sstevel@tonic-gate 	win->_cury = savy;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	/* update the change structure */
153*7c478bd9Sstevel@tonic-gate 	if (win->_firstch[cury] > win->_curx)
154*7c478bd9Sstevel@tonic-gate 		win->_firstch[cury] = win->_curx;
155*7c478bd9Sstevel@tonic-gate 	win->_lastch[cury] = maxx - 1;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	win->_flags |= _WINCHANGED;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	win->_scroll = savscrl;
160*7c478bd9Sstevel@tonic-gate 	win->_sync = savsync;
161*7c478bd9Sstevel@tonic-gate 	win->_immed = savimmed;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	if (win->_sync)
164*7c478bd9Sstevel@tonic-gate 		wsyncup(win);
165*7c478bd9Sstevel@tonic-gate 	return (win->_immed ? wrefresh(win) : OK);
166*7c478bd9Sstevel@tonic-gate }
167