From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id AB4841F47A for ; Sun, 8 Oct 2023 11:45:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1696765534; bh=3IBlPs0RlyZTVZo5raIF1ZPX9xhIT9qiOHwrAOLR0ZA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=4b8gYRBMuirvfTM9A8MM2DYdTsHfwsmrhCGiXENqHa0WLDTb2s5Gi00UofeF6J5bl hxKEopRP0LkCHV5JF0tYezjsgo1a/g/wQ5UODd5ZI10MiukB03RglhbPBDP2Hf3v21 RcB2knw2PmsbzC4u/zNRahTuQB9QhjwoThIUutBk= From: Eric Wong To: spew@80x24.org Subject: [PATCH 2/4] lei rediff: use ProcessIO for --drq support Date: Sun, 8 Oct 2023 11:45:32 +0000 Message-ID: <20231008114534.274616-2-e@80x24.org> In-Reply-To: <20231008114534.274616-1-e@80x24.org> References: <20231008114534.274616-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This required fixing binmode support a few commits ago, along with properly enabling autoflush in popen_wr instead of setting it on the wrapper ProcessIO class. --- lib/PublicInbox/LeiRediff.pm | 33 ++++++++++++++------------------- lib/PublicInbox/ProcessIO.pm | 18 +++++------------- lib/PublicInbox/Spawn.pm | 1 + 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/lib/PublicInbox/LeiRediff.pm b/lib/PublicInbox/LeiRediff.pm index b894342b..230f3e83 100644 --- a/lib/PublicInbox/LeiRediff.pm +++ b/lib/PublicInbox/LeiRediff.pm @@ -138,35 +138,30 @@ EOM undef; } -sub wait_requote { # OnDestroy callback - my ($lei, $pid, $old_1) = @_; - $lei->{1} = $old_1; # closes stdin of `perl -pe 's/^/> /'` - waitpid($pid, 0) == $pid or die "BUG(?) waitpid: \$!=$! \$?=$?"; - $lei->child_error($?) if $?; -} +# awaitpid callback +sub wait_requote { $_[1]->child_error($?) if $? } -sub requote ($$) { +sub requote ($$) { # '> ' prefix(es) lei->{1} my ($lei, $pfx) = @_; - my $old_1 = $lei->{1}; - my $opt = { 1 => $old_1, 2 => $lei->{2} }; + my $opt = { 1 => $lei->{1}, 2 => $lei->{2} }; # $^X (perl) is overkill, but maybe there's a weird system w/o sed - my ($w, $pid) = popen_wr([$^X, '-pe', "s/^/$pfx/"], $lei->{env}, $opt); - $w->autoflush(1); - binmode $w, ':utf8'; # incompatible with ProcessIO due to syswrite - $lei->{1} = $w; - PublicInbox::OnDestroy->new(\&wait_requote, $lei, $pid, $old_1); + my $w = popen_wr([$^X, '-pe', "s/^/$pfx/"], $lei->{env}, $opt, + \&wait_requote, $lei); + binmode $w, ':utf8'; + $w; } sub extract_oids { # Eml each_part callback my ($ary, $self) = @_; + my $lei = $self->{lei}; my ($p, undef, $idx) = @$ary; - $self->{lei}->out($p->header_obj->as_string, "\n"); + $lei->out($p->header_obj->as_string, "\n"); my ($s, undef) = msg_part_text($p, $p->content_type || 'text/plain'); defined $s or return; - my $rq; - if ($self->{dqre} && $s =~ s/$self->{dqre}//g) { # '> ' prefix(es) - $rq = requote($self->{lei}, $1) if $self->{lei}->{opt}->{drq}; - } + + $self->{dqre} && $s =~ s/$self->{dqre}//g && $lei->{opt}->{drq} and + local $lei->{1} = requote($lei, $1); + my @top = split($PublicInbox::ViewDiff::EXTRACT_DIFFS, $s); undef $s; my $blobs = $self->{blobs}; # blobs to resolve diff --git a/lib/PublicInbox/ProcessIO.pm b/lib/PublicInbox/ProcessIO.pm index f120edd0..ea5d3e6c 100644 --- a/lib/PublicInbox/ProcessIO.pm +++ b/lib/PublicInbox/ProcessIO.pm @@ -7,6 +7,7 @@ package PublicInbox::ProcessIO; use v5.12; use PublicInbox::DS qw(awaitpid); use Symbol qw(gensym); +use bytes qw(length); sub maybe_new { my ($cls, $pid, $fh, @cb_arg) = @_; @@ -31,25 +32,16 @@ sub TIEHANDLE { $self; } -# for IO::Uncompress::Gunzip -sub BINMODE { - return binmode($_[0]->{fh}) if @_ == 1; - binmode $_[0]->{fh}, $_[1]; -} +# for IO::Uncompress::Gunzip and PublicInbox::LeiRediff +sub BINMODE { @_ == 1 ? binmode($_[0]->{fh}) : binmode($_[0]->{fh}, $_[1]) } sub READ { read($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) } sub READLINE { readline($_[0]->{fh}) } -sub WRITE { - use bytes qw(length); - syswrite($_[0]->{fh}, $_[1], $_[2] // length($_[1]), $_[3] // 0); -} +sub WRITE { syswrite($_[0]->{fh}, $_[1], $_[2] // length($_[1]), $_[3] // 0) } -sub PRINT { - my $self = shift; - print { $self->{fh} } @_; -} +sub PRINT { print { $_[0]->{fh} } @_[1..$#_] } sub FILENO { fileno($_[0]->{fh}) } diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm index ec256698..b1e58f54 100644 --- a/lib/PublicInbox/Spawn.pm +++ b/lib/PublicInbox/Spawn.pm @@ -375,6 +375,7 @@ sub popen_rd { sub popen_wr { my ($cmd, $env, $opt, @cb_arg) = @_; pipe(local $opt->{0}, my $w) or die "pipe: $!\n"; + $w->autoflush(1); my $pid = spawn($cmd, $env, $opt); PublicInbox::ProcessIO->maybe_new($pid, $w, @cb_arg) }