sorted out inheritance
[spider.git] / perl / DXCommandmode.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the user facing command mode for a dx cluster
4 #
5 # Copyright (c) 1998 Dirk Koopman G1TLH
6 #
7 # $Id$
8
9
10 package DXCommandmode;
11
12 @ISA = qw(DXChannel);
13
14 use DXUtil;
15 use DXChannel;
16 use DXUser;
17 use DXVars;
18
19 use strict;
20 use vars qw( %Cache $last_dir_mtime @cmd);
21
22 $last_dir_mtime = 0;          # the last time one of the cmd dirs was modified
23 @cmd = undef;                 # a list of commands+path pairs (in alphabetical order)
24
25 # this is how a a connection starts, you get a hello message and the motd with
26 # possibly some other messages asking you to set various things up if you are
27 # new (or nearly new and slacking) user.
28
29 sub start
30
31   my $self = shift;
32   my $user = $self->{user};
33   my $call = $self->{call};
34   my $name = $self->{name};
35   $name = $call if !defined $name;
36   $self->msg('l2',$name);
37   $self->send_file($main::motd) if (-e $main::motd);
38   $self->msg('pr', $call);
39   $self->state('prompt');                  # a bit of room for further expansion, passwords etc
40   $self->{priv} = 0;                  # set the connection priv to 0 - can be upgraded later
41 }
42
43 #
44 # This is the normal command prompt driver
45 #
46 sub normal
47 {
48   my $self = shift;
49   my $user = $self->{user};
50   my $call = $self->{call};
51   my $cmdline = shift; 
52
53   # strip out //
54   $cmdline =~ s|//|/|og;
55   
56   # split the command line up into parts, the first part is the command
57   my ($cmd, $args) = $cmdline =~ /^([\w\/]+)\s*(.*)/o;
58
59   if ($cmd) {
60
61     # first expand out the entry to a command
62     $cmd = search($cmd);
63
64     my @ans = $self->eval_file($main::localcmd, $cmd, $args);
65         @ans = $self->eval_file($main::cmd, $cmd, $args) if !$ans[0];
66         if ($ans[0]) {
67       shift @ans;
68           $self->send(@ans) if @ans > 0;
69         } else {
70       shift @ans;
71           if (@ans > 0) {
72             $self->msg('e2', @ans);
73           } else {
74         $self->msg('e1');
75           }
76         }
77   } else {
78     $self->msg('e1');
79   }
80   
81   # send a prompt only if we are in a prompt state
82   $self->prompt() if $self->{state} =~ /^prompt/o;
83 }
84
85 #
86 # This is called from inside the main cluster processing loop and is used
87 # for despatching commands that are doing some long processing job
88 #
89 sub process
90 {
91
92 }
93
94 #
95 # finish up a user context
96 #
97 sub finish
98 {
99
100 }
101
102 #
103 # short cut to output a prompt
104 #
105
106 sub prompt
107 {
108   my $self = shift;
109   my $call = $self->{call};
110   DXChannel::msg($self, 'pr', $call);
111 }
112
113 #
114 # search for the command in the cache of short->long form commands
115 #
116
117 sub search
118 {
119   my $short_cmd = shift;
120   return $short_cmd;    # just return it for now
121 }  
122
123 #
124 # the persistant execution of things from the command directories
125 #
126 #
127 # This allows perl programs to call functions dynamically
128
129 # This has been nicked directly from the perlembed pages
130 #
131
132 #require Devel::Symdump;  
133
134 sub valid_package_name {
135   my($string) = @_;
136   $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
137   
138   #second pass only for words starting with a digit
139   $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
140         
141   #Dress it up as a real package name
142   $string =~ s|/|_|g;
143   return "Emb_" . $string;
144 }
145
146 #borrowed from Safe.pm
147 sub delete_package {
148   my $pkg = shift;
149   my ($stem, $leaf);
150         
151   no strict 'refs';
152   $pkg = "DXChannel::$pkg\::";    # expand to full symbol table name
153   ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
154         
155   my $stem_symtab = *{$stem}{HASH};
156         
157   delete $stem_symtab->{$leaf};
158 }
159
160 sub eval_file {
161   my $self = shift;
162   my $path = shift;
163   my $cmdname = shift;
164   my $package = valid_package_name($cmdname);
165   my $filename = "$path/$cmdname";
166   my $mtime = -M $filename;
167   
168   # return if we can't find it
169   return (0, DXM::msg('e1')) if !defined $mtime;
170   
171   if(defined $Cache{$package}{mtime} && $Cache{$package}{mtime } <= $mtime) {
172     #we have compiled this subroutine already,
173         #it has not been updated on disk, nothing left to do
174         #print STDERR "already compiled $package->handler\n";
175         ;
176   } else {
177         local *FH;
178         if (!open FH, $filename) {
179           return (0, "Syserr: can't open '$filename' $!"); 
180         };
181         local($/) = undef;
182         my $sub = <FH>;
183         close FH;
184                 
185     #wrap the code into a subroutine inside our unique package
186         my $eval = qq{package DXChannel; sub $package { $sub; }};
187         print "eval $eval\n";
188         {
189           #hide our variables within this block
190           my($filename,$mtime,$package,$sub);
191           eval $eval;
192         }
193         if ($@) {
194           delete_package($package);
195           return (0, "Syserr: Eval err $@ on $package");
196         }
197                 
198         #cache it unless we're cleaning out each time
199         $Cache{$package}{mtime} = $mtime;
200   }
201   
202   my @r;
203   my $c = qq{ \@r = \$self->$package(\@_); };
204   print "c = $c\n";
205   eval  $c; ;
206   if ($@) {
207     delete_package($package);
208         return (0, "Syserr: Eval err $@ on cached $package");
209   }
210
211   #take a look if you want
212   #print Devel::Symdump->rnew($package)->as_string, $/;
213   return @r;
214 }
215
216 1;
217 __END__