Commit ab3d8627 authored by Manuel Pégourié-Gonnard's avatar Manuel Pégourié-Gonnard
Browse files

Make config.pl more versatile

parent 9dbe7c5f
#!/usr/bin/perl
# Tune the configuration file
use warnings;
use strict;
# Things that shouldn't be enabled.
my $usage = <<EOU;
$0 [-f <file>] full
$0 [-f <file>] unset <name>
$0 [-f <file>] set <name> [<value>]
EOU
# Things that shouldn't be enabled with "full".
# Notes:
# - POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3 and
# POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION could be enabled if the
......@@ -26,24 +34,40 @@ POLARSSL_PKCS11_C
_ALT\s*$
);
my $include_dir;
my $config_file = "include/polarssl/config.h";
if( @ARGV ) {
die "Invalid number of arguments" if scalar @ARGV != 1;
($include_dir) = @ARGV;
# get -f option
if (@ARGV >= 2 && $ARGV[0] eq "-f") {
shift; # -f
$config_file = shift;
-d $include_dir or die "No such directory: $include_dir\n";
-f $config_file or die "No such file: $config_file\n";
} else {
$include_dir = 'include/polarssl';
unless( -d $include_dir ) {
if (! -f $config_file) {
chdir '..' or die;
-d $include_dir
or die "Without arguments, must be run from root or scripts\n"
-d $config_file
or die "Without -f, must be run from root or scripts\n"
}
}
my $config_file = "$include_dir/config.h";
# get action
die $usage unless @ARGV;
my $action = shift;
my ($name, $value);
if ($action eq "full") {
# nothing to do
} elsif ($action eq "unset") {
die $usage unless @ARGV;
$name = shift;
} elsif ($action eq "set") {
die $usage unless @ARGV;
$name = shift;
$value = shift if @ARGV;
} else {
die $usage;
}
die $usage if @ARGV;
open my $config_read, '<', $config_file or die "read $config_file: $!\n";
my @config_lines = <$config_read>;
......@@ -55,12 +79,26 @@ open my $config_write, '>', $config_file or die "write $config_file: $!\n";
my $done;
for my $line (@config_lines) {
if ($line =~ /name SECTION: Module configuration options/) {
$done = 1;
}
if ($action eq "full") {
if ($line =~ /name SECTION: Module configuration options/) {
$done = 1;
}
if (!$done && $line =~ m!^//\s?#define! && $line !~ /$exclude_re/) {
$line =~ s!^//!!;
if (!$done && $line =~ m!^//\s?#define! && $line !~ /$exclude_re/) {
$line =~ s!^//!!;
}
} elsif ($action eq "unset") {
if (!$done && $line =~ /^\s*#define\s*$name/) {
$line = '//' . $line;
$done = 1;
}
} elsif (!$done && $action eq "set") {
if ($line =~ m!^(?://)?\s*#define\s*$name!) {
$line = "#define $name";
$line .= " $value" if defined $value && $value ne "";
$line .= "\n";
$done = 1;
}
}
print $config_write $line;
......@@ -68,4 +106,7 @@ for my $line (@config_lines) {
close $config_write;
warn "configuration section not found" if ($action eq "full" && !$done);
warn "$name not found" if ($action ne "full" && !$done);
__END__
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment