https://gcc.gnu.org/g:5c662a3a5724b75ceb06496d9c64137d59c89d39

commit r15-1355-g5c662a3a5724b75ceb06496d9c64137d59c89d39
Author: Peter Damianov <peter0...@disroot.org>
Date:   Mon Jun 3 10:07:09 2024 -0700

    diagnostics: Don't hardcode auto_enable_urls to false for mingw hosts
    
    Windows terminal and mintty both have support for link escape sequences, 
and so
    auto_enable_urls shouldn't be hardcoded to false. For older versions of the
    windows console, mingw_ansi_fputs's console API translation logic does 
mangle
    these sequences, but there's nothing useful it could do even if this weren't
    the case, so check if the ansi escape sequences are supported at all.
    
    conhost.exe doesn't support link escape sequences, but printing them does 
not
    cause any problems.
    
    gcc/ChangeLog:
            * diagnostic-color.cc (auto_enable_urls): Don't hardcode to return
            false on mingw hosts.
            (auto_enable_urls): Return true if console
            supports ansi escape sequences.
    
    Signed-off-by: Peter Damianov <peter0...@disroot.org>

Diff:
---
 gcc/diagnostic-color.cc | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/gcc/diagnostic-color.cc b/gcc/diagnostic-color.cc
index 261a14b6c5f2..184419cea367 100644
--- a/gcc/diagnostic-color.cc
+++ b/gcc/diagnostic-color.cc
@@ -384,9 +384,6 @@ parse_env_vars_for_urls ()
 static bool
 auto_enable_urls ()
 {
-#ifdef __MINGW32__
-  return false;
-#else
   const char *term, *colorterm;
 
   /* First check the terminal is capable of printing color escapes,
@@ -394,6 +391,21 @@ auto_enable_urls ()
   if (!should_colorize ())
     return false;
 
+#ifdef __MINGW32__
+  HANDLE handle;
+  DWORD mode;
+
+  handle = GetStdHandle (STD_ERROR_HANDLE);
+  if ((handle == INVALID_HANDLE_VALUE) || (handle == NULL))
+    return false;
+
+  /* If ansi escape sequences aren't supported by the console, then URLs will
+     print mangled from mingw_ansi_fputs's console API translation. It wouldn't
+     be useful even if this weren't the case.  */
+  if (GetConsoleMode (handle, &mode) && !(mode & 
ENABLE_VIRTUAL_TERMINAL_PROCESSING))
+    return false;
+#endif
+
   /* xfce4-terminal is known to not implement URLs at this time.
      Recently new installations (0.8) will safely ignore the URL escape
      sequences, but a large number of legacy installations (0.6.3) print
@@ -428,7 +440,6 @@ auto_enable_urls ()
     return false;
 
   return true;
-#endif
 }
 
 /* Determine if URLs should be enabled, based on RULE,

Reply via email to