commit d8c44621b6d47d787406d934542bec8aff75c960
Author: Przemysław Iskra <[email protected]>
Date:   Wed Feb 2 00:02:53 2011 +0000

    - a simple input/output wrapper to be used instead of tee with lots of magic

 teeboth | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
---
diff --git a/teeboth b/teeboth
new file mode 100755
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;
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/rpm-build-tools.git/commitdiff/12da9e3d7724a0ef69239325ae50ea859c3f3ae4

_______________________________________________
pld-cvs-commit mailing list
[email protected]
http://lists.pld-linux.org/mailman/listinfo/pld-cvs-commit

Reply via email to