1*aec4d439Smrg /* $NetBSD: chartype.c,v 1.37 2023/08/10 20:38:00 mrg Exp $ */
234e53048Schristos
334e53048Schristos /*-
434e53048Schristos * Copyright (c) 2009 The NetBSD Foundation, Inc.
534e53048Schristos * All rights reserved.
634e53048Schristos *
734e53048Schristos * Redistribution and use in source and binary forms, with or without
834e53048Schristos * modification, are permitted provided that the following conditions
934e53048Schristos * are met:
1034e53048Schristos * 1. Redistributions of source code must retain the above copyright
1134e53048Schristos * notice, this list of conditions and the following disclaimer.
1234e53048Schristos * 2. Redistributions in binary form must reproduce the above copyright
1334e53048Schristos * notice, this list of conditions and the following disclaimer in the
1434e53048Schristos * documentation and/or other materials provided with the distribution.
1534e53048Schristos *
1634e53048Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1734e53048Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1834e53048Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1934e53048Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2034e53048Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2134e53048Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2234e53048Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2334e53048Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2434e53048Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2534e53048Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2634e53048Schristos * POSSIBILITY OF SUCH DAMAGE.
2734e53048Schristos */
2834e53048Schristos
2934e53048Schristos /*
3034e53048Schristos * chartype.c: character classification and meta information
3134e53048Schristos */
3234e53048Schristos #include "config.h"
3334e53048Schristos #if !defined(lint) && !defined(SCCSID)
34*aec4d439Smrg __RCSID("$NetBSD: chartype.c,v 1.37 2023/08/10 20:38:00 mrg Exp $");
3534e53048Schristos #endif /* not lint && not SCCSID */
36e84df91eSchristos
37e84df91eSchristos #include <ctype.h>
38e91c3053Schristos #include <limits.h>
39aefc1e44Schristos #include <stdlib.h>
40e84df91eSchristos #include <string.h>
41e84df91eSchristos
42e84df91eSchristos #include "el.h"
43f09cb8c6Schristos
44c11bd863Schristos #define CT_BUFSIZ ((size_t)1024)
4534e53048Schristos
46469d44f8Schristos static int ct_conv_cbuff_resize(ct_buffer_t *, size_t);
47469d44f8Schristos static int ct_conv_wbuff_resize(ct_buffer_t *, size_t);
48a75ea7b9Schristos
49469d44f8Schristos static int
ct_conv_cbuff_resize(ct_buffer_t * conv,size_t csize)5021ea10bdSchristos ct_conv_cbuff_resize(ct_buffer_t *conv, size_t csize)
5134e53048Schristos {
5234e53048Schristos void *p;
5321ea10bdSchristos
5421ea10bdSchristos if (csize <= conv->csize)
5521ea10bdSchristos return 0;
5621ea10bdSchristos
5721ea10bdSchristos conv->csize = csize;
5821ea10bdSchristos
59a13cd756Schristos p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff));
6034e53048Schristos if (p == NULL) {
6134e53048Schristos conv->csize = 0;
6234e53048Schristos el_free(conv->cbuff);
6334e53048Schristos conv->cbuff = NULL;
64aecf67a2Schristos return -1;
6521ea10bdSchristos }
6634e53048Schristos conv->cbuff = p;
6721ea10bdSchristos return 0;
6834e53048Schristos }
6934e53048Schristos
70469d44f8Schristos static int
ct_conv_wbuff_resize(ct_buffer_t * conv,size_t wsize)7121ea10bdSchristos ct_conv_wbuff_resize(ct_buffer_t *conv, size_t wsize)
7221ea10bdSchristos {
7321ea10bdSchristos void *p;
7421ea10bdSchristos
7521ea10bdSchristos if (wsize <= conv->wsize)
7621ea10bdSchristos return 0;
7721ea10bdSchristos
7821ea10bdSchristos conv->wsize = wsize;
7921ea10bdSchristos
80a13cd756Schristos p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff));
8134e53048Schristos if (p == NULL) {
8234e53048Schristos conv->wsize = 0;
8334e53048Schristos el_free(conv->wbuff);
8434e53048Schristos conv->wbuff = NULL;
85aecf67a2Schristos return -1;
8634e53048Schristos }
8721ea10bdSchristos conv->wbuff = p;
88aecf67a2Schristos return 0;
8934e53048Schristos }
9034e53048Schristos
9134e53048Schristos
92469d44f8Schristos char *
ct_encode_string(const wchar_t * s,ct_buffer_t * conv)930594af80Schristos ct_encode_string(const wchar_t *s, ct_buffer_t *conv)
9434e53048Schristos {
9534e53048Schristos char *dst;
96aecf67a2Schristos ssize_t used;
9734e53048Schristos
9834e53048Schristos if (!s)
9934e53048Schristos return NULL;
10034e53048Schristos
10134e53048Schristos dst = conv->cbuff;
102aecf67a2Schristos for (;;) {
103aecf67a2Schristos used = (ssize_t)(dst - conv->cbuff);
104aecf67a2Schristos if ((conv->csize - (size_t)used) < 5) {
10521ea10bdSchristos if (ct_conv_cbuff_resize(conv,
10621ea10bdSchristos conv->csize + CT_BUFSIZ) == -1)
10734e53048Schristos return NULL;
10834e53048Schristos dst = conv->cbuff + used;
10934e53048Schristos }
110aecf67a2Schristos if (!*s)
111aecf67a2Schristos break;
112c11bd863Schristos used = ct_encode_char(dst, (size_t)5, *s);
113c76d71fcSchristos if (used == -1) /* failed to encode, need more buffer space */
114c76d71fcSchristos abort();
11534e53048Schristos ++s;
11634e53048Schristos dst += used;
11734e53048Schristos }
11834e53048Schristos *dst = '\0';
11934e53048Schristos return conv->cbuff;
12034e53048Schristos }
12134e53048Schristos
122469d44f8Schristos wchar_t *
ct_decode_string(const char * s,ct_buffer_t * conv)12334e53048Schristos ct_decode_string(const char *s, ct_buffer_t *conv)
12434e53048Schristos {
125aecf67a2Schristos size_t len;
12634e53048Schristos
12734e53048Schristos if (!s)
12834e53048Schristos return NULL;
12934e53048Schristos
130fcf85103Schristos len = mbstowcs(NULL, s, (size_t)0);
1310c0f2339Schristos if (len == (size_t)-1)
1320c0f2339Schristos return NULL;
133aecf67a2Schristos
13421ea10bdSchristos if (conv->wsize < ++len)
13521ea10bdSchristos if (ct_conv_wbuff_resize(conv, len + CT_BUFSIZ) == -1)
13634e53048Schristos return NULL;
137aecf67a2Schristos
138fcf85103Schristos mbstowcs(conv->wbuff, s, conv->wsize);
13934e53048Schristos return conv->wbuff;
14034e53048Schristos }
14134e53048Schristos
14234e53048Schristos
143a2d6b270Schristos libedit_private wchar_t **
ct_decode_argv(int argc,const char * argv[],ct_buffer_t * conv)14434e53048Schristos ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv)
14534e53048Schristos {
14634e53048Schristos size_t bufspace;
14734e53048Schristos int i;
1480594af80Schristos wchar_t *p;
1490594af80Schristos wchar_t **wargv;
15034e53048Schristos ssize_t bytes;
15134e53048Schristos
15234e53048Schristos /* Make sure we have enough space in the conversion buffer to store all
15334e53048Schristos * the argv strings. */
15434e53048Schristos for (i = 0, bufspace = 0; i < argc; ++i)
15534e53048Schristos bufspace += argv[i] ? strlen(argv[i]) + 1 : 0;
15621ea10bdSchristos if (conv->wsize < ++bufspace)
15721ea10bdSchristos if (ct_conv_wbuff_resize(conv, bufspace + CT_BUFSIZ) == -1)
15834e53048Schristos return NULL;
15934e53048Schristos
160113f06a3Schristos wargv = el_calloc((size_t)(argc + 1), sizeof(*wargv));
16121a4b1ccSchristos if (wargv == NULL)
16221a4b1ccSchristos return NULL;
16334e53048Schristos
16434e53048Schristos for (i = 0, p = conv->wbuff; i < argc; ++i) {
16534e53048Schristos if (!argv[i]) { /* don't pass null pointers to mbstowcs */
16634e53048Schristos wargv[i] = NULL;
167c9043bbfSchristos continue;
16834e53048Schristos } else {
16934e53048Schristos wargv[i] = p;
1703d802cf5Schristos bytes = (ssize_t)mbstowcs(p, argv[i], bufspace);
17134e53048Schristos }
17234e53048Schristos if (bytes == -1) {
17334e53048Schristos el_free(wargv);
17434e53048Schristos return NULL;
17534e53048Schristos } else
17634e53048Schristos bytes++; /* include '\0' in the count */
1773d802cf5Schristos bufspace -= (size_t)bytes;
17834e53048Schristos p += bytes;
17934e53048Schristos }
1804937de3bSchristos wargv[i] = NULL;
18134e53048Schristos
18234e53048Schristos return wargv;
18334e53048Schristos }
18434e53048Schristos
18534e53048Schristos
186a2d6b270Schristos libedit_private size_t
ct_enc_width(wchar_t c)1870594af80Schristos ct_enc_width(wchar_t c)
18834e53048Schristos {
1892cb4fa42Schristos mbstate_t mbs;
19018bb6ea4Schristos char buf[MB_LEN_MAX];
1912cb4fa42Schristos size_t size;
1922cb4fa42Schristos memset(&mbs, 0, sizeof(mbs));
1932cb4fa42Schristos
1942cb4fa42Schristos if ((size = wcrtomb(buf, c, &mbs)) == (size_t)-1)
19518bb6ea4Schristos return 0;
1962cb4fa42Schristos return size;
19734e53048Schristos }
19834e53048Schristos
199a2d6b270Schristos libedit_private ssize_t
ct_encode_char(char * dst,size_t len,wchar_t c)2000594af80Schristos ct_encode_char(char *dst, size_t len, wchar_t c)
20134e53048Schristos {
20234e53048Schristos ssize_t l = 0;
203a5b37ffcSchristos if (len < ct_enc_width(c))
20434e53048Schristos return -1;
205fcf85103Schristos l = wctomb(dst, c);
20634e53048Schristos
20734e53048Schristos if (l < 0) {
208fcf85103Schristos wctomb(NULL, L'\0');
20934e53048Schristos l = 0;
21034e53048Schristos }
21134e53048Schristos return l;
21234e53048Schristos }
21361ee3048Schristos
214a2d6b270Schristos libedit_private const wchar_t *
ct_visual_string(const wchar_t * s,ct_buffer_t * conv)215300e2ca4Schristos ct_visual_string(const wchar_t *s, ct_buffer_t *conv)
21634e53048Schristos {
2170594af80Schristos wchar_t *dst;
218300e2ca4Schristos ssize_t used;
21934e53048Schristos
22034e53048Schristos if (!s)
22134e53048Schristos return NULL;
222300e2ca4Schristos
223300e2ca4Schristos if (ct_conv_wbuff_resize(conv, CT_BUFSIZ) == -1)
224300e2ca4Schristos return NULL;
225300e2ca4Schristos
226300e2ca4Schristos used = 0;
227300e2ca4Schristos dst = conv->wbuff;
22834e53048Schristos while (*s) {
229300e2ca4Schristos used = ct_visual_char(dst,
230300e2ca4Schristos conv->wsize - (size_t)(dst - conv->wbuff), *s);
231300e2ca4Schristos if (used != -1) {
23234e53048Schristos ++s;
23334e53048Schristos dst += used;
234300e2ca4Schristos continue;
23534e53048Schristos }
236300e2ca4Schristos
237300e2ca4Schristos /* failed to encode, need more buffer space */
238*aec4d439Smrg uintptr_t sused = (uintptr_t)dst - (uintptr_t)conv->wbuff;
239300e2ca4Schristos if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
24034e53048Schristos return NULL;
241*aec4d439Smrg dst = conv->wbuff + sused;
242300e2ca4Schristos }
243300e2ca4Schristos
244300e2ca4Schristos if (dst >= (conv->wbuff + conv->wsize)) { /* sigh */
245*aec4d439Smrg uintptr_t sused = (uintptr_t)dst - (uintptr_t)conv->wbuff;
246300e2ca4Schristos if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
247300e2ca4Schristos return NULL;
248*aec4d439Smrg dst = conv->wbuff + sused;
249300e2ca4Schristos }
250300e2ca4Schristos
251300e2ca4Schristos *dst = L'\0';
252300e2ca4Schristos return conv->wbuff;
25334e53048Schristos }
25434e53048Schristos
25534e53048Schristos
25634e53048Schristos
257a2d6b270Schristos libedit_private int
ct_visual_width(wchar_t c)2580594af80Schristos ct_visual_width(wchar_t c)
25934e53048Schristos {
26034e53048Schristos int t = ct_chr_class(c);
26134e53048Schristos switch (t) {
26234e53048Schristos case CHTYPE_ASCIICTL:
26334e53048Schristos return 2; /* ^@ ^? etc. */
26434e53048Schristos case CHTYPE_TAB:
26534e53048Schristos return 1; /* Hmm, this really need to be handled outside! */
26634e53048Schristos case CHTYPE_NL:
26734e53048Schristos return 0; /* Should this be 1 instead? */
26834e53048Schristos case CHTYPE_PRINT:
26934e53048Schristos return wcwidth(c);
27034e53048Schristos case CHTYPE_NONPRINT:
27134e53048Schristos if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
27234e53048Schristos return 8; /* \U+12345 */
27334e53048Schristos else
27434e53048Schristos return 7; /* \U+1234 */
27534e53048Schristos default:
27634e53048Schristos return 0; /* should not happen */
27734e53048Schristos }
27834e53048Schristos }
27934e53048Schristos
28034e53048Schristos
281a2d6b270Schristos libedit_private ssize_t
ct_visual_char(wchar_t * dst,size_t len,wchar_t c)2820594af80Schristos ct_visual_char(wchar_t *dst, size_t len, wchar_t c)
28334e53048Schristos {
28434e53048Schristos int t = ct_chr_class(c);
28534e53048Schristos switch (t) {
286c9043bbfSchristos case CHTYPE_TAB:
287c9043bbfSchristos case CHTYPE_NL:
28834e53048Schristos case CHTYPE_ASCIICTL:
28934e53048Schristos if (len < 2)
29034e53048Schristos return -1; /* insufficient space */
29134e53048Schristos *dst++ = '^';
29234e53048Schristos if (c == '\177')
29334e53048Schristos *dst = '?'; /* DEL -> ^? */
29434e53048Schristos else
29534e53048Schristos *dst = c | 0100; /* uncontrolify it */
29634e53048Schristos return 2;
29734e53048Schristos case CHTYPE_PRINT:
29834e53048Schristos if (len < 1)
29934e53048Schristos return -1; /* insufficient space */
30034e53048Schristos *dst = c;
30134e53048Schristos return 1;
30234e53048Schristos case CHTYPE_NONPRINT:
30334e53048Schristos /* we only use single-width glyphs for display,
30434e53048Schristos * so this is right */
30534e53048Schristos if ((ssize_t)len < ct_visual_width(c))
30634e53048Schristos return -1; /* insufficient space */
30734e53048Schristos *dst++ = '\\';
30834e53048Schristos *dst++ = 'U';
30934e53048Schristos *dst++ = '+';
31034e53048Schristos #define tohexdigit(v) "0123456789ABCDEF"[v]
31134e53048Schristos if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
31234e53048Schristos *dst++ = tohexdigit(((unsigned int) c >> 16) & 0xf);
31334e53048Schristos *dst++ = tohexdigit(((unsigned int) c >> 12) & 0xf);
31434e53048Schristos *dst++ = tohexdigit(((unsigned int) c >> 8) & 0xf);
31534e53048Schristos *dst++ = tohexdigit(((unsigned int) c >> 4) & 0xf);
31634e53048Schristos *dst = tohexdigit(((unsigned int) c ) & 0xf);
317b71bed95Schristos return c > 0xffff ? 8 : 7;
31834e53048Schristos /*FALLTHROUGH*/
31934e53048Schristos /* these two should be handled outside this function */
32034e53048Schristos default: /* we should never hit the default */
32134e53048Schristos return 0;
32234e53048Schristos }
32334e53048Schristos }
32434e53048Schristos
32534e53048Schristos
32634e53048Schristos
32734e53048Schristos
328a2d6b270Schristos libedit_private int
ct_chr_class(wchar_t c)3290594af80Schristos ct_chr_class(wchar_t c)
33034e53048Schristos {
33134e53048Schristos if (c == '\t')
33234e53048Schristos return CHTYPE_TAB;
33334e53048Schristos else if (c == '\n')
33434e53048Schristos return CHTYPE_NL;
335fcf85103Schristos else if (c < 0x100 && iswcntrl(c))
33634e53048Schristos return CHTYPE_ASCIICTL;
337fcf85103Schristos else if (iswprint(c))
33834e53048Schristos return CHTYPE_PRINT;
33934e53048Schristos else
34034e53048Schristos return CHTYPE_NONPRINT;
34134e53048Schristos }
342