package chat;
require 'sys/socket.ph';
if( defined( &main'PF_INET ) ){
$pf_inet = &main'PF_INET;
$sock_stream = &main'SOCK_STREAM;
local($name, $aliases, $proto) = getprotobyname( 'tcp' );
$tcp_proto = $proto;
}
else {
# XXX hardwired $PF_INET, $SOCK_STREAM, 'tcp'
# but who the heck would change these anyway? (:-)
$pf_inet = 2;
$sock_stream = 1;
$tcp_proto = 6;
}
$sockaddr = 'S n a4 x8';
chop($thishost = `hostname`);
# *S = symbol for current I/O, gets assigned *chatsymbol....
$next = "chatsymbol000000"; # next one
$nextpat = "^chatsymbol"; # patterns that match next++, ++, ++, ++
## $handle = &chat'open_port("server.address",$port_number);
sub open_port { local($server, $port) = @_;
local($serveraddr,$serverproc);
$thisaddr = "\0\0\0\0" ;
$thisproc = pack($sockaddr, 2, 0, $thisaddr);
*S = ++$next;
if ($server =~ /^(\d+)+\.(\d+)\.(\d+)\.(\d+)$/) {
$serveraddr = pack('C4', $1, $2, $3, $4);
} else {
local(@x) = gethostbyname($server);
return undef unless @x;
$serveraddr = $x[4];
}
$serverproc = pack($sockaddr, 2, $port, $serveraddr);
unless (socket(S, $pf_inet, $sock_stream, $tcp_proto)) {
($!) = ($!, close(S)); return undef;
}
unless (bind(S, $thisproc)) {
($!) = ($!, close(S)); return undef;
}
unless (connect(S, $serverproc)) {
($!) = ($!, close(S)); return undef;
}
local($fam,$lport);
($fam,$lport,$thisaddr) = unpack($sockaddr, getsockname(S));
$thisproc = pack($sockaddr, 2, 0, $thisaddr);
select((select(S), $| = 1)[0]);
$next; }
sub open_listen {
*S = ++$next;
local($thisport) = shift || 0;
local($thisproc_local) = pack($sockaddr, 2, $thisport, $thisaddr);
local(*NS) = "__" . time;
unless (socket(NS, $pf_inet, $sock_stream, $tcp_proto)) {
($!) = ($!, close(NS));
return undef;
}
unless (bind(NS, $thisproc_local)) {
($!) = ($!, close(NS));
return undef;
}
unless (listen(NS, 1)) {
($!) = ($!, close(NS));
return undef;
}
select((select(NS), $| = 1)[0]);
local($family, $port, @myaddr) =
unpack("S n C C C C x8", getsockname(NS));
$S{"needs_accept"} = *NS; (@myaddr, $port, $next); }
sub open_proc { local(@cmd) = @_;
*S = ++$next;
local(*TTY) = "__TTY" . time;
local($pty,$tty) = &_getpty(S,TTY);
die "Cannot find a new pty" unless defined $pty;
$pid = fork;
die "Cannot fork: $!" unless defined $pid;
unless ($pid) {
close STDIN; close STDOUT; close STDERR;
setpgrp(0,$$);
if (open(DEVTTY, "/dev/tty")) {
ioctl(DEVTTY,0x20007471,0); close DEVTTY;
}
open(STDIN,"<&TTY");
open(STDOUT,">&TTY");
open(STDERR,">&STDOUT");
die "Oops" unless fileno(STDERR) == 2; close(S);
exec @cmd;
die "Cannot exec @cmd: $!";
}
close(TTY);
$next; }
$nextsubname = "expectloop000000";
sub expect { if ($_[0] =~ /$nextpat/) {
*S = shift;
}
local($endtime) = shift;
local($timeout,$eof) = (1,1);
local($caller) = caller;
local($rmask, $nfound, $timeleft, $thisbuf);
local($cases, $pattern, $action, $subname);
$endtime += time if $endtime < 600_000_000;
if (defined $S{"needs_accept"}) { local(*NS) = $S{"needs_accept"};
delete $S{"needs_accept"};
$S{"needs_close"} = *NS;
unless(accept(S,NS)) {
($!) = ($!, close(S), close(NS));
return undef;
}
select((select(S), $| = 1)[0]);
}
unless ($subname = $expect_subname{$caller,@_}) {
$expect_subname{$caller,@_} = $subname = $nextsubname++;
$cases .= <<"EDQ"; sub $subname {
LOOP: {
if (0) { ; }
EDQ
while (@_) {
($pattern,$action) = splice(@_,0,2);
if ($pattern =~ /^eof$/i) {
$cases .= <<"EDQ";
elsif (\$eof) {
package $caller;
$action;
}
EDQ
$eof = 0;
} elsif ($pattern =~ /^timeout$/i) {
$cases .= <<"EDQ";
elsif (\$timeout) {
package $caller;
$action;
}
EDQ
$timeout = 0;
} else {
$pattern =~ s $cases .= <<"EDQ";
elsif (\$S =~ /$pattern/) {
\$S = \$';
package $caller;
$action;
}
EDQ
}
}
$cases .= <<"EDQ" if $eof;
elsif (\$eof) {
undef;
}
EDQ
$cases .= <<"EDQ" if $timeout;
elsif (\$timeout) {
undef;
}
EDQ
$cases .= <<'ESQ';
else {
$rmask = "";
vec($rmask,fileno(S),1) = 1;
($nfound, $rmask) =
select($rmask, undef, undef, $endtime - time);
if ($nfound) {
$nread = sysread(S, $thisbuf, 1024);
if ($nread > 0) {
$S .= $thisbuf;
} else {
$eof++, redo LOOP; # any error is also eof
}
} else {
$timeout++, redo LOOP; # timeout
}
redo LOOP;
}
}
}
ESQ
eval $cases; die "$cases:\n$@" if $@;
}
$eof = $timeout = 0;
do $subname();
}
## &chat'print([$handle,] @data)
sub print { if ($_[0] =~ /$nextpat/) {
*S = shift;
}
local $out = join $, , @_;
syswrite(S, $out, length $out);
if( $chat'debug ){
print STDERR "printed:";
print STDERR @_;
}
}
## &chat'close([$handle,])
sub close { if ($_[0] =~ /$nextpat/) {
*S = shift;
}
close(S);
if (defined $S{"needs_close"}) { local(*NS) = $S{"needs_close"};
delete $S{"needs_close"};
close(NS);
}
}
sub select { local($timeout) = shift;
local(@handles) = @_;
local(%handlename) = ();
local(%ready) = ();
local($caller) = caller;
local($rmask) = "";
for (@handles) {
if (/$nextpat/o) { local(*SYM) = $_;
if (length($SYM)) {
$timeout = 0; $ready{$_}++;
}
$handlename{fileno($_)} = $_;
} else {
$handlename{fileno(/'/ ? $_ : "$caller\'$_")} = $_;
}
}
for (sort keys %handlename) {
vec($rmask, $_, 1) = 1;
}
select($rmask, undef, undef, $timeout);
for (sort keys %handlename) {
$ready{$handlename{$_}}++ if vec($rmask,$_,1);
}
sort keys %ready;
}
# ($pty,$tty) = $chat'_getpty(PTY,TTY):
sub _getpty { local($_PTY,$_TTY) = @_;
$_PTY =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
$_TTY =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
local($pty, $tty, $kind);
if( -e "/dev/pts000" ){ ## mods by Joe Doupnik Dec 1992
$kind = "pts"; ## SVR4 Streams
} else {
$kind = "pty"; ## BSD Clist stuff
}
for $bank (112..127) {
next unless -e sprintf("/dev/$kind%c0", $bank);
for $unit (48..57) {
$pty = sprintf("/dev/$kind%c%c", $bank, $unit);
open($_PTY,"+>$pty") || next;
select((select($_PTY), $| = 1)[0]);
($tty = $pty) =~ s/pty/tty/;
open($_TTY,"+>$tty") || next;
select((select($_TTY), $| = 1)[0]);
system "stty nl>$tty";
return ($pty,$tty);
}
}
undef;
}
1;