3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
24 use vars qw(%Cache %cmd_cache $errstr);
26 %Cache = (); # cache of dynamically loaded routine's mod times
27 %cmd_cache = (); # cache of short names
28 $errstr = (); # error string from eval
30 # obtain a new connection this is derived from dxchannel
35 my $self = DXChannel::alloc(@_);
36 $self->{sort} = 'U'; # in absence of how to find out what sort of an object I am
40 # this is how a a connection starts, you get a hello message and the motd with
41 # possibly some other messages asking you to set various things up if you are
42 # new (or nearly new and slacking) user.
46 my ($self, $line) = @_;
47 my $user = $self->{user};
48 my $call = $self->{call};
49 my $name = $user->{name};
51 $self->{name} = $name ? $name : $call;
52 $self->send($self->msg('l2',$self->{name}));
53 $self->send_file($main::motd) if (-e $main::motd);
54 $self->send($self->msg('pr', $call));
55 $self->state('prompt'); # a bit of room for further expansion, passwords etc
56 $self->{priv} = $user->priv;
57 $self->{lang} = $user->lang;
58 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
59 $self->{consort} = $line; # save the connection type
61 # set some necessary flags on the user if they are connecting
62 $self->{wwv} = $self->{talk} = $self->{ann} = $self->{here} = $self->{dx} = 1;
63 # $self->prompt() if $self->{state} =~ /^prompt/o;
65 # add yourself to the database
66 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
67 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
68 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
70 # issue a pc16 to everybody interested
71 my $nchan = DXChannel->get($main::mycall);
72 my @pc16 = DXProt::pc16($nchan, $cuser);
73 DXProt::broadcast_ak1a(@pc16);
77 # This is the normal command prompt driver
82 my $user = $self->{user};
83 my $call = $self->{call};
87 # are we in stored state?
89 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
90 dbg('eval', "stored func cmd = $c\n");
93 return (1, "Syserr: Eval err $errstr on stored func $self->{func}");
97 # special case only \n input => " "
98 if ($cmdline eq " ") {
104 $cmdline =~ s|//|/|og;
106 # split the command line up into parts, the first part is the command
107 my ($cmd, $args) = $cmdline =~ /^([\w\/]+)\s*(.*)/o;
113 # first expand out the entry to a command
114 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
115 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
117 my $package = find_cmd_name($path, $fcmd);
118 @ans = (0, "Syserr: compile err on $package\n$@$errstr") if !$package ;
121 my $c = qq{ \@ans = $package(\$self, \$args) };
122 dbg('eval', "cluster cmd = $c\n");
125 @ans = (0, "Syserr: Eval err cached $package\n$@");
131 # my @ans = $self->eval_file($path, $fcmd, $args) if $path && $fcmd;
132 # @ans = $self->eval_file($main::cmd, $cmd, $args) if !$ans[0];
135 $self->send(@ans) if @ans > 0;
139 $self->send($self->msg('e2', @ans));
141 $self->send($self->msg('e1'));
145 # send a prompt only if we are in a prompt state
146 $self->prompt() if $self->{state} =~ /^prompt/o;
150 # This is called from inside the main cluster processing loop and is used
151 # for despatching commands that are doing some long processing job
156 my @chan = DXChannel->get_all();
159 foreach $chan (@chan) {
160 next if $chan->sort ne 'U';
162 # send a prompt if no activity out on this channel
163 if ($t >= $chan->t + $main::user_interval) {
164 $chan->prompt() if $chan->{state} =~ /^prompt/o;
171 # finish up a user context
176 my $call = $self->call;
178 if ($call eq $main::myalias) { # unset the channel if it is us really
179 my $node = DXNode->get($main::mycall);
182 my $ref = DXNodeuser->get($call);
184 # issue a pc17 to everybody interested
185 my $nchan = DXChannel->get($main::mycall);
186 my $pc17 = $nchan->pc17($self);
187 DXProt::broadcast_ak1a($pc17);
193 # short cut to output a prompt
199 my $call = $self->{call};
200 $self->send($self->msg('pr', $call));
201 #DXChannel::msg($self, 'pr', $call);
204 # broadcast a message to all users [except those mentioned after buffer]
207 my $pkg = shift; # ignored
208 my $s = shift; # the line to be rebroadcast
209 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
210 my @list = DXChannel->get_all(); # just in case we are called from some funny object
213 L: foreach $chan (@list) {
214 next if !$chan->sort eq 'U'; # only interested in user channels
215 foreach $except (@except) {
216 next L if $except == $chan; # ignore channels in the 'except' list
218 chan->send($s); # send it
222 # gimme all the users
225 my @list = DXChannel->get_all();
228 foreach $ref (@list) {
229 push @out, $ref if $ref->sort eq 'U';
235 # search for the command in the cache of short->long form commands
240 my ($path, $short_cmd, $suffix) = @_;
243 # commands are lower case
244 $short_cmd = lc $short_cmd;
245 dbg('command', "command: $path $short_cmd\n");
247 # return immediately if we have it
248 my ($apath, $acmd) = split ',', $cmd_cache{$short_cmd};
249 if ($apath && $acmd) {
250 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
251 return ($apath, $acmd);
255 my @parts = split '/', $short_cmd;
262 for ($i = 0; $i < @parts; $i++) {
264 opendir(D, $curdir) or confess "can't open $curdir $!";
268 foreach $l (sort @ls) {
270 if ($i < $#parts) { # we are dealing with directories
271 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
272 dbg('command', "got dir: $curdir/$l\n");
277 } else { # we are dealing with commands
278 @lparts = split /\./, $l;
279 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
280 if ($p eq substr($l, 0, length $p)) {
281 pop @lparts; # remove the suffix
282 $l = join '.', @lparts;
283 # chop $dirfn; # remove trailing /
284 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
285 dbg('command', "got path: $path cmd: $dirfn$l\n");
286 return ($path, "$dirfn$l");
294 # clear the command name cache
301 # the persistant execution of things from the command directories
304 # This allows perl programs to call functions dynamically
306 # This has been nicked directly from the perlembed pages
309 #require Devel::Symdump;
311 sub valid_package_name {
313 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
315 #second pass only for words starting with a digit
316 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
318 #Dress it up as a real package name
319 $string =~ s/\//_/og;
320 return "Emb_" . $string;
323 #borrowed from Safe.pm
329 $pkg = "DXCommandmode::$pkg\::"; # expand to full symbol table name
330 ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
332 if ($stem && $leaf) {
333 my $stem_symtab = *{$stem}{HASH};
334 delete $stem_symtab->{$leaf};
338 # find a cmd reference
339 # this is really for use in user written stubs
341 # use the result as a symbolic reference:-
344 # @out = &$r($self, $line);
353 # first expand out the entry to a command
354 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
355 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
357 # make sure it is loaded
358 $r = find_cmd_name($path, $fcmd);
364 # this bit of magic finds a command in the offered directory
368 my $package = valid_package_name($cmdname);
369 my $filename = "$path/$cmdname.pl";
370 my $mtime = -M $filename;
372 # return if we can't find it
375 $errstr = DXM::msg('e1');
379 if(defined $Cache{$package}{mtime} && $Cache{$package}{mtime } <= $mtime) {
380 #we have compiled this subroutine already,
381 #it has not been updated on disk, nothing left to do
382 #print STDERR "already compiled $package->handler\n";
385 my $fh = new FileHandle;
386 if (!open $fh, $filename) {
387 $errstr = "Syserr: can't open '$filename' $!";
389 my $old = $fh->input_record_separator(undef);
391 $fh->input_record_separator($old);
394 #wrap the code into a subroutine inside our unique package
402 my @list = split /\n/, $eval;
405 dbg('eval', $_, "\n");
410 #hide our variables within this block
411 my($filename,$mtime,$package,$sub);
418 delete_package($package);
421 #cache it unless we're cleaning out each time
422 $Cache{$package}{mtime} = $mtime;
426 #print Devel::Symdump->rnew($package)->as_string, $/;
427 $package = "DXCommandmode::$package" if $package;