PERL   24

man cgi

Guest on 19th August 2022 04:29:36 PM

  1. #!/usr/bin/perl
  2.  
  3.  
  4. ###############################################################################
  5. ##
  6. ## man2html CGI wrapper to be used with web-servers.
  7. ##
  8. ## This script prepares an environment to the /usr/bin/man2html program
  9. ## to let it be run under a web-server.
  10. ## It handles the input parameters and passes it to the man commands.
  11. ## Under no parameters, it will generate a simple input form to users.
  12. ##
  13. ## This is free software licensed under the GPL. Use it under your own risk.
  14. ##
  15. ## Avi Alkalay <avi@alkalay.net>
  16. ## 7 Feb 2001
  17. ## Made in Brazil
  18. ##
  19. ###############################################################################
  20.  
  21.  
  22. # Global variables
  23. $CONFIG="man.cgi.config";
  24. $MANCMD="/usr/bin/man -C $CONFIG";
  25.  
  26.  
  27. use CGI qw(:standard);
  28.  
  29.  
  30. ###############################################################################
  31. ##
  32. ## showForm
  33. ##
  34. ## Displays the simple input form.
  35. ## Accepts one argument that will be used as a error message, printed with
  36. ## the form.
  37. ##
  38. ###############################################################################
  39. sub showForm {
  40.         my ($msg)=@_;
  41.  
  42.         print("Content-type: text/html\n\n");
  43.         print STDOUT <<EOF;
  44. <html>
  45.   <head>
  46.     <title>
  47.       Manpage Viewer
  48.     </title>
  49.   </head>
  50.   <body bgcolor=white>
  51.     <h1>Manpage Viewer</h1>
  52. 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.
  53.     <br>
  54.     <p>
  55.     <font color=red><b>$msg</b></font>
  56.     <form method=get>
  57.       <table border=0>
  58.         <tr>
  59.           <td align=right>Section:</td>
  60.           <td>
  61.             <select name=section>
  62.               <option value="0">All Sections</option>
  63.               <option value="1">(1) User Commands</option>
  64.               <option value="2">(2) System Calls</option>
  65.               <option value="3">(3) Library Functions</option>
  66.               <option value="4">(4) Special Files</option>
  67.               <option value="5">(5) File Formats</option>
  68.               <option value="6">(6) Games</option>
  69.               <option value="7">(7) Convetions and Miscellany</option>
  70.               <option value="8">(8) Administration and Priviledged Commands</option>
  71.             </select>
  72.           </td>
  73.         </tr>
  74.         <tr>
  75.           <td align=right>Topic:</td>
  76.           <td><input name=topic>
  77.             <input type=submit value="Show Manual">
  78.           </td>
  79.         </tr>
  80.       </table>
  81.     </form>
  82.     <hr>
  83.     <font size=-2>
  84.       <i>man2html wrapper by Avi Alkalay &lt;avi at alkalay.net&gt;</i>
  85.     </font>
  86.   </body>
  87. </html>
  88. EOF
  89. }
  90.  
  91.  
  92. ###############################################################################
  93. ##
  94. ## readCGIInput
  95. ##
  96. ## Uses de CGI.pm library to get parameters passed via the form or URL.
  97. ## Returns a hash containing the values needed by the program.
  98. ##
  99. ###############################################################################
  100. sub readCGIInput {
  101.         my %input;
  102.  
  103.         $input{section}=param("section");
  104.         $input{topic}=param("topic");
  105.         $input{vhost}=virtual_host();
  106.         $input{path}=script_name();
  107.  
  108.         if ($input{section} eq "") {
  109.                 $input{section}=$ARGV[0];
  110.         }
  111.  
  112.         if ($input{topic} eq "") {
  113.                 $input{topic}=$ARGV[1];
  114.         }
  115.  
  116.         return %input;
  117. }
  118.  
  119.  
  120. ###############################################################################
  121. ##
  122. ## doMan
  123. ##
  124. ## This is the program's most important function.
  125. ## It will analize the parameters and execute the "man" command with special
  126. ## configurations.
  127. ##
  128. ###############################################################################
  129. sub doMan {
  130.         my (%cgiInput)=@_;
  131.         my $child,$parent;
  132.         my $error;
  133.  
  134.         if ($cgiInput{topic} eq "") {
  135.                 showForm("No Topic specified");
  136.                 exit(0);
  137.         }
  138.  
  139.  
  140.         # Prepare the command line with special environment vars to be passed
  141.         # to man2html. Look at man.cgi.config.
  142.         $MANCMD="VHOST=\"$cgiInput{vhost}\" CGIPATH=\"$cgiInput{path}\" " . $MANCMD;
  143.         if ($cgiInput{section} != 0) {
  144.                 $MANCMD.=" -S $cgiInput{section}";
  145.         }
  146.  
  147.  
  148.  
  149.         # Make everything to capture man's STDERR. Then display
  150.         # them with our form.
  151.         # This way we don't have to thing about error messages.
  152.         # Everything is handled by man command.
  153.  
  154.         $MANCMD.=" $cgiInput{topic}";
  155.  
  156.         # Pipes childs' STDERR into a parent's file descriptor.
  157.         pipe($parent,$child);
  158.         if (fork()) {
  159.                 # parent context
  160.                 close $child;
  161.                 while (<$parent>) {
  162.                         $error.=$_;
  163.                 }
  164.         } else {
  165.                 # child context
  166.                 close $parent;
  167.                 open(STDERR,">&=" . fileno($child));
  168.                 exec($MANCMD);
  169.         }
  170.  
  171.         # Back to the parent. Analyze child's error output.
  172.         if (!($error eq "")) {
  173.                 $error=~s/\n/\<br\>/g;
  174.                 showForm($error);
  175.         }
  176. }
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. ###############################################################################
  185. ##
  186. ## Main block
  187. ##
  188. ## Just call above functions in a special order.
  189. ##
  190. ###############################################################################
  191.  
  192. my %cgiInput=readCGIInput();
  193.  
  194. if (!($cgiInput{section} eq "")) {
  195.         doMan(%cgiInput);
  196. } else {
  197.         showForm();
  198. }

Raw Paste


Login or Register to edit or fork this paste. It's free.