File: bulkman.pl

package info (click to toggle)
devhelp 43.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,144 kB
  • sloc: ansic: 9,775; perl: 216; javascript: 152; sh: 100; xml: 66; python: 55; lisp: 29; makefile: 13
file content (305 lines) | stat: -rw-r--r-- 8,039 bytes parent folder | download | duplicates (7)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/perl

#
# * Copyright (C) 2004 Marc Britten <mbritten@monochromatic.net>
# *
# * This program is free software; you can redistribute it and/or
# * modify it under the terms of the GNU General Public License as
# * published by the Free Software Foundation; either version 2 of the
# * License, or (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# * General Public License for more details.
# *
# * You should have received a copy of the GNU General Public
# * License along with this program; if not, see <http://www.gnu.org/licenses/>.
#

# We should be fully functional with search and all that
# if using -S its useful to use -t to make a special book for a specific API
# probably want to use -f at that point too.

use Getopt::Std;

$var = @ARGV;

# depending on distro you may have to change this
$cgi = "lynxcgi:/usr/lib/cgi-bin/man/man2html";

if($var < 1) {
  print "WARNING: this script deletes all html files found in current directory!!!\n\n";
  print "Please specify man section to convert and option flags.\n";
  print "Valid flags are:\n";
  print "-f name of .devhelp file.\n";
  print "-t title of book.\n";
  print "-S search string for manpages to include. Uses grep - very CPU intensive.\n";
  print "-s man section (manditory).\n";
  print "\n To make a specific API book use all 4 options.\n";
  print "bulkman.pl -f SDL.devbook -t \"SDL API Reference\" -S \"SDL API Reference\" -s 3\n\n";
  die " bulkman.pl <opt> -s section \n";
}

getopt("f:t:s=iS:");

$section = $opt_s;

if(!$section) {
  die "Please specify a section with -s number \n";
}

$HTML = "rman -f HTML -S -r '../man$section/%s.%s.html'";

if($opt_f ne "") {
  $xmlfile = $opt_f;
  ($name) = split(/\./, $opt_f);
  $index = $name . ".html";
}
else {
  $xmlfile = "man$section.devhelp";
  $index = "man$section.html";
}

print "HTML index page is being called: $index\n";

print "Cleaning up $xmlfile\n";
unlink($xmlfile);

print "Cleaning html files\n";
unlink(<*.html>);

print "Finding MANPATH's...\n";
open(MANHANDLE, "/etc/man.config") || open(MANHANDLE, "/etc/manpath.config");
while(<MANHANDLE>) {
  if(/\b^MANPATH\b/ || /\b^MANDATORY_MANPATH\b/) {
	chop($_);
	($junk, @path) = split(/\s/, $_);
	foreach $thing (@path) {
	  #something wicked in debian makes me haveto do this
	  if($thing ne "") {
		push(@dirs, $thing);
	  }
	}
  }
}

close(MANHANDLE);

if($opt_t) {
  $title = $opt_t;
}
else {
  $title = "man$section";
}

open(INDEX, ">$index");
open(XMLHANDLE, ">$xmlfile");
print XMLHANDLE "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
print XMLHANDLE "<book name=\"man$N\" version=\"0.1\" title=\"$title\" link=\"$index\">\n";
print XMLHANDLE "<chapters>\n";

print INDEX "<HTML>\n<HEAD>\n<TITLE>$title</TITLE>\n</HEAD>\n<BODY>\n<H2>$title</H2><P><UL>\n";

print "Creating XML file and HTML conversions.\n";

if($opt_S) {
  print "Using search string: $opt_S\n";
}
# Open each dir in turn and do our thing on em
# The gziped non-gziped dual support made this a little clunky.
# so did the man2html's inability to use the .so support in manfiles to link
foreach $dir (@dirs) {
  $mandir = "$dir//man$section";
  if(opendir(DIR, "$mandir")) {
	foreach $file (sort readdir(DIR)) {
	  #	  print "Test: $file\n";
	  $fullfile = $mandir . "//" . $file;
	  if( -f $fullfile) {
		if($opt_S) {
		  if($fullfile =~ /\.gz$/) {
			$syscmd = "gunzip -c $fullfile | grep -q \"$opt_S\"";
		  } else {
			$syscmd = "grep -q \"$opt_S\" $fullfile";
		  }
		  #system returns are backwards so success == failed == next
		  if((system "$syscmd")) {	
			#			print "failed 1st: $file\n";
			# OK the file failed but that doesn't mean it's not a real match.
			# reopen the file and see if it uses .so linking to another manpage.
			if($fullfile =~ /.gz$/) {
			  $syscmd = "gunzip -c $fullfile | grep -q \"\.so\sman.\"";
			} else {
			  $syscmd = "grep -q \"\.so\sman.\" $fullfile";
			}
			if(!(system "$syscmd")) {
			  print "good .so: $file\n";
			  if($fullfile =~ /.gz$/) {
				open(BADFILE, "gunzip -c $fullfile|");
			  } else {
				open(BADFILE, "$fullfile");
			  }
			  @dat = <BADFILE>;
			  ($junk, $data) = split(/\s/, $dat[0]);
			  if(-f "$dir/$data") {
				$syscmd = "grep -q \"$opt_S\" $dir/$data";
			  } elsif(-f "$dir/$data.gz") {
				$syscmd = "gunzip -c $dir/$data | grep -q \"$opt_S\" $dir/$data";
			  }
			  else {
				print "Cannot locate file $data\n";
				$syscmd = "/bin/false";
			  }
			  if((system "$syscmd")) {
				# failed again, skip the damn thing
				next;
			  }
			  else {
				#				print "good test: $syscmd\n";
			  }
			  close(BADFILE);
			}
			else {
			  next;
			}
		  }
		}

		#		print "Good: $file\n";
		if($fullfile =~ /.gz$/) {
			$file =~ s/.gz//g;
			(!system "gunzip -c $fullfile | $HTML > $file.html 2>/dev/null");
		} else {
			(!system "$HTML -n $file $fullfile > $file.html 2>/dev/null");
		}
		($page) = split(/\./, $file);
		print XMLHANDLE "<sub name=\"$page\" link=\"$file.html\">\n";
		print XMLHANDLE "</sub>\n";
		print INDEX "<LI><A HREF=\"$file.html\">$page</A><br>";
	  }
	}
	closedir(DIR);
  }
}

print XMLHANDLE "</chapters>\n";
print XMLHANDLE "<functions>\n";

print INDEX "</UL></BODY></HTML>";
close(INDEX);

print "Fixing up XML search info.\n";

# Now we do it again to setup functions
foreach $dir (@dirs) {
  $mandir = "$dir//man$section";
  if(opendir(DIR, "$mandir")) {
	foreach $file (sort readdir(DIR)) {
	  $fullfile = $mandir . "//" . $file;
	  if( -f $fullfile) {
		($page) = split(/\./, $file);
		print XMLHANDLE "<function name=\"$page\" link=\"$file.html\"/>\n"
	  }
	}
	closedir(DIR);
  }
}

print XMLHANDLE "</functions>\n";
print XMLHANDLE "</book>\n";

close(XMLHANDLE);

# Now its time to cleanup some of the mess.  Some of the manfiles use an
# internal map system to use an already existing man file
# man2html hates that, so we figure out which ones caused an error
# and use symlinks to simulate an existing file.

print "Fixing invalid conversions with links.\n";

open(GREP, "grep -l \"Invalid Manpage\" *.html|");
@bad = <GREP>;
close(GREP);

foreach $file (@bad) {
  foreach $dir (@dirs) {
	$mandir = "$dir//man$section";

	($page) = split(/\./, $file);
	$manfile = $page . "." . $section . ".gz";
	$fullfile = $mandir . "//" . $manfile;

	if(open(BADFILE, "gunzip -c $fullfile|")) {
	  @dat = <BADFILE>;
	  ($junk, $data) = split(/\s/, $dat[0]);
	  ($junk, $data) = split(/\//, $data);
	  $link = $data . ".gz.html";

	  chop($file);
	  unlink($file);
	  symlink($link, $file);

	  close(BADFILE);
	  last;
	}
  }
}

#die "testing no fixing\n";
# Yet another loop through.  This time we need to fixup man2html BS
print "Fixing man2html formatting.\n";

open(PWD, "pwd|");
($mydir) = <PWD>;
chop($mydir);
close(PWD);

opendir(MYDIR, $mydir) || die "can't open $mydir.";
foreach $file (sort readdir(MYDIR)) {
  if(-f $file) {
	($page) = split(/\./, $file);
	# Inplace editing trick from Perl Cookbook
	open(FILE, "+<$file");
	@data = <FILE>;
	#	print $file . "\n";
	foreach $line (@data) {
	  $_ = $line; # for regex only, need to mod $line to get file to change
	  $line =~ s/Content-type: text\/html//g;

	  while($line =~ /$cgi/g) {
		$line =~ s/\"$cgi\"/\"$index\"/g;
		$line =~ s/Return to Main Contents/Documentation Index/g;
		$line =~ s/<A HREF=\"#index\">Index<\/A>//g;

		m{<B><A.*>(.*)</[aA]}sx;
		# $1 should now be our manpage
		$link = $1;
		
		m{\((\d)\).}sx;
		# $1 should now be our man section
		$sec = $1;
		if($sec =~ /\D/) {
		  $sec = $opt_s;
		  if(/(3G)/) {
			$sec = $sec . "x";
		  }
		}
		if($link ne "") {
		  	  #print $file . ": " . $link . ": " . $sec . "\n";
		  $line =~ s/$cgi....//g;
			  #print $line;
		  $line =~ s/\"$link\"/\"$link.$sec.gz.html\"/g;
			  #print $line;
		}
		else {
		  last;
		}
	  }
	}
	seek(FILE, 0, 0);
	print FILE @data;
	truncate(FILE,tell(FILE));
	close(FILE);
  }
}
closedir(DIR);