2 # Configuration file and script preprocessor
3 # (c) 2004 Martin Mares <mj@ucw.cz>
8 @ARGV == 3 or die "Usage: genconf <src> <dest> <config.mk>";
10 open CF, $ARGV[2] or die "Unable to open $ARGV[2]";
13 /^(CONFIG_\w+)=[^0]/ || next;
18 my $opt_regex = join("|", keys %options);
19 sub eval_expr { $_ = "@_"; s/\b($opt_regex)\b/ 1 /g if $opt_regex; s/\bCONFIG_\w+\b/ 0 /g; return eval $_; }
21 open IN, $ARGV[0] or die "Unable to open $ARGV[0]";
22 open OUT, ">$ARGV[1]" or die "Unable to create $ARGV[1]";
23 my @ifs = (); # stack of conditions, 1=satisfied, -1=unsatisfied, 0=shadowed
24 my $empty = 0; # last line was empty
26 if (/^#ifdef\s+(\w+)/) {
27 push @ifs, (@ifs && $ifs[$#ifs] <= 0) ? 0 : (defined $options{$1}) ? 1 : -1;
28 } elsif (/^#ifndef\s+(\w+)/) {
29 push @ifs, (@ifs && $ifs[$#ifs] <= 0) ? 0 : (defined $options{$1}) ? -1 : 1;
30 } elsif (/^#if\s(.*)$/) {
31 push @ifs, (@ifs && $ifs[$#ifs] <= 0) ? 0 : (eval_expr $1) ? 1 : -1;
33 defined pop @ifs || die "Improper nesting of conditionals";
36 defined $x || die "Improper nesting of conditionals";
37 push @ifs, $x >= 0 ? 0 : 1;
38 } elsif (/^#elsif\s(.*)$/) {
40 defined $x || die "Improper nesting of conditionals";
41 push @ifs, $x >= 0 ? 0 : (eval_expr $1) ? 1 : -1;
43 @ifs && $ifs[$#ifs] <= 0 && next;
47 } else { $empty = 0; }
48 if (/^#pipe\s+(.+)/) {
55 @ifs && die "Unterminated #ifdef";