update
[moreutils.git] / vipe
blobadc44a06055de36017d3395cf2756aa8fa170ad0
1 #!/usr/bin/perl
3 =head1 NAME
5 vipe - edit pipe
7 =head1 SYNOPSIS
9 command1 | vipe | command2
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. Your editor will
15 have the full data being piped from command1 loaded into it, and when you
16 close it, that data will be piped into command2.
18 =head1 ENVIRONMENT VARIABLES
20 =over 4
22 =item EDITOR
24 Editor to use.
26 =item VISUAL
28 Also supported to determine what editor to use.
30 =back
32 =head1 AUTHOR
34 Copyright 2006 by Joey Hess <joey@kitenet.net>
36 Licensed under the GNU GPL.
38 =cut
40 use warnings;
41 use strict;
42 use File::Temp q{tempfile};
44 $/=undef;
46 my ($fh, $tmp)=tempfile();
47 die "cannot create tempfile" unless $fh;
48 print ($fh <STDIN>) || die "write temp: $!";
49 close $fh;
50 close STDIN;
51 open(STDIN, "</dev/tty") || die "reopen stdin: $!";
52 open(OUT, ">&STDOUT") || die "save stdout: $!";
53 close STDOUT;
54 open(STDOUT, ">/dev/tty") || die "reopen stdout: $!";
56 my @editor="vi";
57 if (-x "/usr/bin/editor") {
58 @editor="/usr/bin/editor";
60 if (exists $ENV{EDITOR}) {
61 @editor=split(' ', $ENV{EDITOR});
63 if (exists $ENV{VISUAL}) {
64 @editor=split(' ', $ENV{VISUAL});
66 my $ret=system(@editor, $tmp);
67 if ($ret != 0) {
68 die "@editor exited nonzero, aborting\n";
71 open (IN, $tmp) || die "$0: cannot read $tmp: $!\n";
72 print (OUT <IN>) || die "write failure: $!";
73 close IN;
74 unlink($tmp);