fix missing origin for isolated nodes
[spider.git] / cmd / apropos.pl
1
2 # the help subsystem
3 #
4 # apropos - this does a grep on the command file and returns the commands
5 # that contain the string searched for
6 #
7 # Copyright (c) 1998 - Dirk Koopman G1TLH
8 #
9 # $Id$
10 #
11
12 my ($self, $line) = @_;
13 my @out;
14
15 my $lang = $self->lang;
16 $lang = 'en' if !$lang;
17
18 my $in;
19 $line = 'help' unless $line;
20 $line =~ s/\W//g;   # remove dubious characters
21
22 my ($priv, $cmd, $desc);
23 my %cmd;
24
25 my $defh = new IO::File;
26 unless ($defh->open("$main::localcmd/Commands_en.hlp")) {
27         unless($defh->open("$main::cmd/Commands_en.hlp")) {
28                 return (1, $self->msg('helpe1'));
29         }
30 }
31
32 my $h;
33 if ($lang ne 'en') {
34         $h = new IO::File;
35         unless ($h->open("$main::localcmd/Commands_$lang.hlp")) {
36                 unless($h->open("$main::cmd/Commands_$lang.hlp")) {
37                         undef $h;
38                 }
39         }
40 }
41
42 # do english help
43 my $include;
44 foreach $in (<$defh>) {
45         next if $in =~ /^\#/;
46         chomp $in;
47         $in =~ s/\r$//;
48         if ($in =~ /^===/) {
49                 $cmd{$cmd} = "$cmd $desc" if $include;
50                 $include = 0;
51                 $in =~ s/=== //;
52                 ($priv, $cmd, $desc) = split /\^/, $in;
53                 next if $priv > $self->priv;             # ignore subcommands that are of no concern
54                 next unless $cmd =~ /$line/i || $desc =~ /$line/i;
55                 next if $cmd =~ /-$/o;
56                 $include = 1;
57                 next;
58         }
59         $include = 1 if $cmd =~ /$line/i;
60 }
61 $cmd{$cmd} = "$cmd $desc" if $include;
62 $defh->close;
63
64 # override with any not english help
65 if ($h) {
66         my $include;
67         foreach $in (<$h>) {
68                 next if $in =~ /^\#/;
69                 chomp $in;
70                 $in =~ s/\r$//;
71                 if ($in =~ /^===/) {
72                         $cmd{$cmd} = "$cmd $desc" if $include;
73                         $include = 0;
74                         $in =~ s/=== //;
75                         ($priv, $cmd, $desc) = split /\^/, $in;
76                         next if $priv > $self->priv;             # ignore subcommands that are of no concern
77                         next unless $cmd =~ /$line/i || $desc =~ /$line/i;
78                         next if $cmd =~ /-$/o;
79                         $include = 1;
80                         next;
81                 }
82                 $include = 1 if $cmd =~ /$line/i;
83         }
84         $cmd{$cmd} = "$cmd $desc" if $include;
85         $h->close;
86 }
87
88 push @out, map {$cmd{$_}} sort keys %cmd;
89
90 push @out, $self->msg('helpe2', $line) if @out == 0;
91
92 return (1, @out);