fbrosson | 533407a | 2018-04-04 21:44:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 2 | # |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 3 | # Copyright (c) 2014-2016, ARM Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 4 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 5 | # |
| 6 | # This file is provided under the Apache License 2.0, or the |
| 7 | # GNU General Public License v2.0 or later. |
| 8 | # |
| 9 | # ********** |
| 10 | # Apache License 2.0: |
Bence Szépkúti | 51b41d5 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 11 | # |
| 12 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 13 | # not use this file except in compliance with the License. |
| 14 | # You may obtain a copy of the License at |
| 15 | # |
| 16 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | # |
| 18 | # Unless required by applicable law or agreed to in writing, software |
| 19 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 20 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | # See the License for the specific language governing permissions and |
| 22 | # limitations under the License. |
| 23 | # |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 24 | # ********** |
| 25 | # |
| 26 | # ********** |
| 27 | # GNU General Public License v2.0 or later: |
| 28 | # |
| 29 | # This program is free software; you can redistribute it and/or modify |
| 30 | # it under the terms of the GNU General Public License as published by |
| 31 | # the Free Software Foundation; either version 2 of the License, or |
| 32 | # (at your option) any later version. |
| 33 | # |
| 34 | # This program is distributed in the hope that it will be useful, |
| 35 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 | # GNU General Public License for more details. |
| 38 | # |
| 39 | # You should have received a copy of the GNU General Public License along |
| 40 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 41 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 42 | # |
| 43 | # ********** |
| 44 | # |
Bence Szépkúti | 51b41d5 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 45 | # This file is part of Mbed TLS (https://tls.mbed.org) |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 46 | # |
| 47 | # Purpose |
| 48 | # |
| 49 | # Comments and uncomments #define lines in the given header file and optionally |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 50 | # sets their value or can get the value. This is to provide scripting control of |
| 51 | # what preprocessor symbols, and therefore what build time configuration flags |
| 52 | # are set in the 'config.h' file. |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 53 | # |
| 54 | # Usage: config.pl [-f <file> | --file <file>] [-o | --force] |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 55 | # [set <symbol> <value> | unset <symbol> | get <symbol> | |
| 56 | # full | realfull] |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 57 | # |
| 58 | # Full usage description provided below. |
| 59 | # |
Hanno Becker | 7c0f17d | 2017-09-28 11:49:46 +0100 | [diff] [blame] | 60 | # The following options are disabled instead of enabled with "full". |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 61 | # |
Gilles Peskine | 108ebf2 | 2020-04-28 10:26:43 +0200 | [diff] [blame] | 62 | # * Options that require additional build dependencies or unusual hardware. |
| 63 | # * Options that make testing less effective. |
| 64 | # * Options that are incompatible with other options, or more generally that |
| 65 | # interact with other parts of the code in such a way that a bulk enabling |
| 66 | # is not a good way to test them. |
| 67 | # * Options that remove features. |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 68 | # |
Gilles Peskine | 108ebf2 | 2020-04-28 10:26:43 +0200 | [diff] [blame] | 69 | # The baremetal configuration excludes options that require a library or |
| 70 | # operating system feature that is typically not present on bare metal |
| 71 | # systems. Features that are excluded from "full" won't be in "baremetal" |
| 72 | # either. |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 73 | |
| 74 | use warnings; |
| 75 | use strict; |
| 76 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 77 | my $config_file = "include/mbedtls/config.h"; |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 78 | my $usage = <<EOU; |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 79 | $0 [-f <file> | --file <file>] [-o | --force] |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 80 | [set <symbol> <value> | unset <symbol> | get <symbol> | |
Azim Khan | c3c3a68 | 2017-12-21 15:19:53 +0000 | [diff] [blame] | 81 | full | realfull | baremetal] |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 82 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 83 | Commands |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 84 | set <symbol> [<value>] - Uncomments or adds a #define for the <symbol> to |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 85 | the configuration file, and optionally making it |
| 86 | of <value>. |
| 87 | If the symbol isn't present in the file an error |
| 88 | is returned. |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 89 | unset <symbol> - Comments out the #define for the given symbol if |
| 90 | present in the configuration file. |
| 91 | get <symbol> - Finds the #define for the given symbol, returning |
Gilles Peskine | d98e9e8 | 2017-10-09 16:56:18 +0200 | [diff] [blame] | 92 | an exitcode of 0 if the symbol is found, and 1 if |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 93 | not. The value of the symbol is output if one is |
| 94 | specified in the configuration file. |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 95 | full - Uncomments all #define's in the configuration file |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 96 | excluding some reserved symbols, until the |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 97 | 'Module configuration options' section |
| 98 | realfull - Uncomments all #define's with no exclusions |
Azim Khan | c3c3a68 | 2017-12-21 15:19:53 +0000 | [diff] [blame] | 99 | baremetal - Sets full configuration suitable for baremetal build. |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 100 | |
| 101 | Options |
| 102 | -f | --file <filename> - The file or file path for the configuration file |
| 103 | to edit. When omitted, the following default is |
| 104 | used: |
| 105 | $config_file |
| 106 | -o | --force - If the symbol isn't present in the configuration |
Brian J Murray | 2adecba | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 107 | file when setting its value, a #define is |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 108 | appended to the end of the file. |
| 109 | |
| 110 | EOU |
| 111 | |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 112 | my @excluded = qw( |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 113 | MBEDTLS_CTR_DRBG_USE_128_BIT_KEY |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | MBEDTLS_DEPRECATED_REMOVED |
Gilles Peskine | 1e8cafc | 2020-04-29 15:44:00 +0200 | [diff] [blame] | 115 | MBEDTLS_DEPRECATED_WARNING |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | MBEDTLS_HAVE_SSE2 |
Hanno Becker | 307dfcd | 2019-02-26 13:51:00 +0000 | [diff] [blame] | 117 | MBEDTLS_MEMORY_BACKTRACE |
| 118 | MBEDTLS_MEMORY_BUFFER_ALLOC_C |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 119 | MBEDTLS_MEMORY_DEBUG |
| 120 | MBEDTLS_NO_64BIT_MULTIPLICATION |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES |
| 122 | MBEDTLS_NO_PLATFORM_ENTROPY |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 123 | MBEDTLS_NO_UDBL_DIVISION |
| 124 | MBEDTLS_PKCS11_C |
| 125 | MBEDTLS_PLATFORM_NO_STD_FUNCTIONS |
Andres Amaya Garcia | 5d8aade | 2018-10-30 18:21:41 +0000 | [diff] [blame] | 126 | MBEDTLS_REMOVE_3DES_CIPHERSUITES |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 127 | MBEDTLS_REMOVE_ARC4_CIPHERSUITES |
| 128 | MBEDTLS_RSA_NO_CRT |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | MBEDTLS_SSL_HW_RECORD_ACCEL |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 130 | MBEDTLS_TEST_NULL_ENTROPY |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION |
| 132 | MBEDTLS_ZLIB_SUPPORT |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 133 | _ALT\s*$ |
| 134 | ); |
| 135 | |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 136 | # Things that should be disabled in "baremetal" |
| 137 | my @excluded_baremetal = qw( |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 138 | MBEDTLS_ENTROPY_NV_SEED |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 139 | MBEDTLS_FS_IO |
| 140 | MBEDTLS_HAVEGE_C |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 141 | MBEDTLS_HAVE_TIME |
| 142 | MBEDTLS_HAVE_TIME_DATE |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 143 | MBEDTLS_MEMORY_BACKTRACE |
| 144 | MBEDTLS_MEMORY_BUFFER_ALLOC_C |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 145 | MBEDTLS_NET_C |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 146 | MBEDTLS_PLATFORM_FPRINTF_ALT |
Gilles Peskine | df4f7c1 | 2020-04-28 10:41:20 +0200 | [diff] [blame] | 147 | MBEDTLS_PLATFORM_NV_SEED_ALT |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 148 | MBEDTLS_PLATFORM_TIME_ALT |
| 149 | MBEDTLS_THREADING_C |
| 150 | MBEDTLS_THREADING_PTHREAD |
| 151 | MBEDTLS_TIMING_C |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 152 | ); |
| 153 | |
Gilles Peskine | df4f7c1 | 2020-04-28 10:41:20 +0200 | [diff] [blame] | 154 | # Things that should be enabled in "full" even if they match @excluded. |
| 155 | # Platform ALTs enable global variables that allow configuring the behavior |
| 156 | # but default to the default behavior, except for PLATFORM_SETUP_TEARDOWN_ALT |
| 157 | # which requires the application to provide relevant functions like |
| 158 | # non-platform ALTs. |
Manuel Pégourié-Gonnard | b752715 | 2015-06-03 09:59:06 +0100 | [diff] [blame] | 159 | my @non_excluded = qw( |
Gilles Peskine | df4f7c1 | 2020-04-28 10:41:20 +0200 | [diff] [blame] | 160 | PLATFORM_(?!SETUP_TEARDOWN_)[A-Z_0-9]+_ALT |
Manuel Pégourié-Gonnard | b752715 | 2015-06-03 09:59:06 +0100 | [diff] [blame] | 161 | ); |
| 162 | |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 163 | # Things that should be enabled in "baremetal" |
| 164 | my @non_excluded_baremetal = qw( |
| 165 | MBEDTLS_NO_PLATFORM_ENTROPY |
| 166 | ); |
| 167 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 168 | # Process the command line arguments |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 169 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 170 | my $force_option = 0; |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 171 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 172 | my ($arg, $name, $value, $action); |
| 173 | |
SimonB | 73883c1 | 2016-04-04 13:49:10 +0100 | [diff] [blame] | 174 | while ($arg = shift) { |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 175 | |
| 176 | # Check if the argument is an option |
SimonB | 73883c1 | 2016-04-04 13:49:10 +0100 | [diff] [blame] | 177 | if ($arg eq "-f" || $arg eq "--file") { |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 178 | $config_file = shift; |
| 179 | |
| 180 | -f $config_file or die "No such file: $config_file\n"; |
| 181 | |
| 182 | } |
SimonB | 73883c1 | 2016-04-04 13:49:10 +0100 | [diff] [blame] | 183 | elsif ($arg eq "-o" || $arg eq "--force") { |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 184 | $force_option = 1; |
| 185 | |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | # ...else assume it's a command |
| 190 | $action = $arg; |
| 191 | |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 192 | if ($action eq "full" || $action eq "realfull" || $action eq "baremetal" ) { |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 193 | # No additional parameters |
| 194 | die $usage if @ARGV; |
| 195 | |
| 196 | } |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 197 | elsif ($action eq "unset" || $action eq "get") { |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 198 | die $usage unless @ARGV; |
| 199 | $name = shift; |
| 200 | |
| 201 | } |
| 202 | elsif ($action eq "set") { |
| 203 | die $usage unless @ARGV; |
| 204 | $name = shift; |
| 205 | $value = shift if @ARGV; |
| 206 | |
| 207 | } |
| 208 | else { |
| 209 | die "Command '$action' not recognised.\n\n".$usage; |
| 210 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Simon Butcher | 3d26513 | 2016-06-23 21:57:06 +0100 | [diff] [blame] | 214 | # If no command was specified, exit... |
| 215 | if ( not defined($action) ){ die $usage; } |
| 216 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 217 | # Check the config file is present |
| 218 | if (! -f $config_file) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 219 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 220 | chdir '..' or die; |
| 221 | |
| 222 | # Confirm this is the project root directory and try again |
| 223 | if ( !(-d 'scripts' && -d 'include' && -d 'library' && -f $config_file) ) { |
| 224 | die "If no file specified, must be run from the project root or scripts directory.\n"; |
| 225 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 226 | } |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 227 | |
| 228 | |
| 229 | # Now read the file and process the contents |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 230 | |
| 231 | open my $config_read, '<', $config_file or die "read $config_file: $!\n"; |
| 232 | my @config_lines = <$config_read>; |
| 233 | close $config_read; |
| 234 | |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 235 | # Add required baremetal symbols to the list that is included. |
| 236 | if ( $action eq "baremetal" ) { |
| 237 | @non_excluded = ( @non_excluded, @non_excluded_baremetal ); |
| 238 | } |
| 239 | |
| 240 | my ($exclude_re, $no_exclude_re, $exclude_baremetal_re); |
Manuel Pégourié-Gonnard | 1989caf | 2016-01-04 12:57:32 +0100 | [diff] [blame] | 241 | if ($action eq "realfull") { |
| 242 | $exclude_re = qr/^$/; |
| 243 | $no_exclude_re = qr/./; |
| 244 | } else { |
| 245 | $exclude_re = join '|', @excluded; |
| 246 | $no_exclude_re = join '|', @non_excluded; |
| 247 | } |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 248 | if ( $action eq "baremetal" ) { |
| 249 | $exclude_baremetal_re = join '|', @excluded_baremetal; |
| 250 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 251 | |
Gilles Peskine | 01f57e3 | 2017-10-09 16:54:28 +0200 | [diff] [blame] | 252 | my $config_write = undef; |
| 253 | if ($action ne "get") { |
| 254 | open $config_write, '>', $config_file or die "write $config_file: $!\n"; |
| 255 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 256 | |
| 257 | my $done; |
| 258 | for my $line (@config_lines) { |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 259 | if ($action eq "full" || $action eq "realfull" || $action eq "baremetal" ) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 260 | if ($line =~ /name SECTION: Module configuration options/) { |
| 261 | $done = 1; |
| 262 | } |
| 263 | |
Manuel Pégourié-Gonnard | b752715 | 2015-06-03 09:59:06 +0100 | [diff] [blame] | 264 | if (!$done && $line =~ m!^//\s?#define! && |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 265 | ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) && |
| 266 | ( $action ne "baremetal" || ( $line !~ /$exclude_baremetal_re/ ) ) ) { |
Manuel Pégourié-Gonnard | ea0920f | 2015-03-24 09:50:15 +0100 | [diff] [blame] | 267 | $line =~ s!^//\s?!!; |
| 268 | } |
Manuel Pégourié-Gonnard | 7ee5ddd | 2015-06-03 10:33:55 +0100 | [diff] [blame] | 269 | if (!$done && $line =~ m!^\s?#define! && |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 270 | ! ( ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) && |
| 271 | ( $action ne "baremetal" || ( $line !~ /$exclude_baremetal_re/ ) ) ) ) { |
Manuel Pégourié-Gonnard | ea0920f | 2015-03-24 09:50:15 +0100 | [diff] [blame] | 272 | $line =~ s!^!//!; |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 273 | } |
| 274 | } elsif ($action eq "unset") { |
Manuel Pégourié-Gonnard | 7f9049b | 2015-06-23 17:42:51 +0200 | [diff] [blame] | 275 | if (!$done && $line =~ /^\s*#define\s*$name\b/) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 276 | $line = '//' . $line; |
| 277 | $done = 1; |
| 278 | } |
| 279 | } elsif (!$done && $action eq "set") { |
Manuel Pégourié-Gonnard | 7f9049b | 2015-06-23 17:42:51 +0200 | [diff] [blame] | 280 | if ($line =~ m!^(?://)?\s*#define\s*$name\b!) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 281 | $line = "#define $name"; |
| 282 | $line .= " $value" if defined $value && $value ne ""; |
| 283 | $line .= "\n"; |
| 284 | $done = 1; |
| 285 | } |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 286 | } elsif (!$done && $action eq "get") { |
Gilles Peskine | f0f55cc | 2017-10-09 16:51:24 +0200 | [diff] [blame] | 287 | if ($line =~ /^\s*#define\s*$name(?:\s+(.*?))\s*(?:$|\/\*|\/\/)/) { |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 288 | $value = $1; |
| 289 | $done = 1; |
| 290 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Gilles Peskine | 01f57e3 | 2017-10-09 16:54:28 +0200 | [diff] [blame] | 293 | if (defined $config_write) { |
Gilles Peskine | 8ca0e8f | 2017-10-10 11:26:45 +0200 | [diff] [blame] | 294 | print $config_write $line or die "write $config_file: $!\n"; |
Gilles Peskine | 01f57e3 | 2017-10-09 16:54:28 +0200 | [diff] [blame] | 295 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 296 | } |
| 297 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 298 | # Did the set command work? |
Gilles Peskine | d98e9e8 | 2017-10-09 16:56:18 +0200 | [diff] [blame] | 299 | if ($action eq "set" && $force_option && !$done) { |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 300 | |
| 301 | # If the force option was set, append the symbol to the end of the file |
| 302 | my $line = "#define $name"; |
| 303 | $line .= " $value" if defined $value && $value ne ""; |
| 304 | $line .= "\n"; |
| 305 | $done = 1; |
| 306 | |
Gilles Peskine | 01f57e3 | 2017-10-09 16:54:28 +0200 | [diff] [blame] | 307 | print $config_write $line or die "write $config_file: $!\n"; |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 308 | } |
| 309 | |
Gilles Peskine | 01f57e3 | 2017-10-09 16:54:28 +0200 | [diff] [blame] | 310 | if (defined $config_write) { |
| 311 | close $config_write or die "close $config_file: $!\n"; |
| 312 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 313 | |
Simon Butcher | dd9895d | 2016-06-21 15:12:00 +0100 | [diff] [blame] | 314 | if ($action eq "get") { |
Gilles Peskine | d98e9e8 | 2017-10-09 16:56:18 +0200 | [diff] [blame] | 315 | if ($done) { |
Simon Butcher | dd9895d | 2016-06-21 15:12:00 +0100 | [diff] [blame] | 316 | if ($value ne '') { |
Gilles Peskine | d98e9e8 | 2017-10-09 16:56:18 +0200 | [diff] [blame] | 317 | print "$value\n"; |
Simon Butcher | dd9895d | 2016-06-21 15:12:00 +0100 | [diff] [blame] | 318 | } |
| 319 | exit 0; |
| 320 | } else { |
| 321 | # If the symbol was not found, return an error |
Gilles Peskine | d98e9e8 | 2017-10-09 16:56:18 +0200 | [diff] [blame] | 322 | exit 1; |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 323 | } |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 324 | } |
| 325 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 326 | if ($action eq "full" && !$done) { |
| 327 | die "Configuration section was not found in $config_file\n"; |
| 328 | |
| 329 | } |
| 330 | |
| 331 | if ($action ne "full" && $action ne "unset" && !$done) { |
| 332 | die "A #define for the symbol $name was not found in $config_file\n"; |
| 333 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 334 | |
| 335 | __END__ |