1*51ffecc1SBen Gras /* $NetBSD: inwstr.c,v 1.3 2009/07/22 16:57:15 roy Exp $ */
2*51ffecc1SBen Gras
3*51ffecc1SBen Gras /*
4*51ffecc1SBen Gras * Copyright (c) 2005 The NetBSD Foundation Inc.
5*51ffecc1SBen Gras * All rights reserved.
6*51ffecc1SBen Gras *
7*51ffecc1SBen Gras * This code is derived from code donated to the NetBSD Foundation
8*51ffecc1SBen Gras * by Ruibiao Qiu <ruibiao@arl.wustl.edu,ruibiao@gmail.com>.
9*51ffecc1SBen Gras *
10*51ffecc1SBen Gras *
11*51ffecc1SBen Gras * Redistribution and use in source and binary forms, with or without
12*51ffecc1SBen Gras * modification, are permitted provided that the following conditions
13*51ffecc1SBen Gras * are met:
14*51ffecc1SBen Gras * 1. Redistributions of source code must retain the above copyright
15*51ffecc1SBen Gras * notice, this list of conditions and the following disclaimer.
16*51ffecc1SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
17*51ffecc1SBen Gras * notice, this list of conditions and the following disclaimer in the
18*51ffecc1SBen Gras * documentation and/or other materials provided with the distribution.
19*51ffecc1SBen Gras * 3. Neither the name of the NetBSD Foundation nor the names of its
20*51ffecc1SBen Gras * contributors may be used to endorse or promote products derived
21*51ffecc1SBen Gras * from this software without specific prior written permission.
22*51ffecc1SBen Gras *
23*51ffecc1SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
24*51ffecc1SBen Gras * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25*51ffecc1SBen Gras * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26*51ffecc1SBen Gras * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27*51ffecc1SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28*51ffecc1SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29*51ffecc1SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30*51ffecc1SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*51ffecc1SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32*51ffecc1SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33*51ffecc1SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*51ffecc1SBen Gras * SUCH DAMAGE.
35*51ffecc1SBen Gras */
36*51ffecc1SBen Gras
37*51ffecc1SBen Gras #include <sys/cdefs.h>
38*51ffecc1SBen Gras #ifndef lint
39*51ffecc1SBen Gras __RCSID("$NetBSD: inwstr.c,v 1.3 2009/07/22 16:57:15 roy Exp $");
40*51ffecc1SBen Gras #endif /* not lint */
41*51ffecc1SBen Gras
42*51ffecc1SBen Gras #include "curses.h"
43*51ffecc1SBen Gras #include "curses_private.h"
44*51ffecc1SBen Gras
45*51ffecc1SBen Gras /*
46*51ffecc1SBen Gras * inwstr, innwstr --
47*51ffecc1SBen Gras * Return a string of wide characters at cursor position from stdscr.
48*51ffecc1SBen Gras */
49*51ffecc1SBen Gras __warn_references(inwstr,
50*51ffecc1SBen Gras "warning: this program uses inwstr(), which is unsafe.")
51*51ffecc1SBen Gras int
inwstr(wchar_t * wstr)52*51ffecc1SBen Gras inwstr(wchar_t *wstr)
53*51ffecc1SBen Gras {
54*51ffecc1SBen Gras #ifndef HAVE_WCHAR
55*51ffecc1SBen Gras return ERR;
56*51ffecc1SBen Gras #else
57*51ffecc1SBen Gras return winwstr(stdscr, wstr);
58*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
59*51ffecc1SBen Gras }
60*51ffecc1SBen Gras
61*51ffecc1SBen Gras int
innwstr(wchar_t * wstr,int n)62*51ffecc1SBen Gras innwstr(wchar_t *wstr, int n)
63*51ffecc1SBen Gras {
64*51ffecc1SBen Gras #ifndef HAVE_WCHAR
65*51ffecc1SBen Gras return ERR;
66*51ffecc1SBen Gras #else
67*51ffecc1SBen Gras return winnwstr(stdscr, wstr, n);
68*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
69*51ffecc1SBen Gras }
70*51ffecc1SBen Gras
71*51ffecc1SBen Gras /*
72*51ffecc1SBen Gras * mvinwstr, mvinnwstr --
73*51ffecc1SBen Gras * Return a string of wide characters at position (y, x) from stdscr.
74*51ffecc1SBen Gras */
75*51ffecc1SBen Gras __warn_references(mvinwstr,
76*51ffecc1SBen Gras "warning: this program uses mvinwstr(), which is unsafe.")
77*51ffecc1SBen Gras int
mvinwstr(int y,int x,wchar_t * wstr)78*51ffecc1SBen Gras mvinwstr(int y, int x, wchar_t *wstr)
79*51ffecc1SBen Gras {
80*51ffecc1SBen Gras #ifndef HAVE_WCHAR
81*51ffecc1SBen Gras return ERR;
82*51ffecc1SBen Gras #else
83*51ffecc1SBen Gras return mvwinwstr(stdscr, y, x, wstr);
84*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
85*51ffecc1SBen Gras }
86*51ffecc1SBen Gras
87*51ffecc1SBen Gras int
mvinnwstr(int y,int x,wchar_t * wstr,int n)88*51ffecc1SBen Gras mvinnwstr(int y, int x, wchar_t *wstr, int n)
89*51ffecc1SBen Gras {
90*51ffecc1SBen Gras #ifndef HAVE_WCHAR
91*51ffecc1SBen Gras return ERR;
92*51ffecc1SBen Gras #else
93*51ffecc1SBen Gras return mvwinnwstr(stdscr, y, x, wstr, n);
94*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
95*51ffecc1SBen Gras }
96*51ffecc1SBen Gras
97*51ffecc1SBen Gras /*
98*51ffecc1SBen Gras * mvwinwstr, mvwinnwstr --
99*51ffecc1SBen Gras * Return an array wide characters at position (y, x) from the given window.
100*51ffecc1SBen Gras */
101*51ffecc1SBen Gras __warn_references(mvwinwstr,
102*51ffecc1SBen Gras "warning: this program uses mvwinwstr(), which is unsafe.")
103*51ffecc1SBen Gras int
mvwinwstr(WINDOW * win,int y,int x,wchar_t * wstr)104*51ffecc1SBen Gras mvwinwstr(WINDOW *win, int y, int x, wchar_t *wstr)
105*51ffecc1SBen Gras {
106*51ffecc1SBen Gras #ifndef HAVE_WCHAR
107*51ffecc1SBen Gras return ERR;
108*51ffecc1SBen Gras #else
109*51ffecc1SBen Gras if (wmove(win, y, x) == ERR)
110*51ffecc1SBen Gras return ERR;
111*51ffecc1SBen Gras
112*51ffecc1SBen Gras return winwstr(win, wstr);
113*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
114*51ffecc1SBen Gras }
115*51ffecc1SBen Gras
116*51ffecc1SBen Gras int
mvwinnwstr(WINDOW * win,int y,int x,wchar_t * wstr,int n)117*51ffecc1SBen Gras mvwinnwstr(WINDOW *win, int y, int x, wchar_t *wstr, int n)
118*51ffecc1SBen Gras {
119*51ffecc1SBen Gras #ifndef HAVE_WCHAR
120*51ffecc1SBen Gras return ERR;
121*51ffecc1SBen Gras #else
122*51ffecc1SBen Gras if (wmove(win, y, x) == ERR)
123*51ffecc1SBen Gras return ERR;
124*51ffecc1SBen Gras
125*51ffecc1SBen Gras return winnwstr(win, wstr, n);
126*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
127*51ffecc1SBen Gras }
128*51ffecc1SBen Gras
129*51ffecc1SBen Gras /*
130*51ffecc1SBen Gras * winwstr, winnwstr --
131*51ffecc1SBen Gras * Return a string of wide characters at cursor position.
132*51ffecc1SBen Gras */
133*51ffecc1SBen Gras __warn_references(winwstr,
134*51ffecc1SBen Gras "warning: this program uses winwstr(), which is unsafe.")
135*51ffecc1SBen Gras int
winwstr(WINDOW * win,wchar_t * wstr)136*51ffecc1SBen Gras winwstr(WINDOW *win, wchar_t *wstr)
137*51ffecc1SBen Gras {
138*51ffecc1SBen Gras #ifndef HAVE_WCHAR
139*51ffecc1SBen Gras return ERR;
140*51ffecc1SBen Gras #else
141*51ffecc1SBen Gras
142*51ffecc1SBen Gras return winnwstr(win, wstr, -1);
143*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
144*51ffecc1SBen Gras }
145*51ffecc1SBen Gras
146*51ffecc1SBen Gras /*
147*51ffecc1SBen Gras * - winnwstr() returns the number of characters copied only of if it is
148*51ffecc1SBen Gras * called with n >= 0 (ie, as in_wchnstr(), mvin_wchnstr(), mvwin_wchnstr()
149*51ffecc1SBen Gras * or win_wchnstr()). If N < 0, it returns `OK'.
150*51ffecc1SBen Gras * - SUSv2/xcurses doesn't document whether the trailing NUL is included
151*51ffecc1SBen Gras * in the length count or not. For safety's sake it _is_ included.
152*51ffecc1SBen Gras * - This implementation does not (yet) support multi-byte characters
153*51ffecc1SBen Gras * strings.
154*51ffecc1SBen Gras */
155*51ffecc1SBen Gras int
winnwstr(WINDOW * win,wchar_t * wstr,int n)156*51ffecc1SBen Gras winnwstr(WINDOW *win, wchar_t *wstr, int n)
157*51ffecc1SBen Gras {
158*51ffecc1SBen Gras #ifndef HAVE_WCHAR
159*51ffecc1SBen Gras return ERR;
160*51ffecc1SBen Gras #else
161*51ffecc1SBen Gras __LDATA *start;
162*51ffecc1SBen Gras int x, cw, cnt;
163*51ffecc1SBen Gras wchar_t *wcp;
164*51ffecc1SBen Gras
165*51ffecc1SBen Gras if (wstr == NULL)
166*51ffecc1SBen Gras return ERR;
167*51ffecc1SBen Gras
168*51ffecc1SBen Gras start = &win->alines[win->cury]->line[win->curx];
169*51ffecc1SBen Gras x = win->curx;
170*51ffecc1SBen Gras cw = WCOL( *start );
171*51ffecc1SBen Gras if (cw < 0) {
172*51ffecc1SBen Gras start += cw;
173*51ffecc1SBen Gras x += cw;
174*51ffecc1SBen Gras }
175*51ffecc1SBen Gras cnt = 0;
176*51ffecc1SBen Gras wcp = wstr;
177*51ffecc1SBen Gras /* (n - 1) to leave room for the trailing 0 element */
178*51ffecc1SBen Gras while ((x < win->maxx) && ((n < 0) || ((n > 1) && (cnt < n - 1)))) {
179*51ffecc1SBen Gras cw = WCOL( *start );
180*51ffecc1SBen Gras *wcp = start->ch;
181*51ffecc1SBen Gras wcp++;
182*51ffecc1SBen Gras cnt++;
183*51ffecc1SBen Gras x += cw;
184*51ffecc1SBen Gras if ( x < win->maxx )
185*51ffecc1SBen Gras start += cw;
186*51ffecc1SBen Gras }
187*51ffecc1SBen Gras *wcp = L'\0';
188*51ffecc1SBen Gras
189*51ffecc1SBen Gras if (n < 0)
190*51ffecc1SBen Gras return OK;
191*51ffecc1SBen Gras else
192*51ffecc1SBen Gras return cnt;
193*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
194*51ffecc1SBen Gras }
195