From 3f99160691ffee1356ff3757d91a389564270578 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Tue, 3 Jan 2012 08:52:28 +0100 Subject: colorize: use isatty module * bootstrap.conf: Add isatty module. * gnulib: Update to latest. * lib/colorize.h: Remove argument from should_colorize. * lib/ms/colorize.h: Likewise. * lib/colorize-impl.c: Factor isatty call out of here... * lib/ms/colorize-impl.c: ... and here... * src/main.c: ... into here. --- bootstrap.conf | 1 + gnulib | 2 +- lib/colorize-impl.c | 11 +++-------- lib/colorize.h | 2 +- lib/ms/colorize-impl.c | 22 +++++++--------------- lib/ms/colorize.h | 2 +- src/main.c | 2 +- 7 files changed, 15 insertions(+), 27 deletions(-) diff --git a/bootstrap.conf b/bootstrap.conf index b02c26b5..4aee59a0 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -41,6 +41,7 @@ gnupload hard-locale ignore-value intprops +isatty isblank isdir iswctype diff --git a/gnulib b/gnulib index 55c6e4b6..464f9e7b 160000 --- a/gnulib +++ b/gnulib @@ -1 +1 @@ -Subproject commit 55c6e4b6fe65fdfd5154228209a5c9828bb8ec9d +Subproject commit 464f9e7b25ea50440cd453931e18329844023fef diff --git a/lib/colorize-impl.c b/lib/colorize-impl.c index b7c0a3ff..e9d5c730 100644 --- a/lib/colorize-impl.c +++ b/lib/colorize-impl.c @@ -27,13 +27,8 @@ /* Return non-zero if we should highlight matches in output to file descriptor FD. */ int -should_colorize (int fd) +should_colorize (void) { - if (! isatty (fd)) - return 0; - else - { - char const *t = getenv ("TERM"); - return t && strcmp (t, "dumb") != 0; - } + char const *t = getenv ("TERM"); + return t && strcmp (t, "dumb") != 0; } diff --git a/lib/colorize.h b/lib/colorize.h index 104f7a03..93f3b10f 100644 --- a/lib/colorize.h +++ b/lib/colorize.h @@ -19,7 +19,7 @@ #include static inline void init_colorize (void) { } -extern int should_colorize (int); +extern int should_colorize (void); /* Start a colorized text attribute on stdout using the SGR_START format; the attribute is specified by SGR_SEQ. */ diff --git a/lib/ms/colorize-impl.c b/lib/ms/colorize-impl.c index d9d9b959..7341fe20 100644 --- a/lib/ms/colorize-impl.c +++ b/lib/ms/colorize-impl.c @@ -49,22 +49,14 @@ init_colorize (void) /* Return non-zero if we should highlight matches in output. */ int -should_colorize (int fd) +should_colorize (void) { - if (! isatty (fd)) - return 0; - /* Windows isatty returns non-zero for the null device too. */ - else if (lseek (fd, SEEK_CUR, 0) != -1) - return 0; - else - { - /* $TERM is not normally defined on DOS/Windows, so don't require - it for highlighting. But some programs, like Emacs, do define - it when running Grep as a subprocess, so make sure they don't - set TERM=dumb. */ - char const *t = getenv ("TERM"); - return ! (t && strcmp (t, "dumb") == 0); - } + /* $TERM is not normally defined on DOS/Windows, so don't require + it for highlighting. But some programs, like Emacs, do define + it when running Grep as a subprocess, so make sure they don't + set TERM=dumb. */ + char const *t = getenv ("TERM"); + return ! (t && strcmp (t, "dumb") == 0); } /* Convert a color spec, a semi-colon separated list of the form diff --git a/lib/ms/colorize.h b/lib/ms/colorize.h index c49657d6..bb94e763 100644 --- a/lib/ms/colorize.h +++ b/lib/ms/colorize.h @@ -1,4 +1,4 @@ extern void colorize_init (void); -extern int should_colorize (int); +extern int should_colorize (void); extern void print_start_colorize (char const *, char const *); extern void print_end_colorize (char const *); diff --git a/src/main.c b/src/main.c index 8ad84f45..a768d367 100644 --- a/src/main.c +++ b/src/main.c @@ -2132,7 +2132,7 @@ main (int argc, char **argv) } if (color_option == 2) - color_option = should_colorize (STDOUT_FILENO); + color_option = isatty (STDOUT_FILENO) && should_colorize (); init_colorize (); /* POSIX.2 says that -q overrides -l, which in turn overrides the -- cgit v1.2.1