1 /* $NetBSD: getstr.c,v 1.18 2003/08/07 16:44:21 agc 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.18 2003/08/07 16:44:21 agc 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("__wgetnstr: win %p, char 0x%x, remain %d\n", win, 176 c, remain); 177 #endif 178 *str = c; 179 touchline(win, win->cury, 1); 180 if (c == ec || c == KEY_BACKSPACE || c == KEY_LEFT) { 181 *str = '\0'; 182 if (str != ostr) { 183 if ((char) c == ec) { 184 mvwaddch(win, win->cury, win->curx, 185 ' '); 186 wmove(win, win->cury, win->curx - 1); 187 } 188 if (c == KEY_BACKSPACE || c == KEY_LEFT) { 189 /* getch() displays the key sequence */ 190 mvwaddch(win, win->cury, win->curx - 1, 191 ' '); 192 mvwaddch(win, win->cury, win->curx - 2, 193 ' '); 194 wmove(win, win->cury, win->curx - 1); 195 } 196 str--; 197 if (n != -1) { 198 /* We're counting chars */ 199 remain++; 200 } 201 } else { /* str == ostr */ 202 if (c == KEY_BACKSPACE || c == KEY_LEFT) 203 /* getch() displays the other keys */ 204 mvwaddch(win, win->cury, win->curx - 1, 205 ' '); 206 wmove(win, win->cury, oldx); 207 } 208 } else if (c == kc) { 209 *str = '\0'; 210 if (str != ostr) { 211 /* getch() displays the kill character */ 212 mvwaddch(win, win->cury, win->curx - 1, ' '); 213 /* Clear the characters from screen and str */ 214 while (str != ostr) { 215 mvwaddch(win, win->cury, win->curx - 1, 216 ' '); 217 wmove(win, win->cury, win->curx - 1); 218 str--; 219 if (n != -1) 220 /* We're counting chars */ 221 remain++; 222 } 223 mvwaddch(win, win->cury, win->curx - 1, ' '); 224 wmove(win, win->cury, win->curx - 1); 225 } else 226 /* getch() displays the kill character */ 227 mvwaddch(win, win->cury, oldx, ' '); 228 wmove(win, win->cury, oldx); 229 } else if (c >= KEY_MIN && c <= KEY_MAX) { 230 /* getch() displays these characters */ 231 mvwaddch(win, win->cury, win->curx - 1, ' '); 232 wmove(win, win->cury, win->curx - 1); 233 } else { 234 if (remain) { 235 str++; 236 remain--; 237 } else { 238 mvwaddch(win, win->cury, win->curx - 1, ' '); 239 wmove(win, win->cury, win->curx - 1); 240 } 241 } 242 } 243 244 if (c == ERR) { 245 *str = '\0'; 246 return (ERR); 247 } 248 *str = '\0'; 249 return (OK); 250 } 251