197049Speter/****************************************************************************
2262685Sdelphij * Copyright (c) 2002-2010,2011 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
42262685SdelphijMODULE_ID("$Id: lib_unget_wch.c,v 1.15 2011/10/22 16:34:50 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;
58262685Sdelphij	result = (int) wcsrtombs(NULL, &tempp, (size_t) 0, state);
59174993Srafan    } else {
60262629Sdelphij	result = (int) wcrtomb(target, source, state);
61166124Srafan    }
62174993Srafan    if (!isEILSEQ(result) && (result == 0))
63174993Srafan	result = 1;
64262629Sdelphij    return (size_t) result;
65166124Srafan}
66166124Srafan
6797049SpeterNCURSES_EXPORT(int)
68262629SdelphijNCURSES_SP_NAME(unget_wch) (NCURSES_SP_DCLx const wchar_t wch)
6997049Speter{
7097049Speter    int result = OK;
7197049Speter    mbstate_t state;
7297049Speter    size_t length;
7397049Speter    int n;
7497049Speter
75262629Sdelphij    T((T_CALLED("unget_wch(%p, %#lx)"), (void *) SP_PARM, (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);
86262629Sdelphij	    /* ignore the result, since we already validated the character */
87262629Sdelphij	    IGNORE_RC((int) wcrtomb(string, wch, &state));
8897049Speter
89166124Srafan	    for (n = (int) (length - 1); n >= 0; --n) {
90262629Sdelphij		if (NCURSES_SP_NAME(ungetch) (NCURSES_SP_ARGx
91262629Sdelphij					      UChar(string[n])) !=OK) {
92166124Srafan		    result = ERR;
93166124Srafan		    break;
94166124Srafan		}
9597049Speter	    }
96166124Srafan	    free(string);
97166124Srafan	} else {
98166124Srafan	    result = ERR;
9997049Speter	}
10097049Speter    } else {
10197049Speter	result = ERR;
10297049Speter    }
10397049Speter
10497049Speter    returnCode(result);
10597049Speter}
106262629Sdelphij
107262629Sdelphij#if NCURSES_SP_FUNCS
108262629SdelphijNCURSES_EXPORT(int)
109262629Sdelphijunget_wch(const wchar_t wch)
110262629Sdelphij{
111262629Sdelphij    return NCURSES_SP_NAME(unget_wch) (CURRENT_SCREEN, wch);
112262629Sdelphij}
113262629Sdelphij#endif
114