lib_unget_wch.c revision 262629
1316485Sdavidcs/****************************************************************************
2316485Sdavidcs * Copyright (c) 2002-2009,2010 Free Software Foundation, Inc.              *
3316485Sdavidcs *                                                                          *
4316485Sdavidcs * Permission is hereby granted, free of charge, to any person obtaining a  *
5316485Sdavidcs * copy of this software and associated documentation files (the            *
6316485Sdavidcs * "Software"), to deal in the Software without restriction, including      *
7316485Sdavidcs * without limitation the rights to use, copy, modify, merge, publish,      *
8316485Sdavidcs * distribute, distribute with modifications, sublicense, and/or sell       *
9316485Sdavidcs * copies of the Software, and to permit persons to whom the Software is    *
10316485Sdavidcs * furnished to do so, subject to the following conditions:                 *
11316485Sdavidcs *                                                                          *
12316485Sdavidcs * The above copyright notice and this permission notice shall be included  *
13316485Sdavidcs * in all copies or substantial portions of the Software.                   *
14316485Sdavidcs *                                                                          *
15316485Sdavidcs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16316485Sdavidcs * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17316485Sdavidcs * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18316485Sdavidcs * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19316485Sdavidcs * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20316485Sdavidcs * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21316485Sdavidcs * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22316485Sdavidcs *                                                                          *
23316485Sdavidcs * Except as contained in this notice, the name(s) of the above copyright   *
24316485Sdavidcs * holders shall not be used in advertising or otherwise to promote the     *
25316485Sdavidcs * sale, use or other dealings in this Software without prior written       *
26316485Sdavidcs * authorization.                                                           *
27316485Sdavidcs ****************************************************************************/
28316485Sdavidcs
29316485Sdavidcs/****************************************************************************
30316485Sdavidcs *  Author: Thomas E. Dickey 2002                                           *
31316485Sdavidcs ****************************************************************************/
32316485Sdavidcs
33316485Sdavidcs/*
34316485Sdavidcs**	lib_unget_wch.c
35316485Sdavidcs**
36316485Sdavidcs**	The routine unget_wch().
37316485Sdavidcs**
38316485Sdavidcs*/
39316485Sdavidcs
40316485Sdavidcs#include <curses.priv.h>
41316485Sdavidcs
42316485SdavidcsMODULE_ID("$Id: lib_unget_wch.c,v 1.14 2010/07/24 11:35:21 tom Exp $")
43316485Sdavidcs
44316485Sdavidcs/*
45316485Sdavidcs * Wrapper for wcrtomb() which obtains the length needed for the given
46316485Sdavidcs * wide-character 'source'.
47316485Sdavidcs */
48316485SdavidcsNCURSES_EXPORT(size_t)
49316485Sdavidcs_nc_wcrtomb(char *target, wchar_t source, mbstate_t * state)
50316485Sdavidcs{
51316485Sdavidcs    int result;
52316485Sdavidcs
53316485Sdavidcs    if (target == 0) {
54316485Sdavidcs	wchar_t temp[2];
55316485Sdavidcs	const wchar_t *tempp = temp;
56316485Sdavidcs	temp[0] = source;
57316485Sdavidcs	temp[1] = 0;
58316485Sdavidcs	result = (int) wcsrtombs(NULL, &tempp, 0, state);
59316485Sdavidcs    } else {
60316485Sdavidcs	result = (int) wcrtomb(target, source, state);
61316485Sdavidcs    }
62316485Sdavidcs    if (!isEILSEQ(result) && (result == 0))
63316485Sdavidcs	result = 1;
64316485Sdavidcs    return (size_t) result;
65316485Sdavidcs}
66316485Sdavidcs
67316485SdavidcsNCURSES_EXPORT(int)
68316485SdavidcsNCURSES_SP_NAME(unget_wch) (NCURSES_SP_DCLx const wchar_t wch)
69316485Sdavidcs{
70316485Sdavidcs    int result = OK;
71316485Sdavidcs    mbstate_t state;
72316485Sdavidcs    size_t length;
73316485Sdavidcs    int n;
74316485Sdavidcs
75316485Sdavidcs    T((T_CALLED("unget_wch(%p, %#lx)"), (void *) SP_PARM, (unsigned long) wch));
76316485Sdavidcs
77316485Sdavidcs    init_mb(state);
78316485Sdavidcs    length = _nc_wcrtomb(0, wch, &state);
79316485Sdavidcs
80316485Sdavidcs    if (length != (size_t) (-1)
81316485Sdavidcs	&& length != 0) {
82316485Sdavidcs	char *string;
83316485Sdavidcs
84316485Sdavidcs	if ((string = (char *) malloc(length)) != 0) {
85316485Sdavidcs	    init_mb(state);
86316485Sdavidcs	    /* ignore the result, since we already validated the character */
87316485Sdavidcs	    IGNORE_RC((int) wcrtomb(string, wch, &state));
88316485Sdavidcs
89316485Sdavidcs	    for (n = (int) (length - 1); n >= 0; --n) {
90316485Sdavidcs		if (NCURSES_SP_NAME(ungetch) (NCURSES_SP_ARGx
91316485Sdavidcs					      UChar(string[n])) !=OK) {
92316485Sdavidcs		    result = ERR;
93316485Sdavidcs		    break;
94316485Sdavidcs		}
95316485Sdavidcs	    }
96316485Sdavidcs	    free(string);
97316485Sdavidcs	} else {
98316485Sdavidcs	    result = ERR;
99316485Sdavidcs	}
100316485Sdavidcs    } else {
101316485Sdavidcs	result = ERR;
102316485Sdavidcs    }
103316485Sdavidcs
104316485Sdavidcs    returnCode(result);
105316485Sdavidcs}
106316485Sdavidcs
107316485Sdavidcs#if NCURSES_SP_FUNCS
108316485SdavidcsNCURSES_EXPORT(int)
109316485Sdavidcsunget_wch(const wchar_t wch)
110316485Sdavidcs{
111316485Sdavidcs    return NCURSES_SP_NAME(unget_wch) (CURRENT_SCREEN, wch);
112316485Sdavidcs}
113316485Sdavidcs#endif
114316485Sdavidcs