1*51ffecc1SBen Gras /* $NetBSD: cchar.c,v 1.5 2010/12/16 17:42:28 wiz 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: cchar.c,v 1.5 2010/12/16 17:42:28 wiz Exp $");
40*51ffecc1SBen Gras #endif /* not lint */
41*51ffecc1SBen Gras
42*51ffecc1SBen Gras #include <string.h>
43*51ffecc1SBen Gras
44*51ffecc1SBen Gras #include "curses.h"
45*51ffecc1SBen Gras #include "curses_private.h"
46*51ffecc1SBen Gras
47*51ffecc1SBen Gras /*
48*51ffecc1SBen Gras * getcchar --
49*51ffecc1SBen Gras * get a wide-character string and rendition from a cchar_t
50*51ffecc1SBen Gras */
51*51ffecc1SBen Gras int
getcchar(const cchar_t * wcval,wchar_t * wch,attr_t * attrs,short * color_pair,void * opts)52*51ffecc1SBen Gras getcchar(const cchar_t *wcval, wchar_t *wch, attr_t *attrs,
53*51ffecc1SBen Gras short *color_pair, void *opts)
54*51ffecc1SBen Gras {
55*51ffecc1SBen Gras #ifndef HAVE_WCHAR
56*51ffecc1SBen Gras return ERR;
57*51ffecc1SBen Gras #else
58*51ffecc1SBen Gras wchar_t *wp;
59*51ffecc1SBen Gras size_t len;
60*51ffecc1SBen Gras
61*51ffecc1SBen Gras if ( opts )
62*51ffecc1SBen Gras return ERR;
63*51ffecc1SBen Gras
64*51ffecc1SBen Gras len = (wp = wmemchr(wcval->vals, L'\0', CCHARW_MAX))
65*51ffecc1SBen Gras ? wp - wcval->vals : CCHARW_MAX;
66*51ffecc1SBen Gras
67*51ffecc1SBen Gras if (wch == NULL)
68*51ffecc1SBen Gras return (int) len;
69*51ffecc1SBen Gras if (attrs == 0 || color_pair == 0)
70*51ffecc1SBen Gras return ERR;
71*51ffecc1SBen Gras if (len > 0) {
72*51ffecc1SBen Gras *attrs = wcval->attributes;
73*51ffecc1SBen Gras *color_pair = COLOR_PAIR( wcval -> attributes );
74*51ffecc1SBen Gras wmemcpy(wch, wcval->vals, (unsigned) len);
75*51ffecc1SBen Gras wch[len] = L'\0';
76*51ffecc1SBen Gras }
77*51ffecc1SBen Gras return OK;
78*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
79*51ffecc1SBen Gras }
80*51ffecc1SBen Gras
81*51ffecc1SBen Gras /*
82*51ffecc1SBen Gras * setcchar --
83*51ffecc1SBen Gras * set cchar_t from a wide-character string and rendition
84*51ffecc1SBen Gras */
85*51ffecc1SBen Gras int
setcchar(cchar_t * wcval,const wchar_t * wch,const attr_t attrs,short color_pair,const void * opts)86*51ffecc1SBen Gras setcchar(cchar_t *wcval, const wchar_t *wch, const attr_t attrs,
87*51ffecc1SBen Gras short color_pair, const void *opts)
88*51ffecc1SBen Gras {
89*51ffecc1SBen Gras #ifndef HAVE_WCHAR
90*51ffecc1SBen Gras return ERR;
91*51ffecc1SBen Gras #else
92*51ffecc1SBen Gras int i;
93*51ffecc1SBen Gras size_t len;
94*51ffecc1SBen Gras
95*51ffecc1SBen Gras if (opts || (len = wcslen(wch)) > CCHARW_MAX
96*51ffecc1SBen Gras || (len > 1 && wcwidth(wch[0]) < 0)) {
97*51ffecc1SBen Gras return ERR;
98*51ffecc1SBen Gras }
99*51ffecc1SBen Gras
100*51ffecc1SBen Gras /*
101*51ffecc1SBen Gras * If we have a following spacing-character, stop at that point. We
102*51ffecc1SBen Gras * are only interested in adding non-spacing characters.
103*51ffecc1SBen Gras */
104*51ffecc1SBen Gras for (i = 1; i < len; ++i) {
105*51ffecc1SBen Gras if (wcwidth(wch[i]) != 0) {
106*51ffecc1SBen Gras len = i;
107*51ffecc1SBen Gras break;
108*51ffecc1SBen Gras }
109*51ffecc1SBen Gras }
110*51ffecc1SBen Gras
111*51ffecc1SBen Gras memset(wcval, 0, sizeof(*wcval));
112*51ffecc1SBen Gras if (len != 0) {
113*51ffecc1SBen Gras wcval -> attributes = attrs | color_pair;
114*51ffecc1SBen Gras wcval -> elements = 1;
115*51ffecc1SBen Gras memcpy(&wcval->vals, wch, len * sizeof(wchar_t));
116*51ffecc1SBen Gras }
117*51ffecc1SBen Gras
118*51ffecc1SBen Gras return OK;
119*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
120*51ffecc1SBen Gras }
121*51ffecc1SBen Gras
122*51ffecc1SBen Gras void
__cursesi_chtype_to_cchar(chtype in,cchar_t * out)123*51ffecc1SBen Gras __cursesi_chtype_to_cchar(chtype in, cchar_t *out)
124*51ffecc1SBen Gras {
125*51ffecc1SBen Gras unsigned int idx;
126*51ffecc1SBen Gras
127*51ffecc1SBen Gras if (in & __ACS_IS_WACS) {
128*51ffecc1SBen Gras idx = in & __CHARTEXT;
129*51ffecc1SBen Gras if (idx < NUM_ACS) {
130*51ffecc1SBen Gras memcpy(out, &_wacs_char[idx], sizeof(cchar_t));
131*51ffecc1SBen Gras out->attributes |= in & __ATTRIBUTES;
132*51ffecc1SBen Gras return;
133*51ffecc1SBen Gras }
134*51ffecc1SBen Gras }
135*51ffecc1SBen Gras out->vals[0] = in & __CHARTEXT;
136*51ffecc1SBen Gras out->attributes = in & __ATTRIBUTES;
137*51ffecc1SBen Gras out->elements = 1;
138*51ffecc1SBen Gras }
139