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