Re: [PATCH] kernel-shark: Provide parsing for quotation marks in Record command line

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 17 Sep 2019 13:44:52 +0300
"Yordan Karadzhov (VMware)" <y.karadz@xxxxxxxxx> wrote:

> >> 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.

I'm fine with that as you will be maintaining it. But it also needs to
be correct.

> 
> 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('\\'));

What does the above do? Removes all backslashes? Why? A backslashed
quote must be ignored.

 '\'' is a single quote within quotes.

Not to mention, a backslash keeps whatever it backslashed:

 \$ == $
 \' == '
 \" == "
 \1 == 1
 \\ == \

I don't see how this code handles this, as my code does.

-- Steve


> 	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  
> >   




[Index of Archives]     [Linux USB Development]     [Linux USB Development]     [Linux Audio Users]     [Yosemite Hiking]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux