1 |
#!/usr/bin/perl |
2 |
|
3 |
# useless stuff |
4 |
|
5 |
use Coro; |
6 |
use Coro::Event; |
7 |
|
8 |
my $stdin = Coro::Event->io(fd => \*STDIN, poll => 'r'); |
9 |
|
10 |
# this gets started everytime a user enters a finger command |
11 |
sub finger { |
12 |
my $user = shift; |
13 |
my $host = shift; |
14 |
use IO::Socket::INET; |
15 |
# is there ANY way to do non-blocking connects with IO::Socket::INET? |
16 |
# I don't see how... |
17 |
my $io = new IO::Socket::INET PeerAddr => "$host:finger"; |
18 |
$io or die; |
19 |
syswrite $io, "$user\n"; |
20 |
my $r = Coro::Event->io(fd => $io, poll => 'r'); |
21 |
my $buf; |
22 |
$r->next while 0 != sysread $io, $buf, 8192, length $buf; |
23 |
#do_timer(after => 5); |
24 |
print $buf; |
25 |
} |
26 |
|
27 |
# this reads one line from the keyboard |
28 |
sub kbdline { |
29 |
$stdin->next; |
30 |
my $x = <STDIN>; chomp $x; $x; |
31 |
} |
32 |
|
33 |
# this is the main task |
34 |
sub keyboard : Coro { |
35 |
$|=1; |
36 |
while() { |
37 |
print "cmd> "; my $cmd = kbdline; |
38 |
if ($cmd eq "finger") { |
39 |
print "user> "; my $user = kbdline; |
40 |
print "host> "; my $host = kbdline; |
41 |
async { finger(@_) } $user, $host; |
42 |
} elsif ($cmd eq "quit") { |
43 |
unloop(777); |
44 |
terminate; |
45 |
} else { |
46 |
print "enter command, either 'finger' or 'quit'\n"; |
47 |
} |
48 |
} |
49 |
} |
50 |
|
51 |
# display the time or garble the display, YMMV. |
52 |
sub timer : Coro { |
53 |
my $w = Coro::Event->timer(interval => 0.001, hard => 1); |
54 |
use Time::HiRes qw(time); |
55 |
while () { |
56 |
$w->next; |
57 |
print "\e7\e[C\e[C\e[C\e[C\e[C\e[C\e[C\e[C <time ",time,"> \e8"; |
58 |
}; |
59 |
} |
60 |
|
61 |
print "unlooped with value: ",loop(),"\n"; |
62 |
|