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