From f5268e2749d5649a3364550ef54bf31d9aa9c4e1 Mon Sep 17 00:00:00 2001 From: Moritz Orbach Date: Tue, 16 Feb 2010 22:48:00 +0000 Subject: [PATCH] ls: fix a regression by honoring NORMAL attributes again Output the NORMAL attribute before non file name text. This attribute will continue into file names that would not otherwise be colored unless FILE is also set. The regression was introduced with commit 483297d5, 28-02-2009, "ls --color no longer outputs unnecessary escape sequences". * src/ls.c (set_normal_color): A new function to output the NORMAL attribute sequence if it's enabled. (print_current_files): Output NORMAL before printing long format info. (print_file_name_and_frills): Output NORMAL before printing file name. (print_color_indicator): Reset the attributes before a file name with attributes so that NORMAL attributes will not combine with them. (print_name_with_quoting): Ensure attributes are reset after printing the file name if NORMAL attributes were output. * tests/ls/color-norm: A new test for NORMAL and FILE combinations. * tests/Makefile.am: Reference the new test. * NEWS: Mention the fix. Reported in https://savannah.gnu.org/bugs/?26512 --- NEWS | 5 +++ src/ls.c | 21 +++++++++++- tests/Makefile.am | 1 + tests/ls/color-norm | 80 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100755 tests/ls/color-norm diff --git a/NEWS b/NEWS index 323845d99..595a8cf79 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,11 @@ GNU coreutils NEWS -*- outline -*- * Noteworthy changes in release ?.? (????-??-??) [?] +** Bug fixes + + ls --color once again honors the 'NORMAL' dircolors directive. + [bug introduced in coreutils-6.11] + ** New features join now accepts the --header option, to treat the first line of each diff --git a/src/ls.c b/src/ls.c index 680679a9a..3025a76ef 100644 --- a/src/ls.c +++ b/src/ls.c @@ -1149,6 +1149,17 @@ restore_default_color (void) put_indicator (&color_indicator[C_RIGHT]); } +static void +set_normal_color (void) +{ + if (print_with_color && is_colored (C_NORM)) + { + put_indicator (&color_indicator[C_LEFT]); + put_indicator (&color_indicator[C_NORM]); + put_indicator (&color_indicator[C_RIGHT]); + } +} + /* An ordinary signal was received; arrange for the program to exit. */ static void @@ -3444,6 +3455,7 @@ print_current_files (void) case long_format: for (i = 0; i < cwd_n_used; i++) { + set_normal_color (); print_long_format (sorted_file[i]); DIRED_PUTCHAR ('\n'); } @@ -4004,7 +4016,9 @@ print_name_with_quoting (const struct fileinfo *f, const char* name = symlink_target ? f->linkname : f->name; bool used_color_this_time - = (print_with_color && print_color_indicator (f, symlink_target)); + = (print_with_color + && (print_color_indicator (f, symlink_target) + || is_colored (C_NORM))); if (stack) PUSH_CURRENT_DIRED_POS (stack); @@ -4048,6 +4062,8 @@ print_file_name_and_frills (const struct fileinfo *f, size_t start_col) { char buf[MAX (LONGEST_HUMAN_READABLE + 1, INT_BUFSIZE_BOUND (uintmax_t))]; + set_normal_color (); + if (print_inode) printf ("%*s ", format == with_commas ? 0 : inode_number_width, format_inode (buf, sizeof buf, f)); @@ -4218,6 +4234,9 @@ print_color_indicator (const struct fileinfo *f, bool symlink_target) = ext ? &(ext->seq) : &color_indicator[type]; if (s->string != NULL) { + /* Need to reset so not dealing with attribute combinations */ + if (is_colored (C_NORM)) + restore_default_color (); put_indicator (&color_indicator[C_LEFT]); put_indicator (s); put_indicator (&color_indicator[C_RIGHT]); diff --git a/tests/Makefile.am b/tests/Makefile.am index 85503cc18..68b3f71b7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -370,6 +370,7 @@ TESTS = \ ls/abmon-align \ ls/color-clear-to-eol \ ls/color-dtype-dir \ + ls/color-norm \ ls/dangle \ ls/dired \ ls/file-type \ diff --git a/tests/ls/color-norm b/tests/ls/color-norm new file mode 100755 index 000000000..16ffa22a1 --- /dev/null +++ b/tests/ls/color-norm @@ -0,0 +1,80 @@ +#!/bin/sh +# Ensure "ls --color" properly colors "normal" text and files. +# I.E. that it uses NORMAL to style non file name output and +# file names with no associated color (unless FILE is also set). + +# Copyright (C) 2010 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +if test "$VERBOSE" = yes; then + set -x + ls --version +fi + +. $srcdir/test-lib.sh + +# Don't let a different umask perturb the results. +umask 22 + +touch exe || framework_failure +chmod u+x exe || framework_failure +touch nocolor || framework_failure + +# Non coloured files inherit NORMAL attributes +LS_COLORS=no=7 ls -s1U --color exe nocolor >> out || fail=1 +LS_COLORS=no=7 ls -sxU --color exe nocolor >> out || fail=1 +LS_COLORS=no=7 ls -s1U --color nocolor exe >> out || fail=1 +LS_COLORS=no=7 ls -sxU --color nocolor exe >> out || fail=1 + +# NORMAL does not override FILE though +LS_COLORS=no=7:fi=1 ls -s1U --color nocolor exe >> out || fail=1 + +# Support uncolored ordinary files that do _not_ inherit from NORMAL. +# Note there is a redundant RESET output before a non colored +# file in this case which may be removed in future. +LS_COLORS=no=7:fi= ls -s1U --color nocolor exe >> out || fail=1 +LS_COLORS=no=7:fi=0 ls -s1U --color nocolor exe >> out || fail=1 + +# A caveat worth noting is that commas (-m), indicator chars (-F) +# and the "total" line, do not currently use NORMAL attributes +LS_COLORS="no=7" ls -mFU --color nocolor exe >> out || fail=1 + +# Ensure no coloring is done unless enabled +LS_COLORS="no=7" ls -s1U nocolor exe >> out || fail=1 + +cat -A out > out.display || framework_failure +mv out.display out || framework_failure + +cat <<\EOF > exp || framework_failure +^[[0m^[[7m0 ^[[m^[[01;32mexe^[[0m$ +^[[7m0 nocolor^[[0m$ +^[[0m^[[7m0 ^[[m^[[01;32mexe^[[0m ^[[7m0 nocolor^[[0m$ +^[[0m^[[7m0 nocolor^[[0m$ +^[[7m0 ^[[m^[[01;32mexe^[[0m$ +^[[0m^[[7m0 nocolor^[[0m ^[[7m0 ^[[m^[[01;32mexe^[[0m$ +^[[0m^[[7m0 ^[[m^[[1mnocolor^[[0m$ +^[[7m0 ^[[m^[[01;32mexe^[[0m$ +^[[0m^[[7m0 ^[[m^[[mnocolor^[[0m$ +^[[7m0 ^[[m^[[01;32mexe^[[0m$ +^[[0m^[[7m0 ^[[m^[[0mnocolor^[[0m$ +^[[7m0 ^[[m^[[01;32mexe^[[0m$ +^[[0m^[[7mnocolor^[[0m, ^[[7m^[[m^[[01;32mexe^[[0m*$ +0 nocolor$ +0 exe$ +EOF + +compare out exp || fail=1 + +Exit $fail