chartype.c revision 1.30.2.1 1 /* $NetBSD: chartype.c,v 1.30.2.1 2017/03/20 06:56:59 pgoyette 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.30.2.1 2017/03/20 06:56:59 pgoyette Exp $");
35 #endif /* not lint && not SCCSID */
36
37 #include <ctype.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "el.h"
42
43 #define CT_BUFSIZ ((size_t)1024)
44
45 static int ct_conv_cbuff_resize(ct_buffer_t *, size_t);
46 static int ct_conv_wbuff_resize(ct_buffer_t *, size_t);
47
48 static int
49 ct_conv_cbuff_resize(ct_buffer_t *conv, size_t csize)
50 {
51 void *p;
52
53 if (csize <= conv->csize)
54 return 0;
55
56 conv->csize = csize;
57
58 p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff));
59 if (p == NULL) {
60 conv->csize = 0;
61 el_free(conv->cbuff);
62 conv->cbuff = NULL;
63 return -1;
64 }
65 conv->cbuff = p;
66 return 0;
67 }
68
69 static int
70 ct_conv_wbuff_resize(ct_buffer_t *conv, size_t wsize)
71 {
72 void *p;
73
74 if (wsize <= conv->wsize)
75 return 0;
76
77 conv->wsize = wsize;
78
79 p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff));
80 if (p == NULL) {
81 conv->wsize = 0;
82 el_free(conv->wbuff);
83 conv->wbuff = NULL;
84 return -1;
85 }
86 conv->wbuff = p;
87 return 0;
88 }
89
90
91 char *
92 ct_encode_string(const wchar_t *s, ct_buffer_t *conv)
93 {
94 char *dst;
95 ssize_t used;
96
97 if (!s)
98 return NULL;
99
100 dst = conv->cbuff;
101 for (;;) {
102 used = (ssize_t)(dst - conv->cbuff);
103 if ((conv->csize - (size_t)used) < 5) {
104 if (ct_conv_cbuff_resize(conv,
105 conv->csize + CT_BUFSIZ) == -1)
106 return NULL;
107 dst = conv->cbuff + used;
108 }
109 if (!*s)
110 break;
111 used = ct_encode_char(dst, (size_t)5, *s);
112 if (used == -1) /* failed to encode, need more buffer space */
113 abort();
114 ++s;
115 dst += used;
116 }
117 *dst = '\0';
118 return conv->cbuff;
119 }
120
121 wchar_t *
122 ct_decode_string(const char *s, ct_buffer_t *conv)
123 {
124 size_t len;
125
126 if (!s)
127 return NULL;
128
129 len = mbstowcs(NULL, s, (size_t)0);
130 if (len == (size_t)-1)
131 return NULL;
132
133 if (conv->wsize < ++len)
134 if (ct_conv_wbuff_resize(conv, len + CT_BUFSIZ) == -1)
135 return NULL;
136
137 mbstowcs(conv->wbuff, s, conv->wsize);
138 return conv->wbuff;
139 }
140
141
142 libedit_private wchar_t **
143 ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv)
144 {
145 size_t bufspace;
146 int i;
147 wchar_t *p;
148 wchar_t **wargv;
149 ssize_t bytes;
150
151 /* Make sure we have enough space in the conversion buffer to store all
152 * the argv strings. */
153 for (i = 0, bufspace = 0; i < argc; ++i)
154 bufspace += argv[i] ? strlen(argv[i]) + 1 : 0;
155 if (conv->wsize < ++bufspace)
156 if (ct_conv_wbuff_resize(conv, bufspace + CT_BUFSIZ) == -1)
157 return NULL;
158
159 wargv = el_malloc((size_t)(argc + 1) * sizeof(*wargv));
160
161 for (i = 0, p = conv->wbuff; i < argc; ++i) {
162 if (!argv[i]) { /* don't pass null pointers to mbstowcs */
163 wargv[i] = NULL;
164 continue;
165 } else {
166 wargv[i] = p;
167 bytes = (ssize_t)mbstowcs(p, argv[i], bufspace);
168 }
169 if (bytes == -1) {
170 el_free(wargv);
171 return NULL;
172 } else
173 bytes++; /* include '\0' in the count */
174 bufspace -= (size_t)bytes;
175 p += bytes;
176 }
177 wargv[i] = NULL;
178
179 return wargv;
180 }
181
182
183 libedit_private size_t
184 ct_enc_width(wchar_t c)
185 {
186 /* UTF-8 encoding specific values */
187 if (c < 0x80)
188 return 1;
189 else if (c < 0x0800)
190 return 2;
191 else if (c < 0x10000)
192 return 3;
193 else if (c < 0x110000)
194 return 4;
195 else
196 return 0; /* not a valid codepoint */
197 }
198
199 libedit_private ssize_t
200 ct_encode_char(char *dst, size_t len, wchar_t c)
201 {
202 ssize_t l = 0;
203 if (len < ct_enc_width(c))
204 return -1;
205 l = wctomb(dst, c);
206
207 if (l < 0) {
208 wctomb(NULL, L'\0');
209 l = 0;
210 }
211 return l;
212 }
213
214 libedit_private const wchar_t *
215 ct_visual_string(const wchar_t *s, ct_buffer_t *conv)
216 {
217 wchar_t *dst;
218 ssize_t used;
219
220 if (!s)
221 return NULL;
222
223 if (ct_conv_wbuff_resize(conv, CT_BUFSIZ) == -1)
224 return NULL;
225
226 used = 0;
227 dst = conv->wbuff;
228 while (*s) {
229 used = ct_visual_char(dst,
230 conv->wsize - (size_t)(dst - conv->wbuff), *s);
231 if (used != -1) {
232 ++s;
233 dst += used;
234 continue;
235 }
236
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)
240 return NULL;
241 dst = conv->wbuff + used;
242 }
243
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)
247 return NULL;
248 dst = conv->wbuff + used;
249 }
250
251 *dst = L'\0';
252 return conv->wbuff;
253 }
254
255
256
257 libedit_private int
258 ct_visual_width(wchar_t c)
259 {
260 int t = ct_chr_class(c);
261 switch (t) {
262 case CHTYPE_ASCIICTL:
263 return 2; /* ^@ ^? etc. */
264 case CHTYPE_TAB:
265 return 1; /* Hmm, this really need to be handled outside! */
266 case CHTYPE_NL:
267 return 0; /* Should this be 1 instead? */
268 case CHTYPE_PRINT:
269 return wcwidth(c);
270 case CHTYPE_NONPRINT:
271 if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
272 return 8; /* \U+12345 */
273 else
274 return 7; /* \U+1234 */
275 default:
276 return 0; /* should not happen */
277 }
278 }
279
280
281 libedit_private ssize_t
282 ct_visual_char(wchar_t *dst, size_t len, wchar_t c)
283 {
284 int t = ct_chr_class(c);
285 switch (t) {
286 case CHTYPE_TAB:
287 case CHTYPE_NL:
288 case CHTYPE_ASCIICTL:
289 if (len < 2)
290 return -1; /* insufficient space */
291 *dst++ = '^';
292 if (c == '\177')
293 *dst = '?'; /* DEL -> ^? */
294 else
295 *dst = c | 0100; /* uncontrolify it */
296 return 2;
297 case CHTYPE_PRINT:
298 if (len < 1)
299 return -1; /* insufficient space */
300 *dst = c;
301 return 1;
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 */
307 *dst++ = '\\';
308 *dst++ = 'U';
309 *dst++ = '+';
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;
318 /*FALLTHROUGH*/
319 /* these two should be handled outside this function */
320 default: /* we should never hit the default */
321 return 0;
322 }
323 }
324
325
326
327
328 libedit_private int
329 ct_chr_class(wchar_t c)
330 {
331 if (c == '\t')
332 return CHTYPE_TAB;
333 else if (c == '\n')
334 return CHTYPE_NL;
335 else if (c < 0x100 && iswcntrl(c))
336 return CHTYPE_ASCIICTL;
337 else if (iswprint(c))
338 return CHTYPE_PRINT;
339 else
340 return CHTYPE_NONPRINT;
341 }
342