CommandLine.cxx 9.49 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"
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/Traits.hxx"
37
#include "fs/FileSystem.hxx"
38
#include "fs/StandardDirectory.hxx"
39
#include "system/Error.hxx"
40
#include "util/Macros.hxx"
41
#include "util/RuntimeError.hxx"
42
#include "util/Domain.hxx"
43 44
#include "util/OptionDef.hxx"
#include "util/OptionParser.hxx"
45

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

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

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

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

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

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

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

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

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

#ifdef ENABLE_DATABASE
119
	       "\n"
120 121
	       "Database plugins:\n",
	       GIT_VERSION);
122 123 124

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

126 127
	printf("\n\n"
	       "Storage plugins:\n");
128 129 130

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

	printf("\n"
133
#endif
134

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

	printf("\n"
142 143
#endif

144
	       "\n"
145
	       "Decoders plugins:\n");
146

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

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

155
			printf("\n");
156
		});
157

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

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

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

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

196
		printf("\n");
197
	}
198 199

	printf(""
200
#endif
201

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

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

217 218
	printf("\n\n"
	       "Protocols:\n");
219 220
	print_supported_uri_schemes_to_fp(stdout);

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

258
	exit(EXIT_SUCCESS);
259 260
}

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

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

	exit(EXIT_SUCCESS);
}
290

291
class ConfigLoader
292
{
293 294
	ConfigData &config;

295
public:
296 297 298
	explicit ConfigLoader(ConfigData &_config) noexcept
		:config(_config) {}

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

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

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

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

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

		case OPTION_NO_CONFIG:
335
			use_config_file = false;
336 337 338
			break;

		case OPTION_NO_DAEMON:
339
			options.daemon = false;
340 341 342 343
			break;

		case OPTION_STDOUT:
		case OPTION_STDERR:
344
			options.log_stderr = true;
345 346 347
			break;

		case OPTION_VERBOSE:
348
			options.verbose = true;
349 350 351
			break;

		case OPTION_VERSION:
352
			version();
353

354 355 356 357
		case OPTION_HELP:
		case OPTION_HELP2:
			help();
		}
358
	}
359

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

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

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

		throw std::runtime_error("too many arguments");
379
	}
380

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

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

397
	/* use default configuration file path */
398

399
	ConfigLoader loader(config);
400 401

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