From: Yu Watanabe Date: Mon, 6 Jan 2025 19:02:53 +0000 (+0900) Subject: pretty-print: fix handling of line continuation in cat_file() X-Git-Tag: v258-rc1~1519^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e27fb39e07afeb130f4988782a1d8b45ae29f57c;p=thirdparty%2Fsystemd.git pretty-print: fix handling of line continuation in cat_file() Fixes #35878. --- 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;