]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdb/tui] Don't try deferred curses initialization twice
authorTom de Vries <tdevries@suse.de>
Mon, 7 Apr 2025 20:27:39 +0000 (22:27 +0200)
committerTom de Vries <tdevries@suse.de>
Mon, 7 Apr 2025 20:27:39 +0000 (22:27 +0200)
I noticed that if deferred curses initialization fails, for instance when
using TERM=dumb, and we try the same again, we run into the same error:
...
$ TERM=dumb gdb -batch -ex "tui enable" -ex "tui enable"
Cannot enable the TUI: terminal doesn't support cursor addressing [TERM=dumb]
Cannot enable the TUI: terminal doesn't support cursor addressing [TERM=dumb]
...

I think it's better to try deferred curses initialization only once.

Fix this by changing bool tui_finish_init into a tribool, and using
TRIBOOL_UNKNOWN to represent the "initialization failed" state, such that we
get instead:
...
$ TERM=dumb gdb -batch -ex "tui enable" -ex "tui enable"
Cannot enable the TUI: terminal doesn't support cursor addressing [TERM=dumb]
Cannot enable the TUI
...

Tested on x86_64-linux.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/tui/tui.c

index 491fcb18471f3940936ac3a69b7efe0b15e6e3ad..9f60e87e7886296d41799411fc595f6d4a403468 100644 (file)
@@ -67,7 +67,12 @@ show_tui_debug (struct ui_file *file, int from_tty,
 
 /* Tells whether the TUI is active or not.  */
 bool tui_active = false;
-static bool tui_finish_init = true;
+
+/* Tells whether the TUI should do deferred curses initialization.
+   If TRIBOOL_TRUE, then yes.  If TRIBOOL_FALSE. then no (because
+   initialization is already done).  If TRIBOOL_UNKNOWN, then no (because
+   initialization failed).  */
+static tribool tui_finish_init = TRIBOOL_TRUE;
 
 enum tui_key_mode tui_current_key_mode = TUI_COMMAND_MODE;
 
@@ -392,7 +397,13 @@ tui_enable (void)
   /* To avoid to initialize curses when gdb starts, there is a deferred
      curses initialization.  This initialization is made only once
      and the first time the curses mode is entered.  */
-  if (tui_finish_init)
+  if (tui_finish_init == TRIBOOL_UNKNOWN)
+    {
+      /* Initialization failed before, just throw a generic error, don't try
+        again.  */
+      error (_("Cannot enable the TUI"));
+    }
+  else if (tui_finish_init == TRIBOOL_TRUE)
     {
       WINDOW *w;
       SCREEN *s;
@@ -412,6 +423,9 @@ tui_enable (void)
       if (!gdb_stderr->isatty ())
        error (_("Cannot enable the TUI when output is not a terminal"));
 
+      /* Don't try initialization again.  */
+      tui_finish_init = TRIBOOL_UNKNOWN;
+
       s = newterm (NULL, stdout, stdin);
 #ifdef __MINGW32__
       /* The MinGW port of ncurses requires $TERM to be unset in order
@@ -470,7 +484,7 @@ tui_enable (void)
       tui_set_win_focus_to (tui_src_win ());
       keypad (tui_cmd_win ()->handle.get (), TRUE);
       wrefresh (tui_cmd_win ()->handle.get ());
-      tui_finish_init = false;
+      tui_finish_init = TRIBOOL_FALSE;
     }
   else
     {