2019-01-29 01:47:00 -08:00
|
|
|
/* $OpenBSD: chartype.c,v 1.16 2019/01/29 09:47:00 yasuoka Exp $ */
|
2013-05-21 17:31:38 -07:00
|
|
|
/* $NetBSD: chartype.c,v 1.6 2011/07/28 00:48:21 christos Exp $ */
|
2010-06-29 17:05:35 -07: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"
|
2016-03-20 16:48:27 -07:00
|
|
|
|
|
|
|
#include <ctype.h>
|
2011-07-06 22:40:42 -07:00
|
|
|
#include <stdlib.h>
|
2016-03-20 16:48:27 -07:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "el.h"
|
2010-06-29 17:05:35 -07:00
|
|
|
|
|
|
|
#define CT_BUFSIZ 1024
|
|
|
|
|
2016-04-11 14:17:29 -07:00
|
|
|
static void ct_conv_buff_resize(ct_buffer_t *, size_t, size_t);
|
2016-04-11 13:54:05 -07:00
|
|
|
|
2016-04-11 14:17:29 -07:00
|
|
|
static void
|
2010-06-29 17:05:35 -07:00
|
|
|
ct_conv_buff_resize(ct_buffer_t *conv, size_t mincsize, size_t minwsize)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
if (mincsize > conv->csize) {
|
|
|
|
conv->csize = mincsize;
|
2014-10-16 23:07:50 -07:00
|
|
|
p = reallocarray(conv->cbuff, conv->csize, sizeof(char));
|
2010-06-29 17:05:35 -07:00
|
|
|
if (p == NULL) {
|
|
|
|
conv->csize = 0;
|
2014-10-16 23:07:50 -07:00
|
|
|
free(conv->cbuff);
|
2010-06-29 17:05:35 -07:00
|
|
|
conv->cbuff = NULL;
|
2016-03-20 16:48:27 -07:00
|
|
|
} else
|
2010-06-29 17:05:35 -07:00
|
|
|
conv->cbuff = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minwsize > conv->wsize) {
|
|
|
|
conv->wsize = minwsize;
|
2016-04-11 13:43:33 -07:00
|
|
|
p = reallocarray(conv->wbuff, conv->wsize, sizeof(wchar_t));
|
2010-06-29 17:05:35 -07:00
|
|
|
if (p == NULL) {
|
|
|
|
conv->wsize = 0;
|
2014-10-16 23:07:50 -07:00
|
|
|
free(conv->wbuff);
|
2010-06-29 17:05:35 -07:00
|
|
|
conv->wbuff = NULL;
|
|
|
|
} else
|
|
|
|
conv->wbuff = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-11 14:17:29 -07:00
|
|
|
char *
|
2016-04-11 13:43:33 -07:00
|
|
|
ct_encode_string(const wchar_t *s, ct_buffer_t *conv)
|
2010-06-29 17:05:35 -07:00
|
|
|
{
|
|
|
|
char *dst;
|
|
|
|
ssize_t used = 0;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
if (!conv->cbuff)
|
|
|
|
ct_conv_buff_resize(conv, CT_BUFSIZ, 0);
|
|
|
|
if (!conv->cbuff)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dst = conv->cbuff;
|
|
|
|
while (*s) {
|
2013-05-21 17:31:38 -07:00
|
|
|
used = conv->csize - (dst - conv->cbuff);
|
|
|
|
if (used < 5) {
|
2010-06-29 17:05:35 -07:00
|
|
|
used = dst - conv->cbuff;
|
|
|
|
ct_conv_buff_resize(conv, conv->csize + CT_BUFSIZ, 0);
|
|
|
|
if (!conv->cbuff)
|
|
|
|
return NULL;
|
|
|
|
dst = conv->cbuff + used;
|
|
|
|
}
|
2013-05-21 17:31:38 -07:00
|
|
|
used = ct_encode_char(dst, 5, *s);
|
|
|
|
if (used == -1) /* failed to encode, need more buffer space */
|
|
|
|
abort();
|
|
|
|
++s;
|
2010-06-29 17:05:35 -07:00
|
|
|
dst += used;
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
|
|
|
return conv->cbuff;
|
|
|
|
}
|
|
|
|
|
2016-04-11 14:17:29 -07:00
|
|
|
wchar_t *
|
2010-06-29 17:05:35 -07:00
|
|
|
ct_decode_string(const char *s, ct_buffer_t *conv)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
if (!conv->wbuff)
|
|
|
|
ct_conv_buff_resize(conv, 0, CT_BUFSIZ);
|
|
|
|
if (!conv->wbuff)
|
|
|
|
return NULL;
|
|
|
|
|
2016-04-09 13:15:26 -07:00
|
|
|
len = mbstowcs(NULL, s, 0);
|
2013-05-21 17:31:38 -07:00
|
|
|
if (len == (size_t)-1)
|
|
|
|
return NULL;
|
2010-06-29 17:05:35 -07:00
|
|
|
if (len > conv->wsize)
|
|
|
|
ct_conv_buff_resize(conv, 0, len + 1);
|
|
|
|
if (!conv->wbuff)
|
|
|
|
return NULL;
|
2016-04-09 13:15:26 -07:00
|
|
|
|
|
|
|
mbstowcs(conv->wbuff, s, conv->wsize);
|
2010-06-29 17:05:35 -07:00
|
|
|
return conv->wbuff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-11 13:43:33 -07:00
|
|
|
protected wchar_t **
|
2010-06-29 17:05:35 -07:00
|
|
|
ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv)
|
|
|
|
{
|
|
|
|
size_t bufspace;
|
|
|
|
int i;
|
2016-04-11 13:43:33 -07:00
|
|
|
wchar_t *p;
|
|
|
|
wchar_t **wargv;
|
2011-11-17 12:14:24 -08:00
|
|
|
size_t wlen;
|
2010-06-29 17:05:35 -07:00
|
|
|
|
|
|
|
/* 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;
|
2011-11-17 12:14:24 -08:00
|
|
|
ct_conv_buff_resize(conv, 0, bufspace * sizeof(*p));
|
2010-06-29 17:05:35 -07:00
|
|
|
if (!conv->wsize)
|
|
|
|
return NULL;
|
|
|
|
|
2019-01-29 01:47:00 -08:00
|
|
|
wargv = reallocarray(NULL, argc + 1, sizeof(*wargv));
|
2010-06-29 17:05:35 -07: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;
|
2011-11-17 12:14:24 -08:00
|
|
|
wlen = mbstowcs(p, argv[i], bufspace);
|
2010-06-29 17:05:35 -07:00
|
|
|
}
|
2011-11-17 12:14:24 -08:00
|
|
|
if (wlen == (size_t)-1 || wlen == bufspace) {
|
|
|
|
/* Encoding error or not enough room for NUL. */
|
2014-10-16 23:07:50 -07:00
|
|
|
free(wargv);
|
2010-06-29 17:05:35 -07:00
|
|
|
return NULL;
|
|
|
|
} else
|
2011-11-17 12:14:24 -08:00
|
|
|
wlen++; /* include NUL in the count */
|
|
|
|
bufspace -= wlen;
|
|
|
|
p += wlen;
|
2010-06-29 17:05:35 -07:00
|
|
|
}
|
2019-01-29 01:47:00 -08:00
|
|
|
wargv[i] = NULL;
|
2010-06-29 17:05:35 -07:00
|
|
|
|
|
|
|
return wargv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected size_t
|
2016-04-11 13:43:33 -07:00
|
|
|
ct_enc_width(wchar_t c)
|
2010-06-29 17:05:35 -07: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 */
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ssize_t
|
2016-04-11 13:43:33 -07:00
|
|
|
ct_encode_char(char *dst, size_t len, wchar_t c)
|
2010-06-29 17:05:35 -07:00
|
|
|
{
|
|
|
|
ssize_t l = 0;
|
|
|
|
if (len < ct_enc_width(c))
|
|
|
|
return -1;
|
2016-04-09 13:15:26 -07:00
|
|
|
l = wctomb(dst, c);
|
2010-06-29 17:05:35 -07:00
|
|
|
|
|
|
|
if (l < 0) {
|
2016-04-09 13:15:26 -07:00
|
|
|
wctomb(NULL, L'\0');
|
2010-06-29 17:05:35 -07:00
|
|
|
l = 0;
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
2016-03-20 13:16:09 -07:00
|
|
|
|
2016-04-11 13:43:33 -07:00
|
|
|
protected const wchar_t *
|
|
|
|
ct_visual_string(const wchar_t *s)
|
2010-06-29 17:05:35 -07:00
|
|
|
{
|
2016-04-11 13:43:33 -07:00
|
|
|
static wchar_t *buff = NULL;
|
2010-06-29 17:05:35 -07:00
|
|
|
static size_t buffsize = 0;
|
|
|
|
void *p;
|
2016-04-11 13:43:33 -07:00
|
|
|
wchar_t *dst;
|
2010-06-29 17:05:35 -07:00
|
|
|
ssize_t used = 0;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
if (!buff) {
|
|
|
|
buffsize = CT_BUFSIZ;
|
2014-10-16 23:07:50 -07:00
|
|
|
buff = reallocarray(NULL, buffsize, sizeof(*buff));
|
2010-06-29 17:05:35 -07:00
|
|
|
}
|
|
|
|
dst = buff;
|
|
|
|
while (*s) {
|
|
|
|
used = ct_visual_char(dst, buffsize - (dst - buff), *s);
|
|
|
|
if (used == -1) { /* failed to encode, need more buffer space */
|
|
|
|
used = dst - buff;
|
|
|
|
buffsize += CT_BUFSIZ;
|
2014-10-16 23:07:50 -07:00
|
|
|
p = reallocarray(buff, buffsize, sizeof(*buff));
|
2010-06-29 17:05:35 -07:00
|
|
|
if (p == NULL)
|
|
|
|
goto out;
|
|
|
|
buff = p;
|
|
|
|
dst = buff + used;
|
|
|
|
/* don't increment s here - we want to retry it! */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++s;
|
|
|
|
dst += used;
|
|
|
|
}
|
|
|
|
if (dst >= (buff + buffsize)) { /* sigh */
|
|
|
|
buffsize += 1;
|
2014-10-16 23:07:50 -07:00
|
|
|
p = reallocarray(buff, buffsize, sizeof(*buff));
|
2010-06-29 17:05:35 -07:00
|
|
|
if (p == NULL)
|
|
|
|
goto out;
|
|
|
|
buff = p;
|
|
|
|
dst = buff + buffsize - 1;
|
|
|
|
}
|
|
|
|
*dst = 0;
|
|
|
|
return buff;
|
|
|
|
out:
|
2014-10-16 23:07:50 -07:00
|
|
|
free(buff);
|
2010-06-29 17:05:35 -07:00
|
|
|
buffsize = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected int
|
2016-04-11 13:43:33 -07:00
|
|
|
ct_visual_width(wchar_t c)
|
2010-06-29 17:05:35 -07:00
|
|
|
{
|
|
|
|
int t = ct_chr_class(c);
|
2011-04-04 11:48:17 -07:00
|
|
|
int w;
|
2010-06-29 17:05:35 -07:00
|
|
|
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:
|
2011-04-04 11:48:17 -07:00
|
|
|
w = wcwidth(c);
|
|
|
|
return (w == -1 ? 0 : w);
|
2010-06-29 17:05:35 -07:00
|
|
|
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 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected ssize_t
|
2016-04-11 13:43:33 -07:00
|
|
|
ct_visual_char(wchar_t *dst, size_t len, wchar_t c)
|
2010-06-29 17:05:35 -07: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);
|
|
|
|
return (c > 0xffff) ? 8 : 7;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
/* these two should be handled outside this function */
|
|
|
|
default: /* we should never hit the default */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected int
|
2016-04-11 13:43:33 -07:00
|
|
|
ct_chr_class(wchar_t c)
|
2010-06-29 17:05:35 -07:00
|
|
|
{
|
|
|
|
if (c == '\t')
|
|
|
|
return CHTYPE_TAB;
|
|
|
|
else if (c == '\n')
|
|
|
|
return CHTYPE_NL;
|
2016-04-09 13:15:26 -07:00
|
|
|
else if (c < 0x100 && iswcntrl(c))
|
2010-06-29 17:05:35 -07:00
|
|
|
return CHTYPE_ASCIICTL;
|
2016-04-09 13:15:26 -07:00
|
|
|
else if (iswprint(c))
|
2010-06-29 17:05:35 -07:00
|
|
|
return CHTYPE_PRINT;
|
|
|
|
else
|
|
|
|
return CHTYPE_NONPRINT;
|
|
|
|
}
|