xref: /illumos-gate/usr/src/lib/libcurses/screen/waddnstr.c (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
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 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include	<sys/types.h>
45 #include	"curses_inc.h"
46 
47 /* This routine adds a string starting at (_cury, _curx) */
48 
49 int
50 waddnstr(WINDOW *win, char *tstr, int i)
51 {
52 	chtype	ch;
53 	short	maxx_1 = win->_maxx - 1, cury = win->_cury,
54 		curx = win->_curx;
55 	chtype	**_y = win->_y;
56 	bool	savimmed = win->_immed,
57 		savsync = win->_sync;
58 	int	rv = OK;
59 	int	pflag;
60 	unsigned	char *str = (unsigned char *)tstr;
61 
62 #ifdef	DEBUG
63 	if (outf) {
64 		if (win == stdscr)
65 			fprintf(outf, "waddnstr(stdscr, ");
66 		else
67 			fprintf(outf, "waddnstr(%o, ", win);
68 		fprintf(outf, "\"%s\")\n", str);
69 	}
70 #endif	/* DEBUG */
71 
72 	/* throw away any current partial character */
73 	win->_nbyte = -1;
74 	win->_insmode = FALSE;
75 	pflag = 1;
76 
77 	win->_immed = win->_sync = FALSE;
78 
79 	if (i < 0)
80 		i = MAXINT;
81 
82 	while (((ch = *str) != 0) && (i-- > 0)) {
83 		if (pflag == 1) {
84 			if (_scrmax > 1 && (rv = _mbvalid(win)) == ERR)
85 				break;
86 			curx = win->_curx;
87 			cury = win->_cury;
88 		}
89 		if (_mbtrue && ISMBIT(ch)) {
90 			int	m, k, ty;
91 			chtype		c;
92 			/* make sure we have the whole character */
93 			c = RBYTE(ch);
94 			ty = TYPE(c);
95 			m = cswidth[ty] - (ty == 0 ? 1 : 0);
96 			for (k = 1; str[k] != '\0' && k <= m; ++k)
97 				if (!ISMBIT(str[k]))
98 					break;
99 			if (k <= m)
100 				break;
101 			if (m > i)
102 				break;
103 			for (k = 0; k <= m; ++k, ++str) {
104 				if ((rv = _mbaddch(win, A_NORMAL,
105 				    RBYTE(*str))) == ERR)
106 					goto done;
107 				if (k > 0)
108 					i--;
109 			}
110 			pflag = 1;
111 			cury = win->_cury;
112 			curx = win->_curx;
113 			continue;
114 		}
115 
116 		/* do normal characters while not next to edge */
117 		if ((ch >= ' ') && (ch != _CTRL('?')) && (curx < maxx_1)) {
118 			if (_scrmax > 1 && ISMBIT(_y[cury][curx]) &&
119 			    (rv = _mbclrch(win, cury, curx)) == ERR)
120 				break;
121 			if (curx < win->_firstch[cury])
122 				win->_firstch[cury] = curx;
123 			if (curx > win->_lastch[cury])
124 				win->_lastch[cury] = curx;
125 			ch = _WCHAR(win, ch);
126 			_y[cury][curx] = ch;
127 #ifdef	_VR3_COMPAT_CODE
128 			if (_y16update)
129 				/* LINTED */
130 				win->_y16[cury][curx] = _TO_OCHTYPE(ch);
131 #endif	/* _VR3_COMPAT_CODE */
132 			curx++;
133 			pflag = 0;
134 		} else {
135 			win->_curx = curx;
136 			/* found a char that is too tough to handle above */
137 			if (waddch(win, ch) == ERR) {
138 				rv = ERR;
139 				break;
140 			}
141 			cury = win->_cury;
142 			curx = win->_curx;
143 			pflag = 1;
144 		}
145 		str++;
146 		win->_curx = curx;
147 	}
148 
149 done :
150 	win->_curx = curx;
151 	win->_flags |= _WINCHANGED;
152 	win->_sync = savsync;
153 	if (win->_sync)
154 		wsyncup(win);
155 
156 	return ((win->_immed = savimmed) ? wrefresh(win) : rv);
157 }
158