xref: /onnv-gate/usr/src/lib/libcurses/screen/winsnstr.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 1997 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	"curses_inc.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate  * Insert to 'win' at most n chars of a string
49*0Sstevel@tonic-gate  * starting at (cury, curx). However, if n <= 0,
50*0Sstevel@tonic-gate  * insert the entire string.
51*0Sstevel@tonic-gate  * \n, \t, \r, \b are treated in the same way
52*0Sstevel@tonic-gate  * as other control chars.
53*0Sstevel@tonic-gate  */
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate int
winsnstr(WINDOW * win,char * tsp,int n)56*0Sstevel@tonic-gate winsnstr(WINDOW *win, char *tsp, int n)
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	chtype		*wcp;
59*0Sstevel@tonic-gate 	int		x, cury, endx, maxx, len;
60*0Sstevel@tonic-gate 	bool		savscrl, savsync, savimmed;
61*0Sstevel@tonic-gate 	short		savx, savy;
62*0Sstevel@tonic-gate 	unsigned char	*sp = (unsigned char *)tsp;
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	/* only insert at the start of a character */
65*0Sstevel@tonic-gate 	win->_nbyte = -1;
66*0Sstevel@tonic-gate 	win->_insmode = TRUE;
67*0Sstevel@tonic-gate 	if (_scrmax > 1 && _mbvalid(win) == ERR)
68*0Sstevel@tonic-gate 		return (ERR);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	if (n < 0)
71*0Sstevel@tonic-gate 		n = MAXINT;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	/* compute total length of the insertion */
74*0Sstevel@tonic-gate 	endx = win->_curx;
75*0Sstevel@tonic-gate 	maxx = win->_maxx;
76*0Sstevel@tonic-gate 	for (x = 0; sp[x] != '\0' && x < n && endx < maxx; ++x) {
77*0Sstevel@tonic-gate 		len = (sp[x] < ' '|| sp[x] == _CTRL('?')) ? 2 : 1;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 		if (ISMBIT(sp[x])) {
80*0Sstevel@tonic-gate 			int	m, k, ty;
81*0Sstevel@tonic-gate 			chtype	c;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 			/* see if the entire character is defined */
84*0Sstevel@tonic-gate 			c = RBYTE(sp[x]);
85*0Sstevel@tonic-gate 			ty = TYPE(c);
86*0Sstevel@tonic-gate 			m = x + cswidth[ty] - (ty == 0 ? 1 : 0);
87*0Sstevel@tonic-gate 			for (k = x + 1; sp[k] != '\0' && k <= m; ++k) {
88*0Sstevel@tonic-gate 				c = RBYTE(sp[k]);
89*0Sstevel@tonic-gate 				if (TYPE(c) != 0)
90*0Sstevel@tonic-gate 					break;
91*0Sstevel@tonic-gate 			}
92*0Sstevel@tonic-gate 			if (k <= m)
93*0Sstevel@tonic-gate 				break;
94*0Sstevel@tonic-gate 			/* recompute # of columns used */
95*0Sstevel@tonic-gate 			len = _curs_scrwidth[ty];
96*0Sstevel@tonic-gate 			/* skip an appropriate number of bytes */
97*0Sstevel@tonic-gate 			x = m;
98*0Sstevel@tonic-gate 		}
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 		if ((endx += len) > maxx) {
101*0Sstevel@tonic-gate 			endx -= len;
102*0Sstevel@tonic-gate 			break;
103*0Sstevel@tonic-gate 		}
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	/* length of chars to be shifted */
107*0Sstevel@tonic-gate 	if ((len = endx - win->_curx) <= 0)
108*0Sstevel@tonic-gate 		return (ERR);
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	/* number of chars insertible */
111*0Sstevel@tonic-gate 	n = x;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	/* shift data */
114*0Sstevel@tonic-gate 	cury = win->_cury;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	if (_mbinsshift(win, len) == ERR)
117*0Sstevel@tonic-gate 		return (ERR);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	/* insert new data */
120*0Sstevel@tonic-gate 	wcp = win->_y[cury] + win->_curx;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	/* act as if we are adding characters */
123*0Sstevel@tonic-gate 	savx = win->_curx;
124*0Sstevel@tonic-gate 	savy = win->_cury;
125*0Sstevel@tonic-gate 	win->_insmode = FALSE;
126*0Sstevel@tonic-gate 	savscrl = win->_scroll;
127*0Sstevel@tonic-gate 	savimmed = win->_immed;
128*0Sstevel@tonic-gate 	savsync = win->_sync;
129*0Sstevel@tonic-gate 	win->_scroll = win->_sync;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	for (; n > 0; --n, ++sp) {
132*0Sstevel@tonic-gate 		/* multi-byte characters */
133*0Sstevel@tonic-gate 		if (_mbtrue && ISMBIT(*sp)) {
134*0Sstevel@tonic-gate 			(void) _mbaddch(win, A_NORMAL, RBYTE(*sp));
135*0Sstevel@tonic-gate 			wcp = win->_y[cury] + win->_curx;
136*0Sstevel@tonic-gate 			continue;
137*0Sstevel@tonic-gate 		}
138*0Sstevel@tonic-gate 		if (_scrmax > 1 && ISMBIT(*wcp))
139*0Sstevel@tonic-gate 			(void) _mbclrch(win, cury, win->_curx);
140*0Sstevel@tonic-gate 		/* single byte character */
141*0Sstevel@tonic-gate 		win->_nbyte = -1;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 		if (*sp < ' ' || *sp == _CTRL('?')) {
144*0Sstevel@tonic-gate 			*wcp++ = _CHAR('^') | win->_attrs;
145*0Sstevel@tonic-gate 			*wcp = _CHAR(_UNCTRL(*sp)) | win->_attrs;
146*0Sstevel@tonic-gate 		} else
147*0Sstevel@tonic-gate 			*wcp = _CHAR(*sp) | win->_attrs;
148*0Sstevel@tonic-gate 		win->_curx += (*sp < ' ' || *sp == _CTRL('?')) ? 2 : 1;
149*0Sstevel@tonic-gate 		++wcp;
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate 	win->_curx = savx;
152*0Sstevel@tonic-gate 	win->_cury = savy;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	/* update the change structure */
155*0Sstevel@tonic-gate 	if (win->_firstch[cury] > win->_curx)
156*0Sstevel@tonic-gate 		win->_firstch[cury] = win->_curx;
157*0Sstevel@tonic-gate 	win->_lastch[cury] = maxx - 1;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	win->_flags |= _WINCHANGED;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	win->_scroll = savscrl;
162*0Sstevel@tonic-gate 	win->_sync = savsync;
163*0Sstevel@tonic-gate 	win->_immed = savimmed;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	if (win->_sync)
166*0Sstevel@tonic-gate 		wsyncup(win);
167*0Sstevel@tonic-gate 	return (win->_immed ? wrefresh(win) : OK);
168*0Sstevel@tonic-gate }
169