CommandLine.cxx 9.78 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 "util/Macros.hxx"
39 40
#include "util/Error.hxx"
#include "util/Domain.hxx"
41 42
#include "util/OptionDef.hxx"
#include "util/OptionParser.hxx"
43

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

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

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

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

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

69
#ifdef WIN32
70 71
#define CONFIG_FILE_LOCATION PATH_LITERAL("mpd\\mpd.conf")
#define APP_CONFIG_FILE_LOCATION PATH_LITERAL("conf\\mpd.conf")
72
#else
73 74 75
#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")
76
#endif
77

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

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

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

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

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

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

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

	printf("\n"
127
#endif
128

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

	printf("\n"
136 137
#endif

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

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

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

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

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

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

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

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

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

	printf(""
194
#endif
195

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

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

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

210 211 212 213 214 215 216 217
	printf("\n"
	       "Other features:\n"
#ifdef HAVE_AVAHI
	       " avahi"
#endif
#ifdef USE_EPOLL
	       " epoll"
#endif
218 219 220
#ifdef HAVE_ICONV
	       " iconv"
#endif
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
#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");

241
	exit(EXIT_SUCCESS);
242 243
}

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

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

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

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

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

304 305
bool
parse_cmdline(int argc, char **argv, struct options *options,
306
	      Error &error)
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 346 347
		error.Format(cmdline_domain, "invalid option: %s",
			     parser.GetOption());
		return false;
	}
348

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

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

359 360 361 362 363 364 365 366
	// 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;
367
		}
368 369 370
		error.Set(cmdline_domain, "too many arguments");
		return false;
	}
371

372 373
	if (config_file != nullptr) {
		/* use specified configuration file */
374 375 376 377 378 379 380 381 382
#ifdef _UNICODE
		wchar_t buffer[MAX_PATH];
		auto result = MultiByteToWideChar(CP_ACP, 0, config_file, -1,
						  buffer, ARRAY_SIZE(buffer));
		if (result <= 0) {
			error.SetLastError("MultiByteToWideChar() failed");
			return false;
		}

383
		ReadConfigFile(Path::FromFS(buffer));
384
#else
385
		ReadConfigFile(Path::FromFS(config_file));
386
#endif
387
		return true;
388
	}
389

390
	/* use default configuration file path */
391

392
	ConfigLoader loader;
393 394

	bool found =
395
#ifdef WIN32
396 397 398
		loader.TryFile(GetUserConfigDir(), CONFIG_FILE_LOCATION) ||
		loader.TryFile(GetSystemConfigDir(), CONFIG_FILE_LOCATION) ||
		loader.TryFile(GetAppBaseDir(), APP_CONFIG_FILE_LOCATION);
399
#else
400 401 402 403 404
		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));
405
#endif
406 407 408 409 410
	if (!found) {
		error.Set(cmdline_domain, "No configuration file found");
		return false;
	}

411
	return true;
412
}