xref: /illumos-gate/usr/src/lib/libcurses/screen/waddnstr.c (revision 0250c53ad267726f2438e3c6556199a0bbf588a2)
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 /*LINTLIBRARY*/
41 
42 #include	<sys/types.h>
43 #include	"curses_inc.h"
44 
45 /* This routine adds a string starting at (_cury, _curx) */
46 
47 int
48 waddnstr(WINDOW *win, char *tstr, int i)
49 {
50 	chtype	ch;
51 	short	maxx_1 = win->_maxx - 1, cury = win->_cury,
52 		curx = win->_curx;
53 	chtype	**_y = win->_y;
54 	bool	savimmed = win->_immed,
55 		savsync = win->_sync;
56 	int	rv = OK;
57 	int	pflag;
58 	unsigned	char *str = (unsigned char *)tstr;
59 
60 #ifdef	DEBUG
61 	if (outf) {
62 		if (win == stdscr)
63 			fprintf(outf, "waddnstr(stdscr, ");
64 		else
65 			fprintf(outf, "waddnstr(%o, ", win);
66 		fprintf(outf, "\"%s\")\n", str);
67 	}
68 #endif	/* DEBUG */
69 
70 	/* throw away any current partial character */
71 	win->_nbyte = -1;
72 	win->_insmode = FALSE;
73 	pflag = 1;
74 
75 	win->_immed = win->_sync = FALSE;
76 
77 	if (i < 0)
78 		i = MAXINT;
79 
80 	while (((ch = *str) != 0) && (i-- > 0)) {
81 		if (pflag == 1) {
82 			if (_scrmax > 1 && (rv = _mbvalid(win)) == ERR)
83 				break;
84 			curx = win->_curx;
85 			cury = win->_cury;
86 		}
87 		if (_mbtrue && ISMBIT(ch)) {
88 			int	m, k, ty;
89 			chtype		c;
90 			/* make sure we have the whole character */
91 			c = RBYTE(ch);
92 			ty = TYPE(c);
93 			m = cswidth[ty] - (ty == 0 ? 1 : 0);
94 			for (k = 1; str[k] != '\0' && k <= m; ++k)
95 				if (!ISMBIT(str[k]))
96 					break;
97 			if (k <= m)
98 				break;
99 			if (m > i)
100 				break;
101 			for (k = 0; k <= m; ++k, ++str) {
102 				if ((rv = _mbaddch(win, A_NORMAL,
103 				    RBYTE(*str))) == ERR)
104 					goto done;
105 				if (k > 0)
106 					i--;
107 			}
108 			pflag = 1;
109 			cury = win->_cury;
110 			curx = win->_curx;
111 			continue;
112 		}
113 
114 		/* do normal characters while not next to edge */
115 		if ((ch >= ' ') && (ch != _CTRL('?')) && (curx < maxx_1)) {
116 			if (_scrmax > 1 && ISMBIT(_y[cury][curx]) &&
117 			    (rv = _mbclrch(win, cury, curx)) == ERR)
118 				break;
119 			if (curx < win->_firstch[cury])
120 				win->_firstch[cury] = curx;
121 			if (curx > win->_lastch[cury])
122 				win->_lastch[cury] = curx;
123 			ch = _WCHAR(win, ch);
124 			_y[cury][curx] = ch;
125 #ifdef	_VR3_COMPAT_CODE
126 			if (_y16update)
127 				/* LINTED */
128 				win->_y16[cury][curx] = _TO_OCHTYPE(ch);
129 #endif	/* _VR3_COMPAT_CODE */
130 			curx++;
131 			pflag = 0;
132 		} else {
133 			win->_curx = curx;
134 			/* found a char that is too tough to handle above */
135 			if (waddch(win, ch) == ERR) {
136 				rv = ERR;
137 				break;
138 			}
139 			cury = win->_cury;
140 			curx = win->_curx;
141 			pflag = 1;
142 		}
143 		str++;
144 		win->_curx = curx;
145 	}
146 
147 done :
148 	win->_curx = curx;
149 	win->_flags |= _WINCHANGED;
150 	win->_sync = savsync;
151 	if (win->_sync)
152 		wsyncup(win);
153 
154 	return ((win->_immed = savimmed) ? wrefresh(win) : rv);
155 }
156