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