3 # $Id: SWMan.pm,v 1.3 1999/08/19 12:11:10 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.3 1999/08/19 12:11:10 mdw
32 # More improvements to URL recognizer.
34 # Revision 1.2 1999/08/18 17:10:07 mdw
35 # Slight improvements to URL and email address parsing.
37 # Revision 1.1 1999/07/30 18:46:37 mdw
38 # New CGI script for browsing installed software and documentation.
41 #----- Package preamble -----------------------------------------------------
51 use SWCGI qw(:DEFAULT :layout);
54 @EXPORT_OK = qw(subst urlsubst check);
56 #----- Useful functions -----------------------------------------------------
60 # --- @mans(SECTION)@ ---
62 # Returns a reference to a list of manual pages in the given section.
66 $mandb{$sec} and return $mandb{sec};
68 my $d = DirHandle->new("$C{prefix}/man/man$sec") or return undef;
70 while (my $f = $d->read()) {
77 # --- @check(NAME, SECTION)@ ---
79 # See whether there's a manpage called NAME with section SECTION.
82 my $pre = "$C{prefix}/man/man";
86 # --- Quick check for obvious things ---
88 my ($base) = ($sec =~ /^(\d+)/);
89 $f = "$pre$base/$man.$sec";
90 -r $f and return $f; $f .= ".gz"; -r $f and return $f;
92 # --- Snarf the appropriate filename list ---
94 my $fs = mans($base) or return undef;
95 foreach my $f (@$fs) {
96 $f =~ /^$man\.$sec\w+(\.gz)?$/ and return "$C{prefix}/man/man$base/$f";
101 # --- @subst(STRING, NAME, SECTION)@ ---
103 # If NAME(SECTION) is a manual page, return the STRING appropriately wrapped
104 # in an anchor element; otherwise return it unmolested.
107 my ($s, $n, $sec) = @_;
109 return "<a href=\"$ref?act=man&man=$n&sec=$sec\">$s</a>";
113 # --- @urlsubst(URL, STRING)@ ---
115 # Substitutes in a URL reference. The important bit is that embedded `&'
116 # characters are un-entitied from `&'. This doesn't seem to upset
117 # Netscape or Lynx as much as I'd expect (or, in fact, at all), but it's
121 my ($url, $name) = @_;
122 $url =~ s/\&\;/&/;
123 return "<a href=\"$url\">$name</a>";
126 # --- @sections()@ ---
128 # Return a list of manual sections.
133 return @sectionlist if @sectionlist;
135 my $d = DirHandle->new("$C{prefix}/man") or
136 barf("couldn't open man directory: $!");
137 while ($f = $d->read()) {
138 next if $f !~ /^man/ || !-d "$C{prefix}/man/$f";
141 return (@sectionlist = sort(@s));
144 #----- Display a quick section index ----------------------------------------
147 print "Quick section index:\n";
148 foreach $s (sections()) {
149 print "<a href=\"$ref?act=man&sec=$s\">$s</a>\n";
153 #----- Display indices for manual sections ----------------------------------
159 barf("illegal section `$sec'") if $sec =~ m:/:;
161 # --- Snarf the list of manual pages in this section ---
164 my $d = DirHandle->new("$C{prefix}/man/man$sec") or
165 barf("couldn't read directory `$C{prefix}/man/man$sec': $!");
166 while (my $f = $d->read()) {
167 my ($man, $sec) = split(/\./, $f, 3);
168 push(@m, "$man($sec)") if $sec;
172 # --- Sort and emit the index ---
174 print("<h4>Section $sec</h4>\n<table>");
178 foreach my $m (sort(@m)) {
179 my ($man, $sec) = $m =~ /^(.*)\((.*)\)$/;
180 $col or print("<tr>\n");
181 print("<td><a href=\"$ref?act=man&man=$man&sec=$sec\">$m</a>\n");
182 $col = ($col + 1) % 5;
191 header("Index of manual section $sec");
192 quickie(); print "<hr>\n";
194 print "<hr>\n"; quickie();;
199 header("Manual page index");
200 print("<h3>Manual page index</h3>\n");
201 foreach my $s (sections()) { dosection($s); }
205 #----- Display a manual page ------------------------------------------------
208 my ($man, $sec) = ($Q{"man"}, $Q{"sec"});
210 $sec or &index(), return;
211 $man or §ion(), return;
213 my $file = check($man, $sec) or
214 barf("no manual page $man($sec)");
215 barf("illegal filename `$file'") if $file =~ m:\./:;
217 # --- Read the manual page ---
219 my $p = IO::Pipe->new();
221 defined($kid) or barf("fork failed: $!");
224 dup2($p->fileno(), 1);
225 chdir("$C{prefix}/man");
226 if ($file =~ /\.gz$/) {
229 defined($kid) or exit(127);
232 dup2($pp->fileno, 1);
233 exec("gzip", "-dc", $file);
236 exec("nroff", "-man");
238 exec("nroff", "-man", $file);
244 # --- Spit out the manual page now ---
246 header("Manual page $Q{man}($Q{sec})");
247 quickie(); print "<hr>\n";
249 while (my $line = $p->getline()) {
252 # --- Grind through the line turning it into HTML ---
258 for (my $i = 0; $i < length($line); $i++) {
259 my $ch = substr($line, $i, 1);
262 # --- Sort out overstriking ---
264 if (substr($line, $i + 1, 1) eq "
\b") {
265 my ($italic, $bold) = (0, 0);
266 $ch eq "_" and $italic = 1;
267 $ch eq substr($line, $i + 2, 1) and $bold = 1;
268 $ch = substr($line, $i + 2, 1);
269 while (substr($line, $i + 1, 1) eq "
\b") { $i += 2; }
270 if ($italic && $bold) {
271 $nstate = $state ? $state : "b";
278 $state ne $nstate and
279 $l .= ($state && "</$state>") . ($nstate && "<$nstate>");
282 # --- Translate the character if it's magical ---
284 $ch eq "&" and $ch = "&";
285 $ch eq "<" and $ch = "<<";
286 $ch eq ">" and $ch = ">>";
289 $state and $l .= "</$state>";
291 # --- Now find manual references in there ---
293 # I don't use /x regexps very often, but I think this is a good excuse.
295 $l =~ s! ((?:\<[bi]\>)*) # Leading highlighting tags
296 ([-_.\w]+) # Various plausible manual name chars
297 ((?:\</[bi]\>)* # Closing highlighting tags
298 (?:\<[bi]\>)* # And opening ones again
299 \( # An open parenthesis
300 (?:\<[bi]\>)*) # More opening highlights
301 (\d+\w*) # The section number
302 ((?:\</[bi]\>)* # Close highlights
304 (?:\</[bi]\>)*) # Finally more closing tags
305 ! subst($&, $2, $4) !egx;
307 # --- And email and hypertext references too ---
309 $l =~ s! ((?:\<[bi]\>)*) # Leading highlighting
310 ( \b (?: http s? | ftp | file | news ) # A protocol name
311 : # The important and obvious bit
312 [^]<>)\s<>\'\"]+ # Most characters are allowed
313 [^]<>).,\s<>\'\"]) # Don't end on punctuation
314 ((?:\</[bi]\>)*) # Closing tags, optional
315 !urlsubst($2, $&)!egx;
317 $l =~ s! ( (?:\<[bi]\>)* (?:\bmailto:)? )
318 ( [^\s()<>;:{}&<>,.\`\'\"] [^\s()<>;:{}&<>\`\'\"]* \@
319 [^\s()<>;:{&}<>\'\"]* [^\s()<>;:{}&<>.,\'\"])
321 !<a href="mailto:$2">$&</a>!gx;
323 # --- Fix up the HTML ---
325 $l =~ s/\<\;\</</g;
326 $l =~ s/\>\>\;/>/g;
334 # --- Done all of that ---
339 barf("nroff failed (exit status $?)") if $?;
340 print "<hr>\n"; quickie();;
344 #----- Register actions -----------------------------------------------------
346 $main::ACT{"man"} = \&man;
348 #----- That's all, folks ----------------------------------------------------