1 /* $NetBSD: chartype.c,v 1.12 2015/02/22 02:16:19 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.12 2015/02/22 02:16:19 christos Exp $");
35 #endif /* not lint && not SCCSID */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
42 #define CT_BUFSIZ ((size_t)1024)
46 ct_conv_cbuff_resize(ct_buffer_t *conv, size_t csize)
50 if (csize <= conv->csize)
55 p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff));
67 ct_conv_wbuff_resize(ct_buffer_t *conv, size_t wsize)
71 if (wsize <= conv->wsize)
76 p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff));
89 ct_encode_string(const Char *s, ct_buffer_t *conv)
99 used = (ssize_t)(dst - conv->cbuff);
100 if ((conv->csize - (size_t)used) < 5) {
101 if (ct_conv_cbuff_resize(conv,
102 conv->csize + CT_BUFSIZ) == -1)
104 dst = conv->cbuff + used;
108 used = ct_encode_char(dst, (size_t)5, *s);
109 if (used == -1) /* failed to encode, need more buffer space */
119 ct_decode_string(const char *s, ct_buffer_t *conv)
126 len = ct_mbstowcs(NULL, s, (size_t)0);
127 if (len == (size_t)-1)
130 if (conv->wsize < ++len)
131 if (ct_conv_wbuff_resize(conv, len + CT_BUFSIZ) == -1)
134 ct_mbstowcs(conv->wbuff, s, conv->wsize);
140 ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv)
148 /* Make sure we have enough space in the conversion buffer to store all
149 * the argv strings. */
150 for (i = 0, bufspace = 0; i < argc; ++i)
151 bufspace += argv[i] ? strlen(argv[i]) + 1 : 0;
152 if (conv->wsize < ++bufspace)
153 if (ct_conv_wbuff_resize(conv, bufspace + CT_BUFSIZ) == -1)
156 wargv = el_malloc((size_t)argc * sizeof(*wargv));
158 for (i = 0, p = conv->wbuff; i < argc; ++i) {
159 if (!argv[i]) { /* don't pass null pointers to mbstowcs */
164 bytes = (ssize_t)mbstowcs(p, argv[i], bufspace);
170 bytes++; /* include '\0' in the count */
171 bufspace -= (size_t)bytes;
182 /* UTF-8 encoding specific values */
187 else if (c < 0x10000)
189 else if (c < 0x110000)
192 return 0; /* not a valid codepoint */
196 ct_encode_char(char *dst, size_t len, Char c)
199 if (len < ct_enc_width(c))
201 l = ct_wctomb(dst, c);
211 protected const Char *
212 ct_visual_string(const Char *s)
214 static Char *buff = NULL;
215 static size_t buffsize = 0;
223 buffsize = CT_BUFSIZ;
224 buff = el_malloc(buffsize * sizeof(*buff));
228 used = ct_visual_char(dst, buffsize - (size_t)(dst - buff), *s);
229 if (used == -1) { /* failed to encode, need more buffer space */
231 buffsize += CT_BUFSIZ;
232 p = el_realloc(buff, buffsize * sizeof(*buff));
237 /* don't increment s here - we want to retry it! */
243 if (dst >= (buff + buffsize)) { /* sigh */
245 p = el_realloc(buff, buffsize * sizeof(*buff));
249 dst = buff + buffsize - 1;
262 ct_visual_width(Char c)
264 int t = ct_chr_class(c);
266 case CHTYPE_ASCIICTL:
267 return 2; /* ^@ ^? etc. */
269 return 1; /* Hmm, this really need to be handled outside! */
271 return 0; /* Should this be 1 instead? */
275 case CHTYPE_NONPRINT:
276 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
277 return 8; /* \U+12345 */
279 return 7; /* \U+1234 */
283 case CHTYPE_NONPRINT:
287 return 0; /* should not happen */
293 ct_visual_char(Char *dst, size_t len, Char c)
295 int t = ct_chr_class(c);
299 case CHTYPE_ASCIICTL:
301 return -1; /* insufficient space */
304 *dst = '?'; /* DEL -> ^? */
306 *dst = c | 0100; /* uncontrolify it */
310 return -1; /* insufficient space */
313 case CHTYPE_NONPRINT:
314 /* we only use single-width glyphs for display,
315 * so this is right */
316 if ((ssize_t)len < ct_visual_width(c))
317 return -1; /* insufficient space */
322 #define tohexdigit(v) "0123456789ABCDEF"[v]
323 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
324 *dst++ = tohexdigit(((unsigned int) c >> 16) & 0xf);
325 *dst++ = tohexdigit(((unsigned int) c >> 12) & 0xf);
326 *dst++ = tohexdigit(((unsigned int) c >> 8) & 0xf);
327 *dst++ = tohexdigit(((unsigned int) c >> 4) & 0xf);
328 *dst = tohexdigit(((unsigned int) c ) & 0xf);
329 return c > 0xffff ? 8 : 7;
332 #define tooctaldigit(v) ((v) + '0')
333 *dst++ = tooctaldigit(((unsigned int) c >> 6) & 0x7);
334 *dst++ = tooctaldigit(((unsigned int) c >> 3) & 0x7);
335 *dst++ = tooctaldigit(((unsigned int) c ) & 0x7);
338 /* these two should be handled outside this function */
339 default: /* we should never hit the default */
354 else if (IsASCII(c) && Iscntrl(c))
355 return CHTYPE_ASCIICTL;
359 return CHTYPE_NONPRINT;