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
=> "xxxterm.conf";
20 use constant MAIN
=> "xxxterm.c";
22 # This is a script that checks if the default setting values of xxxterm
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) = @_;
35 if ($default_value =~ /^(0|1)$/) {
36 if ($default_value != $config_value) {
39 # Non-boolean setting.
41 if ($default_value eq $config_value) {
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.
62 cmd_font cmd_font_name
63 oops_font oops_font_name
64 tabbar_font tabbar_font_name
65 statusbar_font statusbar_font_name
75 # The -P flag invokes the C pre-processor.
87 # ----------------------------------------------------------------------
91 # Walk through SETTINGS and collect all settings and their default
92 # values. Note that the default string values cannot be initialized in
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
101 with_file
(CONFIG
, \
&process_config
, \
%config);
103 # Finally check if the collected settings follow the rule.
107 # ----------------------------------------------------------------------
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.
116 my ($file, $sub, $hash) = @_;
118 open(my $fh, "<", $file)
119 or die "Cannot open $file: $!";
126 or warn "Cannot close $file: $!";
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+);/) {
143 # Save float settings.
144 if (/^[g]?float\s+(\w+)\s+=\s(\d+\.\d+);/) {
148 # Save boolean settings.
149 if (/^gboolean\s+(\w+)\s+=\s(\w+);/) {
152 } elsif ($2 eq "FALSE") {
155 die "$1 got non-boolean value $2 in "
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";
172 # Find main(), as that is where the strings are initialized.
173 unless ($found_main) {
174 if (/^main\(int argc, char \*argv\[\]\)$/) {
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;
191 # Save integer and boolean settings.
192 if (/^#\s+(\w+)\s+=\s+(\d+)$/) {
197 # Save float settings.
198 if (/^#\s+(\w+)\s+=\s+(\d+\.\d+)$/) {
203 # Save string settings.
204 if (/^#\s+(\w+)\s+=\s+(.*)$/) {
209 my @breaks_the_rule = ();
210 my @missing_in_config = ();
211 my @missing_in_settings = ();
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);
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 }
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;
263 my ($line, $sep) = @_;
265 print "\n$line\n" . $sep x
length($line) . "\n";