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