1 /* $NetBSD: chartype.c,v 1.30 2016/05/09 21:46:56 christos Exp $ */
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * chartype.c: character classification and meta information
33 #if !defined(lint) && !defined(SCCSID)
34 __RCSID("$NetBSD: chartype.c,v 1.30 2016/05/09 21:46:56 christos Exp $");
35 #endif /* not lint && not SCCSID */
43 #define CT_BUFSIZ ((size_t)1024)
45 static int ct_conv_cbuff_resize(ct_buffer_t *, size_t);
46 static int ct_conv_wbuff_resize(ct_buffer_t *, size_t);
49 ct_conv_cbuff_resize(ct_buffer_t *conv, size_t csize)
53 if (csize <= conv->csize)
58 p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff));
70 ct_conv_wbuff_resize(ct_buffer_t *conv, size_t wsize)
74 if (wsize <= conv->wsize)
79 p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff));
92 ct_encode_string(const wchar_t *s, ct_buffer_t *conv)
102 used = (ssize_t)(dst - conv->cbuff);
103 if ((conv->csize - (size_t)used) < 5) {
104 if (ct_conv_cbuff_resize(conv,
105 conv->csize + CT_BUFSIZ) == -1)
107 dst = conv->cbuff + used;
111 used = ct_encode_char(dst, (size_t)5, *s);
112 if (used == -1) /* failed to encode, need more buffer space */
122 ct_decode_string(const char *s, ct_buffer_t *conv)
129 len = mbstowcs(NULL, s, (size_t)0);
130 if (len == (size_t)-1)
133 if (conv->wsize < ++len)
134 if (ct_conv_wbuff_resize(conv, len + CT_BUFSIZ) == -1)
137 mbstowcs(conv->wbuff, s, conv->wsize);
142 libedit_private wchar_t **
143 ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv)
151 /* Make sure we have enough space in the conversion buffer to store all
152 * the argv strings. */
153 for (i = 0, bufspace = 0; i < argc; ++i)
154 bufspace += argv[i] ? strlen(argv[i]) + 1 : 0;
155 if (conv->wsize < ++bufspace)
156 if (ct_conv_wbuff_resize(conv, bufspace + CT_BUFSIZ) == -1)
159 wargv = el_malloc((size_t)argc * sizeof(*wargv));
161 for (i = 0, p = conv->wbuff; i < argc; ++i) {
162 if (!argv[i]) { /* don't pass null pointers to mbstowcs */
167 bytes = (ssize_t)mbstowcs(p, argv[i], bufspace);
173 bytes++; /* include '\0' in the count */
174 bufspace -= (size_t)bytes;
182 libedit_private size_t
183 ct_enc_width(wchar_t c)
185 /* UTF-8 encoding specific values */
190 else if (c < 0x10000)
192 else if (c < 0x110000)
195 return 0; /* not a valid codepoint */
198 libedit_private ssize_t
199 ct_encode_char(char *dst, size_t len, wchar_t c)
202 if (len < ct_enc_width(c))
213 libedit_private const wchar_t *
214 ct_visual_string(const wchar_t *s, ct_buffer_t *conv)
222 if (ct_conv_wbuff_resize(conv, CT_BUFSIZ) == -1)
228 used = ct_visual_char(dst,
229 conv->wsize - (size_t)(dst - conv->wbuff), *s);
236 /* failed to encode, need more buffer space */
237 used = dst - conv->wbuff;
238 if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
240 dst = conv->wbuff + used;
243 if (dst >= (conv->wbuff + conv->wsize)) { /* sigh */
244 used = dst - conv->wbuff;
245 if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
247 dst = conv->wbuff + used;
257 ct_visual_width(wchar_t c)
259 int t = ct_chr_class(c);
261 case CHTYPE_ASCIICTL:
262 return 2; /* ^@ ^? etc. */
264 return 1; /* Hmm, this really need to be handled outside! */
266 return 0; /* Should this be 1 instead? */
269 case CHTYPE_NONPRINT:
270 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
271 return 8; /* \U+12345 */
273 return 7; /* \U+1234 */
275 return 0; /* should not happen */
280 libedit_private ssize_t
281 ct_visual_char(wchar_t *dst, size_t len, wchar_t c)
283 int t = ct_chr_class(c);
287 case CHTYPE_ASCIICTL:
289 return -1; /* insufficient space */
292 *dst = '?'; /* DEL -> ^? */
294 *dst = c | 0100; /* uncontrolify it */
298 return -1; /* insufficient space */
301 case CHTYPE_NONPRINT:
302 /* we only use single-width glyphs for display,
303 * so this is right */
304 if ((ssize_t)len < ct_visual_width(c))
305 return -1; /* insufficient space */
309 #define tohexdigit(v) "0123456789ABCDEF"[v]
310 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
311 *dst++ = tohexdigit(((unsigned int) c >> 16) & 0xf);
312 *dst++ = tohexdigit(((unsigned int) c >> 12) & 0xf);
313 *dst++ = tohexdigit(((unsigned int) c >> 8) & 0xf);
314 *dst++ = tohexdigit(((unsigned int) c >> 4) & 0xf);
315 *dst = tohexdigit(((unsigned int) c ) & 0xf);
316 return c > 0xffff ? 8 : 7;
318 /* these two should be handled outside this function */
319 default: /* we should never hit the default */
328 ct_chr_class(wchar_t c)
334 else if (c < 0x100 && iswcntrl(c))
335 return CHTYPE_ASCIICTL;
336 else if (iswprint(c))
339 return CHTYPE_NONPRINT;