diff options
Diffstat (limited to 'src/main/cpp')
-rw-r--r-- | src/main/cpp/blaze_util_platform.h | 9 | ||||
-rw-r--r-- | src/main/cpp/blaze_util_posix.cc | 22 | ||||
-rw-r--r-- | src/main/cpp/blaze_util_windows.cc | 42 | ||||
-rw-r--r-- | src/main/cpp/option_processor.cc | 6 |
4 files changed, 38 insertions, 41 deletions
diff --git a/src/main/cpp/blaze_util_platform.h b/src/main/cpp/blaze_util_platform.h index 34ea18e695..05eb772062 100644 --- a/src/main/cpp/blaze_util_platform.h +++ b/src/main/cpp/blaze_util_platform.h @@ -203,13 +203,12 @@ std::string GetUserName(); // Returns true iff the current terminal is running inside an Emacs. bool IsEmacsTerminal(); -// Returns true if stderr is connected to a terminal that can support color -// and cursor movement. -bool IsStderrStandardTerminal(); +// Returns true iff the current terminal can support color and cursor movement. +bool IsStandardTerminal(); -// Returns the number of columns of the terminal to which stderr is +// Returns the number of columns of the terminal to which stdout is // connected, or 80 if there is no such terminal. -int GetStderrTerminalColumns(); +int GetTerminalColumns(); // Gets the system-wide explicit limit for the given resource. // diff --git a/src/main/cpp/blaze_util_posix.cc b/src/main/cpp/blaze_util_posix.cc index b093caef6d..af0ce17385 100644 --- a/src/main/cpp/blaze_util_posix.cc +++ b/src/main/cpp/blaze_util_posix.cc @@ -654,27 +654,25 @@ bool IsEmacsTerminal() { return emacs == "t" || !inside_emacs.empty(); } -// Returns true if stderr is connected to a terminal, and it can support color -// and cursor movement (this is computed heuristically based on the values of -// environment variables). The only file handle into which Blaze outputs -// control characters is stderr, so we only care for the stderr descriptor type. -bool IsStderrStandardTerminal() { +// Returns true iff both stdout and stderr are connected to a +// terminal, and it can support color and cursor movement +// (this is computed heuristically based on the values of +// environment variables). +bool IsStandardTerminal() { string term = GetEnv("TERM"); if (term.empty() || term == "dumb" || term == "emacs" || term == "xterm-mono" || term == "symbolics" || term == "9term" || IsEmacsTerminal()) { return false; } - return isatty(STDERR_FILENO); + return isatty(STDOUT_FILENO) && isatty(STDERR_FILENO); } -// Returns the number of columns of the terminal to which stderr is connected, -// or $COLUMNS (default 80) if there is no such terminal. The only file handle -// into which Blaze outputs formatted messages is stderr, so we only care for -// width of a terminal connected to the stderr descriptor. -int GetStderrTerminalColumns() { +// Returns the number of columns of the terminal to which stdout is +// connected, or $COLUMNS (default 80) if there is no such terminal. +int GetTerminalColumns() { struct winsize ws; - if (ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) != -1) { + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) { return ws.ws_col; } string columns_env = GetEnv("COLUMNS"); diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc index 9770e13e03..a5bcf45450 100644 --- a/src/main/cpp/blaze_util_windows.cc +++ b/src/main/cpp/blaze_util_windows.cc @@ -1372,20 +1372,22 @@ bool IsEmacsTerminal() { return emacs == "t" || !inside_emacs.empty(); } -// Returns true if stderr is connected to a terminal, and it can support color -// and cursor movement (this is computed heuristically based on the values of -// environment variables). Blaze only outputs control characters into stderr, -// we only care for the stderr descriptor type. -bool IsStderrStandardTerminal() { +// Returns true iff both stdout and stderr are connected to a +// terminal, and it can support color and cursor movement +// (this is computed heuristically based on the values of +// environment variables). +bool IsStandardTerminal() { #ifdef COMPILER_MSVC - DWORD mode = 0; - HANDLE handle = ::GetStdHandle(STD_ERROR_HANDLE); - // handle may be invalid when stderr is redirected - if (handle == INVALID_HANDLE_VALUE || !::GetConsoleMode(handle, &mode) || - !(mode & ENABLE_PROCESSED_OUTPUT) || - !(mode & ENABLE_WRAP_AT_EOL_OUTPUT) || - !(mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)) { - return false; + for (DWORD i : {STD_OUTPUT_HANDLE, STD_ERROR_HANDLE}) { + DWORD mode = 0; + HANDLE handle = ::GetStdHandle(i); + // handle may be invalid when std{out,err} is redirected + if (handle == INVALID_HANDLE_VALUE || !::GetConsoleMode(handle, &mode) || + !(mode & ENABLE_PROCESSED_OUTPUT) || + !(mode & ENABLE_WRAP_AT_EOL_OUTPUT) || + !(mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)) { + return false; + } } return true; #else // not COMPILER_MSVC @@ -1395,18 +1397,16 @@ bool IsStderrStandardTerminal() { IsEmacsTerminal()) { return false; } - return isatty(STDERR_FILENO); + return isatty(STDOUT_FILENO) && isatty(STDERR_FILENO); #endif // COMPILER_MSVC } -// Returns the number of columns of the terminal to which stderr is connected, -// or $COLUMNS (default 80) if there is no such terminal. Blaze only outputs -// formatted messages to stderr, so we only care for width of a terminal -// connected to the stderr descriptor. -int GetStderrTerminalColumns() { +// Returns the number of columns of the terminal to which stdout is +// connected, or $COLUMNS (default 80) if there is no such terminal. +int GetTerminalColumns() { #ifndef COMPILER_MSVC struct winsize ws; - if (ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) != -1) { + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) { return ws.ws_col; } #endif // not COMPILER_MSVC @@ -1420,7 +1420,7 @@ int GetStderrTerminalColumns() { } } - HANDLE stdout_handle = ::GetStdHandle(STD_ERROR_HANDLE); + HANDLE stdout_handle = ::GetStdHandle(STD_OUTPUT_HANDLE); if (stdout_handle != INVALID_HANDLE_VALUE) { // stdout_handle may be invalid when stdout is redirected. CONSOLE_SCREEN_BUFFER_INFO screen_info; diff --git a/src/main/cpp/option_processor.cc b/src/main/cpp/option_processor.cc index fc6d8b11d6..868f09f0fd 100644 --- a/src/main/cpp/option_processor.cc +++ b/src/main/cpp/option_processor.cc @@ -483,10 +483,10 @@ std::vector<std::string> OptionProcessor::GetBlazercAndEnvCommandArgs( // Provide terminal options as coming from the least important rc file. std::vector<std::string> result = { "--rc_source=client", - "--default_override=0:common=--is_stderr_atty=" + - ToString(IsStderrStandardTerminal()), + "--default_override=0:common=--isatty=" + + ToString(IsStandardTerminal()), "--default_override=0:common=--terminal_columns=" + - ToString(GetStderrTerminalColumns())}; + ToString(GetTerminalColumns())}; // Push the options mapping .blazerc numbers to filenames. for (const RcFile* blazerc : blazercs) { |