releasing version 0.17
[moreutils.git] / vipe
blob8cee2b1775c19a3e7e046984a325eebfc8b94ecf
1 #!/usr/bin/perl
3 =head1 NAME
5 vipe - edit pipe
7 =head1 SYNOPSIS
9 command | vipe | command
11 =head1 DESCRIPTION
13 vipe allows you to run your editor in the middle of a unix pipeline and
14 edit the data that is being piped between programs.
16 =head1 ENVIRONMENT VARIABLES
18 =over 4
20 =item EDITOR
22 Editor to use.
24 =item VISUAL
26 Also supported to determine what editor to use.
28 =back
30 =head1 AUTHOR
32 Copyright 2006 by Joey Hess <joey@kitenet.net>
34 Licensed under the GNU GPL.
36 =cut
38 use warnings;
39 use strict;
40 use File::Temp q{tempfile};
42 $/=undef;
44 my ($fh, $tmp)=tempfile();
45 die "cannot create tempfile" unless $fh;
46 print $fh <STDIN> || die "write temp: $!";
47 close $fh;
48 close STDIN;
49 open(STDIN, "</dev/tty") || die "reopen stdin: $!";
50 open(OUT, ">&STDOUT") || die "save stdout: $!";
51 close STDOUT;
52 open(STDOUT, ">/dev/tty") || die "reopen stdout: $!";
54 my @editor="vi";
55 if (-x "/usr/bin/editor") {
56 @editor="/usr/bin/editor";
58 if (exists $ENV{EDITOR}) {
59 @editor=split(' ', $ENV{EDITOR});
61 if (exists $ENV{VISUAL}) {
62 @editor=split(' ', $ENV{VISUAL});
64 my $ret=system(@editor, $tmp);
65 if ($ret != 0) {
66 die "@editor exited nonzero, aborting\n";
69 open (IN, $tmp) || die "$0: cannot read $tmp: $!\n";
70 print OUT <IN> || die "write failure: $!";
71 close IN;
72 unlink($tmp);