1 /* $NetBSD: chartype.c,v 1.36 2022/10/30 19:11:31 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.36 2022/10/30 19:11:31 christos Exp $");
35 #endif /* not lint && not SCCSID */
44 #define CT_BUFSIZ ((size_t)1024)
46 static int ct_conv_cbuff_resize(ct_buffer_t *, size_t);
47 static int ct_conv_wbuff_resize(ct_buffer_t *, size_t);
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 wchar_t *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 = 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 mbstowcs(conv->wbuff, s, conv->wsize);
143 libedit_private wchar_t **
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_calloc((size_t)(argc + 1), sizeof(*wargv));
164 for (i = 0, p = conv->wbuff; i < argc; ++i) {
165 if (!argv[i]) { /* don't pass null pointers to mbstowcs */
170 bytes = (ssize_t)mbstowcs(p, argv[i], bufspace);
176 bytes++; /* include '\0' in the count */
177 bufspace -= (size_t)bytes;
186 libedit_private size_t
187 ct_enc_width(wchar_t c)
190 char buf[MB_LEN_MAX];
192 memset(&mbs, 0, sizeof(mbs));
194 if ((size = wcrtomb(buf, c, &mbs)) == (size_t)-1)
199 libedit_private ssize_t
200 ct_encode_char(char *dst, size_t len, wchar_t c)
203 if (len < ct_enc_width(c))
214 libedit_private const wchar_t *
215 ct_visual_string(const wchar_t *s, ct_buffer_t *conv)
223 if (ct_conv_wbuff_resize(conv, CT_BUFSIZ) == -1)
229 used = ct_visual_char(dst,
230 conv->wsize - (size_t)(dst - conv->wbuff), *s);
237 /* failed to encode, need more buffer space */
238 used = dst - conv->wbuff;
239 if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
241 dst = conv->wbuff + used;
244 if (dst >= (conv->wbuff + conv->wsize)) { /* sigh */
245 used = dst - conv->wbuff;
246 if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
248 dst = conv->wbuff + used;
258 ct_visual_width(wchar_t 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? */
270 case CHTYPE_NONPRINT:
271 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
272 return 8; /* \U+12345 */
274 return 7; /* \U+1234 */
276 return 0; /* should not happen */
281 libedit_private ssize_t
282 ct_visual_char(wchar_t *dst, size_t len, wchar_t c)
284 int t = ct_chr_class(c);
288 case CHTYPE_ASCIICTL:
290 return -1; /* insufficient space */
293 *dst = '?'; /* DEL -> ^? */
295 *dst = c | 0100; /* uncontrolify it */
299 return -1; /* insufficient space */
302 case CHTYPE_NONPRINT:
303 /* we only use single-width glyphs for display,
304 * so this is right */
305 if ((ssize_t)len < ct_visual_width(c))
306 return -1; /* insufficient space */
310 #define tohexdigit(v) "0123456789ABCDEF"[v]
311 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
312 *dst++ = tohexdigit(((unsigned int) c >> 16) & 0xf);
313 *dst++ = tohexdigit(((unsigned int) c >> 12) & 0xf);
314 *dst++ = tohexdigit(((unsigned int) c >> 8) & 0xf);
315 *dst++ = tohexdigit(((unsigned int) c >> 4) & 0xf);
316 *dst = tohexdigit(((unsigned int) c ) & 0xf);
317 return c > 0xffff ? 8 : 7;
319 /* these two should be handled outside this function */
320 default: /* we should never hit the default */
329 ct_chr_class(wchar_t c)
335 else if (c < 0x100 && iswcntrl(c))
336 return CHTYPE_ASCIICTL;
337 else if (iswprint(c))
340 return CHTYPE_NONPRINT;