summaryrefslogtreecommitdiffabout
path: root/mansrv
blob: b4d3ccaabc705d04d3843c4c0ae8e9ee3a5795ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#! /usr/bin/perl
# Manpage CGI server.
# Copyright (C) 2013 Sergey Poznyakoff
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use File::Basename;
use sigtrap;
use Sys::Syslog;
use Safe;

my $package_name = "mansrv";
our $VERSION = "1.1";
my $cf = "/etc/mansrv.conf";

our $docdir;
our $manref = $ENV{'SCRIPT_NAME'}."?";
our $htmltop;
our $htmlbot;
our $errpage;
our $includepath;
our $cachedir = "/tmp/mansrv";
our $incfilesuf;
my @deps = ( $0 );
my @grohtml_opts;

my $tempcachefile;
my $mansep;

my %decomp = (
    'gz' => 'gzip -d -c',
    'bz2' => 'bzip2 -d -c',
    'z' => 'compress -d -c',
    'Z' => 'compress -d -c'
);
    

sub syserror {
    my $s = shift;
    syslog("LOG_ERR", "$s");
    print "<h1>ABEND</h1>\n"; # Let them guess...
    exit 1;
}

my %conftab = (
    'docdir' => \$docdir,
    'manref' => \$manref,
    'htmltop' => \$htmltop,
    'htmlbot' => \$htmlbot,
    'errpage' => \$errpage,
    'cachedir' => \$cachedir,
    'includepath' => \$includepath,
    'incfilesuf' => \$incfilesuf
);

sub read_config($) {
    my $config_file = shift;
    my $err;
    my $fd;
    open($fd, "<", $config_file) or syserror("cannot open $config_file: $!");
    my $line = 0;
    while (<$fd>) {
	++$line;
        chomp;
        s/^\s+//;
        s/\s+$//;
        s/#.*//;
        next if ($_ eq "");
        if (/([^\s]+)\s*=\s*(.+)/) {
	    if (exists($conftab{$1})) {
		${$conftab{$1}} = $2;
	    } else {
		syslog("LOG_ERR", "$config_file:$line: unknown variable: '$1'");
		print "$conftab{$1}\n";
		++$err;
	    }
	} else {
	    syslog("LOG_ERR", "$config_file:$line: syntax error");
	    ++$err;
	}
    }
    close($fd);
    syserror("errors in config file") if ($err);
}

sub sighan {
    unink($tempcachefile) if -e $tempcachefile;
}

sub build_manpath() {
    my $d;

    opendir($d, $docdir) or return;
    my $dirs=join ':',
                sort map { "$docdir/$_/man" }
                grep { !/^\./ && -d "$docdir/$_"  && -d "$docdir/$_/man" }
                readdir($d);
    closedir($d);
    $ENV{'MANPATH'} = "$dirs:$ENV{'MANPATH'}" if ($dirs);
}

sub expand_template {
    my ($comp, $code, $file, $line) = @_;
    my $r = $comp->reval($code);
    unless (defined($r)) {
	syslog("LOG_ERR", "%s:%d: error expanding template expression %s",
	       $file, $line, $code);
	$r = '';
    }
    return $r;
}

sub interpret_file($$) {
    my ($ofd, $file) = @_;
    my $ifd;

    open($ifd, $file) or syserror("cannot open $file: $!");
    my $s = new Safe 'Root' ;
    %{$s->varglob('ENV')} = %ENV;
    ${$s->varglob('TITLE')} = $ARGV[1];
    ${$s->varglob('SECTION')} = $ARGV[0];
    ${$s->varglob('PACKAGE')} = $package_name;
    ${$s->varglob('VERSION')} = $VERSION;
    ${$s->varglob('SERVER')} = "$ENV{REQUEST_SCHEME}://$ENV{SERVER_NAME}";
	
    while (<$ifd>) {
	chomp;
	s/\{%(.*)%\}/expand_template($s, $1, $file, $.)/ex;
	print $ofd "$_\n";
    }
    close($ifd);
}

sub interpret {
    my $fd = shift;
    foreach my $file (@_) {
	interpret_file($fd, $file);
    }
}

sub addopts($) {
    my $file = shift;
    my $fd;

    open($fd, $file) or return;
    while (<$fd>) {
	chomp;
	s/^\s+//;
	s/\s+$//;
	s/#.*//;
	next if ($_ eq "");
	push @grohtml_opts, "-P $_";
    }
    close($fd);
    push @deps, $file;
}    

sub checkdeps($) {
    my $ts = (stat(shift))[9];
    foreach my $depfile (@deps) {
	return 0 if ($ts < (stat($depfile))[9]);
    }
    return 1;
}

# #############################################################################

openlog(basename($0), "ndelay,pid", "daemon");

if ($ENV{'MANSRV_CONF'}) {
    $cf = $ENV{'MANSRV_CONF'};
}
read_config($cf);
push @deps, $cf;
if ($manref =~ /\?$/) {
    $mansep = '+';
} elsif ($manref =~ /\/$/) {
    $mansep = '/';
} else {
    $manref .= "?";
    $mansep = '+';
}

# Set up environment
build_manpath();

my $proto;
if (exists($ENV{HTTP_X_FORWARDED_PROTO})) {
    $proto = $ENV{HTTP_X_FORWARDED_PROTO};
} elsif ($ENV{HTTPS} eq 'on') {
    $proto = 'https';
} else {
    $proto = 'http';
}
$ENV{REQUEST_SCHEME} = $proto;
$ENV{'MANCGI'}='WEBDOC';

if ($#ARGV != 1) {
    print "Location: $ENV{REQUEST_SCHEME}://$ENV{SERVER_NAME}\n";
    print "\n";
    exit 0;
}

# Begin output
print "Content-Type: text/html\n";
print "\n"; # Body begins

my $file = `man -w $ARGV[0] $ARGV[1] 2>/dev/null`;

unless ($file) {
    my $f;
    open($f, ">&STDOUT");
    interpret($f, $htmltop, $errpage, $htmlbot);
    exit 0;
}
chomp $file;
push @deps, $file;

# Split the name and determine the root mandir
my ($manfile, $mandir) = fileparse($file);
$mandir =~ s/\/man[1-8n]//;
$manfile =~ s/^(.*\.[1-8n])\..*/$1/;
$manfile .= ".html";

if (! -d $cachedir) {
    mkdir($cachedir,0755) or syserror("mkdir $cachedir: $!");
}
$cachedir .= "/cat$ARGV[0]";
if (! -d $cachedir) {
    mkdir($cachedir) or syserror("mkdir $cachedir: $!");
}
my $cachefile = "$cachedir/$manfile";
push @deps, $cachefile;

# Check if it has an include file
if ($file =~ /^$docdir\/*/ && $incfilesuf) {
    my $incfile = "$file$incfilesuf";
    if (-f $incfile) {
	push @deps, $incfile;
	$ENV{'INCFILE'} =  $incfile;
    }
}

# Override top and bottom files, if necessary.
$htmltop = "$mandir/top.html" if (-R "$mandir/top.html");
$htmlbot = "$mandir/bottom.html" if (-R "$mandir/bottom.html");

push @deps, $htmltop, $htmlbot;

# Process grohtml options
if (-R "$mandir/grohtml.opt") {
    addopts("$mandir/grohtml.opt");
    push @deps, "$mandir/grohtml.opt";
}

unless (-e $cachefile && checkdeps($cachefile)) {
    my $tempcachefile = "$cachefile.$$";
    open(my $fd, ">$tempcachefile") or syserror("opening $tempcachefile: $!");
    $SIG{'HUP'} = \&sighan;
    $SIG{'INT'} = \&sighan;
    $SIG{'QUIT'} = \&sighan;
    $SIG{'PIPE'} = \&sighan;
    $SIG{'TERM'} = \&sighan;

    my $groffcmd = "groff -Thtml -man ";
    if ($includepath) {
	$groffcmd .= join(' ',map { "-I $_" } split(/:/,$includepath)) . " ";
    }
    if ($#grohtml_opts >= 0) {
	$groffcmd .= join(' ', @grohtml_opts) . " ";
    }

    if ($file =~ /.*\.([^.]+)$/ && exists($decomp{$1})) {
	$groffcmd = "$decomp{$1} $file | $groffcmd -";
    } else {
	$groffcmd .= $file;
    }

    open(my $p, "-|", $groffcmd) or syserror("running \"$groffcmd\": $!");

    interpret($fd, $htmltop);
    
    # State map:
    #   0 - before <body>
    #   1 - between <body> and </body>
    #  [ 2 - after </body> ]
    my $state = 0;
    while (<$p>) {
	if ($state == 0) {
	    if (/<body>(.*)/) {
		print $fd "$1\n";
		$state = 1;
	    }
	    next;
	}
	
	if (/(.*)<\/body>/) {
	    print $fd "$1\n";
	    last;
	}
	
	if (/<b>[^<>]+<\/b>\([0-9n]\)/) {
	    while (/(.*?)<b>([^<>]+)<\/b>\(([0-9n])\)(.*)/) {
		print $fd "$1";
		my $pref = $2;
		my $href = $2;
		my $sect = $3;
		$_ = $4;
		$href =~ s/&minus;/-/g;
		print $fd '<b><a href="'.$manref.$sect.$mansep.$href.'">'.$pref.'</a></b>('.$sect.')';
            }
	    print $fd "$_\n";
	} else {
	    print $fd $_;
	}
    }
    
    close($p);

    interpret($fd, $htmlbot);
    
    close $fd;
    rename($tempcachefile, $cachefile) or 
        syserror("failed to rename $tempcachefile to $cachefile: $!");
}

open(FILE, $cachefile) or syserror("opening $cachefile for reading: $!");
while (<FILE>) {
    print;
}
close FILE;

__END__
=head1 NAME

mansrv - manpage server

=head1 SYNOPSIS

B<mansrv> I<SECTION> I<NAME>    
    
=head1 DESCRIPTION

This CGI script searches for the manpage I<NAME> in the section I<SECTION>
and displays it as an HTML.  It uses B<grohtml>(1) to create initial
translation and preprocesses its output, replacing references to other
manpages by appropriate hyperlinks and fixing some other minor
inconsistencies.

The program is designed to help display on-line the documentation in
manpage formats for multiple software projects without the need to
install these manpages somewhere in the system B<MANPATH>.  This is
necessary for software forge sites that host a number of projects.

To this effect, the following directory structure is assumed.  Each
software project keeps its documentation files in a separate directory,
located in common B<document root> directory.  If the project's directory
contains a directory named B<man>, its full pathname is prepended to
the system B<MANPATH>.  The prepended directories are sorted in
lexicographical order.

When a manpage is requested, it is looked in the B<MANPATH> using
B<man -w>.  If found, it is piped through B<groff -man -Thtml> to
obtain initial translation.  Each project can customize the call
to B<groff> by placing the file named B<grohtml.opt> in its B<man>
subdirectory.  This file should list the valid B<grohtml>(1) options,
one option per line.  Empty lines and comments (beginning with B<#>)
are ignored.

The HTML prologue (anything up to and including the B<<body>> tag)
and epilogue (starting from the B<</body>> closing tag) are
removed and replaced by the content of B<HTML top> and B<HTML
bottom> files which are supplied in the configuration file (see
the B<htmltop> and B<htmlbot> settings below).  A project can
override any one or both of this files by placing the file
B<top.html> and B<bottom.html> in its B<man> subdirectory.

Before inclusion, both files are subject to variable expansion,
during which any occurrence of B<@>I<variable>B<@> is replaced
with the value of the I<variable>.  See the section B<TEMPLATE
SUBSTITUTIONS> below for the list of valid variable names and
their values.

If the source man page is located in one of the hosted projects'
B<man> subdirectories (as opposed to the system default B<MANPATH>),
B<mansrv> also looks for an optional B<include file>, which has the
same pathname as the source page plus the predefined filename suffix
(see the B<incfilesuf> configuration setting below).  If such file
exists, its full pathname is assigned to the B<INCFILE> environment
variable.  This allows to customize man pages for online display,
by using the following B<groff> construct:

    .if !"\V[INCFILE]"" .so \V[INCFILE]
    
The produced HTML output is stored in the cache directory (as
specified by the B<cachedir> configuration setting).  Subsequent
calls to B<mansrv> with the same arguments will retrieve the
already generated page from the cache, instead of regenerating
it each time.

The cached page, however, is invalidated and its regeneration
is triggered if any of the files it depends upon is newer than
the cached copy.  The list of dependencies includes (apart from
the B<mansrv> program itself and its configuration file), both
HTML top and bottom files, the B<grohtml.opt> and the include
file (if any).    
    
=head1 CONFIGURATION

The program reads its configuration from the file named in the
environment variable B<MANSRV_CONF>.  If not set, the default
file name B</etc/mansrv.conf> is used.

The configuration file has a usual UNIX configuration format.  Empty           
lines and comments (beginning with B<#>) are ignored.  Each non-empty
line must have the form B<variable>=B<value>, with any amount of optional
whitespace around the equals sign.  Valid variable names are:

=over 4

=item B<docdir>

Document root directory.  This is a directory under which B<mansrv> looks
for additional man hierarchies.    
    
=item B<manref>

The stem part for references to another manpages.  By default it is deduced
from the B<SCRIPT_NAME> environment variable.

If the value of this variable ends with B<?>, section number and manpage
name in the resulting reference will be separated by B<+> sign.  If its value
ends with B</> the two arguments will be separated by B</>.  Otherwise,
B<?> will be appended to B<manref> and B<+> separator assumed.    

=item B<cachedir>

Cache directory.  The default is B</tmp/mansrv>.
    
=item B<htmltop>

Path to the HTML top template file.  This file must contain at least
the basic HTML prologue part, between B<<html>> and B<<body>> tags,
inclusive.  See B<TEMPLATE SUBSTITUTIONS> below for details on its
processing.    

=item B<htmlbot>

Path to the HTML bottom template file.  It must contain at least
the closing B<</body>> and B<</html>> tags.    
    
=item B<errpage>

Path to the template file for the error page.  This page is displayed
when the requested manpage was not found.    

=item B<incfilesuf>

Include file suffix.  If defined, and the manpage file to be displayed is
located under B<docdir>, B<mansrv> will look for the file named
I<MANFILE>B<incfilesuf>, where I<MANFILE> is the manpage file name.
If this file exists, its full pathname will be assigned to the environment
variable B<INCFILE>.

=item B<includepath>

Include path for B<groff> (list of directories separated with semicolons).
    
=back

=head1 TEMPLATE SUBSTITUTIONS    

While interpreting the contents of the files B<htmltop>, B<htmlbot> and
B<errpage>, the material between B<{%> and B<%}> is evaluated as a Perl
expression.  It can make references to the following variables:

=over 4

=item B<$PACKAGE>

Canonical name of the program (B<mansrv>).

=item B<$VERSION>

Version of B<mansrv>.

=item B<%ENV>

Trimmed environment from the master process.

=item B<$SERVER>

Base server URL.

=item B<$TITLE>

Manpage title.

=item B<$SECTION>

Requested manpage section.

=back

=head1 LOGGING

The program logs all fatal errors to the B<syslog>(3) facility
B<daemon>.

=head1 ENVIRONMENT VARIABLES

The following environment variables affect the behavior of B<mansrv>:

=over 4

=item MANSRV_CONF

The pathname of the configuration file to use instead of the default
one.

=back

Before invoking B<groff>, the following environment variables are
defined:

=over 4    
    
=item B<MANCGI>

Contains the string B<WEBDOC>,

=item B<INCFILE>

Defined if the include file is found (see the description of the
configuration variable B<incfilesuf>).

=back

=head1 FILES

=over 4

=item B</etc/mansrv.conf>

Default configuration file.

=back

=head1 SEE ALSO

B<groff>(1), B<grohtml>(1).    
    
=head1 AUTHOR

Sergey Poznyakoff <gray@gnu.org>

Return to:

Send suggestions and report system problems to the System administrator.