blob: d04be59c833c8a2afe291ad7343469ac144e014f [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
30POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3
31POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
32POLARSSL_ZLIB_SUPPORT
33POLARSSL_PKCS11_C
34_ALT\s*$
35);
36
37my $config_file = "include/polarssl/config.h";
38
39# get -f option
40if (@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
54die $usage unless @ARGV;
55my $action = shift;
56
57my ($name, $value);
58if ($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}
70die $usage if @ARGV;
71
72open my $config_read, '<', $config_file or die "read $config_file: $!\n";
73my @config_lines = <$config_read>;
74close $config_read;
75
76my $exclude_re = join '|', @excluded;
77
78open my $config_write, '>', $config_file or die "write $config_file: $!\n";
79
80my $done;
81for 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
107close $config_write;
108
109warn "configuration section not found" if ($action eq "full" && !$done);
110warn "$name not found" if ($action ne "full" && !$done);
111
112__END__