1 /* $NetBSD: in_wchstr.c,v 1.3 2009/07/22 16:57:14 roy 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: in_wchstr.c,v 1.3 2009/07/22 16:57:14 roy Exp $"); 40 #endif /* not lint */ 41 42 #include "curses.h" 43 #include "curses_private.h" 44 45 /* 46 * in_wchstr, in_wchnstr -- 47 * Return an array of wide characters at cursor position from stdscr. 48 */ 49 __warn_references(in_wchstr, 50 "warning: this program uses in_wchstr(), which is unsafe.") 51 int 52 in_wchstr(cchar_t *wchstr) 53 { 54 #ifndef HAVE_WCHAR 55 return ERR; 56 #else 57 return win_wchstr(stdscr, wchstr); 58 #endif /* HAVE_WCHAR */ 59 } 60 61 int 62 in_wchnstr(cchar_t *wchstr, int n) 63 { 64 #ifndef HAVE_WCHAR 65 return ERR; 66 #else 67 return win_wchnstr(stdscr, wchstr, n); 68 #endif /* HAVE_WCHAR */ 69 } 70 71 /* 72 * mvin_wchstr, mv_winchnstr -- 73 * Return an array of wide characters at position (y, x) from stdscr. 74 */ 75 __warn_references(mvin_wchstr, 76 "warning: this program uses mvin_wchstr(), which is unsafe.") 77 int 78 mvin_wchstr(int y, int x, cchar_t *wchstr) 79 { 80 #ifndef HAVE_WCHAR 81 return ERR; 82 #else 83 return mvwin_wchstr(stdscr, y, x, wchstr); 84 #endif /* HAVE_WCHAR */ 85 } 86 87 int 88 mvin_wchnstr(int y, int x, cchar_t *wchstr, int n) 89 { 90 #ifndef HAVE_WCHAR 91 return ERR; 92 #else 93 return mvwin_wchnstr(stdscr, y, x, wchstr, n); 94 #endif /* HAVE_WCHAR */ 95 } 96 97 /* 98 * mvwin_wchstr, mvwin_wchnstr -- 99 * Return an array wide characters at position (y, x) from the given window. 100 */ 101 __warn_references(mvwin_wchstr, 102 "warning: this program uses mvwin_wchstr(), which is unsafe.") 103 int 104 mvwin_wchstr(WINDOW *win, int y, int x, cchar_t *wchstr) 105 { 106 #ifndef HAVE_WCHAR 107 return ERR; 108 #else 109 if (wmove(win, y, x) == ERR) 110 return ERR; 111 112 return win_wchstr(win, wchstr); 113 #endif /* HAVE_WCHAR */ 114 } 115 116 int 117 mvwin_wchnstr(WINDOW *win, int y, int x, cchar_t *wchstr, int n) 118 { 119 #ifndef HAVE_WCHAR 120 return ERR; 121 #else 122 if (wmove(win, y, x) == ERR) 123 return ERR; 124 125 return win_wchnstr(win, wchstr, n); 126 #endif /* HAVE_WCHAR */ 127 } 128 129 /* 130 * win_wchstr, win_wchnstr -- 131 * Return an array of characters at cursor position. 132 */ 133 __warn_references(win_wchstr, 134 "warning: this program uses win_wchstr(), which is unsafe.") 135 int 136 win_wchstr(WINDOW *win, cchar_t *wchstr) 137 { 138 #ifndef HAVE_WCHAR 139 return ERR; 140 #else 141 142 return win_wchnstr(win, wchstr, -1); 143 #endif /* HAVE_WCHAR */ 144 } 145 146 /* 147 * - SUSv2/xcurses doesn't document whether the trailing 0 is included 148 * in the length count or not. For safety's sake it _is_ included. 149 */ 150 int 151 win_wchnstr(WINDOW *win, cchar_t *wchstr, int n) 152 { 153 #ifndef HAVE_WCHAR 154 return ERR; 155 #else 156 __LDATA *start; 157 int x = 0, cw = 0, cnt = 0; 158 cchar_t *wcp; 159 nschar_t *np; 160 161 if (wchstr == NULL) 162 return ERR; 163 164 start = &win->alines[win->cury]->line[win->curx]; 165 x = win->curx; 166 cw = WCOL(*start); 167 if (cw < 0) { 168 start += cw; 169 x += cw; 170 } 171 wcp = wchstr; 172 /* (n - 1) to leave room for the trailing 0 element */ 173 while ((x < win->maxx) && ((n < 0) || ((n > 1) && (cnt < n - 1)))) { 174 cw = WCOL( *start ); 175 wcp->vals[ 0 ] = start->ch; 176 wcp->attributes = start->attr; 177 wcp->elements = 1; 178 np = start->nsp; 179 if (np) { 180 do { 181 wcp->vals[ wcp->elements++ ] = np->ch; 182 np = np->next; 183 } while ( np ); 184 } 185 wcp++; 186 cnt++; 187 x += cw; 188 if ( x < win->maxx ) 189 start += cw; 190 } 191 wcp->vals[ 0 ] = L'\0'; 192 wcp->elements = 1; 193 wcp->attributes = win->wattr; 194 195 return OK; 196 #endif /* HAVE_WCHAR */ 197 } 198