CommandLine.cxx 6.97 KB
Newer Older
1
/*
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 "ConfigGlobal.hxx"
26
#include "DecoderList.hxx"
27
#include "DecoderPlugin.hxx"
28
#include "OutputList.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "OutputPlugin.hxx"
30
#include "InputRegistry.hxx"
31
#include "InputPlugin.hxx"
32
#include "PlaylistRegistry.hxx"
33
#include "PlaylistPlugin.hxx"
34
#include "fs/AllocatedPath.hxx"
35
#include "fs/Traits.hxx"
36
#include "fs/FileSystem.hxx"
37 38
#include "util/Error.hxx"
#include "util/Domain.hxx"
39
#include "system/FatalError.hxx"
40

41
#ifdef ENABLE_ENCODER
42
#include "EncoderList.hxx"
43
#include "EncoderPlugin.hxx"
44 45
#endif

46
#ifdef ENABLE_ARCHIVE
47 48
#include "ArchiveList.hxx"
#include "ArchivePlugin.hxx"
49 50
#endif

51 52
#include <glib.h>

53
#include <stdio.h>
Max Kellermann's avatar
Max Kellermann committed
54
#include <stdlib.h>
55

56
#ifdef WIN32
57
#define CONFIG_FILE_LOCATION		"\\mpd\\mpd.conf"
58
#else
59 60
#define USER_CONFIG_FILE_LOCATION1	".mpdconf"
#define USER_CONFIG_FILE_LOCATION2	".mpd/mpd.conf"
61
#define USER_CONFIG_FILE_LOCATION_XDG	"mpd/mpd.conf"
62
#endif
63

64
static constexpr Domain cmdline_domain("cmdline");
65

66
gcc_noreturn
67 68
static void version(void)
{
69
	puts("Music Player Daemon " VERSION "\n"
70 71
	     "\n"
	     "Copyright (C) 2003-2007 Warren Dukes <warren.dukes@gmail.com>\n"
72
	     "Copyright (C) 2008-2014 Max Kellermann <max@duempel.org>\n"
73 74 75
	     "This is free software; see the source for copying conditions.  There is NO\n"
	     "warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
	     "\n"
76 77
	     "Decoders plugins:");

78 79
	decoder_plugins_for_each([](const DecoderPlugin &plugin){
			printf(" [%s]", plugin.name);
80

81 82 83 84
			const char *const*suffixes = plugin.suffixes;
			if (suffixes != nullptr)
				for (; *suffixes != nullptr; ++suffixes)
					printf(" %s", *suffixes);
85

86 87
			puts("");
		});
88

89
	puts("\n"
90 91 92 93
	     "Output plugins:");
	audio_output_plugins_for_each(plugin)
		printf(" %s", plugin->name);
	puts("");
94

95 96
#ifdef ENABLE_ENCODER
	puts("\n"
97 98 99 100
	     "Encoder plugins:");
	encoder_plugins_for_each(plugin)
		printf(" %s", plugin->name);
	puts("");
101 102
#endif

103 104
#ifdef ENABLE_ARCHIVE
	puts("\n"
105 106 107 108 109
	     "Archive plugins:");
	archive_plugins_for_each(plugin) {
		printf(" [%s]", plugin->name);

		const char *const*suffixes = plugin->suffixes;
110 111
		if (suffixes != nullptr)
			for (; *suffixes != nullptr; ++suffixes)
112 113 114 115
				printf(" %s", *suffixes);

		puts("");
	}
116
#endif
117

118
	puts("\n"
119 120 121 122 123
	     "Input plugins:");
	input_plugins_for_each(plugin)
		printf(" %s", plugin->name);

	puts("\n\n"
124 125 126 127 128
	     "Playlist plugins:");
	playlist_plugins_for_each(plugin)
		printf(" %s", plugin->name);

	puts("\n\n"
129
	     "Protocols:");
130 131
	print_supported_uri_schemes_to_fp(stdout);

132
	exit(EXIT_SUCCESS);
133 134
}

135 136 137
static const char *summary =
	"Music Player Daemon - a daemon for playing music.";

138
gcc_pure
139 140
static AllocatedPath
PathBuildChecked(const AllocatedPath &a, PathTraits::const_pointer b)
141 142
{
	if (a.IsNull())
143
		return AllocatedPath::Null();
144

145
	return AllocatedPath::Build(a, b);
146 147
}

148 149
bool
parse_cmdline(int argc, char **argv, struct options *options,
150
	      Error &error)
151
{
152 153 154
	GOptionContext *context;
	bool ret;
	static gboolean option_version,
155
		option_no_daemon,
156
		option_no_config;
157
	const GOptionEntry entries[] = {
158
		{ "kill", 0, 0, G_OPTION_ARG_NONE, &options->kill,
159
		  "kill the currently running mpd session", nullptr },
160
		{ "no-config", 0, 0, G_OPTION_ARG_NONE, &option_no_config,
161
		  "don't read from config", nullptr },
162
		{ "no-daemon", 0, 0, G_OPTION_ARG_NONE, &option_no_daemon,
163
		  "don't detach from console", nullptr },
164
		{ "stdout", 0, 0, G_OPTION_ARG_NONE, &options->log_stderr,
165
		  nullptr, nullptr },
166
		{ "stderr", 0, 0, G_OPTION_ARG_NONE, &options->log_stderr,
167
		  "print messages to stderr", nullptr },
168
		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &options->verbose,
169
		  "verbose logging", nullptr },
170
		{ "version", 'V', 0, G_OPTION_ARG_NONE, &option_version,
171
		  "print version number", nullptr },
Max Kellermann's avatar
Max Kellermann committed
172
		{ nullptr, 0, 0, G_OPTION_ARG_NONE, nullptr, nullptr, nullptr }
173
	};
174

175 176
	options->kill = false;
	options->daemon = true;
177
	options->log_stderr = false;
178
	options->verbose = false;
179

180
	context = g_option_context_new("[path/to/mpd.conf]");
181
	g_option_context_add_main_entries(context, entries, nullptr);
182 183 184

	g_option_context_set_summary(context, summary);

185 186
	GError *gerror = nullptr;
	ret = g_option_context_parse(context, &argc, &argv, &gerror);
187 188
	g_option_context_free(context);

189
	if (!ret)
190
		FatalError("option parsing failed", gerror);
191

192 193 194
	if (option_version)
		version();

195 196 197 198
	/* initialize the logging library, so the configuration file
	   parser can use it already */
	log_early_init(options->verbose);

199 200
	options->daemon = !option_no_daemon;

201
	if (option_no_config) {
202 203
		LogDebug(cmdline_domain,
			 "Ignoring config, using daemon defaults");
204
		return true;
205
	} else if (argc <= 1) {
206
		/* default configuration file path */
207

208
#ifdef WIN32
209
		AllocatedPath path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_user_config_dir()),
210 211
					     CONFIG_FILE_LOCATION);
		if (!path.IsNull() && FileExists(path))
212
			return ReadConfigFile(path, error);
213 214 215 216 217

		const char *const*system_config_dirs =
			g_get_system_config_dirs();

		for (unsigned i = 0; system_config_dirs[i] != nullptr; ++i) {
218
			path = PathBuildChecked(AllocatedPath::FromUTF8(system_config_dirs[i]),
219 220
						CONFIG_FILE_LOCATION);
			if (!path.IsNull() && FileExists(path))
221
				return ReadConfigFile(path, error);
222
		}
223
#else
224 225
		AllocatedPath path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_user_config_dir()),
						      USER_CONFIG_FILE_LOCATION_XDG);
226
		if (!path.IsNull() && FileExists(path))
227
			return ReadConfigFile(path, error);
228

229
		path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_home_dir()),
230 231
					     USER_CONFIG_FILE_LOCATION1);
		if (!path.IsNull() && FileExists(path))
232
			return ReadConfigFile(path, error);
233

234
		path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_home_dir()),
235 236
					USER_CONFIG_FILE_LOCATION2);
		if (!path.IsNull() && FileExists(path))
237
			return ReadConfigFile(path, error);
238

239
		path = AllocatedPath::FromUTF8(SYSTEM_CONFIG_FILE_LOCATION);
240
		if (!path.IsNull() && FileExists(path))
241
			return ReadConfigFile(path, error);
242 243
#endif

244
		error.Set(cmdline_domain, "No configuration file found");
245
		return false;
246 247
	} else if (argc == 2) {
		/* specified configuration file */
248
		return ReadConfigFile(Path::FromFS(argv[1]), error);
249
	} else {
250
		error.Set(cmdline_domain, "too many arguments");
251 252
		return false;
	}
253
}