CommandLine.cxx 6.56 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "CommandLine.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "ls.hxx"
23
#include "Log.hxx"
24
#include "conf.h"
25
#include "DecoderList.hxx"
26
#include "decoder_plugin.h"
27
#include "OutputList.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "OutputPlugin.hxx"
29
#include "InputRegistry.hxx"
30
#include "InputPlugin.hxx"
31
#include "PlaylistRegistry.hxx"
32
#include "PlaylistPlugin.hxx"
33
#include "mpd_error.h"
34 35
#include "fs/Path.hxx"
#include "fs/FileSystem.hxx"
36

37
#ifdef ENABLE_ENCODER
38
#include "EncoderList.hxx"
39
#include "encoder_plugin.h"
40 41
#endif

42
#ifdef ENABLE_ARCHIVE
43 44
#include "ArchiveList.hxx"
#include "ArchivePlugin.hxx"
45 46
#endif

47 48
#include <glib.h>

49
#include <stdio.h>
Max Kellermann's avatar
Max Kellermann committed
50
#include <stdlib.h>
51

52 53 54
#ifdef G_OS_WIN32
#define CONFIG_FILE_LOCATION		"\\mpd\\mpd.conf"
#else /* G_OS_WIN32 */
55 56
#define USER_CONFIG_FILE_LOCATION1	".mpdconf"
#define USER_CONFIG_FILE_LOCATION2	".mpd/mpd.conf"
57
#endif
58

59 60 61 62 63 64
static GQuark
cmdline_quark(void)
{
	return g_quark_from_static_string("cmdline");
}

65
G_GNUC_NORETURN
66 67 68 69 70
static void version(void)
{
	puts(PACKAGE " (MPD: Music Player Daemon) " VERSION " \n"
	     "\n"
	     "Copyright (C) 2003-2007 Warren Dukes <warren.dukes@gmail.com>\n"
71
	     "Copyright (C) 2008-2012 Max Kellermann <max@duempel.org>\n"
72 73 74
	     "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"
75 76 77 78
	     "Decoders plugins:");

	decoder_plugins_for_each(plugin) {
		printf(" [%s]", plugin->name);
79

80 81 82 83
		const char *const*suffixes = plugin->suffixes;
		if (suffixes != NULL)
			for (; *suffixes != NULL; ++suffixes)
				printf(" %s", *suffixes);
84

85 86
		puts("");
	}
87

88
	puts("\n"
89 90 91 92
	     "Output plugins:");
	audio_output_plugins_for_each(plugin)
		printf(" %s", plugin->name);
	puts("");
93

94 95
#ifdef ENABLE_ENCODER
	puts("\n"
96 97 98 99
	     "Encoder plugins:");
	encoder_plugins_for_each(plugin)
		printf(" %s", plugin->name);
	puts("");
100 101
#endif

102 103
#ifdef ENABLE_ARCHIVE
	puts("\n"
104 105 106 107 108 109 110 111 112 113 114
	     "Archive plugins:");
	archive_plugins_for_each(plugin) {
		printf(" [%s]", plugin->name);

		const char *const*suffixes = plugin->suffixes;
		if (suffixes != NULL)
			for (; *suffixes != NULL; ++suffixes)
				printf(" %s", *suffixes);

		puts("");
	}
115
#endif
116

117
	puts("\n"
118 119 120 121 122
	     "Input plugins:");
	input_plugins_for_each(plugin)
		printf(" %s", plugin->name);

	puts("\n\n"
123 124 125 126 127
	     "Playlist plugins:");
	playlist_plugins_for_each(plugin)
		printf(" %s", plugin->name);

	puts("\n\n"
128
	     "Protocols:");
129 130
	print_supported_uri_schemes_to_fp(stdout);

131
	exit(EXIT_SUCCESS);
132 133
}

134 135 136
static const char *summary =
	"Music Player Daemon - a daemon for playing music.";

137 138 139 140 141 142 143 144 145 146
gcc_pure
static Path
PathBuildChecked(const Path &a, Path::const_pointer b)
{
	if (a.IsNull())
		return Path::Null();

	return Path::Build(a, b);
}

147 148 149
bool
parse_cmdline(int argc, char **argv, struct options *options,
	      GError **error_r)
150
{
151 152 153 154
	GError *error = NULL;
	GOptionContext *context;
	bool ret;
	static gboolean option_version,
155
		option_no_daemon,
156
		option_no_config;
157
	const GOptionEntry entries[] = {
158 159 160 161
		{ "kill", 0, 0, G_OPTION_ARG_NONE, &options->kill,
		  "kill the currently running mpd session", NULL },
		{ "no-config", 0, 0, G_OPTION_ARG_NONE, &option_no_config,
		  "don't read from config", NULL },
162 163
		{ "no-daemon", 0, 0, G_OPTION_ARG_NONE, &option_no_daemon,
		  "don't detach from console", NULL },
164
		{ "stdout", 0, 0, G_OPTION_ARG_NONE, &options->log_stderr,
165
		  NULL, NULL },
166
		{ "stderr", 0, 0, G_OPTION_ARG_NONE, &options->log_stderr,
167
		  "print messages to stderr", NULL },
168 169
		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &options->verbose,
		  "verbose logging", NULL },
170 171
		{ "version", 'V', 0, G_OPTION_ARG_NONE, &option_version,
		  "print version number", NULL },
Max Kellermann's avatar
Max Kellermann committed
172
		{ nullptr, 0, 0, G_OPTION_ARG_NONE, nullptr, nullptr, nullptr }
173
	};
174

175 176
	options->kill = false;
	options->daemon = true;
177
	options->log_stderr = false;
178
	options->verbose = false;
179

180 181 182 183 184 185 186 187
	context = g_option_context_new("[path/to/mpd.conf]");
	g_option_context_add_main_entries(context, entries, NULL);

	g_option_context_set_summary(context, summary);

	ret = g_option_context_parse(context, &argc, &argv, &error);
	g_option_context_free(context);

188 189
	if (!ret)
		MPD_ERROR("option parsing failed: %s\n", error->message);
190

191 192 193
	if (option_version)
		version();

194 195 196 197
	/* initialize the logging library, so the configuration file
	   parser can use it already */
	log_early_init(options->verbose);

198 199
	options->daemon = !option_no_daemon;

200 201
	if (option_no_config) {
		g_debug("Ignoring config, using daemon defaults\n");
202
		return true;
203
	} else if (argc <= 1) {
204
		/* default configuration file path */
205

206
#ifdef G_OS_WIN32
207 208 209 210 211 212 213 214 215 216 217 218 219
		Path path = PathBuildChecked(Path::FromUTF8(g_get_user_config_dir()),
					     CONFIG_FILE_LOCATION);
		if (!path.IsNull() && FileExists(path))
			return ReadConfigFile(path, error_r);

		const char *const*system_config_dirs =
			g_get_system_config_dirs();

		for (unsigned i = 0; system_config_dirs[i] != nullptr; ++i) {
			path = PathBuildChecked(Path::FromUTF8(system_config_dirs[i]),
						CONFIG_FILE_LOCATION);
			if (!path.IsNull() && FileExists(path))
				return ReadConfigFile(path, error_r);
220 221
		}
#else /* G_OS_WIN32 */
222 223 224 225 226 227 228 229 230 231 232 233 234
		Path path = PathBuildChecked(Path::FromUTF8(g_get_home_dir()),
					     USER_CONFIG_FILE_LOCATION1);
		if (!path.IsNull() && FileExists(path))
			return ReadConfigFile(path, error_r);

		path = PathBuildChecked(Path::FromUTF8(g_get_home_dir()),
					USER_CONFIG_FILE_LOCATION2);
		if (!path.IsNull() && FileExists(path))
			return ReadConfigFile(path, error_r);

		path = Path::FromUTF8(SYSTEM_CONFIG_FILE_LOCATION);
		if (!path.IsNull() && FileExists(path))
			return ReadConfigFile(path, error_r);
235 236
#endif

237 238 239
		g_set_error(error_r, cmdline_quark(), 0,
			    "No configuration file found");
		return false;
240 241
	} else if (argc == 2) {
		/* specified configuration file */
242
		return ReadConfigFile(Path::FromFS(argv[1]), error_r);
243 244 245 246 247
	} else {
		g_set_error(error_r, cmdline_quark(), 0,
			    "too many arguments");
		return false;
	}
248
}