1 1.37 mrg /* $NetBSD: chartype.c,v 1.37 2023/08/10 20:38:00 mrg Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2009 The NetBSD Foundation, Inc. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 27 1.1 christos */ 28 1.1 christos 29 1.1 christos /* 30 1.1 christos * chartype.c: character classification and meta information 31 1.1 christos */ 32 1.1 christos #include "config.h" 33 1.1 christos #if !defined(lint) && !defined(SCCSID) 34 1.37 mrg __RCSID("$NetBSD: chartype.c,v 1.37 2023/08/10 20:38:00 mrg Exp $"); 35 1.1 christos #endif /* not lint && not SCCSID */ 36 1.18 christos 37 1.18 christos #include <ctype.h> 38 1.33 christos #include <limits.h> 39 1.18 christos #include <stdlib.h> 40 1.18 christos #include <string.h> 41 1.18 christos 42 1.17 christos #include "el.h" 43 1.16 christos 44 1.9 christos #define CT_BUFSIZ ((size_t)1024) 45 1.1 christos 46 1.28 christos static int ct_conv_cbuff_resize(ct_buffer_t *, size_t); 47 1.28 christos static int ct_conv_wbuff_resize(ct_buffer_t *, size_t); 48 1.27 christos 49 1.28 christos static int 50 1.12 christos ct_conv_cbuff_resize(ct_buffer_t *conv, size_t csize) 51 1.1 christos { 52 1.1 christos void *p; 53 1.12 christos 54 1.12 christos if (csize <= conv->csize) 55 1.12 christos return 0; 56 1.12 christos 57 1.12 christos conv->csize = csize; 58 1.12 christos 59 1.12 christos p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff)); 60 1.12 christos if (p == NULL) { 61 1.12 christos conv->csize = 0; 62 1.12 christos el_free(conv->cbuff); 63 1.12 christos conv->cbuff = NULL; 64 1.12 christos return -1; 65 1.1 christos } 66 1.12 christos conv->cbuff = p; 67 1.12 christos return 0; 68 1.12 christos } 69 1.12 christos 70 1.28 christos static int 71 1.12 christos ct_conv_wbuff_resize(ct_buffer_t *conv, size_t wsize) 72 1.12 christos { 73 1.12 christos void *p; 74 1.12 christos 75 1.19 christos if (wsize <= conv->wsize) 76 1.12 christos return 0; 77 1.12 christos 78 1.12 christos conv->wsize = wsize; 79 1.1 christos 80 1.12 christos p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff)); 81 1.12 christos if (p == NULL) { 82 1.12 christos conv->wsize = 0; 83 1.12 christos el_free(conv->wbuff); 84 1.12 christos conv->wbuff = NULL; 85 1.12 christos return -1; 86 1.1 christos } 87 1.12 christos conv->wbuff = p; 88 1.11 christos return 0; 89 1.1 christos } 90 1.1 christos 91 1.1 christos 92 1.28 christos char * 93 1.26 christos ct_encode_string(const wchar_t *s, ct_buffer_t *conv) 94 1.1 christos { 95 1.1 christos char *dst; 96 1.11 christos ssize_t used; 97 1.1 christos 98 1.1 christos if (!s) 99 1.1 christos return NULL; 100 1.1 christos 101 1.1 christos dst = conv->cbuff; 102 1.11 christos for (;;) { 103 1.11 christos used = (ssize_t)(dst - conv->cbuff); 104 1.11 christos if ((conv->csize - (size_t)used) < 5) { 105 1.12 christos if (ct_conv_cbuff_resize(conv, 106 1.12 christos conv->csize + CT_BUFSIZ) == -1) 107 1.1 christos return NULL; 108 1.1 christos dst = conv->cbuff + used; 109 1.1 christos } 110 1.11 christos if (!*s) 111 1.11 christos break; 112 1.9 christos used = ct_encode_char(dst, (size_t)5, *s); 113 1.5 christos if (used == -1) /* failed to encode, need more buffer space */ 114 1.5 christos abort(); 115 1.5 christos ++s; 116 1.1 christos dst += used; 117 1.1 christos } 118 1.1 christos *dst = '\0'; 119 1.1 christos return conv->cbuff; 120 1.1 christos } 121 1.1 christos 122 1.28 christos wchar_t * 123 1.1 christos ct_decode_string(const char *s, ct_buffer_t *conv) 124 1.1 christos { 125 1.11 christos size_t len; 126 1.1 christos 127 1.1 christos if (!s) 128 1.1 christos return NULL; 129 1.1 christos 130 1.25 christos len = mbstowcs(NULL, s, (size_t)0); 131 1.6 christos if (len == (size_t)-1) 132 1.6 christos return NULL; 133 1.11 christos 134 1.12 christos if (conv->wsize < ++len) 135 1.12 christos if (ct_conv_wbuff_resize(conv, len + CT_BUFSIZ) == -1) 136 1.11 christos return NULL; 137 1.11 christos 138 1.25 christos mbstowcs(conv->wbuff, s, conv->wsize); 139 1.1 christos return conv->wbuff; 140 1.1 christos } 141 1.1 christos 142 1.1 christos 143 1.30 christos libedit_private wchar_t ** 144 1.1 christos ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv) 145 1.1 christos { 146 1.1 christos size_t bufspace; 147 1.1 christos int i; 148 1.26 christos wchar_t *p; 149 1.26 christos wchar_t **wargv; 150 1.1 christos ssize_t bytes; 151 1.1 christos 152 1.1 christos /* Make sure we have enough space in the conversion buffer to store all 153 1.1 christos * the argv strings. */ 154 1.1 christos for (i = 0, bufspace = 0; i < argc; ++i) 155 1.1 christos bufspace += argv[i] ? strlen(argv[i]) + 1 : 0; 156 1.12 christos if (conv->wsize < ++bufspace) 157 1.12 christos if (ct_conv_wbuff_resize(conv, bufspace + CT_BUFSIZ) == -1) 158 1.11 christos return NULL; 159 1.1 christos 160 1.35 christos wargv = el_calloc((size_t)(argc + 1), sizeof(*wargv)); 161 1.36 christos if (wargv == NULL) 162 1.36 christos return NULL; 163 1.1 christos 164 1.1 christos for (i = 0, p = conv->wbuff; i < argc; ++i) { 165 1.1 christos if (!argv[i]) { /* don't pass null pointers to mbstowcs */ 166 1.1 christos wargv[i] = NULL; 167 1.3 christos continue; 168 1.1 christos } else { 169 1.1 christos wargv[i] = p; 170 1.10 christos bytes = (ssize_t)mbstowcs(p, argv[i], bufspace); 171 1.1 christos } 172 1.1 christos if (bytes == -1) { 173 1.1 christos el_free(wargv); 174 1.1 christos return NULL; 175 1.1 christos } else 176 1.1 christos bytes++; /* include '\0' in the count */ 177 1.10 christos bufspace -= (size_t)bytes; 178 1.1 christos p += bytes; 179 1.1 christos } 180 1.31 christos wargv[i] = NULL; 181 1.1 christos 182 1.1 christos return wargv; 183 1.1 christos } 184 1.1 christos 185 1.1 christos 186 1.30 christos libedit_private size_t 187 1.26 christos ct_enc_width(wchar_t c) 188 1.1 christos { 189 1.34 christos mbstate_t mbs; 190 1.32 christos char buf[MB_LEN_MAX]; 191 1.34 christos size_t size; 192 1.34 christos memset(&mbs, 0, sizeof(mbs)); 193 1.34 christos 194 1.34 christos if ((size = wcrtomb(buf, c, &mbs)) == (size_t)-1) 195 1.32 christos return 0; 196 1.34 christos return size; 197 1.1 christos } 198 1.1 christos 199 1.30 christos libedit_private ssize_t 200 1.26 christos ct_encode_char(char *dst, size_t len, wchar_t c) 201 1.1 christos { 202 1.1 christos ssize_t l = 0; 203 1.4 christos if (len < ct_enc_width(c)) 204 1.1 christos return -1; 205 1.25 christos l = wctomb(dst, c); 206 1.1 christos 207 1.1 christos if (l < 0) { 208 1.25 christos wctomb(NULL, L'\0'); 209 1.1 christos l = 0; 210 1.1 christos } 211 1.1 christos return l; 212 1.1 christos } 213 1.14 christos 214 1.30 christos libedit_private const wchar_t * 215 1.29 christos ct_visual_string(const wchar_t *s, ct_buffer_t *conv) 216 1.1 christos { 217 1.26 christos wchar_t *dst; 218 1.29 christos ssize_t used; 219 1.1 christos 220 1.1 christos if (!s) 221 1.1 christos return NULL; 222 1.29 christos 223 1.29 christos if (ct_conv_wbuff_resize(conv, CT_BUFSIZ) == -1) 224 1.29 christos return NULL; 225 1.29 christos 226 1.29 christos used = 0; 227 1.29 christos dst = conv->wbuff; 228 1.1 christos while (*s) { 229 1.29 christos used = ct_visual_char(dst, 230 1.29 christos conv->wsize - (size_t)(dst - conv->wbuff), *s); 231 1.29 christos if (used != -1) { 232 1.29 christos ++s; 233 1.29 christos dst += used; 234 1.29 christos continue; 235 1.1 christos } 236 1.29 christos 237 1.29 christos /* failed to encode, need more buffer space */ 238 1.37 mrg uintptr_t sused = (uintptr_t)dst - (uintptr_t)conv->wbuff; 239 1.29 christos if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1) 240 1.29 christos return NULL; 241 1.37 mrg dst = conv->wbuff + sused; 242 1.1 christos } 243 1.29 christos 244 1.29 christos if (dst >= (conv->wbuff + conv->wsize)) { /* sigh */ 245 1.37 mrg uintptr_t sused = (uintptr_t)dst - (uintptr_t)conv->wbuff; 246 1.29 christos if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1) 247 1.29 christos return NULL; 248 1.37 mrg dst = conv->wbuff + sused; 249 1.1 christos } 250 1.29 christos 251 1.29 christos *dst = L'\0'; 252 1.29 christos return conv->wbuff; 253 1.1 christos } 254 1.1 christos 255 1.1 christos 256 1.1 christos 257 1.30 christos libedit_private int 258 1.26 christos ct_visual_width(wchar_t c) 259 1.1 christos { 260 1.1 christos int t = ct_chr_class(c); 261 1.1 christos switch (t) { 262 1.1 christos case CHTYPE_ASCIICTL: 263 1.1 christos return 2; /* ^@ ^? etc. */ 264 1.1 christos case CHTYPE_TAB: 265 1.1 christos return 1; /* Hmm, this really need to be handled outside! */ 266 1.1 christos case CHTYPE_NL: 267 1.1 christos return 0; /* Should this be 1 instead? */ 268 1.1 christos case CHTYPE_PRINT: 269 1.1 christos return wcwidth(c); 270 1.1 christos case CHTYPE_NONPRINT: 271 1.1 christos if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */ 272 1.1 christos return 8; /* \U+12345 */ 273 1.1 christos else 274 1.1 christos return 7; /* \U+1234 */ 275 1.1 christos default: 276 1.1 christos return 0; /* should not happen */ 277 1.1 christos } 278 1.1 christos } 279 1.1 christos 280 1.1 christos 281 1.30 christos libedit_private ssize_t 282 1.26 christos ct_visual_char(wchar_t *dst, size_t len, wchar_t c) 283 1.1 christos { 284 1.1 christos int t = ct_chr_class(c); 285 1.1 christos switch (t) { 286 1.3 christos case CHTYPE_TAB: 287 1.3 christos case CHTYPE_NL: 288 1.1 christos case CHTYPE_ASCIICTL: 289 1.1 christos if (len < 2) 290 1.1 christos return -1; /* insufficient space */ 291 1.1 christos *dst++ = '^'; 292 1.1 christos if (c == '\177') 293 1.1 christos *dst = '?'; /* DEL -> ^? */ 294 1.1 christos else 295 1.1 christos *dst = c | 0100; /* uncontrolify it */ 296 1.1 christos return 2; 297 1.1 christos case CHTYPE_PRINT: 298 1.1 christos if (len < 1) 299 1.1 christos return -1; /* insufficient space */ 300 1.1 christos *dst = c; 301 1.1 christos return 1; 302 1.1 christos case CHTYPE_NONPRINT: 303 1.1 christos /* we only use single-width glyphs for display, 304 1.1 christos * so this is right */ 305 1.1 christos if ((ssize_t)len < ct_visual_width(c)) 306 1.1 christos return -1; /* insufficient space */ 307 1.1 christos *dst++ = '\\'; 308 1.1 christos *dst++ = 'U'; 309 1.1 christos *dst++ = '+'; 310 1.1 christos #define tohexdigit(v) "0123456789ABCDEF"[v] 311 1.1 christos if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */ 312 1.1 christos *dst++ = tohexdigit(((unsigned int) c >> 16) & 0xf); 313 1.1 christos *dst++ = tohexdigit(((unsigned int) c >> 12) & 0xf); 314 1.1 christos *dst++ = tohexdigit(((unsigned int) c >> 8) & 0xf); 315 1.1 christos *dst++ = tohexdigit(((unsigned int) c >> 4) & 0xf); 316 1.1 christos *dst = tohexdigit(((unsigned int) c ) & 0xf); 317 1.8 christos return c > 0xffff ? 8 : 7; 318 1.1 christos /*FALLTHROUGH*/ 319 1.1 christos /* these two should be handled outside this function */ 320 1.1 christos default: /* we should never hit the default */ 321 1.1 christos return 0; 322 1.1 christos } 323 1.1 christos } 324 1.1 christos 325 1.1 christos 326 1.1 christos 327 1.1 christos 328 1.30 christos libedit_private int 329 1.26 christos ct_chr_class(wchar_t c) 330 1.1 christos { 331 1.1 christos if (c == '\t') 332 1.1 christos return CHTYPE_TAB; 333 1.1 christos else if (c == '\n') 334 1.1 christos return CHTYPE_NL; 335 1.25 christos else if (c < 0x100 && iswcntrl(c)) 336 1.1 christos return CHTYPE_ASCIICTL; 337 1.25 christos else if (iswprint(c)) 338 1.1 christos return CHTYPE_PRINT; 339 1.1 christos else 340 1.1 christos return CHTYPE_NONPRINT; 341 1.1 christos } 342