1 |
#! perl |
2 |
|
3 |
use Coro::Debug; |
4 |
use IO::Socket; |
5 |
|
6 |
my %global; # for use by eval'ed commands |
7 |
|
8 |
sub tcp_serve($) { |
9 |
my ($fh) = @_; |
10 |
|
11 |
binmode $fh, ":raw:perlio:utf8"; |
12 |
|
13 |
my $buf; |
14 |
my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z); |
15 |
my (%l, @l); # for use by shell users |
16 |
|
17 |
print $fh <<EOF; |
18 |
Welcome! |
19 |
Remember that everything entered here will be in the main coro context within cf::! |
20 |
You can freely use \$a .. \$z and \@l and \%l |
21 |
|
22 |
Useful commands (note the '&'): |
23 |
|
24 |
reload_perl & |
25 |
reload_config & |
26 |
reload_regions & |
27 |
reload_facedata & |
28 |
reload_treasures & |
29 |
reload_archetypes & |
30 |
ext::help::reload & |
31 |
ext::books::reload & |
32 |
ext::map_tags::reload & |
33 |
ext::map_world::reload & |
34 |
print JSON::XS->new->pretty->encode({cf::mallinfo}) |
35 |
EOF |
36 |
print $fh "\n> "; |
37 |
|
38 |
Event->io (fd => $fh, poll => 'r', data => 0, cb => sub { |
39 |
if (defined (my $cmd = <$fh>)) { |
40 |
$cmd =~ s/\s+$//; |
41 |
|
42 |
if ($cmd =~ /^\s*exit\b/i) { |
43 |
print $fh "will not exit() server.\n"; |
44 |
} elsif ($cmd =~ s/^coro\s+// or $cmd =~ /^(?:ps|bt\s)/) { |
45 |
select $fh; |
46 |
Coro::Debug::command $cmd; |
47 |
select STDOUT; |
48 |
} else { |
49 |
my $sub = sub { |
50 |
package cf; |
51 |
select $fh; |
52 |
|
53 |
# compile first, the execute, as Coro does not support switching in eval string |
54 |
my $cb = eval "sub { $cmd }"; |
55 |
|
56 |
my $t1 = Time::HiRes::time; |
57 |
my @res = $@ ? () : eval { $cb->() }; |
58 |
my $t2 = Time::HiRes::time; |
59 |
|
60 |
print "\n", |
61 |
"command: '$cmd'\n", |
62 |
"execution time: ", $t2 - $t1, "\n"; |
63 |
warn "evaluation error: $@" if $@; |
64 |
print "evaluation error: $@\n" if $@; |
65 |
print "result:\n", cf::dumpval @res > 1 ? \@res : $res[0] if @res; |
66 |
print "\n> "; |
67 |
|
68 |
select STDOUT; |
69 |
}; |
70 |
|
71 |
if ($cmd =~ s/\s*&$//) { |
72 |
cf::async { |
73 |
$Coro::current->desc ($cmd); |
74 |
$sub->() |
75 |
}; |
76 |
} else { |
77 |
$sub->(); |
78 |
} |
79 |
} |
80 |
|
81 |
print $fh "\n> "; |
82 |
} else { |
83 |
$_[0]->w->cancel; |
84 |
} |
85 |
}); |
86 |
} |
87 |
|
88 |
# now a shell listening on a tcp-port - let the firewall decide access rights |
89 |
if ($cf::CFG{perl_shell}) { |
90 |
if (my $listen = new IO::Socket::INET LocalAddr => $cf::CFG{perl_shell}, Listen => 1, ReuseAddr => 1) { |
91 |
Event->io (fd => $listen, poll => 'r', data => cf::WF_AUTOCANCEL, cb => sub { tcp_serve $listen->accept }); |
92 |
} |
93 |
} |
94 |
|
95 |
|