CommandLine.cxx 8.78 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 39
#include "util/Error.hxx"
#include "util/Domain.hxx"
40 41
#include "util/OptionDef.hxx"
#include "util/OptionParser.hxx"
42

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

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

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

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

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

68
#ifdef WIN32
69 70
#define CONFIG_FILE_LOCATION		"mpd\\mpd.conf"
#define APP_CONFIG_FILE_LOCATION	"conf\\mpd.conf"
71
#else
72 73
#define USER_CONFIG_FILE_LOCATION1	".mpdconf"
#define USER_CONFIG_FILE_LOCATION2	".mpd/mpd.conf"
74
#define USER_CONFIG_FILE_LOCATION_XDG	"mpd/mpd.conf"
75
#endif
76

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

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

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

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

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

	puts("\n\n"
	     "Storage plugins:");

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

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

133
	puts("\n\n"
134 135
	     "Decoders plugins:");

136 137
	decoder_plugins_for_each([](const DecoderPlugin &plugin){
			printf(" [%s]", plugin.name);
138

139 140 141 142
			const char *const*suffixes = plugin.suffixes;
			if (suffixes != nullptr)
				for (; *suffixes != nullptr; ++suffixes)
					printf(" %s", *suffixes);
143

144 145
			puts("");
		});
146

147
	puts("\n"
148 149 150 151
	     "Output plugins:");
	audio_output_plugins_for_each(plugin)
		printf(" %s", plugin->name);
	puts("");
152

153 154
#ifdef ENABLE_ENCODER
	puts("\n"
155 156 157 158
	     "Encoder plugins:");
	encoder_plugins_for_each(plugin)
		printf(" %s", plugin->name);
	puts("");
159 160
#endif

161 162
#ifdef ENABLE_ARCHIVE
	puts("\n"
163 164 165 166 167
	     "Archive plugins:");
	archive_plugins_for_each(plugin) {
		printf(" [%s]", plugin->name);

		const char *const*suffixes = plugin->suffixes;
168 169
		if (suffixes != nullptr)
			for (; *suffixes != nullptr; ++suffixes)
170 171 172 173
				printf(" %s", *suffixes);

		puts("");
	}
174
#endif
175

176
	puts("\n"
177
	     "Input plugins:");
178 179 180 181
	input_plugins_for_each(plugin)
		printf(" %s", plugin->name);

	puts("\n\n"
182 183 184 185 186
	     "Playlist plugins:");
	playlist_plugins_for_each(plugin)
		printf(" %s", plugin->name);

	puts("\n\n"
187
	     "Protocols:");
188 189
	print_supported_uri_schemes_to_fp(stdout);

190
	exit(EXIT_SUCCESS);
191 192
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
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)
{
	puts("Usage:\n"
	     "  mpd [OPTION...] [path/to/mpd.conf]\n"
	     "\n"
	     "Music Player Daemon - a daemon for playing music.\n"
	     "\n"
	     "Options:");

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

227
class ConfigLoader
228
{
229 230 231 232
	Error &error;
	bool result;
public:
	ConfigLoader(Error &_error) : error(_error), result(false) { }
233

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	bool GetResult() const { return result; }

	bool TryFile(const Path path);
	bool TryFile(const AllocatedPath &base_path,
		     PathTraitsFS::const_pointer path);
};

bool ConfigLoader::TryFile(Path path)
{
	if (FileExists(path)) {
		result = ReadConfigFile(path, error);
		return true;
	}
	return false;
}

bool ConfigLoader::TryFile(const AllocatedPath &base_path,
			   PathTraitsFS::const_pointer path)
{
	if (base_path.IsNull())
		return false;
	auto full_path = AllocatedPath::Build(base_path, path);
	return TryFile(full_path);
257 258
}

259 260
bool
parse_cmdline(int argc, char **argv, struct options *options,
261
	      Error &error)
262
{
263
	bool use_config_file = true;
264 265
	options->kill = false;
	options->daemon = true;
266
	options->log_stderr = false;
267
	options->verbose = false;
268

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	// 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();
298

299 300 301 302
		error.Format(cmdline_domain, "invalid option: %s",
			     parser.GetOption());
		return false;
	}
303

304 305 306 307
	/* initialize the logging library, so the configuration file
	   parser can use it already */
	log_early_init(options->verbose);

308
	if (!use_config_file) {
309 310
		LogDebug(cmdline_domain,
			 "Ignoring config, using daemon defaults");
311
		return true;
312
	}
313

314 315 316 317 318 319 320 321
	// 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;
322
		}
323 324 325
		error.Set(cmdline_domain, "too many arguments");
		return false;
	}
326

327 328 329 330
	if (config_file != nullptr) {
		/* use specified configuration file */
		return ReadConfigFile(Path::FromFS(config_file), error);
	}
331

332
	/* use default configuration file path */
333 334 335 336

	ConfigLoader loader(error);

	bool found =
337
#ifdef WIN32
338 339 340
		loader.TryFile(GetUserConfigDir(), CONFIG_FILE_LOCATION) ||
		loader.TryFile(GetSystemConfigDir(), CONFIG_FILE_LOCATION) ||
		loader.TryFile(GetAppBaseDir(), APP_CONFIG_FILE_LOCATION);
341
#else
342 343 344 345 346
		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));
347
#endif
348 349 350 351 352 353
	if (!found) {
		error.Set(cmdline_domain, "No configuration file found");
		return false;
	}

	return loader.GetResult();
354
}