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$");
46 #define CT_BUFSIZ ((size_t)1024)
50 ct_conv_cbuff_resize(ct_buffer_t *conv, size_t csize)
54 if (csize <= conv->csize)
59 p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff));
71 ct_conv_wbuff_resize(ct_buffer_t *conv, size_t wsize)
75 if (wsize <= conv->wsize)
80 p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff));
93 ct_encode_string(const Char *s, ct_buffer_t *conv)
103 used = (ssize_t)(dst - conv->cbuff);
104 if ((conv->csize - (size_t)used) < 5) {
105 if (ct_conv_cbuff_resize(conv,
106 conv->csize + CT_BUFSIZ) == -1)
108 dst = conv->cbuff + used;
112 used = ct_encode_char(dst, (size_t)5, *s);
113 if (used == -1) /* failed to encode, need more buffer space */
123 ct_decode_string(const char *s, ct_buffer_t *conv)
130 len = ct_mbstowcs(NULL, s, (size_t)0);
131 if (len == (size_t)-1)
134 if (conv->wsize < ++len)
135 if (ct_conv_wbuff_resize(conv, len + CT_BUFSIZ) == -1)
138 ct_mbstowcs(conv->wbuff, s, conv->wsize);
144 ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv)
152 /* Make sure we have enough space in the conversion buffer to store all
153 * the argv strings. */
154 for (i = 0, bufspace = 0; i < argc; ++i)
155 bufspace += argv[i] ? strlen(argv[i]) + 1 : 0;
156 if (conv->wsize < ++bufspace)
157 if (ct_conv_wbuff_resize(conv, bufspace + CT_BUFSIZ) == -1)
160 wargv = el_malloc((size_t)(argc + 1) * sizeof(*wargv));
162 for (i = 0, p = conv->wbuff; i < argc; ++i) {
163 if (!argv[i]) { /* don't pass null pointers to mbstowcs */
168 bytes = (ssize_t)mbstowcs(p, argv[i], bufspace);
174 bytes++; /* include '\0' in the count */
175 bufspace -= (size_t)bytes;
187 mbstate_t ps = (mbstate_t){{0}};
189 char cbuf[MB_LEN_MAX];
190 len = ct_wcrtomb(cbuf, c, &ps);
191 if (len == (size_t)-1)
197 ct_encode_char(char *dst, size_t len, Char c)
200 if (len < ct_enc_width(c))
202 l = ct_wctomb(dst, c);
212 ct_mbrtowc(wchar_t *wc, const char *s, size_t n)
215 /* This only works because UTF-8 is stateless */
216 memset(&mbs, 0, sizeof(mbs));
217 return mbrtowc(wc, s, n, &mbs);
223 ct_mbrtowc(wchar_t *wc, const char *s, size_t n)
235 protected const Char *
236 ct_visual_string(const Char *s)
238 static Char *buff = NULL;
239 static size_t buffsize = 0;
247 buffsize = CT_BUFSIZ;
248 buff = el_malloc(buffsize * sizeof(*buff));
252 used = ct_visual_char(dst, buffsize - (size_t)(dst - buff), *s);
253 if (used == -1) { /* failed to encode, need more buffer space */
255 buffsize += CT_BUFSIZ;
256 p = el_realloc(buff, buffsize * sizeof(*buff));
261 /* don't increment s here - we want to retry it! */
267 if (dst >= (buff + buffsize)) { /* sigh */
269 p = el_realloc(buff, buffsize * sizeof(*buff));
273 dst = buff + buffsize - 1;
286 ct_visual_width(Char c)
288 int t = ct_chr_class(c);
290 case CHTYPE_ASCIICTL:
291 return 2; /* ^@ ^? etc. */
293 return 1; /* Hmm, this really need to be handled outside! */
295 return 0; /* Should this be 1 instead? */
299 case CHTYPE_NONPRINT:
300 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
301 return 8; /* \U+12345 */
303 return 7; /* \U+1234 */
307 case CHTYPE_NONPRINT:
311 return 0; /* should not happen */
317 ct_visual_char(Char *dst, size_t len, Char c)
319 int t = ct_chr_class(c);
323 case CHTYPE_ASCIICTL:
325 return -1; /* insufficient space */
328 *dst = '?'; /* DEL -> ^? */
330 *dst = c | 0100; /* uncontrolify it */
334 return -1; /* insufficient space */
337 case CHTYPE_NONPRINT:
338 /* we only use single-width glyphs for display,
339 * so this is right */
340 if ((ssize_t)len < ct_visual_width(c))
341 return -1; /* insufficient space */
346 #define tohexdigit(v) "0123456789ABCDEF"[v]
347 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
348 *dst++ = tohexdigit(((unsigned int) c >> 16) & 0xf);
349 *dst++ = tohexdigit(((unsigned int) c >> 12) & 0xf);
350 *dst++ = tohexdigit(((unsigned int) c >> 8) & 0xf);
351 *dst++ = tohexdigit(((unsigned int) c >> 4) & 0xf);
352 *dst = tohexdigit(((unsigned int) c ) & 0xf);
353 return c > 0xffff ? 8 : 7;
356 #define tooctaldigit(v) (Char)((v) + '0')
357 *dst++ = tooctaldigit(((unsigned int) c >> 6) & 0x7);
358 *dst++ = tooctaldigit(((unsigned int) c >> 3) & 0x7);
359 *dst++ = tooctaldigit(((unsigned int) c ) & 0x7);
362 /* these two should be handled outside this function */
363 default: /* we should never hit the default */
378 else if (IsASCII(c) && Iscntrl(c))
379 return CHTYPE_ASCIICTL;
383 return CHTYPE_NONPRINT;