]> mj.ucw.cz Git - libucw.git/blob - build/genconf
image-tool uses RGB automatically when converting non-RGB jpegs
[libucw.git] / build / genconf
1 #!/usr/bin/perl
2 # Configuration file and script preprocessor
3 # (c) 2004 Martin Mares <mj@ucw.cz>
4
5 use strict;
6 use warnings;
7
8 @ARGV == 3 or die "Usage: genconf <src> <dest> <config.mk>";
9
10 open CF, $ARGV[2] or die "Unable to open $ARGV[2]";
11 my %options = ();
12 while (<CF>) {
13         /^(CONFIG_\w+)=[^0]/ || next;
14         $options{$1} = 1;
15 }
16 close CF;
17
18 open IN, $ARGV[0] or die "Unable to open $ARGV[0]";
19 open OUT, ">$ARGV[1]" or die "Unable to create $ARGV[1]";
20 my @ifs = ();   # stack of conditions, 1=satisfied, -1=unsatisfied, 0=shadowed
21 my $empty = 0;  # last line was empty
22 while (<IN>) {
23         if (/^#ifdef\s+(\w+)/) {
24                 push @ifs, (@ifs && $ifs[$#ifs] <= 0) ? 0 : (defined $options{$1}) ? 1 : -1;
25         } elsif (/^#ifndef\s+(\w+)/) {
26                 push @ifs, (@ifs && $ifs[$#ifs] <= 0) ? 0 : (defined $options{$1}) ? -1 : 1;
27         } elsif (/^#endif/) {
28                 defined pop @ifs || die "Improper nesting of conditionals";
29         } elsif (/^#else/) {
30                 my $x = pop @ifs;
31                 defined $x || die "Improper nesting of conditionals";
32                 push @ifs, -$x;
33         } else {
34                 @ifs && $ifs[$#ifs] <= 0 && next;
35                 if (/^$/) {
36                         $empty && next;
37                         $empty = 1;
38                 } else { $empty = 0; }
39                 if (/^#pipe\s+(.+)/) {
40                         print OUT `$1`;
41                 } else {
42                         print OUT;
43                 }
44         }
45 }
46 @ifs && die "Unterminated #ifdef";
47 close IN;
48 close OUT;