Makefile: Warn users of pkgx.dev about their poor life choices
[moreutils.git] / vipe
blob9daaec5eb77d9904368c95723e13748b38f057cd
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 ARGUMENTS
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
25 vipe --suffix csv
27 to create a tempfile with .csv extensions which makes Emacs (or your
28 favorite editor) launch in CSV major mode.
30 =head1 ENVIRONMENT VARIABLES
32 =over 4
34 =item EDITOR
36 Editor to use.
38 =item VISUAL
40 Also supported to determine what editor to use.
42 =back
44 =head1 AUTHOR
46 Copyright 2006 by Joey Hess <id@joeyh.name>
48 Licensed under the GNU GPL.
50 =cut
52 use warnings;
53 use strict;
54 use File::Temp q{tempfile};
55 use Getopt::Long;
57 $/=undef;
59 my $suffix = "";
60 if (! GetOptions("suffix=s" => \$suffix)) {
61 die "Usage: $0 [--suffix=extension]\n";
63 $suffix = ".$suffix" if $suffix =~ m/^[^.]/;
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: $!";
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: $!";
77 my @editor="vi";
78 if (-x "/usr/bin/editor") {
79 @editor="/usr/bin/editor";
81 if (exists $ENV{EDITOR}) {
82 @editor=split(' ', $ENV{EDITOR});
84 if (exists $ENV{VISUAL}) {
85 @editor=split(' ', $ENV{VISUAL});
87 my $ret=system(@editor, $tmp);
88 if ($ret != 0) {
89 die "@editor exited nonzero, aborting\n";
92 open (IN, $tmp) || die "$0: cannot read $tmp: $!\n";
93 print (OUT <IN>) || die "write failure: $!";
94 close IN;