This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [PATCH] init-if-undefined command
- From: Andrew STUBBS <andrew dot stubbs at st dot com>
- To: Jim Blandy <jimb at red-bean dot com>
- Cc: gdb-patches at sources dot redhat dot com
- Date: Thu, 01 Dec 2005 11:52:24 +0000
- Subject: Re: [PATCH] init-if-undefined command
- References: <437B3E56.3080306@st.com> <8f2776cb0511292141t2f9711a2l9633236db737e67b@mail.gmail.com> <438DB351.2050500@st.com> <20051130190311.GA25377@nevyn.them.org> <8f2776cb0511301501t4eb7bb50w484427fd892f5a58@mail.gmail.com>
Jim Blandy wrote:
On 11/30/05, Daniel Jacobowitz <drow@false.org> wrote:
On Wed, Nov 30, 2005 at 02:12:33PM +0000, Andrew STUBBS wrote:
+ printf_unfiltered ("Init-if-undefined requires an assignment expression.\n");
Is there any reason this, and the other outputs, should not be
_("gettextized")?
Also, should this be an error()?
These are things I should have noticed. They need to be fixed before
this can be committed.
Absolutely, the only reason for this is incompetence. How about the
attached?
Thanks
Andrew Stubbs
2005-12-01 Andrew Stubbs <andrew.stubbs@st.com>
* value.c (init_if_undefined_command): New function.
(_initialize_values): Add command init-if-undefined.
doc/
* gdb.texinfo (Convenience variables): Add init-if-undefined command.
Index: src/gdb/value.c
===================================================================
--- src.orig/gdb/value.c 2005-11-29 16:35:46.000000000 +0000
+++ src/gdb/value.c 2005-12-01 11:43:06.000000000 +0000
@@ -727,6 +727,39 @@ show_values (char *num_exp, int from_tty
static struct internalvar *internalvars;
+/* If the variable does not already exist create it and give it the value given.
+ If no value is given then the default is zero. */
+static void
+init_if_undefined_command (char* args, int from_tty)
+{
+ struct internalvar* intvar;
+
+ /* Parse the expression - this is taken from set_command(). */
+ struct expression *expr = parse_expression (args);
+ register struct cleanup *old_chain =
+ make_cleanup (free_current_contents, &expr);
+
+ /* Validate the expression.
+ Was the expression an assignment?
+ Or even an expression at all? */
+ if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN)
+ error (_("Init-if-undefined requires an assignment expression."));
+
+ /* Extract the variable from the parsed expression.
+ In the case of an assign the lvalue will be in elts[1] and elts[2]. */
+ if (expr->elts[1].opcode != OP_INTERNALVAR)
+ error (_("The first parameter to init-if-undefined should be a GDB variable."));
+ intvar = expr->elts[2].internalvar;
+
+ /* Only evaluate the expression if the lvalue is void.
+ This may still fail if the expresssion is invalid. */
+ if (TYPE_CODE (value_type (intvar->value)) == TYPE_CODE_VOID)
+ evaluate_expression (expr);
+
+ do_cleanups (old_chain);
+}
+
+
/* Look up an internal variable with name NAME. NAME should not
normally include a dollar sign.
@@ -1639,4 +1672,11 @@ A few convenience variables are given va
add_cmd ("values", no_class, show_values,
_("Elements of value history around item number IDX (or last ten)."),
&showlist);
+
+ add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
+Initialize a convenience variable if necessary.\n\
+init-if-undefined VARIABLE = EXPRESSION\n\
+Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
+exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
+VARIABLE is already initialized."));
}
Index: src/gdb/doc/gdb.texinfo
===================================================================
--- src.orig/gdb/doc/gdb.texinfo 2005-11-29 16:36:23.000000000 +0000
+++ src/gdb/doc/gdb.texinfo 2005-12-01 11:36:10.000000000 +0000
@@ -6131,6 +6131,18 @@ variable, when used as an expression, ha
@item show convenience
Print a list of convenience variables used so far, and their values.
Abbreviated @code{show conv}.
+
+@kindex init-if-undefined
+@cindex convenience variables, initializing
+@item init-if-undefined $@var{variable} = @var{expression}
+Set a convenience variable if it has not already been set. This is useful
+for user-defined commands that keep some state. It is similar, in concept,
+to using local static variables with initializers in C (except that
+convenience variables are global). It can also be used to allow users to
+override default values used in a command script.
+
+If the variable is already defined then the expression is not evaluated so
+any side-effects do not occur.
@end table
One of the ways to use a convenience variable is as a counter to be