From 6837a0a2a98f17adfd4b3723b6130d3ae4ae258e Mon Sep 17 00:00:00 2001 From: Daniel Berlin Date: Thu, 23 Mar 2000 23:21:27 +0000 Subject: [PATCH] Added the apropos command --- gdb/ChangeLog | 7 +++ gdb/command.c | 88 ++++++++++++++++++++++++++++++++- gdb/doc/ChangeLog | 4 ++ gdb/doc/gdb.texinfo | 19 +++++++ gdb/testsuite/ChangeLog | 4 ++ gdb/testsuite/gdb.base/help.exp | 6 +++ 6 files changed, 127 insertions(+), 1 deletion(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 132e12b3478..8d5b70e565b 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -40,6 +40,13 @@ (delete_file_handler, handle_file_event): Likewise. (gdb_wait_for_event, poll_timers): Likewise. +2000-03-22 Daniel Berlin + * command.c (apropos_cmd_helper): New function, meat of the + apropos command. + (apropos_command): New apropos command to search command + names/documentation for regular expressions. + (_initialize_command): Add the apropos command. + 2000-03-22 Peter Schauer * printcmd.c (print_scalar_formatted): Truncate addresses to the diff --git a/gdb/command.c b/gdb/command.c index e0b7ad79fd1..826cc46dfce 100644 --- a/gdb/command.c +++ b/gdb/command.c @@ -27,7 +27,7 @@ #endif #include "gdb_wait.h" - +#include "gnu-regex.h" /* FIXME: this should be auto-configured! */ #ifdef __MSDOS__ # define CANT_FORK @@ -54,6 +54,10 @@ static struct cmd_list_element *find_cmd PARAMS ((char *command, struct cmd_list_element * clist, int ignore_help_classes, int *nfound)); +static void apropos_cmd_helper (struct ui_file *, struct cmd_list_element *, + struct re_pattern_buffer *, char *); + +void apropos_command (char *, int); void _initialize_command PARAMS ((void)); @@ -370,6 +374,87 @@ delete_cmd (name, list) c = c->next; } } +/* Recursively walk the commandlist structures, and print out the + documentation of commands that match our regex in either their + name, or their documentation. +*/ +static void +apropos_cmd_helper (struct ui_file *stream, struct cmd_list_element *commandlist, + struct re_pattern_buffer *regex, char *prefix) +{ + register struct cmd_list_element *c; + int returnvalue=1; /*Needed to avoid double printing*/ + /* Walk through the commands */ + for (c=commandlist;c;c=c->next) + { + if (c->name != NULL) + { + /* Try to match against the name*/ + returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL); + if (returnvalue >= 0) + { + /* Stolen from help_cmd_list. We don't directly use + * help_cmd_list because it doesn't let us print out + * single commands + */ + fprintf_filtered (stream, "%s%s -- ", prefix, c->name); + print_doc_line (stream, c->doc); + fputs_filtered ("\n", stream); + returnvalue=0; /*Set this so we don't print it again.*/ + } + } + if (c->doc != NULL && returnvalue != 0) + { + /* Try to match against documentation */ + if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0) + { + /* Stolen from help_cmd_list. We don't directly use + * help_cmd_list because it doesn't let us print out + * single commands + */ + fprintf_filtered (stream, "%s%s -- ", prefix, c->name); + print_doc_line (stream, c->doc); + fputs_filtered ("\n", stream); + } + } + /* Check if this command has subcommands */ + if (c->prefixlist != NULL) + { + /* Recursively call ourselves on the subcommand list, + passing the right prefix in. + */ + apropos_cmd_helper(stream,*c->prefixlist,regex,c->prefixname); + } + } +} +/* Search through names of commands and documentations for a certain + regular expression. +*/ +void +apropos_command (char *searchstr, int from_tty) +{ + extern struct cmd_list_element *cmdlist; /*This is the main command list*/ + regex_t pattern; + char *pattern_fastmap; + char errorbuffer[512]; + pattern_fastmap=calloc(256,sizeof(char)); + if (searchstr == NULL) + error("REGEXP string is empty"); + + if (regcomp(&pattern,searchstr,REG_ICASE) == 0) + { + pattern.fastmap=pattern_fastmap; + re_compile_fastmap(&pattern); + apropos_cmd_helper(gdb_stdout,cmdlist,&pattern,""); + } + else + { + regerror(regcomp(&pattern,searchstr,REG_ICASE),NULL,errorbuffer,512); + error("Error in regular expression:%s",errorbuffer); + } + free(pattern_fastmap); +} + /* This command really has to deal with two things: * 1) I want documentation on *this string* (usually called by @@ -1693,4 +1778,5 @@ With no arguments, run an inferior shell."); "Show definitions of user defined commands.\n\ Argument is the name of the user defined command.\n\ With no argument, show definitions of all user defined commands.", &showlist); + add_com ("apropos", class_support, apropos_command, "Search for commands matching a REGEXP"); } diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog index b0f2654e94b..48efadf2bbe 100644 --- a/gdb/doc/ChangeLog +++ b/gdb/doc/ChangeLog @@ -1,3 +1,7 @@ +2000-03-22 Daniel Berlin + + * gdb.texinfo: Add documentation for the apropos command. + 2000-03-20 Michael Snyder * gdb.texinfo: Add white space to prevent overprinting in diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 15a0820ce95..7ccfe03e46f 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -1318,6 +1318,25 @@ Command name abbreviations are allowed if unambiguous. With a command name as @code{help} argument, @value{GDBN} displays a short paragraph on how to use that command. +@kindex apropos +@item apropos @var{args} +The @code{apropos @var{args}} command searches through all of the @value{GDBN} +commands, and their documentation, for the regular expression specified in +@var{args}. It prints out all matches found. For example: + +@smallexample +apropos reload +@end smallexample + +@noindent results in: + +@smallexample +@group +set symbol-reloading -- Set dynamic symbol table reloading multiple times in one run +show symbol-reloading -- Show dynamic symbol table reloading multiple times in one run +@end group +@end smallexample + @kindex complete @item complete @var{args} The @code{complete @var{args}} command lists all the possible completions diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 47bc29f3248..ded1f39d9bc 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2000-03-22 Daniel Berlin + + * gdb.base/help.exp: Added test for new apropos command. + 2000-03-21 Kevin Buettner * gdb.base/pointers.c (usevar): New function. diff --git a/gdb/testsuite/gdb.base/help.exp b/gdb/testsuite/gdb.base/help.exp index c78bd893899..9515132daed 100644 --- a/gdb/testsuite/gdb.base/help.exp +++ b/gdb/testsuite/gdb.base/help.exp @@ -540,3 +540,9 @@ gdb_test "help x" "Examine memory: x/FMT ADDRESS..*\[\r\n\]+ADDRESS is an expres gdb_test "help info bogus-gdb-command" "Undefined info command: \"bogus-gdb-command\". Try \"help info\"." "help info bogus-gdb-command" # test help gotcha gdb_test "help gotcha" "Undefined command: \"gotcha\". Try \"help\"." "help gotcha" +# test apropos regex +gdb_test "apropos \\\(print\[\^ bsiedf\\\".\]\\\)" "handle -- Specify how to handle a signal" +# test apropos >1 word string +gdb_test "apropos handle a signal" "handle -- Specify how to handle a signal" +# test apropos apropos +gdb_test "apropos apropos" "apropos -- Search for commands matching a REGEXP" -- 2.30.2