1#!/usr/bin/env perl
2#***************************************************************************
3#                                  _   _ ____  _
4#  Project                     ___| | | |  _ \| |
5#                             / __| | | | |_) | |
6#                            | (__| |_| |  _ <| |___
7#                             \___|\___/|_| \_\_____|
8#
9# Copyright (C) 2010 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
10#
11# This software is licensed as described in the file COPYING, which
12# you should have received as part of this distribution. The terms
13# are also available at https://curl.se/docs/copyright.html.
14#
15# You may opt to use, copy, modify, merge, publish, distribute and/or sell
16# copies of the Software, and permit persons to whom the Software is
17# furnished to do so, under the terms of the COPYING file.
18#
19# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20# KIND, either express or implied.
21#
22###########################################################################
23#
24
25use strict;
26use warnings;
27
28# the DISABLE options that can be set by configure
29my %disable;
30# the DISABLE options that are used in C files
31my %file;
32# the DISABLE options that are documented
33my %docs;
34
35# we may get the dir root pointed out
36my $root=$ARGV[0] || ".";
37my $DOCS="CURL-DISABLE.md";
38
39sub scanconf {
40    my ($f)=@_;
41    open S, "<$f";
42    while(<S>) {
43        if(/(CURL_DISABLE_[A-Z_]+)/g) {
44            my ($sym)=($1);
45            $disable{$sym} = 1;
46        }
47    }
48    close S;
49}
50
51sub scan_configure {
52    opendir(my $m, "$root/m4") || die "Can't opendir $root/m4: $!";
53    my @m4 = grep { /\.m4$/ } readdir($m);
54    closedir $m;
55    scanconf("$root/configure.ac");
56    # scan all m4 files too
57    for my $e (@m4) {
58        scanconf("$root/m4/$e");
59    }
60}
61
62sub scan_file {
63    my ($source)=@_;
64    open F, "<$source";
65    while(<F>) {
66        if(/(CURL_DISABLE_[A-Z_]+)/g) {
67            my ($sym)=($1);
68            $file{$sym} = $source;
69        }
70    }
71    close F;
72}
73
74sub scan_dir {
75    my ($dir)=@_;
76    opendir(my $dh, $dir) || die "Can't opendir $dir: $!";
77    my @cfiles = grep { /\.[ch]\z/ && -f "$dir/$_" } readdir($dh);
78    closedir $dh;
79    for my $f (sort @cfiles) {
80        scan_file("$dir/$f");
81    }
82}
83
84sub scan_sources {
85    scan_dir("$root/src");
86    scan_dir("$root/lib");
87    scan_dir("$root/lib/vtls");
88    scan_dir("$root/lib/vauth");
89}
90
91sub scan_docs {
92    open F, "<$root/docs/$DOCS";
93    my $line = 0;
94    while(<F>) {
95        $line++;
96        if(/^## (CURL_DISABLE_[A-Z_]+)/g) {
97            my ($sym)=($1);
98            $docs{$sym} = $line;
99        }
100    }
101    close F;
102}
103
104scan_configure();
105scan_sources();
106scan_docs();
107
108
109my $error = 0;
110# Check the configure symbols for use in code
111for my $s (sort keys %disable) {
112    if(!$file{$s}) {
113        printf "Present in configure.ac, not used by code: %s\n", $s;
114        $error++;
115    }
116    if(!$docs{$s}) {
117        printf "Present in configure.ac, not documented in $DOCS: %s\n", $s;
118        $error++;
119    }
120}
121
122# Check the code symbols for use in configure
123for my $s (sort keys %file) {
124    if(!$disable{$s}) {
125        printf "Not set by configure: %s (%s)\n", $s, $file{$s};
126        $error++;
127    }
128    if(!$docs{$s}) {
129        printf "Used in code, not documented in $DOCS: %s\n", $s;
130        $error++;
131    }
132}
133
134# Check the documented symbols
135for my $s (sort keys %docs) {
136    if(!$disable{$s}) {
137        printf "Documented but not in configure: %s\n", $s;
138        $error++;
139    }
140    if(!$file{$s}) {
141        printf "Documented, but not used by code: %s\n", $s;
142        $error++;
143    }
144}
145
146exit $error;
147