From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 53724 invoked by alias); 1 Sep 2018 11:10:04 -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 53712 invoked by uid 89); 1 Sep 2018 11:10:03 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: =?ISO-8859-1?Q?No, score=-27.6 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 spammy=brilliant, UD:be, UD:=c2, regexps?= X-HELO: mailsec108.isp.belgacom.be Received: from mailsec108.isp.belgacom.be (HELO mailsec108.isp.belgacom.be) (195.238.20.104) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 01 Sep 2018 11:09:59 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skynet.be; i=@skynet.be; q=dns/txt; s=securemail; t=1535800197; x=1567336197; h=message-id:subject:from:to:cc:date:in-reply-to: references:mime-version:content-transfer-encoding; bh=eOpc99vA/hX6/cByZ73BxH3mVI4XyFSveqf8XoVbqec=; b=e9XJ2lpLHxydKhrFuKWq9kwvKoZnX6Ae/UlDFJe85dG+7wn1wR/Vt2ZC p5/4p/3KJagorphQ1aDAB4gvkWFTEQ==; Received: from 232.142-134-109.adsl-dyn.isp.belgacom.be (HELO md) ([109.134.142.232]) by relay.skynet.be with ESMTP/TLS/AES256-GCM-SHA384; 01 Sep 2018 13:09:55 +0200 Message-ID: <1535800196.10641.1.camel@skynet.be> Subject: Re: [PATCH 2/2] gdb: Allow parenthesis to group arguments to user-defined commands From: Philippe Waroquiers To: Tom Tromey Cc: Andrew Burgess , gdb-patches@sourceware.org Date: Sat, 01 Sep 2018 11:10:00 -0000 In-Reply-To: <87h8jaz1zz.fsf@tromey.com> References: <1535225533.1438.5.camel@skynet.be> <1535230403.1438.10.camel@skynet.be> <20180825224310.GA32506@embecosm.com> <87lg8q7ai7.fsf@tromey.com> <20180828184331.GJ32506@embecosm.com> <1535488137.7778.4.camel@skynet.be> <20180828232948.GK32506@embecosm.com> <1535663976.7778.12.camel@skynet.be> <87h8jaz1zz.fsf@tromey.com> Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes X-SW-Source: 2018-09/txt/msg00005.txt.bz2 On Fri, 2018-08-31 at 14:59 -0600, Tom Tromey wrote: > > > > > > "Philippe" == Philippe Waroquiers writes: > > Philippe> For what concerns the parenthesis based solution, it looks to not work > Philippe> for some cases. > > Philippe> E.g. if I want to pass one argument (I am using single quotes to show the args > Philippe> I want in the below examples): > Philippe> 'a b) (c d' > Philippe> then I need to use: > Philippe> some_user_defined (a b) (c d) > Philippe> but that is the same notation as if I want to pass 2 arguments > Philippe> 'a b' and 'c d' > > Can you think of an actual example where this would be useful? > My feeling is that there isn't one, though I'd be interested in > tricky-but-plausible counter-examples. For example, I want a user defined command that receives a bunch of REGEXPs, and then for each regexp, the user defined command calls info types $arg0 info types $arg1 info types $arg2 More generally, any kind of user defined command might need to receive whatever args. IMO, quoting with quotes is also more usual (at least in shells) and single quotes are already used in gdb (as pointed out by Pedro). See e.g.  @cindex quotes in commands @cindex completion of quoted strings @cindex C@t{++} scope resolution @cindex quoting names (and of course, my brand new brilliant patch still to be reviewed :) that implements info [args|functions|locals|variables] [-q] [-t TYPEREGEXP] [NAMEREGEXP] is using optional single quotes for TYPEREGEXP, following Pedro's initial comments). So, it looks strange to me to add a new user defined argument quoting mechanism because the current mechanism (using quotes) has quirks, but the new mechanism itself starts its life with quirks/limitations. What about the alternative solution to allow a user defined command to use $argu0, $argu1, $argu2, ... to do unquoted expansion when needed ? $arguX does unquoted expansion if there are quotes, otherwise does normal expansion. With the patch below, here is the behaviour: define show_args   printf "nargs=%d:", $argc   set $i = 0   while $i < $argc     printf " "     eval "echo [$arg%d] [$argu%d]", $i, $i     set $i = $i + 1   end   printf "\n" end (gdb) show_args 'a b' 'c d' e '   f g   ' nargs=4: ['a b'] [a b] ['c d'] [c d] [e] [e] ['   f g   '] [   f g   ] As far as I can see, the above should allow to solve the original problem of Andrew, but without constraints on what can be quoted. This is backward compatible, unless someone already used somewhere $argu followed by one or more digits in a user defined command (and if we want to reduce further the probability of backward incompatibility, we could use something like: $argunquoted0, $argunquoted1, ... but that seems very long :). Patch: diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c index 8496fb85e6..bfeb92dac6 100644 --- a/gdb/cli/cli-script.c +++ b/gdb/cli/cli-script.c @@ -811,7 +811,9 @@ locate_arg (const char *p)    while ((p = strchr (p, '$')))      {        if (startswith (p, "$arg") -         && (isdigit (p[4]) || p[4] == 'c')) +         && (isdigit (p[4])  /* E.g. $arg34.  */ +             || p[4] == 'c'  /* $argc  */ +             || (p[4] == 'u' && isdigit (p[5])))) /* E.g. $argu34.  */         return p;        p++;      } @@ -854,6 +856,10 @@ user_args::insert_args (const char *line) const         {           char *tmp;           unsigned long i; +         bool unquote = p[4] == 'u'; + +         if (unquote) +           p++;             errno = 0;           i = strtoul (p + 4, &tmp, 10); @@ -863,7 +869,13 @@ user_args::insert_args (const char *line) const             error (_("Missing argument %ld in user function."), i);           else             { -             new_line.append (m_args[i].data (), m_args[i].length ()); +             if (unquote +                 && m_args[i].length () >= 2 +                 && m_args[i].data () [0] == '\'' +                 && m_args[i].data () [m_args[i].length () - 1] == '\'') +               new_line.append (m_args[i].data () + 1, m_args[i].length () - 2); +             else +               new_line.append (m_args[i].data (), m_args[i].length ());               line = tmp;             }         } > > This feeling is why I'm generally ok with Andrew's idea. > > Philippe> And it would be nice if the mechanism used to quote args would > Philippe> be compatible between user defined commands and native gdb commands. > > There is no universal quoting in gdb. Yes, and we might maybe avoid to add a new half working new mechanism :). > Instead there are 3 common cases, > plus the extras (neglecting MI, which is totally different and not > relevant here): > > 1. Commands that take an expression. These are things like "print". > Expressions are passed to the language parser, but language parsers > have to follow some gdb rules: optionally terminate parsing at a > top-level (unparenthesized) comma, and also terminate parsing at "if" > or some other things (there is a mildly odd case for Ada tasks). > > 2. Commands that take a linespec. These can also take an expression via > the "*" linespec, so linespecs end up following some expression > rules. Often there are expressions after the linespec too, like > break LINESPEC if EXPR > or > dprintf LINESPEC, "string", EXPR, EXPR... > > 3. Commands that use gdb_argv (or libiberty's buildargv, which is the > same thing). These respect some simple quoting. However, the > quoting is very simple, not like what you might expect from the > shell, for example I don't think you can quote an embedded quotation > mark of the same kind (that is, no "this has \"quotes\""). As far as I can see, gdb_argv is escaping single and double quotes, e.g. (gdb) handle 10 'ignore this \'bidule' Unrecognized or ambiguous flag word: "ignore this 'bidule". (gdb)  > > 4. Everything else. gdb commands are just given a string and some do > whatever the author liked. > > Philippe> I have not understood the reference given by Tom that expressions > Philippe> are terminated with , and that parenthesis stops this termination. > Philippe> Is that described somewhere in the doc ? > Philippe> The doc (or an example if this is not documented) will help > Philippe> me to understand. > > I think it's largely undocumented, since some of these quirks are just > constraints arising from gdb's implementation choices. > > Not having any rhyme or reason to command argument parsing has good and > bad facets. > > The good is that the generally DWIM nature of commands means that > usually you don't have to contort yourself to satisfy some parser. > Like, you can "break foo.c:93" or "break function" rather than something > like the old dbx "stop in" / "stop at" split. > > The bad of course is that you may sometimes want to quote something and > not have any idea of how to do it: because there's no consistency; > because the gdb_argv idea was not really thought through (that's my > conclusion, I don't know the actual story); or perhaps because you've > just tripped across some command that was implemented in a particularly > bad way. > > Now, it would be great to fix this, at least for some subset of things. > I find it hard to see a way forward, though. Breaking compatibility > (see my post upthread) is unpleasant, unless maybe it is done in a very > dramatic way, coupled with a major version bump and some kind of huge > warning for users -- but that's also very hard to implement and release. > > One idea is to add a new standard way to parse arguments, for new > commands. But of course then gdb just has 5 ways to do it ... :( :( But as said above, for user defined command, we might maybe repair what we have now, e.g. using the $argu approach. For sure, the current $arg expansion with quoting is also working (reasonably consistently, except with the fact that you cannot jsut 'pass' a still quoted arg containing a ' to another user defined command ?): (gdb) show_args 'a b' 'c \d e' 'f \\g h' \i \\j 'k \' l' nargs=6: ['a b'] [a b] ['c d e'] [c d e] ['f \g h'] [f \g h] [i] [i] [\j] [\j] ['k ' l'] [k ' l] (so, today, a user defined command can correctly pass a quoted arg to another user defined command, unless this quoted arg initially contained an escaped '). So, in summary, I argue for $argu :). Philippe