/bitcoin/src/common/args.cpp
Line | Count | Source |
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 <codecvt> |
23 | | #include <shellapi.h> |
24 | | #include <shlobj.h> |
25 | | #endif |
26 | | |
27 | | #include <algorithm> |
28 | | #include <cassert> |
29 | | #include <cstdint> |
30 | | #include <cstdlib> |
31 | | #include <cstring> |
32 | | #include <map> |
33 | | #include <optional> |
34 | | #include <stdexcept> |
35 | | #include <string> |
36 | | #include <utility> |
37 | | #include <variant> |
38 | | |
39 | | const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf"; |
40 | | const char * const BITCOIN_SETTINGS_FILENAME = "settings.json"; |
41 | | |
42 | | ArgsManager gArgs; |
43 | | |
44 | | /** |
45 | | * Interpret a string argument as a boolean. |
46 | | * |
47 | | * The definition of LocaleIndependentAtoi<int>() requires that non-numeric string values |
48 | | * like "foo", return 0. This means that if a user unintentionally supplies a |
49 | | * non-integer argument here, the return value is always false. This means that |
50 | | * -foo=false does what the user probably expects, but -foo=true is well defined |
51 | | * but does not do what they probably expected. |
52 | | * |
53 | | * The return value of LocaleIndependentAtoi<int>(...) is zero when given input not |
54 | | * representable as an int. |
55 | | * |
56 | | * For a more extensive discussion of this topic (and a wide range of opinions |
57 | | * on the Right Way to change this code), see PR12713. |
58 | | */ |
59 | | static bool InterpretBool(const std::string& strValue) |
60 | 99.8k | { |
61 | 99.8k | if (strValue.empty()) Branch (61:9): [True: 55.4k, False: 44.3k]
|
62 | 55.4k | return true; |
63 | 44.3k | return (LocaleIndependentAtoi<int>(strValue) != 0); |
64 | 99.8k | } |
65 | | |
66 | | static std::string SettingName(const std::string& arg) |
67 | 19.0M | { |
68 | 19.0M | return arg.size() > 0 && arg[0] == '-' ? arg.substr(1) : arg; Branch (68:12): [True: 19.0M, False: 0]
Branch (68:30): [True: 19.0M, False: 33.2k]
|
69 | 19.0M | } |
70 | | |
71 | | /** |
72 | | * Parse "name", "section.name", "noname", "section.noname" settings keys. |
73 | | * |
74 | | * @note Where an option was negated can be later checked using the |
75 | | * IsArgNegated() method. One use case for this is to have a way to disable |
76 | | * options that are not normally boolean (e.g. using -nodebuglogfile to request |
77 | | * that debug log output is not sent to any file at all). |
78 | | */ |
79 | | KeyInfo InterpretKey(std::string key) |
80 | 255k | { |
81 | 255k | KeyInfo result; |
82 | | // Split section name from key name for keys like "testnet.foo" or "regtest.bar" |
83 | 255k | size_t option_index = key.find('.'); |
84 | 255k | if (option_index != std::string::npos) { Branch (84:9): [True: 0, False: 255k]
|
85 | 0 | result.section = key.substr(0, option_index); |
86 | 0 | key.erase(0, option_index + 1); |
87 | 0 | } |
88 | 255k | if (key.starts_with("no")) { Branch (88:9): [True: 0, False: 255k]
|
89 | 0 | key.erase(0, 2); |
90 | 0 | result.negated = true; |
91 | 0 | } |
92 | 255k | result.name = key; |
93 | 255k | return result; |
94 | 255k | } |
95 | | |
96 | | /** |
97 | | * Interpret settings value based on registered flags. |
98 | | * |
99 | | * @param[in] key key information to know if key was negated |
100 | | * @param[in] value string value of setting to be parsed |
101 | | * @param[in] flags ArgsManager registered argument flags |
102 | | * @param[out] error Error description if settings value is not valid |
103 | | * |
104 | | * @return parsed settings value if it is valid, otherwise nullopt accompanied |
105 | | * by a descriptive error string |
106 | | */ |
107 | | std::optional<common::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value, |
108 | | unsigned int flags, std::string& error) |
109 | 255k | { |
110 | | // Return negated settings as false values. |
111 | 255k | if (key.negated) { Branch (111:9): [True: 0, False: 255k]
|
112 | 0 | if (flags & ArgsManager::DISALLOW_NEGATION) { Branch (112:13): [True: 0, False: 0]
|
113 | 0 | error = strprintf("Negating of -%s is meaningless and therefore forbidden", key.name); |
114 | 0 | return std::nullopt; |
115 | 0 | } |
116 | | // Double negatives like -nofoo=0 are supported (but discouraged) |
117 | 0 | if (value && !InterpretBool(*value)) { Branch (117:13): [True: 0, False: 0]
Branch (117:22): [True: 0, False: 0]
|
118 | 0 | LogPrintf("Warning: parsed potentially confusing double-negative -%s=%s\n", key.name, *value); |
119 | 0 | return true; |
120 | 0 | } |
121 | 0 | return false; |
122 | 0 | } |
123 | 255k | if (!value && (flags & ArgsManager::DISALLOW_ELISION)) { Branch (123:9): [True: 66.5k, False: 188k]
Branch (123:19): [True: 0, False: 66.5k]
|
124 | 0 | error = strprintf("Can not set -%s with no value. Please specify value with -%s=value.", key.name, key.name); |
125 | 0 | return std::nullopt; |
126 | 0 | } |
127 | 255k | return value ? *value : ""; Branch (127:12): [True: 188k, False: 66.5k]
|
128 | 255k | } |
129 | | |
130 | | // Define default constructor and destructor that are not inline, so code instantiating this class doesn't need to |
131 | | // #include class definitions for all members. |
132 | | // For example, m_settings has an internal dependency on univalue. |
133 | 11.0k | ArgsManager::ArgsManager() = default; |
134 | 0 | ArgsManager::~ArgsManager() = default; |
135 | | |
136 | | std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const |
137 | 11.0k | { |
138 | 11.0k | std::set<std::string> unsuitables; |
139 | | |
140 | 11.0k | LOCK(cs_args); |
141 | | |
142 | | // if there's no section selected, don't worry |
143 | 11.0k | if (m_network.empty()) return std::set<std::string> {}; Branch (143:9): [True: 0, False: 11.0k]
|
144 | | |
145 | | // if it's okay to use the default section for this network, don't worry |
146 | 11.0k | if (m_network == ChainTypeToString(ChainType::MAIN)) return std::set<std::string> {}; Branch (146:9): [True: 0, False: 11.0k]
|
147 | | |
148 | 88.7k | for (const auto& arg : m_network_only_args) { Branch (148:26): [True: 88.7k, False: 11.0k]
|
149 | 88.7k | if (OnlyHasDefaultSectionSetting(m_settings, m_network, SettingName(arg))) { Branch (149:13): [True: 0, False: 88.7k]
|
150 | 0 | unsuitables.insert(arg); |
151 | 0 | } |
152 | 88.7k | } |
153 | 11.0k | return unsuitables; |
154 | 11.0k | } |
155 | | |
156 | | std::list<SectionInfo> ArgsManager::GetUnrecognizedSections() const |
157 | 11.0k | { |
158 | | // Section names to be recognized in the config file. |
159 | 11.0k | static const std::set<std::string> available_sections{ |
160 | 11.0k | ChainTypeToString(ChainType::REGTEST), |
161 | 11.0k | ChainTypeToString(ChainType::SIGNET), |
162 | 11.0k | ChainTypeToString(ChainType::TESTNET), |
163 | 11.0k | ChainTypeToString(ChainType::TESTNET4), |
164 | 11.0k | ChainTypeToString(ChainType::MAIN), |
165 | 11.0k | }; |
166 | | |
167 | 11.0k | LOCK(cs_args); |
168 | 11.0k | std::list<SectionInfo> unrecognized = m_config_sections; |
169 | 11.0k | unrecognized.remove_if([](const SectionInfo& appeared){ return available_sections.find(appeared.m_name) != available_sections.end(); }); |
170 | 11.0k | return unrecognized; |
171 | 11.0k | } |
172 | | |
173 | | void ArgsManager::SelectConfigNetwork(const std::string& network) |
174 | 11.0k | { |
175 | 11.0k | LOCK(cs_args); |
176 | 11.0k | m_network = network; |
177 | 11.0k | } |
178 | | |
179 | | bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::string& error) |
180 | 11.0k | { |
181 | 11.0k | LOCK(cs_args); |
182 | 11.0k | m_settings.command_line_options.clear(); |
183 | | |
184 | 266k | for (int i = 1; i < argc; i++) { Branch (184:21): [True: 255k, False: 11.0k]
|
185 | 255k | std::string key(argv[i]); |
186 | | |
187 | | #ifdef __APPLE__ |
188 | | // At the first time when a user gets the "App downloaded from the |
189 | | // internet" warning, and clicks the Open button, macOS passes |
190 | | // a unique process serial number (PSN) as -psn_... command-line |
191 | | // argument, which we filter out. |
192 | | if (key.starts_with("-psn_")) continue; |
193 | | #endif |
194 | | |
195 | 255k | if (key == "-") break; //bitcoin-tx using stdin Branch (195:13): [True: 0, False: 255k]
|
196 | 255k | std::optional<std::string> val; |
197 | 255k | size_t is_index = key.find('='); |
198 | 255k | if (is_index != std::string::npos) { Branch (198:13): [True: 188k, False: 66.5k]
|
199 | 188k | val = key.substr(is_index + 1); |
200 | 188k | key.erase(is_index); |
201 | 188k | } |
202 | | #ifdef WIN32 |
203 | | key = ToLower(key); |
204 | | if (key[0] == '/') |
205 | | key[0] = '-'; |
206 | | #endif |
207 | | |
208 | 255k | if (key[0] != '-') { Branch (208:13): [True: 0, False: 255k]
|
209 | 0 | if (!m_accept_any_command && m_command.empty()) { Branch (209:17): [True: 0, False: 0]
Branch (209:42): [True: 0, False: 0]
|
210 | | // The first non-dash arg is a registered command |
211 | 0 | std::optional<unsigned int> flags = GetArgFlags(key); |
212 | 0 | if (!flags || !(*flags & ArgsManager::COMMAND)) { Branch (212:21): [True: 0, False: 0]
Branch (212:31): [True: 0, False: 0]
|
213 | 0 | error = strprintf("Invalid command '%s'", argv[i]); |
214 | 0 | return false; |
215 | 0 | } |
216 | 0 | } |
217 | 0 | m_command.push_back(key); |
218 | 0 | while (++i < argc) { Branch (218:20): [True: 0, False: 0]
|
219 | | // The remaining args are command args |
220 | 0 | m_command.emplace_back(argv[i]); |
221 | 0 | } |
222 | 0 | break; |
223 | 0 | } |
224 | | |
225 | | // Transform --foo to -foo |
226 | 255k | if (key.length() > 1 && key[1] == '-') Branch (226:13): [True: 255k, False: 0]
Branch (226:33): [True: 0, False: 255k]
|
227 | 0 | key.erase(0, 1); |
228 | | |
229 | | // Transform -foo to foo |
230 | 255k | key.erase(0, 1); |
231 | 255k | KeyInfo keyinfo = InterpretKey(key); |
232 | 255k | std::optional<unsigned int> flags = GetArgFlags('-' + keyinfo.name); |
233 | | |
234 | | // Unknown command line options and command line options with dot |
235 | | // characters (which are returned from InterpretKey with nonempty |
236 | | // section strings) are not valid. |
237 | 255k | if (!flags || !keyinfo.section.empty()) { Branch (237:13): [True: 0, False: 255k]
Branch (237:23): [True: 0, False: 255k]
|
238 | 0 | error = strprintf("Invalid parameter %s", argv[i]); |
239 | 0 | return false; |
240 | 0 | } |
241 | | |
242 | 255k | std::optional<common::SettingsValue> value = InterpretValue(keyinfo, val ? &*val : nullptr, *flags, error); Branch (242:78): [True: 188k, False: 66.5k]
|
243 | 255k | if (!value) return false; Branch (243:13): [True: 0, False: 255k]
|
244 | | |
245 | 255k | m_settings.command_line_options[keyinfo.name].push_back(*value); |
246 | 255k | } |
247 | | |
248 | | // we do not allow -includeconf from command line, only -noincludeconf |
249 | 11.0k | if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) { Branch (249:15): [True: 0, False: 11.0k]
|
250 | 0 | const common::SettingsSpan values{*includes}; |
251 | | // Range may be empty if -noincludeconf was passed |
252 | 0 | if (!values.empty()) { Branch (252:13): [True: 0, False: 0]
|
253 | 0 | error = "-includeconf cannot be used from commandline; -includeconf=" + values.begin()->write(); |
254 | 0 | return false; // pick first value as example |
255 | 0 | } |
256 | 0 | } |
257 | 11.0k | return true; |
258 | 11.0k | } |
259 | | |
260 | | std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) const |
261 | 510k | { |
262 | 510k | LOCK(cs_args); |
263 | 1.73M | for (const auto& arg_map : m_available_args) { Branch (263:30): [True: 1.73M, False: 0]
|
264 | 1.73M | const auto search = arg_map.second.find(name); |
265 | 1.73M | if (search != arg_map.second.end()) { Branch (265:13): [True: 510k, False: 1.22M]
|
266 | 510k | return search->second.m_flags; |
267 | 510k | } |
268 | 1.73M | } |
269 | 0 | return std::nullopt; |
270 | 510k | } |
271 | | |
272 | | fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) const |
273 | 199k | { |
274 | 199k | if (IsArgNegated(arg)) return fs::path{}; Branch (274:9): [True: 0, False: 199k]
|
275 | 199k | std::string path_str = GetArg(arg, ""); |
276 | 199k | if (path_str.empty()) return default_value; Branch (276:9): [True: 133k, False: 66.5k]
|
277 | 66.5k | fs::path result = fs::PathFromString(path_str).lexically_normal(); |
278 | | // Remove trailing slash, if present. |
279 | 66.5k | return result.has_filename() ? result : result.parent_path(); Branch (279:12): [True: 66.5k, False: 0]
|
280 | 199k | } |
281 | | |
282 | | fs::path ArgsManager::GetBlocksDirPath() const |
283 | 77.6k | { |
284 | 77.6k | LOCK(cs_args); |
285 | 77.6k | fs::path& path = m_cached_blocks_path; |
286 | | |
287 | | // Cache the path to avoid calling fs::create_directories on every call of |
288 | | // this function |
289 | 77.6k | if (!path.empty()) return path; Branch (289:9): [True: 66.5k, False: 11.0k]
|
290 | | |
291 | 11.0k | if (IsArgSet("-blocksdir")) { Branch (291:9): [True: 0, False: 11.0k]
|
292 | 0 | path = fs::absolute(GetPathArg("-blocksdir")); |
293 | 0 | if (!fs::is_directory(path)) { Branch (293:13): [True: 0, False: 0]
|
294 | 0 | path = ""; |
295 | 0 | return path; |
296 | 0 | } |
297 | 11.0k | } else { |
298 | 11.0k | path = GetDataDirBase(); |
299 | 11.0k | } |
300 | | |
301 | 11.0k | path /= fs::PathFromString(BaseParams().DataDir()); |
302 | 11.0k | path /= "blocks"; |
303 | 11.0k | fs::create_directories(path); |
304 | 11.0k | return path; |
305 | 11.0k | } |
306 | | |
307 | | fs::path ArgsManager::GetDataDir(bool net_specific) const |
308 | 432k | { |
309 | 432k | LOCK(cs_args); |
310 | 432k | fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path; Branch (310:22): [True: 377k, False: 55.4k]
|
311 | | |
312 | | // Used cached path if available |
313 | 432k | if (!path.empty()) return path; Branch (313:9): [True: 399k, False: 33.2k]
|
314 | | |
315 | 33.2k | const fs::path datadir{GetPathArg("-datadir")}; |
316 | 33.2k | if (!datadir.empty()) { Branch (316:9): [True: 33.2k, False: 0]
|
317 | 33.2k | path = fs::absolute(datadir); |
318 | 33.2k | if (!fs::is_directory(path)) { Branch (318:13): [True: 0, False: 33.2k]
|
319 | 0 | path = ""; |
320 | 0 | return path; |
321 | 0 | } |
322 | 33.2k | } else { |
323 | 0 | path = GetDefaultDataDir(); |
324 | 0 | } |
325 | | |
326 | 33.2k | if (net_specific && !BaseParams().DataDir().empty()) { Branch (326:9): [True: 11.0k, False: 22.1k]
Branch (326:25): [True: 11.0k, False: 0]
|
327 | 11.0k | path /= fs::PathFromString(BaseParams().DataDir()); |
328 | 11.0k | } |
329 | | |
330 | 33.2k | return path; |
331 | 33.2k | } |
332 | | |
333 | | void ArgsManager::ClearPathCache() |
334 | 11.0k | { |
335 | 11.0k | LOCK(cs_args); |
336 | | |
337 | 11.0k | m_cached_datadir_path = fs::path(); |
338 | 11.0k | m_cached_network_datadir_path = fs::path(); |
339 | 11.0k | m_cached_blocks_path = fs::path(); |
340 | 11.0k | } |
341 | | |
342 | | std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const |
343 | 0 | { |
344 | 0 | Command ret; |
345 | 0 | LOCK(cs_args); |
346 | 0 | auto it = m_command.begin(); |
347 | 0 | if (it == m_command.end()) { Branch (347:9): [True: 0, False: 0]
|
348 | | // No command was passed |
349 | 0 | return std::nullopt; |
350 | 0 | } |
351 | 0 | if (!m_accept_any_command) { Branch (351:9): [True: 0, False: 0]
|
352 | | // The registered command |
353 | 0 | ret.command = *(it++); |
354 | 0 | } |
355 | 0 | while (it != m_command.end()) { Branch (355:12): [True: 0, False: 0]
|
356 | | // The unregistered command and args (if any) |
357 | 0 | ret.args.push_back(*(it++)); |
358 | 0 | } |
359 | 0 | return ret; |
360 | 0 | } |
361 | | |
362 | | std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const |
363 | 882k | { |
364 | 882k | std::vector<std::string> result; |
365 | 882k | for (const common::SettingsValue& value : GetSettingsList(strArg)) { Branch (365:45): [True: 122k, False: 882k]
|
366 | 122k | result.push_back(value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str()); Branch (366:26): [True: 0, False: 122k]
Branch (366:50): [True: 0, False: 122k]
|
367 | 122k | } |
368 | 882k | return result; |
369 | 882k | } |
370 | | |
371 | | bool ArgsManager::IsArgSet(const std::string& strArg) const |
372 | 221k | { |
373 | 221k | return !GetSetting(strArg).isNull(); |
374 | 221k | } |
375 | | |
376 | | bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp, bool backup) const |
377 | 44.3k | { |
378 | 44.3k | fs::path settings = GetPathArg("-settings", BITCOIN_SETTINGS_FILENAME); |
379 | 44.3k | if (settings.empty()) { Branch (379:9): [True: 0, False: 44.3k]
|
380 | 0 | return false; |
381 | 0 | } |
382 | 44.3k | if (backup) { Branch (382:9): [True: 0, False: 44.3k]
|
383 | 0 | settings += ".bak"; |
384 | 0 | } |
385 | 44.3k | if (filepath) { Branch (385:9): [True: 33.2k, False: 11.0k]
|
386 | 33.2k | *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings); Branch (386:60): [True: 11.0k, False: 22.1k]
|
387 | 33.2k | } |
388 | 44.3k | return true; |
389 | 44.3k | } |
390 | | |
391 | | static void SaveErrors(const std::vector<std::string> errors, std::vector<std::string>* error_out) |
392 | 0 | { |
393 | 0 | for (const auto& error : errors) { Branch (393:28): [True: 0, False: 0]
|
394 | 0 | if (error_out) { Branch (394:13): [True: 0, False: 0]
|
395 | 0 | error_out->emplace_back(error); |
396 | 0 | } else { |
397 | 0 | LogPrintf("%s\n", error); |
398 | 0 | } |
399 | 0 | } |
400 | 0 | } |
401 | | |
402 | | bool ArgsManager::ReadSettingsFile(std::vector<std::string>* errors) |
403 | 11.0k | { |
404 | 11.0k | fs::path path; |
405 | 11.0k | if (!GetSettingsPath(&path, /* temp= */ false)) { Branch (405:9): [True: 0, False: 11.0k]
|
406 | 0 | return true; // Do nothing if settings file disabled. |
407 | 0 | } |
408 | | |
409 | 11.0k | LOCK(cs_args); |
410 | 11.0k | m_settings.rw_settings.clear(); |
411 | 11.0k | std::vector<std::string> read_errors; |
412 | 11.0k | if (!common::ReadSettings(path, m_settings.rw_settings, read_errors)) { Branch (412:9): [True: 0, False: 11.0k]
|
413 | 0 | SaveErrors(read_errors, errors); |
414 | 0 | return false; |
415 | 0 | } |
416 | 11.0k | for (const auto& setting : m_settings.rw_settings) { Branch (416:30): [True: 0, False: 11.0k]
|
417 | 0 | KeyInfo key = InterpretKey(setting.first); // Split setting key into section and argname |
418 | 0 | if (!GetArgFlags('-' + key.name)) { Branch (418:13): [True: 0, False: 0]
|
419 | 0 | LogPrintf("Ignoring unknown rw_settings value %s\n", setting.first); |
420 | 0 | } |
421 | 0 | } |
422 | 11.0k | return true; |
423 | 11.0k | } |
424 | | |
425 | | bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors, bool backup) const |
426 | 11.0k | { |
427 | 11.0k | fs::path path, path_tmp; |
428 | 11.0k | if (!GetSettingsPath(&path, /*temp=*/false, backup) || !GetSettingsPath(&path_tmp, /*temp=*/true, backup)) { Branch (428:9): [True: 0, False: 11.0k]
Branch (428:60): [True: 0, False: 11.0k]
|
429 | 0 | throw std::logic_error("Attempt to write settings file when dynamic settings are disabled."); |
430 | 0 | } |
431 | | |
432 | 11.0k | LOCK(cs_args); |
433 | 11.0k | std::vector<std::string> write_errors; |
434 | 11.0k | if (!common::WriteSettings(path_tmp, m_settings.rw_settings, write_errors)) { Branch (434:9): [True: 0, False: 11.0k]
|
435 | 0 | SaveErrors(write_errors, errors); |
436 | 0 | return false; |
437 | 0 | } |
438 | 11.0k | if (!RenameOver(path_tmp, path)) { Branch (438:9): [True: 0, False: 11.0k]
|
439 | 0 | SaveErrors({strprintf("Failed renaming settings file %s to %s\n", fs::PathToString(path_tmp), fs::PathToString(path))}, errors); |
440 | 0 | return false; |
441 | 0 | } |
442 | 11.0k | return true; |
443 | 11.0k | } |
444 | | |
445 | | common::SettingsValue ArgsManager::GetPersistentSetting(const std::string& name) const |
446 | 0 | { |
447 | 0 | LOCK(cs_args); |
448 | 0 | return common::GetSetting(m_settings, m_network, name, !UseDefaultSection("-" + name), |
449 | 0 | /*ignore_nonpersistent=*/true, /*get_chain_type=*/false); |
450 | 0 | } |
451 | | |
452 | | bool ArgsManager::IsArgNegated(const std::string& strArg) const |
453 | 255k | { |
454 | 255k | return GetSetting(strArg).isFalse(); |
455 | 255k | } |
456 | | |
457 | | std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const |
458 | 332k | { |
459 | 332k | return GetArg(strArg).value_or(strDefault); |
460 | 332k | } |
461 | | |
462 | | std::optional<std::string> ArgsManager::GetArg(const std::string& strArg) const |
463 | 587k | { |
464 | 587k | const common::SettingsValue value = GetSetting(strArg); |
465 | 587k | return SettingToString(value); |
466 | 587k | } |
467 | | |
468 | | std::optional<std::string> SettingToString(const common::SettingsValue& value) |
469 | 587k | { |
470 | 587k | if (value.isNull()) return std::nullopt; Branch (470:9): [True: 476k, False: 110k]
|
471 | 110k | if (value.isFalse()) return "0"; Branch (471:9): [True: 0, False: 110k]
|
472 | 110k | if (value.isTrue()) return "1"; Branch (472:9): [True: 0, False: 110k]
|
473 | 110k | if (value.isNum()) return value.getValStr(); Branch (473:9): [True: 0, False: 110k]
|
474 | 110k | return value.get_str(); |
475 | 110k | } |
476 | | |
477 | | std::string SettingToString(const common::SettingsValue& value, const std::string& strDefault) |
478 | 0 | { |
479 | 0 | return SettingToString(value).value_or(strDefault); |
480 | 0 | } |
481 | | |
482 | | int64_t ArgsManager::GetIntArg(const std::string& strArg, int64_t nDefault) const |
483 | 485k | { |
484 | 485k | return GetIntArg(strArg).value_or(nDefault); |
485 | 485k | } |
486 | | |
487 | | std::optional<int64_t> ArgsManager::GetIntArg(const std::string& strArg) const |
488 | 729k | { |
489 | 729k | const common::SettingsValue value = GetSetting(strArg); |
490 | 729k | return SettingToInt(value); |
491 | 729k | } |
492 | | |
493 | | std::optional<int64_t> SettingToInt(const common::SettingsValue& value) |
494 | 729k | { |
495 | 729k | if (value.isNull()) return std::nullopt; Branch (495:9): [True: 488k, False: 241k]
|
496 | 241k | if (value.isFalse()) return 0; Branch (496:9): [True: 0, False: 241k]
|
497 | 241k | if (value.isTrue()) return 1; Branch (497:9): [True: 0, False: 241k]
|
498 | 241k | if (value.isNum()) return value.getInt<int64_t>(); Branch (498:9): [True: 0, False: 241k]
|
499 | 241k | return LocaleIndependentAtoi<int64_t>(value.get_str()); |
500 | 241k | } |
501 | | |
502 | | int64_t SettingToInt(const common::SettingsValue& value, int64_t nDefault) |
503 | 0 | { |
504 | 0 | return SettingToInt(value).value_or(nDefault); |
505 | 0 | } |
506 | | |
507 | | bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const |
508 | 16.0M | { |
509 | 16.0M | return GetBoolArg(strArg).value_or(fDefault); |
510 | 16.0M | } |
511 | | |
512 | | std::optional<bool> ArgsManager::GetBoolArg(const std::string& strArg) const |
513 | 16.2M | { |
514 | 16.2M | const common::SettingsValue value = GetSetting(strArg); |
515 | 16.2M | return SettingToBool(value); |
516 | 16.2M | } |
517 | | |
518 | | std::optional<bool> SettingToBool(const common::SettingsValue& value) |
519 | 16.2M | { |
520 | 16.2M | if (value.isNull()) return std::nullopt; Branch (520:9): [True: 16.1M, False: 77.6k]
|
521 | 77.6k | if (value.isBool()) return value.get_bool(); Branch (521:9): [True: 0, False: 77.6k]
|
522 | 77.6k | return InterpretBool(value.get_str()); |
523 | 77.6k | } |
524 | | |
525 | | bool SettingToBool(const common::SettingsValue& value, bool fDefault) |
526 | 0 | { |
527 | 0 | return SettingToBool(value).value_or(fDefault); |
528 | 0 | } |
529 | | |
530 | | bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue) |
531 | 11.0k | { |
532 | 11.0k | LOCK(cs_args); |
533 | 11.0k | if (IsArgSet(strArg)) return false; Branch (533:9): [True: 0, False: 11.0k]
|
534 | 11.0k | ForceSetArg(strArg, strValue); |
535 | 11.0k | return true; |
536 | 11.0k | } |
537 | | |
538 | | bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue) |
539 | 11.0k | { |
540 | 11.0k | if (fValue) Branch (540:9): [True: 11.0k, False: 0]
|
541 | 11.0k | return SoftSetArg(strArg, std::string("1")); |
542 | 0 | else |
543 | 0 | return SoftSetArg(strArg, std::string("0")); |
544 | 11.0k | } |
545 | | |
546 | | void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue) |
547 | 11.0k | { |
548 | 11.0k | LOCK(cs_args); |
549 | 11.0k | m_settings.forced_settings[SettingName(strArg)] = strValue; |
550 | 11.0k | } |
551 | | |
552 | | void ArgsManager::AddCommand(const std::string& cmd, const std::string& help) |
553 | 0 | { |
554 | 0 | Assert(cmd.find('=') == std::string::npos); |
555 | 0 | Assert(cmd.at(0) != '-'); |
556 | |
|
557 | 0 | LOCK(cs_args); |
558 | 0 | m_accept_any_command = false; // latch to false |
559 | 0 | std::map<std::string, Arg>& arg_map = m_available_args[OptionsCategory::COMMANDS]; |
560 | 0 | auto ret = arg_map.emplace(cmd, Arg{"", help, ArgsManager::COMMAND}); |
561 | 0 | Assert(ret.second); // Fail on duplicate commands |
562 | 0 | } |
563 | | |
564 | | void ArgsManager::AddArg(const std::string& name, const std::string& help, unsigned int flags, const OptionsCategory& cat) |
565 | 2.10M | { |
566 | 2.10M | Assert((flags & ArgsManager::COMMAND) == 0); // use AddCommand |
567 | | |
568 | | // Split arg name from its help param |
569 | 2.10M | size_t eq_index = name.find('='); |
570 | 2.10M | if (eq_index == std::string::npos) { Branch (570:9): [True: 920k, False: 1.18M]
|
571 | 920k | eq_index = name.size(); |
572 | 920k | } |
573 | 2.10M | std::string arg_name = name.substr(0, eq_index); |
574 | | |
575 | 2.10M | LOCK(cs_args); |
576 | 2.10M | std::map<std::string, Arg>& arg_map = m_available_args[cat]; |
577 | 2.10M | auto ret = arg_map.emplace(arg_name, Arg{name.substr(eq_index, name.size() - eq_index), help, flags}); |
578 | 2.10M | assert(ret.second); // Make sure an insertion actually happened Branch (578:5): [True: 2.10M, False: 0]
|
579 | | |
580 | 2.10M | if (flags & ArgsManager::NETWORK_ONLY) { Branch (580:9): [True: 88.7k, False: 2.01M]
|
581 | 88.7k | m_network_only_args.emplace(arg_name); |
582 | 88.7k | } |
583 | 2.10M | } |
584 | | |
585 | | void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names) |
586 | 22.1k | { |
587 | 221k | for (const std::string& name : names) { Branch (587:34): [True: 221k, False: 22.1k]
|
588 | 221k | AddArg(name, "", ArgsManager::ALLOW_ANY, OptionsCategory::HIDDEN); |
589 | 221k | } |
590 | 22.1k | } |
591 | | |
592 | | void ArgsManager::CheckMultipleCLIArgs() const |
593 | 0 | { |
594 | 0 | LOCK(cs_args); |
595 | 0 | std::vector<std::string> found{}; |
596 | 0 | auto cmds = m_available_args.find(OptionsCategory::CLI_COMMANDS); |
597 | 0 | if (cmds != m_available_args.end()) { Branch (597:9): [True: 0, False: 0]
|
598 | 0 | for (const auto& [cmd, argspec] : cmds->second) { Branch (598:41): [True: 0, False: 0]
|
599 | 0 | if (IsArgSet(cmd)) { Branch (599:17): [True: 0, False: 0]
|
600 | 0 | found.push_back(cmd); |
601 | 0 | } |
602 | 0 | } |
603 | 0 | if (found.size() > 1) { Branch (603:13): [True: 0, False: 0]
|
604 | 0 | throw std::runtime_error(strprintf("Only one of %s may be specified.", util::Join(found, ", "))); |
605 | 0 | } |
606 | 0 | } |
607 | 0 | } |
608 | | |
609 | | std::string ArgsManager::GetHelpMessage() const |
610 | 0 | { |
611 | 0 | const bool show_debug = GetBoolArg("-help-debug", false); |
612 | |
|
613 | 0 | std::string usage; |
614 | 0 | LOCK(cs_args); |
615 | 0 | for (const auto& arg_map : m_available_args) { Branch (615:30): [True: 0, False: 0]
|
616 | 0 | switch(arg_map.first) { |
617 | 0 | case OptionsCategory::OPTIONS: Branch (617:13): [True: 0, False: 0]
|
618 | 0 | usage += HelpMessageGroup("Options:"); |
619 | 0 | break; |
620 | 0 | case OptionsCategory::CONNECTION: Branch (620:13): [True: 0, False: 0]
|
621 | 0 | usage += HelpMessageGroup("Connection options:"); |
622 | 0 | break; |
623 | 0 | case OptionsCategory::ZMQ: Branch (623:13): [True: 0, False: 0]
|
624 | 0 | usage += HelpMessageGroup("ZeroMQ notification options:"); |
625 | 0 | break; |
626 | 0 | case OptionsCategory::DEBUG_TEST: Branch (626:13): [True: 0, False: 0]
|
627 | 0 | usage += HelpMessageGroup("Debugging/Testing options:"); |
628 | 0 | break; |
629 | 0 | case OptionsCategory::NODE_RELAY: Branch (629:13): [True: 0, False: 0]
|
630 | 0 | usage += HelpMessageGroup("Node relay options:"); |
631 | 0 | break; |
632 | 0 | case OptionsCategory::BLOCK_CREATION: Branch (632:13): [True: 0, False: 0]
|
633 | 0 | usage += HelpMessageGroup("Block creation options:"); |
634 | 0 | break; |
635 | 0 | case OptionsCategory::RPC: Branch (635:13): [True: 0, False: 0]
|
636 | 0 | usage += HelpMessageGroup("RPC server options:"); |
637 | 0 | break; |
638 | 0 | case OptionsCategory::IPC: Branch (638:13): [True: 0, False: 0]
|
639 | 0 | usage += HelpMessageGroup("IPC interprocess connection options:"); |
640 | 0 | break; |
641 | 0 | case OptionsCategory::WALLET: Branch (641:13): [True: 0, False: 0]
|
642 | 0 | usage += HelpMessageGroup("Wallet options:"); |
643 | 0 | break; |
644 | 0 | case OptionsCategory::WALLET_DEBUG_TEST: Branch (644:13): [True: 0, False: 0]
|
645 | 0 | if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:"); Branch (645:21): [True: 0, False: 0]
|
646 | 0 | break; |
647 | 0 | case OptionsCategory::CHAINPARAMS: Branch (647:13): [True: 0, False: 0]
|
648 | 0 | usage += HelpMessageGroup("Chain selection options:"); |
649 | 0 | break; |
650 | 0 | case OptionsCategory::GUI: Branch (650:13): [True: 0, False: 0]
|
651 | 0 | usage += HelpMessageGroup("UI Options:"); |
652 | 0 | break; |
653 | 0 | case OptionsCategory::COMMANDS: Branch (653:13): [True: 0, False: 0]
|
654 | 0 | usage += HelpMessageGroup("Commands:"); |
655 | 0 | break; |
656 | 0 | case OptionsCategory::REGISTER_COMMANDS: Branch (656:13): [True: 0, False: 0]
|
657 | 0 | usage += HelpMessageGroup("Register Commands:"); |
658 | 0 | break; |
659 | 0 | case OptionsCategory::CLI_COMMANDS: Branch (659:13): [True: 0, False: 0]
|
660 | 0 | usage += HelpMessageGroup("CLI Commands:"); |
661 | 0 | break; |
662 | 0 | default: Branch (662:13): [True: 0, False: 0]
|
663 | 0 | break; |
664 | 0 | } |
665 | | |
666 | | // When we get to the hidden options, stop |
667 | 0 | if (arg_map.first == OptionsCategory::HIDDEN) break; Branch (667:13): [True: 0, False: 0]
|
668 | | |
669 | 0 | for (const auto& arg : arg_map.second) { Branch (669:30): [True: 0, False: 0]
|
670 | 0 | if (show_debug || !(arg.second.m_flags & ArgsManager::DEBUG_ONLY)) { Branch (670:17): [True: 0, False: 0]
Branch (670:31): [True: 0, False: 0]
|
671 | 0 | std::string name; |
672 | 0 | if (arg.second.m_help_param.empty()) { Branch (672:21): [True: 0, False: 0]
|
673 | 0 | name = arg.first; |
674 | 0 | } else { |
675 | 0 | name = arg.first + arg.second.m_help_param; |
676 | 0 | } |
677 | 0 | usage += HelpMessageOpt(name, arg.second.m_help_text); |
678 | 0 | } |
679 | 0 | } |
680 | 0 | } |
681 | 0 | return usage; |
682 | 0 | } |
683 | | |
684 | | bool HelpRequested(const ArgsManager& args) |
685 | 11.0k | { |
686 | 11.0k | return args.IsArgSet("-?") || args.IsArgSet("-h") || args.IsArgSet("-help") || args.IsArgSet("-help-debug"); Branch (686:12): [True: 0, False: 11.0k]
Branch (686:35): [True: 0, False: 11.0k]
Branch (686:58): [True: 0, False: 11.0k]
Branch (686:84): [True: 0, False: 11.0k]
|
687 | 11.0k | } |
688 | | |
689 | | void SetupHelpOptions(ArgsManager& args) |
690 | 11.0k | { |
691 | 11.0k | args.AddArg("-help", "Print this help message and exit (also -h or -?)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); |
692 | 11.0k | args.AddHiddenArgs({"-h", "-?"}); |
693 | 11.0k | } |
694 | | |
695 | | static const int screenWidth = 79; |
696 | | static const int optIndent = 2; |
697 | | static const int msgIndent = 7; |
698 | | |
699 | 0 | std::string HelpMessageGroup(const std::string &message) { |
700 | 0 | return std::string(message) + std::string("\n\n"); |
701 | 0 | } |
702 | | |
703 | 0 | std::string HelpMessageOpt(const std::string &option, const std::string &message) { |
704 | 0 | return std::string(optIndent,' ') + std::string(option) + |
705 | 0 | std::string("\n") + std::string(msgIndent,' ') + |
706 | 0 | FormatParagraph(message, screenWidth - msgIndent, msgIndent) + |
707 | 0 | std::string("\n\n"); |
708 | 0 | } |
709 | | |
710 | | const std::vector<std::string> TEST_OPTIONS_DOC{ |
711 | | "addrman (use deterministic addrman)", |
712 | | "bip94 (enforce BIP94 consensus rules)", |
713 | | }; |
714 | | |
715 | | bool HasTestOption(const ArgsManager& args, const std::string& test_option) |
716 | 33.2k | { |
717 | 33.2k | const auto options = args.GetArgs("-test"); |
718 | 33.2k | return std::any_of(options.begin(), options.end(), [test_option](const auto& option) { |
719 | 0 | return option == test_option; |
720 | 0 | }); |
721 | 33.2k | } |
722 | | |
723 | | fs::path GetDefaultDataDir() |
724 | 11.0k | { |
725 | | // Windows: |
726 | | // old: C:\Users\Username\AppData\Roaming\Bitcoin |
727 | | // new: C:\Users\Username\AppData\Local\Bitcoin |
728 | | // macOS: ~/Library/Application Support/Bitcoin |
729 | | // Unix-like: ~/.bitcoin |
730 | | #ifdef WIN32 |
731 | | // Windows |
732 | | // Check for existence of datadir in old location and keep it there |
733 | | fs::path legacy_path = GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin"; |
734 | | if (fs::exists(legacy_path)) return legacy_path; |
735 | | |
736 | | // Otherwise, fresh installs can start in the new, "proper" location |
737 | | return GetSpecialFolderPath(CSIDL_LOCAL_APPDATA) / "Bitcoin"; |
738 | | #else |
739 | 11.0k | fs::path pathRet; |
740 | 11.0k | char* pszHome = getenv("HOME"); |
741 | 11.0k | if (pszHome == nullptr || strlen(pszHome) == 0) Branch (741:9): [True: 0, False: 11.0k]
Branch (741:31): [True: 0, False: 11.0k]
|
742 | 0 | pathRet = fs::path("/"); |
743 | 11.0k | else |
744 | 11.0k | pathRet = fs::path(pszHome); |
745 | | #ifdef __APPLE__ |
746 | | // macOS |
747 | | return pathRet / "Library/Application Support/Bitcoin"; |
748 | | #else |
749 | | // Unix-like |
750 | 11.0k | return pathRet / ".bitcoin"; |
751 | 11.0k | #endif |
752 | 11.0k | #endif |
753 | 11.0k | } |
754 | | |
755 | | bool CheckDataDirOption(const ArgsManager& args) |
756 | 22.1k | { |
757 | 22.1k | const fs::path datadir{args.GetPathArg("-datadir")}; |
758 | 22.1k | return datadir.empty() || fs::is_directory(fs::absolute(datadir)); Branch (758:12): [True: 0, False: 22.1k]
Branch (758:31): [True: 22.1k, False: 0]
|
759 | 22.1k | } |
760 | | |
761 | | fs::path ArgsManager::GetConfigFilePath() const |
762 | 22.1k | { |
763 | 22.1k | LOCK(cs_args); |
764 | 22.1k | return *Assert(m_config_path); |
765 | 22.1k | } |
766 | | |
767 | | void ArgsManager::SetConfigFilePath(fs::path path) |
768 | 0 | { |
769 | 0 | LOCK(cs_args); |
770 | 0 | assert(!m_config_path); Branch (770:5): [True: 0, False: 0]
|
771 | 0 | m_config_path = path; |
772 | 0 | } |
773 | | |
774 | | ChainType ArgsManager::GetChainType() const |
775 | 22.1k | { |
776 | 22.1k | std::variant<ChainType, std::string> arg = GetChainArg(); |
777 | 22.1k | if (auto* parsed = std::get_if<ChainType>(&arg)) return *parsed; Branch (777:15): [True: 22.1k, False: 0]
|
778 | 0 | throw std::runtime_error(strprintf("Unknown chain %s.", std::get<std::string>(arg))); |
779 | 22.1k | } |
780 | | |
781 | | std::string ArgsManager::GetChainTypeString() const |
782 | 0 | { |
783 | 0 | auto arg = GetChainArg(); |
784 | 0 | if (auto* parsed = std::get_if<ChainType>(&arg)) return ChainTypeToString(*parsed); Branch (784:15): [True: 0, False: 0]
|
785 | 0 | return std::get<std::string>(arg); |
786 | 0 | } |
787 | | |
788 | | std::variant<ChainType, std::string> ArgsManager::GetChainArg() const |
789 | 22.1k | { |
790 | 88.7k | auto get_net = [&](const std::string& arg) { |
791 | 88.7k | LOCK(cs_args); |
792 | 88.7k | common::SettingsValue value = common::GetSetting(m_settings, /* section= */ "", SettingName(arg), |
793 | 88.7k | /* ignore_default_section_config= */ false, |
794 | 88.7k | /*ignore_nonpersistent=*/false, |
795 | 88.7k | /* get_chain_type= */ true); |
796 | 88.7k | return value.isNull() ? false : value.isBool() ? value.get_bool() : InterpretBool(value.get_str()); Branch (796:16): [True: 66.5k, False: 22.1k]
Branch (796:41): [True: 0, False: 22.1k]
|
797 | 88.7k | }; |
798 | | |
799 | 22.1k | const bool fRegTest = get_net("-regtest"); |
800 | 22.1k | const bool fSigNet = get_net("-signet"); |
801 | 22.1k | const bool fTestNet = get_net("-testnet"); |
802 | 22.1k | const bool fTestNet4 = get_net("-testnet4"); |
803 | 22.1k | const auto chain_arg = GetArg("-chain"); |
804 | | |
805 | 22.1k | if ((int)chain_arg.has_value() + (int)fRegTest + (int)fSigNet + (int)fTestNet + (int)fTestNet4 > 1) { Branch (805:9): [True: 0, False: 22.1k]
|
806 | 0 | throw std::runtime_error("Invalid combination of -regtest, -signet, -testnet, -testnet4 and -chain. Can use at most one."); |
807 | 0 | } |
808 | 22.1k | if (chain_arg) { Branch (808:9): [True: 0, False: 22.1k]
|
809 | 0 | if (auto parsed = ChainTypeFromString(*chain_arg)) return *parsed; Branch (809:18): [True: 0, False: 0]
|
810 | | // Not a known string, so return original string |
811 | 0 | return *chain_arg; |
812 | 0 | } |
813 | 22.1k | if (fRegTest) return ChainType::REGTEST; Branch (813:9): [True: 22.1k, False: 0]
|
814 | 0 | if (fSigNet) return ChainType::SIGNET; Branch (814:9): [True: 0, False: 0]
|
815 | 0 | if (fTestNet) return ChainType::TESTNET; Branch (815:9): [True: 0, False: 0]
|
816 | 0 | if (fTestNet4) return ChainType::TESTNET4; Branch (816:9): [True: 0, False: 0]
|
817 | 0 | return ChainType::MAIN; |
818 | 0 | } |
819 | | |
820 | | bool ArgsManager::UseDefaultSection(const std::string& arg) const |
821 | 18.9M | { |
822 | 18.9M | return m_network == ChainTypeToString(ChainType::MAIN) || m_network_only_args.count(arg) == 0; Branch (822:12): [True: 0, False: 18.9M]
Branch (822:63): [True: 18.4M, False: 438k]
|
823 | 18.9M | } |
824 | | |
825 | | common::SettingsValue ArgsManager::GetSetting(const std::string& arg) const |
826 | 18.0M | { |
827 | 18.0M | LOCK(cs_args); |
828 | 18.0M | return common::GetSetting( |
829 | 18.0M | m_settings, m_network, SettingName(arg), !UseDefaultSection(arg), |
830 | 18.0M | /*ignore_nonpersistent=*/false, /*get_chain_type=*/false); |
831 | 18.0M | } |
832 | | |
833 | | std::vector<common::SettingsValue> ArgsManager::GetSettingsList(const std::string& arg) const |
834 | 904k | { |
835 | 904k | LOCK(cs_args); |
836 | 904k | return common::GetSettingsList(m_settings, m_network, SettingName(arg), !UseDefaultSection(arg)); |
837 | 904k | } |
838 | | |
839 | | void ArgsManager::logArgsPrefix( |
840 | | const std::string& prefix, |
841 | | const std::string& section, |
842 | | const std::map<std::string, std::vector<common::SettingsValue>>& args) const |
843 | 11.0k | { |
844 | 11.0k | std::string section_str = section.empty() ? "" : "[" + section + "] "; Branch (844:31): [True: 11.0k, False: 0]
|
845 | 232k | for (const auto& arg : args) { Branch (845:26): [True: 232k, False: 11.0k]
|
846 | 255k | for (const auto& value : arg.second) { Branch (846:32): [True: 255k, False: 232k]
|
847 | 255k | std::optional<unsigned int> flags = GetArgFlags('-' + arg.first); |
848 | 255k | if (flags) { Branch (848:17): [True: 255k, False: 0]
|
849 | 255k | std::string value_str = (*flags & SENSITIVE) ? "****" : value.write(); Branch (849:41): [True: 0, False: 255k]
|
850 | 255k | LogPrintf("%s %s%s=%s\n", prefix, section_str, arg.first, value_str); |
851 | 255k | } |
852 | 255k | } |
853 | 232k | } |
854 | 11.0k | } |
855 | | |
856 | | void ArgsManager::LogArgs() const |
857 | 11.0k | { |
858 | 11.0k | LOCK(cs_args); |
859 | 11.0k | for (const auto& section : m_settings.ro_config) { Branch (859:30): [True: 0, False: 11.0k]
|
860 | 0 | logArgsPrefix("Config file arg:", section.first, section.second); |
861 | 0 | } |
862 | 11.0k | for (const auto& setting : m_settings.rw_settings) { Branch (862:30): [True: 0, False: 11.0k]
|
863 | 0 | LogPrintf("Setting file arg: %s = %s\n", setting.first, setting.second.write()); |
864 | 0 | } |
865 | 11.0k | logArgsPrefix("Command-line arg:", "", m_settings.command_line_options); |
866 | 11.0k | } |
867 | | |
868 | | namespace common { |
869 | | #ifdef WIN32 |
870 | | WinCmdLineArgs::WinCmdLineArgs() |
871 | | { |
872 | | wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc); |
873 | | std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf8_cvt; |
874 | | argv = new char*[argc]; |
875 | | args.resize(argc); |
876 | | for (int i = 0; i < argc; i++) { |
877 | | args[i] = utf8_cvt.to_bytes(wargv[i]); |
878 | | argv[i] = &*args[i].begin(); |
879 | | } |
880 | | LocalFree(wargv); |
881 | | } |
882 | | |
883 | | WinCmdLineArgs::~WinCmdLineArgs() |
884 | | { |
885 | | delete[] argv; |
886 | | } |
887 | | |
888 | | std::pair<int, char**> WinCmdLineArgs::get() |
889 | | { |
890 | | return std::make_pair(argc, argv); |
891 | | } |
892 | | #endif |
893 | | } // namespace common |