| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
vipe - edit pipe |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
command1 | vipe | command2 |
| 10 |
|
| 11 |
=head1 DESCRIPTION |
| 12 |
|
| 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. |
| 17 |
|
| 18 |
=head1 ARGUMENTS |
| 19 |
|
| 20 |
vipe takes an argument --suffix that can be used to provide a file |
| 21 |
extension to the temp file generated. This enables editors to provide |
| 22 |
syntax highlighting and activate modes. For example, you can call vipe |
| 23 |
like |
| 24 |
|
| 25 |
vipe --suffix csv |
| 26 |
|
| 27 |
to create a tempfile with .csv extensions which makes Emacs (or your |
| 28 |
favorite editor) launch in CSV major mode. |
| 29 |
|
| 30 |
=head1 ENVIRONMENT VARIABLES |
| 31 |
|
| 32 |
=over 4 |
| 33 |
|
| 34 |
=item EDITOR |
| 35 |
|
| 36 |
Editor to use. |
| 37 |
|
| 38 |
=item VISUAL |
| 39 |
|
| 40 |
Also supported to determine what editor to use. |
| 41 |
|
| 42 |
=back |
| 43 |
|
| 44 |
=head1 AUTHOR |
| 45 |
|
| 46 |
Copyright 2006 by Joey Hess <[email protected]> |
| 47 |
|
| 48 |
Licensed under the GNU GPL. |
| 49 |
|
| 50 |
=cut |
| 51 |
|
| 52 |
use warnings; |
| 53 |
use strict; |
| 54 |
use File::Temp q{tempfile}; |
| 55 |
use Getopt::Long; |
| 56 |
|
| 57 |
$/=undef; |
| 58 |
|
| 59 |
my $suffix = ""; |
| 60 |
if (! GetOptions("suffix=s" => \$suffix)) { |
| 61 |
die "Usage: $0 [--suffix=extension]\n"; |
| 62 |
} |
| 63 |
$suffix = ".$suffix" if $suffix =~ m/^[^.]/; |
| 64 |
|
| 65 |
my ($fh, $tmp)=tempfile(UNLINK => 1, SUFFIX => $suffix); |
| 66 |
die "cannot create tempfile" unless $fh; |
| 67 |
if (! -t STDIN) { |
| 68 |
print ($fh <STDIN>) || die "write temp: $!"; |
| 69 |
} |
| 70 |
close $fh; |
| 71 |
close STDIN; |
| 72 |
open(STDIN, "</dev/tty") || die "reopen stdin: $!"; |
| 73 |
open(OUT, ">&STDOUT") || die "save stdout: $!"; |
| 74 |
close STDOUT; |
| 75 |
open(STDOUT, ">/dev/tty") || die "reopen stdout: $!"; |
| 76 |
|
| 77 |
my @editor="vi"; |
| 78 |
if (-x "/usr/bin/editor") { |
| 79 |
@editor="/usr/bin/editor"; |
| 80 |
} |
| 81 |
if (exists $ENV{EDITOR}) { |
| 82 |
@editor=split(' ', $ENV{EDITOR}); |
| 83 |
} |
| 84 |
if (exists $ENV{VISUAL}) { |
| 85 |
@editor=split(' ', $ENV{VISUAL}); |
| 86 |
} |
| 87 |
my $ret=system(@editor, $tmp); |
| 88 |
if ($ret != 0) { |
| 89 |
die "@editor exited nonzero, aborting\n"; |
| 90 |
} |
| 91 |
|
| 92 |
open (IN, $tmp) || die "$0: cannot read $tmp: $!\n"; |
| 93 |
print (OUT <IN>) || die "write failure: $!"; |
| 94 |
close IN; |