3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
23 use vars qw(%Cache %cmd_cache);
25 %Cache = (); # cache of dynamically loaded routine's mod times
26 %cmd_cache = (); # cache of short names
29 # obtain a new connection this is derived from dxchannel
34 my $self = DXChannel::alloc(@_);
35 $self->{sort} = 'U'; # in absence of how to find out what sort of an object I am
39 # this is how a a connection starts, you get a hello message and the motd with
40 # possibly some other messages asking you to set various things up if you are
41 # new (or nearly new and slacking) user.
45 my ($self, $line) = @_;
46 my $user = $self->{user};
47 my $call = $self->{call};
48 my $name = $user->{name};
50 $self->{name} = $name ? $name : $call;
51 $self->msg('l2',$self->{name});
52 $self->send_file($main::motd) if (-e $main::motd);
53 $self->msg('pr', $call);
54 $self->state('prompt'); # a bit of room for further expansion, passwords etc
55 $self->{priv} = $user->priv;
56 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
57 $self->{consort} = $line; # save the connection type
59 # set some necessary flags on the user if they are connecting
60 $self->{wwv} = $self->{talk} = $self->{ann} = $self->{here} = $self->{dx} = 1;
61 $self->prompt() if $self->{state} =~ /^prompt/o;
66 # This is the normal command prompt driver
71 my $user = $self->{user};
72 my $call = $self->{call};
76 $cmdline =~ s|//|/|og;
78 # split the command line up into parts, the first part is the command
79 my ($cmd, $args) = $cmdline =~ /^([\w\/]+)\s*(.*)/o;
85 # first expand out the entry to a command
86 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
87 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
89 my @ans = $self->eval_file($path, $fcmd, $args) if $path && $fcmd;
90 # @ans = $self->eval_file($main::cmd, $cmd, $args) if !$ans[0];
93 $self->send(@ans) if @ans > 0;
97 $self->msg('e2', @ans);
106 # send a prompt only if we are in a prompt state
107 $self->prompt() if $self->{state} =~ /^prompt/o;
111 # This is called from inside the main cluster processing loop and is used
112 # for despatching commands that are doing some long processing job
117 my @chan = DXChannel->get_all();
120 foreach $chan (@chan) {
121 next if $chan->sort ne 'U';
123 # send a prompt if no activity out on this channel
124 if ($t >= $chan->t + $main::user_interval) {
125 $chan->prompt() if $chan->{state} =~ /^prompt/o;
132 # finish up a user context
140 # short cut to output a prompt
146 my $call = $self->{call};
147 DXChannel::msg($self, 'pr', $call);
150 # broadcast a message to all users [except those mentioned after buffer]
153 my $pkg = shift; # ignored
154 my $s = shift; # the line to be rebroadcast
155 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
156 my @list = DXChannel->get_all(); # just in case we are called from some funny object
159 L: foreach $chan (@list) {
160 next if !$chan->sort eq 'U'; # only interested in user channels
161 foreach $except (@except) {
162 next L if $except == $chan; # ignore channels in the 'except' list
164 chan->send($s); # send it
168 # gimme all the users
171 my @list = DXChannel->get_all();
174 foreach $ref (@list) {
175 push @out, $ref if $ref->sort eq 'U';
181 # search for the command in the cache of short->long form commands
186 my ($path, $short_cmd, $suffix) = @_;
189 # commands are lower case
190 $short_cmd = lc $short_cmd;
191 dbg('command', "command: $path $short_cmd\n");
193 # return immediately if we have it
194 my ($apath, $acmd) = split ',', $cmd_cache{$short_cmd};
195 if ($apath && $acmd) {
196 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
197 return ($apath, $acmd) if $apath;
201 my @parts = split '/', $short_cmd;
207 for ($i = 0; $i < @parts; $i++) {
209 opendir(D, $curdir) or confess "can't open $curdir $!";
213 foreach $l (sort @ls) {
215 if ($i < $#parts) { # we are dealing with directories
216 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
217 dbg('command', "got dir: $curdir/$l\n");
222 } else { # we are dealing with commands
223 next if !$l =~ /\.$suffix$/; # only look for .$suffix files
224 if ($p eq substr($l, 0, length $p)) {
225 $l =~ s/\.$suffix$//; # remove the suffix
226 chop $dirfn; # remove trailing /
227 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn/$l")); # cache it
228 dbg('command', "got path: $path cmd: $dirfn/$l\n");
229 return ($path, "$dirfn/$l");
237 # clear the command name cache
244 # the persistant execution of things from the command directories
247 # This allows perl programs to call functions dynamically
249 # This has been nicked directly from the perlembed pages
252 #require Devel::Symdump;
254 sub valid_package_name {
256 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
258 #second pass only for words starting with a digit
259 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
261 #Dress it up as a real package name
263 return "Emb_" . $string;
266 #borrowed from Safe.pm
272 $pkg = "DXChannel::$pkg\::"; # expand to full symbol table name
273 ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
275 my $stem_symtab = *{$stem}{HASH};
277 delete $stem_symtab->{$leaf};
284 my $package = valid_package_name($cmdname);
285 my $filename = "$path/$cmdname.pl";
286 my $mtime = -M $filename;
288 # return if we can't find it
289 return (0, DXM::msg('e1')) if !defined $mtime;
291 if(defined $Cache{$package}{mtime} && $Cache{$package}{mtime } <= $mtime) {
292 #we have compiled this subroutine already,
293 #it has not been updated on disk, nothing left to do
294 #print STDERR "already compiled $package->handler\n";
298 if (!open FH, $filename) {
299 return (0, "Syserr: can't open '$filename' $!");
305 #wrap the code into a subroutine inside our unique package
306 my $eval = qq{package DXChannel; sub $package { $sub; }};
308 my @list = split /\n/, $eval;
311 dbg('eval', $_, "\n");
314 #print "eval $eval\n";
316 #hide our variables within this block
317 my($filename,$mtime,$package,$sub);
321 delete_package($package);
322 return (0, "Syserr: Eval err $@ on $package");
325 #cache it unless we're cleaning out each time
326 $Cache{$package}{mtime} = $mtime;
330 my $c = qq{ \@r = \$self->$package(\@_); };
331 dbg('eval', "cluster cmd = $c\n");
334 delete_package($package);
335 return (0, "Syserr: Eval err $@ on cached $package");
338 #take a look if you want
339 #print Devel::Symdump->rnew($package)->as_string, $/;