~emersion/hut-dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
6 2

[PATCH v2] pager/editor: Invoke command with "sh -c"

Details
Message ID
<20240131210409.410546-1-admin@xenrox.net>
DKIM signature
pass
Download raw message
Patch: +2 -2
Closes: https://todo.sr.ht/~emersion/hut/44
---
v2: Handle leading dashes and spaces in file path

 main.go  | 2 +-
 pager.go | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/main.go b/main.go
index 7c05bee..825815a 100644
--- a/main.go
+++ b/main.go
@@ -152,7 +152,7 @@ func getInputWithEditor(pattern, initialText string) (string, error) {
		return "", err
	}

	cmd := exec.Command(editor, file.Name())
	cmd := exec.Command("sh", "-c", fmt.Sprintf("%s -- \"%s\"", editor, file.Name()))
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
diff --git a/pager.go b/pager.go
index 7615f89..424d07b 100644
--- a/pager.go
+++ b/pager.go
@@ -24,7 +24,7 @@ func newPager() pager {
		name = "less"
	}

	cmd := exec.Command(name)
	cmd := exec.Command("sh", "-c", name)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Env = append(os.Environ(), "LESS=FRX")

base-commit: c096589b7ba247f939886649c5d58b8f1cc95fd4
--
2.43.0
Details
Message ID
<ULlCEY_DnHK2FGWKAvUF0FpxZDVC6u1TI9lqmgEhV9akCpQonhnm0i_7W71Al7AMg-2voiveFiMiLN8lwsQz6FoC0MRW7nrtqZV7SNXI68M=@emersion.fr>
In-Reply-To
<20240131210409.410546-1-admin@xenrox.net> (view parent)
DKIM signature
pass
Download raw message
Hm, but then this doesn't handle quotes in the filename. What I mean was
something like:

    exec.Command("sh", "-c", editor + ` "$@"`, "--", file.Name())

Re: Re: [PATCH v2] pager/editor: Invoke command with "sh -c"

Details
Message ID
<zvzwndt5iik5ovgbjcw4dgrpbytkpu5vkefpk3n5snwrqji2dc@xokyghqlgsxm>
In-Reply-To
<ULlCEY_DnHK2FGWKAvUF0FpxZDVC6u1TI9lqmgEhV9akCpQonhnm0i_7W71Al7AMg-2voiveFiMiLN8lwsQz6FoC0MRW7nrtqZV7SNXI68M=@emersion.fr> (view parent)
DKIM signature
pass
Download raw message
On Wed, Jan 31, 2024 at 09:07:51PM +0000, Simon Ser wrote:
> Hm, but then this doesn't handle quotes in the filename. What I mean was
> something like:
>
>     exec.Command("sh", "-c", editor + ` "$@"`, "--", file.Name())

I really could not find a solution that would work for all edge cases.

Your proposal above works for quotes but fails if the path starts with a dash:
export TMPDIR="-hut"

Re: Re: [PATCH v2] pager/editor: Invoke command with "sh -c"

Details
Message ID
<SFnuDX1uS7_Lo4oh_cfqRD62Gmzx37JIltjXDdt4ZBxK__Y3c9f_F_5bQDk2n6SWRnody3Mwd6P-_iig_tebfgCUrfpXtSZDOs3CnPIa518=@emersion.fr>
In-Reply-To
<zvzwndt5iik5ovgbjcw4dgrpbytkpu5vkefpk3n5snwrqji2dc@xokyghqlgsxm> (view parent)
DKIM signature
pass
Download raw message
On Thursday, February 1st, 2024 at 14:09, Thorben Günther <admin@xenrox.net> wrote:

> Your proposal above works for quotes but fails if the path starts with a dash:
> export TMPDIR="-hut"

Seems like git does it like I suggested [1].

TBH I'm tempted to just ask users to use an intermediary shell script if
they want to spawn an editor with a shell.

[1]: https://github.com/git/git/blob/bc7ee2e5e16f0d1e710ef8fab3db59ab11f2bbe7/run-command.c#L295
Details
Message ID
<pjvs7htptqrevi7vgiiqqtq6tgywkxuyaasudmk57of7skgyvj@j4pmnarxcqic>
In-Reply-To
<SFnuDX1uS7_Lo4oh_cfqRD62Gmzx37JIltjXDdt4ZBxK__Y3c9f_F_5bQDk2n6SWRnody3Mwd6P-_iig_tebfgCUrfpXtSZDOs3CnPIa518=@emersion.fr> (view parent)
DKIM signature
pass
Download raw message
On Thu, Feb 01, 2024 at 02:13:50PM +0000, Simon Ser wrote:
> Seems like git does it like I suggested [1].
>
> TBH I'm tempted to just ask users to use an intermediary shell script if
> they want to spawn an editor with a shell.
>
> [1]: https://github.com/git/git/blob/bc7ee2e5e16f0d1e710ef8fab3db59ab11f2bbe7/run-command.c#L295

When invoked from the terminal your suggestion works perfectly fine for
all 3 tested edge cases (space, quotes, starting with dash).
Just with exec.Command it fails for "-test". Seems like go parses it a
bit differently...

My favourite solution would be to just add shlex to what we have right
now and call it a day.
Details
Message ID
<YBTRAnPexv_MhjKYKEosEHqRb0oVAaaZfAZBwdiy2CRiv21RI9UbxG9Uatx375EA77kwAnK-u3pq0uowwe-rESa_cvaCZpDvC6_ba4KJnBA=@emersion.fr>
In-Reply-To
<pjvs7htptqrevi7vgiiqqtq6tgywkxuyaasudmk57of7skgyvj@j4pmnarxcqic> (view parent)
DKIM signature
pass
Download raw message
On Thursday, February 1st, 2024 at 15:20, Thorben Günther <admin@xenrox.net> wrote:

> On Thu, Feb 01, 2024 at 02:13:50PM +0000, Simon Ser wrote:
> 
> > Seems like git does it like I suggested 1.
> > 
> > TBH I'm tempted to just ask users to use an intermediary shell script if
> > they want to spawn an editor with a shell.
> 
> 
> When invoked from the terminal your suggestion works perfectly fine for
> all 3 tested edge cases (space, quotes, starting with dash).
> Just with exec.Command it fails for "-test". Seems like go parses it a
> bit differently...

Weird, it consistently fails from the command line for me. Tried both
dash and bash.

What seems to work is sh -c -- '%s "$@"' '-asdf'.

> My favourite solution would be to just add shlex to what we have right
> now and call it a day.

That'd be fine by me as well.
Details
Message ID
<i5el6acmmhn2xmraml7z2kfsrtqodqk3yqbrrsqlaimjacldni@pvvb7g6aazwx>
In-Reply-To
<YBTRAnPexv_MhjKYKEosEHqRb0oVAaaZfAZBwdiy2CRiv21RI9UbxG9Uatx375EA77kwAnK-u3pq0uowwe-rESa_cvaCZpDvC6_ba4KJnBA=@emersion.fr> (view parent)
DKIM signature
pass
Download raw message
On Thu, Feb 01, 2024 at 02:30:55PM +0000, Simon Ser wrote:

> Weird, it consistently fails from the command line for me. Tried both
> dash and bash.
>
> What seems to work is sh -c -- '%s "$@"' '-asdf'.

With my zsh setup it works, but yeah dash fails.
Honestly this all feels like too much "magic" to me, so I sent a shlex
patch.
Reply to thread Export thread (mbox)