]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/escape: flagsify xescape_full()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 5 May 2021 10:41:25 +0000 (12:41 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 5 May 2021 11:59:23 +0000 (13:59 +0200)
src/basic/escape.c
src/basic/escape.h
src/basic/process-util.c
src/test/test-escape.c

index 45899b6b7cc56d4c0c2601c8f25f0cebcc7ca302..f579f15d87e20aa0d3d40558612ace3c4a285926 100644 (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);
 }
index 3cb107ae902cad47e29bb7df75f120847f0a6109..945e7dc82c81b399290aeb6d3c75419c8d0b6843 100644 (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);
index 902265ac21e4518b389eb64702957eaed4ed510c..d7afc4fe5aa5b0bcf5ce576717d3d11cda66aad8 100644 (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;
 
index eca66ea9db30d956221f45cc0c256d846648cb94..63f9306fb4dbcbfad2355e9ab7a4a2a126d90e4f 100644 (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);