1 |
|
|
/* $OpenBSD: lib_unget_wch.c,v 1.1 2010/09/06 17:26:17 nicm Exp $ */ |
2 |
|
|
|
3 |
|
|
/**************************************************************************** |
4 |
|
|
* Copyright (c) 2002-2007,2008 Free Software Foundation, Inc. * |
5 |
|
|
* * |
6 |
|
|
* Permission is hereby granted, free of charge, to any person obtaining a * |
7 |
|
|
* copy of this software and associated documentation files (the * |
8 |
|
|
* "Software"), to deal in the Software without restriction, including * |
9 |
|
|
* without limitation the rights to use, copy, modify, merge, publish, * |
10 |
|
|
* distribute, distribute with modifications, sublicense, and/or sell * |
11 |
|
|
* copies of the Software, and to permit persons to whom the Software is * |
12 |
|
|
* furnished to do so, subject to the following conditions: * |
13 |
|
|
* * |
14 |
|
|
* The above copyright notice and this permission notice shall be included * |
15 |
|
|
* in all copies or substantial portions of the Software. * |
16 |
|
|
* * |
17 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * |
18 |
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * |
19 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * |
20 |
|
|
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * |
21 |
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * |
22 |
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * |
23 |
|
|
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. * |
24 |
|
|
* * |
25 |
|
|
* Except as contained in this notice, the name(s) of the above copyright * |
26 |
|
|
* holders shall not be used in advertising or otherwise to promote the * |
27 |
|
|
* sale, use or other dealings in this Software without prior written * |
28 |
|
|
* authorization. * |
29 |
|
|
****************************************************************************/ |
30 |
|
|
|
31 |
|
|
/**************************************************************************** |
32 |
|
|
* Author: Thomas E. Dickey 2002 * |
33 |
|
|
****************************************************************************/ |
34 |
|
|
|
35 |
|
|
/* |
36 |
|
|
** lib_unget_wch.c |
37 |
|
|
** |
38 |
|
|
** The routine unget_wch(). |
39 |
|
|
** |
40 |
|
|
*/ |
41 |
|
|
|
42 |
|
|
#include <curses.priv.h> |
43 |
|
|
|
44 |
|
|
MODULE_ID("$Id: lib_unget_wch.c,v 1.1 2010/09/06 17:26:17 nicm Exp $") |
45 |
|
|
|
46 |
|
|
/* |
47 |
|
|
* Wrapper for wcrtomb() which obtains the length needed for the given |
48 |
|
|
* wide-character 'source'. |
49 |
|
|
*/ |
50 |
|
|
NCURSES_EXPORT(size_t) |
51 |
|
|
_nc_wcrtomb(char *target, wchar_t source, mbstate_t * state) |
52 |
|
|
{ |
53 |
|
|
int result; |
54 |
|
|
|
55 |
|
|
if (target == 0) { |
56 |
|
|
wchar_t temp[2]; |
57 |
|
|
const wchar_t *tempp = temp; |
58 |
|
|
temp[0] = source; |
59 |
|
|
temp[1] = 0; |
60 |
|
|
result = wcsrtombs(NULL, &tempp, 0, state); |
61 |
|
|
} else { |
62 |
|
|
result = wcrtomb(target, source, state); |
63 |
|
|
} |
64 |
|
|
if (!isEILSEQ(result) && (result == 0)) |
65 |
|
|
result = 1; |
66 |
|
|
return result; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
NCURSES_EXPORT(int) |
70 |
|
|
unget_wch(const wchar_t wch) |
71 |
|
|
{ |
72 |
|
|
int result = OK; |
73 |
|
|
mbstate_t state; |
74 |
|
|
size_t length; |
75 |
|
|
int n; |
76 |
|
|
|
77 |
|
|
T((T_CALLED("unget_wch(%#lx)"), (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 |
|
|
init_mb(state); |
88 |
|
|
wcrtomb(string, wch, &state); |
89 |
|
|
|
90 |
|
|
for (n = (int) (length - 1); n >= 0; --n) { |
91 |
|
|
if (_nc_ungetch(SP, string[n]) != OK) { |
92 |
|
|
result = ERR; |
93 |
|
|
break; |
94 |
|
|
} |
95 |
|
|
} |
96 |
|
|
free(string); |
97 |
|
|
} else { |
98 |
|
|
result = ERR; |
99 |
|
|
} |
100 |
|
|
} else { |
101 |
|
|
result = ERR; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
returnCode(result); |
105 |
|
|
} |