/* Prototypes for local functions. */
static enum command_control_type
-recurse_read_control_structure (char * (*read_next_line_func) (void),
- struct command_line *current_cmd,
- void (*validator)(char *, void *),
- void *closure);
+recurse_read_control_structure
+ (gdb::function_view<const char * ()> read_next_line_func,
+ struct command_line *current_cmd,
+ gdb::function_view<void (const char *)> validator);
+
+static void do_define_command (const char *comname, int from_tty,
+ const counted_command_line *commands);
static char *read_next_line (void);
case compile_control:
case python_control:
case guile_control:
+ case define_control:
return 1;
default:
return 0;
static struct command_line *
build_command_line (enum command_control_type type, const char *args)
{
- if ((args == NULL || *args == '\0')
- && (type == if_control || type == while_control))
- error (_("if/while commands require arguments."));
+ if (args == NULL || *args == '\0')
+ {
+ if (type == if_control)
+ error (_("if command requires an argument."));
+ else if (type == while_control)
+ error (_("while command requires an argument."));
+ else if (type == define_control)
+ error (_("define command requires an argument."));
+ }
gdb_assert (args != NULL);
return new struct command_line (type, xstrdup (args));
command_lines_deleter ());
/* Read in the body of this command. */
- if (recurse_read_control_structure (read_next_line, cmd.get (), 0, 0)
+ if (recurse_read_control_structure (read_next_line, cmd.get (), 0)
== invalid_control)
{
warning (_("Error reading in canned sequence of commands."));
}
}
+/* See cli-script.h. */
+
+void
+execute_control_commands (struct command_line *cmdlines, int from_tty)
+{
+ /* Set the instream to 0, indicating execution of a
+ user-defined function. */
+ scoped_restore restore_instream
+ = make_scoped_restore (¤t_ui->instream, nullptr);
+ scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
+ scoped_restore save_nesting
+ = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
+
+ while (cmdlines)
+ {
+ enum command_control_type ret = execute_control_command (cmdlines,
+ from_tty);
+ if (ret != simple_control && ret != break_control)
+ {
+ warning (_("Error executing canned sequence of commands."));
+ break;
+ }
+ cmdlines = cmdlines->next;
+ }
+}
+
+/* See cli-script.h. */
+
+std::string
+execute_control_commands_to_string (struct command_line *commands,
+ int from_tty)
+{
+ /* GDB_STDOUT should be better already restored during these
+ restoration callbacks. */
+ set_batch_flag_and_restore_page_info save_page_info;
+
+ string_file str_file;
+
+ {
+ current_uiout->redirect (&str_file);
+ ui_out_redirect_pop redirect_popper (current_uiout);
+
+ scoped_restore save_stdout
+ = make_scoped_restore (&gdb_stdout, &str_file);
+ scoped_restore save_stderr
+ = make_scoped_restore (&gdb_stderr, &str_file);
+ scoped_restore save_stdlog
+ = make_scoped_restore (&gdb_stdlog, &str_file);
+ scoped_restore save_stdtarg
+ = make_scoped_restore (&gdb_stdtarg, &str_file);
+ scoped_restore save_stdtargerr
+ = make_scoped_restore (&gdb_stdtargerr, &str_file);
+
+ execute_control_commands (commands, from_tty);
+ }
+
+ return std::move (str_file.string ());
+}
+
void
execute_user_command (struct cmd_list_element *c, const char *args)
{
- struct ui *ui = current_ui;
counted_command_line cmdlines_copy;
- enum command_control_type ret;
extern unsigned int max_user_call_depth;
/* Ensure that the user commands can't be deleted while they are
if (user_args_stack.size () > max_user_call_depth)
error (_("Max user call depth exceeded -- command aborted."));
- /* Set the instream to 0, indicating execution of a
- user-defined function. */
- scoped_restore restore_instream
- = make_scoped_restore (&ui->instream, nullptr);
-
- scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
-
- scoped_restore save_nesting
- = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
- while (cmdlines)
- {
- ret = execute_control_command (cmdlines);
- if (ret != simple_control && ret != break_control)
- {
- warning (_("Error executing canned sequence of commands."));
- break;
- }
- cmdlines = cmdlines->next;
- }
+ execute_control_commands (cmdlines, 0);
}
/* This function is called every time GDB prints a prompt. It ensures
/* Helper for execute_control_command. */
static enum command_control_type
-execute_control_command_1 (struct command_line *cmd)
+execute_control_command_1 (struct command_line *cmd, int from_tty)
{
struct command_line *current;
struct value *val;
{
/* A simple command, execute it and return. */
std::string new_line = insert_user_defined_cmd_args (cmd->line);
- execute_command (new_line.c_str (), 0);
+ execute_command (new_line.c_str (), from_tty);
ret = cmd->control_type;
break;
}
{
scoped_restore save_nesting
= make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
- ret = execute_control_command_1 (current);
+ ret = execute_control_command_1 (current, from_tty);
/* If we got an error, or a "break" command, then stop
looping. */
{
scoped_restore save_nesting
= make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
- ret = execute_control_command_1 (current);
+ ret = execute_control_command_1 (current, from_tty);
/* If we got an error, get out. */
if (ret != simple_control)
ret = simple_control;
break;
+ case define_control:
+ print_command_trace ("define %s", cmd->line);
+ do_define_command (cmd->line, 0, &cmd->body_list_0);
+ ret = simple_control;
+ break;
+
case python_control:
case guile_control:
{
}
enum command_control_type
-execute_control_command (struct command_line *cmd)
+execute_control_command (struct command_line *cmd, int from_tty)
{
/* Make sure we use the console uiout. It's possible that we are executing
breakpoint commands while running the MI interpreter. */
scoped_restore save_uiout
= make_scoped_restore (¤t_uiout, interp_ui_out (console));
- return execute_control_command_1 (cmd);
+ return execute_control_command_1 (cmd, from_tty);
}
/* Like execute_control_command, but first set
Otherwise, only "end" is recognized. */
static enum misc_command_type
-process_next_line (char *p, struct command_line **command, int parse_commands,
- void (*validator)(char *, void *), void *closure)
+process_next_line (const char *p, struct command_line **command,
+ int parse_commands,
+ gdb::function_view<void (const char *)> validator)
+
{
- char *p_end;
- char *p_start;
+ const char *p_end;
+ const char *p_start;
int not_handled = 0;
/* Not sure what to do here. */
{
*command = build_command_line (commands_control, line_first_arg (p));
}
+ else if (command_name_equals (cmd, "define"))
+ *command = build_command_line (define_control, line_first_arg (p));
else if (command_name_equals (cmd, "python") && !inline_cmd)
{
/* Note that we ignore the inline "python command" form
if (validator)
{
-
TRY
{
- validator ((*command)->line, closure);
+ validator ((*command)->line);
}
CATCH (ex, RETURN_MASK_ALL)
{
obtain lines of the command. */
static enum command_control_type
-recurse_read_control_structure (char * (*read_next_line_func) (void),
+recurse_read_control_structure (gdb::function_view<const char * ()> read_next_line_func,
struct command_line *current_cmd,
- void (*validator)(char *, void *),
- void *closure)
+ gdb::function_view<void (const char *)> validator)
{
enum misc_command_type val;
enum command_control_type ret;
current_cmd->control_type != python_control
&& current_cmd->control_type != guile_control
&& current_cmd->control_type != compile_control,
- validator, closure);
+ validator);
/* Just skip blanks and comments. */
if (val == nop_command)
{
control_level++;
ret = recurse_read_control_structure (read_next_line_func, next,
- validator, closure);
+ validator);
control_level--;
if (ret != simple_control)
counted_command_line
read_command_lines (const char *prompt_arg, int from_tty, int parse_commands,
- void (*validator)(char *, void *), void *closure)
+ gdb::function_view<void (const char *)> validator)
{
if (from_tty && input_interactive_p (current_ui))
{
counted_command_line head (nullptr, command_lines_deleter ());
if (current_interp_named_p (INTERP_CONSOLE))
head = read_command_lines_1 (read_next_line, parse_commands,
- validator, closure);
+ validator);
else
{
scoped_restore_interp interp_restorer (INTERP_CONSOLE);
head = read_command_lines_1 (read_next_line, parse_commands,
- validator, closure);
+ validator);
}
if (from_tty && input_interactive_p (current_ui)
obtained using READ_NEXT_LINE_FUNC. */
counted_command_line
-read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
- void (*validator)(char *, void *), void *closure)
+read_command_lines_1 (gdb::function_view<const char * ()> read_next_line_func,
+ int parse_commands,
+ gdb::function_view<void (const char *)> validator)
{
struct command_line *tail, *next;
counted_command_line head (nullptr, command_lines_deleter ());
{
dont_repeat ();
val = process_next_line (read_next_line_func (), &next, parse_commands,
- validator, closure);
+ validator);
/* Ignore blank lines or comments. */
if (val == nop_command)
{
control_level++;
ret = recurse_read_control_structure (read_next_line_func, next,
- validator, closure);
+ validator);
control_level--;
if (ret == invalid_control)
{
}
+/* Define a user-defined command. If COMMANDS is NULL, then this is a
+ top-level call and the commands will be read using
+ read_command_lines. Otherwise, it is a "define" command in an
+ existing command and the commands are provided. In the
+ non-top-level case, various prompts and warnings are disabled. */
+
static void
-define_command (const char *comname, int from_tty)
+do_define_command (const char *comname, int from_tty,
+ const counted_command_line *commands)
{
enum cmd_hook_type
{
if (c && strcmp (comname, c->name) != 0)
c = 0;
- if (c)
+ if (c && commands == nullptr)
{
int q;
hookc = lookup_cmd (&tem, *list, "", -1, 0);
if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
hookc = 0;
- if (!hookc)
+ if (!hookc && commands == nullptr)
{
warning (_("Your new `%s' command does not "
"hook any existing command."),
comname = xstrdup (comname);
- std::string prompt
- = string_printf ("Type commands for definition of \"%s\".", comfull);
- counted_command_line cmds = read_command_lines (prompt.c_str (), from_tty,
- 1, 0, 0);
+ counted_command_line cmds;
+ if (commands == nullptr)
+ {
+ std::string prompt
+ = string_printf ("Type commands for definition of \"%s\".", comfull);
+ cmds = read_command_lines (prompt.c_str (), from_tty, 1, 0);
+ }
+ else
+ cmds = *commands;
newc = add_cmd (comname, class_user, user_defined_command,
(c && c->theclass == class_user)
}
}
+static void
+define_command (const char *comname, int from_tty)
+{
+ do_define_command (comname, from_tty, nullptr);
+}
+
static void
document_command (const char *comname, int from_tty)
{
std::string prompt = string_printf ("Type documentation for \"%s\".",
comfull);
counted_command_line doclines = read_command_lines (prompt.c_str (),
- from_tty, 0, 0, 0);
+ from_tty, 0, 0);
if (c->doc)
xfree ((char *) c->doc);