Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame^] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # Tune the configuration file |
| 4 | |
| 5 | use warnings; |
| 6 | use strict; |
| 7 | |
| 8 | my $usage = <<EOU; |
| 9 | $0 [-f <file>] full |
| 10 | $0 [-f <file>] unset <name> |
| 11 | $0 [-f <file>] set <name> [<value>] |
| 12 | EOU |
| 13 | |
| 14 | # Things that shouldn't be enabled with "full". |
| 15 | # Notes: |
| 16 | # - POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3 and |
| 17 | # POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION could be enabled if the |
| 18 | # respective tests were adapted |
| 19 | my @excluded = qw( |
| 20 | POLARSSL_HAVE_INT8 |
| 21 | POLARSSL_HAVE_INT16 |
| 22 | POLARSSL_HAVE_SSE2 |
| 23 | POLARSSL_PLATFORM_NO_STD_FUNCTIONS |
| 24 | POLARSSL_ECP_DP_M221_ENABLED |
| 25 | POLARSSL_ECP_DP_M383_ENABLED |
| 26 | POLARSSL_ECP_DP_M511_ENABLED |
| 27 | POLARSSL_NO_DEFAULT_ENTROPY_SOURCES |
| 28 | POLARSSL_NO_PLATFORM_ENTROPY |
| 29 | POLARSSL_SSL_HW_RECORD_ACCEL |
| 30 | POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3 |
| 31 | POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION |
| 32 | POLARSSL_ZLIB_SUPPORT |
| 33 | POLARSSL_PKCS11_C |
| 34 | _ALT\s*$ |
| 35 | ); |
| 36 | |
| 37 | my $config_file = "include/polarssl/config.h"; |
| 38 | |
| 39 | # get -f option |
| 40 | if (@ARGV >= 2 && $ARGV[0] eq "-f") { |
| 41 | shift; # -f |
| 42 | $config_file = shift; |
| 43 | |
| 44 | -f $config_file or die "No such file: $config_file\n"; |
| 45 | } else { |
| 46 | if (! -f $config_file) { |
| 47 | chdir '..' or die; |
| 48 | -d $config_file |
| 49 | or die "Without -f, must be run from root or scripts\n" |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | # get action |
| 54 | die $usage unless @ARGV; |
| 55 | my $action = shift; |
| 56 | |
| 57 | my ($name, $value); |
| 58 | if ($action eq "full") { |
| 59 | # nothing to do |
| 60 | } elsif ($action eq "unset") { |
| 61 | die $usage unless @ARGV; |
| 62 | $name = shift; |
| 63 | } elsif ($action eq "set") { |
| 64 | die $usage unless @ARGV; |
| 65 | $name = shift; |
| 66 | $value = shift if @ARGV; |
| 67 | } else { |
| 68 | die $usage; |
| 69 | } |
| 70 | die $usage if @ARGV; |
| 71 | |
| 72 | open my $config_read, '<', $config_file or die "read $config_file: $!\n"; |
| 73 | my @config_lines = <$config_read>; |
| 74 | close $config_read; |
| 75 | |
| 76 | my $exclude_re = join '|', @excluded; |
| 77 | |
| 78 | open my $config_write, '>', $config_file or die "write $config_file: $!\n"; |
| 79 | |
| 80 | my $done; |
| 81 | for my $line (@config_lines) { |
| 82 | if ($action eq "full") { |
| 83 | if ($line =~ /name SECTION: Module configuration options/) { |
| 84 | $done = 1; |
| 85 | } |
| 86 | |
| 87 | if (!$done && $line =~ m!^//\s?#define! && $line !~ /$exclude_re/) { |
| 88 | $line =~ s!^//!!; |
| 89 | } |
| 90 | } elsif ($action eq "unset") { |
| 91 | if (!$done && $line =~ /^\s*#define\s*$name/) { |
| 92 | $line = '//' . $line; |
| 93 | $done = 1; |
| 94 | } |
| 95 | } elsif (!$done && $action eq "set") { |
| 96 | if ($line =~ m!^(?://)?\s*#define\s*$name!) { |
| 97 | $line = "#define $name"; |
| 98 | $line .= " $value" if defined $value && $value ne ""; |
| 99 | $line .= "\n"; |
| 100 | $done = 1; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | print $config_write $line; |
| 105 | } |
| 106 | |
| 107 | close $config_write; |
| 108 | |
| 109 | warn "configuration section not found" if ($action eq "full" && !$done); |
| 110 | warn "$name not found" if ($action ne "full" && !$done); |
| 111 | |
| 112 | __END__ |