emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Fix `grep' command to handle filenames starting with '-'.
@ 2024-11-07  9:24 Xi Lu
  2024-11-07 10:40 ` Rens Oliemans
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Xi Lu @ 2024-11-07  9:24 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: Xi Lu

* lisp/org-mouse.el (org-mouse-context-menu): Fix `grep' command to
handle filenames starting with '-' by adjusting wildcard usage to
prevent misinterpretation as options.
---
 lisp/org-mouse.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/org-mouse.el b/lisp/org-mouse.el
index 322d98682..7f85653f2 100644
--- a/lisp/org-mouse.el
+++ b/lisp/org-mouse.el
@@ -627,7 +627,7 @@ This means, between the beginning of line and the point."
 	   ["Sparse Tree" (org-occur ',region-string)]
 	   ["Find in Buffer" (occur ',region-string)]
 	   ["Grep in Current Dir"
-	    (grep (format "grep -rnH -e '%s' *" ',region-string))]
+	    (grep (format "grep -rnH -e '%s' ./*" ',region-string))]
 	   ["Grep in Parent Dir"
 	    (grep (format "grep -rnH -e '%s' ../*" ',region-string))]
 	   "--"
-- 
2.45.2




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] Fix `grep' command to handle filenames starting with '-'.
  2024-11-07  9:24 [PATCH] Fix `grep' command to handle filenames starting with '-' Xi Lu
@ 2024-11-07 10:40 ` Rens Oliemans
  2024-11-07 10:44   ` lux
  2024-11-08  3:46 ` Max Nikulin
  2024-11-09 14:35 ` Ihor Radchenko
  2 siblings, 1 reply; 6+ messages in thread
From: Rens Oliemans @ 2024-11-07 10:40 UTC (permalink / raw)
  To: Xi Lu, emacs-orgmode; +Cc: Xi Lu

Xi Lu <lx@shellcodes.org> writes:

> -	    (grep (format "grep -rnH -e '%s' *" ',region-string))]
> +	    (grep (format "grep -rnH -e '%s' ./*" ',region-string))]

This works so my comment might be a bit too bikesheddy, but another method would be to
append the argument `--` after the list of options to ensure that the rest of the
arguments aren't parsed as options; perhaps that's preferred.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Fix `grep' command to handle filenames starting with '-'.
  2024-11-07 10:40 ` Rens Oliemans
@ 2024-11-07 10:44   ` lux
  0 siblings, 0 replies; 6+ messages in thread
From: lux @ 2024-11-07 10:44 UTC (permalink / raw)
  To: Rens Oliemans, emacs-orgmode

On Thu, 2024-11-07 at 11:40 +0100, Rens Oliemans wrote:
> Xi Lu <lx@shellcodes.org> writes:
> 
> > -	    (grep (format "grep -rnH -e '%s' *" ',region-string))]
> > +	    (grep (format "grep -rnH -e '%s' ./*" ',region-string))]
> 
> This works so my comment might be a bit too bikesheddy, but another method
> would be to
> append the argument `--` after the list of options to ensure that the rest of
> the
> arguments aren't parsed as options; perhaps that's preferred.

Yes, but I haven't tested it on other Unix-like platforms, so I'm not sure if "-
-" will work properly on all of them.









^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Fix `grep' command to handle filenames starting with '-'.
  2024-11-07  9:24 [PATCH] Fix `grep' command to handle filenames starting with '-' Xi Lu
  2024-11-07 10:40 ` Rens Oliemans
@ 2024-11-08  3:46 ` Max Nikulin
  2024-11-08  3:56   ` lux
  2024-11-09 14:35 ` Ihor Radchenko
  2 siblings, 1 reply; 6+ messages in thread
From: Max Nikulin @ 2024-11-08  3:46 UTC (permalink / raw)
  To: Xi Lu, emacs-orgmode

On 07/11/2024 16:24, Xi Lu wrote:
> +++ b/lisp/org-mouse.el
> @@ -627,7 +627,7 @@ This means, between the beginning of line and the point."
>   	   ["Sparse Tree" (org-occur ',region-string)]
>   	   ["Find in Buffer" (occur ',region-string)]
>   	   ["Grep in Current Dir"
> -	    (grep (format "grep -rnH -e '%s' *" ',region-string))]
> +	    (grep (format "grep -rnH -e '%s' ./*" ',region-string))]

Thanks, I find it as an acceptable quick fix despite it adds noisy "./" 
to file names that are not started with "-". In BASH it is possible to 
do something like

     shopt -s nullglob; echo ./-* [!-]*

but it is not portable (and portable [!CHAR] negation may be treated as 
history expansion by some *interactive* shells).

Xi Lu, is it a kind of friendly trolling that you submitted a patch with 
a workaround for leading dash file name pitfall, but you decided to keep 
ability to pass arbitrary shell code as selection? Just quotes is a poor 
way to protect against shell specials and it can be easily bypassed.

P.S. This code needs some love. I would expect `grep-command' or 
`grep-find-command' variable should be respected here. There are grep 
alternatives that respect VCS ignore files and thus work much faster. 
However it is for another patch, not for this thread.

I am in doubts if "-F" should be added. Literal search for *selected* 
text is more reasonable from my point of view.

     grep -rnH -e '[aa' ./*
     grep: Unmatched [, [^, [:, [., or [=

P.P.S. Some links for the context of leading dash issues:
- <https://www.shellcheck.net/wiki/SC2035>
   "Use ./*glob* or -- *glob* so names with dashes won't become options."
- <https://www.openwall.com/lists/oss-security/2024/11/06/1>
   "shell wildcard expansion (un)safety" Wed, 6 Nov 2024 05:12:15 +0100
- <https://mywiki.wooledge.org/BashPitfalls#pf3>
   3. Filenames with leading dashes



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Fix `grep' command to handle filenames starting with '-'.
  2024-11-08  3:46 ` Max Nikulin
@ 2024-11-08  3:56   ` lux
  0 siblings, 0 replies; 6+ messages in thread
From: lux @ 2024-11-08  3:56 UTC (permalink / raw)
  To: emacs-orgmode

On Fri, 2024-11-08 at 10:46 +0700, Max Nikulin wrote:
> 
> Xi Lu, is it a kind of friendly trolling that you submitted a patch with 
> a workaround for leading dash file name pitfall, but you decided to keep 
> ability to pass arbitrary shell code as selection? Just quotes is a poor 
> way to protect against shell specials and it can be easily bypassed.
> 

No, I was just aiming for a quick and simple solution to address the issue.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Fix `grep' command to handle filenames starting with '-'.
  2024-11-07  9:24 [PATCH] Fix `grep' command to handle filenames starting with '-' Xi Lu
  2024-11-07 10:40 ` Rens Oliemans
  2024-11-08  3:46 ` Max Nikulin
@ 2024-11-09 14:35 ` Ihor Radchenko
  2 siblings, 0 replies; 6+ messages in thread
From: Ihor Radchenko @ 2024-11-09 14:35 UTC (permalink / raw)
  To: Xi Lu; +Cc: emacs-orgmode

Xi Lu <lx@shellcodes.org> writes:

> * lisp/org-mouse.el (org-mouse-context-menu): Fix `grep' command to
> handle filenames starting with '-' by adjusting wildcard usage to
> prevent misinterpretation as options.

Thanks!
Applied, onto main.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=0624f57df3

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-11-09 14:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-07  9:24 [PATCH] Fix `grep' command to handle filenames starting with '-' Xi Lu
2024-11-07 10:40 ` Rens Oliemans
2024-11-07 10:44   ` lux
2024-11-08  3:46 ` Max Nikulin
2024-11-08  3:56   ` lux
2024-11-09 14:35 ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).