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