From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11254 invoked by alias); 16 Nov 2005 14:16:45 -0000 Received: (qmail 11158 invoked by uid 22791); 16 Nov 2005 14:16:37 -0000 Received: from fra-del-01.spheriq.net (HELO fra-del-01.spheriq.net) (195.46.51.97) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Wed, 16 Nov 2005 14:16:37 +0000 Received: from fra-out-01.spheriq.net (fra-out-01.spheriq.net [195.46.51.129]) by fra-del-01.spheriq.net with ESMTP id jAGEGWwb020557 for ; Wed, 16 Nov 2005 14:16:32 GMT Received: from fra-cus-02.spheriq.net (fra-cus-02.spheriq.net [195.46.51.38]) by fra-out-01.spheriq.net with ESMTP id jAGEGTu8032498 for ; Wed, 16 Nov 2005 14:16:30 GMT Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by fra-cus-02.spheriq.net with ESMTP id jAGEGFTw030408 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=OK) for ; Wed, 16 Nov 2005 14:16:20 GMT Received: from zeta.dmz-eu.st.com (ns2.st.com [164.129.230.9]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 89484DA42 for ; Wed, 16 Nov 2005 14:15:13 +0000 (GMT) Received: by zeta.dmz-eu.st.com (STMicroelectronics, from userid 60012) id 43DCD474F8; Wed, 16 Nov 2005 14:18:12 +0000 (GMT) Received: from zeta.dmz-eu.st.com (localhost [127.0.0.1]) by zeta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 1A0137599D for ; Wed, 16 Nov 2005 14:18:12 +0000 (UTC) Received: from mail1.bri.st.com (mail1.bri.st.com [164.129.8.218]) by zeta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 815EF474FE for ; Wed, 16 Nov 2005 14:18:11 +0000 (GMT) Received: from [164.129.15.13] (terrorhawk.bri.st.com [164.129.15.13]) by mail1.bri.st.com (MOS 3.5.8-GR) with ESMTP id CGZ37277 (AUTH "andrew stubbs"); Wed, 16 Nov 2005 14:15:11 GMT Message-ID: <437B3E56.3080306@st.com> Date: Wed, 16 Nov 2005 14:58:00 -0000 From: Andrew STUBBS User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317) MIME-Version: 1.0 To: gdb-patches@sources.redhat.com Subject: [PATCH] init-if-undefined command Content-Type: multipart/mixed; boundary="------------080705030707050806080707" X-O-Spoofed: Not Scanned X-O-General-Status: No X-O-Spam1-Status: Not Scanned X-O-Spam2-Status: Not Scanned X-O-URL-Status: Not Scanned X-O-Virus1-Status: No X-O-Virus2-Status: Not Scanned X-O-Virus3-Status: No X-O-Virus4-Status: No X-O-Virus5-Status: Not Scanned X-O-Image-Status: Not Scanned X-O-Attach-Status: Not Scanned X-SpheriQ-Ver: 4.1.07 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2005-11/txt/msg00228.txt.bz2 This is a multi-part message in MIME format. --------------080705030707050806080707 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-length: 618 Hi, I have decided that, instead of queuing all my patches here while waiting for the current ones to go through, I shall post them all now. Hopefully the low hanging fruit will then get done and only the most controversial ones will remain when I go away for Christmas. The attached patch adds a new CLI command named 'init-if-undefined'. This is essentially the same a 'set variable' (for convenience variables only), but does not set the variable if it already has a value. It is useful in scripts whose values can be overridden, or as an approximation of a statically initialised variable. Andrew Stubbs --------------080705030707050806080707 Content-Type: text/plain; name="init-if-undefined.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="init-if-undefined.patch" Content-length: 3560 2005-11-16 Andrew Stubbs * 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-03 17:44:15.000000000 +0000 +++ src/gdb/value.c 2005-11-03 17:48:33.000000000 +0000 @@ -727,6 +727,47 @@ 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) + { + printf_unfiltered ("usage: init-if-undefined $ = \n"); + do_cleanups (old_chain); + return; + } + + /* 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) + { + printf_unfiltered ("First parameter to init-if-undefined should be a GDB variable\n"); + do_cleanups (old_chain); + return; + } + 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 +1680,9 @@ 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, _("\ +init-if-undefined = \n\ +Ensure that an internal variable exists and set it to\n\ +a given value if it does not.")); } Index: src/gdb/doc/gdb.texinfo =================================================================== --- src.orig/gdb/doc/gdb.texinfo 2005-11-03 17:44:15.000000000 +0000 +++ src/gdb/doc/gdb.texinfo 2005-11-03 17:45: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 --------------080705030707050806080707--