diff options
-rwxr-xr-x | mansrv | 72 | ||||
-rw-r--r-- | top.html | 4 |
2 files changed, 56 insertions, 20 deletions
@@ -19,9 +19,10 @@ use strict; use File::Basename; use sigtrap; use Sys::Syslog; +use Safe; -my $server = "mansrv"; -my $version = "1.0"; +my $package_name = "mansrv"; +our $VERSION = "1.1"; my $cf = "/etc/mansrv.conf"; our $docdir; @@ -110,17 +111,34 @@ sub build_manpath() { $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>) { - s/\@TITLE\@/$ARGV[1]/g; - s/\@SECTION\@/$ARGV[0]/g; - s/\@SERVER\@/$server/g; - s/\@VERSION\@/$version/g; - print $ofd $_; + chomp; + s/\{%(.*?)%\}/expand_template($s, $1, $file, $.)/gex; + print $ofd "$_\n"; } close($ifd); } @@ -159,9 +177,7 @@ sub checkdeps($) { # ############################################################################# -my $script; # This script name. -($script = $0) =~ s/.*\///; -openlog($script, "ndelay,pid", "daemon"); +openlog(basename($0), "ndelay,pid", "daemon"); if ($ENV{'MANSRV_CONF'}) { $cf = $ENV{'MANSRV_CONF'}; @@ -178,11 +194,21 @@ if ($manref =~ /\?$/) { } # Set up environment -&build_manpath; +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: http://man.gnu.org.ua\n"; + print "Location: $ENV{REQUEST_SCHEME}://$ENV{SERVER_NAME}\n"; print "\n"; exit 0; } @@ -205,6 +231,8 @@ 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: $!"); @@ -455,24 +483,32 @@ Include path for B<groff> (list of directories separated with semicolons). =head1 TEMPLATE SUBSTITUTIONS While interpreting the contents of the files B<htmltop>, B<htmlbot> and -B<errpage>, the following character sequences are removed and replaced with -the corresponding expansions: +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<@SERVER@> +=item B<$PACKAGE> Canonical name of the program (B<mansrv>). -=item B<@VERSION@> +=item B<$VERSION> Version of B<mansrv>. -=item B<@TITLE@> +=item B<%ENV> + +Trimmed environment from the master process. + +=item B<$SERVER> + +Base server URL. + +=item B<$TITLE> Manpage title. -=item B<@SECTION@> +=item B<$SECTION> Requested manpage section. @@ -2,9 +2,9 @@ "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> -<meta name="generator" content="@SERVER@ @VERSION@"> +<meta name="generator" content="{% "$PACKAGE $VERSION" %}"> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> -<title>@TITLE@</title> +<title>{% $TITLE %}</title> </head> <body> <div id="content"> |