3 # $Id: SWMan.pm,v 1.1 1999/07/30 18:46:37 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.1 1999/07/30 18:46:37 mdw
32 # New CGI script for browsing installed software and documentation.
35 #----- Package preamble -----------------------------------------------------
45 use SWCGI qw(:DEFAULT :layout);
48 @EXPORT_OK = qw(subst check);
50 #----- Useful functions -----------------------------------------------------
54 # --- @mans(SECTION)@ ---
56 # Returns a reference to a list of manual pages in the given section.
60 $mandb{$sec} and return $mandb{sec};
62 my $d = DirHandle->new("$C{prefix}/man/man$sec") or return undef;
64 while (my $f = $d->read()) {
71 # --- @check(NAME, SECTION)@ ---
73 # See whether there's a manpage called NAME with section SECTION.
76 my $pre = "$C{prefix}/man/man";
80 # --- Quick check for obvious things ---
82 my ($base) = ($sec =~ /^(\d+)/);
83 $f = "$pre$base/$man.$sec";
84 -r $f and return $f; $f .= ".gz"; -r $f and return $f;
86 # --- Snarf the appropriate filename list ---
88 my $fs = mans($base) or return undef;
89 foreach my $f (@$fs) {
90 $f =~ /^$man\.$sec\w+(\.gz)?$/ and return "$C{prefix}/man/man$base/$f";
95 # --- @subst(STRING, NAME, SECTION)@ ---
97 # If NAME(SECTION) is a manual page, return the STRING appropriately wrapped
98 # in an anchor element; otherwise return it unmolested.
101 my ($s, $n, $sec) = @_;
103 return "<a href=\"$ref?act=man&man=$n&sec=$sec\">$s</a>";
107 # --- @sections()@ ---
109 # Return a list of manual sections.
114 return @sectionlist if @sectionlist;
116 my $d = DirHandle->new("$C{prefix}/man") or
117 barf("couldn't open man directory: $!");
118 while ($f = $d->read()) {
119 next if $f !~ /^man/ || !-d "$C{prefix}/man/$f";
122 return (@sectionlist = sort(@s));
125 #----- Display a quick section index ----------------------------------------
128 print "Quick section index:\n";
129 foreach $s (sections()) {
130 print "<a href=\"$ref?act=man&sec=$s\">$s</a>\n";
134 #----- Display indices for manual sections ----------------------------------
140 barf("illegal section `$sec'") if $sec =~ m:/:;
142 # --- Snarf the list of manual pages in this section ---
145 my $d = DirHandle->new("$C{prefix}/man/man$sec") or
146 barf("couldn't read directory `$C{prefix}/man/man$sec': $!");
147 while (my $f = $d->read()) {
148 my ($man, $sec) = split(/\./, $f, 3);
149 push(@m, "$man($sec)") if $sec;
153 # --- Sort and emit the index ---
155 print("<h4>Section $sec</h4>\n<table>");
159 foreach my $m (sort(@m)) {
160 my ($man, $sec) = $m =~ /^(.*)\((.*)\)$/;
161 $col or print("<tr>\n");
162 print("<td><a href=\"$ref?act=man&man=$man&sec=$sec\">$m</a>\n");
163 $col = ($col + 1) % 5;
172 header("Index of manual section $sec");
173 quickie(); print "<hr>\n";
175 print "<hr>\n"; quickie();;
180 header("Manual page index");
181 print("<h3>Manual page index</h3>\n");
182 foreach my $s (sections()) { dosection($s); }
186 #----- Display a manual page ------------------------------------------------
189 my ($man, $sec) = ($Q{"man"}, $Q{"sec"});
191 $sec or &index(), return;
192 $man or §ion(), return;
194 my $file = check($man, $sec) or
195 barf("no manual page $man($sec)");
196 barf("illegal filename `$file'") if $file =~ m:\./:;
198 # --- Read the manual page ---
200 my $p = IO::Pipe->new();
202 defined($kid) or barf("fork failed: $!");
205 dup2($p->fileno(), 1);
206 chdir("$C{prefix}/man");
207 if ($file =~ /\.gz$/) {
210 defined($kid) or exit(127);
213 dup2($pp->fileno, 1);
214 exec("gzip", "-dc", $file);
217 exec("nroff", "-man");
219 exec("nroff", "-man", $file);
225 # --- Spit out the manual page now ---
227 header("Manual page $Q{man}($Q{sec})");
228 quickie(); print "<hr>\n";
230 while (my $line = $p->getline()) {
233 # --- Grind through the line turning it into HTML ---
239 for (my $i = 0; $i < length($line); $i++) {
240 my $ch = substr($line, $i, 1);
243 # --- Sort out overstriking ---
245 if (substr($line, $i + 1, 1) eq "
\b") {
246 my ($italic, $bold) = (0, 0);
247 $ch eq "_" and $italic = 1;
248 $ch eq substr($line, $i + 2, 1) and $bold = 1;
249 $ch = substr($line, $i + 2, 1);
250 while (substr($line, $i + 1, 1) eq "
\b") { $i += 2; }
251 if ($italic && $bold) {
252 $nstate = $state ? $state : "b";
259 $state ne $nstate and
260 $l .= ($state && "</$state>") . ($nstate && "<$nstate>");
263 # --- Translate the character if it's magical ---
265 $ch eq "&" and $ch = "&";
266 $ch eq "<" and $ch = "<";
267 $ch eq ">" and $ch = ">";
270 $state and $l .= "</$state>";
272 # --- Now find manual references in there ---
274 # I don't use /x regexps very often, but I think this is a good excuse.
276 $l =~ s! ((?:\<[bi]\>)*) # Leading highlighting tags
277 ([-_.\w]+) # Various plausible manual name chars
278 ((?:\</[bi]\>)* # Closing highlighting tags
279 (?:\<[bi]\>)* # And opening ones again
280 \( # An open parenthesis
281 (?:\<[bi]\>)*) # More opening highlights
282 (\d+\w*) # The section number
283 ((?:\</[bi]\>)* # Close highlights
285 (?:\</[bi]\>)*) # Finally more closing tags
286 ! subst($&, $2, $4) !egx;
288 # --- And email and hypertext references too ---
290 $l =~ s! ((?:\<[bi]\>)*) # Leading highlighting
291 ((?:http|ftp) # A protocol name
292 :// # The important and obvious bit
293 [^]&)\s]+ # Most characters are allowed
294 [^]&).,\s\'\"]) # Don't end on punctuation
295 ((?:\</[bi]\>)*) # Closing tags, optional
296 !<a href="$2">$&</a>!gx;
298 $l =~ s! ((?:\<[bi]\>)*)
299 ( [^\s()&;{}<>,.\`\"] [^\s()&;{}<>\`\"]* \@
300 [^\s()&;{}<>\'\"]* [^\s()&;{}<>.,\'\"])
302 !<a href="mailto:$2">$&</a>!gx;
310 # --- Done all of that ---
315 barf("nroff failed (exit status $?)") if $?;
316 print "<hr>\n"; quickie();;
320 #----- Register actions -----------------------------------------------------
322 $main::ACT{"man"} = \&man;
324 #----- That's all, folks ----------------------------------------------------