… | |
… | |
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 | |
10 | |
|
|
11 | select $fh; |
|
|
12 | |
11 | binmode $fh, ":raw:perlio:utf8"; |
13 | binmode $fh, ":raw:perlio:utf8"; |
12 | |
14 | |
13 | my $buf; |
15 | 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,%l,@l); # for use by shell users |
17 | my (%l, @l); # for use by shell users |
15 | |
18 | |
16 | print $fh "Welcome\n> "; |
19 | print <<EOF; |
|
|
20 | Welcome! |
|
|
21 | 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 |
17 | |
23 | |
|
|
24 | Useful commands (note the '&'): |
|
|
25 | |
|
|
26 | reload_perl & |
|
|
27 | reload_regions & |
|
|
28 | reload_facedata & |
|
|
29 | reload_treasures & |
|
|
30 | reload_archetypes & |
|
|
31 | ext::map_world::reload & |
|
|
32 | EOF |
|
|
33 | print "\n> "; |
|
|
34 | |
18 | Event->io (fd => $fh, poll => 'r', data => cf::WF_AUTOCANCEL, cb => sub { |
35 | Event->io (fd => $fh, poll => 'r', data => 0, cb => sub { |
19 | if (defined (my $cmd = <$fh>)) { |
36 | if (defined (my $cmd = <$fh>)) { |
20 | my $old_fh = select $fh; |
37 | $cmd =~ s/\s+$//; |
21 | |
38 | |
22 | if ($cmd =~ /^\s*exit\b/i) { |
39 | if ($cmd =~ /^\s*exit\b/i) { |
23 | print "will not exit() server.\n"; |
40 | print "will not exit() server.\n"; |
24 | } else { |
41 | } else { |
25 | { |
42 | my $sub = sub { |
26 | package cf; |
43 | package cf; |
27 | eval $cmd; |
44 | select $fh; |
|
|
45 | |
|
|
46 | # compile first, the execute, as Coro does not support switching in eval string |
|
|
47 | my $cb = eval "sub { $cmd }"; |
|
|
48 | |
|
|
49 | my $t1 = Time::HiRes::time; |
|
|
50 | my @res = $@ ? () : eval { $cb->() }; |
|
|
51 | my $t2 = Time::HiRes::time; |
|
|
52 | |
|
|
53 | print "\n", |
|
|
54 | "command: '$cmd'\n", |
|
|
55 | "execution time: ", $t2 - $t1, "\n"; |
|
|
56 | warn "evaluation error: $@" if $@; |
|
|
57 | print "evaluation error: $@\n" if $@; |
|
|
58 | print "result:\n", cf::dumpval @res > 1 ? \@res : $res[0] if @res; |
|
|
59 | print "\n> "; |
|
|
60 | }; |
|
|
61 | |
|
|
62 | if ($cmd =~ s/\s*&$//) { |
|
|
63 | Coro::async_pool { $sub->() }; |
|
|
64 | } else { |
|
|
65 | $sub->(); |
28 | } |
66 | } |
29 | print $@ if $@; |
|
|
30 | } |
67 | } |
31 | |
68 | |
32 | print "> "; |
69 | print "\n> "; |
33 | select $old_fh; |
|
|
34 | } else { |
70 | } else { |
35 | $_[0]->w->cancel; |
71 | $_[0]->w->cancel; |
36 | } |
72 | } |
37 | }); |
73 | }); |
38 | } |
74 | } |