lineedit: fixes for CONFIG_UNICODE_USING_LOCALE=y
function old new delta load_string 45 91 +46 save_string 40 82 +42 reinit_unicode 34 61 +27 BB_PUTCHAR 97 120 +23 init_unicode 17 37 +20 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 5/0 up/down: 158/0) Total: 158 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
19311bfa7b
commit
353680aa46
107
libbb/lineedit.c
107
libbb/lineedit.c
@ -204,65 +204,82 @@ static void deinit_S(void)
|
|||||||
#if ENABLE_UNICODE_SUPPORT
|
#if ENABLE_UNICODE_SUPPORT
|
||||||
static size_t load_string(const char *src, int maxsize)
|
static size_t load_string(const char *src, int maxsize)
|
||||||
{
|
{
|
||||||
ssize_t len = mbstowcs(command_ps, src, maxsize - 1);
|
if (unicode_status == UNICODE_ON) {
|
||||||
if (len < 0)
|
ssize_t len = mbstowcs(command_ps, src, maxsize - 1);
|
||||||
len = 0;
|
if (len < 0)
|
||||||
command_ps[len] = BB_NUL;
|
len = 0;
|
||||||
return len;
|
command_ps[len] = BB_NUL;
|
||||||
|
return len;
|
||||||
|
} else {
|
||||||
|
unsigned i = 0;
|
||||||
|
while ((command_ps[i] = src[i]) != 0)
|
||||||
|
i++;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
static unsigned save_string(char *dst, unsigned maxsize)
|
static unsigned save_string(char *dst, unsigned maxsize)
|
||||||
{
|
{
|
||||||
|
if (unicode_status == UNICODE_ON) {
|
||||||
# if !ENABLE_UNICODE_PRESERVE_BROKEN
|
# if !ENABLE_UNICODE_PRESERVE_BROKEN
|
||||||
ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
|
ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
|
||||||
if (len < 0)
|
if (len < 0)
|
||||||
len = 0;
|
len = 0;
|
||||||
dst[len] = '\0';
|
dst[len] = '\0';
|
||||||
return len;
|
return len;
|
||||||
# else
|
# else
|
||||||
unsigned dstpos = 0;
|
unsigned dstpos = 0;
|
||||||
unsigned srcpos = 0;
|
unsigned srcpos = 0;
|
||||||
|
|
||||||
maxsize--;
|
maxsize--;
|
||||||
while (dstpos < maxsize) {
|
while (dstpos < maxsize) {
|
||||||
wchar_t wc;
|
wchar_t wc;
|
||||||
int n = srcpos;
|
int n = srcpos;
|
||||||
|
|
||||||
/* Convert up to 1st invalid byte (or up to end) */
|
/* Convert up to 1st invalid byte (or up to end) */
|
||||||
while ((wc = command_ps[srcpos]) != BB_NUL
|
while ((wc = command_ps[srcpos]) != BB_NUL
|
||||||
&& !unicode_is_raw_byte(wc)
|
&& !unicode_is_raw_byte(wc)
|
||||||
) {
|
) {
|
||||||
|
srcpos++;
|
||||||
|
}
|
||||||
|
command_ps[srcpos] = BB_NUL;
|
||||||
|
n = wcstombs(dst + dstpos, command_ps + n, maxsize - dstpos);
|
||||||
|
if (n < 0) /* should not happen */
|
||||||
|
break;
|
||||||
|
dstpos += n;
|
||||||
|
if (wc == BB_NUL) /* usually is */
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* We do have invalid byte here! */
|
||||||
|
command_ps[srcpos] = wc; /* restore it */
|
||||||
srcpos++;
|
srcpos++;
|
||||||
|
if (dstpos == maxsize)
|
||||||
|
break;
|
||||||
|
dst[dstpos++] = (char) wc;
|
||||||
}
|
}
|
||||||
command_ps[srcpos] = BB_NUL;
|
dst[dstpos] = '\0';
|
||||||
n = wcstombs(dst + dstpos, command_ps + n, maxsize - dstpos);
|
return dstpos;
|
||||||
if (n < 0) /* should not happen */
|
|
||||||
break;
|
|
||||||
dstpos += n;
|
|
||||||
if (wc == BB_NUL) /* usually is */
|
|
||||||
break;
|
|
||||||
|
|
||||||
/* We do have invalid byte here! */
|
|
||||||
command_ps[srcpos] = wc; /* restore it */
|
|
||||||
srcpos++;
|
|
||||||
if (dstpos == maxsize)
|
|
||||||
break;
|
|
||||||
dst[dstpos++] = (char) wc;
|
|
||||||
}
|
|
||||||
dst[dstpos] = '\0';
|
|
||||||
return dstpos;
|
|
||||||
# endif
|
# endif
|
||||||
|
} else {
|
||||||
|
unsigned i = 0;
|
||||||
|
while ((dst[i] = command_ps[i]) != 0)
|
||||||
|
i++;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/* I thought just fputwc(c, stdout) would work. But no... */
|
/* I thought just fputwc(c, stdout) would work. But no... */
|
||||||
static void BB_PUTCHAR(wchar_t c)
|
static void BB_PUTCHAR(wchar_t c)
|
||||||
{
|
{
|
||||||
char buf[MB_CUR_MAX + 1];
|
if (unicode_status == UNICODE_ON) {
|
||||||
mbstate_t mbst = { 0 };
|
char buf[MB_CUR_MAX + 1];
|
||||||
ssize_t len;
|
mbstate_t mbst = { 0 };
|
||||||
|
ssize_t len = wcrtomb(buf, c, &mbst);
|
||||||
len = wcrtomb(buf, c, &mbst);
|
if (len > 0) {
|
||||||
if (len > 0) {
|
buf[len] = '\0';
|
||||||
buf[len] = '\0';
|
fputs(buf, stdout);
|
||||||
fputs(buf, stdout);
|
}
|
||||||
|
} else {
|
||||||
|
/* In this case, c is always one byte */
|
||||||
|
putchar(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
|
# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
|
||||||
|
@ -23,12 +23,13 @@ uint8_t unicode_status;
|
|||||||
|
|
||||||
/* Unicode support using libc locale support. */
|
/* Unicode support using libc locale support. */
|
||||||
|
|
||||||
void FAST_FUNC reinit_unicode(const char *LANG UNUSED_PARAM)
|
void FAST_FUNC reinit_unicode(const char *LANG)
|
||||||
{
|
{
|
||||||
static const char unicode_0x394[] = { 0xce, 0x94, 0 };
|
static const char unicode_0x394[] = { 0xce, 0x94, 0 };
|
||||||
size_t width;
|
size_t width;
|
||||||
|
|
||||||
//TODO: call setlocale(LC_ALL, LANG) here?
|
//TODO: avoid repeated calls by caching last string?
|
||||||
|
setlocale(LC_ALL, (LANG && LANG[0]) ? LANG : "C");
|
||||||
|
|
||||||
/* In unicode, this is a one character string */
|
/* In unicode, this is a one character string */
|
||||||
// can use unicode_strlen(string) too, but otherwise unicode_strlen() is unused
|
// can use unicode_strlen(string) too, but otherwise unicode_strlen() is unused
|
||||||
@ -39,7 +40,7 @@ void FAST_FUNC reinit_unicode(const char *LANG UNUSED_PARAM)
|
|||||||
void FAST_FUNC init_unicode(void)
|
void FAST_FUNC init_unicode(void)
|
||||||
{
|
{
|
||||||
if (unicode_status == UNICODE_UNKNOWN)
|
if (unicode_status == UNICODE_UNKNOWN)
|
||||||
reinit_unicode(NULL /*getenv("LANG")*/);
|
reinit_unicode(getenv("LANG"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
Loading…
Reference in New Issue
Block a user