summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org.ua>2019-10-07 10:13:12 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2019-10-07 10:16:59 (GMT)
commit59065ff6e02b605cddb55c15436b294f553c8619 (patch) (side-by-side diff)
tree71768453bb50b772a607a012a03365eb2c4cf745
parenta928def5ed6fff777d32a19c635274aa33635655 (diff)
downloadmansrv-59065ff6e02b605cddb55c15436b294f553c8619.tar.gz
mansrv-59065ff6e02b605cddb55c15436b294f553c8619.tar.bz2
Various improvements
* mansrv: Interpret material appearing between {% and %} in template files as Perl expression. Avoid using hardcoded URLs.
Diffstat (more/less context) (ignore whitespace changes)
-rwxr-xr-xmansrv70
-rw-r--r--top.html4
2 files changed, 54 insertions, 20 deletions
diff --git a/mansrv b/mansrv
index f05fedb..5e99ec1 100755
--- a/mansrv
+++ b/mansrv
@@ -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')} = "$proto://$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, $.)/ex;
+ 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;
}
@@ -457,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.
diff --git a/top.html b/top.html
index 768cefe..c53dc92 100644
--- a/top.html
+++ b/top.html
@@ -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">

Return to:

Send suggestions and report system problems to the System administrator.