blob: 38ead0e8c3156b1ad7cfdc2d91c7d6ec7720821a [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#
15use IO::Socket;
16use Getopt::Long qw(:config no_auto_abbrev);
17
18# Find our internal libraries.
19###my $lib = $0; $lib =~ s@/[^/]+$@@;
20###push(@INC, $lib);
21###require Conmux;
22##use lib ".";
23##use Conmux;
24use lib "/usr/local/conmux/lib";
25use Conmux;
26
27my $P = 'conmux-attach';
28my ($in, $out, $err);
29
30# Usage.
31GetOptions(
32 'i|stdout=s' => \$in,
33 'o|stdin=s' => \$out,
34 'e|stderr=s' => \$err,
35 'b|bot=s' => \$bot,
36) or exit;
37
38if ($in eq '' && $out eq '' && $err eq '') {
39 $in = 'client';
40 $out = 'client';
41}
42
43if ($#ARGV < 1) {
44 print STDERR "$P: <host:port> <program> ...";
45 exit 1;
46}
47my ($mux, $app) = @ARGV;
48shift;
49$app =~ s@.*/@@; $app =~ s@\s.*$@@;
50$app = $bot if ($bot);
51
52#
53# Connect to the client channel.
54#
55sub conmux_connect {
56 my ($mux, $type) = @_;
57
58 if (!$cache{$mux, $type}) {
59 my $con = Conmux::connect($mux);
60 my %r = Conmux::sendCmd($con, 'CONNECT', {
61 'id' => 'bot:' . $app,
62 'to' => 'console',
63 'type' => $type } );
64 die "$P: $mux: connect failed - $r{'status'}\n"
65 if ($r{'status'} ne "OK");
66
67 $cache{$mux, $type} = $con;
68 }
69
70 $cache{$mux, $type};
71}
72
73if ($in) {
74 my $to = conmux_connect($mux, $in);
75 open(STDIN, "<&", $to) ||
76 die "$P: unable to hand off socket to stdin - $!\n";
77}
78if ($out) {
79 my $to = conmux_connect($mux, $out);
80 open(STDOUT, ">&", $to) ||
81 die "$P: unable to hand off socket to stdout - $!\n";
82}
83if ($err) {
84 my $to = conmux_connect($mux, $err);
85 open(STDERR, ">&", $to) ||
86 die "$P: unable to hand off socket to stderr - $!\n";
87}
88exec @ARGV;