1 /* $NetBSD: chartype.c,v 1.10 2011/08/16 16:25:15 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.10 2011/08/16 16:25:15 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_buff_resize(ct_buffer_t *conv, size_t mincsize, size_t minwsize)
49 if (mincsize > conv->csize) {
50 conv->csize = mincsize;
51 p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff));
60 if (minwsize > conv->wsize) {
61 conv->wsize = minwsize;
62 p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff));
74 ct_encode_string(const Char *s, ct_buffer_t *conv)
82 ct_conv_buff_resize(conv, CT_BUFSIZ, (size_t)0);
88 used = (ssize_t)(conv->csize - (size_t)(dst - conv->cbuff));
90 used = dst - conv->cbuff;
91 ct_conv_buff_resize(conv, conv->csize + CT_BUFSIZ,
95 dst = conv->cbuff + used;
97 used = ct_encode_char(dst, (size_t)5, *s);
98 if (used == -1) /* failed to encode, need more buffer space */
108 ct_decode_string(const char *s, ct_buffer_t *conv)
115 ct_conv_buff_resize(conv, (size_t)0, CT_BUFSIZ);
119 len = ct_mbstowcs(NULL, s, (size_t)0);
120 if (len == (size_t)-1)
122 if (len > conv->wsize)
123 ct_conv_buff_resize(conv, (size_t)0, len + 1);
126 ct_mbstowcs(conv->wbuff, s, conv->wsize);
132 ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv)
140 /* Make sure we have enough space in the conversion buffer to store all
141 * the argv strings. */
142 for (i = 0, bufspace = 0; i < argc; ++i)
143 bufspace += argv[i] ? strlen(argv[i]) + 1 : 0;
144 ct_conv_buff_resize(conv, (size_t)0, bufspace);
148 wargv = el_malloc((size_t)argc * sizeof(*wargv));
150 for (i = 0, p = conv->wbuff; i < argc; ++i) {
151 if (!argv[i]) { /* don't pass null pointers to mbstowcs */
156 bytes = (ssize_t)mbstowcs(p, argv[i], bufspace);
162 bytes++; /* include '\0' in the count */
163 bufspace -= (size_t)bytes;
174 /* UTF-8 encoding specific values */
179 else if (c < 0x10000)
181 else if (c < 0x110000)
184 return 0; /* not a valid codepoint */
188 ct_encode_char(char *dst, size_t len, Char c)
191 if (len < ct_enc_width(c))
193 l = ct_wctomb(dst, c);
203 protected const Char *
204 ct_visual_string(const Char *s)
206 static Char *buff = NULL;
207 static size_t buffsize = 0;
215 buffsize = CT_BUFSIZ;
216 buff = el_malloc(buffsize * sizeof(*buff));
220 used = ct_visual_char(dst, buffsize - (size_t)(dst - buff), *s);
221 if (used == -1) { /* failed to encode, need more buffer space */
223 buffsize += CT_BUFSIZ;
224 p = el_realloc(buff, buffsize * sizeof(*buff));
229 /* don't increment s here - we want to retry it! */
235 if (dst >= (buff + buffsize)) { /* sigh */
237 p = el_realloc(buff, buffsize * sizeof(*buff));
241 dst = buff + buffsize - 1;
254 ct_visual_width(Char c)
256 int t = ct_chr_class(c);
258 case CHTYPE_ASCIICTL:
259 return 2; /* ^@ ^? etc. */
261 return 1; /* Hmm, this really need to be handled outside! */
263 return 0; /* Should this be 1 instead? */
267 case CHTYPE_NONPRINT:
268 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
269 return 8; /* \U+12345 */
271 return 7; /* \U+1234 */
275 case CHTYPE_NONPRINT:
279 return 0; /* should not happen */
285 ct_visual_char(Char *dst, size_t len, Char c)
287 int t = ct_chr_class(c);
291 case CHTYPE_ASCIICTL:
293 return -1; /* insufficient space */
296 *dst = '?'; /* DEL -> ^? */
298 *dst = c | 0100; /* uncontrolify it */
302 return -1; /* insufficient space */
305 case CHTYPE_NONPRINT:
306 /* we only use single-width glyphs for display,
307 * so this is right */
308 if ((ssize_t)len < ct_visual_width(c))
309 return -1; /* insufficient space */
314 #define tohexdigit(v) "0123456789ABCDEF"[v]
315 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
316 *dst++ = tohexdigit(((unsigned int) c >> 16) & 0xf);
317 *dst++ = tohexdigit(((unsigned int) c >> 12) & 0xf);
318 *dst++ = tohexdigit(((unsigned int) c >> 8) & 0xf);
319 *dst++ = tohexdigit(((unsigned int) c >> 4) & 0xf);
320 *dst = tohexdigit(((unsigned int) c ) & 0xf);
321 return c > 0xffff ? 8 : 7;
324 #define tooctaldigit(v) ((v) + '0')
325 *dst++ = tooctaldigit(((unsigned int) c >> 6) & 0x7);
326 *dst++ = tooctaldigit(((unsigned int) c >> 3) & 0x7);
327 *dst++ = tooctaldigit(((unsigned int) c ) & 0x7);
330 /* these two should be handled outside this function */
331 default: /* we should never hit the default */
346 else if (IsASCII(c) && Iscntrl(c))
347 return CHTYPE_ASCIICTL;
351 return CHTYPE_NONPRINT;