From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10794 invoked by alias); 7 Apr 2011 23:32:43 -0000 Received: (qmail 10772 invoked by uid 22791); 7 Apr 2011 23:32:41 -0000 X-SWARE-Spam-Status: No, hits=-2.0 required=5.0 tests=AWL,BAYES_00,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from smtp.gentoo.org (HELO smtp.gentoo.org) (140.211.166.183) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 07 Apr 2011 23:32:36 +0000 Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id 583A11B401D; Thu, 7 Apr 2011 23:32:35 +0000 (UTC) From: Mike Frysinger To: gdb-patches@sourceware.org Cc: toolchain-devel@blackfin.uclinux.org Subject: [PATCH] gdb: sim: add command line completion Date: Thu, 07 Apr 2011 23:32:00 -0000 Message-Id: <1302219156-9055-1-git-send-email-vapier@gentoo.org> X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2011-04/txt/msg00117.txt.bz2 For now, only the sub-command name is completed. No support yet for completing options to that command. But even this is a huge step as currently, nothing is completed, and the basic "help sim" is fairly obtuse as to what exactly the "sim" command accepts. Signed-off-by: Mike Frysinger gdb/: 2011-04-07 Mike Frysinger * remote-sim.c (sim_command_completer): New function. (_initialize_remote_sim): Set completer to sim_command_completer. include/gdb/: 2011-04-07 Mike Frysinger * remote-sim.h (sim_complete_command): New prototype. sim/: 2011-04-07 Mike Frysinger * sim-options.c (complete_option_list, sim_complete_command): New functions. --- gdb/remote-sim.c | 19 +++++++++++++++- include/gdb/remote-sim.h | 4 +++ sim/common/sim-options.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/gdb/remote-sim.c b/gdb/remote-sim.c index baaf439..f9232b5 100644 --- a/gdb/remote-sim.c +++ b/gdb/remote-sim.c @@ -1193,6 +1193,18 @@ simulator_command (char *args, int from_tty) registers_changed (); } +char **sim_command_completer (struct cmd_list_element *ignore, + char *text, char *word) +{ + struct sim_inferior_data *sim_data; + + sim_data = inferior_data (current_inferior (), sim_inferior_data_key); + if (sim_data == NULL || sim_data->gdbsim_desc == NULL) + return NULL; + + return sim_complete_command (sim_data->gdbsim_desc, text, word); +} + /* Check to see if a thread is still alive. */ static int @@ -1287,11 +1299,14 @@ init_gdbsim_ops (void) void _initialize_remote_sim (void) { + struct cmd_list_element *c; + init_gdbsim_ops (); add_target (&gdbsim_ops); - add_com ("sim", class_obscure, simulator_command, - _("Send a command to the simulator.")); + c = add_com ("sim", class_obscure, simulator_command, + _("Send a command to the simulator.")); + set_cmd_completer (c, sim_command_completer); sim_inferior_data_key = register_inferior_data_with_cleanup (sim_inferior_data_cleanup); diff --git a/include/gdb/remote-sim.h b/include/gdb/remote-sim.h index a171cfd..5f6000d 100644 --- a/include/gdb/remote-sim.h +++ b/include/gdb/remote-sim.h @@ -276,6 +276,10 @@ void sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc); void sim_do_command (SIM_DESC sd, char *cmd); +/* Complete a command based on the available sim commands. Returns an + array of possible matches. */ +char **sim_complete_command (SIM_DESC sd, char *text, char *word); + #ifdef __cplusplus } #endif diff --git a/sim/common/sim-options.c b/sim/common/sim-options.c index 0b4d4ee..ee8a8ec 100644 --- a/sim/common/sim-options.c +++ b/sim/common/sim-options.c @@ -915,6 +915,57 @@ find_match (SIM_DESC sd, sim_cpu *cpu, char *argv[], int *pargi) return matching_opt; } +static char ** +complete_option_list (char **ret, size_t *cnt, const struct option_list *ol, + char *text, char *word) +{ + const OPTION *opt = NULL; + int argi; + size_t len = strlen (word); + + for ( ; ol != NULL; ol = ol->next) + for (opt = ol->options; OPTION_VALID_P (opt); ++opt) + { + const char *name = opt->opt.name; + + /* A long option to match against? */ + if (!name) + continue; + + /* Does this option actually match? */ + if (strncmp (name, word, len)) + continue; + + ret = xrealloc (ret, ++*cnt * sizeof (ret[0])); + ret[*cnt - 2] = xstrdup (name); + } + + return ret; +} + +/* All leading text is stored in @text, while the current word being + completed is stored in @word. Trailing text of @word is not. */ +char ** +sim_complete_command (SIM_DESC sd, char *text, char *word) +{ + char **ret = NULL; + size_t cnt = 1; + sim_cpu *cpu; + + /* Only complete first word for now. */ + if (text != word) + return ret; + + cpu = STATE_CPU (sd, 0); + if (cpu) + ret = complete_option_list (ret, &cnt, CPU_OPTIONS (cpu), text, word); + ret = complete_option_list (ret, &cnt, STATE_OPTIONS (sd), text, word); + + if (ret) + ret[cnt - 1] = NULL; + return ret; +} + SIM_RC sim_args_command (SIM_DESC sd, char *cmd) { -- 1.7.4.1