1#!/usr/bin/perl -w
2
3#
4# checkcfguse.pl
5#
6# (1) finds all the Configure/config symbols
7#
8# (2) greps for their use in the core files and shows which ones.
9#
10
11use strict;
12use warnings;
13
14my %SYM;
15
16my @PAT =
17    (
18     [
19      # The format is:
20      # (1) aref of filename glob patterns
21      # (2) aref of qr patterns, the submatch $1 is the symbol name
22      [
23       "config_h.SH",
24      ],
25      [
26       qr/^#\$(\w+)\s+(\w+)/,
27      ],
28     ],
29     [
30      [
31       "Porting/config.sh",
32       "plan9/config_h.sample",
33       "win32/config_H.??",
34      ],
35      qr{^(?:\Q/*\E)?#(?:define|undef)\s+(\w+)},
36     ],
37     [
38      [
39       "configure.com",
40      ],
41      qr{^(\w+)="(?:define|undef)"},
42     ],
43    );
44
45{
46  print STDERR "$0: Looking for symbols...\n";
47  for my $pat (@PAT) {
48    for my $fn (map { glob($_) } @{ $pat->[0] }) {
49      if (open(my $fh, '<', $fn)) {
50        while (<$fh>) {
51          for my $p (@$pat) {
52            for my $sym (/$p/g) {
53              $SYM{$sym}{$fn}++;
54            }
55          }
56        }
57      }
58    }
59  }
60}
61
62printf(STDERR "$0: Found %d symbols\n", scalar keys %SYM);
63
64print STDERR "$0: Looking for their uses...\n";
65
66# Much too noisy grepping.
67delete $SYM{'_'};
68delete $SYM{'const'};
69
70my $SYM = join("|", sort { length($b) <=> length($a) || $a cmp $b } keys %SYM);
71
72open(my $mani, '<', "MANIFEST") or die "$0: Failed to open MANIFEST\n";
73
74my %found;
75while (<$mani>) {
76  if (/^(\S+)\s+/) {
77    my $fn = $1;
78    # Skip matches from the config files themselves,
79    # from metaconfig generated files that refer to
80    # the config symbols, and from pods.
81    next if $fn =~ m{^(?:config_h.SH|Configure|configure\.com|Porting/(?:config|Glossary)|(?:plan9|win32)/(?:config|(?:GNU)?[Mm]akefile)|uconfig)|\.pod$};
82    open my $fh, '<', $fn or die qq[$0: Failed to open $fn: $!];
83    while (<$fh>) {
84      while (/\b($SYM)\b/go) {
85        $found{$1}{$fn}++;
86      }
87    }
88  }
89}
90
91for my $sym (sort keys %SYM) {
92  if (exists $found{$sym}) {
93    my @found = keys %{$found{$sym}};
94    print "$sym\t", join(" ", sort @found), "\n";
95  } else {
96    print "$sym\n";
97  }
98}
99