--- Coro/myhttpd/shell.pl 2001/08/12 01:16:48 1.9 +++ Coro/myhttpd/shell.pl 2001/08/14 02:28:51 1.10 @@ -6,42 +6,56 @@ use Event; use Time::HiRes 'time'; +use Text::Abbrev; + my $last_ts = time; +my %complete; +my @commands = qw(quit squit refresh country restart block info); + +abbrev \%complete, @commands; + sub shell { my $fh = shift; while (defined (print $fh "cmd> "), $_ = <$fh>) { s/\015?\012$//; - chomp; - if (/^q/) { - Event::unloop; - } elsif (/^i/) { - $::NOW = time+1e-6; - my @data; - for (values %conn::conn) { - for (values %$_) { - next unless $_; - my $rate = sprintf "%.1f", $_->{written} / ($::NOW - $_->{time}); - push @data, "$_->{country}/$_->{remote_addr} $_->{written} $rate $_->{method} $_->{uri}\n"; + if (s/^(\S+)\s*// && (my $cmd = $complete{$1})) { + if ($cmd eq "quit") { + print "bye bye.\n";#d# + last; + } elsif ($cmd eq "squit") { + Event::unloop; + } elsif ($cmd eq "block") { + print "blocked '$_'\n";#d# + $conn::blocked{$_} = time + $::BLOCKTIME; + } elsif ($cmd eq "info") { + $::NOW = time+1e-6; + my @data; + for (values %conn::conn) { + for (values %$_) { + next unless $_; + my $rate = sprintf "%.1f", $_->{written} / ($::NOW - $_->{time}); + push @data, "$_->{country}/$_->{remote_addr} $_->{written} $rate $_->{method} $_->{uri}\n"; + } } + print $fh sort @data; + print $fh scalar@data, " ($::conns) connections\n";#d# + print $fh "$::written bytes written in the last ",$::NOW - $last_ts, " seconds\n"; + printf $fh "(%.1f bytes/s)\n", $::written / ($::NOW - $last_ts); + ($last_ts, $::written) = ($::NOW, 0); + } elsif ($cmd eq "refresh") { + read_blocklist; + } elsif ($cmd eq "restart") { + $::RESTART = 1; + unloop; + print $fh "restarting, cu!\n"; + last; + } elsif ($cmd eq "country") { + print $fh ip_request($_), "\n"; } - print $fh sort @data; - print $fh scalar@data, " ($::conns) connections\n";#d# - print $fh "$::written bytes written in the last ",$::NOW - $last_ts, " seconds\n"; - printf $fh "(%.1f bytes/s)\n", $::written / ($::NOW - $last_ts); - ($last_ts, $::written) = ($::NOW, 0); - } elsif (/^ref/) { - read_blocklist; - } elsif (/^r/) { - $::RESTART = 1; - unloop; - print $fh "bye bye.\n"; - last; - } elsif (/^co\S*\s+(\S+)/) { - print $fh ip_request($1), "\n"; } else { - print $fh "try quit, info, restart, refresh\n"; + print $fh "try one of @commands\n"; } } }