3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
27 use vars qw(%Cache %cmd_cache $errstr %aliases);
29 %Cache = (); # cache of dynamically loaded routine's mod times
30 %cmd_cache = (); # cache of short names
31 $errstr = (); # error string from eval
32 %aliases = (); # aliases for (parts of) commands
35 # obtain a new connection this is derived from dxchannel
40 my $self = DXChannel::alloc(@_);
41 $self->{sort} = 'U'; # in absence of how to find out what sort of an object I am
45 # this is how a a connection starts, you get a hello message and the motd with
46 # possibly some other messages asking you to set various things up if you are
47 # new (or nearly new and slacking) user.
51 my ($self, $line, $sort) = @_;
52 my $user = $self->{user};
53 my $call = $self->{call};
54 my $name = $user->{name};
56 $self->{name} = $name ? $name : $call;
57 $self->send($self->msg('l2',$self->{name}));
58 $self->send_file($main::motd) if (-e $main::motd);
59 $self->state('prompt'); # a bit of room for further expansion, passwords etc
60 $self->{priv} = $user->priv;
61 $self->{lang} = $user->lang;
62 $self->{pagelth} = 20;
63 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
64 $self->{consort} = $line; # save the connection type
66 # set some necessary flags on the user if they are connecting
67 $self->{wwv} = $self->{talk} = $self->{ann} = $self->{here} = $self->{dx} = 1;
68 # $self->prompt() if $self->{state} =~ /^prompt/o;
70 # add yourself to the database
71 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
72 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
73 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
75 # issue a pc16 to everybody interested
76 my $nchan = DXChannel->get($main::mycall);
77 my @pc16 = DXProt::pc16($nchan, $cuser);
78 DXProt::broadcast_ak1a(@pc16);
79 Log('DXCommand', "$call connected");
81 # send prompts and things
82 my $info = DXCluster::cluster();
83 $self->send("Cluster:$info");
84 $self->send($self->msg('pr', $call));
88 # This is the normal command prompt driver
97 # remove leading and trailing spaces
98 $cmdline =~ s/^\s*(.*)\s*$/$1/;
100 if ($self->{state} eq 'page') {
101 my $i = $self->{pagelth};
102 my $ref = $self->{pagedata};
105 # abort if we get a line starting in with a
106 if ($cmdline =~ /^a/io) {
111 # send a tranche of data
112 while ($i-- > 0 && @$ref) {
113 my $line = shift @$ref;
114 $line =~ s/\s+$//o; # why am having to do this?
118 # reset state if none or else chuck out an intermediate prompt
120 $tot -= $self->{pagelth};
121 $self->send($self->msg('page', $tot));
123 $self->state('prompt');
126 @ans = run_cmd($self, $cmdline) if length $cmdline;
128 if ($self->{pagelth} && @ans > $self->{pagelth}) {
130 for ($i = $self->{pagelth}; $i-- > 0; ) {
131 my $line = shift @ans;
132 $line =~ s/\s+$//o; # why am having to do this?
135 $self->{pagedata} = \@ans;
136 $self->state('page');
137 $self->send($self->msg('page', scalar @ans));
140 s/\s+$//o; # why ?????????
146 # send a prompt only if we are in a prompt state
147 $self->prompt() if $self->{state} =~ /^prompt/o;
151 # this is the thing that runs the command, it is done like this for the
152 # benefit of remote command execution
158 my $user = $self->{user};
159 my $call = $self->{call};
164 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
165 dbg('eval', "stored func cmd = $c\n");
168 return (1, "Syserr: Eval err $errstr on stored func $self->{func}");
173 $cmdline =~ s|//|/|og;
175 # split the command line up into parts, the first part is the command
176 my ($cmd, $args) = $cmdline =~ /^([\w\/]+)\s*(.*)/o;
182 # alias it if possible
183 my $acmd = CmdAlias::get_cmd($cmd);
185 ($cmd, $args) = "$acmd $args" =~ /^([\w\/]+)\s*(.*)/o;
188 # first expand out the entry to a command
189 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
190 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
192 my $package = find_cmd_name($path, $fcmd);
193 @ans = (0) if !$package ;
196 my $c = qq{ \@ans = $package(\$self, \$args) };
197 dbg('eval', "cluster cmd = $c\n");
200 @ans = (0, "Syserr: Eval err cached $package\n$@");
211 unshift @ans, $self->msg('e2');
213 @ans = $self->msg('e1');
220 # This is called from inside the main cluster processing loop and is used
221 # for despatching commands that are doing some long processing job
226 my @chan = DXChannel->get_all();
229 foreach $chan (@chan) {
230 next if $chan->sort ne 'U';
232 # send a prompt if no activity out on this channel
233 if ($t >= $chan->t + $main::user_interval) {
234 $chan->prompt() if $chan->{state} =~ /^prompt/o;
241 # finish up a user context
246 my $call = $self->call;
248 if ($call eq $main::myalias) { # unset the channel if it is us really
249 my $node = DXNode->get($main::mycall);
252 my $ref = DXNodeuser->get($call);
254 # issue a pc17 to everybody interested
255 my $nchan = DXChannel->get($main::mycall);
256 my $pc17 = $nchan->pc17($self);
257 DXProt::broadcast_ak1a($pc17);
259 Log('DXCommand', "$call disconnected");
264 # short cut to output a prompt
270 my $call = $self->{call};
271 $self->send($self->msg('pr', $call));
272 #DXChannel::msg($self, 'pr', $call);
275 # broadcast a message to all users [except those mentioned after buffer]
278 my $pkg = shift; # ignored
279 my $s = shift; # the line to be rebroadcast
280 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
281 my @list = DXChannel->get_all(); # just in case we are called from some funny object
284 L: foreach $chan (@list) {
285 next if !$chan->sort eq 'U'; # only interested in user channels
286 foreach $except (@except) {
287 next L if $except == $chan; # ignore channels in the 'except' list
289 chan->send($s); # send it
293 # gimme all the users
296 my @list = DXChannel->get_all();
299 foreach $ref (@list) {
300 push @out, $ref if $ref->sort eq 'U';
306 # search for the command in the cache of short->long form commands
311 my ($path, $short_cmd, $suffix) = @_;
314 # commands are lower case
315 $short_cmd = lc $short_cmd;
316 dbg('command', "command: $path $short_cmd\n");
318 # return immediately if we have it
319 my ($apath, $acmd) = split ',', $cmd_cache{$short_cmd};
320 if ($apath && $acmd) {
321 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
322 return ($apath, $acmd);
326 my @parts = split '/', $short_cmd;
333 for ($i = 0; $i < @parts; $i++) {
335 opendir(D, $curdir) or confess "can't open $curdir $!";
339 foreach $l (sort @ls) {
341 if ($i < $#parts) { # we are dealing with directories
342 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
343 dbg('command', "got dir: $curdir/$l\n");
348 } else { # we are dealing with commands
349 @lparts = split /\./, $l;
350 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
351 if ($p eq substr($l, 0, length $p)) {
352 pop @lparts; # remove the suffix
353 $l = join '.', @lparts;
354 # chop $dirfn; # remove trailing /
355 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
356 dbg('command', "got path: $path cmd: $dirfn$l\n");
357 return ($path, "$dirfn$l");
365 # clear the command name cache
372 # the persistant execution of things from the command directories
375 # This allows perl programs to call functions dynamically
377 # This has been nicked directly from the perlembed pages
380 #require Devel::Symdump;
382 sub valid_package_name {
384 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
386 #second pass only for words starting with a digit
387 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
389 #Dress it up as a real package name
390 $string =~ s/\//_/og;
391 return "Emb_" . $string;
394 #borrowed from Safe.pm
400 $pkg = "DXCommandmode::$pkg\::"; # expand to full symbol table name
401 ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
403 if ($stem && $leaf) {
404 my $stem_symtab = *{$stem}{HASH};
405 delete $stem_symtab->{$leaf};
409 # find a cmd reference
410 # this is really for use in user written stubs
412 # use the result as a symbolic reference:-
415 # @out = &$r($self, $line);
424 # first expand out the entry to a command
425 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
426 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
428 # make sure it is loaded
429 $r = find_cmd_name($path, $fcmd);
435 # this bit of magic finds a command in the offered directory
439 my $package = valid_package_name($cmdname);
440 my $filename = "$path/$cmdname.pl";
441 my $mtime = -M $filename;
443 # return if we can't find it
446 $errstr = DXM::msg('e1');
450 if(defined $Cache{$package}{mtime} && $Cache{$package}{mtime } <= $mtime) {
451 #we have compiled this subroutine already,
452 #it has not been updated on disk, nothing left to do
453 #print STDERR "already compiled $package->handler\n";
456 my $fh = new FileHandle;
457 if (!open $fh, $filename) {
458 $errstr = "Syserr: can't open '$filename' $!";
465 #wrap the code into a subroutine inside our unique package
473 my @list = split /\n/, $eval;
476 dbg('eval', $_, "\n");
481 #hide our variables within this block
482 my($filename,$mtime,$package,$sub);
489 delete_package($package);
491 #cache it unless we're cleaning out each time
492 $Cache{$package}{mtime} = $mtime;
496 #print Devel::Symdump->rnew($package)->as_string, $/;
497 $package = "DXCommandmode::$package" if $package;
498 $package = undef if $errstr;