xref: /illumos-gate/usr/src/lib/libcurses/screen/waddwchnstr.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*  Copyright (c) 1988 AT&T */
23 /*    All Rights Reserved   */
24 
25 
26 /*
27  *      Copyright (c) 1997, by Sun Microsystems, Inc.
28  *      All rights reserved.
29  */
30 
31 /*LINTLIBRARY*/
32 
33 #include	<sys/types.h>
34 #include	"curses_inc.h"
35 
36 /*
37  * Add ncols worth of data to win, using string as input.
38  * Return the number of chtypes copied.
39  * Note: chtype contains 32/16 bit process code.
40  */
41 int
42 waddwchnstr(WINDOW *win, chtype *string, int ncols)
43 {
44 	int		my_x = win->_curx;
45 	int		my_y = win->_cury;
46 	short		my_maxx;
47 	int		counter;
48 	chtype		*ptr = &(win->_y[my_y][my_x]);
49 	chtype		*sptr = ptr;
50 	char		mbbuf[CSMAX+1];
51 	int		mp, s, scrw;
52 	chtype		rawc;
53 	chtype		attr;
54 	short		my_x1 = win->_curx;
55 
56 
57 	while (ISCBIT(*ptr)) {
58 		ptr--;
59 		my_x1--;
60 	}
61 	while (ptr < sptr)
62 		*ptr++ = win->_bkgd;
63 
64 	if (ncols == -1)
65 		ncols = MAXINT;
66 
67 	counter = win->_maxx - my_x;
68 	while ((ncols > 0) && (*string) && (counter > 0)) {
69 		attr = *string & A_WATTRIBUTES;
70 		rawc = *string & A_WCHARTEXT;
71 
72 		/* conver wchar_t to mbuti byte string */
73 		for (mp = 0; mp < sizeof (mbbuf); mp++)
74 			mbbuf[mp] = '\0';
75 		if (_curs_wctomb(mbbuf, rawc) <= 0)
76 			goto out;
77 
78 		/* if there are no cols on screen, end */
79 		if ((scrw = wcscrw(rawc)) > counter)
80 			goto out;
81 
82 		if (rawc & WCHAR_CSMASK) {
83 			/* store multi-byte string into chtype */
84 			for (s = 0, mp = 0; s < scrw; s++, mp += 2) {
85 				*ptr = _CHAR(RBYTE(mbbuf[mp]) |
86 				    RBYTE(mbbuf[mp + 1]) << 8) | CBIT;
87 				SETMBIT(*ptr);
88 				if (mp > 0)
89 					SETCBIT(*ptr);
90 				else
91 					CLRCBIT(*ptr);
92 				*ptr |= attr;
93 				ptr++;
94 			}
95 		} else {
96 		/* store single-byte string into chtype */
97 			*ptr = mbbuf[0];
98 			*ptr |= attr;
99 			ptr++;
100 		}
101 
102 		ncols--;
103 		string++;
104 		counter -= scrw;
105 	}
106 out :
107 
108 	while (ISCBIT(*ptr))
109 		*ptr++ = win->_bkgd;
110 
111 	/* LINTED */
112 	my_maxx = (short) (ptr - sptr + my_x);
113 
114 	if (my_x1 < win->_firstch[my_y])
115 		win->_firstch[my_y] = my_x1;
116 
117 	if (my_maxx > win->_lastch[my_y])
118 		win->_lastch[my_y] = my_maxx;
119 
120 	win->_flags |= _WINCHANGED;
121 
122 	/* sync with ancestor structures */
123 	if (win->_sync)
124 		wsyncup(win);
125 
126 	return (win->_immed ? wrefresh(win) : OK);
127 }
128