1 /****************************************************************************
2 * Copyright 2018-2021,2023 Thomas E. Dickey *
3 * Copyright 1998-2011,2017 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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 ****************************************************************************/
38 ** The routine wgetstr().
42 #define NEED_KEY_EVENT
43 #include <curses.priv.h>
45 MODULE_ID("$Id: lib_getstr.c,v 1.39 2023/04/29 19:00:17 tom Exp $")
48 * This wipes out the last character, no matter whether it was a tab, control
49 * or other character, and handles reverse wraparound.
52 WipeOut(WINDOW *win, int y, int x, char *first, char *last, int echoed)
63 while (win->_cury < y1
64 || (win->_cury == y1 && win->_curx < x1))
65 waddch(win, (chtype) ' ');
74 wgetnstr_events(WINDOW *win,
77 EVENTLIST_1st(_nc_eventlist * evl))
79 SCREEN *sp = _nc_screen_of(win);
88 T((T_CALLED("wgetnstr(%p,%p,%d)"), (void *) win, (void *) str, maxlen));
93 maxlen = _nc_getstr_limit(maxlen);
95 NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_ARGx &buf);
97 save_flags = sp->_tty_flags;
98 NCURSES_SP_NAME(nl) (NCURSES_SP_ARG);
99 NCURSES_SP_NAME(noecho) (NCURSES_SP_ARG);
100 if (!save_flags._raw)
101 NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG);
103 erasec = NCURSES_SP_NAME(erasechar) (NCURSES_SP_ARG);
104 killc = NCURSES_SP_NAME(killchar) (NCURSES_SP_ARG);
109 if (is_wintouched(win) || (win->_flags & _HASMOVED))
112 while ((ch = wgetch_events(win, evl)) != ERR) {
114 * Some terminals (the Wyse-50 is the most common) generate
115 * a \n from the down-arrow key. With this logic, it is the
116 * user's choice whether to set kcud=\n for wgetch();
117 * terminating *getstr() with \n should work either way.
122 || ch == KEY_ENTER) {
123 if (save_flags._echo == TRUE
124 && win->_cury == win->_maxy
126 wechochar(win, (chtype) '\n');
134 if (ch == KEY_RESIZE)
137 if (ch == erasec || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
139 str = WipeOut(win, y, x, oldstr, str, save_flags._echo);
141 } else if (ch == killc) {
142 while (str > oldstr) {
143 str = WipeOut(win, y, x, oldstr, str, save_flags._echo);
145 } else if (ch >= KEY_MIN
146 || (str - oldstr >= maxlen)) {
147 NCURSES_SP_NAME(beep) (NCURSES_SP_ARG);
150 if (save_flags._echo == TRUE) {
151 int oldy = win->_cury;
152 if (waddch(win, (chtype) ch) == ERR) {
154 * We can't really use the lower-right
155 * corner for input, since it'll mess
156 * up bookkeeping for erases.
158 win->_flags &= ~_WRAPPED;
159 waddch(win, (chtype) ' ');
160 str = WipeOut(win, y, x, oldstr, str, save_flags._echo);
162 } else if (IS_WRAPPED(win)) {
164 * If the last waddch forced a wrap &
165 * scroll, adjust our reference point
169 && oldy == win->_maxy
170 && win->_cury == win->_maxy) {
175 win->_flags &= ~_WRAPPED;
183 win->_flags &= ~_WRAPPED;
184 if (win->_cury < win->_maxy)
188 /* Restore with a single I/O call, to fix minor asymmetry between
191 sp->_tty_flags = save_flags;
192 NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
198 T(("wgetnstr returns %s", _nc_visbuf(oldstr)));
205 if (ch == KEY_RESIZE)
212 #ifdef NCURSES_WGETCH_EVENTS
214 wgetnstr(WINDOW *win, char *str, int maxlen)
216 returnCode(wgetnstr_events(win,
219 EVENTLIST_1st((_nc_eventlist *) 0)));