Debian Codegraph @

helper/ramify-whatis

raw
1 #! /usr/bin/perl
2 use warnings;
3 use strict;
4 use integer;
5 use FindBin;
6 use lib $FindBin::RealBin;
7 use Def;
8
9 # %ignsct: omit ("ignore") some warnings for these man sections, and do
10 # not output man pages belonging to them
11 our %ignsct = map {$_=>1} qw( 2 3 );
12 our $indent = ' ' x 5;
13 our $file_debram = $Def::debram_txt;
14 our $test = 0; # set to 1 to run on test data only
15
16 our $usage = <<END;
17 usage: $0 {-t debram.txt} [-jsvh] [Contents file]
18 -t debram.txt
19 use alternate data file, which need not necessarily be
20 a full debram.txt (this option must appear first)
21 -j ignore the Contents file's header
22 -s append a "Stray Commands" pseudobranch to the output
23 -v verbose: report script progress, and files/links/whatises/etc
24 the script cannot handle, to stderr (normally recommended)
25 -h print this usage message
26 END
27
28 # This relatively unimportant helper script attempts to marshal the
29 # one-line whatis(1) descriptions by binary package and ramification.
30 #
31 # The script's author admits that he does not much like the script. The
32 # script remains buggy, yet its relative unimportance discourages the
33 # author from spending too much more time improving it. To fix the
34 # script right would require an understanding of mandb(8) internals
35 # which the author lacks sufficient time and incentive to gain. At
36 # least the script *seems* to work approximately well enough for the
37 # purpose intended. (The author regrets writing this script in Perl.
38 # Wrong language, in retrospect. Too late now...)
39 #
40 # If you are developing Debram and updating cmdsel.txt, you are likely
41 # to find the script's output interesting nevertheless. Try it with
42 # the -sv options, capturing stdout in one file and stderr in another,
43 # and see what you get.
44 #
45 # TESTING THE SCRIPT
46 #
47 # Because the Contents file is large and there are many manpage files,
48 # the script takes some time to run (on the order of one minute on the
49 # Intel 845G / Pentium 4 machine [*] the author happens to be using at
50 # the moment). To avoid such delays when experimenting with the script,
51 # you can set $test above to 1, then set up a "test" subdirectory in
52 # your present working directory with the relevant parts of a Debian
53 # filesystem: test/usr/share/man/ and the like. In "test"'s top level,
54 # you must include the special files
55 #
56 # * "whatis", with mock "whatis -w '*'" output;
57 # * "manpath", with mock "manpath" output; and
58 # * "dpkggs", with mock "dpkg --get-selections" output.
59 #
60 # Probably also,
61 #
62 # * "Contents", a mock Contents file (need not be gzipped); and
63 # * "debram.txt", a mock debram.txt (of course).
64 #
65 # In the latter case you would run the script with the -t option,
66 # specifying both the mock debram.txt and the mock Contents on the
67 # command line. Naturally you would give the -v option, too.
68 #
69 # An example of mock manpath output:
70 #
71 # test/usr/local/man:test/usr/share/man:test/usr/X11R6/man
72 #
73 # An example line from a mock Contents file:
74 #
75 # usr/share/man/man1/foo.1.gz admin/mypkg
76 #
77 # [*] The funny thing about mentioning the machine you happen to be
78 # using at the moment is: you know that someone will read the mention
79 # years later and think it quaint. So, if you are running an
80 # Intel 7000G / Pentium 7 at a million GHz by the time you read these
81 # words on your high-definition 3-D immersive hyperspatial display, why,
82 # beam the author an e-mail back in time to 2006 and tell him what it's
83 # like.
84 #
85 #
86
87 # Read command-line arguments and options.
88 while ( @ARGV >= 2 && ( $ARGV[0] eq '-t' ) ) {
89 my( $opt, $file ) = splice @ARGV, 0, 2;
90 if ( $opt eq '-t' ) { $file_debram = $file }
91 }
92 my @opt;
93 my @arg;
94 push @{ /^-\S/ ? \@opt : \@arg }, $_ for @ARGV;
95 my %opt = map {
96 my $o = $_;
97 map { substr( $o, $_, 1 ) => 1 } 1 .. length($o)-1
98 } @opt;
99 if ( @arg < 1 || $opt{'?'} || $opt{h} ) {
100 print $usage;
101 exit 0;
102 }
103 my $f_cont = shift @arg;
104 !defined($f_cont) || @arg and die $usage;
105
106 my $pwd = `pwd`; chomp $pwd;
107 my $pat_pwd = quotemeta $pwd;
108
109 sub readlink1 ($) {
110 local $_ = shift;
111 my $link = `readlink -f $_`; chomp $link;
112 $link =~ s/^$pat_pwd\///
113 or die
114 "$0: test link is from outside the present working directory\n"
115 if $test;
116 return $link;
117 }
118
119 sub vwarn { warn @_ if $opt{v} }
120
121 our $bar = '-' x $Def::width_fmt . "\n";
122 my $w_maint_pri = $Def::w_maint + $Def::w_pri;
123
124 # ----------------------------------------------------------------------
125
126 # Read in.
127
128 my %whatis ; # desc ->> whatis
129 my %whatis2; # sect & cmd ->> whatis
130 vwarn "${bar}reading whatis...\n";
131 {
132 for ( $test ? `cat test/whatis` : `whatis -w '*'` ) {
133 my( $cmd, $sect, $sext, $desc );
134 {
135 my $pat;
136 {
137 my $p1 = qr/(\S+)\s+\((\d)([^()]*)\)/;
138 my $p2 = qr/\s(?:.*?\s)??-\s+/ ;
139 my $p3 = qr/((?:\S(?:.*\S)?)?)/ ;
140 $pat = qr/^${p1}${p2}${p3}\s*$/o;
141 }
142 ( $cmd, $sect, $sext, $desc ) = /$pat/o;
143 }
144 unless ( defined $cmd ) {
145 /\s\((\d)([^()]*)\)\s/ && $ignsct{$1}
146 or vwarn( "cannot parse whatis\n$_\n" );
147 next;
148 }
149 my %whatis1 = (
150 fultxt => $_ ,
151 cmd => $cmd ,
152 sect => $sect,
153 sext => $sext,
154 desc => $desc,
155 );
156 push @{ $whatis {$desc} }, \%whatis1;
157 push @{ $whatis2{$sect}{$cmd} }, \%whatis1;
158 }
159 for my $desc ( keys %whatis ) {
160 my @whatis = sort {
161 $a->{sect} <=> $b->{sect} or
162 $a->{cmd } cmp $b->{cmd } or
163 $a->{sext} cmp $b->{sext} or
164 $a->{desc} cmp $b->{desc}
165 } @{ $whatis{$desc} };
166 $whatis{$desc} = \@whatis;
167 }
168 }
169
170 vwarn "${bar}reading the manpath...\n";
171 my @manpath = grep { chomp; !/\blocal\b/ }
172 split ':', ( $test ? `cat test/manpath` : `manpath` );
173 my $pat_manpath;
174 {
175 my $p0 = join '|', map { quotemeta } @manpath;
176 $pat_manpath = qr/$p0/;
177 }
178
179 my %manpg; # sect & cmd -> actual manpage
180 vwarn "${bar}reading actual manpage filenames...\n";
181 {
182 my @manpg0 = `find @manpath -type f -mindepth 2 -maxdepth 2`;
183 my @manpgl0 = `find @manpath -type l -mindepth 2 -maxdepth 2`;
184 my %kill;
185 for my $ctl (
186 { list => \@manpg0 , islink => '' },
187 { list => \@manpgl0, islink => '1' },
188 ) {
189 for ( @{ $ctl->{list} } ) {
190 chomp;
191 my( $cmd, $sect, $sext );
192 {
193 local $_ = $_;
194 s/\.gz$//;
195 ( $cmd, $sect, $sext ) =
196 /^(?:\S*\/)?([^\/\s]+)\.(\d)([^\/.\s]*?)$/
197 or vwarn(
198 $ctl->{islink}
199 ? "cannot parse actual manpage linkname $_\n"
200 : "cannot parse actual regular manpage filename $_\n"
201 ), next;
202 }
203 my $level = 20 * !length($sext) + 10 * ( !$ctl->{islink} + 1 );
204 my %manpg1 = (
205 path => $_ ,
206 cmd => $cmd ,
207 sect => $sect ,
208 sext => $sext ,
209 level => $level,
210 );
211 if ( $ctl->{islink} ) {
212 my $realf = readlink1 $_;
213 unless ( $realf =~ /\S/ ) {
214 $ignsct{$sect}
215 or vwarn "cannot follow the real symlink "
216 . "$_ to its target\n";
217 next;
218 }
219 $manpg1{realf} = $realf;
220 }
221 my $dont_record = '';
222 if ( my $existing = $manpg{$sect}{$cmd} ) {
223 if ( $manpg1{level} > $existing->{level} )
224 { delete $kill{$sect}{$cmd} }
225 elsif ( $manpg1{level} < $existing->{level} )
226 { $dont_record = 1 }
227 else
228 { $kill{$sect}{$cmd} = 1 }
229 $ignsct{$sect} or vwarn "two $cmd ($sect) manpages exist\n";
230 }
231 $manpg{$sect}{$cmd} = \%manpg1 unless $dont_record;
232 }
233 }
234 for my $sect ( keys %kill ) {
235 delete $manpg{$sect}{$_} for keys %{ $kill{$sect} };
236 }
237 }
238
239 my %inv_mp; # path (file/link, not realfile) -> sect & cmd
240 vwarn "${bar}inverting the manpage hash...\n";
241 for my $sect ( keys %manpg ) {
242 for my $cmd ( keys %{ $manpg{$sect} } ) {
243 my $file = $manpg{$sect}{$cmd}{path};
244 exists $inv_mp{$file}
245 and die
246 "$0: impossible: two actual manpage files with the same path\n";
247 $inv_mp{$file} = {
248 sect => $sect,
249 cmd => $cmd ,
250 };
251 }
252 }
253
254 my %dpkggs; # pkg -> (dummy 1)
255 vwarn "${bar}reading dpkg --get-selections...\n";
256 {
257 for ( $test ? `cat test/dpkggs` : `dpkg --get-selections` ) {
258 my( $pkg ) = /^(\S+)/;
259 if ( defined $pkg ) {
260 $dpkggs{$pkg} = 1;
261 }
262 else {
263 vwarn "do not understand from dpkg --get-selections\n$_\n";
264 }
265 }
266 }
267
268 my %contp; # file (cont) ->> pkg
269 my %contq; # link (cont) ->> pkg
270 my %contf; # pkg ->> file (cont)
271 my %contl; # pkg ->> link (cont)
272 vwarn "${bar}reading the Contents file...\n";
273 {
274 my %realf; # link -> file
275 my $isbody = !$opt{j};
276 CONTENTS: for (
277 $f_cont =~ /\.gz$/ ? `zcat $f_cont` : `cat $f_cont`
278 ) {
279 if ( !$isbody ) {
280 $isbody = '1' if /^FILE\s+LOCATION\s*$/;
281 }
282 else {
283 if ( $test ) { s/^/test\// }
284 else { s/^/\// }
285 /^$pat_manpath\/man\d\//o or next CONTENTS;
286 my( $file, $loc ) = /^(\S(?:.*?\S)??)\s+(\S+?)\s*$/
287 or vwarn( "cannot parse Contents entry\n$_\n" ),
288 next CONTENTS;
289 my @loc = grep { $_ eq '->' or $dpkggs{$_} } map {
290 my $ret;
291 if ( /^.*\/([^\/\s]+)$/ ) { $ret = $1 }
292 elsif ( $_ eq '->' ) { $ret = $_ }
293 elsif ( /[,\s]/ || !/\S/ ) {
294 vwarn "cannot parse Contents location $_\n";
295 next CONTENTS;
296 }
297 else { $ret = $_ }
298 $ret;
299 } split ',', $loc;
300 for my $loc ( @loc ) {
301 if ( $loc eq '->' ) {
302 if ( -l $file ) {
303 my $realf = readlink1 $file;
304 if ( $realf =~ /\S/ ) { $realf{$file} = $realf }
305 else {
306 vwarn "cannot follow the symlink from $file\n";
307 }
308 }
309 }
310 else {
311 $contp{$file}{$loc} = 1;
312 $contf{$loc}{$file} = 1;
313 }
314 }
315 }
316 }
317 my %realf_loc; # link ->> pkg
318 for my $link ( keys %realf ) {
319 my $realf = $realf{$link};
320 if ( $contp{$realf} ) {
321 $realf_loc{$link}{$_} = 1 for keys %{ $contp{$realf} };
322 }
323 else {
324 vwarn "can find no package for symlink $link\n";
325 }
326 }
327 for my $link ( keys %realf_loc ) {
328 my $file = $realf{$link};
329 my @loc = keys %{ $realf_loc{$link} };
330 for ( @loc ) {
331 $contp{$file}{$_} = 1;
332 $contf{$_}{$file} = 1;
333 $contq{$link}{$_} = 1;
334 $contl{$_}{$link} = 1;
335 }
336 }
337 }
338
339 my %contpq; # file/link (cont) ->> pkg
340 my %contfl; # pkg ->> file/link (cont)
341 vwarn
342 "${bar}in Contents, "
343 . "combining the symlinks with the real files...\n";
344 for my $hash ( \%contp , \%contq ) {
345 for my $file ( keys %$hash ) {
346 for my $pkg ( keys %{ $hash->{$file} } ) {
347 $contpq{$file}{$pkg} = $hash->{$file}{$pkg};
348 }
349 }
350 }
351 for my $hash ( \%contf , \%contl ) {
352 for my $pkg ( keys %$hash ) {
353 for my $file ( keys %{ $hash->{$pkg } } ) {
354 $contfl{$pkg}{$file} = $hash->{$pkg}{$file};
355 }
356 }
357 }
358
359 vwarn "${bar}have done reading\n";
360
361 # ----------------------------------------------------------------------
362
363 # Process.
364
365 # Give the manpage files each their several whatis descriptions.
366 vwarn "${bar}assigning the manpage files each their "
367 . "several whatis descriptions...\n";
368 for my $sect ( keys %manpg ) {
369 $manpg{$sect}{$_}{desc} = [] for keys %{ $manpg{$sect} };
370 }
371 for my $desc ( keys %whatis ) {
372 my $whatis = $whatis{$desc};
373 for my $whatis1 ( @$whatis ) {
374 my $sect = $whatis1->{sect};
375 my $cmd = $whatis1->{cmd };
376 $manpg{$sect}{$cmd} or $ignsct{$sect} or vwarn
377 "cannot associate with a manpage file the whatis\n"
378 . "$whatis1->{fultxt}\n";
379 $manpg{$sect}{$cmd} and push @{ $manpg{$sect}{$cmd}{desc} }, $desc;
380 }
381 }
382 for my $sect ( keys %manpg ) {
383 for my $cmd ( keys %{ $manpg{$sect} } ) {
384 my @desc = sort @{ $manpg{$sect}{$cmd}{desc} };
385 $manpg{$sect}{$cmd}{desc} = \@desc;
386 }
387 }
388
389 # Object if any actual manpage file/link is not in the Contents.
390 $contpq{$_}
391 or vwarn "actual manpage file/link $_ is not in the Contents\n"
392 for keys %inv_mp;
393
394 # Subroutine: given a binary package name, return all the corresponding
395 # actual manpage files/links and descriptions (except those in ignored
396 # man sections).
397 sub files_descs ($) {
398 my( $pkg ) = @_;
399 my @file0 = grep { $inv_mp{$_} } sort keys %{ $contfl{$pkg} };
400 my @file = grep { !$ignsct{ $_->{sect} } } map {
401 my $sect = $inv_mp{$_}{sect};
402 my $cmd = $inv_mp{$_}{cmd };
403 $manpg{$sect}{$cmd}{found} = 1;
404 {
405 path => $_ ,
406 sect => $sect,
407 cmd => $cmd ,
408 desc => $manpg{$sect}{$cmd}{desc},
409 };
410 } @file0;
411 return \@file;
412 }
413
414 # Subroutine: given a binary package name, return all the corresponding
415 # whatis fulltexts.
416 sub fultxts ($) {
417 my( $pkg ) = @_;
418 my @fultxt;
419 for my $fd ( @{ files_descs $pkg } ) {
420 for my $desc ( @{ $fd->{desc} } ) {
421 for ( @{ $whatis{$desc} } ) {
422 push @fultxt, $_->{fultxt};
423 $_->{found} = 1;
424 }
425 }
426 }
427 return @fultxt;
428 }
429
430 # ----------------------------------------------------------------------
431
432 # Write out.
433
434 open T, '<', $file_debram;
435
436 my $is_full_debram_txt;
437
438 # Subroutines: get and unget debram.txt lines.
439 my @ungotline;
440 sub getline ( ) { @ungotline ? shift(@ungotline) : scalar(<T>) }
441 sub ungetline (@) { unshift @ungotline, @_ }
442 sub geteof ( ) { !@ungotline && eof(T) }
443
444 {
445
446 # Determine if the debram.txt seems a complete one or only a body.
447 # If a complete one, then discard the topmatter.
448 {
449 my @line = ( getline(), getline() );
450 if (
451 $line[0] eq '-' x $Def::width . "\n" &&
452 $line[1] =~ /^\d{${Def::ndig}} \S/
453 ) {
454 $is_full_debram_txt = '' ;
455 ungetline @line;
456 }
457 else {
458 $is_full_debram_txt = '1';
459 1 until getline() eq $Def::mark_main_body;
460 getline();
461 }
462 }
463
464 my $state = 1;
465 # $state == 0 means that an ordinary debram package entry is expected
466 # next. Other states indicate that the script is in the midst of
467 # reading a ram header.
468 my $printsave;
469 while ( !geteof() ) {
470 local $_ = getline();
471 if ( $state == 0 ) {
472 if ( !/\S/ ) {
473 print;
474 ++$state;
475 }
476 else {
477 # Here is an ordinary debram package entry.
478 my( $pkg ) = /^.{$w_maint_pri}(\S+)/
479 or die "$0: cannot parse debram.txt package entry\n$_\n";
480 next unless $dpkggs{$pkg};
481 print;
482 {
483 my %already;
484 print map { s/^/${indent}/; $_; } grep {
485 my $ret = $already{$_} ? '' : '1';
486 $already{$_} = 1;
487 $ret;
488 } fultxts($pkg);
489 }
490 print "\n";
491 }
492 }
493 elsif ( $state == 1 ) {
494 $printsave = $_;
495 ++$state;
496 }
497 elsif ( $state == 2 ) {
498 last unless /^\d{${Def::ndig}} \S/;
499 print $printsave, $_;
500 ++$state;
501 }
502 elsif ( $state == 3 ) {
503 print;
504 $state = 0;
505 }
506 else { die "$0: impossible" }
507 }
508
509 }
510
511 close T;
512
513 # ----------------------------------------------------------------------
514
515 # Warn of and/or report on strays.
516
517 vwarn "${bar}Finding strays...\n";
518 {
519 if ( $opt{s} ) {
520 my $wbar = '-' x $Def::width . "\n";
521 print "${wbar}STRAY COMMANDS\n${wbar}";
522 }
523 for my $desc ( keys %whatis ) {
524 for ( @{ $whatis{$desc} } ) {
525 unless ( $ignsct{ $_->{sect} } || $_->{found} ) {
526 vwarn "stray whatis entry\n$_->{fultxt}\n";
527 print "${indent}$_->{fultxt}";
528 }
529 }
530 }
531 for my $sect ( sort keys %manpg ) {
532 for my $cmd ( sort keys %{ $manpg{$sect} } ) {
533 my $manpg1 = $manpg{$sect}{$cmd};
534 my $sect = $manpg1->{sect};
535 my $cmd = $manpg1->{cmd };
536 my $path = $manpg1->{path};
537 unless ( $ignsct{$sect} || $manpg1->{found} ) {
538 vwarn "stray actual manpage file/link $path\n";
539 # (The following does not seem to work, so it is commented out.)
540 # if ( $opt{s} ) {
541 # my $found = '';
542 # $_->{found} and $found = '1' for @{ $whatis2{$cmd}{$sect} };
543 # print "${indent}$cmd ($sect)\n" unless $found;
544 # }
545 }
546 }
547 }
548 if ( $opt{s} ) {
549 print "\n\n";
550 }
551 }
552