Commit c8bb3c0b authored by Max Kellermann's avatar Max Kellermann

CommandLine: migrate from class Error to C++ exceptions

parent 9990e847
...@@ -35,8 +35,9 @@ ...@@ -35,8 +35,9 @@
#include "fs/Traits.hxx" #include "fs/Traits.hxx"
#include "fs/FileSystem.hxx" #include "fs/FileSystem.hxx"
#include "fs/StandardDirectory.hxx" #include "fs/StandardDirectory.hxx"
#include "system/Error.hxx"
#include "util/Macros.hxx" #include "util/Macros.hxx"
#include "util/Error.hxx" #include "util/RuntimeError.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "util/OptionDef.hxx" #include "util/OptionDef.hxx"
#include "util/OptionParser.hxx" #include "util/OptionParser.hxx"
...@@ -301,9 +302,8 @@ bool ConfigLoader::TryFile(const AllocatedPath &base_path, ...@@ -301,9 +302,8 @@ bool ConfigLoader::TryFile(const AllocatedPath &base_path,
return TryFile(full_path); return TryFile(full_path);
} }
bool void
parse_cmdline(int argc, char **argv, struct options *options, ParseCommandLine(int argc, char **argv, struct options *options)
Error &error)
{ {
bool use_config_file = true; bool use_config_file = true;
options->kill = false; options->kill = false;
...@@ -341,9 +341,8 @@ parse_cmdline(int argc, char **argv, struct options *options, ...@@ -341,9 +341,8 @@ parse_cmdline(int argc, char **argv, struct options *options,
if (parser.CheckOption(opt_help, opt_help_alt)) if (parser.CheckOption(opt_help, opt_help_alt))
help(); help();
error.Format(cmdline_domain, "invalid option: %s", throw FormatRuntimeError("invalid option: %s",
parser.GetOption()); parser.GetOption());
return false;
} }
/* initialize the logging library, so the configuration file /* initialize the logging library, so the configuration file
...@@ -353,7 +352,7 @@ parse_cmdline(int argc, char **argv, struct options *options, ...@@ -353,7 +352,7 @@ parse_cmdline(int argc, char **argv, struct options *options,
if (!use_config_file) { if (!use_config_file) {
LogDebug(cmdline_domain, LogDebug(cmdline_domain,
"Ignoring config, using daemon defaults"); "Ignoring config, using daemon defaults");
return true; return;
} }
// Second pass: find non-option parameters (i.e. config file) // Second pass: find non-option parameters (i.e. config file)
...@@ -365,8 +364,8 @@ parse_cmdline(int argc, char **argv, struct options *options, ...@@ -365,8 +364,8 @@ parse_cmdline(int argc, char **argv, struct options *options,
config_file = argv[i]; config_file = argv[i];
continue; continue;
} }
error.Set(cmdline_domain, "too many arguments");
return false; throw std::runtime_error("too many arguments");
} }
if (config_file != nullptr) { if (config_file != nullptr) {
...@@ -375,16 +374,14 @@ parse_cmdline(int argc, char **argv, struct options *options, ...@@ -375,16 +374,14 @@ parse_cmdline(int argc, char **argv, struct options *options,
wchar_t buffer[MAX_PATH]; wchar_t buffer[MAX_PATH];
auto result = MultiByteToWideChar(CP_ACP, 0, config_file, -1, auto result = MultiByteToWideChar(CP_ACP, 0, config_file, -1,
buffer, ARRAY_SIZE(buffer)); buffer, ARRAY_SIZE(buffer));
if (result <= 0) { if (result <= 0)
error.SetLastError("MultiByteToWideChar() failed"); throw MakeLastError("MultiByteToWideChar() failed");
return false;
}
ReadConfigFile(Path::FromFS(buffer)); ReadConfigFile(Path::FromFS(buffer));
#else #else
ReadConfigFile(Path::FromFS(config_file)); ReadConfigFile(Path::FromFS(config_file));
#endif #endif
return true; return;
} }
/* use default configuration file path */ /* use default configuration file path */
...@@ -403,10 +400,6 @@ parse_cmdline(int argc, char **argv, struct options *options, ...@@ -403,10 +400,6 @@ parse_cmdline(int argc, char **argv, struct options *options,
loader.TryFile(GetHomeDir(), USER_CONFIG_FILE_LOCATION2) || loader.TryFile(GetHomeDir(), USER_CONFIG_FILE_LOCATION2) ||
loader.TryFile(Path::FromFS(SYSTEM_CONFIG_FILE_LOCATION)); loader.TryFile(Path::FromFS(SYSTEM_CONFIG_FILE_LOCATION));
#endif #endif
if (!found) { if (!found)
error.Set(cmdline_domain, "No configuration file found"); throw std::runtime_error("No configuration file found");
return false;
}
return true;
} }
...@@ -20,8 +20,6 @@ ...@@ -20,8 +20,6 @@
#ifndef MPD_COMMAND_LINE_HXX #ifndef MPD_COMMAND_LINE_HXX
#define MPD_COMMAND_LINE_HXX #define MPD_COMMAND_LINE_HXX
class Error;
struct options { struct options {
bool kill; bool kill;
bool daemon; bool daemon;
...@@ -29,8 +27,7 @@ struct options { ...@@ -29,8 +27,7 @@ struct options {
bool verbose; bool verbose;
}; };
bool void
parse_cmdline(int argc, char **argv, struct options *options, ParseCommandLine(int argc, char **argv, struct options *options);
Error &error);
#endif #endif
...@@ -49,7 +49,6 @@ ...@@ -49,7 +49,6 @@
#include "pcm/PcmConvert.hxx" #include "pcm/PcmConvert.hxx"
#include "unix/SignalHandlers.hxx" #include "unix/SignalHandlers.hxx"
#include "system/FatalError.hxx" #include "system/FatalError.hxx"
#include "util/Error.hxx"
#include "thread/Slack.hxx" #include "thread/Slack.hxx"
#include "lib/icu/Init.hxx" #include "lib/icu/Init.hxx"
#include "config/ConfigGlobal.hxx" #include "config/ConfigGlobal.hxx"
...@@ -170,7 +169,6 @@ InitStorage() ...@@ -170,7 +169,6 @@ InitStorage()
static bool static bool
glue_db_init_and_load(void) glue_db_init_and_load(void)
{ {
Error error;
instance->database = instance->database =
CreateConfiguredDatabase(instance->event_loop, *instance); CreateConfiguredDatabase(instance->event_loop, *instance);
if (instance->database == nullptr) if (instance->database == nullptr)
...@@ -367,7 +365,6 @@ static inline ...@@ -367,7 +365,6 @@ static inline
int mpd_main(int argc, char *argv[]) int mpd_main(int argc, char *argv[])
try { try {
struct options options; struct options options;
Error error;
#ifdef ENABLE_DAEMON #ifdef ENABLE_DAEMON
daemonize_close_stdin(); daemonize_close_stdin();
...@@ -399,10 +396,7 @@ try { ...@@ -399,10 +396,7 @@ try {
ReadConfigFile(config_path); ReadConfigFile(config_path);
} }
#else #else
if (!parse_cmdline(argc, argv, &options, error)) { ParseCommandLine(argc, argv, &options);
LogError(error);
return EXIT_FAILURE;
}
#endif #endif
#ifdef ENABLE_DAEMON #ifdef ENABLE_DAEMON
...@@ -461,8 +455,6 @@ try { ...@@ -461,8 +455,6 @@ try {
static int mpd_main_after_fork(struct options options) static int mpd_main_after_fork(struct options options)
try { try {
Error error;
ConfigureFS(); ConfigureFS();
glue_mapper_init(); glue_mapper_init();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment