mpd/src/CommandLine.cxx

254 lines
7.0 KiB
C++
Raw Normal View History

2008-12-27 19:03:33 +01:00
/*
2013-01-03 17:31:38 +01:00
* Copyright (C) 2003-2013 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.
*
* 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
*/
#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"
#include "LogInit.hxx"
2013-01-09 22:33:06 +01:00
#include "Log.hxx"
#include "ConfigGlobal.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"
#include "InputRegistry.hxx"
#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"
#include "fs/AllocatedPath.hxx"
#include "fs/Traits.hxx"
#include "fs/FileSystem.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "system/FatalError.hxx"
2008-12-27 19:03:33 +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"
#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 <glib.h>
2008-12-27 19:03:33 +01:00
#include <stdio.h>
#include <stdlib.h>
2008-12-27 19:03:33 +01:00
2013-08-04 23:48:01 +02:00
#ifdef WIN32
#define CONFIG_FILE_LOCATION "\\mpd\\mpd.conf"
2013-10-15 08:33:44 +02:00
#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
2008-12-27 19:03:33 +01:00
static constexpr Domain cmdline_domain("cmdline");
2013-08-04 23:48:01 +02:00
gcc_noreturn
2008-12-27 19:03:33 +01:00
static void version(void)
{
puts(PACKAGE " (MPD: Music Player Daemon) " VERSION " \n"
"\n"
"Copyright (C) 2003-2007 Warren Dukes <warren.dukes@gmail.com>\n"
2013-10-30 23:37:06 +01:00
"Copyright (C) 2008-2013 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"
2012-06-12 22:29:04 +02:00
"Decoders plugins:");
decoder_plugins_for_each([](const DecoderPlugin &plugin){
printf(" [%s]", plugin.name);
2008-12-27 19:03:33 +01: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
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
#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("");
#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
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:");
print_supported_uri_schemes_to_fp(stdout);
exit(EXIT_SUCCESS);
2008-12-27 19:03:33 +01:00
}
static const char *summary =
"Music Player Daemon - a daemon for playing music.";
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)
2008-12-27 19:03:33 +01:00
{
GOptionContext *context;
bool ret;
static gboolean option_version,
option_no_daemon,
option_no_config;
const GOptionEntry entries[] = {
{ "kill", 0, 0, G_OPTION_ARG_NONE, &options->kill,
2013-10-19 18:19:03 +02:00
"kill the currently running mpd session", nullptr },
{ "no-config", 0, 0, G_OPTION_ARG_NONE, &option_no_config,
2013-10-19 18:19:03 +02:00
"don't read from config", nullptr },
{ "no-daemon", 0, 0, G_OPTION_ARG_NONE, &option_no_daemon,
2013-10-19 18:19:03 +02:00
"don't detach from console", nullptr },
{ "stdout", 0, 0, G_OPTION_ARG_NONE, &options->log_stderr,
2013-10-19 18:19:03 +02:00
nullptr, nullptr },
{ "stderr", 0, 0, G_OPTION_ARG_NONE, &options->log_stderr,
2013-10-19 18:19:03 +02:00
"print messages to stderr", nullptr },
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &options->verbose,
2013-10-19 18:19:03 +02:00
"verbose logging", nullptr },
{ "version", 'V', 0, G_OPTION_ARG_NONE, &option_version,
2013-10-19 18:19:03 +02:00
"print version number", nullptr },
2013-01-03 17:31:38 +01:00
{ nullptr, 0, 0, G_OPTION_ARG_NONE, nullptr, nullptr, nullptr }
};
2008-12-27 19:03:33 +01:00
options->kill = false;
options->daemon = true;
options->log_stderr = false;
options->verbose = false;
2008-12-27 19:03:33 +01:00
context = g_option_context_new("[path/to/mpd.conf]");
2013-10-19 18:19:03 +02:00
g_option_context_add_main_entries(context, entries, nullptr);
g_option_context_set_summary(context, summary);
GError *gerror = nullptr;
ret = g_option_context_parse(context, &argc, &argv, &gerror);
g_option_context_free(context);
if (!ret)
FatalError("option parsing failed", gerror);
2008-12-27 19:03:33 +01:00
if (option_version)
version();
/* initialize the logging library, so the configuration file
parser can use it already */
log_early_init(options->verbose);
options->daemon = !option_no_daemon;
if (option_no_config) {
LogDebug(cmdline_domain,
"Ignoring config, using daemon defaults");
return true;
} else if (argc <= 1) {
/* default configuration file path */
2013-08-04 23:48:01 +02:00
#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);
}
2013-10-15 08:33:44 +02:00
#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;
} else if (argc == 2) {
/* specified configuration file */
return ReadConfigFile(Path::FromFS(argv[1]), error);
} else {
error.Set(cmdline_domain, "too many arguments");
return false;
}
2008-12-27 19:03:33 +01:00
}