/*
* Copyright (C) 2003-2013 The Music Player Daemon Project
* 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.
*
* 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.
*/
#include "config.h"
#include "CommandLine.hxx"
#include "ls.hxx"
#include "LogInit.hxx"
#include "Log.hxx"
#include "ConfigGlobal.hxx"
#include "DecoderList.hxx"
#include "DecoderPlugin.hxx"
#include "OutputList.hxx"
#include "OutputPlugin.hxx"
#include "InputRegistry.hxx"
#include "InputPlugin.hxx"
#include "PlaylistRegistry.hxx"
#include "PlaylistPlugin.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/Traits.hxx"
#include "fs/FileSystem.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/OptionDef.hxx"
#include "util/OptionParser.hxx"
#ifdef ENABLE_ENCODER
#include "EncoderList.hxx"
#include "EncoderPlugin.hxx"
#endif
#ifdef ENABLE_ARCHIVE
#include "ArchiveList.hxx"
#include "ArchivePlugin.hxx"
#endif
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef WIN32
#define CONFIG_FILE_LOCATION "\\mpd\\mpd.conf"
#else
#define USER_CONFIG_FILE_LOCATION1 ".mpdconf"
#define USER_CONFIG_FILE_LOCATION2 ".mpd/mpd.conf"
#define USER_CONFIG_FILE_LOCATION_XDG "mpd/mpd.conf"
#endif
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
static constexpr Domain cmdline_domain("cmdline");
gcc_noreturn
static void version(void)
{
puts("Music Player Daemon " VERSION "\n"
"\n"
"Copyright (C) 2003-2007 Warren Dukes <warren.dukes@gmail.com>\n"
"Copyright (C) 2008-2013 Max Kellermann <max@duempel.org>\n"
"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"
"Decoders plugins:");
decoder_plugins_for_each([](const DecoderPlugin &plugin){
printf(" [%s]", plugin.name);
const char *const*suffixes = plugin.suffixes;
if (suffixes != nullptr)
for (; *suffixes != nullptr; ++suffixes)
printf(" %s", *suffixes);
puts("");
});
puts("\n"
"Output plugins:");
audio_output_plugins_for_each(plugin)
printf(" %s", plugin->name);
puts("");
#ifdef ENABLE_ENCODER
puts("\n"
"Encoder plugins:");
encoder_plugins_for_each(plugin)
printf(" %s", plugin->name);
puts("");
#endif
#ifdef ENABLE_ARCHIVE
puts("\n"
"Archive plugins:");
archive_plugins_for_each(plugin) {
printf(" [%s]", plugin->name);
const char *const*suffixes = plugin->suffixes;
if (suffixes != nullptr)
for (; *suffixes != nullptr; ++suffixes)
printf(" %s", *suffixes);
puts("");
}
#endif
puts("\n"
"Input plugins:");
input_plugins_for_each(plugin)
printf(" %s", plugin->name);
puts("\n\n"
"Playlist plugins:");
playlist_plugins_for_each(plugin)
printf(" %s", plugin->name);
puts("\n\n"
"Protocols:");
print_supported_uri_schemes_to_fp(stdout);
exit(EXIT_SUCCESS);
}
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);
}
gcc_pure
static AllocatedPath
PathBuildChecked(const AllocatedPath &a, PathTraits::const_pointer b)
{
if (a.IsNull())
return AllocatedPath::Null();
return AllocatedPath::Build(a, b);
}
bool
parse_cmdline(int argc, char **argv, struct options *options,
Error &error)
{
bool use_config_file = true;
options->kill = false;
options->daemon = true;
options->log_stderr = false;
options->verbose = false;
// 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();
error.Format(cmdline_domain, "invalid option: %s",
parser.GetOption());
return false;
}
/* initialize the logging library, so the configuration file
parser can use it already */
log_early_init(options->verbose);
if (!use_config_file) {
LogDebug(cmdline_domain,
"Ignoring config, using daemon defaults");
return true;
}
// 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;
}
error.Set(cmdline_domain, "too many arguments");
return false;
}
if (config_file != nullptr) {
/* use specified configuration file */
return ReadConfigFile(Path::FromFS(config_file), error);
}
/* use default configuration file path */
#ifdef WIN32
AllocatedPath path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_user_config_dir()),
CONFIG_FILE_LOCATION);
if (!path.IsNull() && FileExists(path))
return ReadConfigFile(path, error);
const char *const*system_config_dirs =
g_get_system_config_dirs();
for (unsigned i = 0; system_config_dirs[i] != nullptr; ++i) {
path = PathBuildChecked(AllocatedPath::FromUTF8(system_config_dirs[i]),
CONFIG_FILE_LOCATION);
if (!path.IsNull() && FileExists(path))
return ReadConfigFile(path, error);
}
#else
AllocatedPath path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_user_config_dir()),
USER_CONFIG_FILE_LOCATION_XDG);
if (!path.IsNull() && FileExists(path))
return ReadConfigFile(path, error);
path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_home_dir()),
USER_CONFIG_FILE_LOCATION1);
if (!path.IsNull() && FileExists(path))
return ReadConfigFile(path, error);
path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_home_dir()),
USER_CONFIG_FILE_LOCATION2);
if (!path.IsNull() && FileExists(path))
return ReadConfigFile(path, error);
path = AllocatedPath::FromUTF8(SYSTEM_CONFIG_FILE_LOCATION);
if (!path.IsNull() && FileExists(path))
return ReadConfigFile(path, error);
#endif
error.Set(cmdline_domain, "No configuration file found");
return false;
}