Handle hostnames with upper-case letters
[webmin.git] / man / view_man.cgi
1 #!/usr/local/bin/perl
2 # view_man.cgi
3 # Display a single manual page
4
5 require './man-lib.pl';
6 &ReadParse();
7 &ui_print_header(undef, $text{'man_title'}, "");
8
9 if (&has_command($config{'man2html_path'})) {
10         $ocmd = $in{'sec'} ? $config{'list_cmd_sect'}
11                            : $config{'list_cmd'};
12         }
13 else {
14         $ocmd = $in{'sec'} ? $config{'man_cmd_sect'}
15                            : $config{'man_cmd'};
16         }
17 if ($config{'strip_letters'}) {
18         $in{'sec'} =~ s/^(\d+).*$/$1/;
19         }
20 &set_manpath($in{'opts'});
21
22 # Try various man commands, like :
23 # man -s 3x Foo, man -s 3x foo, man -s 3 Foo, man -s 3 foo
24 @sects = ( $in{'sec'} );
25 if ($in{'sec'} =~ /^(\d+)[^0-9]+$/) {
26         push(@sects, $1);
27         }
28 SECT: foreach $sec (@sects) {
29         foreach $page ($in{'page'}, lc($in{'page'})) {
30                 $qpage = quotemeta($page);
31                 $qsec = quotemeta($sec);
32                 $cmd = $ocmd;
33                 $cmd =~ s/PAGE/$qpage/;
34                 $cmd =~ s/SECTION/$qsec/;
35                 $out = &backquote_command("$cmd 2>&1", 1);
36                 if ($out !~ /^.*no manual entry/i && $out !~ /^.*no entry/i &&
37                     $out !~ /^.*nothing appropriate/i) {
38                         # Found it
39                         $found++;
40                         last SECT;
41                         }
42                 }
43         }
44 if (!$found) {
45         print "<p><b>",&text('man_noentry', "<tt>$in{'page'}</tt>"),
46               "</b><p>\n";
47         }
48 else {
49         if (&has_command($config{'man2html_path'})) {
50                 # Last line only
51                 @lines = split(/\r?\n/, $out);
52                 $out = $lines[$#lines];
53                 if ($out =~ /\(<--\s+(.*)\)/) {
54                         # Output has cached file and original path
55                         $out = $1;
56                         }
57                 $out =~ s/ .*//;
58                 if( $out =~ /^.*\.gz/i ) {
59                         $cmd = "gunzip -c";
60                         }
61                 elsif ($out =~ /^.*\.(bz2|bz)/i) {
62                         $cmd = "bunzip2 -c";
63                         }
64                 else {
65                         $cmd = "cat";
66                         }
67                 $qout = quotemeta($out);
68                 $manout = &backquote_command("$config{'man2html_path'} -v 2>&1", 1);
69                 if ($manout =~ /Version:\s+([0-9\.]+)/i && $1 >= 3) {
70                         # New version uses a different syntax!
71                         $cmd .= " $qout | nroff -mman | $config{'man2html_path'} --cgiurl \"view_man.cgi?page=\\\${title}&sec=\\\${section}&opts=$in{'opts'}\" --bare";
72                         $out = &backquote_command("$cmd 2>&1", 1);
73                         }
74                 else {
75                         # Old version of man2html
76                         $cmd .= " $qout | $config{'man2html_path'} -H \"\" -M \"view_man.cgi\"";
77                         $out = &backquote_command("$cmd 2>&1", 1);
78                         $out =~ s/^.*Content-type:.*\n//i;
79                         $out =~ s/http:\/\///ig;
80                         $out =~ s/\?/\?sec=/ig;
81                         $out =~ s/\+/&opts=$in{'opts'}&page=/ig;
82                         $out =~ s/<HTML>.*<BODY>//isg;
83                         $out =~ s/<\/HTML>//ig;
84                         $out =~ s/<\/BODY>//ig;
85                         $out =~ s/<A HREF="file:[^"]+">([^<]+)<\/a>/$1/ig;
86                         $out =~ s/<A HREF="view_man.cgi">/<A HREF=\"\">/i;
87                         }
88                 &show_view_table(&text('man_header', $in{'page'}, $in{'sec'}),
89                                  $out);
90         } else {
91                 $out =~ s/.\010//g;
92                 $out =~ s/^(man:\s*)?(re)?formatting.*//i;
93                 &show_view_table(&text('man_header', $in{'page'}, $in{'sec'}),
94                                  "<pre>".&html_escape($out)."</pre>");
95                 }
96         }
97
98 &ui_print_footer("", $text{'index_return'});
99