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