kconfig.pl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 load_config($) {
  13. my $file = shift;
  14. my %config;
  15. open FILE, "$file" or die "can't open file";
  16. while (<FILE>) {
  17. chomp;
  18. /^$PREFIX(.+?)=(.+)/ and do {
  19. $config{$1} = $2;
  20. next;
  21. };
  22. /^# $PREFIX(.+?) is not set/ and do {
  23. $config{$1} = "#undef";
  24. next;
  25. };
  26. /^#/ and next;
  27. /^(.+)$/ and warn "WARNING: can't parse line: $1\n";
  28. }
  29. return \%config;
  30. }
  31. sub config_and($$) {
  32. my $cfg1 = shift;
  33. my $cfg2 = shift;
  34. my %config;
  35. foreach my $config (keys %$cfg1) {
  36. my $val1 = $cfg1->{$config};
  37. my $val2 = $cfg2->{$config};
  38. $val2 and ($val1 eq $val2) and do {
  39. $config{$config} = $val1;
  40. };
  41. }
  42. return \%config;
  43. }
  44. sub config_add($$$) {
  45. my $cfg1 = shift;
  46. my $cfg2 = shift;
  47. my $mod_plus = shift;
  48. my %config;
  49. for ($cfg1, $cfg2) {
  50. my %cfg = %$_;
  51. foreach my $config (keys %cfg) {
  52. next if $mod_plus and $config{$config} and $config{$config} eq "y";
  53. $config{$config} = $cfg{$config};
  54. }
  55. }
  56. return \%config;
  57. }
  58. sub config_diff($$) {
  59. my $cfg1 = shift;
  60. my $cfg2 = shift;
  61. my %config;
  62. foreach my $config (keys %$cfg2) {
  63. if (!defined($cfg1->{$config}) or $cfg1->{$config} ne $cfg2->{$config}) {
  64. $config{$config} = $cfg2->{$config};
  65. }
  66. }
  67. return \%config
  68. }
  69. sub config_sub($$) {
  70. my $cfg1 = shift;
  71. my $cfg2 = shift;
  72. my %config = %{$cfg1};
  73. foreach my $config (keys %$cfg2) {
  74. delete $config{$config};
  75. }
  76. return \%config;
  77. }
  78. sub print_cfgline($$) {
  79. my $name = shift;
  80. my $val = shift;
  81. if ($val eq '#undef' or $val eq 'n') {
  82. print "# $PREFIX$name is not set\n";
  83. } else {
  84. print "$PREFIX$name=$val\n";
  85. }
  86. }
  87. sub dump_config($) {
  88. my $cfg = shift;
  89. die "argument error in dump_config" unless ($cfg);
  90. my %config = %$cfg;
  91. foreach my $config (sort keys %config) {
  92. print_cfgline($config, $config{$config});
  93. }
  94. }
  95. sub parse_expr($);
  96. sub parse_expr($) {
  97. my $pos = shift;
  98. my $arg = $arg[$$pos++];
  99. die "Parse error" if (!$arg);
  100. if ($arg eq '&') {
  101. my $arg1 = parse_expr($pos);
  102. my $arg2 = parse_expr($pos);
  103. return config_and($arg1, $arg2);
  104. } elsif ($arg =~ /^\+/) {
  105. my $arg1 = parse_expr($pos);
  106. my $arg2 = parse_expr($pos);
  107. return config_add($arg1, $arg2, 0);
  108. } elsif ($arg =~ /^m\+/) {
  109. my $arg1 = parse_expr($pos);
  110. my $arg2 = parse_expr($pos);
  111. return config_add($arg1, $arg2, 1);
  112. } elsif ($arg eq '>') {
  113. my $arg1 = parse_expr($pos);
  114. my $arg2 = parse_expr($pos);
  115. return config_diff($arg1, $arg2);
  116. } elsif ($arg eq '-') {
  117. my $arg1 = parse_expr($pos);
  118. my $arg2 = parse_expr($pos);
  119. return config_sub($arg1, $arg2);
  120. } else {
  121. return load_config($arg);
  122. }
  123. }
  124. while (@ARGV > 0 and $ARGV[0] =~ /^-\w+$/) {
  125. my $cmd = shift @ARGV;
  126. if ($cmd =~ /^-n$/) {
  127. $PREFIX = "";
  128. } elsif ($cmd =~ /^-p$/) {
  129. $PREFIX = shift @ARGV;
  130. } else {
  131. die "Invalid option: $cmd\n";
  132. }
  133. }
  134. @arg = @ARGV;
  135. my $pos = 0;
  136. dump_config(parse_expr(\$pos));
  137. die "Parse error" if ($arg[$pos]);