LCOV - code coverage report
Current view: top level - src/common - args.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 9 18 50.0 %
Date: 2023-11-10 23:46:46 Functions: 7 20 35.0 %
Branches: 0 12 0.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2023 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #ifndef BITCOIN_COMMON_ARGS_H
       6                 :            : #define BITCOIN_COMMON_ARGS_H
       7                 :            : 
       8                 :            : #include <common/settings.h>
       9                 :            : #include <compat/compat.h>
      10                 :            : #include <sync.h>
      11                 :            : #include <util/chaintype.h>
      12                 :            : #include <util/fs.h>
      13                 :            : 
      14                 :            : #include <iosfwd>
      15                 :            : #include <list>
      16                 :            : #include <map>
      17                 :            : #include <optional>
      18                 :            : #include <set>
      19                 :            : #include <stdint.h>
      20                 :            : #include <string>
      21                 :            : #include <variant>
      22                 :            : #include <vector>
      23                 :            : 
      24                 :            : class ArgsManager;
      25                 :            : 
      26                 :            : extern const char * const BITCOIN_CONF_FILENAME;
      27                 :            : extern const char * const BITCOIN_SETTINGS_FILENAME;
      28                 :            : 
      29                 :            : // Return true if -datadir option points to a valid directory or is not specified.
      30                 :            : bool CheckDataDirOption(const ArgsManager& args);
      31                 :            : 
      32                 :            : /**
      33                 :            :  * Most paths passed as configuration arguments are treated as relative to
      34                 :            :  * the datadir if they are not absolute.
      35                 :            :  *
      36                 :            :  * @param args Parsed arguments and settings.
      37                 :            :  * @param path The path to be conditionally prefixed with datadir.
      38                 :            :  * @param net_specific Use network specific datadir variant
      39                 :            :  * @return The normalized path.
      40                 :            :  */
      41                 :            : fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific = true);
      42                 :            : 
      43                 :          0 : inline bool IsSwitchChar(char c)
      44                 :            : {
      45                 :            : #ifdef WIN32
      46                 :            :     return c == '-' || c == '/';
      47                 :            : #else
      48                 :          0 :     return c == '-';
      49                 :            : #endif
      50                 :            : }
      51                 :            : 
      52                 :            : enum class OptionsCategory {
      53                 :            :     OPTIONS,
      54                 :            :     CONNECTION,
      55                 :            :     WALLET,
      56                 :            :     WALLET_DEBUG_TEST,
      57                 :            :     ZMQ,
      58                 :            :     DEBUG_TEST,
      59                 :            :     CHAINPARAMS,
      60                 :            :     NODE_RELAY,
      61                 :            :     BLOCK_CREATION,
      62                 :            :     RPC,
      63                 :            :     GUI,
      64                 :            :     COMMANDS,
      65                 :            :     REGISTER_COMMANDS,
      66                 :            : 
      67                 :            :     HIDDEN // Always the last option to avoid printing these in the help
      68                 :            : };
      69                 :            : 
      70                 :         10 : struct KeyInfo {
      71                 :            :     std::string name;
      72                 :            :     std::string section;
      73                 :         10 :     bool negated{false};
      74                 :            : };
      75                 :            : 
      76                 :            : KeyInfo InterpretKey(std::string key);
      77                 :            : 
      78                 :            : std::optional<common::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value,
      79                 :            :                                                          unsigned int flags, std::string& error);
      80                 :            : 
      81                 :          0 : struct SectionInfo {
      82                 :            :     std::string m_name;
      83                 :            :     std::string m_file;
      84                 :            :     int m_line;
      85                 :            : };
      86                 :            : 
      87                 :            : std::string SettingToString(const common::SettingsValue&, const std::string&);
      88                 :            : std::optional<std::string> SettingToString(const common::SettingsValue&);
      89                 :            : 
      90                 :            : int64_t SettingToInt(const common::SettingsValue&, int64_t);
      91                 :            : std::optional<int64_t> SettingToInt(const common::SettingsValue&);
      92                 :            : 
      93                 :            : bool SettingToBool(const common::SettingsValue&, bool);
      94                 :            : std::optional<bool> SettingToBool(const common::SettingsValue&);
      95                 :            : 
      96                 :            : class ArgsManager
      97                 :            : {
      98                 :            : public:
      99                 :            :     /**
     100                 :            :      * Flags controlling how config and command line arguments are validated and
     101                 :            :      * interpreted.
     102                 :            :      */
     103                 :            :     enum Flags : uint32_t {
     104                 :            :         ALLOW_ANY = 0x01,         //!< disable validation
     105                 :            :         // ALLOW_BOOL = 0x02,     //!< unimplemented, draft implementation in #16545
     106                 :            :         // ALLOW_INT = 0x04,      //!< unimplemented, draft implementation in #16545
     107                 :            :         // ALLOW_STRING = 0x08,   //!< unimplemented, draft implementation in #16545
     108                 :            :         // ALLOW_LIST = 0x10,     //!< unimplemented, draft implementation in #16545
     109                 :            :         DISALLOW_NEGATION = 0x20, //!< disallow -nofoo syntax
     110                 :            :         DISALLOW_ELISION = 0x40,  //!< disallow -foo syntax that doesn't assign any value
     111                 :            : 
     112                 :            :         DEBUG_ONLY = 0x100,
     113                 :            :         /* Some options would cause cross-contamination if values for
     114                 :            :          * mainnet were used while running on regtest/testnet (or vice-versa).
     115                 :            :          * Setting them as NETWORK_ONLY ensures that sharing a config file
     116                 :            :          * between mainnet and regtest/testnet won't cause problems due to these
     117                 :            :          * parameters by accident. */
     118                 :            :         NETWORK_ONLY = 0x200,
     119                 :            :         // This argument's value is sensitive (such as a password).
     120                 :            :         SENSITIVE = 0x400,
     121                 :            :         COMMAND = 0x800,
     122                 :            :     };
     123                 :            : 
     124                 :            : protected:
     125                 :          0 :     struct Arg
     126                 :            :     {
     127                 :            :         std::string m_help_param;
     128                 :            :         std::string m_help_text;
     129                 :            :         unsigned int m_flags;
     130                 :            :     };
     131                 :            : 
     132                 :            :     mutable RecursiveMutex cs_args;
     133                 :            :     common::Settings m_settings GUARDED_BY(cs_args);
     134                 :            :     std::vector<std::string> m_command GUARDED_BY(cs_args);
     135                 :            :     std::string m_network GUARDED_BY(cs_args);
     136                 :            :     std::set<std::string> m_network_only_args GUARDED_BY(cs_args);
     137                 :            :     std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args GUARDED_BY(cs_args);
     138                 :            :     bool m_accept_any_command GUARDED_BY(cs_args){true};
     139                 :            :     std::list<SectionInfo> m_config_sections GUARDED_BY(cs_args);
     140                 :            :     std::optional<fs::path> m_config_path GUARDED_BY(cs_args);
     141                 :            :     mutable fs::path m_cached_blocks_path GUARDED_BY(cs_args);
     142                 :            :     mutable fs::path m_cached_datadir_path GUARDED_BY(cs_args);
     143                 :            :     mutable fs::path m_cached_network_datadir_path GUARDED_BY(cs_args);
     144                 :            : 
     145                 :            :     [[nodiscard]] bool ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys = false);
     146                 :            : 
     147                 :            :     /**
     148                 :            :      * Returns true if settings values from the default section should be used,
     149                 :            :      * depending on the current network and whether the setting is
     150                 :            :      * network-specific.
     151                 :            :      */
     152                 :            :     bool UseDefaultSection(const std::string& arg) const EXCLUSIVE_LOCKS_REQUIRED(cs_args);
     153                 :            : 
     154                 :            :  public:
     155                 :            :     /**
     156                 :            :      * Get setting value.
     157                 :            :      *
     158                 :            :      * Result will be null if setting was unset, true if "-setting" argument was passed
     159                 :            :      * false if "-nosetting" argument was passed, and a string if a "-setting=value"
     160                 :            :      * argument was passed.
     161                 :            :      */
     162                 :            :     common::SettingsValue GetSetting(const std::string& arg) const;
     163                 :            : 
     164                 :            :     /**
     165                 :            :      * Get list of setting values.
     166                 :            :      */
     167                 :            :     std::vector<common::SettingsValue> GetSettingsList(const std::string& arg) const;
     168                 :            : 
     169                 :            :     ArgsManager();
     170                 :            :     ~ArgsManager();
     171                 :            : 
     172                 :            :     /**
     173                 :            :      * Select the network in use
     174                 :            :      */
     175                 :            :     void SelectConfigNetwork(const std::string& network);
     176                 :            : 
     177                 :            :     [[nodiscard]] bool ParseParameters(int argc, const char* const argv[], std::string& error);
     178                 :            : 
     179                 :            :     /**
     180                 :            :      * Return config file path (read-only)
     181                 :            :      */
     182                 :            :     fs::path GetConfigFilePath() const;
     183                 :            :     [[nodiscard]] bool ReadConfigFiles(std::string& error, bool ignore_invalid_keys = false);
     184                 :            : 
     185                 :            :     /**
     186                 :            :      * Log warnings for options in m_section_only_args when
     187                 :            :      * they are specified in the default section but not overridden
     188                 :            :      * on the command line or in a network-specific section in the
     189                 :            :      * config file.
     190                 :            :      */
     191                 :            :     std::set<std::string> GetUnsuitableSectionOnlyArgs() const;
     192                 :            : 
     193                 :            :     /**
     194                 :            :      * Log warnings for unrecognized section names in the config file.
     195                 :            :      */
     196                 :            :     std::list<SectionInfo> GetUnrecognizedSections() const;
     197                 :            : 
     198                 :          0 :     struct Command {
     199                 :            :         /** The command (if one has been registered with AddCommand), or empty */
     200                 :            :         std::string command;
     201                 :            :         /**
     202                 :            :          * If command is non-empty: Any args that followed it
     203                 :            :          * If command is empty: The unregistered command and any args that followed it
     204                 :            :          */
     205                 :            :         std::vector<std::string> args;
     206                 :            :     };
     207                 :            :     /**
     208                 :            :      * Get the command and command args (returns std::nullopt if no command provided)
     209                 :            :      */
     210                 :            :     std::optional<const Command> GetCommand() const;
     211                 :            : 
     212                 :            :     /**
     213                 :            :      * Get blocks directory path
     214                 :            :      *
     215                 :            :      * @return Blocks path which is network specific
     216                 :            :      */
     217                 :            :     const fs::path& GetBlocksDirPath() const;
     218                 :            : 
     219                 :            :     /**
     220                 :            :      * Get data directory path
     221                 :            :      *
     222                 :            :      * @return Absolute path on success, otherwise an empty path when a non-directory path would be returned
     223                 :            :      */
     224                 :          3 :     const fs::path& GetDataDirBase() const { return GetDataDir(false); }
     225                 :            : 
     226                 :            :     /**
     227                 :            :      * Get data directory path with appended network identifier
     228                 :            :      *
     229                 :            :      * @return Absolute path on success, otherwise an empty path when a non-directory path would be returned
     230                 :            :      */
     231                 :          5 :     const fs::path& GetDataDirNet() const { return GetDataDir(true); }
     232                 :            : 
     233                 :            :     /**
     234                 :            :      * Clear cached directory paths
     235                 :            :      */
     236                 :            :     void ClearPathCache();
     237                 :            : 
     238                 :            :     /**
     239                 :            :      * Return a vector of strings of the given argument
     240                 :            :      *
     241                 :            :      * @param strArg Argument to get (e.g. "-foo")
     242                 :            :      * @return command-line arguments
     243                 :            :      */
     244                 :            :     std::vector<std::string> GetArgs(const std::string& strArg) const;
     245                 :            : 
     246                 :            :     /**
     247                 :            :      * Return true if the given argument has been manually set
     248                 :            :      *
     249                 :            :      * @param strArg Argument to get (e.g. "-foo")
     250                 :            :      * @return true if the argument has been set
     251                 :            :      */
     252                 :            :     bool IsArgSet(const std::string& strArg) const;
     253                 :            : 
     254                 :            :     /**
     255                 :            :      * Return true if the argument was originally passed as a negated option,
     256                 :            :      * i.e. -nofoo.
     257                 :            :      *
     258                 :            :      * @param strArg Argument to get (e.g. "-foo")
     259                 :            :      * @return true if the argument was passed negated
     260                 :            :      */
     261                 :            :     bool IsArgNegated(const std::string& strArg) const;
     262                 :            : 
     263                 :            :     /**
     264                 :            :      * Return string argument or default value
     265                 :            :      *
     266                 :            :      * @param strArg Argument to get (e.g. "-foo")
     267                 :            :      * @param strDefault (e.g. "1")
     268                 :            :      * @return command-line argument or default value
     269                 :            :      */
     270                 :            :     std::string GetArg(const std::string& strArg, const std::string& strDefault) const;
     271                 :            :     std::optional<std::string> GetArg(const std::string& strArg) const;
     272                 :            : 
     273                 :            :     /**
     274                 :            :      * Return path argument or default value
     275                 :            :      *
     276                 :            :      * @param arg Argument to get a path from (e.g., "-datadir", "-blocksdir" or "-walletdir")
     277                 :            :      * @param default_value Optional default value to return instead of the empty path.
     278                 :            :      * @return normalized path if argument is set, with redundant "." and ".."
     279                 :            :      * path components and trailing separators removed (see patharg unit test
     280                 :            :      * for examples or implementation for details). If argument is empty or not
     281                 :            :      * set, default_value is returned unchanged.
     282                 :            :      */
     283                 :            :     fs::path GetPathArg(std::string arg, const fs::path& default_value = {}) const;
     284                 :            : 
     285                 :            :     /**
     286                 :            :      * Return integer argument or default value
     287                 :            :      *
     288                 :            :      * @param strArg Argument to get (e.g. "-foo")
     289                 :            :      * @param nDefault (e.g. 1)
     290                 :            :      * @return command-line argument (0 if invalid number) or default value
     291                 :            :      */
     292                 :            :     int64_t GetIntArg(const std::string& strArg, int64_t nDefault) const;
     293                 :            :     std::optional<int64_t> GetIntArg(const std::string& strArg) const;
     294                 :            : 
     295                 :            :     /**
     296                 :            :      * Return boolean argument or default value
     297                 :            :      *
     298                 :            :      * @param strArg Argument to get (e.g. "-foo")
     299                 :            :      * @param fDefault (true or false)
     300                 :            :      * @return command-line argument or default value
     301                 :            :      */
     302                 :            :     bool GetBoolArg(const std::string& strArg, bool fDefault) const;
     303                 :            :     std::optional<bool> GetBoolArg(const std::string& strArg) const;
     304                 :            : 
     305                 :            :     /**
     306                 :            :      * Set an argument if it doesn't already have a value
     307                 :            :      *
     308                 :            :      * @param strArg Argument to set (e.g. "-foo")
     309                 :            :      * @param strValue Value (e.g. "1")
     310                 :            :      * @return true if argument gets set, false if it already had a value
     311                 :            :      */
     312                 :            :     bool SoftSetArg(const std::string& strArg, const std::string& strValue);
     313                 :            : 
     314                 :            :     /**
     315                 :            :      * Set a boolean argument if it doesn't already have a value
     316                 :            :      *
     317                 :            :      * @param strArg Argument to set (e.g. "-foo")
     318                 :            :      * @param fValue Value (e.g. false)
     319                 :            :      * @return true if argument gets set, false if it already had a value
     320                 :            :      */
     321                 :            :     bool SoftSetBoolArg(const std::string& strArg, bool fValue);
     322                 :            : 
     323                 :            :     // Forces an arg setting. Called by SoftSetArg() if the arg hasn't already
     324                 :            :     // been set. Also called directly in testing.
     325                 :            :     void ForceSetArg(const std::string& strArg, const std::string& strValue);
     326                 :            : 
     327                 :            :     /**
     328                 :            :      * Returns the appropriate chain type from the program arguments.
     329                 :            :      * @return ChainType::MAIN by default; raises runtime error if an invalid
     330                 :            :      * combination, or unknown chain is given.
     331                 :            :      */
     332                 :            :     ChainType GetChainType() const;
     333                 :            : 
     334                 :            :     /**
     335                 :            :      * Returns the appropriate chain type string from the program arguments.
     336                 :            :      * @return ChainType::MAIN string by default; raises runtime error if an
     337                 :            :      * invalid combination is given.
     338                 :            :      */
     339                 :            :     std::string GetChainTypeString() const;
     340                 :            : 
     341                 :            :     /**
     342                 :            :      * Add argument
     343                 :            :      */
     344                 :            :     void AddArg(const std::string& name, const std::string& help, unsigned int flags, const OptionsCategory& cat);
     345                 :            : 
     346                 :            :     /**
     347                 :            :      * Add subcommand
     348                 :            :      */
     349                 :            :     void AddCommand(const std::string& cmd, const std::string& help);
     350                 :            : 
     351                 :            :     /**
     352                 :            :      * Add many hidden arguments
     353                 :            :      */
     354                 :            :     void AddHiddenArgs(const std::vector<std::string>& args);
     355                 :            : 
     356                 :            :     /**
     357                 :            :      * Clear available arguments
     358                 :            :      */
     359                 :          1 :     void ClearArgs() {
     360                 :          1 :         LOCK(cs_args);
     361                 :          1 :         m_available_args.clear();
     362                 :          1 :         m_network_only_args.clear();
     363                 :          1 :     }
     364                 :            : 
     365                 :            :     /**
     366                 :            :      * Get the help string
     367                 :            :      */
     368                 :            :     std::string GetHelpMessage() const;
     369                 :            : 
     370                 :            :     /**
     371                 :            :      * Return Flags for known arg.
     372                 :            :      * Return nullopt for unknown arg.
     373                 :            :      */
     374                 :            :     std::optional<unsigned int> GetArgFlags(const std::string& name) const;
     375                 :            : 
     376                 :            :     /**
     377                 :            :      * Get settings file path, or return false if read-write settings were
     378                 :            :      * disabled with -nosettings.
     379                 :            :      */
     380                 :            :     bool GetSettingsPath(fs::path* filepath = nullptr, bool temp = false, bool backup = false) const;
     381                 :            : 
     382                 :            :     /**
     383                 :            :      * Read settings file. Push errors to vector, or log them if null.
     384                 :            :      */
     385                 :            :     bool ReadSettingsFile(std::vector<std::string>* errors = nullptr);
     386                 :            : 
     387                 :            :     /**
     388                 :            :      * Write settings file or backup settings file. Push errors to vector, or
     389                 :            :      * log them if null.
     390                 :            :      */
     391                 :            :     bool WriteSettingsFile(std::vector<std::string>* errors = nullptr, bool backup = false) const;
     392                 :            : 
     393                 :            :     /**
     394                 :            :      * Get current setting from config file or read/write settings file,
     395                 :            :      * ignoring nonpersistent command line or forced settings values.
     396                 :            :      */
     397                 :            :     common::SettingsValue GetPersistentSetting(const std::string& name) const;
     398                 :            : 
     399                 :            :     /**
     400                 :            :      * Access settings with lock held.
     401                 :            :      */
     402                 :            :     template <typename Fn>
     403                 :          0 :     void LockSettings(Fn&& fn)
     404                 :            :     {
     405                 :          0 :         LOCK(cs_args);
     406 [ #  # ][ #  # ]:          0 :         fn(m_settings);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     407                 :          0 :     }
     408                 :            : 
     409                 :            :     /**
     410                 :            :      * Log the config file options and the command line arguments,
     411                 :            :      * useful for troubleshooting.
     412                 :            :      */
     413                 :            :     void LogArgs() const;
     414                 :            : 
     415                 :            : private:
     416                 :            :     /**
     417                 :            :      * Get data directory path
     418                 :            :      *
     419                 :            :      * @param net_specific Append network identifier to the returned path
     420                 :            :      * @return Absolute path on success, otherwise an empty path when a non-directory path would be returned
     421                 :            :      */
     422                 :            :     const fs::path& GetDataDir(bool net_specific) const;
     423                 :            : 
     424                 :            :     /**
     425                 :            :      * Return -regtest/-signet/-testnet/-chain= setting as a ChainType enum if a
     426                 :            :      * recognized chain type was set, or as a string if an unrecognized chain
     427                 :            :      * name was set. Raise an exception if an invalid combination of flags was
     428                 :            :      * provided.
     429                 :            :      */
     430                 :            :     std::variant<ChainType, std::string> GetChainArg() const;
     431                 :            : 
     432                 :            :     // Helper function for LogArgs().
     433                 :            :     void logArgsPrefix(
     434                 :            :         const std::string& prefix,
     435                 :            :         const std::string& section,
     436                 :            :         const std::map<std::string, std::vector<common::SettingsValue>>& args) const;
     437                 :            : };
     438                 :            : 
     439                 :            : extern ArgsManager gArgs;
     440                 :            : 
     441                 :            : /**
     442                 :            :  * @return true if help has been requested via a command-line arg
     443                 :            :  */
     444                 :            : bool HelpRequested(const ArgsManager& args);
     445                 :            : 
     446                 :            : /** Add help options to the args manager */
     447                 :            : void SetupHelpOptions(ArgsManager& args);
     448                 :            : 
     449                 :            : /**
     450                 :            :  * Format a string to be used as group of options in help messages
     451                 :            :  *
     452                 :            :  * @param message Group name (e.g. "RPC server options:")
     453                 :            :  * @return the formatted string
     454                 :            :  */
     455                 :            : std::string HelpMessageGroup(const std::string& message);
     456                 :            : 
     457                 :            : /**
     458                 :            :  * Format a string to be used as option description in help messages
     459                 :            :  *
     460                 :            :  * @param option Option message (e.g. "-rpcuser=<user>")
     461                 :            :  * @param message Option description (e.g. "Username for JSON-RPC connections")
     462                 :            :  * @return the formatted string
     463                 :            :  */
     464                 :            : std::string HelpMessageOpt(const std::string& option, const std::string& message);
     465                 :            : 
     466                 :            : namespace common {
     467                 :            : #ifdef WIN32
     468                 :            : class WinCmdLineArgs
     469                 :            : {
     470                 :            : public:
     471                 :            :     WinCmdLineArgs();
     472                 :            :     ~WinCmdLineArgs();
     473                 :            :     std::pair<int, char**> get();
     474                 :            : 
     475                 :            : private:
     476                 :            :     int argc;
     477                 :            :     char** argv;
     478                 :            :     std::vector<std::string> args;
     479                 :            : };
     480                 :            : #endif
     481                 :            : } // namespace common
     482                 :            : 
     483                 :            : #endif // BITCOIN_COMMON_ARGS_H

Generated by: LCOV version 1.14