kconfig.pl 2.8 KB

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