basic/escape: flagsify xescape_full()

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2021-05-05 12:41:25 +02:00
parent d12ccbc302
commit b19f211698
4 changed files with 18 additions and 12 deletions

View File

@@ -360,13 +360,13 @@ int cunescape_length_with_prefix(const char *s, size_t length, const char *prefi
return t - r;
}
char* xescape_full(const char *s, const char *bad, size_t console_width, bool eight_bits) {
char* xescape_full(const char *s, const char *bad, size_t console_width, XEscapeFlags flags) {
char *ans, *t, *prev, *prev2;
const char *f;
/* Escapes all chars in bad, in addition to \ and all special chars, in \xFF style escaping. May be
* reversed with cunescape(). If eight_bits is true, characters >= 127 are let through unchanged.
* This corresponds to non-ASCII printable characters in pre-unicode encodings.
* reversed with cunescape(). If XESCAPE_8_BIT is specified, characters >= 127 are let through
* unchanged. This corresponds to non-ASCII printable characters in pre-unicode encodings.
*
* If console_width is reached, output is truncated and "..." is appended. */
@@ -388,7 +388,8 @@ char* xescape_full(const char *s, const char *bad, size_t console_width, bool ei
return ans;
}
if ((unsigned char) *f < ' ' || (!eight_bits && (unsigned char) *f >= 127) ||
if ((unsigned char) *f < ' ' ||
(!FLAGS_SET(flags, XESCAPE_8_BIT) && (unsigned char) *f >= 127) ||
*f == '\\' || strchr(bad, *f)) {
if ((size_t) (t - ans) + 4 > console_width)
break;
@@ -427,9 +428,9 @@ char* xescape_full(const char *s, const char *bad, size_t console_width, bool ei
return ans;
}
char* escape_non_printable_full(const char *str, size_t console_width, bool eight_bit) {
if (eight_bit)
return xescape_full(str, "", console_width, true);
char* escape_non_printable_full(const char *str, size_t console_width, XEscapeFlags flags) {
if (FLAGS_SET(flags, XESCAPE_8_BIT))
return xescape_full(str, "", console_width, flags);
else
return utf8_escape_non_printable_full(str, console_width);
}

View File

@@ -54,12 +54,16 @@ static inline int cunescape(const char *s, UnescapeFlags flags, char **ret) {
}
int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit, bool accept_nul);
char* xescape_full(const char *s, const char *bad, size_t console_width, bool eight_bits);
typedef enum XEscapeFlags {
XESCAPE_8_BIT = 1 << 0,
} XEscapeFlags;
char* xescape_full(const char *s, const char *bad, size_t console_width, XEscapeFlags flags);
static inline char* xescape(const char *s, const char *bad) {
return xescape_full(s, bad, SIZE_MAX, false);
return xescape_full(s, bad, SIZE_MAX, 0);
}
char* octescape(const char *s, size_t len);
char* escape_non_printable_full(const char *str, size_t console_width, bool eight_bit);
char* escape_non_printable_full(const char *str, size_t console_width, XEscapeFlags flags);
char* shell_escape(const char *s, const char *bad);
char* shell_maybe_quote(const char *s, ShellEscapeFlags flags);

View File

@@ -175,7 +175,7 @@ int get_process_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags
bool eight_bit = (flags & PROCESS_CMDLINE_USE_LOCALE) && !is_locale_utf8();
ans = escape_non_printable_full(t, max_columns, eight_bit);
ans = escape_non_printable_full(t, max_columns, eight_bit * XESCAPE_8_BIT);
if (!ans)
return -ENOMEM;

View File

@@ -24,11 +24,12 @@ static void test_xescape_full(bool eight_bits) {
"a\\x62c\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\\x7f\\x9c\\xcb" :
"a\\x62c\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\177\234\313";
const unsigned full_fit = !eight_bits ? 55 : 46;
XEscapeFlags flags = eight_bits * XESCAPE_8_BIT;
for (unsigned i = 0; i < 60; i++) {
_cleanup_free_ char *t;
assert_se(t = xescape_full("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", "b", i, eight_bits));
assert_se(t = xescape_full("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", "b", i, flags));
log_info("%02d: %s", i, t);