- my $self = shift;
- my $user = $self->{user};
- my $call = $self->{call};
- my $cmd = shift;
-
- # read in the list of valid commands, note that the commands themselves are cached elsewhere
- scan_cmd_dirs if (!defined %cmd);
-
- # strip out any nasty characters like $@%&|. and double // etc.
- $cmd =~ s/[\%\@\$\&\|\.\`\~]//og;
- $cmd =~ s|//|/|og;
-
- # split the command up into parts
- my @parts = split |[/\b]+|, $cmd;
-
- # first expand out the entry to a command, note that I will accept
- # anything in any case with any (reasonable) seperator
- $self->prompt();
+ my $self = shift;
+ my $cmdline = shift;
+ my @ans;
+
+ # remove leading and trailing spaces
+ $cmdline =~ s/^\s*(.*)\s*$/$1/;
+
+ if ($self->{state} eq 'page') {
+ my $i = $self->{pagelth};
+ my $ref = $self->{pagedata};
+ my $tot = @$ref;
+
+ # abort if we get a line starting in with a
+ if ($cmdline =~ /^a/io) {
+ undef $ref;
+ $i = 0;
+ }
+
+ # send a tranche of data
+ while ($i-- > 0 && @$ref) {
+ my $line = shift @$ref;
+ $line =~ s/\s+$//o; # why am having to do this?
+ $self->send($line);
+ }
+
+ # reset state if none or else chuck out an intermediate prompt
+ if ($ref && @$ref) {
+ $tot -= $self->{pagelth};
+ $self->send($self->msg('page', $tot));
+ } else {
+ $self->state('prompt');
+ }
+ } else {
+ @ans = run_cmd($self, $cmdline) if length $cmdline;
+
+ if ($self->{pagelth} && @ans > $self->{pagelth}) {
+ my $i;
+ for ($i = $self->{pagelth}; $i-- > 0; ) {
+ my $line = shift @ans;
+ $line =~ s/\s+$//o; # why am having to do this?
+ $self->send($line);
+ }
+ $self->{pagedata} = \@ans;
+ $self->state('page');
+ $self->send($self->msg('page', scalar @ans));
+ } else {
+ for (@ans) {
+ s/\s+$//o; # why ?????????
+ $self->send($_);
+ }
+ }
+ }
+
+ # send a prompt only if we are in a prompt state
+ $self->prompt() if $self->{state} =~ /^prompt/o;
+}
+
+#
+# this is the thing that runs the command, it is done like this for the
+# benefit of remote command execution
+#
+
+sub run_cmd
+{
+ my $self = shift;
+ my $user = $self->{user};
+ my $call = $self->{call};
+ my $cmdline = shift;
+ my @ans;
+
+ if ($self->{func}) {
+ my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
+ dbg('eval', "stored func cmd = $c\n");
+ eval $c;
+ if ($@) {
+ return (1, "Syserr: Eval err $errstr on stored func $self->{func}");
+ }
+ } else {
+
+ # strip out //
+ $cmdline =~ s|//|/|og;
+
+ # split the command line up into parts, the first part is the command
+ my ($cmd, $args) = $cmdline =~ /^([\S\/]+)\s*(.*)/o;
+
+ if ($cmd) {
+
+ my ($path, $fcmd);
+
+ dbg('command', "cmd: $cmd");
+
+ # alias it if possible
+ my $acmd = CmdAlias::get_cmd($cmd);
+ if ($acmd) {
+ ($cmd, $args) = "$acmd $args" =~ /^([\w\/]+)\s*(.*)/o;
+ dbg('command', "aliased cmd: $cmd $args");
+ }
+
+ # first expand out the entry to a command
+ ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
+ ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
+
+ dbg('command', "path: $cmd cmd: $fcmd");
+
+ my $package = find_cmd_name($path, $fcmd);
+ @ans = (0) if !$package ;
+
+ if ($package) {
+ dbg('command', "package: $package");
+
+ my $c = qq{ \@ans = $package(\$self, \$args) };
+ dbg('eval', "cluster cmd = $c\n");
+ eval $c;
+ if ($@) {
+ @ans = (0, "Syserr: Eval err cached $package\n$@");
+ }
+ }
+ }
+ }
+
+ if ($ans[0]) {
+ shift @ans;
+ } else {
+ shift @ans;
+ if (@ans > 0) {
+ unshift @ans, $self->msg('e2');
+ } else {
+ @ans = $self->msg('e1');
+ }
+ }
+ return (@ans);