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