diff options
Diffstat (limited to 'vipe')
-rwxr-xr-x | vipe | 69 |
1 files changed, 69 insertions, 0 deletions
@@ -0,0 +1,69 @@ +#!/usr/bin/perl + +=head1 NAME + +vipe - edit pipe + +=head1 SYNOPSIS + +command | vipe | command + +=head1 DESCRIPTION + +vipe allows you to run your editor in the middle of a unix pipeline and +edit the data that is being piped between programs. + +=head1 ENVIRONMENT VARIABLES + +=over 4 + +=item EDITOR + +Editor to use. Defaults to vi if not set. + +=item VISUAL + +Also supported to determine what editor to use. + +=back + +=head1 AUTHOR + +Copyright 2006 by Joey Hess <joey@kitenet.net> + +Licensed under the GNU GPL. + +=cut + +use warnings; +use strict; +use File::Temp q{tempfile}; + +$/=undef; + +my ($fh, $tmp)=tempfile(); +die "cannot create tempfile" unless $fh; +print $fh <STDIN> || die "write temp: $!"; +close $fh; +close STDIN; +open(STDIN, "</dev/tty") || die "reopen stdin: $!"; +open(OUT, ">&STDOUT") || die "save stdout: $!"; +close STDOUT; +open(STDOUT, ">/dev/tty") || die "reopen stdout: $!"; + +my $editor="vi"; +if (exists $ENV{EDITOR}) { + $editor=$ENV{EDITOR}; +} +if (exists $ENV{VISUAL}) { + $editor=$ENV{VISUAL}; +} +my $ret=system($editor, $tmp); +if ($ret != 0) { + die "$editor exited nonzero, aborting\n"; +} + +open (IN, $tmp) || die "$0: cannot read $tmp: $!\n"; +print OUT <IN> || die "write failure: $!"; +close IN; +unlink($tmp); |