3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
26 use vars qw(%Cache %cmd_cache $errstr %aliases);
28 %Cache = (); # cache of dynamically loaded routine's mod times
29 %cmd_cache = (); # cache of short names
30 $errstr = (); # error string from eval
31 %aliases = (); # aliases for (parts of) commands
34 # obtain a new connection this is derived from dxchannel
39 my $self = DXChannel::alloc(@_);
40 $self->{sort} = 'U'; # in absence of how to find out what sort of an object I am
44 # this is how a a connection starts, you get a hello message and the motd with
45 # possibly some other messages asking you to set various things up if you are
46 # new (or nearly new and slacking) user.
50 my ($self, $line) = @_;
51 my $user = $self->{user};
52 my $call = $self->{call};
53 my $name = $user->{name};
55 $self->{name} = $name ? $name : $call;
56 $self->send($self->msg('l2',$self->{name}));
57 $self->send_file($main::motd) if (-e $main::motd);
58 $self->send($self->msg('pr', $call));
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->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
63 $self->{consort} = $line; # save the connection type
65 # set some necessary flags on the user if they are connecting
66 $self->{wwv} = $self->{talk} = $self->{ann} = $self->{here} = $self->{dx} = 1;
67 # $self->prompt() if $self->{state} =~ /^prompt/o;
69 # add yourself to the database
70 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
71 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
72 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
74 # issue a pc16 to everybody interested
75 my $nchan = DXChannel->get($main::mycall);
76 my @pc16 = DXProt::pc16($nchan, $cuser);
77 DXProt::broadcast_ak1a(@pc16);
78 Log('DXCommand', "$call connected");
82 # This is the normal command prompt driver
87 my $user = $self->{user};
88 my $call = $self->{call};
92 # are we in stored state?
94 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
95 dbg('eval', "stored func cmd = $c\n");
98 return (1, "Syserr: Eval err $errstr on stored func $self->{func}");
102 # special case only \n input => " "
103 if ($cmdline eq " ") {
109 $cmdline =~ s|//|/|og;
111 # split the command line up into parts, the first part is the command
112 my ($cmd, $args) = $cmdline =~ /^([\w\/]+)\s*(.*)/o;
118 # alias it if possible
119 my $acmd = CmdAlias::get_cmd($cmd);
121 ($cmd, $args) = "$acmd $args" =~ /^([\w\/]+)\s*(.*)/o;
124 # first expand out the entry to a command
125 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
126 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
128 my $package = find_cmd_name($path, $fcmd);
129 @ans = (0) if !$package ;
132 my $c = qq{ \@ans = $package(\$self, \$args) };
133 dbg('eval', "cluster cmd = $c\n");
136 @ans = (0, "Syserr: Eval err cached $package\n$@");
144 $self->send(@ans) if @ans > 0;
148 $self->send($self->msg('e2'), @ans);
150 $self->send($self->msg('e1'));
154 # send a prompt only if we are in a prompt state
155 $self->prompt() if $self->{state} =~ /^prompt/o;
159 # This is called from inside the main cluster processing loop and is used
160 # for despatching commands that are doing some long processing job
165 my @chan = DXChannel->get_all();
168 foreach $chan (@chan) {
169 next if $chan->sort ne 'U';
171 # send a prompt if no activity out on this channel
172 if ($t >= $chan->t + $main::user_interval) {
173 $chan->prompt() if $chan->{state} =~ /^prompt/o;
180 # finish up a user context
185 my $call = $self->call;
187 if ($call eq $main::myalias) { # unset the channel if it is us really
188 my $node = DXNode->get($main::mycall);
191 my $ref = DXNodeuser->get($call);
193 # issue a pc17 to everybody interested
194 my $nchan = DXChannel->get($main::mycall);
195 my $pc17 = $nchan->pc17($self);
196 DXProt::broadcast_ak1a($pc17);
198 Log('DXCommand', "$call disconnected");
203 # short cut to output a prompt
209 my $call = $self->{call};
210 $self->send($self->msg('pr', $call));
211 #DXChannel::msg($self, 'pr', $call);
214 # broadcast a message to all users [except those mentioned after buffer]
217 my $pkg = shift; # ignored
218 my $s = shift; # the line to be rebroadcast
219 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
220 my @list = DXChannel->get_all(); # just in case we are called from some funny object
223 L: foreach $chan (@list) {
224 next if !$chan->sort eq 'U'; # only interested in user channels
225 foreach $except (@except) {
226 next L if $except == $chan; # ignore channels in the 'except' list
228 chan->send($s); # send it
232 # gimme all the users
235 my @list = DXChannel->get_all();
238 foreach $ref (@list) {
239 push @out, $ref if $ref->sort eq 'U';
245 # search for the command in the cache of short->long form commands
250 my ($path, $short_cmd, $suffix) = @_;
253 # commands are lower case
254 $short_cmd = lc $short_cmd;
255 dbg('command', "command: $path $short_cmd\n");
257 # return immediately if we have it
258 my ($apath, $acmd) = split ',', $cmd_cache{$short_cmd};
259 if ($apath && $acmd) {
260 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
261 return ($apath, $acmd);
265 my @parts = split '/', $short_cmd;
272 for ($i = 0; $i < @parts; $i++) {
274 opendir(D, $curdir) or confess "can't open $curdir $!";
278 foreach $l (sort @ls) {
280 if ($i < $#parts) { # we are dealing with directories
281 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
282 dbg('command', "got dir: $curdir/$l\n");
287 } else { # we are dealing with commands
288 @lparts = split /\./, $l;
289 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
290 if ($p eq substr($l, 0, length $p)) {
291 pop @lparts; # remove the suffix
292 $l = join '.', @lparts;
293 # chop $dirfn; # remove trailing /
294 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
295 dbg('command', "got path: $path cmd: $dirfn$l\n");
296 return ($path, "$dirfn$l");
304 # clear the command name cache
311 # the persistant execution of things from the command directories
314 # This allows perl programs to call functions dynamically
316 # This has been nicked directly from the perlembed pages
319 #require Devel::Symdump;
321 sub valid_package_name {
323 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
325 #second pass only for words starting with a digit
326 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
328 #Dress it up as a real package name
329 $string =~ s/\//_/og;
330 return "Emb_" . $string;
333 #borrowed from Safe.pm
339 $pkg = "DXCommandmode::$pkg\::"; # expand to full symbol table name
340 ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
342 if ($stem && $leaf) {
343 my $stem_symtab = *{$stem}{HASH};
344 delete $stem_symtab->{$leaf};
348 # find a cmd reference
349 # this is really for use in user written stubs
351 # use the result as a symbolic reference:-
354 # @out = &$r($self, $line);
363 # first expand out the entry to a command
364 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
365 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
367 # make sure it is loaded
368 $r = find_cmd_name($path, $fcmd);
374 # this bit of magic finds a command in the offered directory
378 my $package = valid_package_name($cmdname);
379 my $filename = "$path/$cmdname.pl";
380 my $mtime = -M $filename;
382 # return if we can't find it
385 $errstr = DXM::msg('e1');
389 if(defined $Cache{$package}{mtime} && $Cache{$package}{mtime } <= $mtime) {
390 #we have compiled this subroutine already,
391 #it has not been updated on disk, nothing left to do
392 #print STDERR "already compiled $package->handler\n";
395 my $fh = new FileHandle;
396 if (!open $fh, $filename) {
397 $errstr = "Syserr: can't open '$filename' $!";
404 #wrap the code into a subroutine inside our unique package
412 my @list = split /\n/, $eval;
415 dbg('eval', $_, "\n");
420 #hide our variables within this block
421 my($filename,$mtime,$package,$sub);
428 delete_package($package);
430 #cache it unless we're cleaning out each time
431 $Cache{$package}{mtime} = $mtime;
435 #print Devel::Symdump->rnew($package)->as_string, $/;
436 $package = "DXCommandmode::$package" if $package;
437 $package = undef if $errstr;