X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=build%2Fgenconf;h=30e8896c3a58901e5085880ed6628d0e5600665d;hb=df17c554147ac4d68ead793d60ca1f8208bd2e6f;hp=5e2269b0a76f12a0a835982870239182c18e807e;hpb=3c72743c582ce8a47d2b3f54e33b41777ac64dba;p=libucw.git diff --git a/build/genconf b/build/genconf index 5e2269b0..30e8896c 100755 --- a/build/genconf +++ b/build/genconf @@ -1,6 +1,6 @@ #!/usr/bin/perl # Configuration file and script preprocessor -# (c) 2004 Martin Mares +# (c) 2004--2007 Martin Mares use strict; use warnings; @@ -9,14 +9,29 @@ use warnings; open CF, $ARGV[2] or die "Unable to open $ARGV[2]"; my %options = (); +my %vars = (); +sub opt { + my ($k,$v) = @_; + $vars{$k} = $v; + $options{$k} = 1 if ($k =~ /^CONFIG_/); +} +foreach my $k (keys %ENV) { + opt($k, $ENV{$k}); +} while () { - /^(CONFIG_\w+)=[^0]/ || next; - $options{$1} = 1; + chomp; + if (my ($k,$v) = /^(\w+)=(.*)/) { + $v =~ s/\s+$//; + opt($k, $v); + } } close CF; -my $opt_regex = join("|", keys %options); -sub eval_expr { $_ = "@_"; s/\b($opt_regex)\b/ 1 /g if $opt_regex; s/\bCONFIG_\w+\b/ 0 /g; return eval $_; } +sub eval_expr { + $_ = shift @_; + s/\b(CONFIG_\w+)\b/defined($options{$1}) ? 1 : 0/ge; + return eval $_; +} open IN, $ARGV[0] or die "Unable to open $ARGV[0]"; open OUT, ">$ARGV[1]" or die "Unable to create $ARGV[1]"; @@ -51,6 +66,15 @@ while () { die "Piped command '$cmd' failed" if $?; print OUT `$1`; } else { + sub repl($); + sub repl($) { + my $v = shift @_; + exists $vars{$v} or die "Cannot substitute $v: variable not set"; + my $x = $vars{$v}; + while ($x =~ s/\$\((\w+)\)/repl($1)/ge) { } + return $x; + } + s/@(\w+)@/repl($1)/ge; print OUT; } }