2 # Configuration file and script preprocessor
3 # (c) 2004--2007 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]";
16 $options{$k} = 1 if ($k =~ /^CONFIG_/);
18 foreach my $k (keys %ENV) {
23 if (my ($k,$v) = /^(\w+)=(.*)/) {
32 s/\b(CONFIG_\w+)\b/defined($options{$1}) ? 1 : 0/ge;
36 open IN, $ARGV[0] or die "Unable to open $ARGV[0]";
37 open OUT, ">$ARGV[1]" or die "Unable to create $ARGV[1]";
38 my @ifs = (); # stack of conditions, 1=satisfied, -1=unsatisfied, 0=shadowed
39 my $empty = 0; # last line was empty
41 if (/^#ifdef\s+(\w+)/) {
42 push @ifs, (@ifs && $ifs[$#ifs] <= 0) ? 0 : (defined $options{$1}) ? 1 : -1;
43 } elsif (/^#ifndef\s+(\w+)/) {
44 push @ifs, (@ifs && $ifs[$#ifs] <= 0) ? 0 : (defined $options{$1}) ? -1 : 1;
45 } elsif (/^#if\s(.*)$/) {
46 push @ifs, (@ifs && $ifs[$#ifs] <= 0) ? 0 : (eval_expr $1) ? 1 : -1;
48 defined pop @ifs || die "Improper nesting of conditionals";
51 defined $x || die "Improper nesting of conditionals";
52 push @ifs, $x >= 0 ? 0 : 1;
53 } elsif (/^#elsif\s(.*)$/) {
55 defined $x || die "Improper nesting of conditionals";
56 push @ifs, $x >= 0 ? 0 : (eval_expr $1) ? 1 : -1;
58 @ifs && $ifs[$#ifs] <= 0 && next;
62 } else { $empty = 0; }
63 if (/^#pipe\s+(.+)/) {
66 die "Piped command '$cmd' failed" if $?;
72 exists $vars{$v} or die "Cannot substitute $v: variable not set";
74 while ($x =~ s/\$\((\w+)\)/repl($1)/ge) { }
77 s/@(\w+)@/repl($1)/ge;
82 @ifs && die "Unterminated #ifdef";