1 /* $OpenBSD: lib_cchar.c,v 1.2 2023/10/17 09:52:09 nicm Exp $ */ 2 3 /**************************************************************************** 4 * Copyright 2019-2021,2022 Thomas E. Dickey * 5 * Copyright 2001-2016,2017 Free Software Foundation, Inc. * 6 * * 7 * Permission is hereby granted, free of charge, to any person obtaining a * 8 * copy of this software and associated documentation files (the * 9 * "Software"), to deal in the Software without restriction, including * 10 * without limitation the rights to use, copy, modify, merge, publish, * 11 * distribute, distribute with modifications, sublicense, and/or sell * 12 * copies of the Software, and to permit persons to whom the Software is * 13 * furnished to do so, subject to the following conditions: * 14 * * 15 * The above copyright notice and this permission notice shall be included * 16 * in all copies or substantial portions of the Software. * 17 * * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 25 * * 26 * Except as contained in this notice, the name(s) of the above copyright * 27 * holders shall not be used in advertising or otherwise to promote the * 28 * sale, use or other dealings in this Software without prior written * 29 * authorization. * 30 ****************************************************************************/ 31 32 /* 33 ** lib_cchar.c 34 ** 35 ** The routines setcchar() and getcchar(). 36 ** 37 */ 38 39 #include <curses.priv.h> 40 #include <wchar.h> 41 42 MODULE_ID("$Id: lib_cchar.c,v 1.2 2023/10/17 09:52:09 nicm Exp $") 43 44 /* 45 * The SuSv2 description leaves some room for interpretation. We'll assume wch 46 * points to a string which is L'\0' terminated, contains at least one 47 * character with strictly positive width, which must be the first, and 48 * contains no characters of negative width. 49 */ 50 NCURSES_EXPORT(int) 51 setcchar(cchar_t *wcval, 52 const wchar_t *wch, 53 const attr_t attrs, 54 NCURSES_PAIRS_T pair_arg, 55 const void *opts) 56 { 57 int code = OK; 58 int color_pair = pair_arg; 59 unsigned len; 60 61 TR(TRACE_CCALLS, (T_CALLED("setcchar(%p,%s,attrs=%lu,pair=%d,%p)"), 62 (void *) wcval, _nc_viswbuf(wch), 63 (unsigned long) attrs, color_pair, opts)); 64 65 set_extended_pair(opts, color_pair); 66 if (wch == NULL 67 || ((len = (unsigned) wcslen(wch)) > 1 && _nc_wacs_width(wch[0]) < 0) 68 || color_pair < 0) { 69 code = ERR; 70 } else { 71 unsigned i; 72 73 if (len > CCHARW_MAX) 74 len = CCHARW_MAX; 75 76 /* 77 * If we have a following spacing-character, stop at that point. We 78 * are only interested in adding non-spacing characters. 79 */ 80 for (i = 1; i < len; ++i) { 81 if (_nc_wacs_width(wch[i]) != 0) { 82 len = i; 83 break; 84 } 85 } 86 87 memset(wcval, 0, sizeof(*wcval)); 88 89 if (len != 0) { 90 SetAttr(*wcval, attrs); 91 SetPair(CHDEREF(wcval), color_pair); 92 memcpy(&wcval->chars, wch, len * sizeof(wchar_t)); 93 TR(TRACE_CCALLS, ("copy %d wchars, first is %s", len, 94 _tracecchar_t(wcval))); 95 } 96 } 97 98 TR(TRACE_CCALLS, (T_RETURN("%d"), code)); 99 return (code); 100 } 101 102 NCURSES_EXPORT(int) 103 getcchar(const cchar_t *wcval, 104 wchar_t *wch, 105 attr_t *attrs, 106 NCURSES_PAIRS_T *pair_arg, 107 void *opts) 108 { 109 int code = ERR; 110 111 TR(TRACE_CCALLS, (T_CALLED("getcchar(%p,%p,%p,%p,%p)"), 112 (const void *) wcval, 113 (void *) wch, 114 (void *) attrs, 115 (void *) pair_arg, 116 opts)); 117 118 #if !NCURSES_EXT_COLORS 119 if (opts != NULL) { 120 ; /* empty */ 121 } else 122 #endif 123 if (wcval != NULL) { 124 wchar_t *wp; 125 int len; 126 127 #if HAVE_WMEMCHR 128 len = ((wp = wmemchr(wcval->chars, L'\0', (size_t) CCHARW_MAX)) 129 ? (int) (wp - wcval->chars) 130 : CCHARW_MAX); 131 #else 132 len = wcsnlen(wcval->chars, CCHARW_MAX); 133 #endif 134 if (wch == NULL) { 135 /* 136 * If the value is a null, set the length to 1. 137 * If the value is not a null, return the length plus 1 for null. 138 */ 139 code = (len < CCHARW_MAX) ? (len + 1) : CCHARW_MAX; 140 } else if (attrs == 0 || pair_arg == 0) { 141 code = ERR; 142 } else if (len >= 0) { 143 int color_pair; 144 145 TR(TRACE_CCALLS, ("copy %d wchars, first is %s", len, 146 _tracecchar_t(wcval))); 147 *attrs = AttrOf(*wcval) & A_ATTRIBUTES; 148 color_pair = GetPair(*wcval); 149 get_extended_pair(opts, color_pair); 150 *pair_arg = limit_PAIRS(color_pair); 151 wmemcpy(wch, wcval->chars, (size_t) len); 152 wch[len] = L'\0'; 153 if (*pair_arg >= 0) 154 code = OK; 155 } 156 } 157 158 TR(TRACE_CCALLS, (T_RETURN("%d"), code)); 159 return (code); 160 } 161