2011-08-04 Pedro Alves <pedro@codesourcery.com>
[platform/upstream/binutils.git] / gdb / cli / cli-interp.c
1 /* CLI Definitions for GDB, the GNU debugger.
2
3    Copyright (c) 2002, 2003, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "interps.h"
23 #include "wrapper.h"
24 #include "event-top.h"
25 #include "ui-out.h"
26 #include "cli-out.h"
27 #include "top.h"                /* for "execute_command" */
28 #include "gdb_string.h"
29 #include "exceptions.h"
30
31 struct ui_out *cli_uiout;
32
33 /* These are the ui_out and the interpreter for the console
34    interpreter.  */
35
36 /* Longjmp-safe wrapper for "execute_command".  */
37 static struct gdb_exception safe_execute_command (struct ui_out *uiout,
38                                                   char *command, 
39                                                   int from_tty);
40 /* These implement the cli out interpreter: */
41
42 static void *
43 cli_interpreter_init (int top_level)
44 {
45   return NULL;
46 }
47
48 static int
49 cli_interpreter_resume (void *data)
50 {
51   struct ui_file *stream;
52
53   /*sync_execution = 1; */
54
55   /* gdb_setup_readline will change gdb_stdout.  If the CLI was
56      previously writing to gdb_stdout, then set it to the new
57      gdb_stdout afterwards.  */
58
59   stream = cli_out_set_stream (cli_uiout, gdb_stdout);
60   if (stream != gdb_stdout)
61     {
62       cli_out_set_stream (cli_uiout, stream);
63       stream = NULL;
64     }
65
66   gdb_setup_readline ();
67
68   if (stream != NULL)
69     cli_out_set_stream (cli_uiout, gdb_stdout);
70
71   return 1;
72 }
73
74 static int
75 cli_interpreter_suspend (void *data)
76 {
77   gdb_disable_readline ();
78   return 1;
79 }
80
81 /* Don't display the prompt if we are set quiet.  */
82 static int
83 cli_interpreter_display_prompt_p (void *data)
84 {
85   if (interp_quiet_p (NULL))
86     return 0;
87   else
88     return 1;
89 }
90
91 static struct gdb_exception
92 cli_interpreter_exec (void *data, const char *command_str)
93 {
94   struct ui_file *old_stream;
95   struct gdb_exception result;
96
97   /* FIXME: cagney/2003-02-01: Need to const char *propogate
98      safe_execute_command.  */
99   char *str = strcpy (alloca (strlen (command_str) + 1), command_str);
100
101   /* gdb_stdout could change between the time cli_uiout was
102      initialized and now.  Since we're probably using a different
103      interpreter which has a new ui_file for gdb_stdout, use that one
104      instead of the default.
105
106      It is important that it gets reset everytime, since the user
107      could set gdb to use a different interpreter.  */
108   old_stream = cli_out_set_stream (cli_uiout, gdb_stdout);
109   result = safe_execute_command (cli_uiout, str, 1);
110   cli_out_set_stream (cli_uiout, old_stream);
111   return result;
112 }
113
114 static struct gdb_exception
115 safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
116 {
117   volatile struct gdb_exception e;
118   struct ui_out *saved_uiout;
119
120   /* Save and override the global ``struct ui_out'' builder.  */
121   saved_uiout = current_uiout;
122   current_uiout = command_uiout;
123
124   TRY_CATCH (e, RETURN_MASK_ALL)
125     {
126       execute_command (command, from_tty);
127     }
128
129   /* Restore the global builder.  */
130   current_uiout = saved_uiout;
131
132   /* FIXME: cagney/2005-01-13: This shouldn't be needed.  Instead the
133      caller should print the exception.  */
134   exception_print (gdb_stderr, e);
135   return e;
136 }
137
138
139 /* Standard gdb initialization hook.  */
140 extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
141
142 void
143 _initialize_cli_interp (void)
144 {
145   static const struct interp_procs procs = {
146     cli_interpreter_init,       /* init_proc */
147     cli_interpreter_resume,     /* resume_proc */
148     cli_interpreter_suspend,    /* suspend_proc */
149     cli_interpreter_exec,       /* exec_proc */
150     cli_interpreter_display_prompt_p    /* prompt_proc_p */
151   };
152   struct interp *cli_interp;
153
154   /* Create a default uiout builder for the CLI.  */
155   cli_uiout = cli_out_new (gdb_stdout);
156   cli_interp = interp_new (INTERP_CONSOLE, NULL, cli_uiout, &procs);
157
158   interp_add (cli_interp);
159 }