aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/extras/rman/contrib/youki.pl
blob: 8af5865d6b1a63a11bc444589872c587caf76dde (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
#!/usr/local/bin/perl5
#
# CGI script for translating manpage into html on the fly.
# Front-end for PolyglotMan (formerly called RosettaMan)
#
# Author: Youki Kadobayashi  <youki@center.osaka-u.ac.jp>
#
# NOTE: Replace 'CGI::Apache' with just 'CGI' for systems without mod_perl.
#
# ALSO: You may want to recompile rman like this:
#	MANREFPRINTF = "/mod-bin/cgi-rman.pl?keyword=%s&section=%s"
#

use CGI::Apache font;			# for people with mod_perl and apache
## use CGI font;			# for people without mod_perl
$par = "<P>\n";
$brk = "<BR>\n";
$bg = '#c0ffff';

$query = new CGI::Apache;		# for people with mod_perl and apache
## $query = new CGI;			# for people without mod_perl

%mandatory = ('keyword' => 'Name or keyword',
	      'section' => 'Manual page section');

@given = $query->param;
if ($#given < 0) {
	&request_page;
	exit 0;
}

foreach $field (keys %mandatory) {
	if ($query->param($field) eq '') {
		push(@missing, $mandatory{$field});
	}
}
if ($#missing >= 0) {
    &info_missing_page(@missing);
} else {
    if ($query->param('type') eq 'apropos') {
	&apropos_page;
    } else {
	&manual_page;
    }
}
exit 0;

sub standout {
    my ($level, $color, $string) = @_;

    # As per CGI.pm documentation "Generating new HTML tags"
    return $query->font({color => "$color"}, "<$level>$string</$level>");
}

sub error_page {
    my ($message) = @_;

#    print $query->header,
#		$query->start_html(-title=>$message, -BGCOLOR=>$bg);

    print &standout("H2", "brown", $message),
		"The above error occured during the manual page generation",
		" process.  Please check keyword and section number,",
		" then try again.", $par;

#    print $query->end_html;
}

sub info_missing_page {
    my (@missing) = @_;

    print $query->header,
		$query->start_html(-title=>"Information is missing",
				   -BGCOLOR=>$bg);

    print &standout("H2", "brown", "Information is missing"),
		"Sorry but your request was not fulfilled because",
		" the following information is missing in your entry:", $par,
		join(' ', @missing), $par,
		"Please go back and make sure to enter data on the missing field.";

    print $query->end_html;
}

sub request_page {
    print $query->header,
		$query->start_html(-title=>'Hypertext Manual Page',
				   -author=>'Youki Kadobayashi',
				   -BGCOLOR=>$bg);

    print &standout("H2", "green", "Hypertext Manual Page");

    print $query->start_form,
	"Type of Search: ",
		$query->radio_group(-name=>'type',
				    -values=>['man', 'apropos'],
				    -default=>'man'), $brk,
	"Name or Keyword: ",
		$query->textfield(-name=>'keyword'), $brk,
	"Manpage Section: ",
		$query->popup_menu(-name=>'section',
				   -labels=>{
				       0 => 'All Sections',
				       1 => '1 - General Commands',
				       2 => '2 - System Calls',
				       3 => '3 - Library Functions',
				       4 => '4 - Device Special Files',
				       5 => '5 - File Formats',
				       6 => '6 - Games',
				       7 => '7 - Macros and Conventions',
				       8 => '8 - System Administration',
				       'pgsql' => 'PostgreSQL',
				       'tcl' => 'Tcl/Tk',
				       'mh' => 'Message Handler',
				       'isode' => 'ISODE',
				       'X11' => 'X Window System'},
				   -values=>[0, 1, 2, 3, 4, 5, 6, 7, 8,
					     'pgsql', 'tcl', 'mh',
					     'isode', 'X11'],
				   -default=>0), $brk;

    print $query->submit(-name=>'Submit'), $query->reset,
		$query->end_form, $par;

    print $query->end_html;
}

sub manual_page {
    my $keyword = $query->param('keyword');
    my $section = $query->param('section');
    my $man = "man";
    my $ok = "-a-zA-Z0-9._";

    # sanitize for security
    $section =~ s/[^$ok]+/_/go;
    $keyword =~ s/[^$ok]+/_/go;

    if ($section =~ /^[A-Za-z]/) {
	$man .= " -M$section";
    }
    elsif ($section =~ /^\d/) {
	$section =~ s/^(\d)\w*/\1/go;
	$man .= " -s $section";
    }
    $man .= ' ' . $keyword;
    open (MAN, "$man | /usr/local/bin/rman -f html -n $keyword -s $section |");

    print $query->header;
	# start_html and end_html not needed here, since rman tacks them.
    print "<!-- text generated with '$man' by cgi-rman.pl -->\n";
    while (<MAN>) {
	print $_;
    }
    if ($? != 0 || $. < 15) {
	&error_page("Your request for manual page '$keyword' failed.");
#	print "return code $?  line $.\n";
	close(MAN);
	return;
    }
    close(MAN);
}

sub apropos_page {
    my $keyword = $query->param('keyword');
    my $section = $query->param('section');	# igored
    my $man = "man -k";
    my $matches = 0;

    $man .= ' ' . $keyword;
    open (MAN, "$man |");

    $url = $query->url;
    print $query->header, $query->start_html(-title=>$man);

    while (<MAN>) {
	if (/^([a-zA-Z0-9_.-]+)[, ][a-zA-Z0-9_., -]*\((\d)\w*\)/) {
	    print $`, qq{ <A HREF="$url?keyword=$1&section=$2&type=man"> },
		$&, "</A>", $';
	    ++$matches;
	} else {
	    print $_;
	}
	print $brk;
    }
    if ($? != 0 || $matches == 0) {
	&error_page("Your search request with keyword '$keyword' failed.");
#	print "return code $?  matches $matches\n";
	close(MAN);
	return;
    }
    close(MAN);

    print $query->end_html;
}