blob: a50a80cfdf44c7b60d11c6fe0d13d827ecce4527 [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(
Manuel Pégourié-Gonnard4d9e36a2015-09-02 10:10:32 +020021POLARSSL_ERROR_STRERROR_BC
22POLARSSL_MEMORY_C
23POLARSSL_PBKDF2_C
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020024POLARSSL_HAVE_INT8
25POLARSSL_HAVE_INT16
26POLARSSL_HAVE_SSE2
27POLARSSL_PLATFORM_NO_STD_FUNCTIONS
28POLARSSL_ECP_DP_M221_ENABLED
29POLARSSL_ECP_DP_M383_ENABLED
30POLARSSL_ECP_DP_M511_ENABLED
31POLARSSL_NO_DEFAULT_ENTROPY_SOURCES
32POLARSSL_NO_PLATFORM_ENTROPY
33POLARSSL_SSL_HW_RECORD_ACCEL
Manuel Pégourié-Gonnard86b29082014-11-06 02:28:34 +010034POLARSSL_SSL_DISABLE_RENEGOTIATION
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020035POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3
36POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
37POLARSSL_ZLIB_SUPPORT
38POLARSSL_PKCS11_C
39_ALT\s*$
40);
41
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +010042# Things that should be enabled in "full" even if they match @excluded
43my @non_excluded = qw(
44PLATFORM_[A-Z0-9]+_ALT
45);
46
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020047my $config_file = "include/polarssl/config.h";
48
49# get -f option
50if (@ARGV >= 2 && $ARGV[0] eq "-f") {
51 shift; # -f
52 $config_file = shift;
53
54 -f $config_file or die "No such file: $config_file\n";
55} else {
56 if (! -f $config_file) {
57 chdir '..' or die;
58 -d $config_file
59 or die "Without -f, must be run from root or scripts\n"
60 }
61}
62
63# get action
64die $usage unless @ARGV;
65my $action = shift;
66
67my ($name, $value);
68if ($action eq "full") {
69 # nothing to do
70} elsif ($action eq "unset") {
71 die $usage unless @ARGV;
72 $name = shift;
73} elsif ($action eq "set") {
74 die $usage unless @ARGV;
75 $name = shift;
76 $value = shift if @ARGV;
77} else {
78 die $usage;
79}
80die $usage if @ARGV;
81
82open my $config_read, '<', $config_file or die "read $config_file: $!\n";
83my @config_lines = <$config_read>;
84close $config_read;
85
86my $exclude_re = join '|', @excluded;
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +010087my $no_exclude_re = join '|', @non_excluded;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020088
89open my $config_write, '>', $config_file or die "write $config_file: $!\n";
90
91my $done;
92for my $line (@config_lines) {
93 if ($action eq "full") {
94 if ($line =~ /name SECTION: Module configuration options/) {
95 $done = 1;
96 }
97
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +010098 if (!$done && $line =~ m!^//\s?#define! &&
99 ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200100 $line =~ s!^//!!;
101 }
102 } elsif ($action eq "unset") {
103 if (!$done && $line =~ /^\s*#define\s*$name/) {
104 $line = '//' . $line;
105 $done = 1;
106 }
107 } elsif (!$done && $action eq "set") {
108 if ($line =~ m!^(?://)?\s*#define\s*$name!) {
109 $line = "#define $name";
110 $line .= " $value" if defined $value && $value ne "";
111 $line .= "\n";
112 $done = 1;
113 }
114 }
115
116 print $config_write $line;
117}
118
119close $config_write;
120
121warn "configuration section not found" if ($action eq "full" && !$done);
122warn "$name not found" if ($action ne "full" && !$done);
123
124__END__