config.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 %config;
  47. for ($cfg1, $cfg2) {
  48. my %cfg = %$_;
  49. foreach my $config (keys %cfg) {
  50. $config{$config} = $cfg{$config};
  51. }
  52. }
  53. return \%config;
  54. }
  55. sub config_diff($$) {
  56. my $cfg1 = shift;
  57. my $cfg2 = shift;
  58. my %config;
  59. foreach my $config (keys %$cfg2) {
  60. if (!$cfg1->{$config} or $cfg1->{$config} ne $cfg2->{$config}) {
  61. $config{$config} = $cfg2->{$config};
  62. }
  63. }
  64. return \%config
  65. }
  66. sub config_sub($$) {
  67. my $cfg1 = shift;
  68. my $cfg2 = shift;
  69. my %config = %{$cfg1};
  70. foreach my $config (keys %$cfg2) {
  71. delete $config{$config};
  72. }
  73. return \%config;
  74. }
  75. sub print_cfgline($$) {
  76. my $name = shift;
  77. my $val = shift;
  78. if ($val eq '#undef') {
  79. print "# CONFIG_$name is not set\n";
  80. } else {
  81. print "CONFIG_$name=$val\n";
  82. }
  83. }
  84. sub dump_config($) {
  85. my $cfg = shift;
  86. die "argument error in dump_config" unless ($cfg);
  87. my %config = %$cfg;
  88. foreach my $config (sort keys %config) {
  89. print_cfgline($config, $config{$config});
  90. }
  91. }
  92. sub parse_expr($);
  93. sub parse_expr($) {
  94. my $pos = shift;
  95. my $arg = $arg[$$pos++];
  96. die "Parse error" if (!$arg);
  97. if ($arg eq '&') {
  98. my $arg1 = parse_expr($pos);
  99. my $arg2 = parse_expr($pos);
  100. return config_and($arg1, $arg2);
  101. } elsif ($arg =~ /^\+/) {
  102. my $arg1 = parse_expr($pos);
  103. my $arg2 = parse_expr($pos);
  104. return config_add($arg1, $arg2);
  105. } elsif ($arg eq '>') {
  106. my $arg1 = parse_expr($pos);
  107. my $arg2 = parse_expr($pos);
  108. return config_diff($arg1, $arg2);
  109. } elsif ($arg eq '-') {
  110. my $arg1 = parse_expr($pos);
  111. my $arg2 = parse_expr($pos);
  112. return config_sub($arg1, $arg2);
  113. } else {
  114. return load_config($arg);
  115. }
  116. }
  117. my $pos = 0;
  118. dump_config(parse_expr(\$pos));
  119. die "Parse error" if ($arg[$pos]);