Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed command parsing with -e option #961

Merged
merged 3 commits into from
Sep 3, 2022
Merged

Fixed command parsing with -e option #961

merged 3 commits into from
Sep 3, 2022

Conversation

tsujan
Copy link
Member

@tsujan tsujan commented Jun 6, 2022

By using QStringList instead of QString.

Fixes #335 and fixes #665

@tsujan tsujan requested a review from yan12125 June 6, 2022 16:47
@tsujan
Copy link
Member Author

tsujan commented Jun 7, 2022

The executable command (that comes after -e) should also be parsed, in addition to being considered like a string list. I added a commit for that.

Now, in addition to

qterminal -e APP -option "/path/ha ha"
qterminal -e APP -option '/path/ha ha'

All following combinations also work with QTerminal, as they work with xterm:

qterminal -e 'APP -option "/path/ha ha"'
qterminal -e "APP -option \"/path/ha ha\""
qterminal -e "APP -option '/path/ha ha'"

Copy link
Member

@yan12125 yan12125 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great improvements! Haven't checked details, but I suspect parse_command() may be fragile to future changes. Could you add unit tests? I will find time to setup CI for this repo.

src/main.cpp Outdated Show resolved Hide resolved
@tsujan
Copy link
Member Author

tsujan commented Jun 20, 2022

I added a commit for raw string literals.

Unfortunately, I don't have time to add unit tests here — maybe later and in another PR — but, for example, if FeatherPad is installed, the patch can be tested by creating a text file PATH/ha ha (PATH can also contain spaces) and doing:

qterminal -e fpad -s "PATH/ha ha"
qterminal -e fpad -s 'PATH/ha ha'
qterminal -e fpad -s "PATH/ha"\ ha
qterminal -e fpad -s PATH/ha\ ha
qterminal -e 'fpad -s "PATH/ha ha"'
qterminal -e "fpad -s \"PATH/ha ha\""
qterminal -e "fpad -s 'PATH/ha ha'"
qterminal -e 'fpad -s PATH/ha\ ha'
qterminal -e "fpad -s PATH/ha\ ha"

Of course, the home path can be replaced by ~ only when there is no single/double quote, like in:

qterminal -e fpad -s ~/SUB_PATH/ha\ ha

@tsujan
Copy link
Member Author

tsujan commented Jul 21, 2022

I suspect parse_command() may be fragile to future changes.

Strange that I didn't see this important comment!

@yan12125, IMHO, it isn't fragile — because it isn't about the changes in Qterminal — it's either correct or incorrect.

Maybe by "fragile" you meant "dirty". As far as I've seen, parsing codes are usually dirty, because of their complex logic.

@yan12125
Copy link
Member

By fragile, I mean if someone else wants to modify that function to support more command patterns, parsing for existing patterns may get broken (regression). Adding tests ensures that won't happen.

I can try to turn current examples into tests this week.

@tsujan
Copy link
Member Author

tsujan commented Aug 26, 2022

By fragile, I mean if someone else wants to modify that function to support more command patterns, parsing for existing patterns may get broken

Yes, because of its complex logic. By "complex", I mean a bad thing, which I think is inevitable in this case. A worse example of it can be found in https://github.com/lxqt/libqtxdg/blob/master/src/qtxdg/xdgdesktopfile.cpp.

@yan12125
Copy link
Member

@tsujan
Copy link
Member Author

tsujan commented Aug 26, 2022

A good news is that comes with tests ;)

I never said I'm against adding tests — why should I? I just can't add them myself, due to lack of time.

@yan12125
Copy link
Member

Added tests ;)

By the way, during testing, I noticed that xterm lets the shell to handle complex commands. That might be simpler and more reliable.

$ strace -f -e trace=execve xterm -e 'fpad -s \"PATH/ha ha\"'
execve("/usr/bin/xterm", ["xterm", "-e", "fpad -s \\\"PATH/ha ha\\\""], 0x7ffea7d99e18 /* 102 vars */) = 0
[pid 140230] execve("fpad -s \\\"PATH/ha ha\\\"", ["fpad -s \\\"PATH/ha ha\\\""], 0x556787ea45a0 /* 103 vars */) = -1 ENOENT (沒有此一檔案或目錄)
[pid 140230] execve("/usr/bin/zsh", ["zsh", "-c", "fpad -s \\\"PATH/ha ha\\\""], 0x556787ea45a0 /* 103 vars */) = 0
[pid 140230] execve("/usr/bin/fpad", ["fpad", "-s", "\"PATH/ha", "ha\""], 0x7ffec49129f8 /* 103 vars */) = 0

See: https://github.com/ThomasDickey/xterm-snapshots/blob/master/main.c#L5141

tsujan and others added 3 commits August 29, 2022 17:34
By using `QStringList` instead of `QString`.

Fixes #335 and fixes #665
Also extracts parseCommand() to a separate file for simpler testing
setup.
@yan12125
Copy link
Member

yan12125 commented Aug 29, 2022

(Rebased so that tests are run on CI)

@tsujan
Copy link
Member Author

tsujan commented Aug 29, 2022

Thanks for the commits!

I noticed that xterm lets the shell to handle complex commands.

Interesting, and worth experimenting.

@tsujan
Copy link
Member Author

tsujan commented Aug 29, 2022

As for xterm, I remember that its behavior (not its code) was my criterion. In some complex cases (which I don't remember), Konsole's behavior was different from that of xterm and QTermnal, but they were too artificial.

That being said, counterexamples are very welcome.

@yan12125
Copy link
Member

yan12125 commented Sep 3, 2022

Speaking of complex cases, there is a difference between this pull request and xterm:

Command from the user Parsed command
xterm -e 'fpad -s \"PATH/ha ha\"' fpad, -s, "PATH/ha, ha"
qterminal -e 'fpad -s \"PATH/ha ha\"' fpad, -s, \"PATH/ha, ha\"

Anyway, I think it's OK to have different results for complex cases. It's fine as long as parsing logic is reasonable and consistent.

PS. I use zsh, so xterm calls zsh to parse the command as shown in my earlier comment.

@tsujan
Copy link
Member Author

tsujan commented Sep 3, 2022

Speaking of complex cases, there is a difference between this pull request and xterm:

I use bash and the behaviors are exactly the same. I think you've found a problem (or inconsistency) in zsh ;)

@yan12125
Copy link
Member

yan12125 commented Sep 3, 2022

I use bash and the behaviors are exactly the same. I think you've found a problem (or inconsistency) in zsh ;)

Interesting! So parsing arguments within qterminal is better. That avoids differences among shells.

@yan12125
Copy link
Member

yan12125 commented Sep 3, 2022

I think this can be merged now?

@tsujan
Copy link
Member Author

tsujan commented Sep 3, 2022

So parsing arguments within qterminal is better.

I think there's no strict standard. Here, I just followed the rules that I considered "obvious". Of course, they could be changed if needed.

I think this can be merged now?

IMHO, the sooner git users have it, the better. The next release will be made soon.

@yan12125
Copy link
Member

yan12125 commented Sep 3, 2022

I think there's no strict standard. Here, I just followed the rules that I considered "obvious". Of course, they could be changed if needed.

Sure!

IMHO, the sooner git users have it, the better. The next release will be made soon.

Oh, good reminder! Let me merge this first and check other PRs.

@yan12125 yan12125 merged commit 5605b8c into master Sep 3, 2022
@yan12125 yan12125 deleted the fix_e_option branch September 3, 2022 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

double quotes is aumatically escaped as a part of the command qterminal execution breaks up arguments
2 participants