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 | 23983f3 | 2020-05-19 12:38:31 +0200 | [diff] [blame] | 116 | MBEDTLS_ECP_NO_INTERNAL_RNG |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | MBEDTLS_HAVE_SSE2 |
Hanno Becker | 307dfcd | 2019-02-26 13:51:00 +0000 | [diff] [blame] | 118 | MBEDTLS_MEMORY_BACKTRACE |
| 119 | MBEDTLS_MEMORY_BUFFER_ALLOC_C |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 120 | MBEDTLS_MEMORY_DEBUG |
| 121 | MBEDTLS_NO_64BIT_MULTIPLICATION |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES |
| 123 | MBEDTLS_NO_PLATFORM_ENTROPY |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 124 | MBEDTLS_NO_UDBL_DIVISION |
| 125 | MBEDTLS_PKCS11_C |
| 126 | MBEDTLS_PLATFORM_NO_STD_FUNCTIONS |
Andres Amaya Garcia | 5d8aade | 2018-10-30 18:21:41 +0000 | [diff] [blame] | 127 | MBEDTLS_REMOVE_3DES_CIPHERSUITES |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 128 | MBEDTLS_REMOVE_ARC4_CIPHERSUITES |
| 129 | MBEDTLS_RSA_NO_CRT |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | MBEDTLS_SSL_HW_RECORD_ACCEL |
Manuel Pégourié-Gonnard | a237722 | 2020-07-28 10:53:06 +0200 | [diff] [blame^] | 131 | MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 132 | MBEDTLS_TEST_NULL_ENTROPY |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION |
| 134 | MBEDTLS_ZLIB_SUPPORT |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 135 | _ALT\s*$ |
| 136 | ); |
| 137 | |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 138 | # Things that should be disabled in "baremetal" |
| 139 | my @excluded_baremetal = qw( |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 140 | MBEDTLS_ENTROPY_NV_SEED |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 141 | MBEDTLS_FS_IO |
| 142 | MBEDTLS_HAVEGE_C |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 143 | MBEDTLS_HAVE_TIME |
| 144 | MBEDTLS_HAVE_TIME_DATE |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 145 | MBEDTLS_MEMORY_BACKTRACE |
| 146 | MBEDTLS_MEMORY_BUFFER_ALLOC_C |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 147 | MBEDTLS_NET_C |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 148 | MBEDTLS_PLATFORM_FPRINTF_ALT |
Gilles Peskine | df4f7c1 | 2020-04-28 10:41:20 +0200 | [diff] [blame] | 149 | MBEDTLS_PLATFORM_NV_SEED_ALT |
Gilles Peskine | d6189e1 | 2020-04-28 10:28:14 +0200 | [diff] [blame] | 150 | MBEDTLS_PLATFORM_TIME_ALT |
| 151 | MBEDTLS_THREADING_C |
| 152 | MBEDTLS_THREADING_PTHREAD |
| 153 | MBEDTLS_TIMING_C |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 154 | ); |
| 155 | |
Gilles Peskine | df4f7c1 | 2020-04-28 10:41:20 +0200 | [diff] [blame] | 156 | # Things that should be enabled in "full" even if they match @excluded. |
| 157 | # Platform ALTs enable global variables that allow configuring the behavior |
| 158 | # but default to the default behavior, except for PLATFORM_SETUP_TEARDOWN_ALT |
| 159 | # which requires the application to provide relevant functions like |
| 160 | # non-platform ALTs. |
Manuel Pégourié-Gonnard | b752715 | 2015-06-03 09:59:06 +0100 | [diff] [blame] | 161 | my @non_excluded = qw( |
Gilles Peskine | df4f7c1 | 2020-04-28 10:41:20 +0200 | [diff] [blame] | 162 | PLATFORM_(?!SETUP_TEARDOWN_)[A-Z_0-9]+_ALT |
Manuel Pégourié-Gonnard | b752715 | 2015-06-03 09:59:06 +0100 | [diff] [blame] | 163 | ); |
| 164 | |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 165 | # Things that should be enabled in "baremetal" |
| 166 | my @non_excluded_baremetal = qw( |
| 167 | MBEDTLS_NO_PLATFORM_ENTROPY |
| 168 | ); |
| 169 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 170 | # Process the command line arguments |
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 $force_option = 0; |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 173 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 174 | my ($arg, $name, $value, $action); |
| 175 | |
SimonB | 73883c1 | 2016-04-04 13:49:10 +0100 | [diff] [blame] | 176 | while ($arg = shift) { |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 177 | |
| 178 | # Check if the argument is an option |
SimonB | 73883c1 | 2016-04-04 13:49:10 +0100 | [diff] [blame] | 179 | if ($arg eq "-f" || $arg eq "--file") { |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 180 | $config_file = shift; |
| 181 | |
| 182 | -f $config_file or die "No such file: $config_file\n"; |
| 183 | |
| 184 | } |
SimonB | 73883c1 | 2016-04-04 13:49:10 +0100 | [diff] [blame] | 185 | elsif ($arg eq "-o" || $arg eq "--force") { |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 186 | $force_option = 1; |
| 187 | |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | # ...else assume it's a command |
| 192 | $action = $arg; |
| 193 | |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 194 | if ($action eq "full" || $action eq "realfull" || $action eq "baremetal" ) { |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 195 | # No additional parameters |
| 196 | die $usage if @ARGV; |
| 197 | |
| 198 | } |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 199 | elsif ($action eq "unset" || $action eq "get") { |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 200 | die $usage unless @ARGV; |
| 201 | $name = shift; |
| 202 | |
| 203 | } |
| 204 | elsif ($action eq "set") { |
| 205 | die $usage unless @ARGV; |
| 206 | $name = shift; |
| 207 | $value = shift if @ARGV; |
| 208 | |
| 209 | } |
| 210 | else { |
| 211 | die "Command '$action' not recognised.\n\n".$usage; |
| 212 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Simon Butcher | 3d26513 | 2016-06-23 21:57:06 +0100 | [diff] [blame] | 216 | # If no command was specified, exit... |
| 217 | if ( not defined($action) ){ die $usage; } |
| 218 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 219 | # Check the config file is present |
| 220 | if (! -f $config_file) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 221 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 222 | chdir '..' or die; |
| 223 | |
| 224 | # Confirm this is the project root directory and try again |
| 225 | if ( !(-d 'scripts' && -d 'include' && -d 'library' && -f $config_file) ) { |
| 226 | die "If no file specified, must be run from the project root or scripts directory.\n"; |
| 227 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 228 | } |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 229 | |
| 230 | |
| 231 | # Now read the file and process the contents |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 232 | |
| 233 | open my $config_read, '<', $config_file or die "read $config_file: $!\n"; |
| 234 | my @config_lines = <$config_read>; |
| 235 | close $config_read; |
| 236 | |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 237 | # Add required baremetal symbols to the list that is included. |
| 238 | if ( $action eq "baremetal" ) { |
| 239 | @non_excluded = ( @non_excluded, @non_excluded_baremetal ); |
| 240 | } |
| 241 | |
| 242 | my ($exclude_re, $no_exclude_re, $exclude_baremetal_re); |
Manuel Pégourié-Gonnard | 1989caf | 2016-01-04 12:57:32 +0100 | [diff] [blame] | 243 | if ($action eq "realfull") { |
| 244 | $exclude_re = qr/^$/; |
| 245 | $no_exclude_re = qr/./; |
| 246 | } else { |
| 247 | $exclude_re = join '|', @excluded; |
| 248 | $no_exclude_re = join '|', @non_excluded; |
| 249 | } |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 250 | if ( $action eq "baremetal" ) { |
| 251 | $exclude_baremetal_re = join '|', @excluded_baremetal; |
| 252 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 253 | |
Gilles Peskine | 01f57e3 | 2017-10-09 16:54:28 +0200 | [diff] [blame] | 254 | my $config_write = undef; |
| 255 | if ($action ne "get") { |
| 256 | open $config_write, '>', $config_file or die "write $config_file: $!\n"; |
| 257 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 258 | |
| 259 | my $done; |
| 260 | for my $line (@config_lines) { |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 261 | if ($action eq "full" || $action eq "realfull" || $action eq "baremetal" ) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 262 | if ($line =~ /name SECTION: Module configuration options/) { |
| 263 | $done = 1; |
| 264 | } |
| 265 | |
Manuel Pégourié-Gonnard | b752715 | 2015-06-03 09:59:06 +0100 | [diff] [blame] | 266 | if (!$done && $line =~ m!^//\s?#define! && |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 267 | ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) && |
| 268 | ( $action ne "baremetal" || ( $line !~ /$exclude_baremetal_re/ ) ) ) { |
Manuel Pégourié-Gonnard | ea0920f | 2015-03-24 09:50:15 +0100 | [diff] [blame] | 269 | $line =~ s!^//\s?!!; |
| 270 | } |
Manuel Pégourié-Gonnard | 7ee5ddd | 2015-06-03 10:33:55 +0100 | [diff] [blame] | 271 | if (!$done && $line =~ m!^\s?#define! && |
Azim Khan | 0d44573 | 2017-12-21 09:28:39 +0000 | [diff] [blame] | 272 | ! ( ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) && |
| 273 | ( $action ne "baremetal" || ( $line !~ /$exclude_baremetal_re/ ) ) ) ) { |
Manuel Pégourié-Gonnard | ea0920f | 2015-03-24 09:50:15 +0100 | [diff] [blame] | 274 | $line =~ s!^!//!; |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 275 | } |
| 276 | } elsif ($action eq "unset") { |
Manuel Pégourié-Gonnard | 7f9049b | 2015-06-23 17:42:51 +0200 | [diff] [blame] | 277 | if (!$done && $line =~ /^\s*#define\s*$name\b/) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 278 | $line = '//' . $line; |
| 279 | $done = 1; |
| 280 | } |
| 281 | } elsif (!$done && $action eq "set") { |
Manuel Pégourié-Gonnard | 7f9049b | 2015-06-23 17:42:51 +0200 | [diff] [blame] | 282 | if ($line =~ m!^(?://)?\s*#define\s*$name\b!) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 283 | $line = "#define $name"; |
| 284 | $line .= " $value" if defined $value && $value ne ""; |
| 285 | $line .= "\n"; |
| 286 | $done = 1; |
| 287 | } |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 288 | } elsif (!$done && $action eq "get") { |
Gilles Peskine | f0f55cc | 2017-10-09 16:51:24 +0200 | [diff] [blame] | 289 | if ($line =~ /^\s*#define\s*$name(?:\s+(.*?))\s*(?:$|\/\*|\/\/)/) { |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 290 | $value = $1; |
| 291 | $done = 1; |
| 292 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Gilles Peskine | 01f57e3 | 2017-10-09 16:54:28 +0200 | [diff] [blame] | 295 | if (defined $config_write) { |
Gilles Peskine | 8ca0e8f | 2017-10-10 11:26:45 +0200 | [diff] [blame] | 296 | print $config_write $line or die "write $config_file: $!\n"; |
Gilles Peskine | 01f57e3 | 2017-10-09 16:54:28 +0200 | [diff] [blame] | 297 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 298 | } |
| 299 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 300 | # Did the set command work? |
Gilles Peskine | d98e9e8 | 2017-10-09 16:56:18 +0200 | [diff] [blame] | 301 | if ($action eq "set" && $force_option && !$done) { |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 302 | |
| 303 | # If the force option was set, append the symbol to the end of the file |
| 304 | my $line = "#define $name"; |
| 305 | $line .= " $value" if defined $value && $value ne ""; |
| 306 | $line .= "\n"; |
| 307 | $done = 1; |
| 308 | |
Gilles Peskine | 01f57e3 | 2017-10-09 16:54:28 +0200 | [diff] [blame] | 309 | print $config_write $line or die "write $config_file: $!\n"; |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Gilles Peskine | 01f57e3 | 2017-10-09 16:54:28 +0200 | [diff] [blame] | 312 | if (defined $config_write) { |
| 313 | close $config_write or die "close $config_file: $!\n"; |
| 314 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 315 | |
Simon Butcher | dd9895d | 2016-06-21 15:12:00 +0100 | [diff] [blame] | 316 | if ($action eq "get") { |
Gilles Peskine | d98e9e8 | 2017-10-09 16:56:18 +0200 | [diff] [blame] | 317 | if ($done) { |
Simon Butcher | dd9895d | 2016-06-21 15:12:00 +0100 | [diff] [blame] | 318 | if ($value ne '') { |
Gilles Peskine | d98e9e8 | 2017-10-09 16:56:18 +0200 | [diff] [blame] | 319 | print "$value\n"; |
Simon Butcher | dd9895d | 2016-06-21 15:12:00 +0100 | [diff] [blame] | 320 | } |
| 321 | exit 0; |
| 322 | } else { |
| 323 | # If the symbol was not found, return an error |
Gilles Peskine | d98e9e8 | 2017-10-09 16:56:18 +0200 | [diff] [blame] | 324 | exit 1; |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 325 | } |
Simon Butcher | 4ae8691 | 2016-06-21 10:09:25 +0100 | [diff] [blame] | 326 | } |
| 327 | |
SimonB | 60f2cf9 | 2016-04-03 14:16:08 +0100 | [diff] [blame] | 328 | if ($action eq "full" && !$done) { |
| 329 | die "Configuration section was not found in $config_file\n"; |
| 330 | |
| 331 | } |
| 332 | |
| 333 | if ($action ne "full" && $action ne "unset" && !$done) { |
| 334 | die "A #define for the symbol $name was not found in $config_file\n"; |
| 335 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 336 | |
| 337 | __END__ |