kconfig.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright (C) 2006 Felix Fietkau <[email protected]>
  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";
  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. next if $mod_plus and $config{$config} and $config{$config} eq "y";
  64. $config{$config} = $cfg{$config};
  65. }
  66. }
  67. return \%config;
  68. }
  69. sub config_diff($$) {
  70. my $cfg1 = shift;
  71. my $cfg2 = shift;
  72. my %config;
  73. foreach my $config (keys %$cfg2) {
  74. if (!defined($cfg1->{$config}) or $cfg1->{$config} ne $cfg2->{$config}) {
  75. $config{$config} = $cfg2->{$config};
  76. }
  77. }
  78. return \%config
  79. }
  80. sub config_sub($$) {
  81. my $cfg1 = shift;
  82. my $cfg2 = shift;
  83. my %config = %{$cfg1};
  84. foreach my $config (keys %$cfg2) {
  85. delete $config{$config};
  86. }
  87. return \%config;
  88. }
  89. sub print_cfgline($$) {
  90. my $name = shift;
  91. my $val = shift;
  92. if ($val eq '#undef' or $val eq 'n') {
  93. print "# $PREFIX$name is not set\n";
  94. } else {
  95. print "$PREFIX$name=$val\n";
  96. }
  97. }
  98. sub dump_config($) {
  99. my $cfg = shift;
  100. die "argument error in dump_config" unless ($cfg);
  101. my %config = %$cfg;
  102. foreach my $config (sort keys %config) {
  103. print_cfgline($config, $config{$config});
  104. }
  105. }
  106. sub parse_expr {
  107. my $pos = shift;
  108. my $mod_plus = shift;
  109. my $arg = $arg[$$pos++];
  110. die "Parse error" if (!$arg);
  111. if ($arg eq '&') {
  112. my $arg1 = parse_expr($pos);
  113. my $arg2 = parse_expr($pos);
  114. return config_and($arg1, $arg2);
  115. } elsif ($arg =~ /^\+/) {
  116. my $arg1 = parse_expr($pos);
  117. my $arg2 = parse_expr($pos);
  118. return config_add($arg1, $arg2, 0);
  119. } elsif ($arg =~ /^m\+/) {
  120. my $arg1 = parse_expr($pos);
  121. my $arg2 = parse_expr($pos, 1);
  122. return config_add($arg1, $arg2, 1);
  123. } elsif ($arg eq '>') {
  124. my $arg1 = parse_expr($pos);
  125. my $arg2 = parse_expr($pos);
  126. return config_diff($arg1, $arg2);
  127. } elsif ($arg eq '-') {
  128. my $arg1 = parse_expr($pos);
  129. my $arg2 = parse_expr($pos);
  130. return config_sub($arg1, $arg2);
  131. } else {
  132. return load_config($arg, $mod_plus);
  133. }
  134. }
  135. while (@ARGV > 0 and $ARGV[0] =~ /^-\w+$/) {
  136. my $cmd = shift @ARGV;
  137. if ($cmd =~ /^-n$/) {
  138. $PREFIX = "";
  139. } elsif ($cmd =~ /^-p$/) {
  140. $PREFIX = shift @ARGV;
  141. } else {
  142. die "Invalid option: $cmd\n";
  143. }
  144. }
  145. @arg = @ARGV;
  146. my $pos = 0;
  147. dump_config(parse_expr(\$pos));
  148. die "Parse error" if ($arg[$pos]);