]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pretty-print: fix handling of line continuation in cat_file()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 6 Jan 2025 19:02:53 +0000 (04:02 +0900)
committerLuca Boccassi <bluca@debian.org>
Wed, 22 Jan 2025 20:43:16 +0000 (20:43 +0000)
Fixes #35878.

src/shared/pretty-print.c

index fe82f51a63fcced33980381d8d69db6bf8a7c2b7..e7360352b361acdfc28cc24324f39b94c4c1ff19 100644 (file)
@@ -205,7 +205,7 @@ static int cat_file(const char *filename, bool newline, CatFlags flags) {
                ansi_normal());
         fflush(stdout);
 
-        for (;;) {
+        for (bool continued = false;;) {
                 _cleanup_free_ char *line = NULL;
 
                 r = read_line(f, LONG_LINE_MAX, &line);
@@ -224,11 +224,11 @@ static int cat_file(const char *filename, bool newline, CatFlags flags) {
                 }
 
                 /* empty line */
-                if (FLAGS_SET(flags, CAT_TLDR) && isempty(l))
+                if (FLAGS_SET(flags, CAT_TLDR) && (isempty(l) || streq(l, "\\")))
                         continue;
 
                 /* section */
-                if (FLAGS_SET(flags, CAT_FORMAT_HAS_SECTIONS) && *l == '[') {
+                if (FLAGS_SET(flags, CAT_FORMAT_HAS_SECTIONS) && *l == '[' && !continued) {
                         if (FLAGS_SET(flags, CAT_TLDR))
                                 /* On TLDR, let's not print it yet. */
                                 free_and_replace(section, line);
@@ -248,8 +248,17 @@ static int cat_file(const char *filename, bool newline, CatFlags flags) {
                         free_and_replace(old_section, section);
                 }
 
+                /* Check if the line ends with a backslash. */
+                bool escaped = false;
+                for (const char *e = line; *e != '\0'; e++) {
+                        if (escaped)
+                                escaped = false;
+                        else if (*e == '\\')
+                                escaped = true;
+                }
+
                 /* Highlight the left side (directive) of a Foo=bar assignment */
-                if (FLAGS_SET(flags, CAT_FORMAT_HAS_SECTIONS)) {
+                if (FLAGS_SET(flags, CAT_FORMAT_HAS_SECTIONS) && !continued) {
                         const char *p = strchr(line, '=');
                         if (p) {
                                 _cleanup_free_ char *directive = NULL;
@@ -259,12 +268,14 @@ static int cat_file(const char *filename, bool newline, CatFlags flags) {
                                         return log_oom();
 
                                 printf("%s%s=%s%s\n", ansi_highlight_green(), directive, ansi_normal(), p + 1);
+                                continued = escaped;
                                 continue;
                         }
                 }
 
                 /* Otherwise, print the line as is. */
                 printf("%s\n", line);
+                continued = escaped;
         }
 
         return 0;