Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / tui / tui.c
index 27d51576e1b06508dc85fec8e588032fe4b069e4..b04e106e5c474eeae101a4b9c44a5f6b8cc914db 100644 (file)
@@ -1,6 +1,6 @@
 /* General functions for the WDB TUI.
 
-   Copyright (C) 1998-2013 Free Software Foundation, Inc.
+   Copyright (C) 1998-2015 Free Software Foundation, Inc.
 
    Contributed by Hewlett-Packard Company.
 
@@ -37,9 +37,8 @@
 #include "inferior.h"
 #include "symtab.h"
 #include "source.h"
+#include "terminal.h"
 
-#include <stdio.h>
-#include <stdlib.h>
 #include <ctype.h>
 #include <signal.h>
 #include <fcntl.h>
@@ -49,6 +48,7 @@
 #include <setjmp.h>
 
 #include "gdb_curses.h"
+#include "interps.h"
 
 /* This redefines CTRL if it is not already defined, so it must come
    after terminal state releated include files like <term.h> and
@@ -90,16 +90,31 @@ static Keymap tui_readline_standard_keymap;
 static int
 tui_rl_switch_mode (int notused1, int notused2)
 {
-  if (tui_active)
+
+  /* Don't let exceptions escape.  We're in the middle of a readline
+     callback that isn't prepared for that.  */
+  TRY
     {
-      tui_disable ();
-      rl_prep_terminal (0);
+      if (tui_active)
+       {
+         tui_disable ();
+         rl_prep_terminal (0);
+       }
+      else
+       {
+         /* If tui_enable throws, we'll re-prep below.  */
+         rl_deprep_terminal ();
+         tui_enable ();
+       }
     }
-  else
+  CATCH (ex, RETURN_MASK_ALL)
     {
-      rl_deprep_terminal ();
-      tui_enable ();
+      exception_print (gdb_stderr, ex);
+
+      if (!tui_active)
+       rl_prep_terminal (0);
     }
+  END_CATCH
 
   /* Clear the readline in case switching occurred in middle of
      something.  */
@@ -362,6 +377,20 @@ tui_initialize_readline (void)
   rl_bind_key_in_map ('s', tui_rl_next_keymap, tui_ctlx_keymap);
 }
 
+/* Return the TERM variable from the environment, or "<unset>"
+   if not set.  */
+
+static const char *
+gdb_getenv_term (void)
+{
+  const char *term;
+
+  term = getenv ("TERM");
+  if (term != NULL)
+    return term;
+  return "<unset>";
+}
+
 /* Enter in the tui mode (curses).
    When in normal mode, it installs the tui hooks in gdb, redirects
    the gdb output, configures the readline to work in tui mode.
@@ -369,8 +398,7 @@ tui_initialize_readline (void)
 void
 tui_enable (void)
 {
-  if (!tui_allowed_p ())
-    error (_("TUI mode not allowed"));
+  struct interp *interp;
 
   if (tui_active)
     return;
@@ -381,9 +409,49 @@ tui_enable (void)
   if (tui_finish_init)
     {
       WINDOW *w;
+      SCREEN *s;
+      const char *cap;
+      const char *interp;
+
+      /* If the top level interpreter is not the console/tui (e.g.,
+        MI), enabling curses will certainly lose.  */
+      interp = interp_name (top_level_interpreter ());
+      if (strcmp (interp, INTERP_TUI) != 0)
+       error (_("Cannot enable the TUI when the interpreter is '%s'"), interp);
+
+      /* Don't try to setup curses (and print funny control
+        characters) if we're not outputting to a terminal.  */
+      if (!ui_file_isatty (gdb_stdout))
+       error (_("Cannot enable the TUI when output is not a terminal"));
+
+      s = newterm (NULL, stdout, stdin);
+#ifdef __MINGW32__
+      /* The MinGW port of ncurses requires $TERM to be unset in order
+        to activate the Windows console driver.  */
+      if (s == NULL)
+       s = newterm ("unknown", stdout, stdin);
+#endif
+      if (s == NULL)
+       {
+         error (_("Cannot enable the TUI: error opening terminal [TERM=%s]"),
+                gdb_getenv_term ());
+       }
+      w = stdscr;
+
+      /* Check required terminal capabilities.  The MinGW port of
+        ncurses does have them, but doesn't expose them through "cup".  */
+#ifndef __MINGW32__
+      cap = tigetstr ("cup");
+      if (cap == NULL || cap == (char *) -1 || *cap == '\0')
+       {
+         endwin ();
+         delscreen (s);
+         error (_("Cannot enable the TUI: "
+                  "terminal doesn't support cursor addressing [TERM=%s]"),
+                gdb_getenv_term ());
+       }
+#endif
 
-      w = initscr ();
-  
       cbreak ();
       noecho ();
       /* timeout (1); */
@@ -419,15 +487,26 @@ tui_enable (void)
   tui_setup_io (1);
 
   tui_active = 1;
+
+  /* Resize windows before anything might display/refresh a
+     window.  */
+  if (tui_win_resized ())
+    {
+      tui_set_win_resized_to (FALSE);
+      tui_resize_all ();
+    }
+
   if (deprecated_safe_get_selected_frame ())
-     tui_show_frame_info (deprecated_safe_get_selected_frame ());
+    tui_show_frame_info (deprecated_safe_get_selected_frame ());
 
   /* Restore TUI keymap.  */
   tui_set_key_mode (tui_current_key_mode);
+
+  /* Refresh the screen.  */
   tui_refresh_all_win ();
 
   /* Update gdb's knowledge of its terminal.  */
-  target_terminal_save_ours ();
+  gdb_save_tty_state ();
   tui_update_gdb_sizes ();
 }
 
@@ -457,7 +536,7 @@ tui_disable (void)
   tui_setup_io (0);
 
   /* Update gdb's knowledge of its terminal.  */
-  target_terminal_save_ours ();
+  gdb_save_tty_state ();
 
   tui_active = 0;
   tui_update_gdb_sizes ();
This page took 0.025569 seconds and 4 git commands to generate.