1 /* $NetBSD: get_wstr.c,v 1.3 2008/04/14 20:33:59 jdc Exp $ */
4 * Copyright (c) 2005 The NetBSD Foundation Inc.
7 * This code is derived from code donated to the NetBSD Foundation
8 * by Ruibiao Qiu <ruibiao@arl.wustl.edu,ruibiao@gmail.com>.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
37 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: get_wstr.c,v 1.3 2008/04/14 20:33:59 jdc Exp $");
43 #include "curses_private.h"
45 /* prototypes for private functions */
47 static int __wgetn_wstr(WINDOW
*, wchar_t *, int);
48 #endif /* HAVE_WCHAR */
52 * Get a string (of maximum n) characters from stdscr starting at
56 getn_wstr(wchar_t *wstr
, int n
)
61 return wgetn_wstr(stdscr
, wstr
, n
);
62 #endif /* HAVE_WCHAR */
67 * Get a string from stdscr starting at (cury, curx).
69 __warn_references(get_wstr
,
70 "warning: this program uses get_wstr(), which is unsafe.")
72 get_wstr(wchar_t *wstr
)
77 return wget_wstr(stdscr
, wstr
);
78 #endif /* HAVE_WCHAR */
83 * Get a string (of maximum n) characters from stdscr starting at (y, x).
86 mvgetn_wstr(int y
, int x
, wchar_t *wstr
, int n
)
91 return mvwgetn_wstr(stdscr
, y
, x
, wstr
, n
);
92 #endif /* HAVE_WCHAR */
97 * Get a string from stdscr starting at (y, x).
99 __warn_references(mvget_wstr
,
100 "warning: this program uses mvget_wstr(), which is unsafe.")
102 mvget_wstr(int y
, int x
, wchar_t *wstr
)
107 return mvwget_wstr(stdscr
, y
, x
, wstr
);
108 #endif /* HAVE_WCHAR */
113 * Get a string (of maximum n) characters from the given window starting
117 mvwgetn_wstr(WINDOW
*win
, int y
, int x
, wchar_t *wstr
, int n
)
122 if (wmove(win
, y
, x
) == ERR
)
125 return wgetn_wstr(win
, wstr
, n
);
126 #endif /* HAVE_WCHAR */
131 * Get a string from the given window starting at (y, x).
133 __warn_references(mvget_wstr
,
134 "warning: this program uses mvget_wstr(), which is unsafe.")
136 mvwget_wstr(WINDOW
*win
, int y
, int x
, wchar_t *wstr
)
141 if (wmove(win
, y
, x
) == ERR
)
144 return wget_wstr(win
, wstr
);
145 #endif /* HAVE_WCHAR */
150 * Get a string starting at (cury, curx).
152 __warn_references(wget_wstr
,
153 "warning: this program uses wget_wstr(), which is unsafe.")
155 wget_wstr(WINDOW
*win
, wchar_t *wstr
)
160 return __wgetn_wstr(win
, wstr
, -1);
161 #endif /* HAVE_WCHAR */
166 * Get a string starting at (cury, curx).
167 * Note that n < 2 means that we return ERR (SUSv2 specification).
170 wgetn_wstr(WINDOW
*win
, wchar_t *wstr
, int n
)
181 return __wgetn_wstr(win
, wstr
, n
);
182 #endif /* HAVE_WCHAR */
188 * The actual implementation.
189 * Note that we include a trailing L'\0' for safety, so str will contain
190 * at most n - 1 other characters.
193 __wgetn_wstr(WINDOW
*win
, wchar_t *wstr
, int n
)
195 wchar_t *ostr
, ec
, kc
, sc
[ 2 ];
201 if ( erasewchar( &ec
) == ERR
)
203 if ( killwchar( &kc
) == ERR
)
205 sc
[ 0 ] = ( wchar_t )btowc( ' ' );
207 setcchar( &cc
, sc
, win
->wattr
, 0, NULL
);
211 while (wget_wch(win
, &wc
) != ERR
212 && wc
!= L
'\n' && wc
!= L
'\r') {
214 __CTRACE(__CTRACE_INPUT
,
215 "__wgetn_wstr: win %p, char 0x%x, remain %d\n",
219 touchline(win
, win
->cury
, 1);
220 if (wc
== ec
|| wc
== KEY_BACKSPACE
|| wc
== KEY_LEFT
) {
223 if ((wchar_t)wc
== ec
) {
224 mvwadd_wch(win
, win
->cury
,
226 wmove(win
, win
->cury
, win
->curx
- 1);
228 if (wc
== KEY_BACKSPACE
|| wc
== KEY_LEFT
) {
229 /* getch() displays the key sequence */
230 mvwadd_wch(win
, win
->cury
,
232 mvwadd_wch(win
, win
->cury
,
234 wmove(win
, win
->cury
, win
->curx
- 1);
238 /* We're counting chars */
241 } else { /* str == ostr */
242 if (wc
== KEY_BACKSPACE
|| wc
== KEY_LEFT
)
243 /* getch() displays the other keys */
244 mvwadd_wch(win
, win
->cury
,
246 wmove(win
, win
->cury
, oldx
);
248 } else if (wc
== kc
) {
251 /* getch() displays the kill character */
252 mvwadd_wch(win
, win
->cury
, win
->curx
- 1, &cc
);
253 /* Clear the characters from screen and str */
254 while (wstr
!= ostr
) {
255 mvwadd_wch(win
, win
->cury
,
257 wmove(win
, win
->cury
, win
->curx
- 1);
260 /* We're counting chars */
263 mvwadd_wch(win
, win
->cury
, win
->curx
- 1, &cc
);
264 wmove(win
, win
->cury
, win
->curx
- 1);
266 /* getch() displays the kill character */
267 mvwadd_wch( win
, win
->cury
, oldx
, &cc
);
268 wmove(win
, win
->cury
, oldx
);
269 } else if (wc
>= KEY_MIN
&& wc
<= KEY_MAX
) {
270 /* get_wch() displays these characters */
271 mvwadd_wch( win
, win
->cury
, win
->curx
- 1, &cc
);
272 wmove(win
, win
->cury
, win
->curx
- 1);
278 mvwadd_wch(win
, win
->cury
, win
->curx
- 1, &cc
);
279 wmove(win
, win
->cury
, win
->curx
- 1);
291 #endif /* HAVE_WCHAR */