This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[PATCH 1/6] new observer command_option_changed.
Hi,
This patch is to add an extra field 'notify_observer_p' to identify
the command will notify observer 'command_option_changed', and teach
GDB to check the change of command option, and notify observer.
gdb:
2012-07-24 Yao Qi <yao@codesourcery.com>
* cli/cli-decode.c (add_cmd): Set field 'notify_observer_p' to 0.
(add_setshow_auto_boolean_cmd): Move auto_boolean_enums out.
Remove 'static'.
* cli/cli-decode.h (struct cmd_list_element) <notify_observer_p>:
New field.
Declare 'auto_boolean_enums'.
* cli/cli-setshow.c (do_setshow_command): Notify observer
command_option_changed if the option of command is changed.
gdb/doc:
2012-07-24 Yao Qi <yao@codesourcery.com>
* observer.texi: New observer command_option_changed.
---
gdb/cli/cli-decode.c | 4 +-
gdb/cli/cli-decode.h | 4 ++
gdb/cli/cli-setshow.c | 142 ++++++++++++++++++++++++++++++++++++++++---------
gdb/doc/observer.texi | 5 ++
4 files changed, 129 insertions(+), 26 deletions(-)
diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index c337b43..6739aef 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -203,6 +203,7 @@ add_cmd (char *name, enum command_class class, void (*fun) (char *, int),
c->user_commands = NULL;
c->cmd_pointer = NULL;
c->alias_chain = NULL;
+ c->notify_observer_p = 0;
return c;
}
@@ -430,6 +431,8 @@ add_setshow_enum_cmd (char *name,
c->enums = enumlist;
}
+const char *auto_boolean_enums[] = { "on", "off", "auto", NULL };
+
/* Add an auto-boolean command named NAME to both the set and show
command list lists. CLASS is as in add_cmd. VAR is address of the
variable which will contain the value. DOC is the documentation
@@ -445,7 +448,6 @@ add_setshow_auto_boolean_cmd (char *name,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
- static const char *auto_boolean_enums[] = { "on", "off", "auto", NULL };
struct cmd_list_element *c;
add_setshow_cmd_full (name, class, var_auto_boolean, var,
diff --git a/gdb/cli/cli-decode.h b/gdb/cli/cli-decode.h
index b5e0790..73001a2 100644
--- a/gdb/cli/cli-decode.h
+++ b/gdb/cli/cli-decode.h
@@ -210,6 +210,9 @@ struct cmd_list_element
/* Link pointer for aliases on an alias list. */
struct cmd_list_element *alias_chain;
+
+ /* Notify 'command_option_changed' observer if it is true. */
+ int notify_observer_p;
};
extern void help_cmd_list (struct cmd_list_element *, enum command_class,
@@ -232,5 +235,6 @@ extern void not_just_help_class_command (char *arg, int from_tty);
extern void print_doc_line (struct ui_file *, char *);
+extern const char *auto_boolean_enums[];
#endif /* !defined (CLI_DECODE_H) */
diff --git a/gdb/cli/cli-setshow.c b/gdb/cli/cli-setshow.c
index 7ffb89e..9369b29 100644
--- a/gdb/cli/cli-setshow.c
+++ b/gdb/cli/cli-setshow.c
@@ -28,6 +28,8 @@
#include "cli/cli-cmds.h"
#include "cli/cli-setshow.h"
+#include "observer.h"
+
/* Prototypes for local functions. */
static int parse_binary_operation (char *);
@@ -170,50 +172,122 @@ do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c)
#endif
*q++ = '\0';
new = (char *) xrealloc (new, q - new);
- xfree (*(char **) c->var);
- *(char **) c->var = new;
+
+ if (*(char **) c->var == NULL
+ || strcmp (*(char **) c->var, new) != 0)
+ {
+ xfree (*(char **) c->var);
+ *(char **) c->var = new;
+
+ if (c->notify_observer_p)
+ observer_notify_command_option_changed (c->name, new);
+ }
+ else
+ xfree (new);
}
break;
case var_string_noescape:
if (arg == NULL)
arg = "";
- xfree (*(char **) c->var);
- *(char **) c->var = xstrdup (arg);
+
+ if (*(char **) c->var == NULL || strcmp (*(char **) c->var, arg) != 0)
+ {
+ xfree (*(char **) c->var);
+ *(char **) c->var = xstrdup (arg);
+
+ if (c->notify_observer_p)
+ observer_notify_command_option_changed (c->name, arg);
+ }
break;
case var_filename:
if (arg == NULL)
error_no_arg (_("filename to set it to."));
/* FALLTHROUGH */
case var_optional_filename:
- xfree (*(char **) c->var);
+ {
+ char *val = NULL;
- if (arg != NULL)
- {
- /* Clear trailing whitespace of filename. */
- char *ptr = arg + strlen (arg) - 1;
+ if (arg != NULL)
+ {
+ /* Clear trailing whitespace of filename. */
+ char *ptr = arg + strlen (arg) - 1;
- while (ptr >= arg && (*ptr == ' ' || *ptr == '\t'))
- ptr--;
- *(ptr + 1) = '\0';
+ while (ptr >= arg && (*ptr == ' ' || *ptr == '\t'))
+ ptr--;
+ *(ptr + 1) = '\0';
- *(char **) c->var = tilde_expand (arg);
- }
- else
- *(char **) c->var = xstrdup ("");
+ val = tilde_expand (arg);
+ }
+ else
+ val = xstrdup ("");
+
+ if (*(char **) c->var == NULL
+ || strcmp (*(char **) c->var, val) != 0)
+ {
+ xfree (*(char **) c->var);
+ *(char **) c->var = val;
+
+ if (c->notify_observer_p)
+ observer_notify_command_option_changed (c->name, val);
+ }
+ else
+ xfree (val);
+ }
break;
case var_boolean:
- *(int *) c->var = parse_binary_operation (arg);
+ {
+ int val = parse_binary_operation (arg);
+
+ if (val != *(int *) c->var)
+ {
+ *(int *) c->var = val;
+
+ if (c->notify_observer_p)
+ observer_notify_command_option_changed (c->name,
+ val ? "on" : "off");
+ }
+ }
break;
case var_auto_boolean:
- *(enum auto_boolean *) c->var = parse_auto_binary_operation (arg);
+ {
+ enum auto_boolean val = parse_auto_binary_operation (arg);
+
+ if (*(enum auto_boolean *) c->var != val)
+ {
+ *(enum auto_boolean *) c->var = val;
+
+ if (c->notify_observer_p)
+ {
+ char *s = (char *) auto_boolean_enums[val];
+
+ observer_notify_command_option_changed (c->name, s);
+ }
+ }
+ }
break;
case var_uinteger:
case var_zuinteger:
if (arg == NULL)
error_no_arg (_("integer to set it to."));
- *(unsigned int *) c->var = parse_and_eval_long (arg);
- if (c->var_type == var_uinteger && *(unsigned int *) c->var == 0)
- *(unsigned int *) c->var = UINT_MAX;
+ {
+ unsigned int val = parse_and_eval_long (arg);
+
+ if (c->var_type == var_uinteger && val == 0)
+ val = UINT_MAX;
+
+ if (*(unsigned int *) c->var != val)
+ {
+ *(unsigned int *) c->var = val;
+
+ if (c->notify_observer_p)
+ {
+ char s[64];
+
+ xsnprintf (s, sizeof s, "%d", val);
+ observer_notify_command_option_changed (c->name, s);
+ }
+ }
+ }
break;
case var_integer:
case var_zinteger:
@@ -224,11 +298,23 @@ do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c)
error_no_arg (_("integer to set it to."));
val = parse_and_eval_long (arg);
if (val == 0 && c->var_type == var_integer)
- *(int *) c->var = INT_MAX;
+ val = INT_MAX;
else if (val >= INT_MAX)
error (_("integer %u out of range"), val);
- else
- *(int *) c->var = val;
+
+
+ if (*(int *) c->var != val)
+ {
+ *(int *) c->var = val;
+
+ if (c->notify_observer_p)
+ {
+ char s[64];
+
+ xsnprintf (s, sizeof s, "%d", val);
+ observer_notify_command_option_changed (c->name, s);
+ }
+ }
break;
}
case var_enum:
@@ -293,7 +379,13 @@ do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c)
if (nmatches > 1)
error (_("Ambiguous item \"%s\"."), arg);
- *(const char **) c->var = match;
+ if (*(const char **) c->var != match)
+ {
+ *(const char **) c->var = match;
+
+ if (c->notify_observer_p)
+ observer_notify_command_option_changed (c->name, match);
+ }
}
break;
default:
diff --git a/gdb/doc/observer.texi b/gdb/doc/observer.texi
index 14d4ac3..a45df36 100644
--- a/gdb/doc/observer.texi
+++ b/gdb/doc/observer.texi
@@ -234,6 +234,11 @@ the current top-level prompt.
Variable gdb_datadir has been set. The value may not necessarily change.
@end deftypefun
+@deftypefun void command_option_changed (const char *@var{command}, const char *@var{option})
+The option of some @code{set} commands in console are changed. This method is called after
+a command @code{set command option}.
+@end deftypefun
+
@deftypefun void test_notification (int @var{somearg})
This observer is used for internal testing. Do not use.
See testsuite/gdb.gdb/observer.exp.
--
1.7.7.6