~ubuntu-branches/ubuntu/hoary/rman/hoary

« back to all changes in this revision

Viewing changes to contrib/hman.pl

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2001-06-23 19:24:49 UTC
  • Revision ID: james.westby@ubuntu.com-20010623192449-xw26ktesjs3ef4ld
Tags: upstream-3.0.9
ImportĀ upstreamĀ versionĀ 3.0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
#!/citi/gtfd/mach/bin/perl -w
 
3
###############
 
4
# $Id: hman.pl,v 1.7 1997/11/04 21:56:13 teto Exp teto $
 
5
# $Source: /pub/src/dev/hman/RCS/hman.pl,v $
 
6
############################################
 
7
# TODO:
 
8
#       reorganize location of man pages - move 3x stuff from man3 to man3x
 
9
#       recurse - if 'man 3x curses' does not work try 'man 3 curses'
 
10
#       display more STDERR
 
11
#       Fix broken whatis entries - instead of
 
12
#               manpage section - description
 
13
#               manpage description section -
 
14
#       highlite keywords found
 
15
#       pass MANPATH as a command line argument
 
16
############################################
 
17
# Inspired by:
 
18
#       http://www.jinr.dubna.su/~gagin
 
19
#       http://thsun1.jinr.dubna.su/~gagin/rman.pl
 
20
#       http://thsun1.jinr.dubna.su/cgi-bin/rman.pl
 
21
#       http://www.jinr.dubna.su/~gagin/rman.pl.html
 
22
#
 
23
# CGI form interface to PolyglotMan program, which is available as
 
24
#       ftp://ftp.cs.berkeley.edu:/ucb/people/phelps/tcltk/rman.tar.Z
 
25
#
 
26
# The most recent version of this program available as
 
27
#       http://www.geocities.com/SiliconValley/Lakes/8777/hman.html
 
28
#---------------------------------------------------------------------
 
29
#  This is free software; you can redistribute it and/or modify
 
30
#  it under the terms of the GNU General Public License as published by
 
31
#  the Free Software Foundation; either version 2, or (at your option)
 
32
#  any later version.
 
33
 
34
#  hman is distributed in the hope that
 
35
#  it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 
36
#  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
37
#  See the GNU General Public License for more details.
 
38
 
39
#  You should have received a copy of the GNU General Public License
 
40
#  along with this software; see the file COPYING.  If not, write to the
 
41
#  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
42
#  Boston, MA 02111-1307, USA.
 
43
 
44
#  THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
 
45
#  CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
 
46
#  PERFOHMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
 
47
#  NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
 
48
#  SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
 
49
#  SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
 
50
#  PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").
 
51
#  ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES
 
52
#  IS SPECIFICALLY DISCLAIMED.
 
53
#---------------------------------------------------------------------
 
54
# Request form: hman.pl?ManTopic=SOMETOPIC&ManSection=SECTION
 
55
# Request form: hman.pl?DirectPath=filename
 
56
#
 
57
#---------------------------------------------------------------------
 
58
# Stuff to change
 
59
 
 
60
# path to PolyglotMan program. "-b" is not nessesary
 
61
$hman="$ENV{HMANPRG} $ENV{HMANOPT}";
 
62
 
 
63
# path to man program
 
64
$ManPrg='/usr/bin/man';
 
65
 
 
66
# path to cat program
 
67
$ENV{PAGER} = '/bin/cat';
 
68
 
 
69
# path to man directories
 
70
($ManPathFind = $ENV{MANPATH}) =~ s/:/ /g;
 
71
 
 
72
# URL to this program
 
73
$hmanpl='/cgi-bin/hman';
 
74
 
 
75
# if man produced number of lines less then follows,
 
76
# I assume that request failed
 
77
$emptyman=5;
 
78
 
 
79
# tail of every produced html document
 
80
$HtmlTail='<hr><A HREF="' . $hmanpl . '">Back to Hman</a>';
 
81
# $HtmlTitle="<title>CGI form interface to PolyglotMan</title>\n";
 
82
$HtmlHdr="Content-type: text/html\n\n";
 
83
 
 
84
# end changable things
 
85
#----------------------------------------------------------------------
 
86
@ManSections = (
 
87
    '1', 'user commands',
 
88
    '2', 'system calls',
 
89
    '3', 'subroutines',
 
90
    '4', 'devices',
 
91
    '5', 'file formats',
 
92
    '6', 'games',
 
93
    '7', 'miscellanious',
 
94
    '8', 'sys. admin.',
 
95
    '9', 'Linux Internals',
 
96
    'n', 'section n'
 
97
);
 
98
 
 
99
#   Set unbuffered I/O.  Prevents buffering problems with
 
100
#   "system()" calls.
 
101
select((select(STDOUT), $| = 1)[0]);
 
102
print $HtmlHdr;
 
103
 
 
104
$string = $ENV{QUERY_STRING};
 
105
#
 
106
#       Initial Form
 
107
#
 
108
if($string eq "") { initialForm(); }
 
109
 
 
110
#
 
111
#       Generic parameter parsing ...
 
112
#
 
113
$DirectPath = $ManSection = $ManTopic = "";
 
114
$string =~ s/&/'; \$/g;
 
115
$string =~ s/=/='/g;
 
116
$string =~ s/^(.*)$/\$$1';/;
 
117
eval $string;
 
118
 
 
119
hmanDirect($DirectPath) if ($DirectPath ne "");
 
120
 
 
121
if ($ManTopic eq "") { badness("<code>Topic for man search needed</code>\n"); }
 
122
 
 
123
if ($ManSection eq "") { badness("<code>No section specified</code>\n"); }
 
124
 
 
125
$ManSection =~ s/all//;
 
126
if ($ManSection =~ /key/) { manKey($ManTopic); }
 
127
 
 
128
findIt($ManTopic);
 
129
open(MANOUT, "$ManPrg $ManSection $ManTopic |")
 
130
    || die "$hmanpl: can't run \"$ManPrg Section $ManTopic |\", $!\n";
 
131
for (0..$emptyman) {
 
132
    $temp = <MANOUT> || last;
 
133
    push @temp, $temp;
 
134
}
 
135
#
 
136
if (@temp < $emptyman) {
 
137
    close(MANOUT);
 
138
    print"<strong>Request failed for topic $ManTopic:</strong>\n<code>";
 
139
    for (@temp) {print;}
 
140
    print "</code><br><h3>Let's try a keyword search:</h3><hr>\n";
 
141
    manKey($ManTopic);
 
142
}
 
143
#
 
144
$cmd = "$hman -r \"$hmanpl?ManTopic=%s&ManSection=%s\" -l \"Man page for $ManTopic";
 
145
if ($ManSection eq "") {
 
146
    $cmd .= '"';
 
147
} else {
 
148
    $cmd .= "($ManSection)\"";
 
149
}
 
150
#
 
151
open(HMANIN, "| $cmd") || die "$hmanpl: can't open $cmd: $!\n";
 
152
for (@temp) {print HMANIN;}
 
153
while(<MANOUT>){print HMANIN;}
 
154
close(MANOUT);
 
155
close(HMANIN);
 
156
 
 
157
exitIt();
 
158
###############################################################################
 
159
sub initialForm {
 
160
    print <<EOF;
 
161
<h1>Select a manual page:</h1>
 
162
    <FORM METHOD="GET" action="$hmanpl">
 
163
        <table border=0>
 
164
            <tr>
 
165
                <td align=right>Section:</td>
 
166
                <td>
 
167
                    <select name=ManSection>
 
168
                        <OPTION value=all>All</option>
 
169
                        <OPTION value=key>Keyword Search</option>
 
170
EOF
 
171
    for ($ndx = 0; $ndx < @ManSections; $ndx += 2) {
 
172
        print '                 <OPTION value=' . $ManSections[$ndx] . '>'
 
173
            . "$ManSections[$ndx] - $ManSections[$ndx + 1] "
 
174
            . '</option>' . "\n";
 
175
    }
 
176
    print <<EOF;
 
177
                    </SELECT>
 
178
                </td>
 
179
            </tr>
 
180
            <tr>
 
181
                <td align=right>Topic:</td>
 
182
                <td><input type="TEXT" name="ManTopic"></td>
 
183
            </tr>
 
184
            <tr>
 
185
                <td align=right><INPUT TYPE="submit" VALUE="Search">
 
186
                <td align=left><input type="reset" value="Reset">
 
187
                </td>
 
188
            </tr>
 
189
        </table>
 
190
    </FORM>
 
191
EOF
 
192
    exitIt();
 
193
}
 
194
sub findIt {
 
195
    my($topic) = ($_[0]);
 
196
    my($cmd, $mixedCase, $navigation, $zcat);
 
197
    $mixedCase = '';
 
198
    foreach (split(/\s*/, $topic)) {
 
199
        $mixedCase .= "[" . $_ . uc($_) . "]";
 
200
    }
 
201
    $cmd = 'find ' . $ManPathFind . ' \( -type f -o -type l \) -name '
 
202
        . $mixedCase .'\* | sort -t. +1';
 
203
    open(FINDIN, "$cmd |") || die "can't open pipe \"$cmd |\": $!";
 
204
    $navigation = 0;
 
205
    while (<FINDIN>) {
 
206
        if ($navigation == 0) {
 
207
            print "<UL>\n";
 
208
            $navigation = 1;
 
209
        }
 
210
        $_ =~ s/[\n\r]*$//;
 
211
        print "<li><A HREF=\"$hmanpl?DirectPath=$_\">$_</a>\n";
 
212
    }
 
213
    close(FINDIN);
 
214
    print "</UL><hr>\n" if ($navigation == 1);
 
215
}
 
216
sub hmanDirect {
 
217
    my($path) = ($_[0], $_[1]);
 
218
    my($cmd, $dir, $topic, $section);
 
219
    ($dir = $path) =~ s/\.(gz|z|Z)$//;
 
220
    ($topic = $dir) =~ s,^.*/,,;
 
221
    $dir =~ s,/[^/]*/[^/]*$,,;
 
222
    # $dir =~ s,/[^/]*$,,;
 
223
    ($section=$topic)=~s/^.*\.([^\.]*)$/$1/;
 
224
    $topic =~ s/\.[^\.]*$//;
 
225
    findIt($topic);
 
226
    $cmd = "cd $dir; (gzip -dc < $path 2>/dev/null || cat < $path) | $hman -r '"
 
227
        . $hmanpl . '?ManTopic=%s&ManSection=%s' ."' -l 'Man page for $topic($section)'";
 
228
    system($cmd) || warn "can't run command \"$cmd\": $!";
 
229
    print $HtmlTail;
 
230
    exit 0;
 
231
}
 
232
sub exitIt {
 
233
    print $HtmlTail;
 
234
    exit 0;
 
235
}
 
236
sub badness {
 
237
    my($text) = ($_[0]);
 
238
    print "<strong>Request failed:</strong> $text, Try again<hr>\n";
 
239
    initialForm();
 
240
}
 
241
sub manKey {
 
242
    my($topic) = ($_[0]);
 
243
    open(TMPHTML,"$ManPrg -k $topic | sort -u |")
 
244
        || die "can't open pipe \"$ManPrg -k $topic | sort -u |\": $!\n";
 
245
    print "<title>Keyword search results for \"$topic\"</title>\n";
 
246
    print "<h1>Keyword search results for \"$topic\"</h1><hr>\n";
 
247
    while(<TMPHTML>) {
 
248
        s/\( \)//g;
 
249
        next if (! /^([^(]+)\s*\(([^)]+)[^-]+-\s(.*)[\n\r]*$/);
 
250
        @topics=split(/, /,$1);
 
251
        next if ($2 eq "");
 
252
        print "<h2>$3:</h2>\n";
 
253
        print "<UL>\n";
 
254
        for $topic (@topics) {
 
255
            print "<li><A HREF=\"$hmanpl?ManSection=$2&ManTopic=$topic\">$topic</a>($2)\n";
 
256
        }
 
257
        print "</UL>\n";
 
258
    }
 
259
    close(TMPHTML);
 
260
    exitIt();
 
261
}