tvipe - scripts - random scripts
 (HTM) git clone git://parazyd.org/scripts.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       tvipe (1403B)
       ---
            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 ENVIRONMENT VARIABLES
           19 
           20 =over 4
           21 
           22 =item EDITOR
           23 
           24 Editor to use.
           25 
           26 =item VISUAL
           27 
           28 Also supported to determine what editor to use.
           29 
           30 =back
           31 
           32 =head1 AUTHOR
           33 
           34 Copyright 2006 by Joey Hess <id@joeyh.name>
           35 
           36 Licensed under the GNU GPL.
           37 
           38 =cut
           39 
           40 use warnings;
           41 use strict;
           42 use File::Temp q{tempfile};
           43 
           44 $/=undef;
           45 
           46 my ($fh, $tmp)=tempfile();
           47 die "cannot create tempfile" unless $fh;
           48 print ($fh <STDIN>) || die "write temp: $!";
           49 close $fh;
           50 close STDIN;
           51 open(STDIN, "</dev/tty") || die "reopen stdin: $!";
           52 open(OUT, ">&STDOUT") || die "save stdout: $!";
           53 close STDOUT;
           54 open(STDOUT, ">/dev/tty") || die "reopen stdout: $!";
           55 
           56 my @editor="vi";
           57 if (-x "/usr/bin/editor") {
           58         @editor="/usr/bin/editor";
           59 }
           60 if (exists $ENV{EDITOR}) {
           61         @editor=split(' ', $ENV{EDITOR});
           62 }
           63 if (exists $ENV{VISUAL}) {
           64         @editor=split(' ', $ENV{VISUAL});
           65 }
           66 my $ret=system(@editor, $tmp);
           67 if ($ret != 0) {
           68         die "@editor exited nonzero, aborting\n";
           69 }
           70 
           71 open (IN, $tmp) || die "$0: cannot read $tmp: $!\n";
           72 print (OUT <IN>) || die "write failure: $!";
           73 close IN;
           74 unlink($tmp);