1 /**************************************************************************** 2 * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc. * 3 * * 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: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 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. * 22 * * 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 * 26 * authorization. * 27 ****************************************************************************/ 28 29 /**************************************************************************** 30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 31 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 32 ****************************************************************************/ 33 34 /* 35 ** lib_getstr.c 36 ** 37 ** The routine wgetstr(). 38 ** 39 */ 40 41 #include <curses.priv.h> 42 43 MODULE_ID("$Id: lib_getstr.c,v 1.29 2009/10/24 21:59:02 tom Exp $") 44 45 /* 46 * This wipes out the last character, no matter whether it was a tab, control 47 * or other character, and handles reverse wraparound. 48 */ 49 static char * 50 WipeOut(WINDOW *win, int y, int x, char *first, char *last, bool echoed) 51 { 52 if (last > first) { 53 *--last = '\0'; 54 if (echoed) { 55 int y1 = win->_cury; 56 int x1 = win->_curx; 57 58 wmove(win, y, x); 59 waddstr(win, first); 60 getyx(win, y, x); 61 while (win->_cury < y1 62 || (win->_cury == y1 && win->_curx < x1)) 63 waddch(win, (chtype) ' '); 64 65 wmove(win, y, x); 66 } 67 } 68 return last; 69 } 70 71 NCURSES_EXPORT(int) 72 wgetnstr_events(WINDOW *win, 73 char *str, 74 int maxlen, 75 EVENTLIST_1st(_nc_eventlist * evl)) 76 { 77 SCREEN *sp = _nc_screen_of(win); 78 TTY buf; 79 bool oldnl, oldecho, oldraw, oldcbreak; 80 char erasec; 81 char killc; 82 char *oldstr; 83 int ch; 84 int y, x; 85 86 T((T_CALLED("wgetnstr(%p,%p,%d)"), (void *) win, (void *) str, maxlen)); 87 88 if (!win) 89 returnCode(ERR); 90 91 NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_ARGx &buf); 92 93 oldnl = sp->_nl; 94 oldecho = sp->_echo; 95 oldraw = sp->_raw; 96 oldcbreak = sp->_cbreak; 97 NCURSES_SP_NAME(nl) (NCURSES_SP_ARG); 98 NCURSES_SP_NAME(noecho) (NCURSES_SP_ARG); 99 NCURSES_SP_NAME(noraw) (NCURSES_SP_ARG); 100 NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG); 101 102 erasec = NCURSES_SP_NAME(erasechar) (NCURSES_SP_ARG); 103 killc = NCURSES_SP_NAME(killchar) (NCURSES_SP_ARG); 104 105 oldstr = str; 106 getyx(win, y, x); 107 108 if (is_wintouched(win) || (win->_flags & _HASMOVED)) 109 wrefresh(win); 110 111 while ((ch = wgetch_events(win, evl)) != ERR) { 112 /* 113 * Some terminals (the Wyse-50 is the most common) generate 114 * a \n from the down-arrow key. With this logic, it's the 115 * user's choice whether to set kcud=\n for wgetch(); 116 * terminating *getstr() with \n should work either way. 117 */ 118 if (ch == '\n' 119 || ch == '\r' 120 || ch == KEY_DOWN 121 || ch == KEY_ENTER) { 122 if (oldecho == TRUE 123 && win->_cury == win->_maxy 124 && win->_scroll) 125 wechochar(win, (chtype) '\n'); 126 break; 127 } 128 #ifdef KEY_EVENT 129 if (ch == KEY_EVENT) 130 break; 131 #endif 132 #ifdef KEY_RESIZE 133 if (ch == KEY_RESIZE) 134 break; 135 #endif 136 if (ch == erasec || ch == KEY_LEFT || ch == KEY_BACKSPACE) { 137 if (str > oldstr) { 138 str = WipeOut(win, y, x, oldstr, str, oldecho); 139 } 140 } else if (ch == killc) { 141 while (str > oldstr) { 142 str = WipeOut(win, y, x, oldstr, str, oldecho); 143 } 144 } else if (ch >= KEY_MIN 145 || (maxlen >= 0 && str - oldstr >= maxlen)) { 146 NCURSES_SP_NAME(beep) (NCURSES_SP_ARG); 147 } else { 148 *str++ = (char) ch; 149 if (oldecho == TRUE) { 150 int oldy = win->_cury; 151 if (waddch(win, (chtype) ch) == ERR) { 152 /* 153 * We can't really use the lower-right 154 * corner for input, since it'll mess 155 * up bookkeeping for erases. 156 */ 157 win->_flags &= ~_WRAPPED; 158 waddch(win, (chtype) ' '); 159 str = WipeOut(win, y, x, oldstr, str, oldecho); 160 continue; 161 } else if (win->_flags & _WRAPPED) { 162 /* 163 * If the last waddch forced a wrap & 164 * scroll, adjust our reference point 165 * for erasures. 166 */ 167 if (win->_scroll 168 && oldy == win->_maxy 169 && win->_cury == win->_maxy) { 170 if (--y <= 0) { 171 y = 0; 172 } 173 } 174 win->_flags &= ~_WRAPPED; 175 } 176 wrefresh(win); 177 } 178 } 179 } 180 181 win->_curx = 0; 182 win->_flags &= ~_WRAPPED; 183 if (win->_cury < win->_maxy) 184 win->_cury++; 185 wrefresh(win); 186 187 /* Restore with a single I/O call, to fix minor asymmetry between 188 * raw/noraw, etc. 189 */ 190 sp->_nl = oldnl; 191 sp->_echo = oldecho; 192 sp->_raw = oldraw; 193 sp->_cbreak = oldcbreak; 194 195 NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf); 196 197 *str = '\0'; 198 if (ch == ERR) 199 returnCode(ch); 200 201 T(("wgetnstr returns %s", _nc_visbuf(oldstr))); 202 203 #ifdef KEY_EVENT 204 if (ch == KEY_EVENT) 205 returnCode(ch); 206 #endif 207 #ifdef KEY_RESIZE 208 if (ch == KEY_RESIZE) 209 returnCode(ch); 210 #endif 211 212 returnCode(OK); 213 } 214 215 #ifdef NCURSES_WGETCH_EVENTS 216 NCURSES_EXPORT(int) 217 wgetnstr(WINDOW *win, char *str, int maxlen) 218 { 219 returnCode(wgetnstr_events(win, 220 str, 221 maxlen, 222 EVENTLIST_1st((_nc_eventlist *) 0))); 223 } 224 #endif 225