[patch] Add support for hooking prefix commands

Sorin Otescu
2011-03-09T11:15:26Z

[-- Attachment #1: Type: text/plain, Size: 641 bytes --]

Hi all,

I recently needed to add a hook for the "info threads" command.
Unfortunately, this was not possible because GDB only allows hooking of
single word commands.
This patch adds support for multi-word command (prefix command) hooks.
In my case, the format of the hook commands is:

define hook-info_threads
define hookpost-info_threads

Sorin

gdb/cli/
2011-03-09 Sorin Otescu<sorinu@gmail.com>

* cli-script.c (define_command): Add support for hooking prefix commands.
Example usage: for hooking "info threads", a hook command will be defined as:
define hook-info_threads and/or define hookpost-info_threads

[-- Attachment #2: cli-script.c.patch --]
[-- Type: text/x-patch, Size: 1986 bytes --]

--- cli-script.c.orig 2010-05-17 22:28:12.000000000 +0300
+++ cli-script.c 2011-03-09 11:37:04.917093842 +0200
@@ -1416,8 +1350,9 @@
CMD_POST_HOOK
};
struct command_line *cmds;
- struct cmd_list_element *c, *newc, *hookc = 0, **list;
- char *tem, *comfull;
+ struct cmd_list_element *c, *newc, *hookc = 0, **list;
+ char *tem, *tem2, *comfull;
+ char tembuf[MAX_TMPBUF];
char tmpbuf[MAX_TMPBUF];
int hook_type = CMD_NO_HOOK;
int hook_name_size = 0;
--- cli-script.c.orig 2010-05-17 22:28:12.000000000 +0300
+++ cli-script.c 2011-03-09 11:37:04.917093842 +0200
@@ -1430,8 +1365,11 @@
comfull = comname;
list = validate_comname (&comname);

+ strncpy(tembuf, comname, MAX_TMPBUF);
+ tembuf[MAX_TMPBUF-1] = 0;
+
/* Look it up, and verify that we got an exact match. */
- tem = comname;
+ tem = tembuf;
c = lookup_cmd (&tem, *list, "", -1, 1);
if (c && strcmp (comname, c->name) != 0)
c = 0;
--- cli-script.c.orig 2010-05-17 22:28:12.000000000 +0300
+++ cli-script.c 2011-03-09 11:37:04.917093842 +0200
@@ -1465,11 +1402,26 @@

if (hook_type != CMD_NO_HOOK)
{
+ /* Convert _ characters into spaces, to allow hooking prefix commands */
+ tem = tembuf + hook_name_size;
+ tem2 = strchr(tem, '_');
+ while (tem2)
+ {
+ *tem2 = ' ';
+ tem2 = strchr(tem2+1, '_');
+ }
+
/* Look up cmd it hooks, and verify that we got an exact match. */
- tem = comname + hook_name_size;
hookc = lookup_cmd (&tem, *list, "", -1, 0);
- if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
+ while (hookc && *tem)
+ {
+ /* tem might point to the argument of a prefix command */
+ if (hookc->prefixlist)
+ hookc = lookup_cmd (&tem, *hookc->prefixlist, "", -1, 0);
+ else
hookc = 0;
+ }
+
if (!hookc)
{
warning (_("Your new `%s' command does not hook any existing command."),