CommandLine.cxx 9.5 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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/File.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
namespace {
71
#ifdef _WIN32
72 73
constexpr auto CONFIG_FILE_LOCATION = Path::FromFS(PATH_LITERAL("mpd\\mpd.conf"));
constexpr auto APP_CONFIG_FILE_LOCATION = Path::FromFS(PATH_LITERAL("conf\\mpd.conf"));
74
#else
75 76 77
constexpr auto USER_CONFIG_FILE_LOCATION1 = Path::FromFS(PATH_LITERAL(".mpdconf"));
constexpr auto USER_CONFIG_FILE_LOCATION2 = Path::FromFS(PATH_LITERAL(".mpd/mpd.conf"));
constexpr auto USER_CONFIG_FILE_LOCATION_XDG = Path::FromFS(PATH_LITERAL("mpd/mpd.conf"));
78
#endif
79
}
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
enum Option {
	OPTION_KILL,
	OPTION_NO_CONFIG,
	OPTION_NO_DAEMON,
	OPTION_STDOUT,
	OPTION_STDERR,
	OPTION_VERBOSE,
	OPTION_VERSION,
	OPTION_HELP,
	OPTION_HELP2,
};

static constexpr OptionDef option_defs[] = {
	{"kill", "kill the currently running mpd session"},
	{"no-config", "don't read from config"},
	{"no-daemon", "don't detach from console"},
	{"stdout", nullptr}, // hidden, compatibility with old versions
	{"stderr", "print messages to stderr"},
	{"verbose", 'v', "verbose logging"},
	{"version", 'V', "print version number"},
	{"help", 'h', "show help options"},
	{nullptr, '?', nullptr}, // hidden, standard alias for --help
};
104

105
static constexpr Domain cmdline_domain("cmdline");
106

107
gcc_noreturn
108 109
static void version(void)
{
110
	printf("Music Player Daemon " VERSION
111
#ifdef GIT_COMMIT
112
	       " (" GIT_COMMIT ")"
113
#endif
114 115 116
	       "\n"
	       "\n"
	       "Copyright (C) 2003-2007 Warren Dukes <warren.dukes@gmail.com>\n"
117
	       "Copyright 2008-2017 Max Kellermann <max.kellermann@gmail.com>\n"
118
	       "This is free software; see the source for copying conditions.  There is NO\n"
119
	       "warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
120 121

#ifdef ENABLE_DATABASE
122
	       "\n"
123
	       "Database plugins:\n");
124 125 126

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

128 129
	printf("\n\n"
	       "Storage plugins:\n");
130 131 132

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

	printf("\n"
135
#endif
136

137
#ifdef ENABLE_NEIGHBOR_PLUGINS
138
	       "\n"
139
	       "Neighbor plugins:\n");
140 141
	for (auto i = neighbor_plugins; *i != nullptr; ++i)
		printf(" %s", (*i)->name);
142 143

	printf("\n"
144 145
#endif

146
	       "\n"
147
	       "Decoders plugins:\n");
148

149 150
	decoder_plugins_for_each([](const DecoderPlugin &plugin){
			printf(" [%s]", plugin.name);
151

152 153 154 155
			const char *const*suffixes = plugin.suffixes;
			if (suffixes != nullptr)
				for (; *suffixes != nullptr; ++suffixes)
					printf(" %s", *suffixes);
156

157
			printf("\n");
158
		});
159

160
	printf("\n"
161 162 163 164 165 166 167 168
	       "Filters:\n"
#ifdef ENABLE_LIBSAMPLERATE
	       " libsamplerate"
#endif
#ifdef ENABLE_SOXR
	       " soxr"
#endif
	       "\n\n"
169
	       "Tag plugins:\n"
170
#ifdef ENABLE_ID3TAG
171
	       " id3tag"
172
#endif
173 174
	       "\n\n"
	       "Output plugins:\n");
175 176
	audio_output_plugins_for_each(plugin)
		printf(" %s", plugin->name);
177
	printf("\n"
178

179
#ifdef ENABLE_ENCODER
180
	       "\n"
181
	       "Encoder plugins:\n");
182 183
	encoder_plugins_for_each(plugin)
		printf(" %s", plugin->name);
184
	printf("\n"
185 186
#endif

187
#ifdef ENABLE_ARCHIVE
188
	       "\n"
189
	       "Archive plugins:\n");
190 191 192 193
	archive_plugins_for_each(plugin) {
		printf(" [%s]", plugin->name);

		const char *const*suffixes = plugin->suffixes;
194 195
		if (suffixes != nullptr)
			for (; *suffixes != nullptr; ++suffixes)
196 197
				printf(" %s", *suffixes);

198
		printf("\n");
199
	}
200 201

	printf(""
202
#endif
203

204
	       "\n"
205 206 207 208 209 210
	       "Input plugins:\n"
	       " file"
#ifdef ENABLE_ARCHIVE
	       " archive"
#endif
	       );
211 212 213
	input_plugins_for_each(plugin)
		printf(" %s", plugin->name);

214 215
	printf("\n\n"
	       "Playlist plugins:\n");
216 217 218
	playlist_plugins_for_each(plugin)
		printf(" %s", plugin->name);

219 220
	printf("\n\n"
	       "Protocols:\n");
221 222
	print_supported_uri_schemes_to_fp(stdout);

223 224 225 226 227
	printf("\n"
	       "Other features:\n"
#ifdef HAVE_AVAHI
	       " avahi"
#endif
228 229 230
#ifdef ENABLE_DBUS
	       " dbus"
#endif
Max Kellermann's avatar
Max Kellermann committed
231 232 233
#ifdef ENABLE_UDISKS
	       " udisks"
#endif
234 235 236
#ifdef USE_EPOLL
	       " epoll"
#endif
237 238 239
#ifdef HAVE_ICONV
	       " iconv"
#endif
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
#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");

260
	exit(EXIT_SUCCESS);
261 262
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
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)
{
279 280 281 282 283 284
	printf("Usage:\n"
	       "  mpd [OPTION...] [path/to/mpd.conf]\n"
	       "\n"
	       "Music Player Daemon - a daemon for playing music.\n"
	       "\n"
	       "Options:\n");
285

286
	for (const auto &i : option_defs)
287 288
		if(i.HasDescription() == true) // hide hidden options from help print
			PrintOption(i);
289 290 291

	exit(EXIT_SUCCESS);
}
292

293
class ConfigLoader
294
{
295 296
	ConfigData &config;

297
public:
298 299 300
	explicit ConfigLoader(ConfigData &_config) noexcept
		:config(_config) {}

301
	bool TryFile(const Path path);
302
	bool TryFile(const AllocatedPath &base_path, Path path);
303 304 305 306 307
};

bool ConfigLoader::TryFile(Path path)
{
	if (FileExists(path)) {
308
		ReadConfigFile(config, path);
309 310 311 312 313
		return true;
	}
	return false;
}

314
bool ConfigLoader::TryFile(const AllocatedPath &base_path, Path path)
315 316 317
{
	if (base_path.IsNull())
		return false;
318
	auto full_path = base_path / path;
319
	return TryFile(full_path);
320 321
}

322
void
323 324
ParseCommandLine(int argc, char **argv, struct options &options,
		 ConfigData &config)
325
{
326
	bool use_config_file = true;
327

328
	// First pass: handle command line options
329
	OptionParser parser(option_defs, argc, argv);
330 331
	while (auto o = parser.Next()) {
		switch (Option(o.index)) {
332
		case OPTION_KILL:
333
			options.kill = true;
334 335 336
			break;

		case OPTION_NO_CONFIG:
337
			use_config_file = false;
338 339 340
			break;

		case OPTION_NO_DAEMON:
341
			options.daemon = false;
342 343 344 345
			break;

		case OPTION_STDOUT:
		case OPTION_STDERR:
346
			options.log_stderr = true;
347 348 349
			break;

		case OPTION_VERBOSE:
350
			options.verbose = true;
351 352 353
			break;

		case OPTION_VERSION:
354
			version();
355

356 357 358 359
		case OPTION_HELP:
		case OPTION_HELP2:
			help();
		}
360
	}
361

362 363
	/* initialize the logging library, so the configuration file
	   parser can use it already */
364
	log_early_init(options.verbose);
365

366
	if (!use_config_file) {
367 368
		LogDebug(cmdline_domain,
			 "Ignoring config, using daemon defaults");
369
		return;
370
	}
371

372 373
	// Second pass: find non-option parameters (i.e. config file)
	const char *config_file = nullptr;
374
	for (const char *i : parser.GetRemaining()) {
375
		if (config_file == nullptr) {
376
			config_file = i;
377
			continue;
378
		}
379 380

		throw std::runtime_error("too many arguments");
381
	}
382

383 384
	if (config_file != nullptr) {
		/* use specified configuration file */
385 386 387 388
#ifdef _UNICODE
		wchar_t buffer[MAX_PATH];
		auto result = MultiByteToWideChar(CP_ACP, 0, config_file, -1,
						  buffer, ARRAY_SIZE(buffer));
389 390
		if (result <= 0)
			throw MakeLastError("MultiByteToWideChar() failed");
391

392
		ReadConfigFile(config, Path::FromFS(buffer));
393
#else
394
		ReadConfigFile(config, Path::FromFS(config_file));
395
#endif
396
		return;
397
	}
398

399
	/* use default configuration file path */
400

401
	ConfigLoader loader(config);
402 403

	bool found =
404
#ifdef _WIN32
405 406 407
		loader.TryFile(GetUserConfigDir(), CONFIG_FILE_LOCATION) ||
		loader.TryFile(GetSystemConfigDir(), CONFIG_FILE_LOCATION) ||
		loader.TryFile(GetAppBaseDir(), APP_CONFIG_FILE_LOCATION);
408
#else
409 410 411 412 413
		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));
414
#endif
415 416
	if (!found)
		throw std::runtime_error("No configuration file found");
417
}