kconfig.pl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright (C) 2006 Felix Fietkau <nbd@nbd.name>
  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. my @keys = map {
  90. my $expr = $_;
  91. $expr =~ /[?.*]/ ?
  92. map {
  93. /^$expr$/ ? $_ : ()
  94. } keys %config : $expr;
  95. } keys %$cfg2;
  96. foreach my $config (@keys) {
  97. delete $config{$config};
  98. }
  99. return \%config;
  100. }
  101. sub print_cfgline($$) {
  102. my $name = shift;
  103. my $val = shift;
  104. if ($val eq '#undef' or $val eq 'n') {
  105. print "# $PREFIX$name is not set\n";
  106. } else {
  107. print "$PREFIX$name=$val\n";
  108. }
  109. }
  110. sub dump_config($) {
  111. my $cfg = shift;
  112. die "argument error in dump_config" unless ($cfg);
  113. my %config = %$cfg;
  114. foreach my $config (sort keys %config) {
  115. print_cfgline($config, $config{$config});
  116. }
  117. }
  118. sub parse_expr {
  119. my $pos = shift;
  120. my $mod_plus = shift;
  121. my $arg = $arg[$$pos++];
  122. die "Parse error" if (!$arg);
  123. if ($arg eq '&') {
  124. my $arg1 = parse_expr($pos);
  125. my $arg2 = parse_expr($pos);
  126. return config_and($arg1, $arg2);
  127. } elsif ($arg =~ /^\+/) {
  128. my $arg1 = parse_expr($pos);
  129. my $arg2 = parse_expr($pos);
  130. return config_add($arg1, $arg2, 0);
  131. } elsif ($arg =~ /^m\+/) {
  132. my $arg1 = parse_expr($pos);
  133. my $arg2 = parse_expr($pos, 1);
  134. return config_add($arg1, $arg2, 1);
  135. } elsif ($arg eq '>') {
  136. my $arg1 = parse_expr($pos);
  137. my $arg2 = parse_expr($pos);
  138. return config_diff($arg1, $arg2, 0);
  139. } elsif ($arg eq '>+') {
  140. my $arg1 = parse_expr($pos);
  141. my $arg2 = parse_expr($pos);
  142. return config_diff($arg1, $arg2, 1);
  143. } elsif ($arg eq '-') {
  144. my $arg1 = parse_expr($pos);
  145. my $arg2 = parse_expr($pos);
  146. return config_sub($arg1, $arg2);
  147. } else {
  148. return load_config($arg, $mod_plus);
  149. }
  150. }
  151. while (@ARGV > 0 and $ARGV[0] =~ /^-\w+$/) {
  152. my $cmd = shift @ARGV;
  153. if ($cmd =~ /^-n$/) {
  154. $PREFIX = "";
  155. } elsif ($cmd =~ /^-p$/) {
  156. $PREFIX = shift @ARGV;
  157. } else {
  158. die "Invalid option: $cmd\n";
  159. }
  160. }
  161. @arg = @ARGV;
  162. my $pos = 0;
  163. dump_config(parse_expr(\$pos));
  164. die "Parse error" if ($arg[$pos]);