Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: tom@tromey.com
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 00/16] Add styling to the gdb CLI and TUI
Date: Fri, 08 Mar 2019 14:55:00 -0000	[thread overview]
Message-ID: <83d0n1a01f.fsf@gnu.org> (raw)
In-Reply-To: <834l8he3gn.fsf@gnu.org> (message from Eli Zaretskii on Tue, 05	Mar 2019 17:38:32 +0200)

> Date: Tue, 05 Mar 2019 17:38:32 +0200
> From: Eli Zaretskii <eliz@gnu.org>
> CC: gdb-patches@sourceware.org
> 
> > From: Tom Tromey <tom@tromey.com>
> > Cc: Tom Tromey <tom@tromey.com>,  gdb-patches@sourceware.org
> > Date: Mon, 04 Mar 2019 09:16:25 -0700
> > 
> > (gdb) printf "\e[38;2;255;100;0mTRUECOLOR\e[0m\n"
> 
> Thanks, I used this example (and its variations) to extend the support
> to any kind of color.

Here's the patch I propose to enable styling on native MS-Windows
console.

OK to commit, master and the release branch?

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index de5e74a..bb10c7d 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,21 @@
+2019-03-08  Eli Zaretskii  <eliz@gnu.org>
+
+	Support styling on native MS-Windows console
+
+	PR/24315
+	* utils.c (can_emit_style_escape) [_WIN32]: Don't disable styling
+	on MS-Windows if $TERM is not defined.
+
+	* cli/cli-style.c: Set cli_styling to 1 in the MinGW build.
+
+	* posix-hdep.c (gdb_console_fputs):
+	* mingw-hdep.c (rgb_to_16colors, gdb_console_fputs): New
+	functions.
+	* ui-file.h (gdb_console_fputs): Add prototype.
+
+	* ui-file.c (stdio_file::puts): Call gdb_console_fputs, and fall
+	back to fputs only if the former returns zero.
+
 2019-03-07  Tom Tromey  <tom@tromey.com>
 
 	* symmisc.c (print_symbol_bcache_statistics): Update.
diff --git a/gdb/cli/cli-style.c b/gdb/cli/cli-style.c
index 234cc41..ec385da 100644
--- a/gdb/cli/cli-style.c
+++ b/gdb/cli/cli-style.c
@@ -24,7 +24,7 @@
 
 /* True if styling is enabled.  */
 
-#if defined(_WIN32) || defined (__CYGWIN__)
+#if defined (__MSDOS__) || defined (__CYGWIN__)
 int cli_styling = 0;
 #else
 int cli_styling = 1;
diff --git a/gdb/mingw-hdep.c b/gdb/mingw-hdep.c
index 1a4b93b..8ed4b44 100644
--- a/gdb/mingw-hdep.c
+++ b/gdb/mingw-hdep.c
@@ -177,3 +177,195 @@ gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
 
   return num_ready;
 }
+
+/* Map COLOR's RGB triplet, with 8 bits per component, into 16 Windows
+   console colors, where each component has just 1 bit, plus a single
+   intensity bit which affects all 3 components.  */
+static int
+rgb_to_16colors (const ui_file_style::color &color)
+{
+  uint8_t rgb[3];
+  color.get_rgb (rgb);
+
+  int retval = 0;
+  for (int i = 0; i < 3; i++)
+    {
+      /* Subdivide 256 possible values of each RGB component into 3
+	 regions: no color, normal color, bright color.  256 / 3 = 85,
+	 but ui-style.c follows xterm and uses 92 for R and G
+	 components of the bright-blue color, so we bias the divisor a
+	 bit to have the bright colors between 9 and 15 identical to
+	 what ui-style.c expects.  */
+      int bits = rgb[i] / 93;
+      retval |= ((bits > 0) << (2 - i)) | ((bits > 1) << 3);
+    }
+
+  return retval;
+}
+
+/* Zero if not yet initialized, 1 if stdout is a console device, else -1.  */
+static int mingw_console_initialized;
+
+/* Handle to stdout . */
+static HANDLE hstdout = INVALID_HANDLE_VALUE;
+
+/* Text attribute to use for normal text (the "none" pseudo-color).  */
+static SHORT  norm_attr;
+
+/* The most recently applied style.  */
+static ui_file_style last_style;
+
+/* Alternative for the libc 'fputs' which handles embedded SGR
+   sequences in support of styling.  */
+
+int
+gdb_console_fputs (const char *linebuf, FILE *fstream)
+{
+  if (!mingw_console_initialized)
+    {
+      hstdout = (HANDLE)_get_osfhandle (fileno (fstream));
+      DWORD cmode;
+      CONSOLE_SCREEN_BUFFER_INFO csbi;
+
+      if (hstdout != INVALID_HANDLE_VALUE
+	  && GetConsoleMode (hstdout, &cmode) != 0
+	  && GetConsoleScreenBufferInfo (hstdout, &csbi))
+	{
+	  norm_attr = csbi.wAttributes;
+	  mingw_console_initialized = 1;
+	}
+      else if (hstdout != INVALID_HANDLE_VALUE)
+	mingw_console_initialized = -1; /* valid, but not a console device */
+    }
+  /* If our stdout is not a console device, let the default 'fputs'
+     handle the task. */
+  if (mingw_console_initialized <= 0)
+    return 0;
+
+  /* Mapping between 8 ANSI colors and Windows console attributes.  */
+  static int fg_color[] = {
+    0,					/* black */
+    FOREGROUND_RED,			/* red */
+    FOREGROUND_GREEN,			/* green */
+    FOREGROUND_GREEN | FOREGROUND_RED,	/* yellow */
+    FOREGROUND_BLUE,			/* blue */
+    FOREGROUND_BLUE | FOREGROUND_RED,	/* magenta */
+    FOREGROUND_BLUE | FOREGROUND_GREEN, /* cyan */
+    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* gray */
+  };
+  static int bg_color[] = {
+    0,					/* black */
+    BACKGROUND_RED,			/* red */
+    BACKGROUND_GREEN,			/* green */
+    BACKGROUND_GREEN | BACKGROUND_RED,	/* yellow */
+    BACKGROUND_BLUE,			/* blue */
+    BACKGROUND_BLUE | BACKGROUND_RED,	/* magenta */
+    BACKGROUND_BLUE | BACKGROUND_GREEN, /* cyan */
+    BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE /* gray */
+  };
+
+  ui_file_style style = last_style;
+  unsigned char c;
+  size_t n_read;
+
+  for ( ; (c = *linebuf) != 0; linebuf += n_read)
+    {
+      if (c == '\033')
+	{
+	  fflush (fstream);
+	  bool parsed = style.parse (linebuf, &n_read);
+	  if (n_read <= 0)	/* should never happen */
+	    n_read = 1;
+	  if (!parsed)
+	    {
+	      /* This means we silently swallow SGR sequences we
+		 cannot parse.  */
+	      continue;
+	    }
+	  /* Colors.  */
+	  const ui_file_style::color &fg = style.get_foreground ();
+	  const ui_file_style::color &bg = style.get_background ();
+	  int fgcolor, bgcolor, bright, inverse;
+	  if (fg.is_none ())
+	    fgcolor = norm_attr & 15;
+	  else if (fg.is_basic ())
+	    fgcolor = fg_color[fg.get_value () & 15];
+	  else
+	    fgcolor = rgb_to_16colors (fg);
+	  if (bg.is_none ())
+	    bgcolor = norm_attr & (15 << 4);
+	  else if (bg.is_basic ())
+	    bgcolor = bg_color[bg.get_value () & 15];
+	  else
+	    bgcolor = rgb_to_16colors (bg) << 4;
+
+	  /* Intensity.  */
+	  switch (style.get_intensity ())
+	    {
+	    case ui_file_style::NORMAL:
+	    case ui_file_style::DIM:
+	      bright = 0;
+	      break;
+	    case ui_file_style::BOLD:
+	      bright = 1;
+	      break;
+	    default:
+	      gdb_assert_not_reached ("invalid intensity");
+	    }
+
+	  /* Inverse video.  */
+	  if (style.is_reverse ())
+	    inverse = 1;
+	  else
+	    inverse = 0;
+
+	  /* Construct the attribute.  */
+	  if (inverse)
+	    {
+	      int t = fgcolor;
+	      fgcolor = (bgcolor >> 4);
+	      bgcolor = (t << 4);
+	    }
+	  if (bright)
+	    fgcolor |= FOREGROUND_INTENSITY;
+
+	  SHORT attr = (bgcolor & (15 << 4)) | (fgcolor & 15);
+
+	  /* Apply the attribute.  */
+	  SetConsoleTextAttribute (hstdout, attr);
+	}
+      else
+	{
+	  /* When we are about to write newline, we need to clear to
+	     EOL with the normal attribute, to avoid spilling the
+	     colors to the next screen line.  We assume here that no
+	     non-default attribute extends beyond the newline.  */
+	  if (c == '\n')
+	    {
+	      DWORD nchars;
+	      COORD start_pos;
+	      DWORD written;
+	      CONSOLE_SCREEN_BUFFER_INFO csbi;
+
+	      fflush (fstream);
+	      GetConsoleScreenBufferInfo (hstdout, &csbi);
+
+	      if (csbi.wAttributes != norm_attr)
+		{
+		  start_pos = csbi.dwCursorPosition;
+		  nchars = csbi.dwSize.X - start_pos.X;
+
+		  FillConsoleOutputAttribute (hstdout, norm_attr, nchars,
+					      start_pos, &written);
+		  FillConsoleOutputCharacter (hstdout, ' ', nchars,
+					      start_pos, &written);
+		}
+	    }
+	  fputc (c, fstream);
+	  n_read = 1;
+	}
+    }
+
+  last_style = style;
+  return 1;
+}
diff --git a/gdb/posix-hdep.c b/gdb/posix-hdep.c
index 7911bc5..d0f467e 100644
--- a/gdb/posix-hdep.c
+++ b/gdb/posix-hdep.c
@@ -30,3 +30,11 @@ gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
 {
   return select (n, readfds, writefds, exceptfds, timeout);
 }
+
+/* Host-dependent console fputs method.  POSIX platforms always return
+   zero, to use the default C 'fputs'.  */
+int
+gdb_console_fputs (const char *buf, FILE *f)
+{
+  return 0;
+}
diff --git a/gdb/ui-file.c b/gdb/ui-file.c
index 690fc62..77f6b31 100644
--- a/gdb/ui-file.c
+++ b/gdb/ui-file.c
@@ -236,6 +236,12 @@ stdio_file::write_async_safe (const char *buf, long length_buf)
 void
 stdio_file::puts (const char *linebuffer)
 {
+  /* This host-dependent function (with implementations in
+     posix-hdep.c and mingw-hdep.c) is given the opportunity to
+     process the output first in host-dependent way.  If it does, it
+     should return non-zero, to avoid calling fputs below.  */
+  if (gdb_console_fputs (linebuffer, m_file))
+    return;
   /* Calling error crashes when we are called from the exception framework.  */
   if (fputs (linebuffer, m_file))
     {
diff --git a/gdb/ui-file.h b/gdb/ui-file.h
index 26cf2bf..6e6ca1c 100644
--- a/gdb/ui-file.h
+++ b/gdb/ui-file.h
@@ -101,6 +101,8 @@ extern void ui_file_write_async_safe (struct ui_file *file, const char *buf,
 
 extern long ui_file_read (struct ui_file *file, char *buf, long length_buf);
 
+extern int gdb_console_fputs (const char *, FILE *);
+
 /* A std::string-based ui_file.  Can be used as a scratch buffer for
    collecting output.  */
 
diff --git a/gdb/utils.c b/gdb/utils.c
index 258614a..840779a 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -1445,8 +1445,18 @@ can_emit_style_escape (struct ui_file *stream)
       || !ui_file_isatty (stream))
     return false;
   const char *term = getenv ("TERM");
+  /* Windows doesn't by default define $TERM, but can support styles
+     regardless.  */
+#ifndef _WIN32
   if (term == nullptr || !strcmp (term, "dumb"))
     return false;
+#else
+  /* But if they do define $TERM, let us behave the same as on Posix
+     platforms, for the benefit of programs which invoke GDB as their
+     back-end.  */
+  if (term && !strcmp (term, "dumb"))
+    return false;
+#endif
   return true;
 }
 


  reply	other threads:[~2019-03-08 14:55 UTC|newest]

Thread overview: 99+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-28  0:16 Tom Tromey
2018-11-28  0:14 ` [PATCH 12/16] Style addresses Tom Tromey
2018-11-28  0:14 ` [PATCH 09/16] Style print_address_symbolic Tom Tromey
2018-11-28  0:14 ` [PATCH 05/16] Add output styles to gdb Tom Tromey
2018-12-24  4:08   ` Joel Brobecker
2018-12-28 18:55     ` Tom Tromey
2018-11-28  0:14 ` [PATCH 14/16] Use wclrtoeol in tui_show_source_line Tom Tromey
2018-12-24  8:03   ` Joel Brobecker
2018-11-28  0:14 ` [PATCH 10/16] Style the gdb welcome message Tom Tromey
2018-11-28  0:14 ` [PATCH 06/16] Reset terminal styles Tom Tromey
2018-12-24  4:16   ` Joel Brobecker
2018-12-28 19:01     ` Tom Tromey
2018-11-28  0:14 ` [PATCH 02/16] Add a "context" argument to add_setshow_enum_cmd Tom Tromey
2018-11-28  0:14 ` [PATCH 03/16] Introduce ui_file_style Tom Tromey
2018-12-24  3:40   ` Joel Brobecker
2018-12-28 18:54     ` Tom Tromey
2018-11-28  0:16 ` [PATCH 16/16] Document the "set style" commands Tom Tromey
2018-11-28  6:51   ` Eli Zaretskii
2018-12-28 19:19     ` Tom Tromey
2018-11-28  0:16 ` [PATCH 07/16] Style variable names Tom Tromey
2018-11-28  0:16 ` [PATCH 11/16] Style the "Reading symbols" message Tom Tromey
2018-11-28  0:16 ` [PATCH 15/16] Highlight source code using GNU Source Highlight Tom Tromey
2019-11-16  0:58   ` Andrew Pinski
2019-11-16  8:19     ` Eli Zaretskii
2019-11-24 17:41     ` Tom Tromey
2019-11-24 18:13       ` Christian Biesinger via gdb-patches
2019-11-24 19:53         ` Tom Tromey
2018-11-28  0:16 ` [PATCH 04/16] Change gdb test suite's TERM setting Tom Tromey
2018-11-28  0:16 ` [PATCH 13/16] Make ANSI terminal escape sequences work in TUI Tom Tromey
2018-12-24  8:02   ` Joel Brobecker
2018-12-28 19:42   ` Tom Tromey
2018-11-28  0:16 ` [PATCH 08/16] Style locations when setting a breakpoint Tom Tromey
2018-11-28  0:19 ` [PATCH 01/16] Change wrap buffering to use a std::string Tom Tromey
2018-12-23 15:26   ` Joel Brobecker
2018-12-28 18:47     ` Tom Tromey
2018-11-28  7:02 ` [PATCH 00/16] Add styling to the gdb CLI and TUI Eli Zaretskii
2018-11-29 22:44   ` Tom Tromey
2018-11-30  7:02     ` Eli Zaretskii
2018-11-30 16:17       ` Tom Tromey
2018-12-23 10:49         ` Joel Brobecker
2019-03-01 13:10         ` Pedro Alves
2019-03-01 13:56           ` Eli Zaretskii
2019-03-01 14:10             ` Pedro Alves
2019-03-01 14:50               ` Eli Zaretskii
2019-03-01  7:47     ` Eli Zaretskii
2019-03-01 18:42       ` Tom Tromey
2019-03-01 19:40         ` Eli Zaretskii
2019-03-01 21:04           ` Tom Tromey
2019-03-02  7:15             ` Eli Zaretskii
2019-03-03 15:42             ` Eli Zaretskii
2019-03-04 15:08               ` Tom Tromey
2019-03-04 15:57                 ` Eli Zaretskii
2019-03-04 16:16                   ` Tom Tromey
2019-03-05 15:38                     ` Eli Zaretskii
2019-03-08 14:55                       ` Eli Zaretskii [this message]
2019-03-08 16:14                         ` Hannes Domani via gdb-patches
2019-03-08 21:08                           ` Tom Tromey
2019-03-08 21:11                         ` Tom Tromey
2019-03-09  6:49                           ` Eli Zaretskii
2019-03-03 15:53             ` Eli Zaretskii
2019-03-03 16:16               ` Matt Rice
2019-03-03 17:13                 ` Eli Zaretskii
2019-03-03 18:04                   ` Matt Rice
2019-03-04 15:01               ` Tom Tromey
2019-03-04 17:37                 ` Eli Zaretskii
2019-03-04 17:40                   ` Tom Tromey
2019-03-06 16:02                     ` Eli Zaretskii
2019-03-20 19:35                       ` GDB version as convenience variable Eli Zaretskii
2019-03-25 17:31                         ` Eli Zaretskii
2019-03-25 17:58                           ` Simon Marchi
2019-03-25 18:10                             ` Eli Zaretskii
2019-03-25 18:33                               ` Simon Marchi
2019-03-25 18:37                                 ` Simon Marchi
2019-03-25 18:43                                 ` Eli Zaretskii
2019-03-25 18:51                                   ` Simon Marchi
2019-03-25 19:19                                     ` Eli Zaretskii
2019-03-26 14:47                                       ` Simon Marchi
2019-03-26 20:57                                         ` Joel Brobecker
2019-03-27  3:34                                           ` Eli Zaretskii
2019-03-27 12:56                                             ` Joel Brobecker
2019-03-30 17:25                                               ` Simon Marchi
2019-03-30 10:01                                         ` Eli Zaretskii
2019-03-21  1:55                       ` [PATCH 00/16] Add styling to the gdb CLI and TUI Simon Marchi
2019-03-21 14:38                         ` Eli Zaretskii
2019-03-21 15:02                           ` Simon Marchi
2019-03-21 16:01                             ` Eli Zaretskii
2019-03-21 16:06                               ` Simon Marchi
2019-03-21 16:12                                 ` Pedro Alves
2019-03-21 16:54                                   ` John Baldwin
2019-03-21 17:02                                   ` Eli Zaretskii
2019-03-21 18:08                                     ` Simon Marchi
2019-03-21 18:19                                       ` Pedro Alves
2019-03-21 18:38                                         ` Eli Zaretskii
2019-03-07  6:02                   ` Joel Brobecker
2019-03-07 14:53                     ` Eli Zaretskii
2019-03-08  5:40                       ` Joel Brobecker
2019-03-04 16:04       ` Eli Zaretskii
2018-12-24  9:27 ` Joel Brobecker
2018-12-28 20:57   ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=83d0n1a01f.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox