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