diff options
author | Guilherme de Almeida Suckevicz <guito.linux@gmail.com> | 2014-04-16 16:38:49 +0100 |
---|---|---|
committer | Pádraig Brady <P@draigBrady.com> | 2014-04-17 02:16:04 +0100 |
commit | 08783f100f511b010d040d6119c107cc0d5bc5fc (patch) | |
tree | 15a436cea6ba502ccdfe3493450766771209619f /src/ls.c | |
parent | 943f3592ca10caaee7b991078f33a4e0f985a2f8 (diff) | |
download | coreutils-08783f100f511b010d040d6119c107cc0d5bc5fc.tar.xz |
ls: don't output colors with unknown TERM env variable
--colors controls whether to output colors depending on
whether we're connected to a terminal or not, while this
change gives control over which terminals we output colors to.
* NEWS: Mention the change in behavior.
* src/ls.c (known_term_type): A new function to search the static
list from dircolors.h
(parse_ls_colors): Honor the TERM when both LS_COLORS and COLORTERM
are non empty.
* tests/ls/color-term.sh: A new test.
* tests/local.mk: Reference the new test.
Fixes http://bugs.gnu.org/15992
Diffstat (limited to 'src/ls.c')
-rw-r--r-- | src/ls.c | 36 |
1 files changed, 35 insertions, 1 deletions
@@ -108,6 +108,7 @@ #include "xstrtol.h" #include "areadlink.h" #include "mbsalign.h" +#include "dircolors.h" /* Include <sys/capability.h> last to avoid a clash of <sys/types.h> include guards with some premature versions of libcap. @@ -2326,6 +2327,30 @@ enum parse_state PS_FAIL }; + +/* Check if the content of TERM is a valid name in dircolors. */ + +static bool +known_term_type (void) +{ + char const *term = getenv ("TERM"); + if (! term || ! *term) + return false; + + char const *line = G_line; + while (line - G_line < sizeof (G_line)) + { + if (STRNCMP_LIT (line, "TERM ") == 0) + { + if (STREQ (term, line + 5)) + return true; + } + line += strlen (line) + 1; + } + + return false; +} + static void parse_ls_color (void) { @@ -2336,7 +2361,16 @@ parse_ls_color (void) struct color_ext_type *ext; /* Extension we are working on */ if ((p = getenv ("LS_COLORS")) == NULL || *p == '\0') - return; + { + /* LS_COLORS takes precedence, but if that's not set then + honor the COLORTERM and TERM env variables so that + we only go with the internal ANSI color codes if the + former is non empty or the latter is set to a known value. */ + char const *colorterm = getenv ("COLORTERM"); + if (! (colorterm && *colorterm) && ! known_term_type ()) + print_with_color = false; + return; + } ext = NULL; strcpy (label, "??"); |