#!/usr/bin/perl ############################################################################### ## ## man2html CGI wrapper to be used with web-servers. ## ## This script prepares an environment to the /usr/bin/man2html program ## to let it be run under a web-server. ## It handles the input parameters and passes it to the man commands. ## Under no parameters, it will generate a simple input form to users. ## ## This is free software licensed under the GPL. Use it under your own risk. ## ## Avi Alkalay ## 7 Feb 2001 ## Made in Brazil ## ############################################################################### # Global variables $CONFIG="man.cgi.config"; $MANCMD="/usr/bin/man -C $CONFIG"; use CGI qw(:standard); ############################################################################### ## ## showForm ## ## Displays the simple input form. ## Accepts one argument that will be used as a error message, printed with ## the form. ## ############################################################################### sub showForm { my ($msg)=@_; print("Content-type: text/html\n\n"); print STDOUT < Manpage Viewer

Manpage Viewer

The following form allows you to view a manpage on this system. Please fill out the following fields and select Submit to view a manpage.

$msg

Section:
Topic:

man2html wrapper by Avi Alkalay <avi at alkalay.net> EOF } ############################################################################### ## ## readCGIInput ## ## Uses de CGI.pm library to get parameters passed via the form or URL. ## Returns a hash containing the values needed by the program. ## ############################################################################### sub readCGIInput { my %input; $input{section}=param("section"); $input{topic}=param("topic"); $input{vhost}=virtual_host(); $input{path}=script_name(); if ($input{section} eq "") { $input{section}=$ARGV[0]; } if ($input{topic} eq "") { $input{topic}=$ARGV[1]; } return %input; } ############################################################################### ## ## doMan ## ## This is the program's most important function. ## It will analize the parameters and execute the "man" command with special ## configurations. ## ############################################################################### sub doMan { my (%cgiInput)=@_; my $child,$parent; my $error; if ($cgiInput{topic} eq "") { showForm("No Topic specified"); exit(0); } # Prepare the command line with special environment vars to be passed # to man2html. Look at man.cgi.config. $MANCMD="VHOST=\"$cgiInput{vhost}\" CGIPATH=\"$cgiInput{path}\" " . $MANCMD; if ($cgiInput{section} != 0) { $MANCMD.=" -S $cgiInput{section}"; } # Make everything to capture man's STDERR. Then display # them with our form. # This way we don't have to thing about error messages. # Everything is handled by man command. $MANCMD.=" $cgiInput{topic}"; # Pipes childs' STDERR into a parent's file descriptor. pipe($parent,$child); if (fork()) { # parent context close $child; while (<$parent>) { $error.=$_; } } else { # child context close $parent; open(STDERR,">&=" . fileno($child)); exec($MANCMD); } # Back to the parent. Analyze child's error output. if (!($error eq "")) { $error=~s/\n/\/g; showForm($error); } } ############################################################################### ## ## Main block ## ## Just call above functions in a special order. ## ############################################################################### my %cgiInput=readCGIInput(); if (!($cgiInput{section} eq "")) { doMan(%cgiInput); } else { showForm(); }