#!/usr/bin/perl -w
#
# tap.pl, a polyp client, for testing purposes mostly
# version 2.0.0 by Pegasus Epsilon <pegasus@pimpninjas.org>
#
# (C) 2014, Distribute Unmodified - http://pegasus.pimpninjas.org/license
use strict;
use POSIX;
use Socket;
use Fcntl;
unless ($ARGV[0]) {
print "Usage: $0 name\n";
exit;
}
$0 = join ' ', $0, @ARGV;
-e "$ARGV[0].pid" || do {
print "[tap] $ARGV[0] not running\n";
exit;
};
open(*PID, "<", "$ARGV[0].pid");
my $pid = <PID>;
close PID;
# pass ctrl+c through to wrapped app
$SIG{INT} = sub { print "\r"; kill SIGINT, $pid; };
socket TAPPED, PF_UNIX, SOCK_STREAM, 0
or do { print "[tap] can't create socket\n"; exit };
connect TAPPED, sockaddr_un("$ARGV[0].polyp")
or do { print "[tap] can't connect to $ARGV[0]\n"; exit };
my $inputs = ''; my ($fd_tapped, $fd_stdin) = map {
fcntl $_, F_SETFL, O_NONBLOCK;
vec($inputs, $_ = fileno $_, 1) = 1;
$_
} (\*TAPPED, \*STDIN);
select TAPPED; $| = 1; select STDOUT; $| = 1;
while (my $n = select(my $r = $inputs, undef, my $e = $inputs, undef)) {
if (-1 == $n && $!{EINTR}) { undef $!; next; }
if (vec $e, $fd_tapped, 1) { print "socket error\n"; exit }
if (vec $r, $fd_tapped, 1) {
defined(local $_ = <TAPPED>) or do { print "socket closed\n"; exit };
while () { print $_; }
continue { defined($_ = <TAPPED>) or last };
}
if (vec $r, $fd_stdin, 1) {
defined(local $_ = <STDIN>) or do { print "EOF stdin\n"; exit };
while () { print TAPPED $_ }
continue { defined($_ = <STDIN>) or last };
}
}