From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 025401FFB0 for ; Fri, 18 Dec 2020 12:09:53 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 25/26] lei: revise output routines Date: Fri, 18 Dec 2020 12:09:49 +0000 Message-Id: <20201218120950.23272-26-e@80x24.org> In-Reply-To: <20201218120950.23272-1-e@80x24.org> References: <20201218120950.23272-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Drop emit(), since we hard code the channel (client FD) 99% of the time and use prototypes to avoid parentheses because my hands are tired. --- lib/PublicInbox/LEI.pm | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index c28c9b59..97c5d91b 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -250,18 +250,13 @@ sub x_it ($$) { # pronounced "exit" sub puts ($;@) { print { shift->{1} } map { "$_\n" } @_ } -sub emit { - my ($self, $channel) = @_; # $buf = $_[2] - print { $self->{$channel} } $_[2] or die "print FD[$channel]: $!"; -} +sub out ($;@) { print { shift->{1} } @_ } -sub err { - my ($self, $buf) = @_; - $buf .= "\n" unless $buf =~ /\n\z/s; - emit($self, 2, $buf); +sub err ($;@) { + print { shift->{2} } @_, (substr($_[-1], -1, 1) eq "\n" ? () : "\n"); } -sub qerr { $_[0]->{opt}->{quiet} or err(@_) } +sub qerr ($;@) { $_[0]->{opt}->{quiet} or err(shift, @_) } sub fail ($$;$) { my ($self, $buf, $exit_code) = @_; @@ -341,8 +336,7 @@ EOF $msg .= $rhs; $msg .= "\n"; } - my $channel = $errmsg ? 2 : 1; - emit($self, $channel, $msg); + print { $self->{$errmsg ? 2 : 1} } $msg; x_it($self, $errmsg ? 1 << 8 : 0); # stderr => failure undef; } @@ -404,7 +398,6 @@ sub optparse ($$$) { } last if $err; } - # warn "inf=$inf ".scalar(@$argv). ' '.scalar(@args)."\n"; if (!$inf && scalar(@$argv) > scalar(@args)) { $err //= 'too many arguments'; } @@ -413,7 +406,7 @@ sub optparse ($$$) { sub dispatch { my ($self, $cmd, @argv) = @_; - local $SIG{__WARN__} = sub { err($self, "@_") }; + local $SIG{__WARN__} = sub { err($self, @_) }; return _help($self, 'no command given') unless defined($cmd); my $func = "lei_$cmd"; $func =~ tr/-/_/; @@ -525,7 +518,7 @@ E: leistore.dir=$cur already initialized and it is not $dir return qerr($self, $exists); } -sub lei_daemon_pid { emit($_[0], 1, "$$\n") } +sub lei_daemon_pid { puts shift, $$ } sub lei_daemon_kill { my ($self) = @_; @@ -547,7 +540,7 @@ sub lei_daemon_env { my $eor = $opt->{z} ? "\0" : "\n"; my $buf = ''; while (my ($k, $v) = each %ENV) { $buf .= "$k=$v$eor" } - emit($self, 1, $buf) + out $self, $buf; } }