9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Michael Forney <mforney@mforney.org>
To: 9front@9front.org
Subject: [9front] [PATCH] g: use xargs instead of finding complete file list before greping
Date: Sat, 01 Jan 2022 10:26:14 +0000	[thread overview]
Message-ID: <F6C25774810194975DF835D286F1D76D@arrow> (raw)

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

             reply	other threads:[~2022-01-02  0:22 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-01 10:26 Michael Forney [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=F6C25774810194975DF835D286F1D76D@arrow \
    --to=mforney@mforney.org \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).