kconfig.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
  4. #
  5. # This is free software, licensed under the GNU General Public License v2.
  6. # See /LICENSE for more information.
  7. #
  8. use warnings;
  9. use strict;
  10. my @arg;
  11. my $PREFIX = "CONFIG_";
  12. sub set_config($$$$) {
  13. my $config = shift;
  14. my $idx = shift;
  15. my $newval = shift;
  16. my $mod_plus = shift;
  17. if (!defined($config->{$idx}) or !$mod_plus or
  18. $config->{$idx} eq '#undef' or $newval eq 'y') {
  19. $config->{$idx} = $newval;
  20. }
  21. }
  22. sub load_config($$) {
  23. my $file = shift;
  24. my $mod_plus = shift;
  25. my %config;
  26. open FILE, "$file" or die "can't open file '$file'";
  27. while (<FILE>) {
  28. chomp;
  29. /^$PREFIX(.+?)=(.+)/ and do {
  30. set_config(\%config, $1, $2, $mod_plus);
  31. next;
  32. };
  33. /^# $PREFIX(.+?) is not set/ and do {
  34. set_config(\%config, $1, "#undef", $mod_plus);
  35. next;
  36. };
  37. /^#/ and next;
  38. /^(.+)$/ and warn "WARNING: can't parse line: $1\n";
  39. }
  40. return \%config;
  41. }
  42. sub config_and($$) {
  43. my $cfg1 = shift;
  44. my $cfg2 = shift;
  45. my %config;
  46. foreach my $config (keys %$cfg1) {
  47. my $val1 = $cfg1->{$config};
  48. my $val2 = $cfg2->{$config};
  49. $val2 and ($val1 eq $val2) and do {
  50. $config{$config} = $val1;
  51. };
  52. }
  53. return \%config;
  54. }
  55. sub config_add($$$) {
  56. my $cfg1 = shift;
  57. my $cfg2 = shift;
  58. my $mod_plus = shift;
  59. my %config;
  60. for ($cfg1, $cfg2) {
  61. my %cfg = %$_;
  62. foreach my $config (keys %cfg) {
  63. if ($mod_plus and $config{$config}) {
  64. next if $config{$config} eq "y";
  65. next if $cfg{$config} eq '#undef';
  66. }
  67. $config{$config} = $cfg{$config};
  68. }
  69. }
  70. return \%config;
  71. }
  72. sub config_diff($$$) {
  73. my $cfg1 = shift;
  74. my $cfg2 = shift;
  75. my $new_only = shift;
  76. my %config;
  77. foreach my $config (keys %$cfg2) {
  78. if (!defined($cfg1->{$config}) or $cfg1->{$config} ne $cfg2->{$config}) {
  79. next if $new_only and !defined($cfg1->{$config}) and $cfg2->{$config} eq '#undef';
  80. $config{$config} = $cfg2->{$config};
  81. }
  82. }
  83. return \%config
  84. }
  85. sub config_sub($$) {
  86. my $cfg1 = shift;
  87. my $cfg2 = shift;
  88. my %config = %{$cfg1};
  89. foreach my $config (keys %$cfg2) {
  90. delete $config{$config};
  91. }
  92. return \%config;
  93. }
  94. sub print_cfgline($$) {
  95. my $name = shift;
  96. my $val = shift;
  97. if ($val eq '#undef' or $val eq 'n') {
  98. print "# $PREFIX$name is not set\n";
  99. } else {
  100. print "$PREFIX$name=$val\n";
  101. }
  102. }
  103. sub dump_config($) {
  104. my $cfg = shift;
  105. die "argument error in dump_config" unless ($cfg);
  106. my %config = %$cfg;
  107. foreach my $config (sort keys %config) {
  108. print_cfgline($config, $config{$config});
  109. }
  110. }
  111. sub parse_expr {
  112. my $pos = shift;
  113. my $mod_plus = shift;
  114. my $arg = $arg[$$pos++];
  115. die "Parse error" if (!$arg);
  116. if ($arg eq '&') {
  117. my $arg1 = parse_expr($pos);
  118. my $arg2 = parse_expr($pos);
  119. return config_and($arg1, $arg2);
  120. } elsif ($arg =~ /^\+/) {
  121. my $arg1 = parse_expr($pos);
  122. my $arg2 = parse_expr($pos);
  123. return config_add($arg1, $arg2, 0);
  124. } elsif ($arg =~ /^m\+/) {
  125. my $arg1 = parse_expr($pos);
  126. my $arg2 = parse_expr($pos, 1);
  127. return config_add($arg1, $arg2, 1);
  128. } elsif ($arg eq '>') {
  129. my $arg1 = parse_expr($pos);
  130. my $arg2 = parse_expr($pos);
  131. return config_diff($arg1, $arg2, 0);
  132. } elsif ($arg eq '>+') {
  133. my $arg1 = parse_expr($pos);
  134. my $arg2 = parse_expr($pos);
  135. return config_diff($arg1, $arg2, 1);
  136. } elsif ($arg eq '-') {
  137. my $arg1 = parse_expr($pos);
  138. my $arg2 = parse_expr($pos);
  139. return config_sub($arg1, $arg2);
  140. } else {
  141. return load_config($arg, $mod_plus);
  142. }
  143. }
  144. while (@ARGV > 0 and $ARGV[0] =~ /^-\w+$/) {
  145. my $cmd = shift @ARGV;
  146. if ($cmd =~ /^-n$/) {
  147. $PREFIX = "";
  148. } elsif ($cmd =~ /^-p$/) {
  149. $PREFIX = shift @ARGV;
  150. } else {
  151. die "Invalid option: $cmd\n";
  152. }
  153. }
  154. @arg = @ARGV;
  155. my $pos = 0;
  156. dump_config(parse_expr(\$pos));
  157. die "Parse error" if ($arg[$pos]);