3 # $Id: SWMan.pm,v 1.2 1999/08/18 17:10:07 mdw Exp $
5 # Display and other fiddling of manual pages
10 #----- Licensing notice -----------------------------------------------------
12 # This file is part of sw-tools.
14 # sw-tools is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
19 # sw-tools is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with sw-tools; if not, write to the Free Software Foundation,
26 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #----- Revision history -----------------------------------------------------
31 # Revision 1.2 1999/08/18 17:10:07 mdw
32 # Slight improvements to URL and email address parsing.
34 # Revision 1.1 1999/07/30 18:46:37 mdw
35 # New CGI script for browsing installed software and documentation.
38 #----- Package preamble -----------------------------------------------------
48 use SWCGI qw(:DEFAULT :layout);
51 @EXPORT_OK = qw(subst check);
53 #----- Useful functions -----------------------------------------------------
57 # --- @mans(SECTION)@ ---
59 # Returns a reference to a list of manual pages in the given section.
63 $mandb{$sec} and return $mandb{sec};
65 my $d = DirHandle->new("$C{prefix}/man/man$sec") or return undef;
67 while (my $f = $d->read()) {
74 # --- @check(NAME, SECTION)@ ---
76 # See whether there's a manpage called NAME with section SECTION.
79 my $pre = "$C{prefix}/man/man";
83 # --- Quick check for obvious things ---
85 my ($base) = ($sec =~ /^(\d+)/);
86 $f = "$pre$base/$man.$sec";
87 -r $f and return $f; $f .= ".gz"; -r $f and return $f;
89 # --- Snarf the appropriate filename list ---
91 my $fs = mans($base) or return undef;
92 foreach my $f (@$fs) {
93 $f =~ /^$man\.$sec\w+(\.gz)?$/ and return "$C{prefix}/man/man$base/$f";
98 # --- @subst(STRING, NAME, SECTION)@ ---
100 # If NAME(SECTION) is a manual page, return the STRING appropriately wrapped
101 # in an anchor element; otherwise return it unmolested.
104 my ($s, $n, $sec) = @_;
106 return "<a href=\"$ref?act=man&man=$n&sec=$sec\">$s</a>";
110 # --- @sections()@ ---
112 # Return a list of manual sections.
117 return @sectionlist if @sectionlist;
119 my $d = DirHandle->new("$C{prefix}/man") or
120 barf("couldn't open man directory: $!");
121 while ($f = $d->read()) {
122 next if $f !~ /^man/ || !-d "$C{prefix}/man/$f";
125 return (@sectionlist = sort(@s));
128 #----- Display a quick section index ----------------------------------------
131 print "Quick section index:\n";
132 foreach $s (sections()) {
133 print "<a href=\"$ref?act=man&sec=$s\">$s</a>\n";
137 #----- Display indices for manual sections ----------------------------------
143 barf("illegal section `$sec'") if $sec =~ m:/:;
145 # --- Snarf the list of manual pages in this section ---
148 my $d = DirHandle->new("$C{prefix}/man/man$sec") or
149 barf("couldn't read directory `$C{prefix}/man/man$sec': $!");
150 while (my $f = $d->read()) {
151 my ($man, $sec) = split(/\./, $f, 3);
152 push(@m, "$man($sec)") if $sec;
156 # --- Sort and emit the index ---
158 print("<h4>Section $sec</h4>\n<table>");
162 foreach my $m (sort(@m)) {
163 my ($man, $sec) = $m =~ /^(.*)\((.*)\)$/;
164 $col or print("<tr>\n");
165 print("<td><a href=\"$ref?act=man&man=$man&sec=$sec\">$m</a>\n");
166 $col = ($col + 1) % 5;
175 header("Index of manual section $sec");
176 quickie(); print "<hr>\n";
178 print "<hr>\n"; quickie();;
183 header("Manual page index");
184 print("<h3>Manual page index</h3>\n");
185 foreach my $s (sections()) { dosection($s); }
189 #----- Display a manual page ------------------------------------------------
192 my ($man, $sec) = ($Q{"man"}, $Q{"sec"});
194 $sec or &index(), return;
195 $man or §ion(), return;
197 my $file = check($man, $sec) or
198 barf("no manual page $man($sec)");
199 barf("illegal filename `$file'") if $file =~ m:\./:;
201 # --- Read the manual page ---
203 my $p = IO::Pipe->new();
205 defined($kid) or barf("fork failed: $!");
208 dup2($p->fileno(), 1);
209 chdir("$C{prefix}/man");
210 if ($file =~ /\.gz$/) {
213 defined($kid) or exit(127);
216 dup2($pp->fileno, 1);
217 exec("gzip", "-dc", $file);
220 exec("nroff", "-man");
222 exec("nroff", "-man", $file);
228 # --- Spit out the manual page now ---
230 header("Manual page $Q{man}($Q{sec})");
231 quickie(); print "<hr>\n";
233 while (my $line = $p->getline()) {
236 # --- Grind through the line turning it into HTML ---
242 for (my $i = 0; $i < length($line); $i++) {
243 my $ch = substr($line, $i, 1);
246 # --- Sort out overstriking ---
248 if (substr($line, $i + 1, 1) eq "
\b") {
249 my ($italic, $bold) = (0, 0);
250 $ch eq "_" and $italic = 1;
251 $ch eq substr($line, $i + 2, 1) and $bold = 1;
252 $ch = substr($line, $i + 2, 1);
253 while (substr($line, $i + 1, 1) eq "
\b") { $i += 2; }
254 if ($italic && $bold) {
255 $nstate = $state ? $state : "b";
262 $state ne $nstate and
263 $l .= ($state && "</$state>") . ($nstate && "<$nstate>");
266 # --- Translate the character if it's magical ---
268 $ch eq "&" and $ch = "&";
269 $ch eq "<" and $ch = "<";
270 $ch eq ">" and $ch = ">";
273 $state and $l .= "</$state>";
275 # --- Now find manual references in there ---
277 # I don't use /x regexps very often, but I think this is a good excuse.
279 $l =~ s! ((?:\<[bi]\>)*) # Leading highlighting tags
280 ([-_.\w]+) # Various plausible manual name chars
281 ((?:\</[bi]\>)* # Closing highlighting tags
282 (?:\<[bi]\>)* # And opening ones again
283 \( # An open parenthesis
284 (?:\<[bi]\>)*) # More opening highlights
285 (\d+\w*) # The section number
286 ((?:\</[bi]\>)* # Close highlights
288 (?:\</[bi]\>)*) # Finally more closing tags
289 ! subst($&, $2, $4) !egx;
291 # --- And email and hypertext references too ---
293 $l =~ s! ((?:\<[bi]\>)*) # Leading highlighting
294 ((?:https?|ftp|file|news) # A protocol name
295 : # The important and obvious bit
296 [^]&)\s]+ # Most characters are allowed
297 [^]&).,\s\'\"]) # Don't end on punctuation
298 ((?:\</[bi]\>)*) # Closing tags, optional
299 !$1<a href="$2">$&</a>$3!gx;
301 $l =~ s! ((?:\<[bi]\>)*(?:\bmailto:)?)
302 ( [^\s()&;:{}<>,.\`\"] [^\s()&;:{}<>\`\"]* \@
303 [^\s()&;:{}<>\'\"]* [^\s()&;:{}<>.,\'\"])
305 !$1<a href="mailto:$2">$&</a>$3!gx;
313 # --- Done all of that ---
318 barf("nroff failed (exit status $?)") if $?;
319 print "<hr>\n"; quickie();;
323 #----- Register actions -----------------------------------------------------
325 $main::ACT{"man"} = \&man;
327 #----- That's all, folks ----------------------------------------------------