1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 2002-2009,2011 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Thomas E. Dickey *
32 ****************************************************************************/
37 ** The routine wgetn_wstr().
41 #include <curses.priv.h>
43 MODULE_ID("$Id: lib_get_wstr.c,v 1.20 2021/10/23 19:02:59 tom Exp $")
46 wadd_wint(WINDOW *win, wint_t *src)
51 wch[0] = (wchar_t) (*src);
53 setcchar(&tmp, wch, A_NORMAL, (short) 0, NULL);
54 return wadd_wch(win, &tmp);
58 * This wipes out the last character, no matter whether it was a tab, control
59 * or other character, and handles reverse wraparound.
62 WipeOut(WINDOW *win, int y, int x, wint_t *first, wint_t *last, int echoed)
72 for (n = 0; first[n] != 0; ++n) {
73 wadd_wint(win, first + n);
76 while (win->_cury < y1
77 || (win->_cury == y1 && win->_curx < x1))
78 waddch(win, (chtype) ' ');
87 wgetn_wstr(WINDOW *win, wint_t *str, int maxlen)
89 SCREEN *sp = _nc_screen_of(win);
91 bool oldnl, oldecho, oldraw, oldcbreak;
99 T((T_CALLED("wgetn_wstr(%p,%p, %d)"), (void *) win, (void *) str, maxlen));
104 maxlen = _nc_getstr_limit(maxlen);
106 _nc_get_tty_mode(&buf);
111 oldcbreak = sp->_cbreak;
112 NCURSES_SP_NAME(nl) (NCURSES_SP_ARG);
113 NCURSES_SP_NAME(noecho) (NCURSES_SP_ARG);
114 NCURSES_SP_NAME(raw) (NCURSES_SP_ARG);
116 NCURSES_SP_NAME(erasewchar) (NCURSES_SP_ARGx &erasec);
117 NCURSES_SP_NAME(killwchar) (NCURSES_SP_ARGx &killc);
121 if (is_wintouched(win) || (win->_flags & _HASMOVED))
124 while ((code = wget_wch(win, &ch)) != ERR) {
126 * Map special characters into key-codes.
134 if (ch != 0 && ch < KEY_MIN) {
135 if (ch == (wint_t) erasec) {
139 if (ch == (wint_t) killc) {
144 if (code == KEY_CODE_YES) {
146 * Some terminals (the Wyse-50 is the most common) generate a \n
147 * from the down-arrow key. With this logic, it is the user's
148 * choice whether to set kcud=\n for wget_wch(); terminating
149 * *getn_wstr() with \n should work either way.
151 if (ch == KEY_DOWN || ch == KEY_ENTER) {
153 && win->_cury == win->_maxy
155 wechochar(win, (chtype) '\n');
158 if (ch == KEY_LEFT || ch == KEY_BACKSPACE) {
159 if (tmpstr > oldstr) {
160 tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
162 } else if (ch == KEY_EOL) {
163 while (tmpstr > oldstr) {
164 tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
169 } else if (tmpstr - oldstr >= maxlen) {
174 if (oldecho == TRUE) {
175 int oldy = win->_cury;
177 if (wadd_wint(win, tmpstr - 1) == ERR) {
179 * We can't really use the lower-right corner for input,
180 * since it'll mess up bookkeeping for erases.
182 win->_flags &= ~_WRAPPED;
183 waddch(win, (chtype) ' ');
184 tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
186 } else if (IS_WRAPPED(win)) {
188 * If the last waddch forced a wrap & scroll, adjust our
189 * reference point for erasures.
192 && oldy == win->_maxy
193 && win->_cury == win->_maxy) {
198 win->_flags &= ~_WRAPPED;
206 win->_flags &= ~_WRAPPED;
207 if (win->_cury < win->_maxy)
211 /* Restore with a single I/O call, to fix minor asymmetry between
217 sp->_cbreak = oldcbreak;
219 (void) _nc_set_tty_mode(&buf);
223 if (tmpstr == oldstr) {
230 T(("wgetn_wstr returns %s", _nc_viswibuf(oldstr)));