1 /* $OpenBSD: lib_getstr.c,v 1.1 1999/01/18 19:09:46 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998 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 /* 38 ** lib_getstr.c 39 ** 40 ** The routine wgetstr(). 41 ** 42 */ 43 44 #include <curses.priv.h> 45 #include <term.h> 46 47 MODULE_ID("$From: lib_getstr.c,v 1.20 1998/12/20 00:16:01 tom 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 *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, ' '); 67 68 wmove(win, y, x); 69 } 70 } 71 return last; 72 } 73 74 int wgetnstr(WINDOW *win, char *str, int maxlen) 75 { 76 TTY buf; 77 bool oldnl, oldecho, oldraw, oldcbreak; 78 char erasec; 79 char killc; 80 char *oldstr; 81 int ch; 82 int y, x; 83 84 T((T_CALLED("wgetnstr(%p,%p, %d)"), win, str, maxlen)); 85 86 if (!win) 87 returnCode(ERR); 88 89 _nc_get_tty_mode(&buf); 90 91 oldnl = SP->_nl; 92 oldecho = SP->_echo; 93 oldraw = SP->_raw; 94 oldcbreak = SP->_cbreak; 95 nl(); 96 noecho(); 97 noraw(); 98 cbreak(); 99 100 erasec = erasechar(); 101 killc = killchar(); 102 103 oldstr = str; 104 getyx(win, y, x); 105 106 if (is_wintouched(win) || (win->_flags & _HASMOVED)) 107 wrefresh(win); 108 109 while ((ch = wgetch(win)) != ERR) { 110 /* 111 * Some terminals (the Wyse-50 is the most common) generate 112 * a \n from the down-arrow key. With this logic, it's the 113 * user's choice whether to set kcud=\n for wgetch(); 114 * terminating *getstr() with \n should work either way. 115 */ 116 if (ch == '\n' 117 || ch == '\r' 118 || ch == KEY_DOWN 119 || ch == KEY_ENTER) { 120 if (oldecho == TRUE 121 && win->_cury == win->_maxy 122 && win->_scroll) 123 wechochar(win, '\n'); 124 break; 125 } 126 if (ch == erasec || ch == KEY_LEFT || ch == KEY_BACKSPACE) { 127 if (str > oldstr) { 128 str = WipeOut(win, y, x, oldstr, str, oldecho); 129 } 130 } else if (ch == killc) { 131 while (str > oldstr) { 132 str = WipeOut(win, y, x, oldstr, str, oldecho); 133 } 134 } else if (ch >= KEY_MIN 135 || (maxlen >= 0 && str - oldstr >= maxlen)) { 136 beep(); 137 } else { 138 *str++ = ch; 139 if (oldecho == TRUE) { 140 int oldy = win->_cury; 141 if (waddch(win, ch) == ERR) { 142 /* 143 * We can't really use the lower-right 144 * corner for input, since it'll mess 145 * up bookkeeping for erases. 146 */ 147 win->_flags &= ~_WRAPPED; 148 waddch(win, ' '); 149 str = WipeOut(win, y, x, oldstr, str, oldecho); 150 continue; 151 } else if (win->_flags & _WRAPPED) { 152 /* 153 * If the last waddch forced a wrap & 154 * scroll, adjust our reference point 155 * for erasures. 156 */ 157 if (win->_scroll 158 && oldy == win->_maxy 159 && win->_cury == win->_maxy) { 160 if (--y <= 0) { 161 y = 0; 162 } 163 } 164 win->_flags &= ~_WRAPPED; 165 } 166 wrefresh(win); 167 } 168 } 169 } 170 171 win->_curx = 0; 172 win->_flags &= ~_WRAPPED; 173 if (win->_cury < win->_maxy) 174 win->_cury++; 175 wrefresh(win); 176 177 /* Restore with a single I/O call, to fix minor asymmetry between 178 * raw/noraw, etc. 179 */ 180 SP->_nl = oldnl; 181 SP->_echo = oldecho; 182 SP->_raw = oldraw; 183 SP->_cbreak = oldcbreak; 184 185 _nc_set_tty_mode(&buf); 186 187 *str = '\0'; 188 if (ch == ERR) 189 returnCode(ERR); 190 191 T(("wgetnstr returns %s", _nc_visbuf(oldstr))); 192 193 returnCode(OK); 194 } 195