blob: 695d9ff8a1414fa80dc04233b2df8891d7b86533 [file] [log] [blame]
apw3812c032006-12-07 21:01:14 +00001#!/usr/bin/perl
2#
3# conmux-attach -- attach commands to the console
4#
5# Attach stand alone commands to a console stream. You can specify
6# which of the commands file descriptors are connected to which of the
7# 'steams' in the multiplexor; the console input/output and the user
8# output.
9#
10# (C) Copyright IBM Corp. 2004, 2005, 2006
11# Author: Andy Whitcroft <andyw@uk.ibm.com>
12#
13# The Console Multiplexor is released under the GNU Public License V2
14#
mblighf5180482008-02-20 16:09:08 +000015use FindBin;
apw3812c032006-12-07 21:01:14 +000016use IO::Socket;
17use Getopt::Long qw(:config no_auto_abbrev);
18
19# Find our internal libraries.
mblighf5180482008-02-20 16:09:08 +000020use lib $FindBin::Bin;
apw3812c032006-12-07 21:01:14 +000021use Conmux;
22
23my $P = 'conmux-attach';
24my ($in, $out, $err);
25
26# Usage.
27GetOptions(
28 'i|stdout=s' => \$in,
29 'o|stdin=s' => \$out,
30 'e|stderr=s' => \$err,
31 'b|bot=s' => \$bot,
32) or exit;
33
34if ($in eq '' && $out eq '' && $err eq '') {
35 $in = 'client';
36 $out = 'client';
37}
38
39if ($#ARGV < 1) {
mblighff48fca2008-01-16 18:28:27 +000040 print STDERR "$P: <host:port> <program> ...\n";
apw3812c032006-12-07 21:01:14 +000041 exit 1;
42}
43my ($mux, $app) = @ARGV;
44shift;
45$app =~ s@.*/@@; $app =~ s@\s.*$@@;
46$app = $bot if ($bot);
47
48#
49# Connect to the client channel.
50#
51sub conmux_connect {
52 my ($mux, $type) = @_;
53
54 if (!$cache{$mux, $type}) {
55 my $con = Conmux::connect($mux);
56 my %r = Conmux::sendCmd($con, 'CONNECT', {
57 'id' => 'bot:' . $app,
58 'to' => 'console',
59 'type' => $type } );
60 die "$P: $mux: connect failed - $r{'status'}\n"
61 if ($r{'status'} ne "OK");
62
63 $cache{$mux, $type} = $con;
64 }
65
66 $cache{$mux, $type};
67}
68
69if ($in) {
70 my $to = conmux_connect($mux, $in);
71 open(STDIN, "<&", $to) ||
72 die "$P: unable to hand off socket to stdin - $!\n";
73}
74if ($out) {
75 my $to = conmux_connect($mux, $out);
76 open(STDOUT, ">&", $to) ||
77 die "$P: unable to hand off socket to stdout - $!\n";
78}
79if ($err) {
80 my $to = conmux_connect($mux, $err);
81 open(STDERR, ">&", $to) ||
82 die "$P: unable to hand off socket to stderr - $!\n";
83}
84exec @ARGV;