}
} else { # worker, Net::NNTP (Net::Cmd) uses STDERR directly
open STDERR, '+>&='.fileno($self->{2}) or warn "open $!";
- delete $self->{0};
}
for (delete @$self{qw(3 old_1 au_done)}) {
close($_) if defined($_);
}
my %path_to_fd = ('/dev/stdin' => 0, '/dev/stdout' => 1, '/dev/stderr' => 2);
-$path_to_fd{"/dev/fd/$_"} = $path_to_fd{"/proc/self/fd/$_"} for (0..2);
-sub fopen {
- my ($self, $mode, $path) = @_;
- rel2abs($self, $path);
+$path_to_fd{"/dev/fd/$_"} = $_ for (0..2);
+
+# this also normalizes the path
+sub path_to_fd {
+ my ($self, $path) = @_;
+ $path = rel2abs($self, $path);
$path =~ tr!/!/!s;
- if (defined(my $fd = $path_to_fd{$path})) {
- return $self->{$fd};
- }
- if ($path =~ m!\A/(?:dev|proc/self)/fd/[0-9]+\z!) {
- return fail($self, "cannot open $path from daemon");
- }
- open my $fh, $mode, $path or return;
- $fh;
+ $path_to_fd{$path} // (
+ ($path =~ m!\A/(?:dev|proc/self)/fd/[0-9]+\z!) ?
+ fail($self, "cannot open $path from daemon") : -1
+ );
}
# caller needs to "-t $self->{1}" to check if tty
my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
$lei->{l2m} or return
$lei->fail("output not specified or is not a mail destination");
- $lei->{opt}->{augment} = 1 unless $ovv->{dst} eq '/dev/stdout';
+ my $devfd = $lei->path_to_fd($ovv->{dst}) // return;
+ $lei->{opt}->{augment} = 1 if $devfd < 0;
$self->prepare_inputs($lei, \@inputs) or return;
my $op = $lei->workers_start($self, 'lei_convert', 1);
$lei->{cnv} = $self;
return;
}
$input =~ s!\A([a-z0-9]+):!!i and $ifmt = lc($1);
- if (-f $input) {
+ my $devfd = $lei->path_to_fd($input) // return;
+ if ($devfd >= 0) {
+ $self->input_fh($ifmt, $lei->{$devfd}, $input, @args);
+ } elsif (-f $input) {
my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
PublicInbox::MboxLock->defaults);
my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
--in-format=$in_fmt and `$ifmt:' conflict
}
- if (-f $input_path) {
+ my $devfd = $lei->path_to_fd($input_path) // return;
+ if ($devfd >= 0 || (-f $input_path || -p _)) {
require PublicInbox::MboxLock;
require PublicInbox::MboxReader;
PublicInbox::MboxReader->reads($ifmt) or return
$lei->fail("$ifmt not supported");
- } elsif (-d _) {
+ } elsif (-d $input_path) {
require PublicInbox::MdirReader;
$ifmt eq 'maildir' or return
$lei->fail("$ifmt not supported");
} else {
return $lei->fail("Unable to handle $input");
}
- } elsif (-f $input) { push @f, $input }
- elsif (-d _) { push @d, $input }
- else { return $lei->fail("Unable to handle $input") }
+ } else {
+ my $devfd = $lei->path_to_fd($input) // return;
+ if ($devfd >= 0 || -f $input || -p _) {
+ push @f, $input
+ } elsif (-d $input) {
+ push @d, $input
+ } else {
+ return $lei->fail("Unable to handle $input")
+ }
+ }
}
if (@f) { check_input_format($lei, \@f) or return }
if (@d) { # TODO: check for MH vs Maildir, here
--$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);
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 {
$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) {
my $in = $self->{0};
unless ($in) {
my $input = $self->{input};
- if (-e $input) {
- $in = $lei->fopen('<', $input) or
+ my $devfd = $lei->path_to_fd($input) // return;
+ if ($devfd >= 0) {
+ $in = $lei->{$devfd};
+ } elsif (-e $input) {
+ open($in, '<', $input) or
return $lei->fail("open < $input: $!");
} else {
my @cmd = (qw(git format-patch --stdout -1), $input);
sub _pre_augment_mbox {
my ($self, $lei) = @_;
my $dst = $lei->{ovv}->{dst};
- my $out = $lei->{1};
- if ($dst ne '/dev/stdout') {
+ my $out;
+ my $devfd = $lei->path_to_fd($dst) // die "bad $dst";
+ if ($devfd >= 0) {
+ $out = $lei->{$devfd};
+ } else { # normal-looking path
if (-p $dst) {
- $out = undef;
open $out, '>', $dst or die "open($dst): $!";
} elsif (-f _ || !-e _) {
require PublicInbox::MboxLock;
PublicInbox::MboxLock->defaults;
$self->{mbl} = PublicInbox::MboxLock->acq($dst, 1, $m);
$out = $self->{mbl}->{fh};
+ } else {
+ die "$dst is not a file or FIFO\n";
}
$lei->{old_1} = $lei->{1}; # keep for spawning MUA
}
# Perl does SEEK_END even with O_APPEND :<
$self->{seekable} = seek($out, 0, SEEK_SET);
- if (!$self->{seekable} && $! != ESPIPE && $dst ne '/dev/stdout') {
+ if (!$self->{seekable} && $! != ESPIPE && !defined($devfd)) {
die "seek($dst): $!\n";
}
if (!$self->{seekable}) {
my $exp = do { local $/; <$fh> };
is($out, $exp, 'stdin => stdout');
- lei_ok qw(convert -F eml -o mboxcl2:/dev/stdout t/plack-qp.eml);
+ lei_ok qw(convert -F eml -o mboxcl2:/dev/fd/1 t/plack-qp.eml);
open $fh, '<', \$lei_out or BAIL_OUT;
@bar = ();
PublicInbox::MboxReader->mboxcl2($fh, sub {
$eml->header_set('Message-ID', '<k@y>');
$in = 'From k@y Fri Oct 2 00:00:00 1993'."\n".$eml->as_string;
-lei_ok([qw(import -F mboxrd -)], undef, { %$lei_opt, 0 => \$in },
+lei_ok([qw(import -F mboxrd /dev/fd/0)], undef, { %$lei_opt, 0 => \$in },
\'import single file with --kw (default) from stdin');
lei(qw(q m:k@y));
$res = json_utf8->decode($lei_out);