From 65ec1f78464ce8cf4f6379aca7bbae38743c50f0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 26 Mar 2021 09:51:25 +0000 Subject: lei: support /dev/fd/[0-2] inputs and outputs in daemon Since lei-daemon won't have the same FDs as the client, we need to special-case thse mappings and won't be able to open arbitrary, non-standard FDs. We also won't attempt to support /proc/self/fd/[0-2] since that's a Linux-ism. /dev/fd/[0-2] and /dev/std{in,out,err} are portable to FreeBSD, at least. mawk(1) also supports /dev/std{out,err}, as does gawk(1) (which supports everything we can support, and arbitrary /dev/fd/$FD). --- lib/PublicInbox/LeiOverview.pm | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'lib/PublicInbox/LeiOverview.pm') diff --git a/lib/PublicInbox/LeiOverview.pm b/lib/PublicInbox/LeiOverview.pm index 96bfff24..8e26cba4 100644 --- a/lib/PublicInbox/LeiOverview.pm +++ b/lib/PublicInbox/LeiOverview.pm @@ -71,8 +71,9 @@ sub new { --$ofmt_key=$fmt and --output=$ofmt conflict } - $fmt //= 'json' if $dst eq '/dev/stdout'; - $fmt //= detect_fmt($lei, $dst) or return; + + my $devfd = $lei->path_to_fd($dst) // return; + $fmt //= $devfd >= 0 ? 'json' : (detect_fmt($lei, $dst) or return); if (index($dst, '://') < 0) { # not a URL, so assume path $dst = File::Spec->canonpath($dst); @@ -84,11 +85,11 @@ sub new { if ($fmt =~ /\A($JSONL|(?:concat)?json)\z/) { $json = $self->{json} = ref(PublicInbox::Config->json); } - if ($dst eq '/dev/stdout') { - my $isatty = $lei->{need_pager} = -t $lei->{1}; + if ($devfd >= 0) { + my $isatty = $lei->{need_pager} = -t $lei->{$devfd}; $opt->{pretty} //= $isatty; if (!$isatty && -f _) { - my $fl = fcntl($lei->{1}, F_GETFL, 0) // + my $fl = fcntl($lei->{$devfd}, F_GETFL, 0) // return $lei->fail("fcntl(stdout): $!"); ovv_out_lk_init($self) unless ($fl & O_APPEND); } else { @@ -101,7 +102,7 @@ sub new { $lei->{dedupe} //= PublicInbox::LeiDedupe->new($lei); } else { # default to the cheapest sort since MUA usually resorts - $opt->{'sort'} //= 'docid' if $dst ne '/dev/stdout'; + $opt->{'sort'} //= 'docid' if $devfd < 0; $lei->{l2m} = eval { PublicInbox::LeiToMail->new($lei) }; return $lei->fail($@) if $@; if ($opt->{mua} && $lei->{l2m}->lock_free) { -- cgit v1.2.3-24-ge0c7