release 1.5
[spider.git] / perl / DXCommandmode.pm
index 024ccb0e491ece4cac8962e9cdb0efbfd241f36c..8af394b8898d4852b6e9ca0656a3a1c5014e3313 100644 (file)
@@ -17,15 +17,19 @@ use DXUser;
 use DXVars;
 use DXDebug;
 use DXM;
+use DXLog;
+use CmdAlias;
 use FileHandle;
 use Carp;
 
 use strict;
-use vars qw(%Cache %cmd_cache $errstr);
+use vars qw(%Cache %cmd_cache $errstr %aliases);
 
 %Cache = ();                  # cache of dynamically loaded routine's mod times
 %cmd_cache = ();            # cache of short names
 $errstr = ();                # error string from eval
+%aliases = ();              # aliases for (parts of) commands
+
 #
 # obtain a new connection this is derived from dxchannel
 #
@@ -43,7 +47,7 @@ sub new
 
 sub start
 { 
-  my ($self, $line) = @_;
+  my ($self, $line, $sort) = @_;
   my $user = $self->{user};
   my $call = $self->{call};
   my $name = $user->{name};
@@ -71,12 +75,31 @@ sub start
   my $nchan = DXChannel->get($main::mycall);
   my @pc16 = DXProt::pc16($nchan, $cuser);
   DXProt::broadcast_ak1a(@pc16);
+  Log('DXCommand', "$call connected");
 }
 
 #
 # This is the normal command prompt driver
 #
+
 sub normal
+{
+       my $self = shift;
+       my $cmdline = shift;
+       
+       my @ans = run_cmd($self, $cmdline);
+       $self->send(@ans) if @ans > 0;
+       
+       # 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};
@@ -109,13 +132,19 @@ sub normal
     if ($cmd) {
     
          my ($path, $fcmd);
+         
+         # alias it if possible
+         my $acmd = CmdAlias::get_cmd($cmd);
+         if ($acmd) {
+           ($cmd, $args) = "$acmd $args" =~ /^([\w\/]+)\s*(.*)/o;
+         }
    
       # 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;
 
       my $package = find_cmd_name($path, $fcmd);
-         @ans = (0, "Syserr: compile err on $package\n$@$errstr") if !$package ;
+         @ans = (0) if !$package ;
 
       if ($package) {
            my $c = qq{ \@ans = $package(\$self, \$args) };
@@ -128,22 +157,17 @@ sub normal
        }
   }
        
-#    my @ans = $self->eval_file($path, $fcmd, $args) if $path && $fcmd;
-#      @ans = $self->eval_file($main::cmd, $cmd, $args) if !$ans[0];
   if ($ans[0]) {
     shift @ans;
-       $self->send(@ans) if @ans > 0;
   } else {
     shift @ans;
        if (@ans > 0) {
-         $self->send($self->msg('e2', @ans));
+               unshift @ans, $self->msg('e2');
        } else {
-      $self->send($self->msg('e1'));
+               @ans = $self->msg('e1');
        }
   }
-  
-  # send a prompt only if we are in a prompt state
-  $self->prompt() if $self->{state} =~ /^prompt/o;
+  return @ans;
 }
 
 #
@@ -185,7 +209,8 @@ sub finish
   my $nchan = DXChannel->get($main::mycall);
   my $pc17 = $nchan->pc17($self);
   DXProt::broadcast_ak1a($pc17);
-  
+
+  Log('DXCommand', "$call disconnected");
   $ref->del() if $ref;
 }
 
@@ -385,10 +410,10 @@ sub find_cmd_name {
        my $fh = new FileHandle;
        if (!open $fh, $filename) {
          $errstr = "Syserr: can't open '$filename' $!";
+         return undef;
        };
-       my $old = $fh->input_record_separator(undef);
+       local $/ = undef;
        my $sub = <$fh>;
-       $fh->input_record_separator($old);
        close $fh;
                
     #wrap the code into a subroutine inside our unique package
@@ -416,7 +441,6 @@ sub find_cmd_name {
          print "\$\@ = $@";
          $errstr = $@;
          delete_package($package);
-         $package = undef;
        } else {
       #cache it unless we're cleaning out each time
          $Cache{$package}{mtime} = $mtime;
@@ -425,6 +449,7 @@ sub find_cmd_name {
   
   #print Devel::Symdump->rnew($package)->as_string, $/;
   $package = "DXCommandmode::$package" if $package;
+  $package = undef if $errstr;
   return $package;
 }