1 /* $OpenBSD: lib_unget_wch.c,v 1.2 2023/10/17 09:52:09 nicm Exp $ */ 2 3 /**************************************************************************** 4 * Copyright 2020,2023 Thomas E. Dickey * 5 * Copyright 2002-2011,2016 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 * Author: Thomas E. Dickey 2002 * 34 ****************************************************************************/ 35 36 /* 37 ** lib_unget_wch.c 38 ** 39 ** The routine unget_wch(). 40 ** 41 */ 42 43 #include <curses.priv.h> 44 45 MODULE_ID("$Id: lib_unget_wch.c,v 1.2 2023/10/17 09:52:09 nicm Exp $") 46 47 /* 48 * Wrapper for wcrtomb() which obtains the length needed for the given 49 * wide-character 'source'. 50 */ 51 NCURSES_EXPORT(size_t) 52 _nc_wcrtomb(char *target, wchar_t source, mbstate_t * state) 53 { 54 int result; 55 56 if (target == 0) { 57 wchar_t temp[2]; 58 const wchar_t *tempp = temp; 59 temp[0] = source; 60 temp[1] = 0; 61 result = (int) wcsrtombs(NULL, &tempp, (size_t) 0, state); 62 } else { 63 result = (int) wcrtomb(target, source, state); 64 } 65 if (!isEILSEQ(result) && ((result == 0) || (result > MB_LEN_MAX))) 66 result = 1; 67 return (size_t) result; 68 } 69 70 NCURSES_EXPORT(int) 71 NCURSES_SP_NAME(unget_wch) (NCURSES_SP_DCLx const wchar_t wch) 72 { 73 int result = OK; 74 mbstate_t state; 75 size_t length; 76 77 T((T_CALLED("unget_wch(%p, %#lx)"), (void *) SP_PARM, (unsigned long) wch)); 78 79 init_mb(state); 80 length = _nc_wcrtomb(0, wch, &state); 81 82 if (length != (size_t) (-1) 83 && length != 0) { 84 char *string; 85 86 if ((string = (char *) malloc(length)) != 0) { 87 int n; 88 89 init_mb(state); 90 /* ignore the result, since we already validated the character */ 91 IGNORE_RC((int) wcrtomb(string, wch, &state)); 92 93 for (n = (int) (length - 1); n >= 0; --n) { 94 if (NCURSES_SP_NAME(ungetch) (NCURSES_SP_ARGx 95 UChar(string[n])) !=OK) { 96 result = ERR; 97 break; 98 } 99 } 100 free(string); 101 } else { 102 result = ERR; 103 } 104 } else { 105 result = ERR; 106 } 107 108 returnCode(result); 109 } 110 111 #if NCURSES_SP_FUNCS 112 NCURSES_EXPORT(int) 113 unget_wch(const wchar_t wch) 114 { 115 return NCURSES_SP_NAME(unget_wch) (CURRENT_SCREEN, wch); 116 } 117 #endif 118