summaryrefslogtreecommitdiff
path: root/doc/man2html.pl
blob: 1a94fc82a5b44b3cc6e741105973214efbd8564f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/perl

# TODO: We need to make this more resilient
# currently expects args without enforcing

$FIL = $NAM = $SEC = @ARGV[0];

$NAM =~ s/^([^.]+)\..+$/$1/;
$SEC =~ s/^.+\.([^.]+)$/$1/;

$command =     "groff";
@args =        split(" ", "-Tascii -mdoc $FIL");

$enable_include_links = 0;

man($NAM, $SEC);

sub man {
    local($name, $section) = @_;
    local($_, $title, $head, *MAN);
    local($html_name, $html_section, $prefix);
    local(@manargs);
    local($query) = $name;

    # $section =~ s/^([0-9ln]).*$/$1/;
    $section =~ tr/A-Z/a-z/;

    $prefix = "Man ";
    if ($alttitle) {
        $prefix = "";
        $title = &encode_title($alttitle);
        $head = &encode_data($alttitle);
    } elsif ($section) {
        $title = &encode_title("${name}($section)");
        $head = &encode_data("${name}($section)");
    } else {
        $title = &encode_title("${name}");
        $head = &encode_data("${name}");
    }

    print &html_header("$title");
    print "<H1>Man Page: ${title}</H1>";
    print "<PRE>\n";

    $html_name = &encode_data($name);
    $html_section = &encode_data($section);

    #print Dumper($sectionpath);
    #print "yy $section yy $manpath\n";
    if ($name =~ /^\s*$/) {
        print "Empty input, no man page given.\n";
        return;
    }

    if (index($name, '*') != -1) {
        print "Invalid character input '*': $name\n";
        return;
    }

    if ($section !~ /^[0-9ln]\w*$/ && $section ne '') {
        print "Sorry, section `$section' is not valid\n";
        return;
    }

    if (!$section) {
        if ($sectionpath->{$manpath}) {
            $section = "-S " . $sectionpath->{$manpath}{'path'};
        } else {
            $section =  '';
        }
    } else {
        if ($sectionpath->{$manpath}{$section}) {
            $section = "-S " . $sectionpath->{$manpath}{$section};
        } else {
            $section = "-S $section";
        }
    }

    # print "X $command{'man'} @manargs -- x $name x\n";
    &proc(*MAN, $command, @args) ||
        &mydie ("$0: open of $command{'man'} command failed: $!\n");
    if (eof(MAN)) {
        # print "X $command{'man'} @manargs -- x $name x\n";
        print "Sorry, no data found for `$html_name" .
                ($html_section ? "($html_section)": '') . "'.\n";
        return;
    }

    local($space) = 1;
    local(@sect);
    local($i, $j);
    while(<MAN>) {
        # remove tailing white space
        if (/^\s+$/) {
            next if $space;
            $space = 1;
        } else {
            $space = 0;
        }

        $_ = &encode_data($_);
        if($enable_include_links &&
           m,(<B>)?\#include(</B>)?\s+(<B>)?\&lt\;(.*\.h)\&gt\;(</B>)?,) {
            $match = $4; ($regexp = $match) =~ s/\./\\\./;
            s,$regexp,\<A HREF=\"$BASE/usr/include/$match\"\>$match\</A\>,;
        }
        /^\s/ &&                         # skip headers
            s,((<[IB]>)?[\w\_\.\-]+\s*(</[IB]>)?\s*\(([1-9ln][a-zA-Z]*)\)),&mlnk($1),oige;

        # detect E-Mail Addreses in manpages
        if (/\@/) {
            s/([a-z0-9_\-\.]+\@[a-z0-9\-\.]+\.[a-z]+)/<A HREF="mailto:$1">$1<\/A>/gi;
        }

        # detect URLs in manpages
        if (m%tp://%) {
            s,((ftp|http)://[^\s<>\)]+),<A HREF="$1">$1</A>,gi;
        }

        if (/^<B>\S+/ && m%^<B>([^<]+)%) {
            $i = $1; $j = &encode_url($i);
            s%^<B>([^<]+)</B>%<B>$i</B>%;
            push(@sect, $1);
        }
        print;
    }
    close(MAN);

    print "</PRE>\n";
    print "<H6>&nbsp;&nbsp;&nbsp;[ <a href='./'>back</a> | <a href='../../'>home</a> ]</h6>";
    print "</BODY>\n";
    print "</HTML>\n";

    # Sleep 0.35 seconds to avoid DoS attacs
    select undef, undef, undef, 0.35;
}

# encode unknown data for use in <TITLE>...</TITILE>
sub encode_title {
    # like encode_url but less strict (I couldn't find docs on this)
    local($_) = @_;
    s/([\000-\031\%\&\<\>\177-\377])/sprintf('%%%02x',ord($1))/eg;
    $_;
}

# encode unknown data for use in a URL <A HREF="...">
sub encode_url {
    local($_) = @_;
    # rfc1738 says that ";"|"/"|"?"|":"|"@"|"&"|"=" may be reserved.
    # And % is the escape character so we escape it along with
    # single-quote('), double-quote("), grave accent(`), less than(<),
    # greater than(>), and non-US-ASCII characters (binary data),
    # and white space.  Whew.
    s/([\000-\032\;\/\?\:\@\&\=\%\'\"\`\<\>\177-\377 ])/sprintf('%%%02x',ord($1))/eg;
    s/%20/+/g;
    $_;
}
# encode unknown data for use inside markup attributes <MARKUP ATTR="...">
sub encode_attribute {
    # rfc1738 says to use entity references here
    local($_) = @_;
    s/([\000-\031\"\'\`\%\&\<\>\177-\377])/sprintf('\&#%03d;',ord($1))/eg;
    $_;
}
# encode unknown text data for using as HTML,
# treats ^H as overstrike ala nroff.
sub encode_data {
    local($_) = @_;
    local($str);

    # Escape &, < and >
    s,\010[><&],,g;
    s/\&/\&amp\;/g;
    s/\</\&lt\;/g;
    s/\>/\&gt\;/g;

    s,((_\010.)+),($str = $1) =~ s/.\010//g; "<I>$str</I>";,ge;
    s,(.\010)+,$1,g;

    if (!s,((.\010.)+\s+(.\010.)+),($str = $1) =~ s/.\010//g; "<B>$str</B>";,ge) {
        s,((.\010.)+),($str = $1) =~ s/.\010//g; "<B>$str</B>";,ge;
    }

    s,.\010,,g;

    $_;
}

sub html_header {
    return qq{<HTML>
<HEAD>
<TITLE>$_[0]</TITLE>
<link rev="made" href="mailto:wosch\@FreeBSD.ORG">
<META name="robots" content="nofollow">
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
<link rel="stylesheet" type="text/css" href="/swalter/style.css">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n\n};
}

sub mlnk {
    local($matched) = @_;
    return qq{<U>$matched</U>};
}

sub proc {
    local(*FH, $prog, @args) = @_;
    local($pid) = open(FH, "-|");
    return undef unless defined($pid);
    if ($pid == 0) {
    exec $prog, @args;
    &mydie("exec $prog failed\n");
    }
    1;
}

# CGI script must die with error status 0
sub mydie {
	local($message) = @_;
	print &html_header("Error");
	print $message;

print qq{
<p>
<A HREF="$BASE">Index Page and Help</A>
</BODY>
</HTML>
};

	exit(0);
}