xref: /netbsd-src/lib/libcurses/get_wstr.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*   $NetBSD: get_wstr.c,v 1.2 2007/05/28 15:01:55 blymn Exp $ */
2 
3 /*
4  * Copyright (c) 2005 The NetBSD Foundation Inc.
5  * All rights reserved.
6  *
7  * This code is derived from code donated to the NetBSD Foundation
8  * by Ruibiao Qiu <ruibiao@arl.wustl.edu,ruibiao@gmail.com>.
9  *
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *	notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *	notice, this list of conditions and the following disclaimer in the
18  *	documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the NetBSD Foundation nor the names of its
20  *	contributors may be used to endorse or promote products derived
21  *	from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
24  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: get_wstr.c,v 1.2 2007/05/28 15:01:55 blymn Exp $");
40 #endif						  /* not lint */
41 
42 #include "curses.h"
43 #include "curses_private.h"
44 
45 /*
46  * getn_wstr --
47  *	Get a string (of maximum n) characters from stdscr starting at
48  *	(cury, curx).
49  */
50 int
51 getn_wstr(wchar_t *wstr, int n)
52 {
53 #ifndef HAVE_WCHAR
54 	return ERR;
55 #else
56 	return wgetn_wstr(stdscr, wstr, n);
57 #endif /* HAVE_WCHAR */
58 }
59 
60 /*
61  * get_wstr --
62  *	Get a string from stdscr starting at (cury, curx).
63  */
64 __warn_references(get_wstr,
65 	"warning: this program uses get_wstr(), which is unsafe.")
66 int
67 get_wstr(wchar_t *wstr)
68 {
69 #ifndef HAVE_WCHAR
70 	return ERR;
71 #else
72 	return wget_wstr(stdscr, wstr);
73 #endif /* HAVE_WCHAR */
74 }
75 
76 /*
77  * mvgetn_wstr --
78  *  Get a string (of maximum n) characters from stdscr starting at (y, x).
79  */
80 int
81 mvgetn_wstr(int y, int x, wchar_t *wstr, int n)
82 {
83 #ifndef HAVE_WCHAR
84 	return ERR;
85 #else
86 	return mvwgetn_wstr(stdscr, y, x, wstr, n);
87 #endif /* HAVE_WCHAR */
88 }
89 
90 /*
91  * mvget_wstr --
92  *	  Get a string from stdscr starting at (y, x).
93  */
94 __warn_references(mvget_wstr,
95 	"warning: this program uses mvget_wstr(), which is unsafe.")
96 int
97 mvget_wstr(int y, int x, wchar_t *wstr)
98 {
99 #ifndef HAVE_WCHAR
100 	return ERR;
101 #else
102 	return mvwget_wstr(stdscr, y, x, wstr);
103 #endif /* HAVE_WCHAR */
104 }
105 
106 /*
107  * mvwgetn_wstr --
108  *  Get a string (of maximum n) characters from the given window starting
109  *	at (y, x).
110  */
111 int
112 mvwgetn_wstr(WINDOW *win, int y, int x, wchar_t *wstr, int n)
113 {
114 #ifndef HAVE_WCHAR
115 	return ERR;
116 #else
117 	if (wmove(win, y, x) == ERR)
118 		return ERR;
119 
120 	return wgetn_wstr(win, wstr, n);
121 #endif /* HAVE_WCHAR */
122 }
123 
124 /*
125  * mvwget_wstr --
126  *	  Get a string from the given window starting at (y, x).
127  */
128 __warn_references(mvget_wstr,
129 	"warning: this program uses mvget_wstr(), which is unsafe.")
130 int
131 mvwget_wstr(WINDOW *win, int y, int x, wchar_t *wstr)
132 {
133 #ifndef HAVE_WCHAR
134 	return ERR;
135 #else
136 	if (wmove(win, y, x) == ERR)
137 		return ERR;
138 
139 	return wget_wstr(win, wstr);
140 #endif /* HAVE_WCHAR */
141 }
142 
143 /*
144  * wget_wstr --
145  *	Get a string starting at (cury, curx).
146  */
147 __warn_references(wget_wstr,
148 	"warning: this program uses wget_wstr(), which is unsafe.")
149 int
150 wget_wstr(WINDOW *win, wchar_t *wstr)
151 {
152 #ifndef HAVE_WCHAR
153 	return ERR;
154 #else
155 	return __wgetn_wstr(win, wstr, -1);
156 #endif /* HAVE_WCHAR */
157 }
158 
159 /*
160  * wgetn_wstr --
161  *	Get a string starting at (cury, curx).
162  *	Note that n <  2 means that we return ERR (SUSv2 specification).
163  */
164 int
165 wgetn_wstr(WINDOW *win, wchar_t *wstr, int n)
166 {
167 #ifndef HAVE_WCHAR
168 	return ERR;
169 #else
170 	if (n < 1)
171 		return (ERR);
172 	if (n == 1) {
173 		wstr[0] = L'\0';
174 		return (ERR);
175 	}
176 	return __wgetn_wstr(win, wstr, n);
177 #endif /* HAVE_WCHAR */
178 }
179 
180 /*
181  * __wgetn_wstr --
182  *	The actual implementation.
183  *	Note that we include a trailing L'\0' for safety, so str will contain
184  *	at most n - 1 other characters.
185  */
186 int
187 __wgetn_wstr(WINDOW *win, wchar_t *wstr, int n)
188 {
189 #ifndef HAVE_WCHAR
190 	return ERR;
191 #else
192 	wchar_t *ostr, ec, kc, sc[ 2 ];
193 	int oldx, remain;
194 	wint_t wc;
195 	cchar_t cc;
196 
197 	ostr = wstr;
198 	if ( erasewchar( &ec ) == ERR )
199 		return ERR;
200 	if ( killwchar( &kc ) == ERR )
201 		return ERR;
202 	sc[ 0 ] = ( wchar_t )btowc( ' ' );
203 	sc[ 1 ] = L'\0';
204 	setcchar( &cc, sc, win->wattr, 0, NULL );
205 	oldx = win->curx;
206 	remain = n - 1;
207 
208 	while (wget_wch(win, &wc) != ERR
209 	       && wc != L'\n' && wc != L'\r') {
210 #ifdef DEBUG
211 		__CTRACE(__CTRACE_INPUT,
212 		    "__wgetn_wstr: win %p, char 0x%x, remain %d\n",
213 		    win, wc, remain);
214 #endif
215 		*wstr = wc;
216 		touchline(win, win->cury, 1);
217 		if (wc == ec || wc == KEY_BACKSPACE || wc == KEY_LEFT) {
218 			*wstr = L'\0';
219 			if (wstr != ostr) {
220 				if ((wchar_t)wc == ec) {
221 					mvwadd_wch(win, win->cury,
222 						win->curx, &cc);
223 					wmove(win, win->cury, win->curx - 1);
224 				}
225 				if (wc == KEY_BACKSPACE || wc == KEY_LEFT) {
226 					/* getch() displays the key sequence */
227 					mvwadd_wch(win, win->cury,
228 						win->curx - 1, &cc);
229 					mvwadd_wch(win, win->cury,
230 						win->curx - 2, &cc);
231 					wmove(win, win->cury, win->curx - 1);
232 				}
233 				wstr--;
234 				if (n != -1) {
235 					/* We're counting chars */
236 					remain++;
237 				}
238 			} else { /* str == ostr */
239 				if (wc == KEY_BACKSPACE || wc == KEY_LEFT)
240 					/* getch() displays the other keys */
241 					mvwadd_wch(win, win->cury,
242 						win->curx - 1, &cc);
243 				wmove(win, win->cury, oldx);
244 			}
245 		} else if (wc == kc) {
246 			*wstr = L'\0';
247 			if (wstr != ostr) {
248 				/* getch() displays the kill character */
249 				mvwadd_wch(win, win->cury, win->curx - 1, &cc);
250 				/* Clear the characters from screen and str */
251 				while (wstr != ostr) {
252 					mvwadd_wch(win, win->cury,
253 						win->curx - 1, &cc);
254 					wmove(win, win->cury, win->curx - 1);
255 					wstr--;
256 					if (n != -1)
257 						/* We're counting chars */
258 						remain++;
259 				}
260 				mvwadd_wch(win, win->cury, win->curx - 1, &cc);
261 				wmove(win, win->cury, win->curx - 1);
262 			} else
263 				/* getch() displays the kill character */
264 				mvwadd_wch( win, win->cury, oldx, &cc );
265 			wmove(win, win->cury, oldx);
266 		} else if (wc >= KEY_MIN && wc <= KEY_MAX) {
267 			/* get_wch() displays these characters */
268 			mvwadd_wch( win, win->cury, win->curx - 1, &cc );
269 			wmove(win, win->cury, win->curx - 1);
270 		} else {
271 			if (remain) {
272 				wstr++;
273 				remain--;
274 			} else {
275 				mvwadd_wch(win, win->cury, win->curx - 1, &cc);
276 				wmove(win, win->cury, win->curx - 1);
277 			}
278 		}
279 	}
280 
281 	if (wc == ERR) {
282 		*wstr = L'\0';
283 		return ERR;
284 	}
285 	*wstr = L'\0';
286 	return OK;
287 #endif /* HAVE_WCHAR */
288 }
289