blob: 6ee1345e9a3acac0e1ff8d609e83a1ea66629d26 [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é-Gonnardb7527152015-06-03 09:59:06 +010039# Things that should be enabled in "full" even if they match @excluded
40my @non_excluded = qw(
41PLATFORM_[A-Z0-9]+_ALT
42);
43
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020044my $config_file = "include/polarssl/config.h";
45
46# get -f option
47if (@ARGV >= 2 && $ARGV[0] eq "-f") {
48 shift; # -f
49 $config_file = shift;
50
51 -f $config_file or die "No such file: $config_file\n";
52} else {
53 if (! -f $config_file) {
54 chdir '..' or die;
55 -d $config_file
56 or die "Without -f, must be run from root or scripts\n"
57 }
58}
59
60# get action
61die $usage unless @ARGV;
62my $action = shift;
63
64my ($name, $value);
65if ($action eq "full") {
66 # nothing to do
67} elsif ($action eq "unset") {
68 die $usage unless @ARGV;
69 $name = shift;
70} elsif ($action eq "set") {
71 die $usage unless @ARGV;
72 $name = shift;
73 $value = shift if @ARGV;
74} else {
75 die $usage;
76}
77die $usage if @ARGV;
78
79open my $config_read, '<', $config_file or die "read $config_file: $!\n";
80my @config_lines = <$config_read>;
81close $config_read;
82
83my $exclude_re = join '|', @excluded;
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +010084my $no_exclude_re = join '|', @non_excluded;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020085
86open my $config_write, '>', $config_file or die "write $config_file: $!\n";
87
88my $done;
89for my $line (@config_lines) {
90 if ($action eq "full") {
91 if ($line =~ /name SECTION: Module configuration options/) {
92 $done = 1;
93 }
94
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +010095 if (!$done && $line =~ m!^//\s?#define! &&
96 ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020097 $line =~ s!^//!!;
98 }
99 } elsif ($action eq "unset") {
100 if (!$done && $line =~ /^\s*#define\s*$name/) {
101 $line = '//' . $line;
102 $done = 1;
103 }
104 } elsif (!$done && $action eq "set") {
105 if ($line =~ m!^(?://)?\s*#define\s*$name!) {
106 $line = "#define $name";
107 $line .= " $value" if defined $value && $value ne "";
108 $line .= "\n";
109 $done = 1;
110 }
111 }
112
113 print $config_write $line;
114}
115
116close $config_write;
117
118warn "configuration section not found" if ($action eq "full" && !$done);
119warn "$name not found" if ($action ne "full" && !$done);
120
121__END__