3 # Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
13 my $PREFIX = "CONFIG_";
15 sub set_config($$$$) {
21 if (!defined($config->{$idx}) or !$mod_plus or
22 $config->{$idx} eq '#undef' or $newval eq 'y') {
23 $config->{$idx} = $newval;
32 open FILE, "$file" or die "can't open file";
35 /^$PREFIX(.+?)=(.+)/ and do {
36 set_config(\%config, $1, $2, $mod_plus);
39 /^# $PREFIX(.+?) is not set/ and do {
40 set_config(\%config, $1, "#undef", $mod_plus);
44 /^(.+)$/ and warn "WARNING: can't parse line: $1\n";
55 foreach my $config (keys %$cfg1) {
56 my $val1 = $cfg1->{$config};
57 my $val2 = $cfg2->{$config};
58 $val2 and ($val1 eq $val2) and do {
59 $config{$config} = $val1;
75 foreach my $config (keys %cfg) {
76 if ($mod_plus and $config{$config}) {
77 next if $config{$config} eq "y";
78 next if $cfg{$config} eq '#undef';
80 $config{$config} = $cfg{$config};
86 sub config_diff($$$) {
92 foreach my $config (keys %$cfg2) {
93 if (!defined($cfg1->{$config}) or $cfg1->{$config} ne $cfg2->{$config}) {
94 next if $new_only and !defined($cfg1->{$config}) and $cfg2->{$config} eq '#undef';
95 $config{$config} = $cfg2->{$config};
104 my %config = %{$cfg1};
106 foreach my $config (keys %$cfg2) {
107 delete $config{$config};
112 sub print_cfgline($$) {
115 if ($val eq '#undef' or $val eq 'n') {
116 print "# $PREFIX$name is not set\n";
118 print "$PREFIX$name=$val\n";
125 die "argument error in dump_config" unless ($cfg);
127 foreach my $config (sort keys %config) {
128 print_cfgline($config, $config{$config});
134 my $mod_plus = shift;
135 my $arg = $arg[$$pos++];
137 die "Parse error" if (!$arg);
140 my $arg1 = parse_expr($pos);
141 my $arg2 = parse_expr($pos);
142 return config_and($arg1, $arg2);
143 } elsif ($arg =~ /^\+/) {
144 my $arg1 = parse_expr($pos);
145 my $arg2 = parse_expr($pos);
146 return config_add($arg1, $arg2, 0);
147 } elsif ($arg =~ /^m\+/) {
148 my $arg1 = parse_expr($pos);
149 my $arg2 = parse_expr($pos, 1);
150 return config_add($arg1, $arg2, 1);
151 } elsif ($arg eq '>') {
152 my $arg1 = parse_expr($pos);
153 my $arg2 = parse_expr($pos);
154 return config_diff($arg1, $arg2, 0);
155 } elsif ($arg eq '>+') {
156 my $arg1 = parse_expr($pos);
157 my $arg2 = parse_expr($pos);
158 return config_diff($arg1, $arg2, 1);
159 } elsif ($arg eq '-') {
160 my $arg1 = parse_expr($pos);
161 my $arg2 = parse_expr($pos);
162 return config_sub($arg1, $arg2);
164 return load_config($arg, $mod_plus);
168 while (@ARGV > 0 and $ARGV[0] =~ /^-\w+$/) {
169 my $cmd = shift @ARGV;
170 if ($cmd =~ /^-n$/) {
172 } elsif ($cmd =~ /^-p$/) {
173 $PREFIX = shift @ARGV;
175 die "Invalid option: $cmd\n";
181 dump_config(parse_expr(\$pos));
182 die "Parse error" if ($arg[$pos]);