CommandLine.cxx 9.39 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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"
22
#include "GitVersion.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "ls.hxx"
24
#include "LogInit.hxx"
25
#include "Log.hxx"
26
#include "config/File.hxx"
27 28
#include "decoder/DecoderList.hxx"
#include "decoder/DecoderPlugin.hxx"
29
#include "output/Registry.hxx"
30
#include "output/OutputPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
31 32
#include "input/Registry.hxx"
#include "input/InputPlugin.hxx"
33 34
#include "playlist/PlaylistRegistry.hxx"
#include "playlist/PlaylistPlugin.hxx"
35
#include "fs/AllocatedPath.hxx"
36
#include "fs/NarrowPath.hxx"
37
#include "fs/Traits.hxx"
38
#include "fs/FileSystem.hxx"
39
#include "fs/StandardDirectory.hxx"
40
#include "io/uring/Features.h"
41
#include "util/Domain.hxx"
42 43
#include "util/OptionDef.hxx"
#include "util/OptionParser.hxx"
44
#include "Version.h"
45

46 47 48 49
#ifdef _WIN32
#include "system/Error.hxx"
#endif

50 51 52
#ifdef ENABLE_DATABASE
#include "db/Registry.hxx"
#include "db/DatabasePlugin.hxx"
53 54
#include "storage/Registry.hxx"
#include "storage/StoragePlugin.hxx"
55 56
#endif

57 58 59 60 61
#ifdef ENABLE_NEIGHBOR_PLUGINS
#include "neighbor/Registry.hxx"
#include "neighbor/NeighborPlugin.hxx"
#endif

62
#include "encoder/Features.h"
63
#ifdef ENABLE_ENCODER
64 65
#include "encoder/EncoderList.hxx"
#include "encoder/EncoderPlugin.hxx"
66 67
#endif

68
#ifdef ENABLE_ARCHIVE
69 70
#include "archive/ArchiveList.hxx"
#include "archive/ArchivePlugin.hxx"
71 72
#endif

73
namespace {
74
#ifdef _WIN32
75 76
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"));
77
#else
78 79 80
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"));
81
#endif
82
} // namespace
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
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
};
107

108
static constexpr Domain cmdline_domain("cmdline");
109

110
[[noreturn]]
111
static void version()
112
{
113
	printf("Music Player Daemon " VERSION " (%s)"
114
	       "\n"
115 116
	       "Copyright 2003-2007 Warren Dukes <warren.dukes@gmail.com>\n"
	       "Copyright 2008-2018 Max Kellermann <max.kellermann@gmail.com>\n"
117
	       "This is free software; see the source for copying conditions.  There is NO\n"
118 119
	       "warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
	       GIT_VERSION);
120 121

#ifdef ENABLE_DATABASE
122 123
	printf("\n"
	       "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
	printf("\n"
139
	       "Neighbor plugins:\n");
140 141
	for (auto i = neighbor_plugins; *i != nullptr; ++i)
		printf(" %s", (*i)->name);
142

143 144
#endif

145
	printf("\n"
146
	       "\n"
147
	       "Decoders plugins:\n");
148

149
	decoder_plugins_for_each([](const DecoderPlugin &plugin){
150
		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 158 159 160
		if (plugin.protocols != nullptr)
			for (const auto &i : plugin.protocols())
				printf(" %s", i.c_str());

161 162
		printf("\n");
	});
163

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

183
#ifdef ENABLE_ENCODER
184
	       "\n"
185
	       "Encoder plugins:\n");
186 187
	encoder_plugins_for_each(plugin)
		printf(" %s", plugin->name);
188
	printf("\n"
189 190
#endif

191
#ifdef ENABLE_ARCHIVE
192
	       "\n"
193
	       "Archive plugins:\n");
194 195 196 197
	archive_plugins_for_each(plugin) {
		printf(" [%s]", plugin->name);

		const char *const*suffixes = plugin->suffixes;
198 199
		if (suffixes != nullptr)
			for (; *suffixes != nullptr; ++suffixes)
200 201
				printf(" %s", *suffixes);

202
		printf("\n");
203
	}
204 205

	printf(""
206
#endif
207

208
	       "\n"
209 210
	       "Input plugins:\n"
	       " file"
211 212 213
#ifdef HAVE_URING
	       " io_uring"
#endif
214 215 216 217
#ifdef ENABLE_ARCHIVE
	       " archive"
#endif
	       );
218 219 220
	input_plugins_for_each(plugin)
		printf(" %s", plugin->name);

221 222
	printf("\n\n"
	       "Playlist plugins:\n");
223 224 225
	playlist_plugins_for_each(plugin)
		printf(" %s", plugin->name);

226 227
	printf("\n\n"
	       "Protocols:\n");
228 229
	print_supported_uri_schemes_to_fp(stdout);

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

267
	std::exit(EXIT_SUCCESS);
268 269
}

270 271 272 273 274 275 276 277 278 279 280 281 282
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());
}

283
[[noreturn]]
284
static void help()
285
{
286 287 288 289 290 291
	printf("Usage:\n"
	       "  mpd [OPTION...] [path/to/mpd.conf]\n"
	       "\n"
	       "Music Player Daemon - a daemon for playing music.\n"
	       "\n"
	       "Options:\n");
292

293
	for (const auto &i : option_defs)
294
		if(i.HasDescription()) // hide hidden options from help print
295
			PrintOption(i);
296

297
	std::exit(EXIT_SUCCESS);
298
}
299

300
class ConfigLoader
301
{
302 303
	ConfigData &config;

304
public:
305 306 307
	explicit ConfigLoader(ConfigData &_config) noexcept
		:config(_config) {}

308
	bool TryFile(Path path);
309
	bool TryFile(const AllocatedPath &base_path, Path path);
310 311 312 313 314
};

bool ConfigLoader::TryFile(Path path)
{
	if (FileExists(path)) {
315
		ReadConfigFile(config, path);
316 317 318 319 320
		return true;
	}
	return false;
}

321
bool ConfigLoader::TryFile(const AllocatedPath &base_path, Path path)
322 323 324
{
	if (base_path.IsNull())
		return false;
325
	auto full_path = base_path / path;
326
	return TryFile(full_path);
327 328
}

329
void
330 331
ParseCommandLine(int argc, char **argv, struct options &options,
		 ConfigData &config)
332
{
333
	bool use_config_file = true;
334

335
	// First pass: handle command line options
336
	OptionParser parser(option_defs, argc, argv);
337 338
	while (auto o = parser.Next()) {
		switch (Option(o.index)) {
339
		case OPTION_KILL:
340
			options.kill = true;
341 342 343
			break;

		case OPTION_NO_CONFIG:
344
			use_config_file = false;
345 346 347
			break;

		case OPTION_NO_DAEMON:
348
			options.daemon = false;
349 350 351 352
			break;

		case OPTION_STDOUT:
		case OPTION_STDERR:
353
			options.log_stderr = true;
354 355 356
			break;

		case OPTION_VERBOSE:
357
			options.verbose = true;
358 359 360
			break;

		case OPTION_VERSION:
361
			version();
362

363 364 365 366
		case OPTION_HELP:
		case OPTION_HELP2:
			help();
		}
367
	}
368

369 370
	/* initialize the logging library, so the configuration file
	   parser can use it already */
371
	log_early_init(options.verbose);
372

373
	if (!use_config_file) {
374 375
		LogDebug(cmdline_domain,
			 "Ignoring config, using daemon defaults");
376
		return;
377
	}
378

379 380
	// Second pass: find non-option parameters (i.e. config file)
	const char *config_file = nullptr;
381
	for (const char *i : parser.GetRemaining()) {
382
		if (config_file == nullptr) {
383
			config_file = i;
384
			continue;
385
		}
386 387

		throw std::runtime_error("too many arguments");
388
	}
389

390 391
	if (config_file != nullptr) {
		/* use specified configuration file */
392
		ReadConfigFile(config, FromNarrowPath(config_file));
393
		return;
394
	}
395

396
	/* use default configuration file path */
397

398
	ConfigLoader loader(config);
399 400

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