- #!/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 <avi@alkalay.net>
- ## 7 Feb 2001
- ## Made in Brazil
- ##
- ###############################################################################
- # Global variables
- $CONFIG="man.cgi.config";
- $MANCMD="/usr/bin/man -C $CONFIG";
- ###############################################################################
- ##
- ## 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)=@_;
- <html>
- <head>
- <title>
- Manpage Viewer
- </title>
- </head>
- <body bgcolor=white>
- <h1>Manpage Viewer</h1>
- 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.
- <br>
- <p>
- <font color=red><b>$msg</b></font>
- <form method=get>
- <table border=0>
- <tr>
- <td align=right>Section:</td>
- <td>
- <select name=section>
- <option value="0">All Sections</option>
- <option value="1">(1) User Commands</option>
- <option value="2">(2) System Calls</option>
- <option value="3">(3) Library Functions</option>
- <option value="4">(4) Special Files</option>
- <option value="5">(5) File Formats</option>
- <option value="6">(6) Games</option>
- <option value="7">(7) Convetions and Miscellany</option>
- <option value="8">(8) Administration and Priviledged Commands</option>
- </select>
- </td>
- </tr>
- <tr>
- <td align=right>Topic:</td>
- <td><input name=topic>
- <input type=submit value="Show Manual">
- </td>
- </tr>
- </table>
- </form>
- <hr>
- <font size=-2>
- <i>man2html wrapper by Avi Alkalay <avi at alkalay.net></i>
- </font>
- </body>
- </html>
- 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];
- }
- }
- ###############################################################################
- ##
- ## 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");
- }
- # 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.
- # parent context
- while (<$parent>) {
- $error.=$_;
- }
- } else {
- # child context
- }
- # Back to the parent. Analyze child's error output.
- if (!($error eq "")) {
- showForm($error);
- }
- }
- ###############################################################################
- ##
- ## Main block
- ##
- ## Just call above functions in a special order.
- ##
- ###############################################################################
- my %cgiInput=readCGIInput();
- if (!($cgiInput{section} eq "")) {
- doMan(%cgiInput);
- } else {
- showForm();
- }
Raw Paste