From 98bef49e83a69f64b47dd582ccef880cbd8a7979 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 12 Nov 2024 14:15:24 +0200 Subject: Use POSIX fdterm_color() semantics for MSYS terminal (GH issue #447) --- libbutl/fdstream.cxx | 18 +++++++++++++++++- libbutl/fdstream.hxx | 3 +++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/libbutl/fdstream.cxx b/libbutl/fdstream.cxx index df5b531..9008923 100644 --- a/libbutl/fdstream.cxx +++ b/libbutl/fdstream.cxx @@ -1447,6 +1447,8 @@ namespace butl bool fdterm_color (int, bool) { + // Note: Windows version of fdterm_color() uses the same logic for MSYS. + // const char* t (std::getenv ("TERM")); // This test was lifted from GCC (Emacs shell sets TERM=dumb). @@ -1875,6 +1877,8 @@ namespace butl { // @@ Both GCC and Clang simply call GetConsoleMode() for this check. I // wonder why we don't do the same? See also fdterm_color() below. + // Answer: probably because it would fail for the MSYS pipe (see + // below). // We don't need to close it (see fd_to_handle()). // @@ -1978,7 +1982,19 @@ namespace butl // DWORD m; if (!GetConsoleMode (h, &m)) - throw_system_ios_failure (GetLastError ()); + { + DWORD e (GetLastError ()); + + // This might be the MSYS terminal connected via a pipe, in which case + // we use the POSIX semantics (we can assume it's not any pipe because + // calling this function is only valid if fdterm() returned true). + // + if (GetFileType (h) != FILE_TYPE_PIPE) + throw_system_ios_failure (e); + + const char* t (std::getenv ("TERM")); + return t != nullptr && strcmp (t, "dumb") != 0; + } // Some terminals (e.g. Windows Terminal) enable VT processing by default. // diff --git a/libbutl/fdstream.hxx b/libbutl/fdstream.hxx index 9c8f786..11abf53 100644 --- a/libbutl/fdstream.hxx +++ b/libbutl/fdstream.hxx @@ -906,6 +906,9 @@ namespace butl // applicable on some platforms, such as Windows). Throw ios::failure on the // underlying OS error. // + // Note that calling this function is only valid if fdterm() above returned + // true for this file descriptor. + // LIBBUTL_SYMEXPORT bool fdterm_color (int, bool enable); -- cgit v1.1