3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
25 use vars qw(%Cache %cmd_cache $errstr %aliases);
27 %Cache = (); # cache of dynamically loaded routine's mod times
28 %cmd_cache = (); # cache of short names
29 $errstr = (); # error string from eval
30 %aliases = (); # aliases for (parts of) commands
33 # obtain a new connection this is derived from dxchannel
38 my $self = DXChannel::alloc(@_);
39 $self->{sort} = 'U'; # in absence of how to find out what sort of an object I am
43 # this is how a a connection starts, you get a hello message and the motd with
44 # possibly some other messages asking you to set various things up if you are
45 # new (or nearly new and slacking) user.
49 my ($self, $line) = @_;
50 my $user = $self->{user};
51 my $call = $self->{call};
52 my $name = $user->{name};
54 $self->{name} = $name ? $name : $call;
55 $self->send($self->msg('l2',$self->{name}));
56 $self->send_file($main::motd) if (-e $main::motd);
57 $self->send($self->msg('pr', $call));
58 $self->state('prompt'); # a bit of room for further expansion, passwords etc
59 $self->{priv} = $user->priv;
60 $self->{lang} = $user->lang;
61 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
62 $self->{consort} = $line; # save the connection type
64 # set some necessary flags on the user if they are connecting
65 $self->{wwv} = $self->{talk} = $self->{ann} = $self->{here} = $self->{dx} = 1;
66 # $self->prompt() if $self->{state} =~ /^prompt/o;
68 # add yourself to the database
69 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
70 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
71 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
73 # issue a pc16 to everybody interested
74 my $nchan = DXChannel->get($main::mycall);
75 my @pc16 = DXProt::pc16($nchan, $cuser);
76 DXProt::broadcast_ak1a(@pc16);
80 # This is the normal command prompt driver
85 my $user = $self->{user};
86 my $call = $self->{call};
90 # are we in stored state?
92 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
93 dbg('eval', "stored func cmd = $c\n");
96 return (1, "Syserr: Eval err $errstr on stored func $self->{func}");
100 # special case only \n input => " "
101 if ($cmdline eq " ") {
107 $cmdline =~ s|//|/|og;
109 # split the command line up into parts, the first part is the command
110 my ($cmd, $args) = $cmdline =~ /^([\w\/]+)\s*(.*)/o;
116 # alias it if possible
117 my $acmd = CmdAlias::get_cmd($cmd);
119 ($cmd, $args) = "$acmd $args" =~ /^([\w\/]+)\s*(.*)/o;
122 # first expand out the entry to a command
123 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
124 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
126 my $package = find_cmd_name($path, $fcmd);
127 @ans = (0) if !$package ;
130 my $c = qq{ \@ans = $package(\$self, \$args) };
131 dbg('eval', "cluster cmd = $c\n");
134 @ans = (0, "Syserr: Eval err cached $package\n$@");
142 $self->send(@ans) if @ans > 0;
146 $self->send($self->msg('e2'), @ans);
148 $self->send($self->msg('e1'));
152 # send a prompt only if we are in a prompt state
153 $self->prompt() if $self->{state} =~ /^prompt/o;
157 # This is called from inside the main cluster processing loop and is used
158 # for despatching commands that are doing some long processing job
163 my @chan = DXChannel->get_all();
166 foreach $chan (@chan) {
167 next if $chan->sort ne 'U';
169 # send a prompt if no activity out on this channel
170 if ($t >= $chan->t + $main::user_interval) {
171 $chan->prompt() if $chan->{state} =~ /^prompt/o;
178 # finish up a user context
183 my $call = $self->call;
185 if ($call eq $main::myalias) { # unset the channel if it is us really
186 my $node = DXNode->get($main::mycall);
189 my $ref = DXNodeuser->get($call);
191 # issue a pc17 to everybody interested
192 my $nchan = DXChannel->get($main::mycall);
193 my $pc17 = $nchan->pc17($self);
194 DXProt::broadcast_ak1a($pc17);
200 # short cut to output a prompt
206 my $call = $self->{call};
207 $self->send($self->msg('pr', $call));
208 #DXChannel::msg($self, 'pr', $call);
211 # broadcast a message to all users [except those mentioned after buffer]
214 my $pkg = shift; # ignored
215 my $s = shift; # the line to be rebroadcast
216 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
217 my @list = DXChannel->get_all(); # just in case we are called from some funny object
220 L: foreach $chan (@list) {
221 next if !$chan->sort eq 'U'; # only interested in user channels
222 foreach $except (@except) {
223 next L if $except == $chan; # ignore channels in the 'except' list
225 chan->send($s); # send it
229 # gimme all the users
232 my @list = DXChannel->get_all();
235 foreach $ref (@list) {
236 push @out, $ref if $ref->sort eq 'U';
242 # search for the command in the cache of short->long form commands
247 my ($path, $short_cmd, $suffix) = @_;
250 # commands are lower case
251 $short_cmd = lc $short_cmd;
252 dbg('command', "command: $path $short_cmd\n");
254 # return immediately if we have it
255 my ($apath, $acmd) = split ',', $cmd_cache{$short_cmd};
256 if ($apath && $acmd) {
257 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
258 return ($apath, $acmd);
262 my @parts = split '/', $short_cmd;
269 for ($i = 0; $i < @parts; $i++) {
271 opendir(D, $curdir) or confess "can't open $curdir $!";
275 foreach $l (sort @ls) {
277 if ($i < $#parts) { # we are dealing with directories
278 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
279 dbg('command', "got dir: $curdir/$l\n");
284 } else { # we are dealing with commands
285 @lparts = split /\./, $l;
286 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
287 if ($p eq substr($l, 0, length $p)) {
288 pop @lparts; # remove the suffix
289 $l = join '.', @lparts;
290 # chop $dirfn; # remove trailing /
291 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
292 dbg('command', "got path: $path cmd: $dirfn$l\n");
293 return ($path, "$dirfn$l");
301 # clear the command name cache
308 # the persistant execution of things from the command directories
311 # This allows perl programs to call functions dynamically
313 # This has been nicked directly from the perlembed pages
316 #require Devel::Symdump;
318 sub valid_package_name {
320 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
322 #second pass only for words starting with a digit
323 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
325 #Dress it up as a real package name
326 $string =~ s/\//_/og;
327 return "Emb_" . $string;
330 #borrowed from Safe.pm
336 $pkg = "DXCommandmode::$pkg\::"; # expand to full symbol table name
337 ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
339 if ($stem && $leaf) {
340 my $stem_symtab = *{$stem}{HASH};
341 delete $stem_symtab->{$leaf};
345 # find a cmd reference
346 # this is really for use in user written stubs
348 # use the result as a symbolic reference:-
351 # @out = &$r($self, $line);
360 # first expand out the entry to a command
361 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
362 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
364 # make sure it is loaded
365 $r = find_cmd_name($path, $fcmd);
371 # this bit of magic finds a command in the offered directory
375 my $package = valid_package_name($cmdname);
376 my $filename = "$path/$cmdname.pl";
377 my $mtime = -M $filename;
379 # return if we can't find it
382 $errstr = DXM::msg('e1');
386 if(defined $Cache{$package}{mtime} && $Cache{$package}{mtime } <= $mtime) {
387 #we have compiled this subroutine already,
388 #it has not been updated on disk, nothing left to do
389 #print STDERR "already compiled $package->handler\n";
392 my $fh = new FileHandle;
393 if (!open $fh, $filename) {
394 $errstr = "Syserr: can't open '$filename' $!";
401 #wrap the code into a subroutine inside our unique package
409 my @list = split /\n/, $eval;
412 dbg('eval', $_, "\n");
417 #hide our variables within this block
418 my($filename,$mtime,$package,$sub);
425 delete_package($package);
427 #cache it unless we're cleaning out each time
428 $Cache{$package}{mtime} = $mtime;
432 #print Devel::Symdump->rnew($package)->as_string, $/;
433 $package = "DXCommandmode::$package" if $package;
434 $package = undef if $errstr;