xref: /freebsd/contrib/libedit/chartype.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*	$NetBSD: chartype.c,v 1.35 2019/07/23 10:18:52 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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  *
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.
27  */
28 
29 /*
30  * chartype.c: character classification and meta information
31  */
32 #include "config.h"
33 #if !defined(lint) && !defined(SCCSID)
34 __RCSID("$NetBSD: chartype.c,v 1.35 2019/07/23 10:18:52 christos Exp $");
35 #endif /* not lint && not SCCSID */
36 
37 #include <ctype.h>
38 #include <limits.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "el.h"
43 
44 #define CT_BUFSIZ ((size_t)1024)
45 
46 static int ct_conv_cbuff_resize(ct_buffer_t *, size_t);
47 static int ct_conv_wbuff_resize(ct_buffer_t *, size_t);
48 
49 static int
50 ct_conv_cbuff_resize(ct_buffer_t *conv, size_t csize)
51 {
52 	void *p;
53 
54 	if (csize <= conv->csize)
55 		return 0;
56 
57 	conv->csize = csize;
58 
59 	p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff));
60 	if (p == NULL) {
61 		conv->csize = 0;
62 		el_free(conv->cbuff);
63 		conv->cbuff = NULL;
64 		return -1;
65 	}
66 	conv->cbuff = p;
67 	return 0;
68 }
69 
70 static int
71 ct_conv_wbuff_resize(ct_buffer_t *conv, size_t wsize)
72 {
73 	void *p;
74 
75 	if (wsize <= conv->wsize)
76 		return 0;
77 
78 	conv->wsize = wsize;
79 
80 	p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff));
81 	if (p == NULL) {
82 		conv->wsize = 0;
83 		el_free(conv->wbuff);
84 		conv->wbuff = NULL;
85 		return -1;
86 	}
87 	conv->wbuff = p;
88 	return 0;
89 }
90 
91 
92 char *
93 ct_encode_string(const wchar_t *s, ct_buffer_t *conv)
94 {
95 	char *dst;
96 	ssize_t used;
97 
98 	if (!s)
99 		return NULL;
100 
101 	dst = conv->cbuff;
102 	for (;;) {
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)
107 				return NULL;
108 			dst = conv->cbuff + used;
109 		}
110 		if (!*s)
111 			break;
112 		used = ct_encode_char(dst, (size_t)5, *s);
113 		if (used == -1) /* failed to encode, need more buffer space */
114 			abort();
115 		++s;
116 		dst += used;
117 	}
118 	*dst = '\0';
119 	return conv->cbuff;
120 }
121 
122 wchar_t *
123 ct_decode_string(const char *s, ct_buffer_t *conv)
124 {
125 	size_t len;
126 
127 	if (!s)
128 		return NULL;
129 
130 	len = mbstowcs(NULL, s, (size_t)0);
131 	if (len == (size_t)-1)
132 		return NULL;
133 
134 	if (conv->wsize < ++len)
135 		if (ct_conv_wbuff_resize(conv, len + CT_BUFSIZ) == -1)
136 			return NULL;
137 
138 	mbstowcs(conv->wbuff, s, conv->wsize);
139 	return conv->wbuff;
140 }
141 
142 
143 libedit_private wchar_t **
144 ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv)
145 {
146 	size_t bufspace;
147 	int i;
148 	wchar_t *p;
149 	wchar_t **wargv;
150 	ssize_t bytes;
151 
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)
158 			return NULL;
159 
160 	wargv = el_calloc((size_t)(argc + 1), sizeof(*wargv));
161 
162 	for (i = 0, p = conv->wbuff; i < argc; ++i) {
163 		if (!argv[i]) {   /* don't pass null pointers to mbstowcs */
164 			wargv[i] = NULL;
165 			continue;
166 		} else {
167 			wargv[i] = p;
168 			bytes = (ssize_t)mbstowcs(p, argv[i], bufspace);
169 		}
170 		if (bytes == -1) {
171 			el_free(wargv);
172 			return NULL;
173 		} else
174 			bytes++;  /* include '\0' in the count */
175 		bufspace -= (size_t)bytes;
176 		p += bytes;
177 	}
178 	wargv[i] = NULL;
179 
180 	return wargv;
181 }
182 
183 
184 libedit_private size_t
185 ct_enc_width(wchar_t c)
186 {
187 	mbstate_t mbs;
188 	char buf[MB_LEN_MAX];
189 	size_t size;
190 	memset(&mbs, 0, sizeof(mbs));
191 
192 	if ((size = wcrtomb(buf, c, &mbs)) == (size_t)-1)
193 		return 0;
194 	return size;
195 }
196 
197 libedit_private ssize_t
198 ct_encode_char(char *dst, size_t len, wchar_t c)
199 {
200 	ssize_t l = 0;
201 	if (len < ct_enc_width(c))
202 		return -1;
203 	l = wctomb(dst, c);
204 
205 	if (l < 0) {
206 		wctomb(NULL, L'\0');
207 		l = 0;
208 	}
209 	return l;
210 }
211 
212 libedit_private const wchar_t *
213 ct_visual_string(const wchar_t *s, ct_buffer_t *conv)
214 {
215 	wchar_t *dst;
216 	ssize_t used;
217 
218 	if (!s)
219 		return NULL;
220 
221 	if (ct_conv_wbuff_resize(conv, CT_BUFSIZ) == -1)
222 		return NULL;
223 
224 	used = 0;
225 	dst = conv->wbuff;
226 	while (*s) {
227 		used = ct_visual_char(dst,
228 		    conv->wsize - (size_t)(dst - conv->wbuff), *s);
229 		if (used != -1) {
230 			++s;
231 			dst += used;
232 			continue;
233 		}
234 
235 		/* failed to encode, need more buffer space */
236 		used = dst - conv->wbuff;
237 		if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
238 			return NULL;
239 		dst = conv->wbuff + used;
240 	}
241 
242 	if (dst >= (conv->wbuff + conv->wsize)) { /* sigh */
243 		used = dst - conv->wbuff;
244 		if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
245 			return NULL;
246 		dst = conv->wbuff + used;
247 	}
248 
249 	*dst = L'\0';
250 	return conv->wbuff;
251 }
252 
253 
254 
255 libedit_private int
256 ct_visual_width(wchar_t c)
257 {
258 	int t = ct_chr_class(c);
259 	switch (t) {
260 	case CHTYPE_ASCIICTL:
261 		return 2; /* ^@ ^? etc. */
262 	case CHTYPE_TAB:
263 		return 1; /* Hmm, this really need to be handled outside! */
264 	case CHTYPE_NL:
265 		return 0; /* Should this be 1 instead? */
266 	case CHTYPE_PRINT:
267 		return wcwidth(c);
268 	case CHTYPE_NONPRINT:
269 		if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
270 			return 8; /* \U+12345 */
271 		else
272 			return 7; /* \U+1234 */
273 	default:
274 		return 0; /* should not happen */
275 	}
276 }
277 
278 
279 libedit_private ssize_t
280 ct_visual_char(wchar_t *dst, size_t len, wchar_t c)
281 {
282 	int t = ct_chr_class(c);
283 	switch (t) {
284 	case CHTYPE_TAB:
285 	case CHTYPE_NL:
286 	case CHTYPE_ASCIICTL:
287 		if (len < 2)
288 			return -1;   /* insufficient space */
289 		*dst++ = '^';
290 		if (c == '\177')
291 			*dst = '?'; /* DEL -> ^? */
292 		else
293 			*dst = c | 0100;    /* uncontrolify it */
294 		return 2;
295 	case CHTYPE_PRINT:
296 		if (len < 1)
297 			return -1;  /* insufficient space */
298 		*dst = c;
299 		return 1;
300 	case CHTYPE_NONPRINT:
301 		/* we only use single-width glyphs for display,
302 		 * so this is right */
303 		if ((ssize_t)len < ct_visual_width(c))
304 			return -1;   /* insufficient space */
305 		*dst++ = '\\';
306 		*dst++ = 'U';
307 		*dst++ = '+';
308 #define tohexdigit(v) "0123456789ABCDEF"[v]
309 		if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
310 			*dst++ = tohexdigit(((unsigned int) c >> 16) & 0xf);
311 		*dst++ = tohexdigit(((unsigned int) c >> 12) & 0xf);
312 		*dst++ = tohexdigit(((unsigned int) c >>  8) & 0xf);
313 		*dst++ = tohexdigit(((unsigned int) c >>  4) & 0xf);
314 		*dst   = tohexdigit(((unsigned int) c      ) & 0xf);
315 		return c > 0xffff ? 8 : 7;
316 		/*FALLTHROUGH*/
317 	/* these two should be handled outside this function */
318 	default:            /* we should never hit the default */
319 		return 0;
320 	}
321 }
322 
323 
324 
325 
326 libedit_private int
327 ct_chr_class(wchar_t c)
328 {
329 	if (c == '\t')
330 		return CHTYPE_TAB;
331 	else if (c == '\n')
332 		return CHTYPE_NL;
333 	else if (c < 0x100 && iswcntrl(c))
334 		return CHTYPE_ASCIICTL;
335 	else if (iswprint(c))
336 		return CHTYPE_PRINT;
337 	else
338 		return CHTYPE_NONPRINT;
339 }
340