2 # Copyright © 2008, 2009 Jamie Zawinski <jwz@jwz.org>
4 # Permission to use, copy, modify, distribute, and sell this software and its
5 # documentation for any purpose is hereby granted without fee, provided that
6 # the above copyright notice appear in all copies and that both that
7 # copyright notice and this permission notice appear in supporting
8 # documentation. No representations are made about the suitability of this
9 # software for any purpose. It is provided "as is" without express or
12 # This parses the .c and .xml files and makes sure they are in sync: that
13 # options are spelled the same, and that all the numbers are in sync.
15 # Created: 1-Aug-2008.
21 my $progname = $0; $progname =~ s@.*/@@g;
22 my $version = q{ $Revision: 1.3 $ }; $version =~ s/^[^\d]+([\d.]+).*/$1/;
27 my $xlockmore_default_opts = '';
28 foreach (qw(count cycles delay ncolors size font)) {
29 $xlockmore_default_opts .= "{\"-$_\", \".$_\", XrmoptionSepArg, 0},\n";
31 $xlockmore_default_opts .=
32 "{\"-wireframe\", \".wireframe\", XrmoptionNoArg, \"true\"},\n" .
33 "{\"-3d\", \".use3d\", XrmoptionNoArg, \"true\"},\n";
37 # - A table of the default resource values.
38 # - A table of "-switch" => "resource: value", or "-switch" => "resource: %"
42 my $file = lc($saver) . ".c";
45 $file = 'apple2-main.c' if ($file eq 'apple2.c');
46 $file = 'sproingiewrap.c' if ($file eq 'sproingies.c');
47 $file = 'b_lockglue.c' if ($file eq 'bubble3d.c');
48 $file = 'polyhedra-gl.c' if ($file eq 'polyhedra.c');
50 $file = "glx/$file" unless (-f $file);
53 open (IN, "<$file") || error ("$file: $!");
54 while (<IN>) { $body .= $_; }
58 $body =~ s@/\*.*?\*/@@gs;
59 $body =~ s@^#\s*(if|ifdef|ifndef|elif|else|endif).*$@@gm;
60 $body =~ s/ANALOGTV_(DEFAULTS|OPTIONS)//gs;
64 print STDERR "$progname: $file: defaults:\n" if ($verbose > 2);
66 if ($body =~ m/_defaults\s*\[\]\s*=\s*{(.*?)}\s*;/s) {
67 foreach (split (/,\s*\n/, $1)) {
71 my ($key, $val) = m@^\"([^:\s]+)\s*:\s*(.*?)\s*\"$@;
72 print STDERR "$progname: $file: unparsable: $_\n" unless $key;
74 $res_to_val{$key} = $val;
75 print STDERR "$progname: $file: $key = $val\n" if ($verbose > 2);
77 } elsif ($body =~ m/\#\s*define\s*DEFAULTS\s*\\?\s*(.*?)\n[\n#]/s) {
80 $str =~ s/\"\s*\\\n\s*\"//gs;
81 $str =~ m/^\s*\"(.*?)\"\s*\\?\s*$/ ||
82 error ("$file: unparsable defaults: $str");
84 $str =~ s/\s*\\n\s*/\n/gs;
85 foreach (split (/\n/, $str)) {
86 my ($key, $val) = m@^([^:\s]+)\s*:\s*(.*?)\s*$@;
87 print STDERR "$progname: $file: unparsable: $_\n" unless $key;
89 $res_to_val{$key} = $val;
90 print STDERR "$progname: $file: $key = $val\n" if ($verbose > 2);
93 while ($body =~ s/^#\s*define\s+(DEF_([A-Z\d_]+))\s+\"([^\"]+)\"//m) {
94 my ($key1, $key2, $val) = ($1, lc($2), $3);
95 $key2 =~ s/_(.)/\U$1/gs; # "foo_bar" -> "fooBar"
96 $key2 =~ s/Rpm/RPM/; # kludge
97 $res_to_val{$key2} = $val;
98 print STDERR "$progname: $file: $key1 ($key2) = $val\n"
103 error ("$file: no defaults");
106 $body =~ m/XSCREENSAVER_MODULE(_2)?\s*\(\s*\"([^\"]+)\"/ ||
107 error ("$file: no module name");
108 $res_to_val{progclass} = $2;
109 $res_to_val{doFPS} = 'false';
110 print STDERR "$progname: $file: progclass = $2\n" if ($verbose > 2);
112 print STDERR "$progname: $file: switches to resources:\n"
115 $switch_to_res{-fps} = 'doFPS: true';
117 my ($ign, $opts) = ($body =~ m/(_options|\bopts)\s*\[\]\s*=\s*{(.*?)}\s*;/s);
118 if ($xlockmore_p || $opts) {
119 $opts = '' unless $opts;
120 $opts .= ",\n$xlockmore_default_opts" if ($xlockmore_p);
121 foreach (split (/,\s*\n/, $opts)) {
125 next if m/^{\s*0\s*,/s;
126 my ($switch, $res, $type, $v0, $v1, $v2) =
127 m@^ \s* { \s * \"([^\"]+)\" \s* ,
128 \s * \"([^\"]+)\" \s* ,
130 \s * (\"([^\"]*)\"|([a-zA-Z\d_]+)) \s* }@xi;
131 print STDERR "$progname: $file: unparsable: $_\n" unless $switch;
132 my $val = defined($v1) ? $v1 : $v2;
133 $val = '%' if ($type eq 'XrmoptionSepArg');
135 $res =~ s/^[a-z\d]+\.//si;
136 $switch =~ s/^\+/-no-/s;
139 if (defined ($switch_to_res{$switch})) {
140 print STDERR "$progname: $file: DUP! $switch = \"$val\"\n"
143 $switch_to_res{$switch} = $val;
144 print STDERR "$progname: $file: $switch = \"$val\"\n"
149 error ("$file: no options");
152 return (\%res_to_val, \%switch_to_res);
156 # "resource = default value"
157 # or "resource != non-default value"
160 my ($saver, $switch_to_res) = @_;
161 my $file = "config/" . lc($saver) . ".xml";
164 open (IN, "<$file") || error ("$file: $!");
165 while (<IN>) { $body .= $_; }
171 $body =~ s/<!--.*?-->/ /gsi;
174 $body =~ s/</\001</gs;
175 $body =~ s/\001(<option)/$1/gs;
177 print STDERR "$progname: $file: options:\n" if ($verbose > 2);
178 foreach (split (m/\001/, $body)) {
180 my ($type, $args) = m@^<([?/]?[-_a-z]+)\b\s*(.*)$@si;
181 error ("$progname: $file: unparsable: $_") unless $type;
182 next if ($type =~ m@^/@);
184 if ($type =~ m/^([hv]group|\?xml|command|string|file|_description|xscreensaver-(image|text))/s) {
186 } elsif ($type eq 'screensaver') {
187 my ($name) = ($args =~ m/\b_label\s*=\s*\"([^\"]+)\"/);
188 my $val = "progclass = $name";
190 print STDERR "$progname: $file: name: $name\n" if ($verbose > 2);
192 } elsif ($type eq 'number') {
193 my ($arg) = ($args =~ m/\barg\s*=\s*\"([^\"]+)\"/);
194 my ($val) = ($args =~ m/\bdefault\s*=\s*\"([^\"]+)\"/);
195 $val = '' unless defined($val);
198 $switch =~ s/\s+.*$//;
199 my ($res) = $switch_to_res->{$switch};
200 error ("$file: no resource for $type switch \"$arg\"") unless $res;
202 error ("$file: unparsable value: $res") if ($res =~ m/:/);
203 $val = "$res = $val";
205 print STDERR "$progname: $file: number: $val\n" if ($verbose > 2);
207 } elsif ($type eq 'boolean') {
208 my ($set) = ($args =~ m/\barg-set\s*=\s*\"([^\"]+)\"/);
209 my ($unset) = ($args =~ m/\barg-unset\s*=\s*\"([^\"]+)\"/);
210 my ($arg) = $set || $unset || error ("$file: unparsable: $args");
211 my ($res) = $switch_to_res->{$arg};
212 error ("$file: no resource for boolean switch \"$arg\"") unless $res;
213 my ($res2, $val) = ($res =~ m/^(.*?): (.*)$/s);
214 error ("$file: unparsable boolean resource: $res") unless $res2;
216 # $val = ($set ? "$res != $val" : "$res = $val");
217 $val = "$res != $val";
219 print STDERR "$progname: $file: boolean: $val\n" if ($verbose > 2);
221 } elsif ($type eq 'select') {
222 $args =~ s/</\001</gs;
223 my @opts = split (/\001/, $args);
226 my $this_res = undef;
228 error ("$file: unparsable: $_") unless (m/^<option\s/);
229 my ($set) = m/\barg-set\s*=\s*\"([^\"]+)\"/;
231 my ($set2, $val) = ($set =~ m/^(.*?) (.*)$/s);
232 $set = $set2 if ($set2);
233 my ($res) = $switch_to_res->{$set};
234 error ("$file: no resource for select switch \"$set\"") unless $res;
236 my ($res2, $val2) = ($res =~ m/^(.*?): (.*)$/s);
237 error ("$file: unparsable select resource: $res") unless $res2;
239 $val = $val2 unless ($val2 eq '%');
241 error ("$file: mismatched resources: $res vs $this_res")
242 if (defined($this_res) && $this_res ne $res);
245 $val = "$res != $val";
247 print STDERR "$progname: $file: select: $val\n" if ($verbose > 2);
250 error ("$file: multiple default options: $set") if ($unset_p);
256 error ("$file: unknown type \"$type\" for no arg");
264 sub check_config($) {
268 return 0 if ($saver =~ m/(-helper)$/);
270 my ($src_opts, $switchmap) = parse_src ($saver);
271 my (@xml_opts) = parse_xml ($saver, $switchmap);
274 foreach my $claim (@xml_opts) {
275 my ($res, $compare, $xval) = ($claim =~ m/^(.*) (=|!=) (.*)$/s);
276 error ("$saver: unparsable xml claim: $_") unless $compare;
278 my $sval = $src_opts->{$res};
279 if (!defined($sval)) {
280 print STDERR "$progname: $saver: $res: not in source\n";
281 } elsif ($compare eq '!='
284 print STDERR "$progname: $saver: " .
285 "src has \"$res = $sval\", xml has \"$claim\"\n";
287 } elsif ($verbose > 1) {
288 print STDERR "$progname: $saver: OK: \"$res = $sval\" vs \"$claim\"\n";
292 # Now make sure the progclass in the source and XML also matches
293 # the XCode target name.
295 my $obd = "../OSX/build/Debug";
297 my $progclass = $src_opts->{progclass};
298 my $f = (glob("$obd/$progclass.saver*"))[0];
299 if (!$f && $progclass ne 'Flurry') {
300 print STDERR "$progname: $progclass.saver does not exist\n";
305 print STDERR "$progname: $saver: OK\n"
306 if ($verbose == 1 && $failures == 0);
314 print STDERR "$progname: $err\n";
319 print STDERR "usage: $progname [--verbose] files ...\n";
325 while ($#ARGV >= 0) {
327 if (m/^--?verbose$/) { $verbose++; }
328 elsif (m/^-v+$/) { $verbose += length($_)-1; }
329 elsif (m/^-./) { usage; }
330 else { push @files, $_; }
334 usage unless ($#files >= 0);
336 foreach (@files) { $failures += check_config($_); }