1 /* $NetBSD: chartype.c,v 1.23 2016/02/28 23:02:24 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.23 2016/02/28 23:02:24 christos Exp $");
35 #endif /* not lint && not SCCSID */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
45 #define CT_BUFSIZ ((size_t)1024)
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 Char *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 = ct_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 ct_mbstowcs(conv->wbuff, s, conv->wsize);
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;
185 /* UTF-8 encoding specific values */
190 else if (c < 0x10000)
192 else if (c < 0x110000)
195 return 0; /* not a valid codepoint */
199 ct_encode_char(char *dst, size_t len, Char c)
202 if (len < ct_enc_width(c))
204 l = ct_wctomb(dst, c);
214 ct_mbrtowc(wchar_t *wc, const char *s, size_t n)
217 /* This only works because UTF-8 is stateless */
218 memset(&mbs, 0, sizeof(mbs));
219 return mbrtowc(wc, s, n, &mbs);
225 ct_mbrtowc(wchar_t *wc, const char *s, size_t n)
237 protected const Char *
238 ct_visual_string(const Char *s)
240 static Char *buff = NULL;
241 static size_t buffsize = 0;
249 buffsize = CT_BUFSIZ;
250 buff = el_malloc(buffsize * sizeof(*buff));
254 used = ct_visual_char(dst, buffsize - (size_t)(dst - buff), *s);
255 if (used == -1) { /* failed to encode, need more buffer space */
257 buffsize += CT_BUFSIZ;
258 p = el_realloc(buff, buffsize * sizeof(*buff));
263 /* don't increment s here - we want to retry it! */
269 if (dst >= (buff + buffsize)) { /* sigh */
271 p = el_realloc(buff, buffsize * sizeof(*buff));
275 dst = buff + buffsize - 1;
288 ct_visual_width(Char c)
290 int t = ct_chr_class(c);
292 case CHTYPE_ASCIICTL:
293 return 2; /* ^@ ^? etc. */
295 return 1; /* Hmm, this really need to be handled outside! */
297 return 0; /* Should this be 1 instead? */
301 case CHTYPE_NONPRINT:
302 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
303 return 8; /* \U+12345 */
305 return 7; /* \U+1234 */
309 case CHTYPE_NONPRINT:
313 return 0; /* should not happen */
319 ct_visual_char(Char *dst, size_t len, Char c)
321 int t = ct_chr_class(c);
325 case CHTYPE_ASCIICTL:
327 return -1; /* insufficient space */
330 *dst = '?'; /* DEL -> ^? */
332 *dst = c | 0100; /* uncontrolify it */
336 return -1; /* insufficient space */
339 case CHTYPE_NONPRINT:
340 /* we only use single-width glyphs for display,
341 * so this is right */
342 if ((ssize_t)len < ct_visual_width(c))
343 return -1; /* insufficient space */
348 #define tohexdigit(v) "0123456789ABCDEF"[v]
349 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
350 *dst++ = tohexdigit(((unsigned int) c >> 16) & 0xf);
351 *dst++ = tohexdigit(((unsigned int) c >> 12) & 0xf);
352 *dst++ = tohexdigit(((unsigned int) c >> 8) & 0xf);
353 *dst++ = tohexdigit(((unsigned int) c >> 4) & 0xf);
354 *dst = tohexdigit(((unsigned int) c ) & 0xf);
355 return c > 0xffff ? 8 : 7;
358 #define tooctaldigit(v) (Char)((v) + '0')
359 *dst++ = tooctaldigit(((unsigned int) c >> 6) & 0x7);
360 *dst++ = tooctaldigit(((unsigned int) c >> 3) & 0x7);
361 *dst++ = tooctaldigit(((unsigned int) c ) & 0x7);
364 /* these two should be handled outside this function */
365 default: /* we should never hit the default */
380 else if (IsASCII(c) && Iscntrl(c))
381 return CHTYPE_ASCIICTL;
385 return CHTYPE_NONPRINT;