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.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the NetBSD
18 * Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * chartype.c: character classification and meta information
40 #if !defined(lint) && !defined(SCCSID)
41 __RCSID("$NetBSD: chartype.c,v 1.10 2011/08/16 16:25:15 christos Exp $");
42 #endif /* not lint && not SCCSID */
46 #define CT_BUFSIZ ((size_t)1024)
50 ct_conv_buff_resize(ct_buffer_t
*conv
, size_t mincsize
, size_t minwsize
)
53 if (mincsize
> conv
->csize
) {
54 conv
->csize
= mincsize
;
55 p
= el_realloc(conv
->cbuff
, conv
->csize
* sizeof(*conv
->cbuff
));
64 if (minwsize
> conv
->wsize
) {
65 conv
->wsize
= minwsize
;
66 p
= el_realloc(conv
->wbuff
, conv
->wsize
* sizeof(*conv
->wbuff
));
78 ct_encode_string(const Char
*s
, ct_buffer_t
*conv
)
86 ct_conv_buff_resize(conv
, CT_BUFSIZ
, (size_t)0);
92 used
= (ssize_t
)(conv
->csize
- (size_t)(dst
- conv
->cbuff
));
94 used
= dst
- conv
->cbuff
;
95 ct_conv_buff_resize(conv
, conv
->csize
+ CT_BUFSIZ
,
99 dst
= conv
->cbuff
+ used
;
101 used
= ct_encode_char(dst
, (size_t)5, *s
);
102 if (used
== -1) /* failed to encode, need more buffer space */
112 ct_decode_string(const char *s
, ct_buffer_t
*conv
)
119 ct_conv_buff_resize(conv
, (size_t)0, CT_BUFSIZ
);
123 len
= ct_mbstowcs(NULL
, s
, (size_t)0);
124 if (len
== (size_t)-1)
126 if (len
> conv
->wsize
)
127 ct_conv_buff_resize(conv
, (size_t)0, len
+ 1);
130 ct_mbstowcs(conv
->wbuff
, s
, conv
->wsize
);
136 ct_decode_argv(int argc
, const char *argv
[], ct_buffer_t
*conv
)
144 /* Make sure we have enough space in the conversion buffer to store all
145 * the argv strings. */
146 for (i
= 0, bufspace
= 0; i
< argc
; ++i
)
147 bufspace
+= argv
[i
] ? strlen(argv
[i
]) + 1 : 0;
148 ct_conv_buff_resize(conv
, (size_t)0, bufspace
);
152 wargv
= el_malloc((size_t)argc
* sizeof(*wargv
));
154 for (i
= 0, p
= conv
->wbuff
; i
< argc
; ++i
) {
155 if (!argv
[i
]) { /* don't pass null pointers to mbstowcs */
160 bytes
= (ssize_t
)mbstowcs(p
, argv
[i
], bufspace
);
166 bytes
++; /* include '\0' in the count */
167 bufspace
-= (size_t)bytes
;
178 /* UTF-8 encoding specific values */
183 else if (c
< 0x10000)
185 else if (c
< 0x110000)
188 return 0; /* not a valid codepoint */
192 ct_encode_char(char *dst
, size_t len
, Char c
)
195 if (len
< ct_enc_width(c
))
197 l
= ct_wctomb(dst
, c
);
207 protected const Char
*
208 ct_visual_string(const Char
*s
)
210 static Char
*buff
= NULL
;
211 static size_t buffsize
= 0;
219 buffsize
= CT_BUFSIZ
;
220 buff
= el_malloc(buffsize
* sizeof(*buff
));
224 used
= ct_visual_char(dst
, buffsize
- (size_t)(dst
- buff
), *s
);
225 if (used
== -1) { /* failed to encode, need more buffer space */
227 buffsize
+= CT_BUFSIZ
;
228 p
= el_realloc(buff
, buffsize
* sizeof(*buff
));
233 /* don't increment s here - we want to retry it! */
239 if (dst
>= (buff
+ buffsize
)) { /* sigh */
241 p
= el_realloc(buff
, buffsize
* sizeof(*buff
));
245 dst
= buff
+ buffsize
- 1;
258 ct_visual_width(Char c
)
260 int t
= ct_chr_class(c
);
262 case CHTYPE_ASCIICTL
:
263 return 2; /* ^@ ^? etc. */
265 return 1; /* Hmm, this really need to be handled outside! */
267 return 0; /* Should this be 1 instead? */
271 case CHTYPE_NONPRINT
:
272 if (c
> 0xffff) /* prefer standard 4-byte display over 5-byte */
273 return 8; /* \U+12345 */
275 return 7; /* \U+1234 */
279 case CHTYPE_NONPRINT
:
283 return 0; /* should not happen */
289 ct_visual_char(Char
*dst
, size_t len
, Char c
)
291 int t
= ct_chr_class(c
);
295 case CHTYPE_ASCIICTL
:
297 return -1; /* insufficient space */
300 *dst
= '?'; /* DEL -> ^? */
302 *dst
= c
| 0100; /* uncontrolify it */
306 return -1; /* insufficient space */
309 case CHTYPE_NONPRINT
:
310 /* we only use single-width glyphs for display,
311 * so this is right */
312 if ((ssize_t
)len
< ct_visual_width(c
))
313 return -1; /* insufficient space */
318 #define tohexdigit(v) "0123456789ABCDEF"[v]
319 if (c
> 0xffff) /* prefer standard 4-byte display over 5-byte */
320 *dst
++ = tohexdigit(((unsigned int) c
>> 16) & 0xf);
321 *dst
++ = tohexdigit(((unsigned int) c
>> 12) & 0xf);
322 *dst
++ = tohexdigit(((unsigned int) c
>> 8) & 0xf);
323 *dst
++ = tohexdigit(((unsigned int) c
>> 4) & 0xf);
324 *dst
= tohexdigit(((unsigned int) c
) & 0xf);
325 return c
> 0xffff ? 8 : 7;
328 #define tooctaldigit(v) ((v) + '0')
329 *dst
++ = tooctaldigit(((unsigned int) c
>> 6) & 0x7);
330 *dst
++ = tooctaldigit(((unsigned int) c
>> 3) & 0x7);
331 *dst
++ = tooctaldigit(((unsigned int) c
) & 0x7);
334 /* these two should be handled outside this function */
335 default: /* we should never hit the default */
350 else if (IsASCII(c
) && Iscntrl(c
))
351 return CHTYPE_ASCIICTL
;
355 return CHTYPE_NONPRINT
;