[RFA,7/8] Add truncate_repeat_arguments function

Message ID 20171013205950.22943-8-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Oct. 13, 2017, 8:59 p.m. UTC
  The "x" and "list" commands have special repetition behavior:
repeating the command doesn't re-run it with the same arguments, but
rather advances (through memory or the program listing).

This is currenty implemented by modifying the passed-in argument; but
that won't work properly with const arguments (and seems pretty
obscure besides).

This patch adds a new "truncate_repeat_arguments" function and changes
the relevant places to call it.

2017-10-13  Tom Tromey  <tom@tromey.com>

	* printcmd.c (x_command): Call truncate_repeat_arguments.
	* cli/cli-cmds.c (list_command): Call truncate_repeat_arguments.
	* top.c (should_truncate_repeat_arguments): New global.
	(truncate_repeat_arguments): New function.
	(execute_command): Handle should_truncate_repeat_arguments.
	* command.h (truncate_repeat_arguments): Declare.
---
 gdb/ChangeLog      |  9 +++++++++
 gdb/cli/cli-cmds.c |  2 +-
 gdb/command.h      |  5 +++++
 gdb/printcmd.c     |  2 +-
 gdb/top.c          | 19 +++++++++++++++++++
 5 files changed, 35 insertions(+), 2 deletions(-)
  

Comments

Tom Tromey Oct. 14, 2017, 4:48 a.m. UTC | #1
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

Tom> The "x" and "list" commands have special repetition behavior:
Tom> repeating the command doesn't re-run it with the same arguments, but
Tom> rather advances (through memory or the program listing).

I found a couple more functions that do something similar, but not
purely truncating their arguments.  Both show_commands and show_values
do:

  if (from_tty && num_exp)
    {
      num_exp[0] = '+';
      num_exp[1] = '\0';
    }

I'm rewriting this patch to allow those uses as well... so you might as
well skip this patch if you're reviewing.  However, I think the other
patches in the series are still ok to read.

Tom
  

Patch

diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index a1c308a38d..d45a7353db 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -1082,7 +1082,7 @@  list_command (char *arg, int from_tty)
      turn it into the no-arg variant.  */
 
   if (from_tty)
-    *arg = 0;
+    truncate_repeat_arguments ();
 
   if (dummy_beg && sal_end.symtab == 0)
     error (_("No default source file yet.  Do \"help list\"."));
diff --git a/gdb/command.h b/gdb/command.h
index a99544563c..b590e897ed 100644
--- a/gdb/command.h
+++ b/gdb/command.h
@@ -445,6 +445,11 @@  extern void dont_repeat (void);
 
 extern scoped_restore_tmpl<int> prevent_dont_repeat (void);
 
+/* Truncate the arguments to the last command, so that a repetition of
+   the command sees no arguments.  */
+
+extern void truncate_repeat_arguments ();
+
 /* Used to mark commands that don't do anything.  If we just leave the
    function field NULL, the command is interpreted as a help topic, or
    as a class of commands.  */
diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index 77a05e5d9f..3218b5ab51 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -1636,7 +1636,7 @@  x_command (char *exp, int from_tty)
          repeated with Newline.  But don't clobber a user-defined
          command's definition.  */
       if (from_tty)
-	*exp = 0;
+	truncate_repeat_arguments ();
       val = evaluate_expression (expr.get ());
       if (TYPE_IS_REFERENCE (value_type (val)))
 	val = coerce_ref (val);
diff --git a/gdb/top.c b/gdb/top.c
index 3b3bbee4ac..be90ed6e30 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -528,6 +528,19 @@  maybe_wait_sync_command_done (int was_sync)
     wait_sync_command_done ();
 }
 
+/* True if execute_command should truncate the arguments to the last
+   command, so that a repetition of the command sees no arguments.  */
+
+static int should_truncate_repeat_arguments;
+
+/* See command.h.  */
+
+void
+truncate_repeat_arguments ()
+{
+  should_truncate_repeat_arguments = 1;
+}
+
 /* Execute the line P as a command, in the current user context.
    Pass FROM_TTY as second argument to the defining function.  */
 
@@ -571,6 +584,10 @@  execute_command (char *p, int from_tty)
       c = lookup_cmd (&cmd, cmdlist, "", 0, 1);
       p = (char *) cmd;
 
+      scoped_restore save_truncation
+	= make_scoped_restore (&should_truncate_repeat_arguments, 0);
+      char *args_pointer = p;
+
       /* Pass null arg rather than an empty one.  */
       arg = *p ? p : 0;
 
@@ -619,6 +636,8 @@  execute_command (char *p, int from_tty)
       /* If this command has been post-hooked, run the hook last.  */
       execute_cmd_post_hook (c);
 
+      if (should_truncate_repeat_arguments)
+	*args_pointer = '\0';
     }
 
   check_frame_language_change ();