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