1 /****************************************************************************
2 * Copyright (c) 1998-2017,2018 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
37 ** The routine wgetstr().
41 #include <curses.priv.h>
43 MODULE_ID("$Id: lib_getstr.c,v 1.33 2018/09/01 20:18:09 tom Exp $")
46 * This wipes out the last character, no matter whether it was a tab, control
47 * or other character, and handles reverse wraparound.
50 WipeOut(WINDOW *win, int y, int x, char *first, char *last, int echoed)
61 while (win->_cury < y1
62 || (win->_cury == y1 && win->_curx < x1))
63 waddch(win, (chtype) ' ');
72 wgetnstr_events(WINDOW *win,
75 EVENTLIST_1st(_nc_eventlist * evl))
77 SCREEN *sp = _nc_screen_of(win);
79 bool oldnl, oldecho, oldraw, oldcbreak;
86 T((T_CALLED("wgetnstr(%p,%p,%d)"), (void *) win, (void *) str, maxlen));
91 maxlen = _nc_getstr_limit(maxlen);
93 NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_ARGx &buf);
98 oldcbreak = sp->_cbreak;
99 NCURSES_SP_NAME(nl) (NCURSES_SP_ARG);
100 NCURSES_SP_NAME(noecho) (NCURSES_SP_ARG);
101 NCURSES_SP_NAME(noraw) (NCURSES_SP_ARG);
102 NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG);
104 erasec = NCURSES_SP_NAME(erasechar) (NCURSES_SP_ARG);
105 killc = NCURSES_SP_NAME(killchar) (NCURSES_SP_ARG);
110 if (is_wintouched(win) || (win->_flags & _HASMOVED))
113 while ((ch = wgetch_events(win, evl)) != ERR) {
115 * Some terminals (the Wyse-50 is the most common) generate
116 * a \n from the down-arrow key. With this logic, it's the
117 * user's choice whether to set kcud=\n for wgetch();
118 * terminating *getstr() with \n should work either way.
123 || ch == KEY_ENTER) {
125 && win->_cury == win->_maxy
127 wechochar(win, (chtype) '\n');
135 if (ch == KEY_RESIZE)
138 if (ch == erasec || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
140 str = WipeOut(win, y, x, oldstr, str, oldecho);
142 } else if (ch == killc) {
143 while (str > oldstr) {
144 str = WipeOut(win, y, x, oldstr, str, oldecho);
146 } else if (ch >= KEY_MIN
147 || (str - oldstr >= maxlen)) {
148 NCURSES_SP_NAME(beep) (NCURSES_SP_ARG);
151 if (oldecho == TRUE) {
152 int oldy = win->_cury;
153 if (waddch(win, (chtype) ch) == ERR) {
155 * We can't really use the lower-right
156 * corner for input, since it'll mess
157 * up bookkeeping for erases.
159 win->_flags &= ~_WRAPPED;
160 waddch(win, (chtype) ' ');
161 str = WipeOut(win, y, x, oldstr, str, oldecho);
163 } else if (win->_flags & _WRAPPED) {
165 * If the last waddch forced a wrap &
166 * scroll, adjust our reference point
170 && oldy == win->_maxy
171 && win->_cury == win->_maxy) {
176 win->_flags &= ~_WRAPPED;
184 win->_flags &= ~_WRAPPED;
185 if (win->_cury < win->_maxy)
189 /* Restore with a single I/O call, to fix minor asymmetry between
195 sp->_cbreak = oldcbreak;
197 NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
203 T(("wgetnstr returns %s", _nc_visbuf(oldstr)));
210 if (ch == KEY_RESIZE)
217 #ifdef NCURSES_WGETCH_EVENTS
219 wgetnstr(WINDOW *win, char *str, int maxlen)
221 returnCode(wgetnstr_events(win,
224 EVENTLIST_1st((_nc_eventlist *) 0)));