1 /* $OpenBSD: lib_getstr.c,v 1.4 2023/10/17 09:52:08 nicm Exp $ */ 2 3 /**************************************************************************** 4 * Copyright 2018-2021,2023 Thomas E. Dickey * 5 * Copyright 1998-2011,2017 Free Software Foundation, Inc. * 6 * * 7 * Permission is hereby granted, free of charge, to any person obtaining a * 8 * copy of this software and associated documentation files (the * 9 * "Software"), to deal in the Software without restriction, including * 10 * without limitation the rights to use, copy, modify, merge, publish, * 11 * distribute, distribute with modifications, sublicense, and/or sell * 12 * copies of the Software, and to permit persons to whom the Software is * 13 * furnished to do so, subject to the following conditions: * 14 * * 15 * The above copyright notice and this permission notice shall be included * 16 * in all copies or substantial portions of the Software. * 17 * * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 25 * * 26 * Except as contained in this notice, the name(s) of the above copyright * 27 * holders shall not be used in advertising or otherwise to promote the * 28 * sale, use or other dealings in this Software without prior written * 29 * authorization. * 30 ****************************************************************************/ 31 32 /**************************************************************************** 33 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 34 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 35 ****************************************************************************/ 36 37 /* 38 ** lib_getstr.c 39 ** 40 ** The routine wgetstr(). 41 ** 42 */ 43 44 #define NEED_KEY_EVENT 45 #include <curses.priv.h> 46 47 MODULE_ID("$Id: lib_getstr.c,v 1.4 2023/10/17 09:52:08 nicm Exp $") 48 49 /* 50 * This wipes out the last character, no matter whether it was a tab, control 51 * or other character, and handles reverse wraparound. 52 */ 53 static char * 54 WipeOut(WINDOW *win, int y, int x, char *first, char *last, int echoed) 55 { 56 if (last > first) { 57 *--last = '\0'; 58 if (echoed) { 59 int y1 = win->_cury; 60 int x1 = win->_curx; 61 62 wmove(win, y, x); 63 waddstr(win, first); 64 getyx(win, y, x); 65 while (win->_cury < y1 66 || (win->_cury == y1 && win->_curx < x1)) 67 waddch(win, (chtype) ' '); 68 69 wmove(win, y, x); 70 } 71 } 72 return last; 73 } 74 75 NCURSES_EXPORT(int) 76 wgetnstr_events(WINDOW *win, 77 char *str, 78 int maxlen, 79 EVENTLIST_1st(_nc_eventlist * evl)) 80 { 81 SCREEN *sp = _nc_screen_of(win); 82 TTY buf; 83 TTY_FLAGS save_flags; 84 char erasec; 85 char killc; 86 char *oldstr; 87 int ch; 88 int y, x; 89 90 T((T_CALLED("wgetnstr(%p,%p,%d)"), (void *) win, (void *) str, maxlen)); 91 92 if (!win || !str) 93 returnCode(ERR); 94 95 maxlen = _nc_getstr_limit(maxlen); 96 97 NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_ARGx &buf); 98 99 save_flags = sp->_tty_flags; 100 NCURSES_SP_NAME(nl) (NCURSES_SP_ARG); 101 NCURSES_SP_NAME(noecho) (NCURSES_SP_ARG); 102 if (!save_flags._raw) 103 NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG); 104 105 erasec = NCURSES_SP_NAME(erasechar) (NCURSES_SP_ARG); 106 killc = NCURSES_SP_NAME(killchar) (NCURSES_SP_ARG); 107 108 oldstr = str; 109 getyx(win, y, x); 110 111 if (is_wintouched(win) || (win->_flags & _HASMOVED)) 112 wrefresh(win); 113 114 while ((ch = wgetch_events(win, evl)) != ERR) { 115 /* 116 * Some terminals (the Wyse-50 is the most common) generate 117 * a \n from the down-arrow key. With this logic, it is the 118 * user's choice whether to set kcud=\n for wgetch(); 119 * terminating *getstr() with \n should work either way. 120 */ 121 if (ch == '\n' 122 || ch == '\r' 123 || ch == KEY_DOWN 124 || ch == KEY_ENTER) { 125 if (save_flags._echo == TRUE 126 && win->_cury == win->_maxy 127 && win->_scroll) 128 wechochar(win, (chtype) '\n'); 129 break; 130 } 131 #ifdef KEY_EVENT 132 if (ch == KEY_EVENT) 133 break; 134 #endif 135 #ifdef KEY_RESIZE 136 if (ch == KEY_RESIZE) 137 break; 138 #endif 139 if (ch == erasec || ch == KEY_LEFT || ch == KEY_BACKSPACE) { 140 if (str > oldstr) { 141 str = WipeOut(win, y, x, oldstr, str, save_flags._echo); 142 } 143 } else if (ch == killc) { 144 while (str > oldstr) { 145 str = WipeOut(win, y, x, oldstr, str, save_flags._echo); 146 } 147 } else if (ch >= KEY_MIN 148 || (str - oldstr >= maxlen)) { 149 NCURSES_SP_NAME(beep) (NCURSES_SP_ARG); 150 } else { 151 *str++ = (char) ch; 152 if (save_flags._echo == TRUE) { 153 int oldy = win->_cury; 154 if (waddch(win, (chtype) ch) == ERR) { 155 /* 156 * We can't really use the lower-right 157 * corner for input, since it'll mess 158 * up bookkeeping for erases. 159 */ 160 win->_flags &= ~_WRAPPED; 161 waddch(win, (chtype) ' '); 162 str = WipeOut(win, y, x, oldstr, str, save_flags._echo); 163 continue; 164 } else if (IS_WRAPPED(win)) { 165 /* 166 * If the last waddch forced a wrap & 167 * scroll, adjust our reference point 168 * for erasures. 169 */ 170 if (win->_scroll 171 && oldy == win->_maxy 172 && win->_cury == win->_maxy) { 173 if (--y <= 0) { 174 y = 0; 175 } 176 } 177 win->_flags &= ~_WRAPPED; 178 } 179 wrefresh(win); 180 } 181 } 182 } 183 184 win->_curx = 0; 185 win->_flags &= ~_WRAPPED; 186 if (win->_cury < win->_maxy) 187 win->_cury++; 188 wrefresh(win); 189 190 /* Restore with a single I/O call, to fix minor asymmetry between 191 * raw/noraw, etc. 192 */ 193 sp->_tty_flags = save_flags; 194 NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf); 195 196 *str = '\0'; 197 if (ch == ERR) 198 returnCode(ch); 199 200 T(("wgetnstr returns %s", _nc_visbuf(oldstr))); 201 202 #ifdef KEY_EVENT 203 if (ch == KEY_EVENT) 204 returnCode(ch); 205 #endif 206 #ifdef KEY_RESIZE 207 if (ch == KEY_RESIZE) 208 returnCode(ch); 209 #endif 210 211 returnCode(OK); 212 } 213 214 #ifdef NCURSES_WGETCH_EVENTS 215 NCURSES_EXPORT(int) 216 wgetnstr(WINDOW *win, char *str, int maxlen) 217 { 218 returnCode(wgetnstr_events(win, 219 str, 220 maxlen, 221 EVENTLIST_1st((_nc_eventlist *) 0))); 222 } 223 #endif 224