Remove unnecessary casts of NULL
[deliverable/binutils-gdb.git] / gdb / tui / tui-io.c
index 5a84d08a28394f8248bcc810e6753d33aad4e906..31c1c769381fb99cab6d9774f51e802a00d32646 100644 (file)
@@ -1,6 +1,6 @@
 /* TUI support I/O functions.
 
-   Copyright (C) 1998-2018 Free Software Foundation, Inc.
+   Copyright (C) 1998-2019 Free Software Foundation, Inc.
 
    Contributed by Hewlett-Packard Company.
 
 #include "cli-out.h"
 #include <fcntl.h>
 #include <signal.h>
-#include "filestuff.h"
+#ifdef __MINGW32__
+#include <windows.h>
+#endif
+#include "common/filestuff.h"
 #include "completer.h"
 #include "gdb_curses.h"
 #include <map>
    "gdb_curses.h".  */
 #include "readline/readline.h"
 
+#ifdef __MINGW32__
+static SHORT ncurses_norm_attr;
+#endif
+
 static int tui_getc (FILE *fp);
 
 static int
@@ -267,6 +274,15 @@ static int last_color_pair = -1;
 
 static ui_file_style last_style;
 
+/* If true, we're highlighting the current source line in reverse
+   video mode.  */
+static bool reverse_mode_p = false;
+
+/* The background/foreground colors before we entered reverse
+   mode.  */
+static ui_file_style::color reverse_save_bg (ui_file_style::NONE);
+static ui_file_style::color reverse_save_fg (ui_file_style::NONE);
+
 /* Given two colors, return their color pair index; making a new one
    if necessary.  */
 
@@ -291,21 +307,11 @@ get_color_pair (int fg, int bg)
   return it->second;
 }
 
-/* Apply an ANSI escape sequence from BUF to W.  BUF must start with
-   the ESC character.  If BUF does not start with an ANSI escape,
-   return 0.  Otherwise, apply the sequence if it is recognized, or
-   simply ignore it if not.  In this case, the number of bytes read
-   from BUF is returned.  */
+/* Apply STYLE to W.  */
 
-static size_t
-apply_ansi_escape (WINDOW *w, const char *buf)
+static void
+apply_style (WINDOW *w, ui_file_style style)
 {
-  ui_file_style style = last_style;
-  size_t n_read;
-
-  if (!style.parse (buf, &n_read))
-    return n_read;
-
   /* Reset.  */
   wattron (w, A_NORMAL);
   wattroff (w, A_BOLD);
@@ -322,6 +328,16 @@ apply_ansi_escape (WINDOW *w, const char *buf)
       int fgi, bgi;
       if (get_color (fg, &fgi) && get_color (bg, &bgi))
        {
+#ifdef __MINGW32__
+         /* MS-Windows port of ncurses doesn't support implicit
+            default foreground and background colors, so we must
+            specify them explicitly when needed, using the colors we
+            saw at startup.  */
+         if (fgi == -1)
+           fgi = ncurses_norm_attr & 15;
+         if (bgi == -1)
+           bgi = (ncurses_norm_attr >> 4) & 15;
+#endif
          int pair = get_color_pair (fgi, bgi);
          if (last_color_pair != -1)
            wattroff (w, COLOR_PAIR (last_color_pair));
@@ -351,9 +367,83 @@ apply_ansi_escape (WINDOW *w, const char *buf)
     wattron (w, A_REVERSE);
 
   last_style = style;
+}
+
+/* Apply an ANSI escape sequence from BUF to W.  BUF must start with
+   the ESC character.  If BUF does not start with an ANSI escape,
+   return 0.  Otherwise, apply the sequence if it is recognized, or
+   simply ignore it if not.  In this case, the number of bytes read
+   from BUF is returned.  */
+
+static size_t
+apply_ansi_escape (WINDOW *w, const char *buf)
+{
+  ui_file_style style = last_style;
+  size_t n_read;
+
+  if (!style.parse (buf, &n_read))
+    return n_read;
+
+  if (reverse_mode_p)
+    {
+      /* We want to reverse _only_ the default foreground/background
+        colors.  If the foreground color is not the default (because
+        the text was styled), we want to leave it as is.  If e.g.,
+        the terminal is fg=BLACK, and bg=WHITE, and the style wants
+        to print text in RED, we want to reverse the background color
+        (print in BLACK), but still print the text in RED.  To do
+        that, we enable the A_REVERSE attribute, and re-reverse the
+        parsed-style's fb/bg colors.
+
+        Notes on the approach:
+
+         - there's no portable way to know which colors the default
+           fb/bg colors map to.
+
+         - this approach does the right thing even if you change the
+           terminal colors while GDB is running -- the reversed
+           colors automatically adapt.
+      */
+      if (!style.is_default ())
+       {
+         ui_file_style::color bg = style.get_background ();
+         ui_file_style::color fg = style.get_foreground ();
+         style.set_fg (bg);
+         style.set_bg (fg);
+       }
+
+      /* Enable A_REVERSE.  */
+      style.set_reverse (true);
+    }
+
+  apply_style (w, style);
   return n_read;
 }
 
+/* See tui.io.h.  */
+
+void
+tui_set_reverse_mode (WINDOW *w, bool reverse)
+{
+  ui_file_style style = last_style;
+
+  reverse_mode_p = reverse;
+  style.set_reverse (reverse);
+
+  if (reverse)
+    {
+      reverse_save_bg = style.get_background ();
+      reverse_save_fg = style.get_foreground ();
+    }
+  else
+    {
+      style.set_bg (reverse_save_bg);
+      style.set_fg (reverse_save_fg);
+    }
+
+  apply_style (w, style);
+}
+
 /* Print LENGTH characters from the buffer pointed to by BUF to the
    curses command window.  The output is buffered.  It is up to the
    caller to refresh the screen if necessary.  */
@@ -367,9 +457,8 @@ tui_write (const char *buf, size_t length)
 }
 
 static void
-tui_puts_internal (const char *string, int *height)
+tui_puts_internal (WINDOW *w, const char *string, int *height)
 {
-  WINDOW *w = TUI_CMD_WIN->generic.handle;
   char c;
   int prev_col = 0;
 
@@ -410,9 +499,11 @@ tui_puts_internal (const char *string, int *height)
    necessary.  */
 
 void
-tui_puts (const char *string)
+tui_puts (const char *string, WINDOW *w)
 {
-  tui_puts_internal (string, nullptr);
+  if (w == nullptr)
+    w = TUI_CMD_WIN->generic.handle;
+  tui_puts_internal (w, string, nullptr);
 }
 
 /* Readline callback.
@@ -453,7 +544,7 @@ tui_redisplay_readline (void)
   prev_col = 0;
   height = 1;
   if (prompt != nullptr)
-    tui_puts_internal (prompt, &height);
+    tui_puts_internal (TUI_CMD_WIN->generic.handle, prompt, &height);
 
   prev_col = getcurx (w);
   for (in = 0; in <= rl_end; in++)
@@ -787,7 +878,7 @@ tui_initialize_io (void)
   if (tui_rl_outstream == 0)
     error (_("Cannot redirect readline output"));
 
-  setvbuf (tui_rl_outstream, (char*) NULL, _IOLBF, 0);
+  setvbuf (tui_rl_outstream, NULL, _IOLBF, 0);
 
 #ifdef O_NONBLOCK
   (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NONBLOCK);
@@ -800,6 +891,19 @@ tui_initialize_io (void)
 #else
   tui_rl_outstream = stdout;
 #endif
+
+#ifdef __MINGW32__
+  /* MS-Windows port of ncurses doesn't support default foreground and
+     background colors, so we must record the default colors at startup.  */
+  HANDLE hstdout = (HANDLE)_get_osfhandle (fileno (stdout));
+  DWORD cmode;
+  CONSOLE_SCREEN_BUFFER_INFO csbi;
+
+  if (hstdout != INVALID_HANDLE_VALUE
+      && GetConsoleMode (hstdout, &cmode) != 0
+      && GetConsoleScreenBufferInfo (hstdout, &csbi))
+    ncurses_norm_attr = csbi.wAttributes;
+#endif
 }
 
 /* Get a character from the command window.  This is called from the
@@ -821,7 +925,7 @@ tui_getc (FILE *fp)
 
   /* The \n must be echoed because it will not be printed by
      readline.  */
-  if (ch == '\n')
+  if (ch == '\n' || ch == '\r')
     {
       /* When hitting return with an empty input, gdb executes the last
          command.  If we emit a newline, this fills up the command window
This page took 0.029204 seconds and 4 git commands to generate.