aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2024-11-12 14:15:24 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2024-11-12 14:23:52 +0200
commit98bef49e83a69f64b47dd582ccef880cbd8a7979 (patch)
tree1f6ff3d64f5cc986790e4edc081fdbf0fa4d4c74
parent34e3bf8d8e01e72d760dc11e600de40a6756f9cd (diff)
Use POSIX fdterm_color() semantics for MSYS terminal (GH issue #447)HEADmaster
-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);