From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: linux-trace-devel@vger.kernel.org, Stephen Brennan <stephen@brennan.io>
Subject: Re: [PATCH] kernel-shark: Provide parsing for quotation marks in Record command line
Date: Tue, 17 Sep 2019 13:44:52 +0300 [thread overview]
Message-ID: <41a6b643-ce85-967e-a47f-99c32f1e2d6a@gmail.com> (raw)
In-Reply-To: <20190828093007.0b0bdabb@gandalf.local.home>
On 28.08.19 г. 14:30 ч., Steven Rostedt wrote:
> On Wed, 28 Aug 2019 09:25:33 -0400
> Steven Rostedt <rostedt@goodmis.org> wrote:
>
>> On Tue, 27 Aug 2019 16:14:18 +0300
>> "Yordan Karadzhov (VMware)" <y.karadz@gmail.com> wrote:
>>
>>> + cmdTmp = _commandLineEdit.text();
>>> + if (!cmdTmp.contains('\'') && !cmdTmp.contains('\"')) {
>>> + /* Split all command line arguments. */
>>> + argv << cmdTmp.split(" ", opt);
>>> + } else {
>>> + int iOpenQuots, iCloseQuots, size = cmdTmp.size();
>>> + int iSingleQuots = (cmdTmp.count('\'') == 2) ? cmdTmp.indexOf('\'') : size;
>>> + int iDoubleQuots = (cmdTmp.count('\"') == 2) ? cmdTmp.indexOf('\"') : size;
>>> +
>>> + if (iSingleQuots < iDoubleQuots) {
>>> + iOpenQuots = iSingleQuots;
>>> + iCloseQuots = cmdTmp.lastIndexOf('\'') + 1;
>>> + } else if (iDoubleQuots < iSingleQuots) {
>>> + iOpenQuots = iDoubleQuots;
>>> + iCloseQuots = cmdTmp.lastIndexOf('\"') + 1;
>>> + } else {
>>> + emit print("\nERROR: Unable to parse the command.");
>>> + return {};
>>> + }
>>> +
>>> + /* Split the arguments. */
>>> + argv << cmdTmp.left(iOpenQuots).split(" ", opt);
>>> +
>>> + /* Everything in between the quotation marks goes in one piece. */
>>> + argv << cmdTmp.mid(iOpenQuots, iCloseQuots - iOpenQuots);
>>> +
>>> + /* Split the rest of the arguments. */
>>> + argv << cmdTmp.right(size - iCloseQuots).split(" ", opt);
>>> + }
>>
>> This is where I hate C++, because it makes simple things so
>> complicated ;-)
>>
>> What we need to do is simply:
>>
>> char *str = _commandLineEdit.text();
>> char *last_word = str;
>> char quote = 0;
>> int i;
>>
>> // remove front and end spaces
>> while (isspace(*str))
>> str++;
>> i = strlen(str);
>> while (i > 0 && isspace(str[i-1])
>> i--;
>> str[i] = 0;
>
> Oops, need to have:
>
> last_word = &str[i];
>
> here.
>
> -- Steve
>
>>
>> for (i = 0; str[i]; i++) {
>> if (isspace(str[i]) && !quote) {
>> str[i++] = 0;
>> argv << last_word;
>> while (isspace(str[i]))
>> i++;
>> last_word = &str[i];
>> }
>> switch(str[i]) {
>> case '\\':
>> i++;
>> break;
>> case '\'':
>> case '"':
>> if (!quote)
>> quote = str[i];
>> else if (quote == str[i])
>> quote = 0;
>> break;
>> }
>> }
>> argv << last_word;
>>
>> Note, the above may be buggy, I didn't test it.
>>
Hi Steve,
I understand very well your feelings ;)
However, I also really dislike eclectic programming stiles. Since this
is part of the C++/Qt code, I would prefer to stick to the C++/Qt way of
doing things.
Here is a version of your code that is more C++/Qt-ish:
QString::SplitBehavior opt = QString::SkipEmptyParts;
QChar quote = 0;
int i, progress = 0, size;
cmd.remove(QChar('\\'));
size = cmd.count();
auto lamMid = [&] () {return cmd.mid(progress, i - progress);};
for (i = 0; i < size; ++i) {
if (cmd[i] == '\'' || cmd[i] == '"') {
if (quote.isNull()) {
args << lamMid().split(" ", opt);
quote = cmd[i++];
progress = i;
} else if (quote == cmd[i]) {
args << lamMid();
quote = 0;
progress = ++i;
}
}
}
args << cmd.right(size - progress).split(" ", opt);
If you are OK with this I will send a new version of the patch.
Thanks!
Yordan
>> -- Steve
>
next prev parent reply other threads:[~2019-09-17 10:44 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-27 13:14 [PATCH] kernel-shark: Provide parsing for quotation marks in Record command line Yordan Karadzhov (VMware)
2019-08-28 5:10 ` stephen
2019-08-28 13:06 ` Steven Rostedt
2019-08-28 14:10 ` Yordan Karadzhov (VMware)
2019-08-28 14:56 ` Steven Rostedt
2019-08-28 13:25 ` Steven Rostedt
2019-08-28 13:30 ` Steven Rostedt
2019-09-17 10:44 ` Yordan Karadzhov (VMware) [this message]
2019-09-17 13:17 ` Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=41a6b643-ce85-967e-a47f-99c32f1e2d6a@gmail.com \
--to=y.karadz@gmail.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=stephen@brennan.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).