/* GDB CLI command scripting.
- Copyright (C) 1986-2017 Free Software Foundation, Inc.
+ Copyright (C) 1986-2018 Free Software Foundation, Inc.
This file is part of GDB.
#include "extension.h"
#include "interps.h"
#include "compile/compile.h"
+#include "common/gdb_string_view.h"
#include <vector>
/* This is to prevent certain commands being printed twice. */
static int suppress_next_print_command_trace = 0;
-/* A non-owning slice of a string. */
-
-struct string_view
-{
- string_view (const char *str_, size_t len_)
- : str (str_), len (len_)
- {}
-
- const char *str;
- size_t len;
-};
-
/* Structure for arguments to user defined functions. */
class user_args
std::string m_command_line;
/* The arguments. Each element points inside M_COMMAND_LINE. */
- std::vector<string_view> m_args;
+ std::vector<gdb::string_view> m_args;
};
/* The stack of arguments passed to user defined functions. We need a
static struct command_line *
build_command_line (enum command_control_type type, const char *args)
{
- struct command_line *cmd;
-
- if (args == NULL && (type == if_control || type == while_control))
+ if ((args == NULL || *args == '\0')
+ && (type == if_control || type == while_control))
error (_("if/while commands require arguments."));
gdb_assert (args != NULL);
- cmd = XNEW (struct command_line);
- cmd->next = NULL;
- cmd->control_type = type;
-
- cmd->body_count = 1;
- cmd->body_list = XCNEWVEC (struct command_line *, cmd->body_count);
- cmd->line = xstrdup (args);
-
- return cmd;
+ return new struct command_line (type, xstrdup (args));
}
/* Build and return a new command structure for the control commands
such as "if" and "while". */
-command_line_up
+counted_command_line
get_command_line (enum command_control_type type, const char *arg)
{
/* Allocate and build a new command line structure. */
- command_line_up cmd (build_command_line (type, arg));
+ counted_command_line cmd (build_command_line (type, arg),
+ command_lines_deleter ());
/* Read in the body of this command. */
if (recurse_read_control_structure (read_next_line, cmd.get (), 0, 0)
else
uiout->field_string (NULL, list->line);
uiout->text ("\n");
- print_command_lines (uiout, *list->body_list, depth + 1);
+ print_command_lines (uiout, list->body_list_0.get (), depth + 1);
if (depth)
uiout->spaces (2 * depth);
uiout->field_string (NULL, "end");
uiout->field_fmt (NULL, "if %s", list->line);
uiout->text ("\n");
/* The true arm. */
- print_command_lines (uiout, list->body_list[0], depth + 1);
+ print_command_lines (uiout, list->body_list_0.get (), depth + 1);
/* Show the false arm if it exists. */
- if (list->body_count == 2)
+ if (list->body_list_1 != nullptr)
{
if (depth)
uiout->spaces (2 * depth);
uiout->field_string (NULL, "else");
uiout->text ("\n");
- print_command_lines (uiout, list->body_list[1], depth + 1);
+ print_command_lines (uiout, list->body_list_1.get (), depth + 1);
}
if (depth)
else
uiout->field_string (NULL, "commands");
uiout->text ("\n");
- print_command_lines (uiout, *list->body_list, depth + 1);
+ print_command_lines (uiout, list->body_list_0.get (), depth + 1);
if (depth)
uiout->spaces (2 * depth);
uiout->field_string (NULL, "end");
uiout->field_string (NULL, "python");
uiout->text ("\n");
/* Don't indent python code at all. */
- print_command_lines (uiout, *list->body_list, 0);
+ print_command_lines (uiout, list->body_list_0.get (), 0);
if (depth)
uiout->spaces (2 * depth);
uiout->field_string (NULL, "end");
{
uiout->field_string (NULL, "compile expression");
uiout->text ("\n");
- print_command_lines (uiout, *list->body_list, 0);
+ print_command_lines (uiout, list->body_list_0.get (), 0);
if (depth)
uiout->spaces (2 * depth);
uiout->field_string (NULL, "end");
{
uiout->field_string (NULL, "guile");
uiout->text ("\n");
- print_command_lines (uiout, *list->body_list, depth + 1);
+ print_command_lines (uiout, list->body_list_0.get (), depth + 1);
if (depth)
uiout->spaces (2 * depth);
uiout->field_string (NULL, "end");
/* Handle pre-post hooks. */
-static void
-clear_hook_in_cleanup (void *data)
+class scoped_restore_hook_in
{
- struct cmd_list_element *c = (struct cmd_list_element *) data;
+public:
- c->hook_in = 0; /* Allow hook to work again once it is complete. */
-}
+ scoped_restore_hook_in (struct cmd_list_element *c)
+ : m_cmd (c)
+ {
+ }
+
+ ~scoped_restore_hook_in ()
+ {
+ m_cmd->hook_in = 0;
+ }
+
+ scoped_restore_hook_in (const scoped_restore_hook_in &) = delete;
+ scoped_restore_hook_in &operator= (const scoped_restore_hook_in &) = delete;
+
+private:
+
+ struct cmd_list_element *m_cmd;
+};
void
execute_cmd_pre_hook (struct cmd_list_element *c)
{
if ((c->hook_pre) && (!c->hook_in))
{
- struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
+ scoped_restore_hook_in restore_hook (c);
c->hook_in = 1; /* Prevent recursive hooking. */
- execute_user_command (c->hook_pre, (char *) 0);
- do_cleanups (cleanups);
+ execute_user_command (c->hook_pre, nullptr);
}
}
{
if ((c->hook_post) && (!c->hook_in))
{
- struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
-
+ scoped_restore_hook_in restore_hook (c);
c->hook_in = 1; /* Prevent recursive hooking. */
- execute_user_command (c->hook_post, (char *) 0);
- do_cleanups (cleanups);
+ execute_user_command (c->hook_post, nullptr);
}
}
void
-execute_user_command (struct cmd_list_element *c, char *args)
+execute_user_command (struct cmd_list_element *c, const char *args)
{
struct ui *ui = current_ui;
- struct command_line *cmdlines;
+ counted_command_line cmdlines_copy;
enum command_control_type ret;
extern unsigned int max_user_call_depth;
- cmdlines = c->user_commands;
- if (cmdlines == 0)
+ /* Ensure that the user commands can't be deleted while they are
+ executing. */
+ cmdlines_copy = c->user_commands;
+ if (cmdlines_copy == 0)
/* Null command */
return;
+ struct command_line *cmdlines = cmdlines_copy.get ();
scoped_user_args_level push_user_args (args);
printf_filtered ("%s\n", cmd);
}
-enum command_control_type
-execute_control_command (struct command_line *cmd)
+/* Helper for execute_control_command. */
+
+static enum command_control_type
+execute_control_command_1 (struct command_line *cmd)
{
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[0], 0);
+ execute_command (new_line.c_str (), 0);
ret = cmd->control_type;
break;
}
break;
/* Execute the body of the while statement. */
- current = *cmd->body_list;
+ current = cmd->body_list_0.get ();
while (current)
{
scoped_restore save_nesting
= make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
- ret = execute_control_command (current);
+ ret = execute_control_command_1 (current);
/* If we got an error, or a "break" command, then stop
looping. */
/* Choose which arm to take commands from based on the value
of the conditional expression. */
if (value_true (val))
- current = *cmd->body_list;
- else if (cmd->body_count == 2)
- current = *(cmd->body_list + 1);
+ current = cmd->body_list_0.get ();
+ else if (cmd->body_list_1 != nullptr)
+ current = cmd->body_list_1.get ();
value_free_to_mark (val_mark);
/* Execute commands in the given arm. */
{
scoped_restore save_nesting
= make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
- ret = execute_control_command (current);
+ ret = execute_control_command_1 (current);
/* If we got an error, get out. */
if (ret != simple_control)
return ret;
}
+enum command_control_type
+execute_control_command (struct command_line *cmd)
+{
+ /* Make sure we use the console uiout. It's possible that we are executing
+ breakpoint commands while running the MI interpreter. */
+ interp *console = interp_lookup (current_ui, INTERP_CONSOLE);
+ scoped_restore save_uiout
+ = make_scoped_restore (¤t_uiout, interp_ui_out (console));
+
+ return execute_control_command_1 (cmd);
+}
+
/* Like execute_control_command, but first set
suppress_next_print_command_trace. */
loop condition is nonzero. */
static void
-while_command (char *arg, int from_tty)
+while_command (const char *arg, int from_tty)
{
control_level = 1;
- command_line_up command = get_command_line (while_control, arg);
+ counted_command_line command = get_command_line (while_control, arg);
if (command == NULL)
return;
on the value of the if conditional. */
static void
-if_command (char *arg, int from_tty)
+if_command (const char *arg, int from_tty)
{
control_level = 1;
- command_line_up command = get_command_line (if_control, arg);
+ counted_command_line command = get_command_line (if_control, arg);
if (command == NULL)
return;
error (_("Missing argument %ld in user function."), i);
else
{
- new_line.append (m_args[i].str, m_args[i].len);
+ new_line.append (m_args[i].data (), m_args[i].length ());
line = tmp;
}
}
}
\f
-/* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
- code bodies. This is typically used when we encounter an "else"
- clause for an "if" command. */
-
-static void
-realloc_body_list (struct command_line *command, int new_length)
-{
- int n;
- struct command_line **body_list;
-
- n = command->body_count;
-
- /* Nothing to do? */
- if (new_length <= n)
- return;
-
- body_list = XCNEWVEC (struct command_line *, new_length);
-
- memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
-
- xfree (command->body_list);
- command->body_list = body_list;
- command->body_count = new_length;
-}
-
/* Read next line from stdin. Passed to read_command_line_1 and
recurse_read_control_structure whenever we need to read commands
from stdin. */
{
const char *first_arg = p + find_command_name_length (p);
- return skip_spaces_const (first_arg);
+ return skip_spaces (first_arg);
}
/* Process one input line. If the command is an "end", return such an
const char *cmd_name = p;
struct cmd_list_element *cmd
= lookup_cmd_1 (&cmd_name, cmdlist, NULL, 1);
- cmd_name = skip_spaces_const (cmd_name);
+ cmd_name = skip_spaces (cmd_name);
bool inline_cmd = *cmd_name != '\0';
/* If commands are parsed, we skip initial spaces. Otherwise,
*command = build_command_line (guile_control, "");
}
else if (p_end - p == 10 && startswith (p, "loop_break"))
- {
- *command = XNEW (struct command_line);
- (*command)->next = NULL;
- (*command)->line = NULL;
- (*command)->control_type = break_control;
- (*command)->body_count = 0;
- (*command)->body_list = NULL;
- }
+ *command = new struct command_line (break_control);
else if (p_end - p == 13 && startswith (p, "loop_continue"))
- {
- *command = XNEW (struct command_line);
- (*command)->next = NULL;
- (*command)->line = NULL;
- (*command)->control_type = continue_control;
- (*command)->body_count = 0;
- (*command)->body_list = NULL;
- }
+ *command = new struct command_line (continue_control);
else
not_handled = 1;
}
if (!parse_commands || not_handled)
{
/* A normal command. */
- *command = XNEW (struct command_line);
- (*command)->next = NULL;
- (*command)->line = savestring (p, p_end - p);
- (*command)->control_type = simple_control;
- (*command)->body_count = 0;
- (*command)->body_list = NULL;
+ *command = new struct command_line (simple_control,
+ savestring (p, p_end - p));
}
if (validator)
}
CATCH (ex, RETURN_MASK_ALL)
{
- xfree (*command);
+ free_command_lines (command);
throw_exception (ex);
}
END_CATCH
void (*validator)(char *, void *),
void *closure)
{
- int current_body, i;
enum misc_command_type val;
enum command_control_type ret;
struct command_line **body_ptr, *child_tail, *next;
+ counted_command_line *current_body = ¤t_cmd->body_list_0;
child_tail = NULL;
- current_body = 1;
/* Sanity checks. */
if (current_cmd->control_type == simple_control)
error (_("Recursed on a simple control type."));
- if (current_body > current_cmd->body_count)
- error (_("Allocated body is smaller than this command type needs."));
-
/* Read lines from the input stream and build control structures. */
while (1)
{
if (val == else_command)
{
if (current_cmd->control_type == if_control
- && current_body == 1)
+ && current_body == ¤t_cmd->body_list_0)
{
- realloc_body_list (current_cmd, 2);
- current_body = 2;
+ current_body = ¤t_cmd->body_list_1;
child_tail = NULL;
continue;
}
child_tail->next = next;
}
else
- {
- body_ptr = current_cmd->body_list;
- for (i = 1; i < current_body; i++)
- body_ptr++;
-
- *body_ptr = next;
-
- }
+ *current_body = counted_command_line (next, command_lines_deleter ());
child_tail = next;
return ret;
}
-static void
-restore_interp (void *arg)
-{
- interp_set_temp (interp_name ((struct interp *)arg));
-}
-
/* Read lines from the input stream and accumulate them in a chain of
struct command_line's, which is then returned. For input from a
terminal, the special command "end" is used to mark the end of the
#define END_MESSAGE "End with a line saying just \"end\"."
-command_line_up
+counted_command_line
read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
void (*validator)(char *, void *), void *closure)
{
/* Reading commands assumes the CLI behavior, so temporarily
override the current interpreter with CLI. */
- command_line_up head;
+ 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);
else
{
- struct interp *old_interp = interp_set_temp (INTERP_CONSOLE);
- struct cleanup *old_chain = make_cleanup (restore_interp, old_interp);
+ scoped_restore_interp interp_restorer (INTERP_CONSOLE);
head = read_command_lines_1 (read_next_line, parse_commands,
validator, closure);
- do_cleanups (old_chain);
}
if (from_tty && input_interactive_p (current_ui)
/* Act the same way as read_command_lines, except that each new line is
obtained using READ_NEXT_LINE_FUNC. */
-command_line_up
+counted_command_line
read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
void (*validator)(char *, void *), void *closure)
{
struct command_line *tail, *next;
- command_line_up head;
+ counted_command_line head (nullptr, command_lines_deleter ());
enum command_control_type ret;
enum misc_command_type val;
}
else
{
- head.reset (next);
+ head = counted_command_line (next, command_lines_deleter ());
}
tail = next;
}
{
struct command_line *l = *lptr;
struct command_line *next;
- struct command_line **blist;
- int i;
while (l)
{
- if (l->body_count > 0)
- {
- blist = l->body_list;
- for (i = 0; i < l->body_count; i++, blist++)
- free_command_lines (blist);
- }
next = l->next;
- xfree (l->line);
- xfree (l);
+ delete l;
l = next;
}
*lptr = NULL;
}
-
-command_line_up
-copy_command_lines (struct command_line *cmds)
-{
- struct command_line *result = NULL;
-
- if (cmds)
- {
- result = XNEW (struct command_line);
-
- result->next = copy_command_lines (cmds->next).release ();
- result->line = xstrdup (cmds->line);
- result->control_type = cmds->control_type;
- result->body_count = cmds->body_count;
- if (cmds->body_count > 0)
- {
- int i;
-
- result->body_list = XNEWVEC (struct command_line *, cmds->body_count);
-
- for (i = 0; i < cmds->body_count; i++)
- result->body_list[i]
- = copy_command_lines (cmds->body_list[i]).release ();
- }
- else
- result->body_list = NULL;
- }
-
- return command_line_up (result);
-}
\f
/* Validate that *COMNAME is a valid name for a command. Return the
containing command list, in case it starts with a prefix command.
prefix. */
static struct cmd_list_element **
-validate_comname (char **comname)
+validate_comname (const char **comname)
{
struct cmd_list_element **list = &cmdlist;
- char *p, *last_word;
+ const char *p, *last_word;
if (*comname == 0)
error_no_arg (_("name of command to define"));
if (last_word != *comname)
{
struct cmd_list_element *c;
- char saved_char;
- const char *tem = *comname;
/* Separate the prefix and the command. */
- saved_char = last_word[-1];
- last_word[-1] = '\0';
+ std::string prefix (*comname, last_word - 1);
+ const char *tem = prefix.c_str ();
c = lookup_cmd (&tem, cmdlist, "", 0, 1);
if (c->prefixlist == NULL)
- error (_("\"%s\" is not a prefix command."), *comname);
+ error (_("\"%s\" is not a prefix command."), prefix.c_str ());
list = c->prefixlist;
- last_word[-1] = saved_char;
*comname = last_word;
}
/* This is just a placeholder in the command data structures. */
static void
-user_defined_command (char *ignore, int from_tty)
+user_defined_command (const char *ignore, int from_tty)
{
}
static void
-define_command (char *comname, int from_tty)
+define_command (const char *comname, int from_tty)
{
#define MAX_TMPBUF 128
enum cmd_hook_type
CMD_POST_HOOK
};
struct cmd_list_element *c, *newc, *hookc = 0, **list;
- char *tem, *comfull;
- const char *tem_c;
+ const char *tem, *comfull;
char tmpbuf[MAX_TMPBUF];
int hook_type = CMD_NO_HOOK;
int hook_name_size = 0;
list = validate_comname (&comname);
/* Look it up, and verify that we got an exact match. */
- tem_c = comname;
- c = lookup_cmd (&tem_c, *list, "", -1, 1);
+ tem = comname;
+ c = lookup_cmd (&tem, *list, "", -1, 1);
if (c && strcmp (comname, c->name) != 0)
c = 0;
if (hook_type != CMD_NO_HOOK)
{
/* Look up cmd it hooks, and verify that we got an exact match. */
- tem_c = comname + hook_name_size;
- hookc = lookup_cmd (&tem_c, *list, "", -1, 0);
+ tem = comname + hook_name_size;
+ hookc = lookup_cmd (&tem, *list, "", -1, 0);
if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
hookc = 0;
if (!hookc)
comname = xstrdup (comname);
- /* If the rest of the commands will be case insensitive, this one
- should behave in the same manner. */
- for (tem = comname; *tem; tem++)
- if (isupper (*tem))
- *tem = tolower (*tem);
-
xsnprintf (tmpbuf, sizeof (tmpbuf),
"Type commands for definition of \"%s\".", comfull);
- command_line_up cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
-
- if (c && c->theclass == class_user)
- free_command_lines (&c->user_commands);
+ counted_command_line cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
newc = add_cmd (comname, class_user, user_defined_command,
(c && c->theclass == class_user)
? c->doc : xstrdup ("User-defined."), list);
- newc->user_commands = cmds.release ();
+ newc->user_commands = std::move (cmds);
/* If this new command is a hook, then mark both commands as being
tied. */
}
static void
-document_command (char *comname, int from_tty)
+document_command (const char *comname, int from_tty)
{
struct cmd_list_element *c, **list;
const char *tem;
- char *comfull;
+ const char *comfull;
char tmpbuf[128];
comfull = comname;
xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
comfull);
- command_line_up doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
+ counted_command_line doclines = read_command_lines (tmpbuf, from_tty,
+ 0, 0, 0);
if (c->doc)
xfree ((char *) c->doc);
return;
}
- cmdlines = c->user_commands;
+ cmdlines = c->user_commands.get ();
fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
if (!cmdlines)
fputs_filtered ("\n", stream);
}
-\f
-
-initialize_file_ftype _initialize_cli_script;
-
void
_initialize_cli_script (void)
{