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