From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6352 invoked by alias); 13 Nov 2013 19:34:17 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 6342 invoked by uid 89); 13 Nov 2013 19:34:17 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.7 required=5.0 tests=AWL,BAYES_05,RDNS_NONE,SPF_HELO_PASS,SPF_PASS autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Received: from Unknown (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 13 Nov 2013 19:34:15 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rADJY7nC028663 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 13 Nov 2013 14:34:07 -0500 Received: from [127.0.0.1] (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id rADJY6T6024008; Wed, 13 Nov 2013 14:34:06 -0500 Message-ID: <5283D42D.8000909@redhat.com> Date: Wed, 13 Nov 2013 19:35:00 -0000 From: Pedro Alves User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130625 Thunderbird/17.0.7 MIME-Version: 1.0 To: Joel Brobecker CC: gdb-patches@sourceware.org Subject: Re: [RFA 2/3] New function cli-utils.c:extract_arg_const References: <1384151855-12926-1-git-send-email-brobecker@adacore.com> <1384151855-12926-2-git-send-email-brobecker@adacore.com> In-Reply-To: <1384151855-12926-2-git-send-email-brobecker@adacore.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-SW-Source: 2013-11/txt/msg00352.txt.bz2 On 11/11/2013 06:37 AM, Joel Brobecker wrote: > Hello, > > This function provides the exact same functionality as extract_arg, > except that it takes a "const char**" instead of a "char **". This > will be useful in the context of my next patch. > > gdb/ChangeLog: > > * cli/cli-utils.h (extract_arg_const): Add declaration. > * cli/cli-utils.c (extract_arg_const): New function. > > Tested on x86_64-linux. OK to push? Instead of essentially duplicating the code, how about we reimplement one on top of the other? See below. It compiles, but it's otherwise untested. ----------------- gdb/cli/cli-utils.c | 25 +++++++++++++++++-------- gdb/cli/cli-utils.h | 7 +++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/gdb/cli/cli-utils.c b/gdb/cli/cli-utils.c index f74e6b1..316cf4f 100644 --- a/gdb/cli/cli-utils.c +++ b/gdb/cli/cli-utils.c @@ -261,30 +261,39 @@ remove_trailing_whitespace (const char *start, char *s) /* See documentation in cli-utils.h. */ char * -extract_arg (char **arg) +extract_arg_const (const char **arg) { - char *result, *copy; + const char *result; if (!*arg) return NULL; /* Find the start of the argument. */ - *arg = skip_spaces (*arg); + *arg = skip_spaces_const (*arg); if (!**arg) return NULL; result = *arg; /* Find the end of the argument. */ - *arg = skip_to_space (*arg + 1); + *arg = skip_to_space_const (*arg + 1); if (result == *arg) return NULL; - copy = xmalloc (*arg - result + 1); - memcpy (copy, result, *arg - result); - copy[*arg - result] = '\0'; + return savestring (result, *arg - result); +} + +/* See documentation in cli-utils.h. */ + +char * +extract_arg (char **arg) +{ + const char *arg_const = *arg; + char *result; - return copy; + result = extract_arg_const (&arg_const); + *arg += arg_const - *arg; + return result; } /* See documentation in cli-utils.h. */ diff --git a/gdb/cli/cli-utils.h b/gdb/cli/cli-utils.h index 152fb89..ebae2d2 100644 --- a/gdb/cli/cli-utils.h +++ b/gdb/cli/cli-utils.h @@ -118,6 +118,13 @@ extern char *remove_trailing_whitespace (const char *start, char *s); extern char *extract_arg (char **arg); +/* A const-correct version of "extract_arg". + + Since the returned value is xmalloc'd, it eventually needs to be + xfree'ed, which prevents us from making it const as well. */ + +extern char *extract_arg_const (const char **arg); + /* A helper function that looks for an argument at the start of a string. The argument must also either be at the end of the string, or be followed by whitespace. Returns 1 if it finds the argument,