]> git.pld-linux.org Git - packages/rpm-build-tools.git/commitdiff
- a simple input/output wrapper to be used instead of tee with lots of magic
authorPrzemysław Iskra <sparky@pld-linux.org>
Wed, 2 Feb 2011 00:02:53 +0000 (00:02 +0000)
committerElan Ruusamäe <glen@delfi.ee>
Sat, 12 Jan 2013 10:18:05 +0000 (12:18 +0200)
teeboth [new file with mode: 0755]

diff --git a/teeboth b/teeboth
new file mode 100755 (executable)
index 0000000..b30e92c
--- /dev/null
+++ b/teeboth
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Fcntl;
+use POSIX ":sys_wait_h";
+use IPC::Open3;
+use IO::Handle;
+use IO::Select;
+
+my $out = shift @ARGV;
+die unless @ARGV;
+
+open my $fout, ">", $out or die;
+
+my $select = IO::Select->new();
+my $alive = 1;
+my $pid;
+
+my $code;
+sub sigchld
+{
+       my $kid;
+       do {
+               $kid = waitpid( -1, WNOHANG );
+               if ( $kid == $pid ) {
+                       $code = $? >> 8;
+                       $alive = 0
+               }
+       } while ( $kid > 0 );
+}
+$SIG{CHLD} = \&sigchld;
+
+$pid = open3( \*child_in, \*child_out, \*child_err, @ARGV );
+close child_in;
+
+sub sethandle
+{
+       my $h = shift;
+       my $flags = 0;
+
+       fcntl ( $h, F_GETFL, $flags )
+               or die "Couldn't get flags for HANDLE : $!\n";
+       $flags |= O_NONBLOCK;
+       fcntl ( $h, F_SETFL, $flags )
+               or die "Couldn't set flags for HANDLE: $!\n";
+       
+       $select->add( $h );
+}
+
+sethandle( \*child_out );
+sethandle( \*child_err );
+
+while ( $alive ) {
+       foreach my $h ( $select->can_read() ) {
+               sysread $h, $_, 1024;
+               print $fout $_;
+               if ( $h == \*child_err ) {
+                       print "\033[31m$_\033[0m";
+               } else {
+                       print $_;
+               }
+               STDOUT->flush();
+       }
+}
+
+exit $code;
This page took 0.066862 seconds and 4 git commands to generate.