xref: /dflybsd-src/contrib/gdb-7/gdb/tui/tui-interp.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* TUI Interpreter definitions for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 2003-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "interps.h"
225796c8dcSSimon Schubert #include "top.h"
235796c8dcSSimon Schubert #include "event-top.h"
245796c8dcSSimon Schubert #include "event-loop.h"
255796c8dcSSimon Schubert #include "ui-out.h"
265796c8dcSSimon Schubert #include "cli-out.h"
275796c8dcSSimon Schubert #include "tui/tui-data.h"
285796c8dcSSimon Schubert #include "readline/readline.h"
295796c8dcSSimon Schubert #include "tui/tui-win.h"
305796c8dcSSimon Schubert #include "tui/tui.h"
315796c8dcSSimon Schubert #include "tui/tui-io.h"
325796c8dcSSimon Schubert #include "exceptions.h"
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert /* Set to 1 when the TUI mode must be activated when we first start
355796c8dcSSimon Schubert    gdb.  */
365796c8dcSSimon Schubert static int tui_start_enabled = 0;
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert /* Cleanup the tui before exiting.  */
395796c8dcSSimon Schubert 
405796c8dcSSimon Schubert static void
tui_exit(void)415796c8dcSSimon Schubert tui_exit (void)
425796c8dcSSimon Schubert {
435796c8dcSSimon Schubert   /* Disable the tui.  Curses mode is left leaving the screen in a
445796c8dcSSimon Schubert      clean state (see endwin()).  */
455796c8dcSSimon Schubert   tui_disable ();
465796c8dcSSimon Schubert }
475796c8dcSSimon Schubert 
48cf7f2e2dSJohn Marino /* True if TUI is the top-level interpreter.  */
49cf7f2e2dSJohn Marino static int tui_is_toplevel = 0;
50cf7f2e2dSJohn Marino 
515796c8dcSSimon Schubert /* These implement the TUI interpreter.  */
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert static void *
tui_init(struct interp * self,int top_level)54a45ae5f8SJohn Marino tui_init (struct interp *self, int top_level)
555796c8dcSSimon Schubert {
56cf7f2e2dSJohn Marino   tui_is_toplevel = top_level;
57cf7f2e2dSJohn Marino 
585796c8dcSSimon Schubert   /* Install exit handler to leave the screen in a good shape.  */
595796c8dcSSimon Schubert   atexit (tui_exit);
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert   tui_initialize_static_data ();
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert   tui_initialize_io ();
645796c8dcSSimon Schubert   tui_initialize_win ();
65cf7f2e2dSJohn Marino   if (ui_file_isatty (gdb_stdout))
665796c8dcSSimon Schubert     tui_initialize_readline ();
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert   return NULL;
695796c8dcSSimon Schubert }
705796c8dcSSimon Schubert 
71cf7f2e2dSJohn Marino /* True if enabling the TUI is allowed.  Example, if the top level
72cf7f2e2dSJohn Marino    interpreter is MI, enabling curses will certainly lose.  */
73cf7f2e2dSJohn Marino 
74cf7f2e2dSJohn Marino int
tui_allowed_p(void)75cf7f2e2dSJohn Marino tui_allowed_p (void)
76cf7f2e2dSJohn Marino {
77cf7f2e2dSJohn Marino   /* Only if TUI is the top level interpreter.  Also don't try to
78cf7f2e2dSJohn Marino      setup curses (and print funny control characters) if we're not
79cf7f2e2dSJohn Marino      outputting to a terminal.  */
80cf7f2e2dSJohn Marino   return tui_is_toplevel && ui_file_isatty (gdb_stdout);
81cf7f2e2dSJohn Marino }
82cf7f2e2dSJohn Marino 
835796c8dcSSimon Schubert static int
tui_resume(void * data)845796c8dcSSimon Schubert tui_resume (void *data)
855796c8dcSSimon Schubert {
865796c8dcSSimon Schubert   struct ui_file *stream;
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert   /* gdb_setup_readline will change gdb_stdout.  If the TUI was
895796c8dcSSimon Schubert      previously writing to gdb_stdout, then set it to the new
905796c8dcSSimon Schubert      gdb_stdout afterwards.  */
915796c8dcSSimon Schubert 
925796c8dcSSimon Schubert   stream = cli_out_set_stream (tui_old_uiout, gdb_stdout);
935796c8dcSSimon Schubert   if (stream != gdb_stdout)
945796c8dcSSimon Schubert     {
955796c8dcSSimon Schubert       cli_out_set_stream (tui_old_uiout, stream);
965796c8dcSSimon Schubert       stream = NULL;
975796c8dcSSimon Schubert     }
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert   gdb_setup_readline ();
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert   if (stream != NULL)
1025796c8dcSSimon Schubert     cli_out_set_stream (tui_old_uiout, gdb_stdout);
1035796c8dcSSimon Schubert 
1045796c8dcSSimon Schubert   if (tui_start_enabled)
1055796c8dcSSimon Schubert     tui_enable ();
1065796c8dcSSimon Schubert   return 1;
1075796c8dcSSimon Schubert }
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert static int
tui_suspend(void * data)1105796c8dcSSimon Schubert tui_suspend (void *data)
1115796c8dcSSimon Schubert {
1125796c8dcSSimon Schubert   tui_start_enabled = tui_active;
1135796c8dcSSimon Schubert   tui_disable ();
1145796c8dcSSimon Schubert   return 1;
1155796c8dcSSimon Schubert }
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert /* Display the prompt if we are silent.  */
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert static int
tui_display_prompt_p(void * data)1205796c8dcSSimon Schubert tui_display_prompt_p (void *data)
1215796c8dcSSimon Schubert {
1225796c8dcSSimon Schubert   if (interp_quiet_p (NULL))
1235796c8dcSSimon Schubert     return 0;
1245796c8dcSSimon Schubert   else
1255796c8dcSSimon Schubert     return 1;
1265796c8dcSSimon Schubert }
1275796c8dcSSimon Schubert 
128a45ae5f8SJohn Marino static struct ui_out *
tui_ui_out(struct interp * self)129a45ae5f8SJohn Marino tui_ui_out (struct interp *self)
130a45ae5f8SJohn Marino {
131a45ae5f8SJohn Marino   if (tui_active)
132a45ae5f8SJohn Marino     return tui_out;
133a45ae5f8SJohn Marino   else
134a45ae5f8SJohn Marino     return tui_old_uiout;
135a45ae5f8SJohn Marino }
136a45ae5f8SJohn Marino 
1375796c8dcSSimon Schubert static struct gdb_exception
tui_exec(void * data,const char * command_str)1385796c8dcSSimon Schubert tui_exec (void *data, const char *command_str)
1395796c8dcSSimon Schubert {
1405796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__, _("tui_exec called"));
1415796c8dcSSimon Schubert }
1425796c8dcSSimon Schubert 
1435796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes.  */
1445796c8dcSSimon Schubert extern initialize_file_ftype _initialize_tui_interp;
1455796c8dcSSimon Schubert 
1465796c8dcSSimon Schubert void
_initialize_tui_interp(void)1475796c8dcSSimon Schubert _initialize_tui_interp (void)
1485796c8dcSSimon Schubert {
1495796c8dcSSimon Schubert   static const struct interp_procs procs = {
1505796c8dcSSimon Schubert     tui_init,
1515796c8dcSSimon Schubert     tui_resume,
1525796c8dcSSimon Schubert     tui_suspend,
1535796c8dcSSimon Schubert     tui_exec,
1545796c8dcSSimon Schubert     tui_display_prompt_p,
155a45ae5f8SJohn Marino     tui_ui_out,
1565796c8dcSSimon Schubert   };
157a45ae5f8SJohn Marino   struct interp *tui_interp;
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert   /* Create a default uiout builder for the TUI.  */
160a45ae5f8SJohn Marino   tui_interp = interp_new (INTERP_TUI, &procs);
161a45ae5f8SJohn Marino   interp_add (tui_interp);
1625796c8dcSSimon Schubert   if (interpreter_p && strcmp (interpreter_p, INTERP_TUI) == 0)
1635796c8dcSSimon Schubert     tui_start_enabled = 1;
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert   if (interpreter_p && strcmp (interpreter_p, INTERP_CONSOLE) == 0)
1665796c8dcSSimon Schubert     {
1675796c8dcSSimon Schubert       xfree (interpreter_p);
1685796c8dcSSimon Schubert       interpreter_p = xstrdup (INTERP_TUI);
1695796c8dcSSimon Schubert     }
1705796c8dcSSimon Schubert }
171