summaryrefslogtreecommitdiff
path: root/vest
diff options
context:
space:
mode:
authorgraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-16 02:38:18 +0000
committergraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-16 02:38:18 +0000
commit8925d36e0f451ff15bbf0e9e0db23623ad77ce85 (patch)
tree80cf646d22f127830b120fa0c8b4dbf20c11bc43 /vest
parent538bc2149631e989e4806165632c5460c3514670 (diff)
hooks up two processes, 2nd of which has one line of output per line of input, expected by the first, which starts off the communication
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@281 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'vest')
-rwxr-xr-xvest/line_mediator.pl35
1 files changed, 35 insertions, 0 deletions
diff --git a/vest/line_mediator.pl b/vest/line_mediator.pl
new file mode 100755
index 00000000..defc11e8
--- /dev/null
+++ b/vest/line_mediator.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl -w
+#hooks up two processes, 2nd of which has one line of output per line of input, expected by the first, which starts off the communication
+
+# if you don't know how to fork/exec in a C program, this could be helpful under limited cirmustances (would be ok to liaise with sentserver)
+
+use strict;
+use IPC::Open2;
+$,=' ';
+my $quiet=$ENV{QUIET};
+sub info {
+ print STDERR @_ unless $quiet;
+}
+
+my @c1;
+do {
+ push @c1,shift
+} while $c1[$#c1] ne '--';
+pop @c1;
+my @c2=@ARGV;
+(scalar @c1 && scalar @c2) || die "usage: $0 cmd1 args -- cmd2 args; hooks up two processes, 2nd of which has one line of output per line of input, expected by the first, which starts off the communication. crosses stdin/stderr of cmd1 and cmd2 line by line (both must flush on newline and output. cmd1 initiates the conversation (sends the first line). QUIET env var suppresses debugging output.";
+info("1 cmd:",@c1,"\n");
+info("2 cmd:",@c2,"\n");
+my ($R1,$W1,$R2,$W2);
+my $c1p=open2($R1,$W1,@c1); # Open2 R W backward from Open3.
+my $c2p=open2($R2,$W2,@c2);
+select $W2;
+$|=1;
+while(<$R1>) {
+ info("1:",$_);
+ print $_;
+ $_=<$R2>;
+ last unless defined $_;
+ info("2:",$_);
+ print $W1 $_;
+}