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