xref: /titanic_44/usr/src/lib/libcurses/screen/waddwchnstr.c (revision c28749e97052f09388969427adf7df641cdcdc22)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 /*LINTLIBRARY*/
34 
35 #include	<sys/types.h>
36 #include	"curses_inc.h"
37 
38 /*
39  * Add ncols worth of data to win, using string as input.
40  * Return the number of chtypes copied.
41  * Note: chtype contains 32/16 bit process code.
42  */
43 int
44 waddwchnstr(WINDOW *win, chtype *string, int ncols)
45 {
46 	int		my_x = win->_curx;
47 	int		my_y = win->_cury;
48 	short		my_maxx;
49 	int		counter;
50 	chtype		*ptr = &(win->_y[my_y][my_x]);
51 	chtype		*sptr = ptr;
52 	char		mbbuf[CSMAX+1];
53 	int		mp, s, scrw;
54 	chtype		rawc;
55 	chtype		attr;
56 	short		my_x1 = win->_curx;
57 
58 
59 	while (ISCBIT(*ptr)) {
60 		ptr--;
61 		my_x1--;
62 	}
63 	while (ptr < sptr)
64 		*ptr++ = win->_bkgd;
65 
66 	if (ncols == -1)
67 		ncols = MAXINT;
68 
69 	counter = win->_maxx - my_x;
70 	while ((ncols > 0) && (*string) && (counter > 0)) {
71 		attr = *string & A_WATTRIBUTES;
72 		rawc = *string & A_WCHARTEXT;
73 
74 		/* conver wchar_t to mbuti byte string */
75 		for (mp = 0; mp < sizeof (mbbuf); mp++)
76 			mbbuf[mp] = '\0';
77 		if (_curs_wctomb(mbbuf, rawc) <= 0)
78 			goto out;
79 
80 		/* if there are no cols on screen, end */
81 		if ((scrw = wcscrw(rawc)) > counter)
82 			goto out;
83 
84 		if (rawc & WCHAR_CSMASK) {
85 			/* store multi-byte string into chtype */
86 			for (s = 0, mp = 0; s < scrw; s++, mp += 2) {
87 				*ptr = _CHAR(RBYTE(mbbuf[mp]) |
88 				    RBYTE(mbbuf[mp + 1]) << 8) | CBIT;
89 				SETMBIT(*ptr);
90 				if (mp > 0)
91 					SETCBIT(*ptr);
92 				else
93 					CLRCBIT(*ptr);
94 				*ptr |= attr;
95 				ptr++;
96 			}
97 		} else {
98 		/* store single-byte string into chtype */
99 			*ptr = mbbuf[0];
100 			*ptr |= attr;
101 			ptr++;
102 		}
103 
104 		ncols--;
105 		string++;
106 		counter -= scrw;
107 	}
108 out :
109 
110 	while (ISCBIT(*ptr))
111 		*ptr++ = win->_bkgd;
112 
113 	/* LINTED */
114 	my_maxx = (short) (ptr - sptr + my_x);
115 
116 	if (my_x1 < win->_firstch[my_y])
117 		win->_firstch[my_y] = my_x1;
118 
119 	if (my_maxx > win->_lastch[my_y])
120 		win->_lastch[my_y] = my_maxx;
121 
122 	win->_flags |= _WINCHANGED;
123 
124 	/* sync with ancestor structures */
125 	if (win->_sync)
126 		wsyncup(win);
127 
128 	return (win->_immed ? wrefresh(win) : OK);
129 }
130