GDB (API)
/home/stan/gdb/src/gdb/tui/tui-interp.c
Go to the documentation of this file.
00001 /* TUI Interpreter definitions for GDB, the GNU debugger.
00002 
00003    Copyright (C) 2003-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "interps.h"
00022 #include "top.h"
00023 #include "event-top.h"
00024 #include "event-loop.h"
00025 #include "ui-out.h"
00026 #include "cli-out.h"
00027 #include "tui/tui-data.h"
00028 #include "readline/readline.h"
00029 #include "tui/tui-win.h"
00030 #include "tui/tui.h"
00031 #include "tui/tui-io.h"
00032 #include "exceptions.h"
00033 
00034 /* Set to 1 when the TUI mode must be activated when we first start
00035    gdb.  */
00036 static int tui_start_enabled = 0;
00037 
00038 /* Cleanup the tui before exiting.  */
00039 
00040 static void
00041 tui_exit (void)
00042 {
00043   /* Disable the tui.  Curses mode is left leaving the screen in a
00044      clean state (see endwin()).  */
00045   tui_disable ();
00046 }
00047 
00048 /* True if TUI is the top-level interpreter.  */
00049 static int tui_is_toplevel = 0;
00050 
00051 /* These implement the TUI interpreter.  */
00052 
00053 static void *
00054 tui_init (struct interp *self, int top_level)
00055 {
00056   tui_is_toplevel = top_level;
00057 
00058   /* Install exit handler to leave the screen in a good shape.  */
00059   atexit (tui_exit);
00060 
00061   tui_initialize_static_data ();
00062 
00063   tui_initialize_io ();
00064   tui_initialize_win ();
00065   if (ui_file_isatty (gdb_stdout))
00066     tui_initialize_readline ();
00067 
00068   return NULL;
00069 }
00070 
00071 /* True if enabling the TUI is allowed.  Example, if the top level
00072    interpreter is MI, enabling curses will certainly lose.  */
00073 
00074 int
00075 tui_allowed_p (void)
00076 {
00077   /* Only if TUI is the top level interpreter.  Also don't try to
00078      setup curses (and print funny control characters) if we're not
00079      outputting to a terminal.  */
00080   return tui_is_toplevel && ui_file_isatty (gdb_stdout);
00081 }
00082 
00083 static int
00084 tui_resume (void *data)
00085 {
00086   struct ui_file *stream;
00087 
00088   /* gdb_setup_readline will change gdb_stdout.  If the TUI was
00089      previously writing to gdb_stdout, then set it to the new
00090      gdb_stdout afterwards.  */
00091 
00092   stream = cli_out_set_stream (tui_old_uiout, gdb_stdout);
00093   if (stream != gdb_stdout)
00094     {
00095       cli_out_set_stream (tui_old_uiout, stream);
00096       stream = NULL;
00097     }
00098 
00099   gdb_setup_readline ();
00100 
00101   if (stream != NULL)
00102     cli_out_set_stream (tui_old_uiout, gdb_stdout);
00103 
00104   if (tui_start_enabled)
00105     tui_enable ();
00106   return 1;
00107 }
00108 
00109 static int
00110 tui_suspend (void *data)
00111 {
00112   tui_start_enabled = tui_active;
00113   tui_disable ();
00114   return 1;
00115 }
00116 
00117 /* Display the prompt if we are silent.  */
00118 
00119 static int
00120 tui_display_prompt_p (void *data)
00121 {
00122   if (interp_quiet_p (NULL))
00123     return 0;
00124   else
00125     return 1;
00126 }
00127 
00128 static struct ui_out *
00129 tui_ui_out (struct interp *self)
00130 {
00131   if (tui_active)
00132     return tui_out;
00133   else
00134     return tui_old_uiout;
00135 }
00136 
00137 static struct gdb_exception
00138 tui_exec (void *data, const char *command_str)
00139 {
00140   internal_error (__FILE__, __LINE__, _("tui_exec called"));
00141 }
00142 
00143 /* Provide a prototype to silence -Wmissing-prototypes.  */
00144 extern initialize_file_ftype _initialize_tui_interp;
00145 
00146 void
00147 _initialize_tui_interp (void)
00148 {
00149   static const struct interp_procs procs = {
00150     tui_init,
00151     tui_resume,
00152     tui_suspend,
00153     tui_exec,
00154     tui_display_prompt_p,
00155     tui_ui_out,
00156     NULL,
00157     cli_command_loop
00158   };
00159   struct interp *tui_interp;
00160 
00161   /* Create a default uiout builder for the TUI.  */
00162   tui_interp = interp_new (INTERP_TUI, &procs);
00163   interp_add (tui_interp);
00164   if (interpreter_p && strcmp (interpreter_p, INTERP_TUI) == 0)
00165     tui_start_enabled = 1;
00166 
00167   if (interpreter_p && strcmp (interpreter_p, INTERP_CONSOLE) == 0)
00168     {
00169       xfree (interpreter_p);
00170       interpreter_p = xstrdup (INTERP_TUI);
00171     }
00172 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines