releasing version 0.5
[moreutils.git] / vipe
blobcbcffd82b51e7bde2cb8d22887bdd99e18706fc2
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. Defaults to vi if not set.
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 (exists $ENV{EDITOR}) {
56 $editor=$ENV{EDITOR};
58 if (exists $ENV{VISUAL}) {
59 $editor=$ENV{VISUAL};
61 my $ret=system($editor, $tmp);
62 if ($ret != 0) {
63 die "$editor exited nonzero, aborting\n";
66 open (IN, $tmp) || die "$0: cannot read $tmp: $!\n";
67 print OUT <IN> || die "write failure: $!";
68 close IN;
69 unlink($tmp);