1 /* $NetBSD: inwstr.c,v 1.11 2024/12/23 02:58:03 blymn 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: inwstr.c,v 1.11 2024/12/23 02:58:03 blymn Exp $"); 40 #endif /* not lint */ 41 42 #include "curses.h" 43 #include "curses_private.h" 44 45 /* 46 * inwstr, innwstr -- 47 * Return a string of wide characters at cursor position from stdscr. 48 */ 49 __warn_references(inwstr, 50 "warning: this program uses inwstr(), which is unsafe.") 51 int 52 inwstr(wchar_t *wstr) 53 { 54 return winwstr(stdscr, wstr); 55 } 56 57 int 58 innwstr(wchar_t *wstr, int n) 59 { 60 return winnwstr(stdscr, wstr, n); 61 } 62 63 /* 64 * mvinwstr, mvinnwstr -- 65 * Return a string of wide characters at position (y, x) from stdscr. 66 */ 67 __warn_references(mvinwstr, 68 "warning: this program uses mvinwstr(), which is unsafe.") 69 int 70 mvinwstr(int y, int x, wchar_t *wstr) 71 { 72 return mvwinwstr(stdscr, y, x, wstr); 73 } 74 75 int 76 mvinnwstr(int y, int x, wchar_t *wstr, int n) 77 { 78 return mvwinnwstr(stdscr, y, x, wstr, n); 79 } 80 81 /* 82 * mvwinwstr, mvwinnwstr -- 83 * Return an array wide characters at position (y, x) from the given window. 84 */ 85 __warn_references(mvwinwstr, 86 "warning: this program uses mvwinwstr(), which is unsafe.") 87 int 88 mvwinwstr(WINDOW *win, int y, int x, wchar_t *wstr) 89 { 90 if (wmove(win, y, x) == ERR) 91 return ERR; 92 93 return winwstr(win, wstr); 94 } 95 96 int 97 mvwinnwstr(WINDOW *win, int y, int x, wchar_t *wstr, int n) 98 { 99 if (wmove(win, y, x) == ERR) 100 return ERR; 101 102 return winnwstr(win, wstr, n); 103 } 104 105 /* 106 * winwstr, winnwstr -- 107 * Return a string of wide characters at cursor position. 108 */ 109 __warn_references(winwstr, 110 "warning: this program uses winwstr(), which is unsafe.") 111 int 112 winwstr(WINDOW *win, wchar_t *wstr) 113 { 114 115 return winnwstr(win, wstr, -1); 116 } 117 118 /* 119 * - winnwstr() returns the number of characters copied only of if it is 120 * called with n >= 0 (ie, as in_wchnstr(), mvin_wchnstr(), mvwin_wchnstr() 121 * or win_wchnstr()). If N < 0, it returns `OK'. 122 * - SUSv2/xcurses doesn't document whether the trailing NUL is included 123 * in the length count or not. For safety's sake it _is_ included. 124 * - This implementation does not (yet) support multi-byte characters 125 * strings. 126 */ 127 int 128 winnwstr(WINDOW *win, wchar_t *wstr, int n) 129 { 130 __LDATA *start; 131 int x, cw, cnt; 132 wchar_t *wcp; 133 134 if (__predict_false(win == NULL)) 135 return ERR; 136 137 if (wstr == NULL) 138 return ERR; 139 140 start = &win->alines[win->cury]->line[win->curx]; 141 x = win->curx; 142 cw = start->wcols; 143 if (cw < 0) { 144 start += cw; 145 x += cw; 146 } 147 cnt = 0; 148 wcp = wstr; 149 /* (n - 1) to leave room for the trailing 0 element */ 150 while ((x < win->maxx) && ((n < 0) || ((n > 1) && (cnt < n - 1)))) { 151 cw = start->wcols; 152 *wcp = start->ch; 153 wcp++; 154 cnt++; 155 x += cw; 156 if (x < win->maxx) 157 start += cw; 158 } 159 *wcp = L'\0'; 160 161 if (n < 0) 162 return OK; 163 else 164 return cnt; 165 } 166