2017-09-13 15:57:58 +00:00
|
|
|
/* $NetBSD: chartype.c,v 1.31 2017/01/09 02:54:18 christos Exp $ */
|
2011-03-31 18:34:50 +00:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* chartype.c: character classification and meta information
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#if !defined(lint) && !defined(SCCSID)
|
2017-09-13 15:57:58 +00:00
|
|
|
__RCSID("$NetBSD: chartype.c,v 1.31 2017/01/09 02:54:18 christos Exp $");
|
2011-03-31 18:34:50 +00:00
|
|
|
#endif /* not lint && not SCCSID */
|
2016-02-28 15:57:37 +00:00
|
|
|
|
|
|
|
#include <ctype.h>
|
2011-03-31 18:34:50 +00:00
|
|
|
#include <stdlib.h>
|
2016-02-28 15:57:37 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "el.h"
|
2011-03-31 18:34:50 +00:00
|
|
|
|
2012-09-25 10:41:30 +00:00
|
|
|
#define CT_BUFSIZ ((size_t)1024)
|
2011-03-31 18:34:50 +00:00
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
static int ct_conv_cbuff_resize(ct_buffer_t *, size_t);
|
|
|
|
static int ct_conv_wbuff_resize(ct_buffer_t *, size_t);
|
|
|
|
|
|
|
|
static int
|
2016-02-07 01:38:50 +00:00
|
|
|
ct_conv_cbuff_resize(ct_buffer_t *conv, size_t csize)
|
2011-03-31 18:34:50 +00:00
|
|
|
{
|
|
|
|
void *p;
|
2016-02-07 01:38:50 +00:00
|
|
|
|
|
|
|
if (csize <= conv->csize)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
conv->csize = csize;
|
|
|
|
|
|
|
|
p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff));
|
|
|
|
if (p == NULL) {
|
|
|
|
conv->csize = 0;
|
|
|
|
el_free(conv->cbuff);
|
|
|
|
conv->cbuff = NULL;
|
|
|
|
return -1;
|
2011-03-31 18:34:50 +00:00
|
|
|
}
|
2016-02-07 01:38:50 +00:00
|
|
|
conv->cbuff = p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
static int
|
2016-02-07 01:38:50 +00:00
|
|
|
ct_conv_wbuff_resize(ct_buffer_t *conv, size_t wsize)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
2016-02-28 15:57:37 +00:00
|
|
|
if (wsize <= conv->wsize)
|
2016-02-07 01:38:50 +00:00
|
|
|
return 0;
|
2011-03-31 18:34:50 +00:00
|
|
|
|
2016-02-07 01:38:50 +00:00
|
|
|
conv->wsize = wsize;
|
|
|
|
|
|
|
|
p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff));
|
|
|
|
if (p == NULL) {
|
|
|
|
conv->wsize = 0;
|
|
|
|
el_free(conv->wbuff);
|
|
|
|
conv->wbuff = NULL;
|
|
|
|
return -1;
|
2011-03-31 18:34:50 +00:00
|
|
|
}
|
2016-02-07 01:38:50 +00:00
|
|
|
conv->wbuff = p;
|
|
|
|
return 0;
|
2011-03-31 18:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
char *
|
|
|
|
ct_encode_string(const wchar_t *s, ct_buffer_t *conv)
|
2011-03-31 18:34:50 +00:00
|
|
|
{
|
|
|
|
char *dst;
|
2016-02-07 01:38:50 +00:00
|
|
|
ssize_t used;
|
2011-03-31 18:34:50 +00:00
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dst = conv->cbuff;
|
2016-02-07 01:38:50 +00:00
|
|
|
for (;;) {
|
|
|
|
used = (ssize_t)(dst - conv->cbuff);
|
|
|
|
if ((conv->csize - (size_t)used) < 5) {
|
|
|
|
if (ct_conv_cbuff_resize(conv,
|
|
|
|
conv->csize + CT_BUFSIZ) == -1)
|
2011-03-31 18:34:50 +00:00
|
|
|
return NULL;
|
|
|
|
dst = conv->cbuff + used;
|
|
|
|
}
|
2016-02-07 01:38:50 +00:00
|
|
|
if (!*s)
|
|
|
|
break;
|
2012-09-25 10:41:30 +00:00
|
|
|
used = ct_encode_char(dst, (size_t)5, *s);
|
|
|
|
if (used == -1) /* failed to encode, need more buffer space */
|
|
|
|
abort();
|
|
|
|
++s;
|
2011-03-31 18:34:50 +00:00
|
|
|
dst += used;
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
|
|
|
return conv->cbuff;
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
wchar_t *
|
2011-03-31 18:34:50 +00:00
|
|
|
ct_decode_string(const char *s, ct_buffer_t *conv)
|
|
|
|
{
|
2016-02-07 01:38:50 +00:00
|
|
|
size_t len;
|
2011-03-31 18:34:50 +00:00
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
len = mbstowcs(NULL, s, (size_t)0);
|
2012-09-25 10:41:30 +00:00
|
|
|
if (len == (size_t)-1)
|
|
|
|
return NULL;
|
2016-02-07 01:38:50 +00:00
|
|
|
|
|
|
|
if (conv->wsize < ++len)
|
|
|
|
if (ct_conv_wbuff_resize(conv, len + CT_BUFSIZ) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
mbstowcs(conv->wbuff, s, conv->wsize);
|
2011-03-31 18:34:50 +00:00
|
|
|
return conv->wbuff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
libedit_private wchar_t **
|
2011-03-31 18:34:50 +00:00
|
|
|
ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv)
|
|
|
|
{
|
|
|
|
size_t bufspace;
|
|
|
|
int i;
|
2017-09-13 15:57:58 +00:00
|
|
|
wchar_t *p;
|
|
|
|
wchar_t **wargv;
|
2011-03-31 18:34:50 +00:00
|
|
|
ssize_t bytes;
|
|
|
|
|
|
|
|
/* Make sure we have enough space in the conversion buffer to store all
|
|
|
|
* the argv strings. */
|
|
|
|
for (i = 0, bufspace = 0; i < argc; ++i)
|
|
|
|
bufspace += argv[i] ? strlen(argv[i]) + 1 : 0;
|
2016-02-07 01:38:50 +00:00
|
|
|
if (conv->wsize < ++bufspace)
|
|
|
|
if (ct_conv_wbuff_resize(conv, bufspace + CT_BUFSIZ) == -1)
|
|
|
|
return NULL;
|
2011-03-31 18:34:50 +00:00
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
wargv = el_malloc((size_t)(argc + 1) * sizeof(*wargv));
|
2011-03-31 18:34:50 +00:00
|
|
|
|
|
|
|
for (i = 0, p = conv->wbuff; i < argc; ++i) {
|
|
|
|
if (!argv[i]) { /* don't pass null pointers to mbstowcs */
|
|
|
|
wargv[i] = NULL;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
wargv[i] = p;
|
2012-09-25 10:41:30 +00:00
|
|
|
bytes = (ssize_t)mbstowcs(p, argv[i], bufspace);
|
2011-03-31 18:34:50 +00:00
|
|
|
}
|
|
|
|
if (bytes == -1) {
|
|
|
|
el_free(wargv);
|
|
|
|
return NULL;
|
|
|
|
} else
|
|
|
|
bytes++; /* include '\0' in the count */
|
2012-09-25 10:41:30 +00:00
|
|
|
bufspace -= (size_t)bytes;
|
2011-03-31 18:34:50 +00:00
|
|
|
p += bytes;
|
|
|
|
}
|
2017-09-13 15:57:58 +00:00
|
|
|
wargv[i] = NULL;
|
2011-03-31 18:34:50 +00:00
|
|
|
|
|
|
|
return wargv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
libedit_private size_t
|
|
|
|
ct_enc_width(wchar_t c)
|
2011-03-31 18:34:50 +00:00
|
|
|
{
|
|
|
|
/* UTF-8 encoding specific values */
|
|
|
|
if (c < 0x80)
|
|
|
|
return 1;
|
|
|
|
else if (c < 0x0800)
|
|
|
|
return 2;
|
|
|
|
else if (c < 0x10000)
|
|
|
|
return 3;
|
|
|
|
else if (c < 0x110000)
|
|
|
|
return 4;
|
|
|
|
else
|
|
|
|
return 0; /* not a valid codepoint */
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
libedit_private ssize_t
|
|
|
|
ct_encode_char(char *dst, size_t len, wchar_t c)
|
2011-03-31 18:34:50 +00:00
|
|
|
{
|
|
|
|
ssize_t l = 0;
|
|
|
|
if (len < ct_enc_width(c))
|
|
|
|
return -1;
|
2017-09-13 15:57:58 +00:00
|
|
|
l = wctomb(dst, c);
|
2011-03-31 18:34:50 +00:00
|
|
|
|
|
|
|
if (l < 0) {
|
2017-09-13 15:57:58 +00:00
|
|
|
wctomb(NULL, L'\0');
|
2011-03-31 18:34:50 +00:00
|
|
|
l = 0;
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
2016-02-28 15:57:37 +00:00
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
libedit_private const wchar_t *
|
|
|
|
ct_visual_string(const wchar_t *s, ct_buffer_t *conv)
|
2011-03-31 18:34:50 +00:00
|
|
|
{
|
2017-09-13 15:57:58 +00:00
|
|
|
wchar_t *dst;
|
|
|
|
ssize_t used;
|
2011-03-31 18:34:50 +00:00
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
2017-09-13 15:57:58 +00:00
|
|
|
|
|
|
|
if (ct_conv_wbuff_resize(conv, CT_BUFSIZ) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
used = 0;
|
|
|
|
dst = conv->wbuff;
|
2011-03-31 18:34:50 +00:00
|
|
|
while (*s) {
|
2017-09-13 15:57:58 +00:00
|
|
|
used = ct_visual_char(dst,
|
|
|
|
conv->wsize - (size_t)(dst - conv->wbuff), *s);
|
|
|
|
if (used != -1) {
|
|
|
|
++s;
|
|
|
|
dst += used;
|
|
|
|
continue;
|
2011-03-31 18:34:50 +00:00
|
|
|
}
|
2017-09-13 15:57:58 +00:00
|
|
|
|
|
|
|
/* failed to encode, need more buffer space */
|
|
|
|
used = dst - conv->wbuff;
|
|
|
|
if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
|
|
|
|
return NULL;
|
|
|
|
dst = conv->wbuff + used;
|
2011-03-31 18:34:50 +00:00
|
|
|
}
|
2017-09-13 15:57:58 +00:00
|
|
|
|
|
|
|
if (dst >= (conv->wbuff + conv->wsize)) { /* sigh */
|
|
|
|
used = dst - conv->wbuff;
|
|
|
|
if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
|
|
|
|
return NULL;
|
|
|
|
dst = conv->wbuff + used;
|
2011-03-31 18:34:50 +00:00
|
|
|
}
|
2017-09-13 15:57:58 +00:00
|
|
|
|
|
|
|
*dst = L'\0';
|
|
|
|
return conv->wbuff;
|
2011-03-31 18:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
libedit_private int
|
|
|
|
ct_visual_width(wchar_t c)
|
2011-03-31 18:34:50 +00:00
|
|
|
{
|
|
|
|
int t = ct_chr_class(c);
|
|
|
|
switch (t) {
|
|
|
|
case CHTYPE_ASCIICTL:
|
|
|
|
return 2; /* ^@ ^? etc. */
|
|
|
|
case CHTYPE_TAB:
|
|
|
|
return 1; /* Hmm, this really need to be handled outside! */
|
|
|
|
case CHTYPE_NL:
|
|
|
|
return 0; /* Should this be 1 instead? */
|
|
|
|
case CHTYPE_PRINT:
|
|
|
|
return wcwidth(c);
|
|
|
|
case CHTYPE_NONPRINT:
|
|
|
|
if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
|
|
|
|
return 8; /* \U+12345 */
|
|
|
|
else
|
|
|
|
return 7; /* \U+1234 */
|
|
|
|
default:
|
|
|
|
return 0; /* should not happen */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
libedit_private ssize_t
|
|
|
|
ct_visual_char(wchar_t *dst, size_t len, wchar_t c)
|
2011-03-31 18:34:50 +00:00
|
|
|
{
|
|
|
|
int t = ct_chr_class(c);
|
|
|
|
switch (t) {
|
|
|
|
case CHTYPE_TAB:
|
|
|
|
case CHTYPE_NL:
|
|
|
|
case CHTYPE_ASCIICTL:
|
|
|
|
if (len < 2)
|
|
|
|
return -1; /* insufficient space */
|
|
|
|
*dst++ = '^';
|
|
|
|
if (c == '\177')
|
|
|
|
*dst = '?'; /* DEL -> ^? */
|
|
|
|
else
|
|
|
|
*dst = c | 0100; /* uncontrolify it */
|
|
|
|
return 2;
|
|
|
|
case CHTYPE_PRINT:
|
|
|
|
if (len < 1)
|
|
|
|
return -1; /* insufficient space */
|
|
|
|
*dst = c;
|
|
|
|
return 1;
|
|
|
|
case CHTYPE_NONPRINT:
|
|
|
|
/* we only use single-width glyphs for display,
|
|
|
|
* so this is right */
|
|
|
|
if ((ssize_t)len < ct_visual_width(c))
|
|
|
|
return -1; /* insufficient space */
|
|
|
|
*dst++ = '\\';
|
|
|
|
*dst++ = 'U';
|
|
|
|
*dst++ = '+';
|
|
|
|
#define tohexdigit(v) "0123456789ABCDEF"[v]
|
|
|
|
if (c > 0xffff) /* prefer standard 4-byte display over 5-byte */
|
|
|
|
*dst++ = tohexdigit(((unsigned int) c >> 16) & 0xf);
|
|
|
|
*dst++ = tohexdigit(((unsigned int) c >> 12) & 0xf);
|
|
|
|
*dst++ = tohexdigit(((unsigned int) c >> 8) & 0xf);
|
|
|
|
*dst++ = tohexdigit(((unsigned int) c >> 4) & 0xf);
|
|
|
|
*dst = tohexdigit(((unsigned int) c ) & 0xf);
|
2012-09-25 10:41:30 +00:00
|
|
|
return c > 0xffff ? 8 : 7;
|
2011-03-31 18:34:50 +00:00
|
|
|
/*FALLTHROUGH*/
|
|
|
|
/* these two should be handled outside this function */
|
|
|
|
default: /* we should never hit the default */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-13 15:57:58 +00:00
|
|
|
libedit_private int
|
|
|
|
ct_chr_class(wchar_t c)
|
2011-03-31 18:34:50 +00:00
|
|
|
{
|
|
|
|
if (c == '\t')
|
|
|
|
return CHTYPE_TAB;
|
|
|
|
else if (c == '\n')
|
|
|
|
return CHTYPE_NL;
|
2017-09-13 15:57:58 +00:00
|
|
|
else if (c < 0x100 && iswcntrl(c))
|
2011-03-31 18:34:50 +00:00
|
|
|
return CHTYPE_ASCIICTL;
|
2017-09-13 15:57:58 +00:00
|
|
|
else if (iswprint(c))
|
2011-03-31 18:34:50 +00:00
|
|
|
return CHTYPE_PRINT;
|
|
|
|
else
|
|
|
|
return CHTYPE_NONPRINT;
|
|
|
|
}
|