LCOV - code coverage report
Current view: top level - src/common - args.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 72.0 % 543 391
Test Date: 2026-05-21 06:55:05 Functions: 55.4 % 101 56
Branches: 42.9 % 982 421

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present The Bitcoin Core developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #include <common/args.h>
       7                 :             : 
       8                 :             : #include <chainparamsbase.h>
       9                 :             : #include <common/settings.h>
      10                 :             : #include <logging.h>
      11                 :             : #include <sync.h>
      12                 :             : #include <tinyformat.h>
      13                 :             : #include <univalue.h>
      14                 :             : #include <util/chaintype.h>
      15                 :             : #include <util/check.h>
      16                 :             : #include <util/fs.h>
      17                 :             : #include <util/fs_helpers.h>
      18                 :             : #include <util/strencodings.h>
      19                 :             : #include <util/string.h>
      20                 :             : 
      21                 :             : #ifdef WIN32
      22                 :             : #include <shlobj.h>
      23                 :             : #endif
      24                 :             : 
      25                 :             : #include <algorithm>
      26                 :             : #include <cassert>
      27                 :             : #include <cstdint>
      28                 :             : #include <cstdlib>
      29                 :             : #include <cstring>
      30                 :             : #include <map>
      31                 :             : #include <optional>
      32                 :             : #include <stdexcept>
      33                 :             : #include <string>
      34                 :             : #include <utility>
      35                 :             : #include <variant>
      36                 :             : 
      37                 :             : const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
      38                 :             : const char * const BITCOIN_SETTINGS_FILENAME = "settings.json";
      39                 :             : 
      40                 :             : ArgsManager gArgs;
      41                 :             : 
      42                 :             : /**
      43                 :             :  * Interpret a string argument as a boolean.
      44                 :             :  *
      45                 :             :  * The definition of LocaleIndependentAtoi<int>() requires that non-numeric string values
      46                 :             :  * like "foo", return 0. This means that if a user unintentionally supplies a
      47                 :             :  * non-integer argument here, the return value is always false. This means that
      48                 :             :  * -foo=false does what the user probably expects, but -foo=true is well defined
      49                 :             :  * but does not do what they probably expected.
      50                 :             :  *
      51                 :             :  * The return value of LocaleIndependentAtoi<int>(...) is zero when given input not
      52                 :             :  * representable as an int.
      53                 :             :  *
      54                 :             :  * For a more extensive discussion of this topic (and a wide range of opinions
      55                 :             :  * on the Right Way to change this code), see PR12713.
      56                 :             :  */
      57                 :        5933 : static bool InterpretBool(const std::string& strValue)
      58                 :             : {
      59         [ +  + ]:        5933 :     if (strValue.empty())
      60                 :             :         return true;
      61         [ -  + ]:        2185 :     return (LocaleIndependentAtoi<int>(strValue) != 0);
      62                 :             : }
      63                 :             : 
      64                 :     1153815 : static std::string SettingName(const std::string& arg)
      65                 :             : {
      66   [ -  +  +  +  :     1165543 :     return arg.size() > 0 && arg[0] == '-' ? arg.substr(1) : arg;
                   +  + ]
      67                 :             : }
      68                 :             : 
      69                 :             : /**
      70                 :             :  * Parse "name", "section.name", "noname", "section.noname" settings keys.
      71                 :             :  *
      72                 :             :  * @note Where an option was negated can be later checked using the
      73                 :             :  * IsArgNegated() method. One use case for this is to have a way to disable
      74                 :             :  * options that are not normally boolean (e.g. using -nodebuglogfile to request
      75                 :             :  * that debug log output is not sent to any file at all).
      76                 :             :  */
      77                 :       12411 : KeyInfo InterpretKey(std::string key)
      78                 :             : {
      79         [ +  + ]:       12411 :     KeyInfo result;
      80                 :             :     // Split section name from key name for keys like "testnet.foo" or "regtest.bar"
      81                 :       12411 :     size_t option_index = key.find('.');
      82         [ +  + ]:       12411 :     if (option_index != std::string::npos) {
      83         [ +  - ]:         556 :         result.section = key.substr(0, option_index);
      84         [ +  - ]:         556 :         key.erase(0, option_index + 1);
      85                 :             :     }
      86   [ -  +  +  + ]:       12411 :     if (key.starts_with("no")) {
      87         [ +  - ]:         983 :         key.erase(0, 2);
      88                 :         983 :         result.negated = true;
      89                 :             :     }
      90         [ +  - ]:       12411 :     result.name = key;
      91                 :       12411 :     return result;
      92                 :           0 : }
      93                 :             : 
      94                 :             : /**
      95                 :             :  * Interpret settings value based on registered flags.
      96                 :             :  *
      97                 :             :  * @param[in]   key      key information to know if key was negated
      98                 :             :  * @param[in]   value    string value of setting to be parsed
      99                 :             :  * @param[in]   flags    ArgsManager registered argument flags
     100                 :             :  * @param[out]  error    Error description if settings value is not valid
     101                 :             :  *
     102                 :             :  * @return parsed settings value if it is valid, otherwise nullopt accompanied
     103                 :             :  * by a descriptive error string
     104                 :             :  */
     105                 :       12193 : std::optional<common::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value,
     106                 :             :                                                   unsigned int flags, std::string& error)
     107                 :             : {
     108                 :             :     // Return negated settings as false values.
     109         [ +  + ]:       12193 :     if (key.negated) {
     110         [ -  + ]:         916 :         if (flags & ArgsManager::DISALLOW_NEGATION) {
     111                 :           0 :             error = strprintf("Negating of -%s is meaningless and therefore forbidden", key.name);
     112                 :           0 :             return std::nullopt;
     113                 :             :         }
     114                 :             :         // Double negatives like -nofoo=0 are supported (but discouraged)
     115   [ +  +  +  + ]:         916 :         if (value && !InterpretBool(*value)) {
     116                 :         540 :             LogWarning("Parsed potentially confusing double-negative -%s=%s", key.name, *value);
     117                 :         540 :             return true;
     118                 :             :         }
     119                 :         376 :         return false;
     120                 :             :     }
     121   [ +  +  +  + ]:       11277 :     if (!value && (flags & ArgsManager::DISALLOW_ELISION)) {
     122                 :           3 :         error = strprintf("Can not set -%s with no value. Please specify value with -%s=value.", key.name, key.name);
     123                 :           3 :         return std::nullopt;
     124                 :             :     }
     125         [ +  - ]:       11274 :     return value ? *value : "";
     126                 :             : }
     127                 :             : 
     128                 :             : // Define default constructor and destructor that are not inline, so code instantiating this class doesn't need to
     129                 :             : // #include class definitions for all members.
     130                 :             : // For example, m_settings has an internal dependency on univalue.
     131                 :        2030 : ArgsManager::ArgsManager() = default;
     132         [ -  + ]:       10150 : ArgsManager::~ArgsManager() = default;
     133                 :             : 
     134                 :        1797 : std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const
     135                 :             : {
     136         [ +  - ]:        1797 :     std::set<std::string> unsuitables;
     137                 :             : 
     138         [ +  - ]:        1797 :     LOCK(cs_args);
     139                 :             : 
     140                 :             :     // if there's no section selected, don't worry
     141         [ +  + ]:        1797 :     if (m_network.empty()) return std::set<std::string> {};
     142                 :             : 
     143                 :             :     // if it's okay to use the default section for this network, don't worry
     144   [ +  -  +  + ]:        1421 :     if (m_network == ChainTypeToString(ChainType::MAIN)) return std::set<std::string> {};
     145                 :             : 
     146         [ +  + ]:       11329 :     for (const auto& arg : m_network_only_args) {
     147   [ +  -  +  -  :        9914 :         if (OnlyHasDefaultSectionSetting(m_settings, m_network, SettingName(arg))) {
                   -  + ]
     148         [ #  # ]:           0 :             unsuitables.insert(arg);
     149                 :             :         }
     150                 :             :     }
     151                 :        1415 :     return unsuitables;
     152                 :        1797 : }
     153                 :             : 
     154                 :        1797 : std::list<SectionInfo> ArgsManager::GetUnrecognizedSections() const
     155                 :             : {
     156                 :             :     // Section names to be recognized in the config file.
     157                 :        1797 :     static const std::set<std::string> available_sections{
     158                 :             :         ChainTypeToString(ChainType::REGTEST),
     159                 :             :         ChainTypeToString(ChainType::SIGNET),
     160                 :             :         ChainTypeToString(ChainType::TESTNET),
     161                 :             :         ChainTypeToString(ChainType::TESTNET4),
     162                 :             :         ChainTypeToString(ChainType::MAIN),
     163   [ +  +  +  -  :        2331 :     };
          -  +  +  +  -  
                      - ]
     164                 :             : 
     165                 :        1797 :     LOCK(cs_args);
     166         [ +  - ]:        1797 :     std::list<SectionInfo> unrecognized = m_config_sections;
     167                 :        1797 :     unrecognized.remove_if([](const SectionInfo& appeared){ return available_sections.contains(appeared.m_name); });
     168         [ +  - ]:        1797 :     return unrecognized;
     169   [ +  -  +  -  :        1886 : }
          +  -  +  -  +  
                -  -  - ]
     170                 :             : 
     171                 :       21774 : void ArgsManager::SelectConfigNetwork(const std::string& network)
     172                 :             : {
     173                 :       21774 :     LOCK(cs_args);
     174   [ +  -  +  - ]:       43548 :     m_network = network;
     175                 :       21774 : }
     176                 :             : 
     177                 :        3994 : bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::string& error)
     178                 :             : {
     179                 :        3994 :     LOCK(cs_args);
     180                 :        3994 :     m_settings.command_line_options.clear();
     181                 :             : 
     182         [ +  + ]:       16184 :     for (int i = 1; i < argc; i++) {
     183         [ +  - ]:       14642 :         std::string key(argv[i]);
     184                 :             : 
     185                 :             : #ifdef __APPLE__
     186                 :             :         // At the first time when a user gets the "App downloaded from the
     187                 :             :         // internet" warning, and clicks the Open button, macOS passes
     188                 :             :         // a unique process serial number (PSN) as -psn_... command-line
     189                 :             :         // argument, which we filter out.
     190                 :             :         if (key.starts_with("-psn_")) continue;
     191                 :             : #endif
     192                 :             : 
     193         [ +  + ]:       14642 :         if (key == "-") break; //bitcoin-tx using stdin
     194                 :       14613 :         std::optional<std::string> val;
     195                 :       14613 :         size_t is_index = key.find('=');
     196         [ +  + ]:       14613 :         if (is_index != std::string::npos) {
     197         [ +  - ]:        6871 :             val = key.substr(is_index + 1);
     198         [ +  - ]:        6871 :             key.erase(is_index);
     199                 :             :         }
     200                 :             : #ifdef WIN32
     201                 :             :         key = ToLower(key);
     202                 :             :         if (key[0] == '/')
     203                 :             :             key[0] = '-';
     204                 :             : #endif
     205                 :             : 
     206         [ +  + ]:       14613 :         if (key[0] != '-') {
     207   [ +  +  +  + ]:        2202 :             if (!m_accept_any_command && m_command.empty()) {
     208                 :             :                 // The first non-dash arg is a registered command
     209         [ +  - ]:          82 :                 std::optional<unsigned int> flags = GetArgFlags_(key);
     210   [ +  +  +  - ]:          82 :                 if (!flags || !(*flags & ArgsManager::COMMAND)) {
     211         [ +  - ]:          82 :                     error = strprintf("Invalid command '%s'", argv[i]);
     212                 :          82 :                     return false;
     213                 :             :                 }
     214                 :             :             }
     215         [ +  - ]:        2120 :             m_command.push_back(key);
     216         [ +  + ]:        8022 :             while (++i < argc) {
     217                 :             :                 // The remaining args are command args
     218         [ +  - ]:        5902 :                 m_command.emplace_back(argv[i]);
     219                 :             :             }
     220                 :        2120 :             break;
     221                 :             :         }
     222                 :             : 
     223                 :             :         // Transform --foo to -foo
     224   [ -  +  +  +  :       12411 :         if (key.length() > 1 && key[1] == '-')
                   +  + ]
     225         [ +  - ]:           7 :             key.erase(0, 1);
     226                 :             : 
     227                 :             :         // Transform -foo to foo
     228         [ +  - ]:       12411 :         key.erase(0, 1);
     229   [ -  +  +  - ]:       24822 :         KeyInfo keyinfo = InterpretKey(key);
     230   [ +  -  +  - ]:       12411 :         std::optional<unsigned int> flags = GetArgFlags_('-' + keyinfo.name);
     231                 :             : 
     232                 :             :         // Unknown command line options and command line options with dot
     233                 :             :         // characters (which are returned from InterpretKey with nonempty
     234                 :             :         // section strings) are not valid.
     235   [ +  +  +  + ]:       12411 :         if (!flags || !keyinfo.section.empty()) {
     236         [ +  - ]:         218 :             error = strprintf("Invalid parameter %s", argv[i]);
     237                 :         218 :             return false;
     238                 :             :         }
     239                 :             : 
     240   [ +  +  +  - ]:       17696 :         std::optional<common::SettingsValue> value = InterpretValue(keyinfo, val ? &*val : nullptr, *flags, error);
     241         [ +  + ]:       12193 :         if (!value) return false;
     242                 :             : 
     243   [ +  -  +  - ]:       12190 :         m_settings.command_line_options[keyinfo.name].push_back(*value);
     244                 :       29176 :     }
     245                 :             : 
     246                 :             :     // we do not allow -includeconf from command line, only -noincludeconf
     247   [ +  -  +  + ]:        3691 :     if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) {
     248                 :           5 :         const common::SettingsSpan values{*includes};
     249                 :             :         // Range may be empty if -noincludeconf was passed
     250   [ +  -  +  - ]:           5 :         if (!values.empty()) {
     251   [ +  -  +  -  :           5 :             error = "-includeconf cannot be used from commandline; -includeconf=" + values.begin()->write();
                   +  - ]
     252                 :           5 :             return false; // pick first value as example
     253                 :             :         }
     254                 :             :     }
     255                 :             :     return true;
     256                 :        3994 : }
     257                 :             : 
     258                 :       33276 : std::optional<unsigned int> ArgsManager::GetArgFlags_(const std::string& name) const
     259                 :             : {
     260                 :       33276 :     AssertLockHeld(cs_args);
     261         [ +  + ]:      102162 :     for (const auto& arg_map : m_available_args) {
     262                 :       97752 :         const auto search = arg_map.second.find(name);
     263         [ +  + ]:       97752 :         if (search != arg_map.second.end()) {
     264                 :       28866 :             return search->second.m_flags;
     265                 :             :         }
     266                 :             :     }
     267                 :        4410 :     return m_default_flags;
     268                 :             : }
     269                 :             : 
     270                 :       20783 : std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) const
     271                 :             : {
     272                 :       20783 :     LOCK(cs_args);
     273         [ +  - ]:       20783 :     return GetArgFlags_(name);
     274                 :       20783 : }
     275                 :             : 
     276                 :           0 : void ArgsManager::SetDefaultFlags(std::optional<unsigned int> flags)
     277                 :             : {
     278                 :           0 :     LOCK(cs_args);
     279         [ #  # ]:           0 :     m_default_flags = flags;
     280                 :           0 : }
     281                 :             : 
     282                 :        6058 : fs::path ArgsManager::GetPathArg_(std::string arg, const fs::path& default_value) const
     283                 :             : {
     284                 :        6058 :     AssertLockHeld(cs_args);
     285                 :        6058 :     const auto value = GetSetting_(arg);
     286         [ +  + ]:        6058 :     if (value.isFalse()) return {};
     287   [ +  -  +  - ]:        5968 :     std::string path_str = SettingToString(value, "");
     288   [ +  +  +  - ]:       10790 :     if (path_str.empty()) return default_value;
     289   [ +  -  +  - ]:       14466 :     fs::path result = fs::PathFromString(path_str).lexically_normal();
     290                 :             :     // Remove trailing slash, if present.
     291   [ +  -  +  -  :       14466 :     return result.has_filename() ? result : result.parent_path();
                   -  - ]
     292                 :       10880 : }
     293                 :             : 
     294                 :        1236 : fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) const
     295                 :             : {
     296                 :        1236 :     LOCK(cs_args);
     297   [ +  -  +  - ]:        2472 :     return GetPathArg_(std::move(arg), default_value);
     298                 :        1236 : }
     299                 :             : 
     300                 :        5326 : fs::path ArgsManager::GetBlocksDirPath() const
     301                 :             : {
     302                 :        5326 :     LOCK(cs_args);
     303                 :        5326 :     fs::path& path = m_cached_blocks_path;
     304                 :             : 
     305                 :             :     // Cache the path to avoid calling fs::create_directories on every call of
     306                 :             :     // this function
     307   [ +  +  +  - ]:        5326 :     if (!path.empty()) return path;
     308                 :             : 
     309   [ +  -  +  -  :        2411 :     if (!GetSetting_("-blocksdir").isNull()) {
                   -  + ]
     310   [ #  #  #  #  :           0 :         path = fs::absolute(GetPathArg_("-blocksdir"));
                   #  # ]
     311   [ #  #  #  # ]:           0 :         if (!fs::is_directory(path)) {
     312         [ #  # ]:           0 :             path = "";
     313         [ #  # ]:           0 :             return path;
     314                 :             :         }
     315                 :             :     } else {
     316         [ +  - ]:        7233 :         path = GetDataDir(/*net_specific=*/false);
     317                 :             :     }
     318                 :             : 
     319   [ +  -  +  - ]:        4822 :     path /= fs::PathFromString(BaseParams().DataDir());
     320         [ +  - ]:        2411 :     path /= "blocks";
     321         [ +  - ]:        2411 :     fs::create_directories(path);
     322   [ +  -  +  - ]:        5326 :     return path;
     323                 :        5326 : }
     324                 :             : 
     325                 :          25 : fs::path ArgsManager::GetDataDirBase() const {
     326                 :          25 :     LOCK(cs_args);
     327         [ +  - ]:          25 :     return GetDataDir(/*net_specific=*/false);
     328                 :          25 : }
     329                 :             : 
     330                 :       30841 : fs::path ArgsManager::GetDataDirNet() const {
     331                 :       30841 :     LOCK(cs_args);
     332         [ +  - ]:       30841 :     return GetDataDir(/*net_specific=*/true);
     333                 :       30841 : }
     334                 :             : 
     335                 :       33277 : fs::path ArgsManager::GetDataDir(bool net_specific) const
     336                 :             : {
     337                 :       33277 :     AssertLockHeld(cs_args);
     338         [ +  + ]:       33277 :     fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;
     339                 :             : 
     340                 :             :     // Used cached path if available
     341         [ +  + ]:       33277 :     if (!path.empty()) return path;
     342                 :             : 
     343   [ +  -  +  - ]:        9644 :     const fs::path datadir{GetPathArg_("-datadir")};
     344         [ +  - ]:        4822 :     if (!datadir.empty()) {
     345         [ +  - ]:        9644 :         path = fs::absolute(datadir);
     346   [ +  -  -  + ]:        4822 :         if (!fs::is_directory(path)) {
     347         [ #  # ]:           0 :             path = "";
     348         [ #  # ]:           0 :             return path;
     349                 :             :         }
     350                 :             :     } else {
     351         [ #  # ]:           0 :         path = GetDefaultDataDir();
     352                 :             :     }
     353                 :             : 
     354   [ +  +  +  -  :        4822 :     if (net_specific && !BaseParams().DataDir().empty()) {
                   +  + ]
     355   [ +  -  +  - ]:        7212 :         path /= fs::PathFromString(BaseParams().DataDir());
     356                 :             :     }
     357                 :             : 
     358         [ +  - ]:        4822 :     return path;
     359                 :        4822 : }
     360                 :             : 
     361                 :        1236 : void ArgsManager::ClearPathCache()
     362                 :             : {
     363                 :        1236 :     LOCK(cs_args);
     364                 :             : 
     365                 :        1236 :     m_cached_datadir_path = fs::path();
     366                 :        1236 :     m_cached_network_datadir_path = fs::path();
     367         [ +  - ]:        2472 :     m_cached_blocks_path = fs::path();
     368                 :        1236 : }
     369                 :             : 
     370                 :         561 : std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const
     371                 :             : {
     372         [ +  - ]:         561 :     Command ret;
     373         [ +  - ]:         561 :     LOCK(cs_args);
     374         [ +  + ]:         561 :     auto it = m_command.begin();
     375         [ +  + ]:         561 :     if (it == m_command.end()) {
     376                 :             :         // No command was passed
     377                 :         444 :         return std::nullopt;
     378                 :             :     }
     379         [ +  + ]:         117 :     if (!m_accept_any_command) {
     380                 :             :         // The registered command
     381         [ +  - ]:          16 :         ret.command = *(it++);
     382                 :             :     }
     383         [ +  + ]:        8123 :     while (it != m_command.end()) {
     384                 :             :         // The unregistered command and args (if any)
     385         [ +  - ]:        8006 :         ret.args.push_back(*(it++));
     386                 :             :     }
     387                 :         117 :     return ret;
     388                 :        1122 : }
     389                 :             : 
     390                 :         117 : bool ArgsManager::CheckCommandOptions(const std::string& command, std::vector<std::string>* errors) const
     391                 :             : {
     392                 :         117 :     LOCK(cs_args);
     393                 :             : 
     394                 :         117 :     auto command_options = m_available_args.find(OptionsCategory::COMMAND_OPTIONS);
     395         [ -  + ]:         117 :     if (command_options == m_available_args.end()) {
     396                 :             :         // There are no command-specific options at all, so everything is fine
     397                 :             :         return true;
     398                 :             :     }
     399                 :             : 
     400                 :           0 :     const auto command_args = m_command_args.find(command);
     401                 :           0 :     auto is_valid_opt = [&](const auto& opt) EXCLUSIVE_LOCKS_REQUIRED(cs_args) -> bool {
     402         [ #  # ]:           0 :         if (command_args == m_command_args.end()) {
     403                 :             :             // Caller may not have checked that command actually exists
     404                 :             :             // before calling this function.  In that case, treat it as
     405                 :             :             // having no valid command-specific options.
     406                 :             :             return false;
     407                 :             :         } else {
     408                 :           0 :             return command_args->second.contains(opt);
     409                 :             :         }
     410                 :           0 :     };
     411                 :             : 
     412                 :           0 :     bool ok = true;
     413   [ #  #  #  # ]:           0 :     for (const auto& [arg, _] : command_options->second) {
     414   [ #  #  #  #  :           0 :         if (!GetSetting_(arg).isNull() && !is_valid_opt(arg)) {
             #  #  #  # ]
     415                 :           0 :             ok = false;
     416         [ #  # ]:           0 :             if (errors != nullptr) {
     417   [ #  #  #  # ]:           0 :                 errors->emplace_back(strprintf("The %s option cannot be used with the '%s' command.", arg, command));
     418                 :             :             }
     419                 :             :         }
     420                 :             :     }
     421                 :             :     return ok;
     422                 :         117 : }
     423                 :             : 
     424                 :      210551 : std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
     425                 :             : {
     426                 :      210551 :     std::vector<std::string> result;
     427   [ +  -  +  + ]:      216693 :     for (const common::SettingsValue& value : GetSettingsList(strArg)) {
     428   [ -  +  -  -  :       18426 :         result.push_back(value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str());
          -  +  -  -  +  
             -  -  +  +  
                      - ]
     429                 :             :     }
     430                 :      210551 :     return result;
     431                 :           0 : }
     432                 :             : 
     433                 :       13336 : bool ArgsManager::IsArgSet(const std::string& strArg) const
     434                 :             : {
     435                 :       13336 :     return !GetSetting(strArg).isNull();
     436                 :             : }
     437                 :             : 
     438                 :           0 : bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp, bool backup) const
     439                 :             : {
     440   [ #  #  #  # ]:           0 :     fs::path settings = GetPathArg("-settings", BITCOIN_SETTINGS_FILENAME);
     441         [ #  # ]:           0 :     if (settings.empty()) {
     442                 :             :         return false;
     443                 :             :     }
     444         [ #  # ]:           0 :     if (backup) {
     445         [ #  # ]:           0 :         settings += ".bak";
     446                 :             :     }
     447         [ #  # ]:           0 :     if (filepath) {
     448   [ #  #  #  #  :           0 :         *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings);
          #  #  #  #  #  
                #  #  # ]
     449                 :             :     }
     450                 :             :     return true;
     451                 :           0 : }
     452                 :             : 
     453                 :           0 : static void SaveErrors(const std::vector<std::string> errors, std::vector<std::string>* error_out)
     454                 :             : {
     455         [ #  # ]:           0 :     for (const auto& error : errors) {
     456         [ #  # ]:           0 :         if (error_out) {
     457                 :           0 :             error_out->emplace_back(error);
     458                 :             :         } else {
     459                 :           0 :             LogWarning("%s", error);
     460                 :             :         }
     461                 :             :     }
     462                 :           0 : }
     463                 :             : 
     464                 :           0 : bool ArgsManager::ReadSettingsFile(std::vector<std::string>* errors)
     465                 :             : {
     466                 :           0 :     fs::path path;
     467   [ #  #  #  # ]:           0 :     if (!GetSettingsPath(&path, /* temp= */ false)) {
     468                 :             :         return true; // Do nothing if settings file disabled.
     469                 :             :     }
     470                 :             : 
     471         [ #  # ]:           0 :     LOCK(cs_args);
     472                 :           0 :     m_settings.rw_settings.clear();
     473                 :           0 :     std::vector<std::string> read_errors;
     474   [ #  #  #  # ]:           0 :     if (!common::ReadSettings(path, m_settings.rw_settings, read_errors)) {
     475   [ #  #  #  # ]:           0 :         SaveErrors(read_errors, errors);
     476                 :           0 :         return false;
     477                 :             :     }
     478         [ #  # ]:           0 :     for (const auto& setting : m_settings.rw_settings) {
     479   [ #  #  #  # ]:           0 :         KeyInfo key = InterpretKey(setting.first); // Split setting key into section and argname
     480   [ #  #  #  #  :           0 :         if (!GetArgFlags_('-' + key.name)) {
                   #  # ]
     481         [ #  # ]:           0 :             LogWarning("Ignoring unknown rw_settings value %s", setting.first);
     482                 :             :         }
     483                 :           0 :     }
     484                 :             :     return true;
     485         [ #  # ]:           0 : }
     486                 :             : 
     487                 :           0 : bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors, bool backup) const
     488                 :             : {
     489                 :           0 :     fs::path path, path_tmp;
     490   [ #  #  #  #  :           0 :     if (!GetSettingsPath(&path, /*temp=*/false, backup) || !GetSettingsPath(&path_tmp, /*temp=*/true, backup)) {
             #  #  #  # ]
     491         [ #  # ]:           0 :         throw std::logic_error("Attempt to write settings file when dynamic settings are disabled.");
     492                 :             :     }
     493                 :             : 
     494         [ #  # ]:           0 :     LOCK(cs_args);
     495                 :           0 :     std::vector<std::string> write_errors;
     496   [ #  #  #  # ]:           0 :     if (!common::WriteSettings(path_tmp, m_settings.rw_settings, write_errors)) {
     497   [ #  #  #  # ]:           0 :         SaveErrors(write_errors, errors);
     498                 :           0 :         return false;
     499                 :             :     }
     500   [ #  #  #  #  :           0 :     if (!RenameOver(path_tmp, path)) {
             #  #  #  # ]
     501   [ #  #  #  #  :           0 :         SaveErrors({strprintf("Failed renaming settings file %s to %s\n", fs::PathToString(path_tmp), fs::PathToString(path))}, errors);
          #  #  #  #  #  
                      # ]
     502                 :           0 :         return false;
     503                 :             :     }
     504                 :             :     return true;
     505   [ #  #  #  #  :           0 : }
                   #  # ]
     506                 :             : 
     507                 :           0 : common::SettingsValue ArgsManager::GetPersistentSetting(const std::string& name) const
     508                 :             : {
     509                 :           0 :     LOCK(cs_args);
     510   [ #  #  #  #  :           0 :     return common::GetSetting(m_settings, m_network, name, !UseDefaultSection("-" + name),
                   #  # ]
     511         [ #  # ]:           0 :         /*ignore_nonpersistent=*/true, /*get_chain_type=*/false);
     512                 :           0 : }
     513                 :             : 
     514                 :        1797 : bool ArgsManager::IsArgNegated(const std::string& strArg) const
     515                 :             : {
     516                 :        1797 :     return GetSetting(strArg).isFalse();
     517                 :             : }
     518                 :             : 
     519                 :       15194 : std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
     520                 :             : {
     521         [ +  - ]:       30388 :     return GetArg(strArg).value_or(strDefault);
     522                 :             : }
     523                 :             : 
     524                 :       84258 : std::optional<std::string> ArgsManager::GetArg(const std::string& strArg) const
     525                 :             : {
     526                 :       84258 :     const common::SettingsValue value = GetSetting(strArg);
     527         [ +  - ]:      168516 :     return SettingToString(value);
     528                 :       84258 : }
     529                 :             : 
     530                 :       90226 : std::optional<std::string> SettingToString(const common::SettingsValue& value)
     531                 :             : {
     532         [ +  + ]:       90226 :     if (value.isNull()) return std::nullopt;
     533         [ -  + ]:        4958 :     if (value.isFalse()) return "0";
     534         [ -  + ]:        4958 :     if (value.isTrue()) return "1";
     535   [ -  +  -  - ]:        4958 :     if (value.isNum()) return value.getValStr();
     536         [ -  + ]:        9916 :     return value.get_str();
     537                 :             : }
     538                 :             : 
     539                 :        5968 : std::string SettingToString(const common::SettingsValue& value, const std::string& strDefault)
     540                 :             : {
     541         [ +  - ]:       11936 :     return SettingToString(value).value_or(strDefault);
     542                 :             : }
     543                 :             : 
     544                 :             : template <std::integral Int>
     545                 :      562883 : Int ArgsManager::GetArg(const std::string& strArg, Int nDefault) const
     546                 :             : {
     547         [ +  + ]:      562883 :     return GetArg<Int>(strArg).value_or(nDefault);
     548                 :             : }
     549                 :             : 
     550                 :             : template <std::integral Int>
     551                 :      635059 : std::optional<Int> ArgsManager::GetArg(const std::string& strArg) const
     552                 :             : {
     553                 :      635059 :     const common::SettingsValue value = GetSetting(strArg);
     554         [ +  - ]:     1270118 :     return SettingTo<Int>(value);
     555                 :      635059 : }
     556                 :             : 
     557                 :             : template <std::integral Int>
     558         [ +  + ]:      635059 : std::optional<Int> SettingTo(const common::SettingsValue& value)
     559                 :             : {
     560         [ +  + ]:      635059 :     if (value.isNull()) return std::nullopt;
     561         [ -  + ]:       31104 :     if (value.isFalse()) return 0;
     562         [ -  + ]:       31104 :     if (value.isTrue()) return 1;
     563         [ -  + ]:       31104 :     if (value.isNum()) return value.getInt<Int>();
     564         [ -  + ]:       31104 :     return LocaleIndependentAtoi<Int>(value.get_str());
     565                 :             : }
     566                 :             : 
     567                 :             : template <std::integral Int>
     568                 :           0 : Int SettingTo(const common::SettingsValue& value, Int nDefault)
     569                 :             : {
     570         [ #  # ]:           0 :     return SettingTo<Int>(value).value_or(nDefault);
     571                 :             : }
     572                 :             : 
     573                 :      125481 : bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
     574                 :             : {
     575         [ +  + ]:      125481 :     return GetBoolArg(strArg).value_or(fDefault);
     576                 :             : }
     577                 :             : 
     578                 :      133011 : std::optional<bool> ArgsManager::GetBoolArg(const std::string& strArg) const
     579                 :             : {
     580                 :      133011 :     const common::SettingsValue value = GetSetting(strArg);
     581         [ +  - ]:      266022 :     return SettingToBool(value);
     582                 :      133011 : }
     583                 :             : 
     584                 :      133011 : std::optional<bool> SettingToBool(const common::SettingsValue& value)
     585                 :             : {
     586         [ +  + ]:      133011 :     if (value.isNull()) return std::nullopt;
     587         [ -  + ]:        5371 :     if (value.isBool()) return value.get_bool();
     588                 :        5371 :     return InterpretBool(value.get_str());
     589                 :             : }
     590                 :             : 
     591                 :           0 : bool SettingToBool(const common::SettingsValue& value, bool fDefault)
     592                 :             : {
     593         [ #  # ]:           0 :     return SettingToBool(value).value_or(fDefault);
     594                 :             : }
     595                 :             : 
     596                 :             : #define INSTANTIATE_INT_TYPE(Type)                                                    \
     597                 :             :     template Type ArgsManager::GetArg<Type>(const std::string&, Type) const;          \
     598                 :             :     template std::optional<Type> ArgsManager::GetArg<Type>(const std::string&) const; \
     599                 :             :     template Type SettingTo<Type>(const common::SettingsValue&, Type);                \
     600                 :             :     template std::optional<Type> SettingTo<Type>(const common::SettingsValue&)
     601                 :             : 
     602                 :             : INSTANTIATE_INT_TYPE(int8_t);
     603                 :             : INSTANTIATE_INT_TYPE(uint8_t);
     604                 :             : INSTANTIATE_INT_TYPE(int16_t);
     605                 :             : INSTANTIATE_INT_TYPE(uint16_t);
     606                 :             : INSTANTIATE_INT_TYPE(int32_t);
     607                 :             : INSTANTIATE_INT_TYPE(uint32_t);
     608                 :             : INSTANTIATE_INT_TYPE(int64_t);
     609                 :             : INSTANTIATE_INT_TYPE(uint64_t);
     610                 :             : 
     611                 :             : #undef INSTANTIATE_INT_TYPE
     612                 :             : 
     613                 :        8105 : bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
     614                 :             : {
     615                 :        8105 :     LOCK(cs_args);
     616   [ +  -  +  + ]:        8105 :     if (!GetSetting_(strArg).isNull()) return false;
     617   [ +  -  +  -  :        2162 :     m_settings.forced_settings[SettingName(strArg)] = strValue;
                   +  - ]
     618                 :        2162 :     return true;
     619                 :        8105 : }
     620                 :             : 
     621                 :        6441 : bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)
     622                 :             : {
     623         [ +  + ]:        6441 :     if (fValue)
     624         [ +  - ]:        6385 :         return SoftSetArg(strArg, std::string("1"));
     625                 :             :     else
     626         [ +  - ]:          56 :         return SoftSetArg(strArg, std::string("0"));
     627                 :             : }
     628                 :             : 
     629                 :       39965 : void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
     630                 :             : {
     631                 :       39965 :     LOCK(cs_args);
     632   [ +  -  +  -  :       39965 :     m_settings.forced_settings[SettingName(strArg)] = strValue;
             +  -  +  - ]
     633                 :       39965 : }
     634                 :             : 
     635                 :        1070 : void ArgsManager::AddCommand(const std::string& cmd, const std::string& help, std::set<std::string> options)
     636                 :             : {
     637         [ -  + ]:        1070 :     Assert(cmd.find('=') == std::string::npos);
     638         [ -  + ]:        1070 :     Assert(cmd.at(0) != '-');
     639                 :             : 
     640                 :        1070 :     LOCK(cs_args);
     641                 :        1070 :     m_accept_any_command = false; // latch to false
     642         [ +  - ]:        1070 :     std::map<std::string, Arg>& arg_map = m_available_args[OptionsCategory::COMMANDS];
     643   [ +  -  -  +  :        2140 :     auto ret = arg_map.emplace(cmd, Arg{"", help, ArgsManager::COMMAND});
                   +  - ]
     644         [ -  + ]:        1070 :     if (!options.empty()) {
     645         [ #  # ]:           0 :         auto& cmdopts = m_available_args[OptionsCategory::COMMAND_OPTIONS];
     646                 :           0 :         bool command_has_all_options_defined = true;
     647         [ #  # ]:           0 :         for (const auto& opt : options) {
     648         [ #  # ]:           0 :             if (!cmdopts.contains(opt)) {
     649                 :           0 :                 command_has_all_options_defined = false;
     650                 :             :             }
     651                 :             :         }
     652         [ #  # ]:           0 :         Assert(command_has_all_options_defined);
     653                 :             : 
     654         [ #  # ]:           0 :         m_command_args.try_emplace(cmd, std::move(options));
     655                 :             :     }
     656   [ -  +  +  - ]:        1070 :     Assert(ret.second); // Fail on duplicate commands
     657                 :        1070 : }
     658                 :             : 
     659                 :      242149 : void ArgsManager::AddArg(const std::string& name, const std::string& help, unsigned int flags, const OptionsCategory& cat)
     660                 :             : {
     661         [ -  + ]:      242149 :     Assert((flags & ArgsManager::COMMAND) == 0); // use AddCommand
     662                 :             : 
     663                 :             :     // Split arg name from its help param
     664                 :      242149 :     size_t eq_index = name.find('=');
     665         [ +  + ]:      242149 :     if (eq_index == std::string::npos) {
     666         [ -  + ]:      113605 :         eq_index = name.size();
     667                 :             :     }
     668                 :      242149 :     std::string arg_name = name.substr(0, eq_index);
     669                 :             : 
     670         [ +  - ]:      242149 :     LOCK(cs_args);
     671         [ +  - ]:      242149 :     std::map<std::string, Arg>& arg_map = m_available_args[cat];
     672   [ -  +  +  -  :      484298 :     auto ret = arg_map.emplace(arg_name, Arg{name.substr(eq_index, name.size() - eq_index), help, flags});
             -  +  +  - ]
     673         [ -  + ]:      242149 :     assert(ret.second); // Make sure an insertion actually happened
     674                 :             : 
     675         [ +  + ]:      242149 :     if (flags & ArgsManager::NETWORK_ONLY) {
     676         [ +  - ]:       10485 :         m_network_only_args.emplace(arg_name);
     677                 :             :     }
     678                 :      242149 : }
     679                 :             : 
     680                 :       10790 : void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names)
     681                 :             : {
     682         [ +  + ]:       38041 :     for (const std::string& name : names) {
     683         [ +  - ]:       54502 :         AddArg(name, "", ArgsManager::ALLOW_ANY, OptionsCategory::HIDDEN);
     684                 :             :     }
     685                 :       10790 : }
     686                 :             : 
     687                 :        1730 : void ArgsManager::ClearArgs()
     688                 :             : {
     689                 :        1730 :     LOCK(cs_args);
     690                 :        1730 :     m_settings = {};
     691                 :        1730 :     m_available_args.clear();
     692                 :        1730 :     m_command_args.clear();
     693                 :        1730 :     m_network_only_args.clear();
     694         [ +  - ]:        1730 :     m_config_sections.clear();
     695                 :        1730 : }
     696                 :             : 
     697                 :           0 : void ArgsManager::CheckMultipleCLIArgs() const
     698                 :             : {
     699                 :           0 :     LOCK(cs_args);
     700                 :           0 :     std::vector<std::string> found{};
     701                 :           0 :     auto cmds = m_available_args.find(OptionsCategory::CLI_COMMANDS);
     702         [ #  # ]:           0 :     if (cmds != m_available_args.end()) {
     703   [ #  #  #  # ]:           0 :         for (const auto& [cmd, argspec] : cmds->second) {
     704   [ #  #  #  # ]:           0 :             if (!GetSetting_(cmd).isNull()) {
     705         [ #  # ]:           0 :                 found.push_back(cmd);
     706                 :             :             }
     707                 :             :         }
     708   [ #  #  #  # ]:           0 :         if (found.size() > 1) {
     709   [ #  #  #  #  :           0 :             throw std::runtime_error(strprintf("Only one of %s may be specified.", util::Join(found, ", ")));
                   #  # ]
     710                 :             :         }
     711                 :             :     }
     712         [ #  # ]:           0 : }
     713                 :             : 
     714                 :         561 : std::string ArgsManager::GetHelpMessage() const
     715                 :             : {
     716         [ +  - ]:         561 :     const bool show_debug = GetBoolArg("-help-debug", false);
     717                 :             : 
     718         [ +  - ]:         561 :     std::string usage;
     719         [ +  - ]:         561 :     LOCK(cs_args);
     720                 :             : 
     721                 :         561 :     const auto command_options = m_available_args.find(OptionsCategory::COMMAND_OPTIONS);
     722         [ #  # ]:           0 :     const auto for_matching_cmd_opts = [&](const std::set<std::string>& select, auto&& fn) EXCLUSIVE_LOCKS_REQUIRED(cs_args) {
     723         [ #  # ]:           0 :         if (select.empty()) return;
     724         [ #  # ]:           0 :         if (command_options == m_available_args.end()) return;
     725   [ #  #  #  # ]:           0 :         for (const auto& [name, info] : command_options->second) {
     726   [ #  #  #  # ]:           0 :             if (!show_debug && (info.m_flags & ArgsManager::DEBUG_ONLY)) continue;
     727         [ #  # ]:           0 :             if (!select.contains(name)) continue;
     728                 :           0 :             fn(name, info);
     729                 :             :         }
     730                 :         561 :     };
     731                 :             : 
     732   [ +  +  +  +  :        1077 :     for (const auto& [category, category_args] : m_available_args) {
          +  +  +  +  +  
          +  +  +  +  +  
             +  +  +  + ]
     733   [ +  +  +  +  :         852 :         switch(category) {
          +  +  +  +  +  
          +  +  +  +  +  
                   +  + ]
     734                 :         313 :             case OptionsCategory::OPTIONS:
     735   [ +  -  +  - ]:         626 :                 usage += HelpMessageGroup("Options:");
     736                 :         313 :                 break;
     737                 :          25 :             case OptionsCategory::CONNECTION:
     738   [ +  -  +  - ]:          50 :                 usage += HelpMessageGroup("Connection options:");
     739                 :          25 :                 break;
     740                 :           4 :             case OptionsCategory::ZMQ:
     741   [ +  -  +  - ]:           8 :                 usage += HelpMessageGroup("ZeroMQ notification options:");
     742                 :           4 :                 break;
     743                 :          14 :             case OptionsCategory::DEBUG_TEST:
     744   [ +  -  +  - ]:          28 :                 usage += HelpMessageGroup("Debugging/Testing options:");
     745                 :          14 :                 break;
     746                 :          13 :             case OptionsCategory::NODE_RELAY:
     747   [ +  -  +  - ]:          26 :                 usage += HelpMessageGroup("Node relay options:");
     748                 :          13 :                 break;
     749                 :           4 :             case OptionsCategory::BLOCK_CREATION:
     750   [ +  -  +  - ]:           8 :                 usage += HelpMessageGroup("Block creation options:");
     751                 :           4 :                 break;
     752                 :           2 :             case OptionsCategory::RPC:
     753   [ +  -  +  - ]:           4 :                 usage += HelpMessageGroup("RPC server options:");
     754                 :           2 :                 break;
     755                 :           3 :             case OptionsCategory::IPC:
     756   [ +  -  +  - ]:           6 :                 usage += HelpMessageGroup("IPC interprocess connection options:");
     757                 :           3 :                 break;
     758                 :           6 :             case OptionsCategory::WALLET:
     759   [ +  -  +  - ]:          12 :                 usage += HelpMessageGroup("Wallet options:");
     760                 :           6 :                 break;
     761                 :          13 :             case OptionsCategory::WALLET_DEBUG_TEST:
     762   [ -  +  -  -  :          13 :                 if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:");
                   -  - ]
     763                 :             :                 break;
     764                 :           4 :             case OptionsCategory::CHAINPARAMS:
     765   [ +  -  +  - ]:           8 :                 usage += HelpMessageGroup("Chain selection options:");
     766                 :           4 :                 break;
     767                 :          10 :             case OptionsCategory::GUI:
     768   [ +  -  +  - ]:          20 :                 usage += HelpMessageGroup("UI Options:");
     769                 :          10 :                 break;
     770                 :          81 :             case OptionsCategory::COMMANDS:
     771   [ +  -  +  - ]:         162 :                 usage += HelpMessageGroup("Commands:");
     772                 :          81 :                 break;
     773                 :           2 :             case OptionsCategory::REGISTER_COMMANDS:
     774   [ +  -  +  - ]:           4 :                 usage += HelpMessageGroup("Register Commands:");
     775                 :           2 :                 break;
     776                 :          20 :             case OptionsCategory::CLI_COMMANDS:
     777   [ +  -  +  - ]:          40 :                 usage += HelpMessageGroup("CLI Commands:");
     778                 :          20 :                 break;
     779                 :             :             case OptionsCategory::COMMAND_OPTIONS:
     780                 :             :             case OptionsCategory::HIDDEN:
     781                 :             :                 break;
     782                 :             :         } // no default case, so the compiler can warn about missing cases
     783                 :             : 
     784         [ +  + ]:         852 :         if (category == OptionsCategory::COMMAND_OPTIONS) continue;
     785                 :             : 
     786                 :             :         // When we get to the hidden options, stop
     787         [ +  + ]:         850 :         if (category == OptionsCategory::HIDDEN) break;
     788                 :             : 
     789   [ +  +  +  + ]:        1688 :         for (const auto& [arg_name, arg_info] : category_args) {
     790   [ +  +  +  + ]:        1174 :             if (show_debug || !(arg_info.m_flags & ArgsManager::DEBUG_ONLY)) {
     791   [ -  +  -  +  :        1580 :                 usage += HelpMessageOpt(arg_name, arg_info.m_help_param, arg_info.m_help_text);
             -  +  +  - ]
     792                 :             : 
     793         [ +  + ]:         790 :                 if (category == OptionsCategory::COMMANDS) {
     794                 :         468 :                     const auto cmd_args = m_command_args.find(arg_name);
     795         [ +  - ]:         468 :                     if (cmd_args == m_command_args.end()) continue;
     796         [ #  # ]:           0 :                     for_matching_cmd_opts(cmd_args->second, [&](const auto& cmdopt_name, const auto& cmdopt_info) {
     797   [ #  #  #  #  :           0 :                         usage += HelpMessageOpt(cmdopt_name, cmdopt_info.m_help_param, cmdopt_info.m_help_text, /*subopt=*/true);
             #  #  #  # ]
     798                 :           0 :                     });
     799                 :             :                 }
     800                 :             :             }
     801                 :             :         }
     802                 :             :     }
     803         [ +  - ]:         561 :     return usage;
     804                 :         561 : }
     805                 :             : 
     806                 :         561 : bool HelpRequested(const ArgsManager& args)
     807                 :             : {
     808   [ +  -  +  -  :        2684 :     return args.IsArgSet("-?") || args.IsArgSet("-h") || args.IsArgSet("-help") || args.IsArgSet("-help-debug");
          +  +  +  -  +  
          -  +  +  +  -  
          +  -  +  +  +  
          -  +  -  +  +  
          +  +  +  +  -  
             -  -  -  -  
                      - ]
     809                 :             : }
     810                 :             : 
     811                 :        1648 : void SetupHelpOptions(ArgsManager& args)
     812                 :             : {
     813   [ +  -  +  - ]:        3296 :     args.AddArg("-help", "Print this help message and exit (also -h or -?)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     814         [ +  - ]:        1648 :     args.AddHiddenArgs({"-h", "-?"});
     815                 :        1648 : }
     816                 :             : 
     817                 :        1544 : std::string HelpMessageGroup(const std::string &message) {
     818         [ +  - ]:        4632 :     return std::string(message) + std::string("\n\n");
     819                 :             : }
     820                 :             : 
     821                 :        2876 : std::string HelpMessageOpt(std::string_view option, std::string_view help_param, std::string_view message, bool subopt)
     822                 :             : {
     823                 :        2876 :     constexpr int screen_width = 79;
     824                 :        2876 :     int opt_indent = 2;
     825                 :        2876 :     int msg_indent = 7;
     826                 :             : 
     827         [ -  + ]:        2876 :     if (subopt) {
     828                 :           0 :         int bump = msg_indent - opt_indent;
     829                 :           0 :         opt_indent += bump; // opt_indent now at the old msg_indent level
     830                 :           0 :         msg_indent += bump; // indent by the same amount
     831                 :             :     }
     832                 :        2876 :     int msg_width = screen_width - msg_indent;
     833                 :             : 
     834                 :        2876 :     return strprintf("%*s%s%s\n%*s%s\n\n",
     835                 :             :                      opt_indent, "", option, help_param,
     836         [ +  - ]:        5752 :                      msg_indent, "", FormatParagraph(message, msg_width, msg_indent));
     837                 :             : }
     838                 :             : 
     839                 :             : const std::vector<std::string> TEST_OPTIONS_DOC{
     840                 :             :     "addrman (use deterministic addrman)",
     841                 :             :     "reindex_after_failure_noninteractive_yes (When asked for a reindex after failure interactively, simulate as-if answered with 'yes')",
     842                 :             :     "bip94 (enforce BIP94 consensus rules)",
     843                 :             : };
     844                 :             : 
     845                 :        2482 : bool HasTestOption(const ArgsManager& args, const std::string& test_option)
     846                 :             : {
     847         [ +  - ]:        2482 :     const auto options = args.GetArgs("-test");
     848   [ -  +  -  +  :       29784 :     return std::any_of(options.begin(), options.end(), [test_option](const auto& option) {
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
                      - ]
     849         [ #  # ]:           0 :         return option == test_option;
     850                 :        2482 :     });
     851                 :        2482 : }
     852                 :             : 
     853                 :           0 : fs::path GetDefaultDataDir()
     854                 :             : {
     855                 :             :     // Windows:
     856                 :             :     //   old: C:\Users\Username\AppData\Roaming\Bitcoin
     857                 :             :     //   new: C:\Users\Username\AppData\Local\Bitcoin
     858                 :             :     // macOS: ~/Library/Application Support/Bitcoin
     859                 :             :     // Unix-like: ~/.bitcoin
     860                 :             : #ifdef WIN32
     861                 :             :     // Windows
     862                 :             :     // Check for existence of datadir in old location and keep it there
     863                 :             :     fs::path legacy_path = GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
     864                 :             :     if (fs::exists(legacy_path)) return legacy_path;
     865                 :             : 
     866                 :             :     // Otherwise, fresh installs can start in the new, "proper" location
     867                 :             :     return GetSpecialFolderPath(CSIDL_LOCAL_APPDATA) / "Bitcoin";
     868                 :             : #else
     869                 :           0 :     fs::path pathRet;
     870                 :           0 :     char* pszHome = getenv("HOME");
     871   [ #  #  #  # ]:           0 :     if (pszHome == nullptr || strlen(pszHome) == 0)
     872         [ #  # ]:           0 :         pathRet = fs::path("/");
     873                 :             :     else
     874         [ #  # ]:           0 :         pathRet = fs::path(pszHome);
     875                 :             : #ifdef __APPLE__
     876                 :             :     // macOS
     877                 :             :     return pathRet / "Library/Application Support/Bitcoin";
     878                 :             : #else
     879                 :             :     // Unix-like
     880   [ #  #  #  # ]:           0 :     return pathRet / ".bitcoin";
     881                 :             : #endif
     882                 :             : #endif
     883                 :           0 : }
     884                 :             : 
     885                 :           0 : bool CheckDataDirOption(const ArgsManager& args)
     886                 :             : {
     887   [ #  #  #  # ]:           0 :     const fs::path datadir{args.GetPathArg("-datadir")};
     888   [ #  #  #  #  :           0 :     return datadir.empty() || fs::is_directory(fs::absolute(datadir));
             #  #  #  # ]
     889                 :           0 : }
     890                 :             : 
     891                 :           0 : fs::path ArgsManager::GetConfigFilePath() const
     892                 :             : {
     893                 :           0 :     LOCK(cs_args);
     894   [ #  #  #  #  :           0 :     return *Assert(m_config_path);
                   #  # ]
     895                 :           0 : }
     896                 :             : 
     897                 :           0 : void ArgsManager::SetConfigFilePath(fs::path path)
     898                 :             : {
     899                 :           0 :     LOCK(cs_args);
     900         [ #  # ]:           0 :     assert(!m_config_path);
     901         [ #  # ]:           0 :     m_config_path = path;
     902                 :           0 : }
     903                 :             : 
     904                 :        1236 : ChainType ArgsManager::GetChainType() const
     905                 :             : {
     906                 :        1236 :     std::variant<ChainType, std::string> arg = GetChainArg();
     907         [ +  - ]:        1236 :     if (auto* parsed = std::get_if<ChainType>(&arg)) return *parsed;
     908   [ #  #  #  #  :           0 :     throw std::runtime_error(strprintf("Unknown chain %s.", std::get<std::string>(arg)));
                   #  # ]
     909                 :        1236 : }
     910                 :             : 
     911                 :         561 : std::string ArgsManager::GetChainTypeString() const
     912                 :             : {
     913                 :         561 :     auto arg = GetChainArg();
     914   [ +  +  +  - ]:         561 :     if (auto* parsed = std::get_if<ChainType>(&arg)) return ChainTypeToString(*parsed);
     915   [ -  +  -  + ]:         568 :     return std::get<std::string>(arg);
     916                 :         561 : }
     917                 :             : 
     918                 :        1797 : std::variant<ChainType, std::string> ArgsManager::GetChainArg() const
     919                 :             : {
     920                 :        8985 :     auto get_net = [&](const std::string& arg) {
     921                 :        7188 :         LOCK(cs_args);
     922   [ +  -  +  - ]:       14376 :         common::SettingsValue value = common::GetSetting(m_settings, /* section= */ "", SettingName(arg),
     923                 :             :             /* ignore_default_section_config= */ false,
     924                 :             :             /*ignore_nonpersistent=*/false,
     925         [ +  - ]:        7188 :             /* get_chain_type= */ true);
     926   [ +  +  -  +  :        7188 :         return value.isNull() ? false : value.isBool() ? value.get_bool() : InterpretBool(value.get_str());
          -  -  +  -  +  
                      - ]
     927         [ +  - ]:       14376 :     };
     928                 :             : 
     929         [ +  - ]:        1797 :     const bool fRegTest = get_net("-regtest");
     930         [ +  - ]:        1797 :     const bool fSigNet  = get_net("-signet");
     931         [ +  - ]:        1797 :     const bool fTestNet = get_net("-testnet");
     932         [ +  - ]:        1797 :     const bool fTestNet4 = get_net("-testnet4");
     933         [ +  - ]:        1797 :     const auto chain_arg = GetArg("-chain");
     934                 :             : 
     935         [ -  + ]:        1797 :     if ((int)chain_arg.has_value() + (int)fRegTest + (int)fSigNet + (int)fTestNet + (int)fTestNet4 > 1) {
     936         [ #  # ]:           0 :         throw std::runtime_error("Invalid combination of -regtest, -signet, -testnet, -testnet4 and -chain. Can use at most one.");
     937                 :             :     }
     938         [ +  + ]:        1797 :     if (chain_arg) {
     939   [ -  +  +  -  :           8 :         if (auto parsed = ChainTypeFromString(*chain_arg)) return *parsed;
                   +  + ]
     940                 :             :         // Not a known string, so return original string
     941         [ -  + ]:          14 :         return *chain_arg;
     942                 :             :     }
     943         [ +  + ]:        1789 :     if (fRegTest) return ChainType::REGTEST;
     944         [ -  + ]:        1784 :     if (fSigNet) return ChainType::SIGNET;
     945         [ -  + ]:        1784 :     if (fTestNet) return ChainType::TESTNET;
     946         [ +  + ]:        1784 :     if (fTestNet4) return ChainType::TESTNET4;
     947                 :        1781 :     return ChainType::MAIN;
     948                 :        1797 : }
     949                 :             : 
     950                 :     1094586 : bool ArgsManager::UseDefaultSection(const std::string& arg) const
     951                 :             : {
     952                 :     1094586 :     AssertLockHeld(cs_args);
     953   [ +  +  +  + ]:     1094586 :     return m_network == ChainTypeToString(ChainType::MAIN) || !m_network_only_args.contains(arg);
     954                 :             : }
     955                 :             : 
     956                 :      884035 : common::SettingsValue ArgsManager::GetSetting_(const std::string& arg) const
     957                 :             : {
     958                 :      884035 :     AssertLockHeld(cs_args);
     959                 :      884035 :     return common::GetSetting(
     960                 :      884035 :         m_settings, m_network, SettingName(arg), !UseDefaultSection(arg),
     961         [ +  - ]:     1768070 :         /*ignore_nonpersistent=*/false, /*get_chain_type=*/false);
     962                 :             : }
     963                 :             : 
     964                 :      867461 : common::SettingsValue ArgsManager::GetSetting(const std::string& arg) const
     965                 :             : {
     966                 :      867461 :     LOCK(cs_args);
     967         [ +  - ]:      867461 :     return GetSetting_(arg);
     968                 :      867461 : }
     969                 :             : 
     970                 :      210551 : std::vector<common::SettingsValue> ArgsManager::GetSettingsList(const std::string& arg) const
     971                 :             : {
     972                 :      210551 :     LOCK(cs_args);
     973   [ +  -  +  -  :      421102 :     return common::GetSettingsList(m_settings, m_network, SettingName(arg), !UseDefaultSection(arg));
             +  -  +  - ]
     974                 :      210551 : }
     975                 :             : 
     976                 :           0 : void ArgsManager::logArgsPrefix(
     977                 :             :     const std::string& prefix,
     978                 :             :     const std::string& section,
     979                 :             :     const std::map<std::string, std::vector<common::SettingsValue>>& args) const
     980                 :             : {
     981                 :           0 :     AssertLockHeld(cs_args);
     982   [ #  #  #  # ]:           0 :     std::string section_str = section.empty() ? "" : "[" + section + "] ";
     983         [ #  # ]:           0 :     for (const auto& arg : args) {
     984         [ #  # ]:           0 :         for (const auto& value : arg.second) {
     985   [ #  #  #  # ]:           0 :             std::optional<unsigned int> flags = GetArgFlags_('-' + arg.first);
     986         [ #  # ]:           0 :             if (flags) {
     987   [ #  #  #  #  :           0 :                 std::string value_str = (*flags & SENSITIVE) ? "****" : value.write();
                   #  # ]
     988         [ #  # ]:           0 :                 LogInfo("%s %s%s=%s\n", prefix, section_str, arg.first, value_str);
     989                 :           0 :             }
     990                 :             :         }
     991                 :             :     }
     992                 :           0 : }
     993                 :             : 
     994                 :           0 : void ArgsManager::LogArgs() const
     995                 :             : {
     996                 :           0 :     LOCK(cs_args);
     997         [ #  # ]:           0 :     for (const auto& section : m_settings.ro_config) {
     998   [ #  #  #  # ]:           0 :         logArgsPrefix("Config file arg:", section.first, section.second);
     999                 :             :     }
    1000         [ #  # ]:           0 :     for (const auto& setting : m_settings.rw_settings) {
    1001   [ #  #  #  # ]:           0 :         LogInfo("Setting file arg: %s = %s\n", setting.first, setting.second.write());
    1002                 :             :     }
    1003   [ #  #  #  #  :           0 :     logArgsPrefix("Command-line arg:", "", m_settings.command_line_options);
             #  #  #  # ]
    1004                 :           0 : }
        

Generated by: LCOV version 2.0-1