libncurses: updated to 6.0
[tomato.git] / release / src / router / libncurses / ncurses / tinfo / obsolete.c
blob63476dc7987910db960fe37a7b6ea40c29fb5087
1 /****************************************************************************
2 * Copyright (c) 2013,2014 Free Software Foundation, Inc. *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey 2013-on *
31 ****************************************************************************/
34 ** Support for obsolete/unusual features.
37 #include <curses.priv.h>
39 MODULE_ID("$Id: obsolete.c,v 1.3 2014/10/11 02:39:35 tom Exp $")
42 * Obsolete entrypoint retained for binary compatbility.
44 NCURSES_EXPORT(void)
45 NCURSES_SP_NAME(_nc_set_buffer) (NCURSES_SP_DCLx FILE *ofp, int buffered)
47 #if NCURSES_SP_FUNCS
48 (void) SP_PARM;
49 #endif
50 (void) ofp;
51 (void) buffered;
54 #if NCURSES_SP_FUNCS
55 NCURSES_EXPORT(void)
56 _nc_set_buffer(FILE *ofp, int buffered)
58 NCURSES_SP_NAME(_nc_set_buffer) (CURRENT_SCREEN, ofp, buffered);
60 #endif
62 #if !HAVE_STRDUP
63 NCURSES_EXPORT(char *)
64 _nc_strdup(const char *s)
66 char *result = 0;
67 if (s != 0) {
68 size_t need = strlen(s);
69 result = malloc(need + 1);
70 if (result != 0) {
71 strcpy(result, s);
74 return result;
76 #endif
78 #if USE_MY_MEMMOVE
79 #define DST ((char *)s1)
80 #define SRC ((const char *)s2)
81 NCURSES_EXPORT(void *)
82 _nc_memmove(void *s1, const void *s2, size_t n)
84 if (n != 0) {
85 if ((DST + n > SRC) && (SRC + n > DST)) {
86 static char *bfr;
87 static size_t length;
88 register size_t j;
89 if (length < n) {
90 length = (n * 3) / 2;
91 bfr = typeRealloc(char, length, bfr);
93 for (j = 0; j < n; j++)
94 bfr[j] = SRC[j];
95 s2 = bfr;
97 while (n-- != 0)
98 DST[n] = SRC[n];
100 return s1;
102 #endif /* USE_MY_MEMMOVE */
104 #ifdef EXP_XTERM_1005
105 NCURSES_EXPORT(int)
106 _nc_conv_to_utf8(unsigned char *target, unsigned source, unsigned limit)
108 #define CH(n) UChar((source) >> ((n) * 8))
109 int rc = 0;
111 if (source <= 0x0000007f)
112 rc = 1;
113 else if (source <= 0x000007ff)
114 rc = 2;
115 else if (source <= 0x0000ffff)
116 rc = 3;
117 else if (source <= 0x001fffff)
118 rc = 4;
119 else if (source <= 0x03ffffff)
120 rc = 5;
121 else /* (source <= 0x7fffffff) */
122 rc = 6;
124 if ((unsigned) rc > limit) { /* whatever it is, we cannot decode it */
125 rc = 0;
128 if (target != 0) {
129 switch (rc) {
130 case 1:
131 target[0] = CH(0);
132 break;
134 case 2:
135 target[1] = UChar(0x80 | (CH(0) & 0x3f));
136 target[0] = UChar(0xc0 | (CH(0) >> 6) | ((CH(1) & 0x07) << 2));
137 break;
139 case 3:
140 target[2] = UChar(0x80 | (CH(0) & 0x3f));
141 target[1] = UChar(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
142 target[0] = UChar(0xe0 | ((int) (CH(1) & 0xf0) >> 4));
143 break;
145 case 4:
146 target[3] = UChar(0x80 | (CH(0) & 0x3f));
147 target[2] = UChar(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
148 target[1] = UChar(0x80 |
149 ((int) (CH(1) & 0xf0) >> 4) |
150 ((int) (CH(2) & 0x03) << 4));
151 target[0] = UChar(0xf0 | ((int) (CH(2) & 0x1f) >> 2));
152 break;
154 case 5:
155 target[4] = UChar(0x80 | (CH(0) & 0x3f));
156 target[3] = UChar(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
157 target[2] = UChar(0x80 |
158 ((int) (CH(1) & 0xf0) >> 4) |
159 ((int) (CH(2) & 0x03) << 4));
160 target[1] = UChar(0x80 | (CH(2) >> 2));
161 target[0] = UChar(0xf8 | (CH(3) & 0x03));
162 break;
164 case 6:
165 target[5] = UChar(0x80 | (CH(0) & 0x3f));
166 target[4] = UChar(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
167 target[3] = UChar(0x80 | (CH(1) >> 4) | ((CH(2) & 0x03) << 4));
168 target[2] = UChar(0x80 | (CH(2) >> 2));
169 target[1] = UChar(0x80 | (CH(3) & 0x3f));
170 target[0] = UChar(0xfc | ((int) (CH(3) & 0x40) >> 6));
171 break;
175 return rc; /* number of bytes needed in target */
176 #undef CH
179 NCURSES_EXPORT(int)
180 _nc_conv_to_utf32(unsigned *target, const char *source, unsigned limit)
182 #define CH(n) UChar((*target) >> ((n) * 8))
183 int rc = 0;
184 int j;
185 unsigned mask = 0;
188 * Find the number of bytes we will need from the source.
190 if ((*source & 0x80) == 0) {
191 rc = 1;
192 mask = (unsigned) *source;
193 } else if ((*source & 0xe0) == 0xc0) {
194 rc = 2;
195 mask = (unsigned) (*source & 0x1f);
196 } else if ((*source & 0xf0) == 0xe0) {
197 rc = 3;
198 mask = (unsigned) (*source & 0x0f);
199 } else if ((*source & 0xf8) == 0xf0) {
200 rc = 4;
201 mask = (unsigned) (*source & 0x07);
202 } else if ((*source & 0xfc) == 0xf8) {
203 rc = 5;
204 mask = (unsigned) (*source & 0x03);
205 } else if ((*source & 0xfe) == 0xfc) {
206 rc = 6;
207 mask = (unsigned) (*source & 0x01);
210 if ((unsigned) rc > limit) { /* whatever it is, we cannot decode it */
211 rc = 0;
215 * sanity-check.
217 if (rc > 1) {
218 for (j = 1; j < rc; j++) {
219 if ((source[j] & 0xc0) != 0x80)
220 break;
222 if (j != rc) {
223 rc = 0;
227 if (target != 0) {
228 int shift = 0;
229 *target = 0;
230 for (j = 1; j < rc; j++) {
231 *target |= (unsigned) (source[rc - j] & 0x3f) << shift;
232 shift += 6;
234 *target |= mask << shift;
236 return rc;
237 #undef CH
239 #endif /* EXP_XTERM_1005 */