CommandLine.cxx 9.69 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "LogInit.hxx"
24
#include "Log.hxx"
25
#include "config/ConfigGlobal.hxx"
26 27
#include "decoder/DecoderList.hxx"
#include "decoder/DecoderPlugin.hxx"
28
#include "output/Registry.hxx"
29
#include "output/OutputPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
30 31
#include "input/Registry.hxx"
#include "input/InputPlugin.hxx"
32 33
#include "playlist/PlaylistRegistry.hxx"
#include "playlist/PlaylistPlugin.hxx"
34
#include "fs/AllocatedPath.hxx"
35
#include "fs/Traits.hxx"
36
#include "fs/FileSystem.hxx"
37
#include "fs/StandardDirectory.hxx"
38
#include "system/Error.hxx"
39
#include "util/Macros.hxx"
40
#include "util/RuntimeError.hxx"
41
#include "util/Domain.hxx"
42 43
#include "util/OptionDef.hxx"
#include "util/OptionParser.hxx"
44

45 46 47
#ifdef ENABLE_DATABASE
#include "db/Registry.hxx"
#include "db/DatabasePlugin.hxx"
48 49
#include "storage/Registry.hxx"
#include "storage/StoragePlugin.hxx"
50 51
#endif

52 53 54 55 56
#ifdef ENABLE_NEIGHBOR_PLUGINS
#include "neighbor/Registry.hxx"
#include "neighbor/NeighborPlugin.hxx"
#endif

57
#ifdef ENABLE_ENCODER
58 59
#include "encoder/EncoderList.hxx"
#include "encoder/EncoderPlugin.hxx"
60 61
#endif

62
#ifdef ENABLE_ARCHIVE
63 64
#include "archive/ArchiveList.hxx"
#include "archive/ArchivePlugin.hxx"
65 66 67
#endif

#include <stdio.h>
Max Kellermann's avatar
Max Kellermann committed
68
#include <stdlib.h>
69

70
#ifdef WIN32
71 72
#define CONFIG_FILE_LOCATION PATH_LITERAL("mpd\\mpd.conf")
#define APP_CONFIG_FILE_LOCATION PATH_LITERAL("conf\\mpd.conf")
73
#else
74 75 76
#define USER_CONFIG_FILE_LOCATION1 PATH_LITERAL(".mpdconf")
#define USER_CONFIG_FILE_LOCATION2 PATH_LITERAL(".mpd/mpd.conf")
#define USER_CONFIG_FILE_LOCATION_XDG PATH_LITERAL("mpd/mpd.conf")
77
#endif
78

79
static constexpr OptionDef opt_kill(
80
	"kill", "kill the currently running mpd session");
81
static constexpr OptionDef opt_no_config(
82
	"no-config", "don't read from config");
83
static constexpr OptionDef opt_no_daemon(
84
	"no-daemon", "don't detach from console");
85
static constexpr OptionDef opt_stdout(
86
	"stdout", nullptr); // hidden, compatibility with old versions
87
static constexpr OptionDef opt_stderr(
88
	"stderr", "print messages to stderr");
89
static constexpr OptionDef opt_verbose(
90
	"verbose", 'v', "verbose logging");
91
static constexpr OptionDef opt_version(
92
	"version", 'V', "print version number");
93
static constexpr OptionDef opt_help(
94
	"help", 'h', "show help options");
95
static constexpr OptionDef opt_help_alt(
96 97
	nullptr, '?', nullptr); // hidden, standard alias for --help

98
static constexpr Domain cmdline_domain("cmdline");
99

100
gcc_noreturn
101 102
static void version(void)
{
103
	printf("Music Player Daemon " VERSION
104
#ifdef GIT_COMMIT
105
	       " (" GIT_COMMIT ")"
106
#endif
107 108 109
	       "\n"
	       "\n"
	       "Copyright (C) 2003-2007 Warren Dukes <warren.dukes@gmail.com>\n"
110
	       "Copyright (C) 2008-2015 Max Kellermann <max@duempel.org>\n"
111
	       "This is free software; see the source for copying conditions.  There is NO\n"
112
	       "warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
113 114

#ifdef ENABLE_DATABASE
115
	       "\n"
116
	       "Database plugins:\n");
117 118 119

	for (auto i = database_plugins; *i != nullptr; ++i)
		printf(" %s", (*i)->name);
120

121 122
	printf("\n\n"
	       "Storage plugins:\n");
123 124 125

	for (auto i = storage_plugins; *i != nullptr; ++i)
		printf(" %s", (*i)->name);
126 127

	printf("\n"
128
#endif
129

130
#ifdef ENABLE_NEIGHBOR_PLUGINS
131
	       "\n"
132
	       "Neighbor plugins:\n");
133 134
	for (auto i = neighbor_plugins; *i != nullptr; ++i)
		printf(" %s", (*i)->name);
135 136

	printf("\n"
137 138
#endif

139
	       "\n"
140
	       "Decoders plugins:\n");
141

142 143
	decoder_plugins_for_each([](const DecoderPlugin &plugin){
			printf(" [%s]", plugin.name);
144

145 146 147 148
			const char *const*suffixes = plugin.suffixes;
			if (suffixes != nullptr)
				for (; *suffixes != nullptr; ++suffixes)
					printf(" %s", *suffixes);
149

150
			printf("\n");
151
		});
152

153
	printf("\n"
154 155 156 157 158 159 160 161
	       "Filters:\n"
#ifdef ENABLE_LIBSAMPLERATE
	       " libsamplerate"
#endif
#ifdef ENABLE_SOXR
	       " soxr"
#endif
	       "\n\n"
162
	       "Tag plugins:\n"
163
#ifdef ENABLE_ID3TAG
164
	       " id3tag"
165
#endif
166 167
	       "\n\n"
	       "Output plugins:\n");
168 169
	audio_output_plugins_for_each(plugin)
		printf(" %s", plugin->name);
170
	printf("\n"
171

172
#ifdef ENABLE_ENCODER
173
	       "\n"
174
	       "Encoder plugins:\n");
175 176
	encoder_plugins_for_each(plugin)
		printf(" %s", plugin->name);
177
	printf("\n"
178 179
#endif

180
#ifdef ENABLE_ARCHIVE
181
	       "\n"
182
	       "Archive plugins:\n");
183 184 185 186
	archive_plugins_for_each(plugin) {
		printf(" [%s]", plugin->name);

		const char *const*suffixes = plugin->suffixes;
187 188
		if (suffixes != nullptr)
			for (; *suffixes != nullptr; ++suffixes)
189 190
				printf(" %s", *suffixes);

191
		printf("\n");
192
	}
193 194

	printf(""
195
#endif
196

197
	       "\n"
198
	       "Input plugins:\n");
199 200 201
	input_plugins_for_each(plugin)
		printf(" %s", plugin->name);

202 203
	printf("\n\n"
	       "Playlist plugins:\n");
204 205 206
	playlist_plugins_for_each(plugin)
		printf(" %s", plugin->name);

207 208
	printf("\n\n"
	       "Protocols:\n");
209 210
	print_supported_uri_schemes_to_fp(stdout);

211 212 213 214 215 216 217 218
	printf("\n"
	       "Other features:\n"
#ifdef HAVE_AVAHI
	       " avahi"
#endif
#ifdef USE_EPOLL
	       " epoll"
#endif
219 220 221
#ifdef HAVE_ICONV
	       " iconv"
#endif
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
#ifdef HAVE_ICU
	       " icu"
#endif
#ifdef ENABLE_INOTIFY
	       " inotify"
#endif
#ifdef HAVE_IPV6
	       " ipv6"
#endif
#ifdef ENABLE_SYSTEMD_DAEMON
	       " systemd"
#endif
#ifdef HAVE_TCP
	       " tcp"
#endif
#ifdef HAVE_UN
	       " un"
#endif
	       "\n");

242
	exit(EXIT_SUCCESS);
243 244
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
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)
{
261 262 263 264 265 266
	printf("Usage:\n"
	       "  mpd [OPTION...] [path/to/mpd.conf]\n"
	       "\n"
	       "Music Player Daemon - a daemon for playing music.\n"
	       "\n"
	       "Options:\n");
267 268 269 270 271 272 273 274 275 276 277

	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);
}
278

279
class ConfigLoader
280
{
281 282 283
public:
	bool TryFile(const Path path);
	bool TryFile(const AllocatedPath &base_path,
284
		     PathTraitsFS::const_pointer_type path);
285 286 287 288 289
};

bool ConfigLoader::TryFile(Path path)
{
	if (FileExists(path)) {
290
		ReadConfigFile(path);
291 292 293 294 295 296
		return true;
	}
	return false;
}

bool ConfigLoader::TryFile(const AllocatedPath &base_path,
297
			   PathTraitsFS::const_pointer_type path)
298 299 300 301 302
{
	if (base_path.IsNull())
		return false;
	auto full_path = AllocatedPath::Build(base_path, path);
	return TryFile(full_path);
303 304
}

305 306
void
ParseCommandLine(int argc, char **argv, struct options *options)
307
{
308
	bool use_config_file = true;
309 310
	options->kill = false;
	options->daemon = true;
311
	options->log_stderr = false;
312
	options->verbose = false;
313

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	// 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();
343

344 345
		throw FormatRuntimeError("invalid option: %s",
					 parser.GetOption());
346
	}
347

348 349 350 351
	/* initialize the logging library, so the configuration file
	   parser can use it already */
	log_early_init(options->verbose);

352
	if (!use_config_file) {
353 354
		LogDebug(cmdline_domain,
			 "Ignoring config, using daemon defaults");
355
		return;
356
	}
357

358 359 360 361 362 363 364 365
	// 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;
366
		}
367 368

		throw std::runtime_error("too many arguments");
369
	}
370

371 372
	if (config_file != nullptr) {
		/* use specified configuration file */
373 374 375 376
#ifdef _UNICODE
		wchar_t buffer[MAX_PATH];
		auto result = MultiByteToWideChar(CP_ACP, 0, config_file, -1,
						  buffer, ARRAY_SIZE(buffer));
377 378
		if (result <= 0)
			throw MakeLastError("MultiByteToWideChar() failed");
379

380
		ReadConfigFile(Path::FromFS(buffer));
381
#else
382
		ReadConfigFile(Path::FromFS(config_file));
383
#endif
384
		return;
385
	}
386

387
	/* use default configuration file path */
388

389
	ConfigLoader loader;
390 391

	bool found =
392
#ifdef WIN32
393 394 395
		loader.TryFile(GetUserConfigDir(), CONFIG_FILE_LOCATION) ||
		loader.TryFile(GetSystemConfigDir(), CONFIG_FILE_LOCATION) ||
		loader.TryFile(GetAppBaseDir(), APP_CONFIG_FILE_LOCATION);
396
#else
397 398 399 400 401
		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));
402
#endif
403 404
	if (!found)
		throw std::runtime_error("No configuration file found");
405
}