9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] [PATCH] g: use xargs instead of finding complete file list before greping
@ 2022-01-01 10:26 Michael Forney
  2022-01-02  1:13 ` 有澤 健治
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Michael Forney @ 2022-01-01 10:26 UTC (permalink / raw)
  To: 9front

---
This patch requires the other g patch I sent earlier to apply cleanly.

I noticed that with the recent rc changes, I see the message 'Write
error' on stderr when using g in a large directory (such as
/sys/src/cmd).  This is because rc now uses io.c code to write env
files before executing a child, and this fails when the size of a list
exceeds Maxenvsize.  Previously, these write errors were just ignored.

It can be reproduced with

	% a=`{seq 1 4000}
	% echo
	Write error
	
	%

I'm not sure if this error message is desired or not for cases like
this, but regardless, I think g could be a bit more efficient by using
xargs rather than enumerating the complete list of files up front,
avoiding the issue completely.

One slight issue with this patch is that files named '-n' are skipped.
I don't think it's too big a deal, but I wasn't quite sure how to
handle this.  One idea is to use `walk -f -- $f` instead of `echo $f`.
Another idea is `echo -n $f$nl`.  In general, is there a good way to
print a variable, regardless of its value (similar to POSIX `printf
'%s\n' "$f"`)?

diff 525c7bc4922fc86ba0bbe2281fbe92e697a4f6d8 dd997a744e5ec3704d48dedc259d06c062d56aec
--- a/rc/bin/g	Sat Jan  1 14:51:39 2022
+++ b/rc/bin/g	Sat Jan  1 02:26:14 2022
@@ -14,25 +14,24 @@
 }
 if(~ $1 --)
 	shift
+if(~ $#* 0) {
+	echo 'usage: g [flags] pattern [files]' >[1=2]
+	exit usage
+}
+pattern=$1
+shift
 
 suffixes='\.([bcChlmsy]|asm|awk|cc|cgi|cpp|cs|go|goc|hs|java|lua|lx|mk|ml|mli|ms|myr|pl|py|rc|sh|tex|xy)$'
 fullnames='(^|/)mkfile$'
 switch($#*){
 case 0
-	echo 'usage: g [flags] pattern [files]' >[1=2]
-	exit usage
-case 1
-	pattern=$1
-	files=`$nl{walk -f $recurse | grep -e $fullnames -e $suffixes >[2]/dev/null}
+	walk -f $recurse | grep -e $fullnames -e $suffixes >[2]/dev/null
 case *
-	pattern=$1
-	shift
 	for(f in $*){
 		if(test -d $f)
-			files=($files `$nl{walk -f $recurse -- $f \
-				| grep -e $fullnames -e $suffixes >[2]/dev/null})
+			walk -f $recurse -- $f \
+				| grep -e $fullnames -e $suffixes >[2]/dev/null
 		if not
-			files=($files $f)
+			echo $f
 	}
-}
-grep -n $flags -- $pattern $files /dev/null
+} | xargs grep -n $flags -- $pattern

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

end of thread, other threads:[~2022-01-06 10:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-01 10:26 [9front] [PATCH] g: use xargs instead of finding complete file list before greping Michael Forney
2022-01-02  1:13 ` 有澤 健治
2022-01-02  1:20   ` ori
2022-01-02  4:56     ` 有澤 健治
2022-01-02  2:34   ` Michael Forney
2022-01-02  1:28 ` 有澤 健治
2022-01-05  0:05 ` igor
2022-01-05  3:05   ` Michael Forney
2022-01-06  1:35     ` ori
2022-01-06 10:47       ` igor

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