1 /* $NetBSD: getstr.c,v 1.20 2007/05/28 15:01:55 blymn Exp $ */ 2 3 /* 4 * Copyright (c) 1981, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <assert.h> 33 #include <sys/cdefs.h> 34 #ifndef lint 35 #if 0 36 static char sccsid[] = "@(#)getstr.c 8.2 (Berkeley) 5/4/94"; 37 #else 38 __RCSID("$NetBSD: getstr.c,v 1.20 2007/05/28 15:01:55 blymn Exp $"); 39 #endif 40 #endif /* not lint */ 41 42 #include "curses.h" 43 #include "curses_private.h" 44 45 #ifndef _CURSES_USE_MACROS 46 47 /* 48 * getnstr -- 49 * Get a string (of maximum n) characters from stdscr starting at 50 * (cury, curx). 51 */ 52 int 53 getnstr(char *str, int n) 54 { 55 return wgetnstr(stdscr, str, n); 56 } 57 58 /* 59 * getstr -- 60 * Get a string from stdscr starting at (cury, curx). 61 */ 62 __warn_references(getstr, 63 "warning: this program uses getstr(), which is unsafe.") 64 int 65 getstr(char *str) 66 { 67 return wgetstr(stdscr, str); 68 } 69 70 /* 71 * mvgetnstr -- 72 * Get a string (of maximum n) characters from stdscr starting at (y, x). 73 */ 74 int 75 mvgetnstr(int y, int x, char *str, int n) 76 { 77 return mvwgetnstr(stdscr, y, x, str, n); 78 } 79 80 /* 81 * mvgetstr -- 82 * Get a string from stdscr starting at (y, x). 83 */ 84 __warn_references(mvgetstr, 85 "warning: this program uses mvgetstr(), which is unsafe.") 86 int 87 mvgetstr(int y, int x, char *str) 88 { 89 return mvwgetstr(stdscr, y, x, str); 90 } 91 92 /* 93 * mvwgetnstr -- 94 * Get a string (of maximum n) characters from the given window starting 95 * at (y, x). 96 */ 97 int 98 mvwgetnstr(WINDOW *win, int y, int x, char *str, int n) 99 { 100 if (wmove(win, y, x) == ERR) 101 return ERR; 102 103 return wgetnstr(win, str, n); 104 } 105 106 /* 107 * mvwgetstr -- 108 * Get a string from the given window starting at (y, x). 109 */ 110 __warn_references(mvgetstr, 111 "warning: this program uses mvgetstr(), which is unsafe.") 112 int 113 mvwgetstr(WINDOW *win, int y, int x, char *str) 114 { 115 if (wmove(win, y, x) == ERR) 116 return ERR; 117 118 return wgetstr(win, str); 119 } 120 121 #endif 122 123 /* 124 * wgetstr -- 125 * Get a string starting at (cury, curx). 126 */ 127 __warn_references(wgetstr, 128 "warning: this program uses wgetstr(), which is unsafe.") 129 int 130 wgetstr(WINDOW *win, char *str) 131 { 132 return __wgetnstr(win, str, -1); 133 } 134 135 /* 136 * wgetnstr -- 137 * Get a string starting at (cury, curx). 138 * Note that n < 2 means that we return ERR (SUSv2 specification). 139 */ 140 int 141 wgetnstr(WINDOW *win, char *str, int n) 142 { 143 if (n < 1) 144 return (ERR); 145 if (n == 1) { 146 str[0] = '\0'; 147 return (ERR); 148 } 149 return __wgetnstr(win, str, n); 150 } 151 152 /* 153 * __wgetnstr -- 154 * The actual implementation. 155 * Note that we include a trailing '\0' for safety, so str will contain 156 * at most n - 1 other characters. 157 * XXX: character deletion from screen is based on how the characters 158 * are displayed by wgetch(). 159 */ 160 int 161 __wgetnstr(WINDOW *win, char *str, int n) 162 { 163 char *ostr, ec, kc; 164 int c, oldx, remain; 165 166 ostr = str; 167 ec = erasechar(); 168 kc = killchar(); 169 oldx = win->curx; 170 _DIAGASSERT(n == -1 || n > 1); 171 remain = n - 1; 172 173 while ((c = wgetch(win)) != ERR && c != '\n' && c != '\r') { 174 #ifdef DEBUG 175 __CTRACE(__CTRACE_INPUT, 176 "__wgetnstr: win %p, char 0x%x, remain %d\n", 177 win, c, remain); 178 #endif 179 *str = c; 180 touchline(win, win->cury, 1); 181 if (c == ec || c == KEY_BACKSPACE || c == KEY_LEFT) { 182 *str = '\0'; 183 if (str != ostr) { 184 if ((char) c == ec) { 185 mvwaddch(win, win->cury, win->curx, 186 ' '); 187 wmove(win, win->cury, win->curx - 1); 188 } 189 if (c == KEY_BACKSPACE || c == KEY_LEFT) { 190 /* getch() displays the key sequence */ 191 mvwaddch(win, win->cury, win->curx - 1, 192 ' '); 193 mvwaddch(win, win->cury, win->curx - 2, 194 ' '); 195 wmove(win, win->cury, win->curx - 1); 196 } 197 str--; 198 if (n != -1) { 199 /* We're counting chars */ 200 remain++; 201 } 202 } else { /* str == ostr */ 203 if (c == KEY_BACKSPACE || c == KEY_LEFT) 204 /* getch() displays the other keys */ 205 mvwaddch(win, win->cury, win->curx - 1, 206 ' '); 207 wmove(win, win->cury, oldx); 208 } 209 } else if (c == kc) { 210 *str = '\0'; 211 if (str != ostr) { 212 /* getch() displays the kill character */ 213 mvwaddch(win, win->cury, win->curx - 1, ' '); 214 /* Clear the characters from screen and str */ 215 while (str != ostr) { 216 mvwaddch(win, win->cury, win->curx - 1, 217 ' '); 218 wmove(win, win->cury, win->curx - 1); 219 str--; 220 if (n != -1) 221 /* We're counting chars */ 222 remain++; 223 } 224 mvwaddch(win, win->cury, win->curx - 1, ' '); 225 wmove(win, win->cury, win->curx - 1); 226 } else 227 /* getch() displays the kill character */ 228 mvwaddch(win, win->cury, oldx, ' '); 229 wmove(win, win->cury, oldx); 230 } else if (c >= KEY_MIN && c <= KEY_MAX) { 231 /* getch() displays these characters */ 232 mvwaddch(win, win->cury, win->curx - 1, ' '); 233 wmove(win, win->cury, win->curx - 1); 234 } else { 235 if (remain) { 236 str++; 237 remain--; 238 } else { 239 mvwaddch(win, win->cury, win->curx - 1, ' '); 240 wmove(win, win->cury, win->curx - 1); 241 } 242 } 243 } 244 245 if (c == ERR) { 246 *str = '\0'; 247 return (ERR); 248 } 249 *str = '\0'; 250 return (OK); 251 } 252