2008-12-27 19:03:33 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2008-12-27 19:03:33 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-12-27 19:03:33 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-03 17:31:38 +01:00
|
|
|
#include "CommandLine.hxx"
|
2013-01-03 17:34:51 +01:00
|
|
|
#include "ls.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "LogInit.hxx"
|
2013-01-09 22:33:06 +01:00
|
|
|
#include "Log.hxx"
|
2013-09-05 08:47:10 +02:00
|
|
|
#include "ConfigGlobal.hxx"
|
2014-01-13 22:29:07 +01:00
|
|
|
#include "DatabaseRegistry.hxx"
|
|
|
|
#include "DatabasePlugin.hxx"
|
2013-01-30 17:18:48 +01:00
|
|
|
#include "DecoderList.hxx"
|
2013-07-28 13:18:48 +02:00
|
|
|
#include "DecoderPlugin.hxx"
|
2013-01-04 09:46:41 +01:00
|
|
|
#include "OutputList.hxx"
|
2013-04-17 01:12:05 +02:00
|
|
|
#include "OutputPlugin.hxx"
|
2013-01-10 10:15:09 +01:00
|
|
|
#include "InputRegistry.hxx"
|
2013-01-25 22:43:01 +01:00
|
|
|
#include "InputPlugin.hxx"
|
2013-01-26 01:04:02 +01:00
|
|
|
#include "PlaylistRegistry.hxx"
|
2013-01-27 17:38:09 +01:00
|
|
|
#include "PlaylistPlugin.hxx"
|
2013-10-17 21:59:35 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2013-10-17 23:23:25 +02:00
|
|
|
#include "fs/Traits.hxx"
|
2013-01-29 17:23:35 +01:00
|
|
|
#include "fs/FileSystem.hxx"
|
2013-12-06 23:33:58 +01:00
|
|
|
#include "fs/StandardDirectory.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2013-11-24 12:19:51 +01:00
|
|
|
#include "util/OptionDef.hxx"
|
|
|
|
#include "util/OptionParser.hxx"
|
2008-12-27 19:03:33 +01:00
|
|
|
|
2009-11-17 20:39:26 +01:00
|
|
|
#ifdef ENABLE_ENCODER
|
2013-04-17 22:22:37 +02:00
|
|
|
#include "EncoderList.hxx"
|
2013-07-30 09:04:05 +02:00
|
|
|
#include "EncoderPlugin.hxx"
|
2009-11-17 20:39:26 +01:00
|
|
|
#endif
|
|
|
|
|
2008-12-27 19:03:33 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
2013-01-24 19:18:58 +01:00
|
|
|
#include "ArchiveList.hxx"
|
|
|
|
#include "ArchivePlugin.hxx"
|
2008-12-27 19:03:33 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <stdlib.h>
|
2008-12-27 19:03:33 +01:00
|
|
|
|
2013-08-04 23:48:01 +02:00
|
|
|
#ifdef WIN32
|
2013-12-06 23:33:58 +01:00
|
|
|
#define CONFIG_FILE_LOCATION "mpd\\mpd.conf"
|
|
|
|
#define APP_CONFIG_FILE_LOCATION "conf\\mpd.conf"
|
2013-10-15 08:33:44 +02:00
|
|
|
#else
|
2009-01-14 14:27:17 +01:00
|
|
|
#define USER_CONFIG_FILE_LOCATION1 ".mpdconf"
|
|
|
|
#define USER_CONFIG_FILE_LOCATION2 ".mpd/mpd.conf"
|
2013-08-07 16:08:48 +02:00
|
|
|
#define USER_CONFIG_FILE_LOCATION_XDG "mpd/mpd.conf"
|
2010-05-20 15:53:07 +02:00
|
|
|
#endif
|
2008-12-27 19:03:33 +01:00
|
|
|
|
2013-11-24 12:19:51 +01:00
|
|
|
static const OptionDef opt_kill(
|
|
|
|
"kill", "kill the currently running mpd session");
|
|
|
|
static const OptionDef opt_no_config(
|
|
|
|
"no-config", "don't read from config");
|
|
|
|
static const OptionDef opt_no_daemon(
|
|
|
|
"no-daemon", "don't detach from console");
|
|
|
|
static const OptionDef opt_stdout(
|
|
|
|
"stdout", nullptr); // hidden, compatibility with old versions
|
|
|
|
static const OptionDef opt_stderr(
|
|
|
|
"stderr", "print messages to stderr");
|
|
|
|
static const OptionDef opt_verbose(
|
|
|
|
"verbose", 'v', "verbose logging");
|
|
|
|
static const OptionDef opt_version(
|
|
|
|
"version", 'V', "print version number");
|
|
|
|
static const OptionDef opt_help(
|
|
|
|
"help", 'h', "show help options");
|
|
|
|
static const OptionDef opt_help_alt(
|
|
|
|
nullptr, '?', nullptr); // hidden, standard alias for --help
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain cmdline_domain("cmdline");
|
2009-09-24 21:40:05 +02:00
|
|
|
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_noreturn
|
2008-12-27 19:03:33 +01:00
|
|
|
static void version(void)
|
|
|
|
{
|
2013-10-31 00:12:26 +01:00
|
|
|
puts("Music Player Daemon " VERSION "\n"
|
2008-12-27 19:03:33 +01:00
|
|
|
"\n"
|
|
|
|
"Copyright (C) 2003-2007 Warren Dukes <warren.dukes@gmail.com>\n"
|
2014-01-13 22:30:36 +01:00
|
|
|
"Copyright (C) 2008-2014 Max Kellermann <max@duempel.org>\n"
|
2008-12-27 19:03:33 +01:00
|
|
|
"This is free software; see the source for copying conditions. There is NO\n"
|
|
|
|
"warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
|
|
|
|
"\n"
|
2014-01-13 22:29:07 +01:00
|
|
|
"Database plugins:");
|
|
|
|
|
|
|
|
for (auto i = database_plugins; *i != nullptr; ++i)
|
|
|
|
printf(" %s", (*i)->name);
|
|
|
|
|
|
|
|
puts("\n\n"
|
2012-06-12 22:29:04 +02:00
|
|
|
"Decoders plugins:");
|
|
|
|
|
2013-10-21 22:02:19 +02:00
|
|
|
decoder_plugins_for_each([](const DecoderPlugin &plugin){
|
|
|
|
printf(" [%s]", plugin.name);
|
2008-12-27 19:03:33 +01:00
|
|
|
|
2013-10-21 22:02:19 +02:00
|
|
|
const char *const*suffixes = plugin.suffixes;
|
|
|
|
if (suffixes != nullptr)
|
|
|
|
for (; *suffixes != nullptr; ++suffixes)
|
|
|
|
printf(" %s", *suffixes);
|
2008-12-27 19:03:33 +01:00
|
|
|
|
2013-10-21 22:02:19 +02:00
|
|
|
puts("");
|
|
|
|
});
|
2008-12-30 14:08:59 +01:00
|
|
|
|
2008-12-27 19:03:33 +01:00
|
|
|
puts("\n"
|
2012-06-12 22:29:04 +02:00
|
|
|
"Output plugins:");
|
|
|
|
audio_output_plugins_for_each(plugin)
|
|
|
|
printf(" %s", plugin->name);
|
|
|
|
puts("");
|
2008-12-27 19:03:33 +01:00
|
|
|
|
2009-11-17 20:39:26 +01:00
|
|
|
#ifdef ENABLE_ENCODER
|
|
|
|
puts("\n"
|
2012-06-12 22:29:04 +02:00
|
|
|
"Encoder plugins:");
|
|
|
|
encoder_plugins_for_each(plugin)
|
|
|
|
printf(" %s", plugin->name);
|
|
|
|
puts("");
|
2009-11-17 20:39:26 +01:00
|
|
|
#endif
|
|
|
|
|
2008-12-27 19:03:33 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
|
|
|
puts("\n"
|
2012-06-12 22:29:04 +02:00
|
|
|
"Archive plugins:");
|
|
|
|
archive_plugins_for_each(plugin) {
|
|
|
|
printf(" [%s]", plugin->name);
|
|
|
|
|
|
|
|
const char *const*suffixes = plugin->suffixes;
|
2013-10-19 18:19:03 +02:00
|
|
|
if (suffixes != nullptr)
|
|
|
|
for (; *suffixes != nullptr; ++suffixes)
|
2012-06-12 22:29:04 +02:00
|
|
|
printf(" %s", *suffixes);
|
|
|
|
|
|
|
|
puts("");
|
|
|
|
}
|
2008-12-27 19:03:33 +01:00
|
|
|
#endif
|
2009-01-01 18:26:59 +01:00
|
|
|
|
2009-03-03 17:01:42 +01:00
|
|
|
puts("\n"
|
2012-06-12 22:24:42 +02:00
|
|
|
"Input plugins:");
|
|
|
|
input_plugins_for_each(plugin)
|
|
|
|
printf(" %s", plugin->name);
|
|
|
|
|
|
|
|
puts("\n\n"
|
2012-06-12 19:59:03 +02:00
|
|
|
"Playlist plugins:");
|
|
|
|
playlist_plugins_for_each(plugin)
|
|
|
|
printf(" %s", plugin->name);
|
|
|
|
|
|
|
|
puts("\n\n"
|
2012-06-12 22:29:04 +02:00
|
|
|
"Protocols:");
|
2009-03-03 17:01:42 +01:00
|
|
|
print_supported_uri_schemes_to_fp(stdout);
|
|
|
|
|
2009-01-01 18:26:59 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2008-12-27 19:03:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-24 12:19:51 +01:00
|
|
|
static void PrintOption(const OptionDef &opt)
|
|
|
|
{
|
|
|
|
if (opt.HasShortOption())
|
|
|
|
printf(" -%c, --%-12s%s\n",
|
|
|
|
opt.GetShortOption(),
|
|
|
|
opt.GetLongOption(),
|
|
|
|
opt.GetDescription());
|
|
|
|
else
|
|
|
|
printf(" --%-16s%s\n",
|
|
|
|
opt.GetLongOption(),
|
|
|
|
opt.GetDescription());
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_noreturn
|
|
|
|
static void help(void)
|
|
|
|
{
|
|
|
|
puts("Usage:\n"
|
|
|
|
" mpd [OPTION...] [path/to/mpd.conf]\n"
|
|
|
|
"\n"
|
|
|
|
"Music Player Daemon - a daemon for playing music.\n"
|
|
|
|
"\n"
|
|
|
|
"Options:");
|
|
|
|
|
|
|
|
PrintOption(opt_help);
|
|
|
|
PrintOption(opt_kill);
|
|
|
|
PrintOption(opt_no_config);
|
|
|
|
PrintOption(opt_no_daemon);
|
|
|
|
PrintOption(opt_stderr);
|
|
|
|
PrintOption(opt_verbose);
|
|
|
|
PrintOption(opt_version);
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2008-12-27 19:36:03 +01:00
|
|
|
|
2013-12-06 23:33:58 +01:00
|
|
|
class ConfigLoader
|
2013-01-29 17:23:35 +01:00
|
|
|
{
|
2013-12-06 23:33:58 +01:00
|
|
|
Error &error;
|
|
|
|
bool result;
|
|
|
|
public:
|
|
|
|
ConfigLoader(Error &_error) : error(_error), result(false) { }
|
2013-01-29 17:23:35 +01:00
|
|
|
|
2013-12-06 23:33:58 +01:00
|
|
|
bool GetResult() const { return result; }
|
|
|
|
|
|
|
|
bool TryFile(const Path path);
|
|
|
|
bool TryFile(const AllocatedPath &base_path,
|
|
|
|
PathTraitsFS::const_pointer path);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ConfigLoader::TryFile(Path path)
|
|
|
|
{
|
|
|
|
if (FileExists(path)) {
|
|
|
|
result = ReadConfigFile(path, error);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigLoader::TryFile(const AllocatedPath &base_path,
|
|
|
|
PathTraitsFS::const_pointer path)
|
|
|
|
{
|
|
|
|
if (base_path.IsNull())
|
|
|
|
return false;
|
|
|
|
auto full_path = AllocatedPath::Build(base_path, path);
|
|
|
|
return TryFile(full_path);
|
2013-01-29 17:23:35 +01:00
|
|
|
}
|
|
|
|
|
2009-09-24 21:40:05 +02:00
|
|
|
bool
|
|
|
|
parse_cmdline(int argc, char **argv, struct options *options,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2008-12-27 19:03:33 +01:00
|
|
|
{
|
2013-11-24 12:19:51 +01:00
|
|
|
bool use_config_file = true;
|
2008-12-27 19:35:30 +01:00
|
|
|
options->kill = false;
|
|
|
|
options->daemon = true;
|
2009-07-22 13:40:19 +02:00
|
|
|
options->log_stderr = false;
|
2008-12-27 19:35:30 +01:00
|
|
|
options->verbose = false;
|
2008-12-27 19:03:33 +01:00
|
|
|
|
2013-11-24 12:19:51 +01:00
|
|
|
// First pass: handle command line options
|
|
|
|
OptionParser parser(argc, argv);
|
|
|
|
while (parser.HasEntries()) {
|
|
|
|
if (!parser.ParseNext())
|
|
|
|
continue;
|
|
|
|
if (parser.CheckOption(opt_kill)) {
|
|
|
|
options->kill = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (parser.CheckOption(opt_no_config)) {
|
|
|
|
use_config_file = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (parser.CheckOption(opt_no_daemon)) {
|
|
|
|
options->daemon = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (parser.CheckOption(opt_stderr, opt_stdout)) {
|
|
|
|
options->log_stderr = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (parser.CheckOption(opt_verbose)) {
|
|
|
|
options->verbose = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (parser.CheckOption(opt_version))
|
|
|
|
version();
|
|
|
|
if (parser.CheckOption(opt_help, opt_help_alt))
|
|
|
|
help();
|
2008-12-27 19:03:33 +01:00
|
|
|
|
2013-11-24 12:19:51 +01:00
|
|
|
error.Format(cmdline_domain, "invalid option: %s",
|
|
|
|
parser.GetOption());
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-27 19:36:03 +01:00
|
|
|
|
2009-02-19 08:35:20 +01:00
|
|
|
/* initialize the logging library, so the configuration file
|
|
|
|
parser can use it already */
|
|
|
|
log_early_init(options->verbose);
|
|
|
|
|
2013-11-24 12:19:51 +01:00
|
|
|
if (!use_config_file) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogDebug(cmdline_domain,
|
|
|
|
"Ignoring config, using daemon defaults");
|
2009-09-24 21:40:05 +02:00
|
|
|
return true;
|
2013-11-24 12:19:51 +01:00
|
|
|
}
|
2013-01-29 17:23:35 +01:00
|
|
|
|
2013-11-24 12:19:51 +01:00
|
|
|
// Second pass: find non-option parameters (i.e. config file)
|
|
|
|
const char *config_file = nullptr;
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
if (OptionParser::IsOption(argv[i]))
|
|
|
|
continue;
|
|
|
|
if (config_file == nullptr) {
|
|
|
|
config_file = argv[i];
|
|
|
|
continue;
|
2010-05-20 15:53:07 +02:00
|
|
|
}
|
2013-11-24 12:19:51 +01:00
|
|
|
error.Set(cmdline_domain, "too many arguments");
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-07 16:08:48 +02:00
|
|
|
|
2013-11-24 12:19:51 +01:00
|
|
|
if (config_file != nullptr) {
|
|
|
|
/* use specified configuration file */
|
|
|
|
return ReadConfigFile(Path::FromFS(config_file), error);
|
|
|
|
}
|
2013-01-29 17:23:35 +01:00
|
|
|
|
2013-11-24 12:19:51 +01:00
|
|
|
/* use default configuration file path */
|
2013-12-06 23:33:58 +01:00
|
|
|
|
|
|
|
ConfigLoader loader(error);
|
|
|
|
|
|
|
|
bool found =
|
2013-11-24 12:19:51 +01:00
|
|
|
#ifdef WIN32
|
2013-12-06 23:33:58 +01:00
|
|
|
loader.TryFile(GetUserConfigDir(), CONFIG_FILE_LOCATION) ||
|
|
|
|
loader.TryFile(GetSystemConfigDir(), CONFIG_FILE_LOCATION) ||
|
|
|
|
loader.TryFile(GetAppBaseDir(), APP_CONFIG_FILE_LOCATION);
|
2013-11-24 12:19:51 +01:00
|
|
|
#else
|
2013-12-06 23:33:58 +01:00
|
|
|
loader.TryFile(GetUserConfigDir(),
|
|
|
|
USER_CONFIG_FILE_LOCATION_XDG) ||
|
|
|
|
loader.TryFile(GetHomeDir(), USER_CONFIG_FILE_LOCATION1) ||
|
|
|
|
loader.TryFile(GetHomeDir(), USER_CONFIG_FILE_LOCATION2) ||
|
|
|
|
loader.TryFile(Path::FromFS(SYSTEM_CONFIG_FILE_LOCATION));
|
2013-11-24 12:19:51 +01:00
|
|
|
#endif
|
2013-12-06 23:33:58 +01:00
|
|
|
if (!found) {
|
|
|
|
error.Set(cmdline_domain, "No configuration file found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return loader.GetResult();
|
2008-12-27 19:03:33 +01:00
|
|
|
}
|