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]";
15 if (my ($k,$v) = /^(\w+)=(.*)/) {
18 $options{$k} = 1 if ($k =~ /^CONFIG_/);
23 my $opt_regex = join("|", keys %options);
24 sub eval_expr { $_ = "@_"; s/\b($opt_regex)\b/ 1 /g if $opt_regex; s/\bCONFIG_\w+\b/ 0 /g; return eval $_; }
26 open IN, $ARGV[0] or die "Unable to open $ARGV[0]";
27 open OUT, ">$ARGV[1]" or die "Unable to create $ARGV[1]";
28 my @ifs = (); # stack of conditions, 1=satisfied, -1=unsatisfied, 0=shadowed
29 my $empty = 0; # last line was empty
31 if (/^#ifdef\s+(\w+)/) {
32 push @ifs, (@ifs && $ifs[$#ifs] <= 0) ? 0 : (defined $options{$1}) ? 1 : -1;
33 } elsif (/^#ifndef\s+(\w+)/) {
34 push @ifs, (@ifs && $ifs[$#ifs] <= 0) ? 0 : (defined $options{$1}) ? -1 : 1;
35 } elsif (/^#if\s(.*)$/) {
36 push @ifs, (@ifs && $ifs[$#ifs] <= 0) ? 0 : (eval_expr $1) ? 1 : -1;
38 defined pop @ifs || die "Improper nesting of conditionals";
41 defined $x || die "Improper nesting of conditionals";
42 push @ifs, $x >= 0 ? 0 : 1;
43 } elsif (/^#elsif\s(.*)$/) {
45 defined $x || die "Improper nesting of conditionals";
46 push @ifs, $x >= 0 ? 0 : (eval_expr $1) ? 1 : -1;
48 @ifs && $ifs[$#ifs] <= 0 && next;
52 } else { $empty = 0; }
53 if (/^#pipe\s+(.+)/) {
56 die "Piped command '$cmd' failed" if $?;
61 exists $vars{$v} or die "Cannot substitute $v: variable not set";
64 s/@(\w+)@/repl($1)/ge;
69 @ifs && die "Unterminated #ifdef";