Debian Codegraph @

helper/find-cmdsel-dupes

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 use Cmdsel;
9
10 # This helper script reports repeated entries in cmdsel.txt, whether the
11 # repetition is in the command's name and section or in its description.
12 # If the user names no cmdsel.txt file, the script examines the
13 # main debram.txt file in the expected place relative to the script.
14 #
15 # The script warns you if it notices a command out of its proper
16 # ram/man section. It warns you and dies if a line is too long or is
17 # badly formatted.
18 #
19 # In addition to reporting repeats, the script also returns counts of
20 # command lines and commands (the difference being that a few command
21 # lines contain two or more commands, separated by commas) and branches.
22
23 our $n_dig_stat = 5;
24 our $usage = <<END;
25 usage: $0 [-h] { cmdsel.txt }
26 -h print this usage message
27 END
28
29 # Read command-line arguments and options.
30 my @opt;
31 my @arg;
32 push @{ /^-\S/ ? \@opt : \@arg }, $_ for @ARGV;
33 my %opt = map {
34 my $o = $_;
35 map { substr( $o, $_, 1 ) => 1 } 1 .. length($o)-1
36 } @opt;
37 if ( $opt{'?'} || $opt{h} || @arg > 1 ) {
38 print $usage;
39 exit 0;
40 }
41 my $cmdsel = @arg ? shift(@arg) : $Def::cmdsel_txt;
42 die $usage if @arg;
43
44 # Read in and process.
45 open F, '<', $cmdsel;
46 my $parse = Cmdsel::parse <F>;
47 close F;
48
49 my %line;
50 my %cmd ;
51 my %desc;
52
53 for my $ram ( keys %$parse ) {
54 my $ram1 = $parse->{$ram};
55 my $x = $ram1->{x};
56 for my $cmd ( keys %$x ) {
57 my $cmd1 = $x->{$cmd};
58 ++$cmd { $cmd1->{sect} }{$cmd};
59 ++$desc{ $cmd1->{desc} } unless $line{ $cmd1->{line} };
60 ++$line{ $cmd1->{line} };
61 }
62 }
63
64 {
65 print "\nRepeated Commands:\n";
66 print "$_\n" for map {
67 my $s = $_ ;
68 my $cs = $cmd{$s};
69 map { "$_ ($s)" } grep { $cs->{$_} > 1 } sort keys %$cs;
70 } sort keys %cmd;
71 print "\nRepeated Descriptions:\n";
72 print "$_\n" for grep { $desc{$_} > 1 } sort keys %desc;
73 printf "\nCommand line count: %${n_dig_stat}d", scalar keys %line ;
74 {
75 my $n;
76 $n += scalar( keys %{ $cmd{$_} } ) for keys %cmd;
77 printf "\nCommand count : %${n_dig_stat}d", $n;
78 }
79 printf "\nBranch count : %${n_dig_stat}d", scalar keys %$parse;
80 print "\n\n";
81 }
82