Match recent FreeBSD Makefile fix in dragonfly Makefile.
[xombrero.git] / config-checker.pl
blobf4e8dca190153b68a3869de885c80ca939889e4b
1 #!/usr/bin/perl -P
3 # Copyright (c) 2012 Stevan Andjelkovic <stevan.andjelkovic@strath.ac.uk>
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 use constant SETTINGS => "settings.c";
19 use constant CONFIG => "xombrero.conf";
20 use constant MAIN => "xombrero.c";
22 # This is a script that checks if the default setting values of xombrero
23 # (found in SETTINGS) are reflected by the CONFIG. It is meant to help
24 # the developers; the end user need not bother with it.
26 # The rule of the game: if a setting has a boolean value (i.e. on or
27 # off), then the config value should be the negation of the default
28 # value, otherwise (i.e. the value is non-boolean) the config value
29 # should be the default value.
31 sub follows_the_rule {
32 my ($default_value, $config_value) = @_;
34 # Boolean setting.
35 if ($default_value =~ /^(0|1)$/) {
36 if ($default_value != $config_value) {
37 return (1);
39 # Non-boolean setting.
40 } else {
41 if ($default_value eq $config_value) {
42 return (1);
45 return (0);
48 # There are plenty of limitations, for example: struct settings are
49 # ignored, some string settings are initialized in special ways (we only
50 # account for strings initialized in main() using g_strdup.), etc.
52 # The following are settings which will not be checked by the script,
53 # because either they are exceptions to the rule or it is hard to check
54 # them in a systematic way. Check them manually.
55 my @unsupported = qw(
56 url_regex
57 http_proxy
58 browser_mode
59 default_script
60 search_string
62 cmd_font cmd_font_name
63 oops_font oops_font_name
64 tabbar_font tabbar_font_name
65 statusbar_font statusbar_font_name
67 runtime_settings
68 cookie_wl
69 js_wl
70 keybinding
71 mime_type
72 alias
75 # The -P flag invokes the C pre-processor.
76 #define DEBUG (0)
78 use strict;
79 #if (DEBUG)
80 use warnings;
81 #endif
83 my %settings = ();
84 my %config = ();
87 # ----------------------------------------------------------------------
88 # Main
91 # Walk through SETTINGS and collect all settings and their default
92 # values. Note that the default string values cannot be initialized in
93 # SETTINGS.
94 with_file(SETTINGS, \&process_settings, \%settings);
96 # We need to go through MAIN to get the default string values.
97 with_file(MAIN, \&process_main, \%settings);
99 # Then walk through CONFIG and collect all settings and their config
100 # values.
101 with_file(CONFIG, \&process_config, \%config);
103 # Finally check if the collected settings follow the rule.
104 summary();
107 # ----------------------------------------------------------------------
108 # Helper subroutines
111 # Higher-order subroutine that takes a file, a subroutine and a hash. It
112 # opens the file and applies the subroutine to each line of the file. The
113 # hash is used for debugging output.
114 sub with_file {
116 my ($file, $sub, $hash) = @_;
118 open(my $fh, "<", $file)
119 or die "Cannot open $file: $!";
121 while (<$fh>) {
122 &$sub($_);
125 close($fh)
126 or warn "Cannot close $file: $!";
128 if (DEBUG) {
129 print("$file:\n" . "=" x length($file) . "\n");
130 while (my ($key, $value) = each(%{$hash})) {
131 print "$key => $value\n";
136 sub process_settings {
138 # Save integer settings.
139 if (/^[g]?int\s+(\w+)\s+=\s(\d+);/) {
140 $settings{$1} = $2;
143 # Save float settings.
144 if (/^[g]?float\s+(\w+)\s+=\s(\d+\.\d+);/) {
145 $settings{$1} = $2;
148 # Save boolean settings.
149 if (/^gboolean\s+(\w+)\s+=\s(\w+);/) {
150 if ($2 eq "TRUE") {
151 $settings{$1} = 1;
152 } elsif ($2 eq "FALSE") {
153 $settings{$1} = 0;
154 } else {
155 die "$1 got non-boolean value $2 in "
156 . SETTINGS;
160 # Save string setting. Note that the default string cannot be
161 # initialized in SETTINGS, that is why we also process MAIN.
162 if (/^[g]?char\s+\*(\w+)\s+=\s+NULL;/ or
163 /^[g]?char\s+(\w+)\[\w+\];/) {
164 $settings{$1} = "NULL";
168 my $found_main = 0;
170 sub process_main {
172 # Find main(), as that is where the strings are initialized.
173 unless ($found_main) {
174 if (/^main\(int argc, char \*argv\[\]\)$/) {
175 $found_main = 1;
177 return;
180 # Once we found main(), get the string settings.
181 for my $setting (keys %settings) {
182 $setting = quotemeta $setting;
183 if (/^\s+$setting\s+=\s+g_strdup\("(.*)"\);/) {
184 $settings{$setting} = $1;
189 sub process_config {
191 # Save integer and boolean settings.
192 if (/^#\s+(\w+)\s+=\s+(\d+)$/) {
193 $config{$1} = $2;
194 return;
197 # Save float settings.
198 if (/^#\s+(\w+)\s+=\s+(\d+\.\d+)$/) {
199 $config{$1} = $2;
200 return;
203 # Save string settings.
204 if (/^#\s+(\w+)\s+=\s+(.*)$/) {
205 $config{$1} = $2;
209 my @breaks_the_rule = ();
210 my @missing_in_config = ();
211 my @missing_in_settings = ();
213 sub summary {
215 print_with_sep("Summary", "=");
217 # Collect settings that are in SETTINGS, but missing in CONFIG
218 # as well as settings that break the rule.
219 while (my ($setting, $default_value) = each(%settings)) {
221 my $config_value = $config{$setting};
223 if (!defined $config_value) {
224 push(@missing_in_config, $setting);
225 next;
228 if (!follows_the_rule($default_value, $config_value)) {
229 push(@breaks_the_rule, $setting);
233 # Collect settings that are in CONFIG, but not in SETTINGS.
234 for my $setting (keys %config) {
235 if (!defined $settings{$setting}) {
236 push(@missing_in_settings, $setting);
240 # Report the settings that break the rule.
241 print_with_sep("Settings that break the rule", "-");
242 for my $setting (@breaks_the_rule) {
243 print SETTINGS . ":\t$setting = $settings{$setting}\n"
244 . CONFIG . ":\t$setting = $config{$setting}\n\n"
245 unless $setting ~~ @unsupported;
248 # Report the settings that are in SETTINGS, but not in CONFIG.
249 print_with_sep("Settings in " . SETTINGS . " that are not in " . CONFIG, "-");
250 map { print "$_ = $settings{$_}\n" unless $_ ~~ @unsupported }
251 @missing_in_config;
253 # And vice versa.
254 print_with_sep("Settings in " . CONFIG . " that are not in " . SETTINGS, "-");
255 map { print "$_ = $config{$_}\n" unless $_ ~~ @unsupported }
256 @missing_in_settings;
258 print_with_sep("Manually check the following", "-");
259 map { print "$_\n" } @unsupported;
262 sub print_with_sep {
263 my ($line, $sep) = @_;
265 print "\n$line\n" . $sep x length($line) . "\n";