Handle wide characters and ECMA-48 CSI sequences in the format string when calculating string widths.
This commit is contained in:
+14
-62
@@ -22,77 +22,27 @@
|
||||
|
||||
|
||||
/*
|
||||
* Return the number of display columns needed to show the given string.
|
||||
*
|
||||
* To do this, we convert it to a wide character string, and use the wide
|
||||
* character display width function "wcswidth()" on it.
|
||||
* Return the number of display columns needed to show the given
|
||||
* null-terminated string.
|
||||
*
|
||||
* If NLS is disabled, or the string cannot be converted, this is just the
|
||||
* same as "strlen()".
|
||||
*/
|
||||
static size_t display_width(const char *string)
|
||||
{
|
||||
size_t width;
|
||||
|
||||
#if defined(ENABLE_NLS) && defined(HAVE_WCHAR_H)
|
||||
size_t wide_char_count;
|
||||
size_t wide_string_buffer_size;
|
||||
wchar_t *wide_string;
|
||||
size_t bytes;
|
||||
|
||||
if (NULL == string)
|
||||
return 0;
|
||||
|
||||
/*@-nullpass@ */
|
||||
bytes = strlen(string); /* flawfinder: ignore */
|
||||
/*
|
||||
* splint note: mbstowcs() manual page on Linux explicitly says it
|
||||
* takes NULL.
|
||||
* flawfinder rationale: we have already checked for NULL, and it is
|
||||
* explicitly required of the caller to provide a null-terminated
|
||||
* string.
|
||||
*/
|
||||
wide_char_count = mbstowcs(NULL, string, 0);
|
||||
/*@+nullpass@ */
|
||||
if (wide_char_count == (size_t) -1) {
|
||||
debug("%s: %s: %s", "mbstowcs", string, strerror(errno));
|
||||
return strlen(string); /* flawfinder: ignore */
|
||||
/*
|
||||
* flawfinder rationale: we have already checked for NULL,
|
||||
* and we don't know the size of the originating buffer so
|
||||
* can't use strnlen(); it is up to the caller to provide a
|
||||
* terminated string.
|
||||
*/
|
||||
}
|
||||
|
||||
wide_string_buffer_size = sizeof(*wide_string) * (1 + wide_char_count);
|
||||
wide_string = malloc(wide_string_buffer_size);
|
||||
if (NULL == wide_string) {
|
||||
perror("malloc");
|
||||
return strlen(string); /* flawfinder: ignore */
|
||||
/* flawfinder rationale: see above. */
|
||||
}
|
||||
memset(wide_string, 0, wide_string_buffer_size);
|
||||
|
||||
if (mbstowcs(wide_string, string, 1 + wide_char_count) == (size_t) -1) {
|
||||
debug("%s: %s: %s", "mbstowcs", string, strerror(errno));
|
||||
width = strlen(string); /* flawfinder: ignore */
|
||||
/* flawfinder rationale: see above. */
|
||||
} else if (NULL != wide_string) {
|
||||
/*@-unrecog @ */
|
||||
/* splint seems unable to see the prototype. */
|
||||
width = wcswidth(wide_string, wide_char_count);
|
||||
/*@+unrecog @ */
|
||||
} else {
|
||||
width = 0;
|
||||
}
|
||||
|
||||
free(wide_string);
|
||||
|
||||
#else /* ! defined(ENABLE_NLS) && defined(HAVE_WCHAR_H) */
|
||||
if (NULL == string)
|
||||
return 0;
|
||||
|
||||
width = strlen(string); /* flawfinder: ignore */
|
||||
/* flawfinder rationale: see above. */
|
||||
#endif /* defined(ENABLE_NLS) && defined(HAVE_WCHAR_H) */
|
||||
|
||||
return width;
|
||||
return pv_strwidth(string, bytes);
|
||||
}
|
||||
|
||||
|
||||
@@ -134,9 +84,9 @@ static void display_word_wrap_7bit(const char *string, size_t display_width, siz
|
||||
|
||||
|
||||
/*
|
||||
* Output a string to standard output, word wrapping to "display_width"
|
||||
* display character positions, and left-padding any new lines after the
|
||||
* first one with "left_margin" spaces.
|
||||
* Output a null-terminated string to standard output, word wrapping to
|
||||
* "display_width" display character positions, and left-padding any new
|
||||
* lines after the first one with "left_margin" spaces.
|
||||
*
|
||||
* Wide characters are handled if NLS is enabled, but if they can't be, this
|
||||
* falls back to a version which just counts bytes as characters.
|
||||
@@ -153,7 +103,7 @@ static void display_word_wrap(const char *string, size_t display_width, size_t l
|
||||
return;
|
||||
|
||||
/*@-nullpass@ */
|
||||
/* splint note: see earlier mbstowcs() call. */
|
||||
/* splint note: see mbstowcs() call in pv_strwidth(). */
|
||||
wide_char_count = mbstowcs(NULL, string, 0);
|
||||
/*@+nullpass@ */
|
||||
if (wide_char_count == (size_t) -1) {
|
||||
@@ -181,7 +131,9 @@ static void display_word_wrap(const char *string, size_t display_width, size_t l
|
||||
start_idx = 0;
|
||||
chars_remaining = wide_char_count;
|
||||
|
||||
/*@-unrecog@ *//* splint seems unable to see the prototype for wcswidth(). */
|
||||
while (chars_remaining > 0 && wcswidth(&(wide_string[start_idx]), chars_remaining) > display_width) {
|
||||
/*@+unrecog@ */
|
||||
size_t next_idx;
|
||||
|
||||
end_idx = start_idx + display_width;
|
||||
|
||||
@@ -1530,6 +1530,9 @@ static void pv__format_init(pvstate_t state, /*@null@ */ const char *format_supp
|
||||
display->format[segment].bytes = str_bytes;
|
||||
display->format[segment].width = pv_strwidth(&(display_format[str_start]), str_bytes);
|
||||
|
||||
debug("format[%d]:[%.*s], length=%d, width=%d", segment, str_bytes, display_format + str_start,
|
||||
str_bytes, display->format[segment].width);
|
||||
|
||||
} else {
|
||||
char dummy_buffer[4]; /* flawfinder: ignore - unused. */
|
||||
|
||||
@@ -1777,6 +1780,9 @@ bool pv_format(pvstate_t state, /*@null@ */ const char *format_supplied, pvdispl
|
||||
|
||||
new_display_string_bytes += segment->bytes;
|
||||
new_display_string_width += segment->width;
|
||||
|
||||
debug("segment[%d]: bytes=%d, width=%d: [%.*s]", segment_idx, segment->bytes, segment->width,
|
||||
segment->bytes, display->display_buffer + display_buffer_offset - segment->bytes);
|
||||
}
|
||||
|
||||
debug("%s: %d", "new display string length in bytes", (int) new_display_string_bytes);
|
||||
|
||||
+115
-5
@@ -13,6 +13,12 @@
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#if defined(ENABLE_NLS) && defined(HAVE_WCHAR_H)
|
||||
#include <wchar.h>
|
||||
#if defined(HAVE_WCTYPE_H)
|
||||
#include <wctype.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ -161,9 +167,9 @@ char *pv_strdup(const char *original)
|
||||
void *pv_memrchr(const void *buffer, int match, size_t length)
|
||||
{
|
||||
#ifdef HAVE_MEMRCHR
|
||||
/*@-unrecog @ *//* splint doesn't know of memrchr() */
|
||||
/*@-unrecog@ *//* splint doesn't know of memrchr() */
|
||||
return memrchr(buffer, match, length);
|
||||
/*@+unrecog @ */
|
||||
/*@+unrecog@ */
|
||||
#else
|
||||
unsigned char *ptr;
|
||||
|
||||
@@ -183,14 +189,118 @@ void *pv_memrchr(const void *buffer, int match, size_t length)
|
||||
|
||||
|
||||
/*
|
||||
* Return the displayed width of a string.
|
||||
* Return the number of display columns needed to show the
|
||||
* non-null-terminated string "string" whose length in bytes is "bytes".
|
||||
*
|
||||
* Skips ECMA-48 CSI (ESC [ ...) sequences, but any other control characters
|
||||
* are treated as printable.
|
||||
*
|
||||
* To do this, we convert it to a wide character string, and use the wide
|
||||
* character display width function "wcswidth()" on it.
|
||||
*
|
||||
* If NLS is disabled, or the string cannot be converted, this just returns
|
||||
* the value of "bytes".
|
||||
*
|
||||
* Note that this function uses internal buffers if the string is short
|
||||
* enough, otherwise it has to call malloc() and free(), so it becomes less
|
||||
* efficient with larger strings.
|
||||
*/
|
||||
size_t pv_strwidth(const char *string, size_t bytes)
|
||||
{
|
||||
char *allocated_raw = NULL;
|
||||
static char internal_raw[256]; /* flawfinder: ignore - bounded */
|
||||
char *raw_string = NULL;
|
||||
size_t read_pos, write_pos;
|
||||
size_t raw_bytes, width;
|
||||
#if defined(ENABLE_NLS) && defined(HAVE_WCHAR_H)
|
||||
size_t wide_char_count;
|
||||
size_t wide_string_buffer_size;
|
||||
wchar_t *allocated_wide = NULL;
|
||||
static wchar_t internal_wide[256]; /* flawfinder: ignore - bounded */
|
||||
wchar_t *wide_string = NULL;
|
||||
#endif /* defined(ENABLE_NLS) && defined(HAVE_WCHAR_H) */
|
||||
|
||||
if (NULL == string)
|
||||
return 0;
|
||||
if (0 == bytes)
|
||||
return 0;
|
||||
/* TODO: actually calculate displayed width rather than assuming. */
|
||||
return bytes;
|
||||
|
||||
if (bytes < sizeof(internal_raw) - 1) {
|
||||
raw_string = internal_raw;
|
||||
} else {
|
||||
allocated_raw = calloc(1, 1 + bytes);
|
||||
if (NULL == allocated_raw)
|
||||
return bytes;
|
||||
raw_string = allocated_raw;
|
||||
}
|
||||
|
||||
/* Copy the original string, skipping ECMA-48 CSI sequences. */
|
||||
for (read_pos = 0, write_pos = 0; read_pos < bytes; read_pos++) {
|
||||
if ((string[read_pos] != '\033') || (read_pos >= bytes - 1) || (string[read_pos + 1] != '[')) {
|
||||
raw_string[write_pos++] = string[read_pos];
|
||||
continue;
|
||||
}
|
||||
read_pos += 2;
|
||||
while ((read_pos < bytes - 1)
|
||||
&& ((string[read_pos] >= '0' && string[read_pos] <= '9')
|
||||
|| (';' == string[read_pos])
|
||||
)
|
||||
) {
|
||||
read_pos++;
|
||||
}
|
||||
}
|
||||
raw_string[write_pos] = '\0';
|
||||
raw_bytes = write_pos;
|
||||
|
||||
width = raw_bytes;
|
||||
|
||||
#if defined(ENABLE_NLS) && defined(HAVE_WCHAR_H)
|
||||
/*@-nullpass@ */
|
||||
/*
|
||||
* splint note: mbstowcs() manual page on Linux explicitly says it
|
||||
* takes NULL.
|
||||
*/
|
||||
wide_char_count = mbstowcs(NULL, raw_string, 0);
|
||||
/*@+nullpass@ */
|
||||
if (wide_char_count == (size_t) -1) {
|
||||
debug("%s: %s: %s", "mbstowcs", raw_string, strerror(errno));
|
||||
if (NULL != allocated_raw)
|
||||
free(allocated_raw);
|
||||
return raw_bytes;
|
||||
}
|
||||
|
||||
wide_string_buffer_size = sizeof(*wide_string) * (1 + wide_char_count);
|
||||
|
||||
if (wide_string_buffer_size < sizeof(internal_wide)) {
|
||||
wide_string = internal_wide;
|
||||
} else {
|
||||
allocated_wide = malloc(wide_string_buffer_size);
|
||||
if (NULL == allocated_wide) {
|
||||
perror("malloc");
|
||||
if (NULL != allocated_raw)
|
||||
free(allocated_raw);
|
||||
return raw_bytes;
|
||||
}
|
||||
wide_string = allocated_wide;
|
||||
}
|
||||
memset(wide_string, 0, wide_string_buffer_size);
|
||||
|
||||
if (mbstowcs(wide_string, raw_string, 1 + wide_char_count) == (size_t) -1) {
|
||||
debug("%s: %s: %s", "mbstowcs", raw_string, strerror(errno));
|
||||
} else if (NULL != wide_string) {
|
||||
/*@-unrecog@ *//* splint seems unable to see the prototype. */
|
||||
width = wcswidth(wide_string, wide_char_count);
|
||||
/*@+unrecog@ */
|
||||
} else {
|
||||
width = 0;
|
||||
}
|
||||
|
||||
if (NULL != allocated_wide)
|
||||
free(allocated_wide);
|
||||
#endif /* defined(ENABLE_NLS) && defined(HAVE_WCHAR_H) */
|
||||
|
||||
if (NULL != allocated_raw)
|
||||
free(allocated_raw);
|
||||
|
||||
return width;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user