[RFA,1/4] Add previous_saved_command_line to allow a command to repeat a previous command.
Commit Message
Currently, a previous command can be repeated when the user types an
empty line. This is implemented in handle_line_of_input by
returning saved_command_line in case an empty line has been input.
If we want a command to repeat the previous command, we need to save
the previous saved_command_line, as when a command runs, the saved_command_line
already contains the current command line of the command being executed.
gdb/ChangeLog
2019-04-20 Philippe Waroquiers <philippe.waroquiers@skynet.be>
* top.h (previous_saved_command_line): New declaration.
* main.c (captured_main_1): Initialize previous_saved_command_line.
* event-top.c (handle_line_of_input): Copy saved_command_line into
previous_saved_command_line before replacing saved_command_line.
* command.h (repeat_previous): New declaration.
* top.c (previous_saved_command_line): New variable.
(repeat_previous): New function.
---
gdb/command.h | 1 +
gdb/event-top.c | 3 ++-
gdb/main.c | 1 +
gdb/top.c | 29 ++++++++++++++++++++++++++++-
gdb/top.h | 1 +
5 files changed, 33 insertions(+), 2 deletions(-)
Comments
>>>>> "Philippe" == Philippe Waroquiers <philippe.waroquiers@skynet.be> writes:
Philippe> +/* The previous last command line executed on the console. Used for command
Philippe> + repetitions when a command want to relaunch the previously launched
Philippe> + command. We need this as when a command is running, saved_command_line
Philippe> + already contains the line of the currently executing command. */
Philippe> +char *previous_saved_command_line;
Is there a way to make this static? Like have repeat_previous return
the desired value or something like that?
Philippe> +void
Philippe> +repeat_previous (void)
You can use () instead of (void) in C++.
Philippe> + /* We cannot free saved_command_line, as this line is being executed,
Philippe> + so swap it with previous_saved_command_line. */
Philippe> + previous_saved_command_line = saved_command_line;
Philippe> + saved_command_line = tmp;
Just use std::swap here.
Tom
@@ -449,6 +449,7 @@ extern void cmd_show_list (struct cmd_list_element *, int, const char *);
extern void error_no_arg (const char *) ATTRIBUTE_NORETURN;
extern void dont_repeat (void);
+extern void repeat_previous (void);
extern scoped_restore_tmpl<int> prevent_dont_repeat (void);
@@ -724,7 +724,8 @@ handle_line_of_input (struct buffer *cmd_line_buffer,
/* Save into global buffer if appropriate. */
if (repeat)
{
- xfree (saved_command_line);
+ xfree (previous_saved_command_line);
+ previous_saved_command_line = saved_command_line;
saved_command_line = xstrdup (cmd);
return saved_command_line;
}
@@ -500,6 +500,7 @@ captured_main_1 (struct captured_main_args *context)
notice_open_fds ();
saved_command_line = (char *) xstrdup ("");
+ previous_saved_command_line = (char *) xstrdup ("");
#ifdef __MINGW32__
/* Ensure stderr is unbuffered. A Cygwin pty or pipe is implemented
@@ -134,8 +134,13 @@ show_confirm (struct ui_file *file, int from_tty,
char *current_directory;
/* The last command line executed on the console. Used for command
- repetitions. */
+ repetitions when the user enters an empty line. */
char *saved_command_line;
+/* The previous last command line executed on the console. Used for command
+ repetitions when a command want to relaunch the previously launched
+ command. We need this as when a command is running, saved_command_line
+ already contains the line of the currently executing command. */
+char *previous_saved_command_line;
/* Nonzero if the current command is modified by "server ". This
affects things like recording into the command history, commands
@@ -711,6 +716,28 @@ dont_repeat (void)
*saved_command_line = 0;
}
+/* Command call this if they want to repeat the previous command.
+ Such commands repeating the previous command must indicate
+ to not repeat themselves, to avoid recursive repeat.
+ repeat_previous will mark the current command as not repeating,
+ and will restore saved_command_line from the
+ previous_saved_command_line, so that the currently executing
+ command can repeat it by using saved_command_line. */
+
+void
+repeat_previous (void)
+{
+ char *tmp = previous_saved_command_line;
+
+ /* Do not repeat this command, as this command is a repeating command. */
+ dont_repeat ();
+
+ /* We cannot free saved_command_line, as this line is being executed,
+ so swap it with previous_saved_command_line. */
+ previous_saved_command_line = saved_command_line;
+ saved_command_line = tmp;
+}
+
/* Prevent dont_repeat from working, and return a cleanup that
restores the previous state. */
@@ -218,6 +218,7 @@ extern void ui_unregister_input_event_handler (struct ui *ui);
/* From top.c. */
extern char *saved_command_line;
+extern char *previous_saved_command_line;
extern int confirm;
extern int inhibit_gdbinit;
extern const char gdbinit[];