197049Speter/**************************************************************************** 2184989Srafan * Copyright (c) 2002-2007,2008 Free Software Foundation, Inc. * 397049Speter * * 497049Speter * Permission is hereby granted, free of charge, to any person obtaining a * 597049Speter * copy of this software and associated documentation files (the * 697049Speter * "Software"), to deal in the Software without restriction, including * 797049Speter * without limitation the rights to use, copy, modify, merge, publish, * 897049Speter * distribute, distribute with modifications, sublicense, and/or sell * 997049Speter * copies of the Software, and to permit persons to whom the Software is * 1097049Speter * furnished to do so, subject to the following conditions: * 1197049Speter * * 1297049Speter * The above copyright notice and this permission notice shall be included * 1397049Speter * in all copies or substantial portions of the Software. * 1497049Speter * * 1597049Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 1697049Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 1797049Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 1897049Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 1997049Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 2097049Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 2197049Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 2297049Speter * * 2397049Speter * Except as contained in this notice, the name(s) of the above copyright * 2497049Speter * holders shall not be used in advertising or otherwise to promote the * 2597049Speter * sale, use or other dealings in this Software without prior written * 2697049Speter * authorization. * 2797049Speter ****************************************************************************/ 2897049Speter 2997049Speter/**************************************************************************** 3097049Speter * Author: Thomas E. Dickey 2002 * 3197049Speter ****************************************************************************/ 3297049Speter 3397049Speter/* 3497049Speter** lib_unget_wch.c 3597049Speter** 3697049Speter** The routine unget_wch(). 3797049Speter** 3897049Speter*/ 3997049Speter 4097049Speter#include <curses.priv.h> 4197049Speter 42184989SrafanMODULE_ID("$Id: lib_unget_wch.c,v 1.10 2008/06/07 14:50:37 tom Exp $") 4397049Speter 44166124Srafan/* 45174993Srafan * Wrapper for wcrtomb() which obtains the length needed for the given 46174993Srafan * wide-character 'source'. 47166124Srafan */ 48166124SrafanNCURSES_EXPORT(size_t) 49166124Srafan_nc_wcrtomb(char *target, wchar_t source, mbstate_t * state) 50166124Srafan{ 51174993Srafan int result; 52174993Srafan 53166124Srafan if (target == 0) { 54166124Srafan wchar_t temp[2]; 55166124Srafan const wchar_t *tempp = temp; 56166124Srafan temp[0] = source; 57166124Srafan temp[1] = 0; 58174993Srafan result = wcsrtombs(NULL, &tempp, 0, state); 59174993Srafan } else { 60174993Srafan result = wcrtomb(target, source, state); 61166124Srafan } 62174993Srafan if (!isEILSEQ(result) && (result == 0)) 63174993Srafan result = 1; 64174993Srafan return result; 65166124Srafan} 66166124Srafan 6797049SpeterNCURSES_EXPORT(int) 6897049Speterunget_wch(const wchar_t wch) 6997049Speter{ 7097049Speter int result = OK; 7197049Speter mbstate_t state; 7297049Speter size_t length; 7397049Speter int n; 7497049Speter 75166124Srafan T((T_CALLED("unget_wch(%#lx)"), (unsigned long) wch)); 7697049Speter 77166124Srafan init_mb(state); 78166124Srafan length = _nc_wcrtomb(0, wch, &state); 7997049Speter 8097049Speter if (length != (size_t) (-1) 8197049Speter && length != 0) { 82166124Srafan char *string; 8397049Speter 84166124Srafan if ((string = (char *) malloc(length)) != 0) { 85166124Srafan init_mb(state); 86166124Srafan wcrtomb(string, wch, &state); 8797049Speter 88166124Srafan for (n = (int) (length - 1); n >= 0; --n) { 89184989Srafan if (_nc_ungetch(SP, string[n]) != OK) { 90166124Srafan result = ERR; 91166124Srafan break; 92166124Srafan } 9397049Speter } 94166124Srafan free(string); 95166124Srafan } else { 96166124Srafan result = ERR; 9797049Speter } 9897049Speter } else { 9997049Speter result = ERR; 10097049Speter } 10197049Speter 10297049Speter returnCode(result); 10397049Speter} 104