+2016-12-02 Pedro Alves <palves@redhat.com>
+
+ PR cli/20559
+ * NEWS: Mention "eval" expands user-defined command arguments.
+ * cli/cli-script.c (execute_control_command): Adjust to rename.
+ (insert_args): Rename to ...
+ (insert_user_defined_cmd_args): ... this, and make extern.
+ * cli/cli-script.h (insert_user_defined_cmd_args): New
+ declaration.
+ * printcmd.c: Include "cli/cli-script.h".
+ (eval_command): Call insert_user_defined_cmd_args.
+
2016-12-02 Tom Tromey <tom@tromey.com>
PR symtab/16264:
* Support for Java programs compiled with gcj has been removed.
+* The "eval" command now expands user-defined command arguments.
+
+ This makes it easier to process a variable number of arguments:
+
+ define mycommand
+ set $i = 0
+ while $i < $argc
+ eval "print $arg%d", $i
+ set $i = $i + 1
+ end
+ end
+
* New targets
Synopsys ARC arc*-*-elf32
void (*validator)(char *, void *),
void *closure);
-static std::string insert_args (const char *line);
-
static struct cleanup * setup_user_args (char *p);
static char *read_next_line (void);
case simple_control:
{
/* A simple command, execute it and return. */
- std::string new_line = insert_args (cmd->line);
+ std::string new_line = insert_user_defined_cmd_args (cmd->line);
execute_command (&new_line[0], 0);
ret = cmd->control_type;
break;
print_command_trace (buffer);
/* Parse the loop control expression for the while statement. */
- std::string new_line = insert_args (cmd->line);
+ std::string new_line = insert_user_defined_cmd_args (cmd->line);
expression_up expr = parse_expression (new_line.c_str ());
ret = simple_control;
print_command_trace (buffer);
/* Parse the conditional for the if statement. */
- std::string new_line = insert_args (cmd->line);
+ std::string new_line = insert_user_defined_cmd_args (cmd->line);
expression_up expr = parse_expression (new_line.c_str ());
current = NULL;
{
/* Breakpoint commands list, record the commands in the
breakpoint's command list and return. */
- std::string new_line = insert_args (cmd->line);
+ std::string new_line = insert_user_defined_cmd_args (cmd->line);
ret = commands_from_control_command (new_line.c_str (), cmd);
break;
}
return NULL;
}
-/* Insert the user defined arguments stored in user_arg into the $arg
- arguments found in line. */
+/* See cli-script.h. */
-static std::string
-insert_args (const char *line)
+std::string
+insert_user_defined_cmd_args (const char *line)
{
- /* If we are not in a user-defined function, treat $argc, $arg0, et
+ /* If we are not in a user-defined command, treat $argc, $arg0, et
cetera as normal convenience variables. */
if (user_args == NULL)
return line;
extern void execute_user_command (struct cmd_list_element *c, char *args);
+/* If we're in a user-defined command, replace any $argc/$argN
+ reference found in LINE with the arguments that were passed to the
+ command. Otherwise, treat $argc/$argN as normal convenience
+ variables. */
+extern std::string insert_user_defined_cmd_args (const char *line);
+
/* Exported to top.c */
extern void print_command_trace (const char *cmd);
+2016-12-02 Pedro Alves <palves@redhat.com>
+
+ PR cli/20559
+ * gdb.texinfo (Define): Add example of using "eval" to process a
+ variable number of arguments.
+ (Output) <eval>: Add anchor.
+
2016-11-30 Simon Marchi <simon.marchi@polymtl.ca>
* Makefile.in: Likewise.
end
@end smallexample
+Combining with the @code{eval} command (@pxref{eval}) makes it easier
+to process a variable number of arguments:
+
+@smallexample
+define adder
+ set $i = 0
+ set $sum = 0
+ while $i < $argc
+ eval "set $sum = $sum + $arg%d", $i
+ set $i = $i + 1
+ end
+ print $sum
+end
+@end smallexample
+
@table @code
@kindex define
printf "D32: %Hf - D64: %Df - D128: %DDf\n",1.2345df,1.2E10dd,1.2E1dl
@end smallexample
+@anchor{eval}
@kindex eval
@item eval @var{template}, @var{expressions}@dots{}
Convert the values of one or more @var{expressions} under the control of
#include "charset.h"
#include "arch-utils.h"
#include "cli/cli-utils.h"
+#include "cli/cli-script.h"
#include "format.h"
#include "source.h"
std::string expanded = ui_file_as_string (ui_out);
+ expanded = insert_user_defined_cmd_args (expanded.c_str ());
+
execute_command (&expanded[0], from_tty);
do_cleanups (cleanups);
+2016-12-02 Pedro Alves <palves@redhat.com>
+
+ PR cli/20559
+ * gdb.base/commands.exp (user_defined_command_args_eval): New
+ procedure.
+ (top level): Call it.
+
2016-12-02 Luis Machado <lgustavo@codesourcery.com>
* gdb.base/ovldbreak.exp (take_gdb_out_of_choice_menu): Restore
"display user-defined empty command"
}
+# Test that "eval" in a user-defined command expands $argc/$argN.
+
+proc_with_prefix user_defined_command_args_eval {} {
+ global gdb_prompt
+
+ gdb_test_multiple "define command_args_eval" \
+ "define command_args_eval" {
+ -re "End with" {
+ pass "define"
+ }
+ }
+
+ # Make a command that constructs references to $argc and $argN via
+ # eval.
+ gdb_test \
+ [multi_line \
+ {eval "printf \"argc = %%d,\", $arg%c", 'c'} \
+ {set $i = 0} \
+ {while $i < $argc} \
+ { eval "printf \" %%d\", $arg%d", $i} \
+ { set $i = $i + 1} \
+ {end} \
+ {printf "\n"} \
+ {end}] \
+ "" \
+ "enter commands"
+
+ gdb_test "command_args_eval 1 2 3" "argc = 3, 1 2 3" "execute command"
+}
+
proc_with_prefix watchpoint_command_test {} {
global gdb_prompt
infrun_breakpoint_command_test
breakpoint_command_test
user_defined_command_test
+user_defined_command_args_eval
watchpoint_command_test
test_command_prompt_position
deprecated_command_test