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