aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbutl/fdstream.cxx18
-rw-r--r--libbutl/fdstream.hxx3
2 files changed, 20 insertions, 1 deletions
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);