From e27fb39e07afeb130f4988782a1d8b45ae29f57c Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 7 Jan 2025 04:02:53 +0900 Subject: [PATCH] pretty-print: fix handling of line continuation in cat_file() Fixes #35878. --- src/shared/pretty-print.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/shared/pretty-print.c b/src/shared/pretty-print.c index fe82f51a63f..e7360352b36 100644 --- a/src/shared/pretty-print.c +++ b/src/shared/pretty-print.c @@ -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; -- 2.47.3