CommandLine.cxx 6.59 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 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 "path.h"
Max Kellermann's avatar
Max Kellermann committed
23
#include "ls.hxx"
Max Kellermann's avatar
Max Kellermann committed
24 25

extern "C" {
26
#include "log.h"
Max Kellermann's avatar
Max Kellermann committed
27 28
}

29
#include "conf.h"
30
#include "decoder_list.h"
31
#include "decoder_plugin.h"
32
#include "OutputList.hxx"
33
#include "output_plugin.h"
34 35
#include "input_registry.h"
#include "input_plugin.h"
36 37
#include "playlist_list.h"
#include "playlist_plugin.h"
38
#include "mpd_error.h"
39
#include "glib_compat.h"
40

41 42
#ifdef ENABLE_ENCODER
#include "encoder_list.h"
43
#include "encoder_plugin.h"
44 45
#endif

46 47
#ifdef ENABLE_ARCHIVE
#include "archive_list.h"
48
#include "archive_plugin.h"
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 57 58
#ifdef G_OS_WIN32
#define CONFIG_FILE_LOCATION		"\\mpd\\mpd.conf"
#else /* G_OS_WIN32 */
59 60
#define USER_CONFIG_FILE_LOCATION1	".mpdconf"
#define USER_CONFIG_FILE_LOCATION2	".mpd/mpd.conf"
61
#endif
62

63 64 65 66 67 68
static GQuark
cmdline_quark(void)
{
	return g_quark_from_static_string("cmdline");
}

69
G_GNUC_NORETURN
70 71 72 73 74
static void version(void)
{
	puts(PACKAGE " (MPD: Music Player Daemon) " VERSION " \n"
	     "\n"
	     "Copyright (C) 2003-2007 Warren Dukes <warren.dukes@gmail.com>\n"
75
	     "Copyright (C) 2008-2012 Max Kellermann <max@duempel.org>\n"
76 77 78
	     "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"
79 80 81 82
	     "Decoders plugins:");

	decoder_plugins_for_each(plugin) {
		printf(" [%s]", plugin->name);
83

84 85 86 87
		const char *const*suffixes = plugin->suffixes;
		if (suffixes != NULL)
			for (; *suffixes != NULL; ++suffixes)
				printf(" %s", *suffixes);
88

89 90
		puts("");
	}
91

92
	puts("\n"
93 94 95 96
	     "Output plugins:");
	audio_output_plugins_for_each(plugin)
		printf(" %s", plugin->name);
	puts("");
97

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

106 107
#ifdef ENABLE_ARCHIVE
	puts("\n"
108 109 110 111 112 113 114 115 116 117 118
	     "Archive plugins:");
	archive_plugins_for_each(plugin) {
		printf(" [%s]", plugin->name);

		const char *const*suffixes = plugin->suffixes;
		if (suffixes != NULL)
			for (; *suffixes != NULL; ++suffixes)
				printf(" %s", *suffixes);

		puts("");
	}
119
#endif
120

121
	puts("\n"
122 123 124 125 126
	     "Input plugins:");
	input_plugins_for_each(plugin)
		printf(" %s", plugin->name);

	puts("\n\n"
127 128 129 130 131
	     "Playlist plugins:");
	playlist_plugins_for_each(plugin)
		printf(" %s", plugin->name);

	puts("\n\n"
132
	     "Protocols:");
133 134
	print_supported_uri_schemes_to_fp(stdout);

135
	exit(EXIT_SUCCESS);
136 137
}

138 139 140
static const char *summary =
	"Music Player Daemon - a daemon for playing music.";

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

169 170
	options->kill = false;
	options->daemon = true;
171
	options->log_stderr = false;
172
	options->verbose = false;
173

174 175 176 177 178 179 180 181
	context = g_option_context_new("[path/to/mpd.conf]");
	g_option_context_add_main_entries(context, entries, NULL);

	g_option_context_set_summary(context, summary);

	ret = g_option_context_parse(context, &argc, &argv, &error);
	g_option_context_free(context);

182 183
	if (!ret)
		MPD_ERROR("option parsing failed: %s\n", error->message);
184

185 186 187
	if (option_version)
		version();

188 189 190 191
	/* initialize the logging library, so the configuration file
	   parser can use it already */
	log_early_init(options->verbose);

192 193
	options->daemon = !option_no_daemon;

194 195
	if (option_no_config) {
		g_debug("Ignoring config, using daemon defaults\n");
196
		return true;
197
	} else if (argc <= 1) {
198
		/* default configuration file path */
199 200
		char *path1;

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
#ifdef G_OS_WIN32
		path1 = g_build_filename(g_get_user_config_dir(),
					CONFIG_FILE_LOCATION, NULL);
		if (g_file_test(path1, G_FILE_TEST_IS_REGULAR))
			ret = config_read_file(path1, error_r);
		else {
			int i = 0;
			char *system_path = NULL;
			const char * const *system_config_dirs;

			system_config_dirs = g_get_system_config_dirs();

			while(system_config_dirs[i] != NULL) {
				system_path = g_build_filename(system_config_dirs[i],
						CONFIG_FILE_LOCATION,
						NULL);
				if(g_file_test(system_path,
						G_FILE_TEST_IS_REGULAR)) {
					ret = config_read_file(system_path,error_r);
220
					g_free(system_path);
221
					break;
222 223 224
				} else
					g_free(system_path);
				++i;
225 226 227 228
			}
		}
#else /* G_OS_WIN32 */
		char *path2;
229 230 231 232 233
		path1 = g_build_filename(g_get_home_dir(),
					USER_CONFIG_FILE_LOCATION1, NULL);
		path2 = g_build_filename(g_get_home_dir(),
					USER_CONFIG_FILE_LOCATION2, NULL);
		if (g_file_test(path1, G_FILE_TEST_IS_REGULAR))
234
			ret = config_read_file(path1, error_r);
235
		else if (g_file_test(path2, G_FILE_TEST_IS_REGULAR))
236
			ret = config_read_file(path2, error_r);
237 238
		else if (g_file_test(SYSTEM_CONFIG_FILE_LOCATION,
				     G_FILE_TEST_IS_REGULAR))
239 240
			ret = config_read_file(SYSTEM_CONFIG_FILE_LOCATION,
					       error_r);
241 242
#endif

243
		g_free(path1);
244
#ifndef G_OS_WIN32
245
		g_free(path2);
246
#endif
247

248
		return ret;
249 250
	} else if (argc == 2) {
		/* specified configuration file */
251
		return config_read_file(argv[1], error_r);
252 253 254 255 256
	} else {
		g_set_error(error_r, cmdline_quark(), 0,
			    "too many arguments");
		return false;
	}
257
}