15796c8dcSSimon Schubert /* CLI Definitions for GDB, the GNU debugger.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 2002-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 "event-top.h"
235796c8dcSSimon Schubert #include "ui-out.h"
245796c8dcSSimon Schubert #include "cli-out.h"
255796c8dcSSimon Schubert #include "top.h" /* for "execute_command" */
265796c8dcSSimon Schubert #include "gdb_string.h"
275796c8dcSSimon Schubert #include "exceptions.h"
285796c8dcSSimon Schubert
295796c8dcSSimon Schubert struct ui_out *cli_uiout;
305796c8dcSSimon Schubert
31c50c785cSJohn Marino /* These are the ui_out and the interpreter for the console
32c50c785cSJohn Marino interpreter. */
335796c8dcSSimon Schubert
345796c8dcSSimon Schubert /* Longjmp-safe wrapper for "execute_command". */
355796c8dcSSimon Schubert static struct gdb_exception safe_execute_command (struct ui_out *uiout,
36c50c785cSJohn Marino char *command,
37c50c785cSJohn Marino int from_tty);
385796c8dcSSimon Schubert /* These implement the cli out interpreter: */
395796c8dcSSimon Schubert
405796c8dcSSimon Schubert static void *
cli_interpreter_init(struct interp * self,int top_level)41a45ae5f8SJohn Marino cli_interpreter_init (struct interp *self, int top_level)
425796c8dcSSimon Schubert {
435796c8dcSSimon Schubert return NULL;
445796c8dcSSimon Schubert }
455796c8dcSSimon Schubert
465796c8dcSSimon Schubert static int
cli_interpreter_resume(void * data)475796c8dcSSimon Schubert cli_interpreter_resume (void *data)
485796c8dcSSimon Schubert {
495796c8dcSSimon Schubert struct ui_file *stream;
505796c8dcSSimon Schubert
515796c8dcSSimon Schubert /*sync_execution = 1; */
525796c8dcSSimon Schubert
53c50c785cSJohn Marino /* gdb_setup_readline will change gdb_stdout. If the CLI was
54c50c785cSJohn Marino previously writing to gdb_stdout, then set it to the new
55c50c785cSJohn Marino gdb_stdout afterwards. */
565796c8dcSSimon Schubert
575796c8dcSSimon Schubert stream = cli_out_set_stream (cli_uiout, gdb_stdout);
585796c8dcSSimon Schubert if (stream != gdb_stdout)
595796c8dcSSimon Schubert {
605796c8dcSSimon Schubert cli_out_set_stream (cli_uiout, stream);
615796c8dcSSimon Schubert stream = NULL;
625796c8dcSSimon Schubert }
635796c8dcSSimon Schubert
645796c8dcSSimon Schubert gdb_setup_readline ();
655796c8dcSSimon Schubert
665796c8dcSSimon Schubert if (stream != NULL)
675796c8dcSSimon Schubert cli_out_set_stream (cli_uiout, gdb_stdout);
685796c8dcSSimon Schubert
695796c8dcSSimon Schubert return 1;
705796c8dcSSimon Schubert }
715796c8dcSSimon Schubert
725796c8dcSSimon Schubert static int
cli_interpreter_suspend(void * data)735796c8dcSSimon Schubert cli_interpreter_suspend (void *data)
745796c8dcSSimon Schubert {
755796c8dcSSimon Schubert gdb_disable_readline ();
765796c8dcSSimon Schubert return 1;
775796c8dcSSimon Schubert }
785796c8dcSSimon Schubert
795796c8dcSSimon Schubert /* Don't display the prompt if we are set quiet. */
805796c8dcSSimon Schubert static int
cli_interpreter_display_prompt_p(void * data)815796c8dcSSimon Schubert cli_interpreter_display_prompt_p (void *data)
825796c8dcSSimon Schubert {
835796c8dcSSimon Schubert if (interp_quiet_p (NULL))
845796c8dcSSimon Schubert return 0;
855796c8dcSSimon Schubert else
865796c8dcSSimon Schubert return 1;
875796c8dcSSimon Schubert }
885796c8dcSSimon Schubert
895796c8dcSSimon Schubert static struct gdb_exception
cli_interpreter_exec(void * data,const char * command_str)905796c8dcSSimon Schubert cli_interpreter_exec (void *data, const char *command_str)
915796c8dcSSimon Schubert {
925796c8dcSSimon Schubert struct ui_file *old_stream;
935796c8dcSSimon Schubert struct gdb_exception result;
945796c8dcSSimon Schubert
955796c8dcSSimon Schubert /* FIXME: cagney/2003-02-01: Need to const char *propogate
965796c8dcSSimon Schubert safe_execute_command. */
975796c8dcSSimon Schubert char *str = strcpy (alloca (strlen (command_str) + 1), command_str);
985796c8dcSSimon Schubert
99c50c785cSJohn Marino /* gdb_stdout could change between the time cli_uiout was
100c50c785cSJohn Marino initialized and now. Since we're probably using a different
101c50c785cSJohn Marino interpreter which has a new ui_file for gdb_stdout, use that one
102c50c785cSJohn Marino instead of the default.
1035796c8dcSSimon Schubert
104c50c785cSJohn Marino It is important that it gets reset everytime, since the user
105c50c785cSJohn Marino could set gdb to use a different interpreter. */
1065796c8dcSSimon Schubert old_stream = cli_out_set_stream (cli_uiout, gdb_stdout);
1075796c8dcSSimon Schubert result = safe_execute_command (cli_uiout, str, 1);
1085796c8dcSSimon Schubert cli_out_set_stream (cli_uiout, old_stream);
1095796c8dcSSimon Schubert return result;
1105796c8dcSSimon Schubert }
1115796c8dcSSimon Schubert
112a45ae5f8SJohn Marino static struct gdb_exception
safe_execute_command(struct ui_out * command_uiout,char * command,int from_tty)113a45ae5f8SJohn Marino safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
1145796c8dcSSimon Schubert {
115a45ae5f8SJohn Marino volatile struct gdb_exception e;
116a45ae5f8SJohn Marino struct ui_out *saved_uiout;
117cf7f2e2dSJohn Marino
118a45ae5f8SJohn Marino /* Save and override the global ``struct ui_out'' builder. */
119a45ae5f8SJohn Marino saved_uiout = current_uiout;
120a45ae5f8SJohn Marino current_uiout = command_uiout;
121a45ae5f8SJohn Marino
122a45ae5f8SJohn Marino TRY_CATCH (e, RETURN_MASK_ALL)
123a45ae5f8SJohn Marino {
124a45ae5f8SJohn Marino execute_command (command, from_tty);
1255796c8dcSSimon Schubert }
1265796c8dcSSimon Schubert
127a45ae5f8SJohn Marino /* Restore the global builder. */
128a45ae5f8SJohn Marino current_uiout = saved_uiout;
129cf7f2e2dSJohn Marino
1305796c8dcSSimon Schubert /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
1315796c8dcSSimon Schubert caller should print the exception. */
1325796c8dcSSimon Schubert exception_print (gdb_stderr, e);
1335796c8dcSSimon Schubert return e;
1345796c8dcSSimon Schubert }
1355796c8dcSSimon Schubert
136a45ae5f8SJohn Marino static struct ui_out *
cli_ui_out(struct interp * self)137a45ae5f8SJohn Marino cli_ui_out (struct interp *self)
138a45ae5f8SJohn Marino {
139a45ae5f8SJohn Marino return cli_uiout;
140a45ae5f8SJohn Marino }
1415796c8dcSSimon Schubert
1425796c8dcSSimon Schubert /* Standard gdb initialization hook. */
1435796c8dcSSimon Schubert extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
1445796c8dcSSimon Schubert
1455796c8dcSSimon Schubert void
_initialize_cli_interp(void)1465796c8dcSSimon Schubert _initialize_cli_interp (void)
1475796c8dcSSimon Schubert {
1485796c8dcSSimon Schubert static const struct interp_procs procs = {
1495796c8dcSSimon Schubert cli_interpreter_init, /* init_proc */
1505796c8dcSSimon Schubert cli_interpreter_resume, /* resume_proc */
1515796c8dcSSimon Schubert cli_interpreter_suspend, /* suspend_proc */
1525796c8dcSSimon Schubert cli_interpreter_exec, /* exec_proc */
153a45ae5f8SJohn Marino cli_interpreter_display_prompt_p, /* prompt_proc_p */
154a45ae5f8SJohn Marino cli_ui_out /* ui_out_proc */
1555796c8dcSSimon Schubert };
1565796c8dcSSimon Schubert struct interp *cli_interp;
1575796c8dcSSimon Schubert
1585796c8dcSSimon Schubert /* Create a default uiout builder for the CLI. */
1595796c8dcSSimon Schubert cli_uiout = cli_out_new (gdb_stdout);
160a45ae5f8SJohn Marino cli_interp = interp_new (INTERP_CONSOLE, &procs);
1615796c8dcSSimon Schubert
1625796c8dcSSimon Schubert interp_add (cli_interp);
1635796c8dcSSimon Schubert }
164