cmdline: Add default config paths for win32.
[mpd-mk.git] / src / cmdline.c
blob747d7c3bbda7d107aab5be94cff4b366fbf2cd8d
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "cmdline.h"
22 #include "path.h"
23 #include "log.h"
24 #include "conf.h"
25 #include "decoder_list.h"
26 #include "decoder_plugin.h"
27 #include "output_list.h"
28 #include "ls.h"
30 #ifdef ENABLE_ENCODER
31 #include "encoder_list.h"
32 #endif
34 #ifdef ENABLE_ARCHIVE
35 #include "archive_list.h"
36 #endif
38 #include <glib.h>
40 #include <stdio.h>
41 #include <stdlib.h>
43 #ifdef G_OS_WIN32
44 #define CONFIG_FILE_LOCATION "\\mpd\\mpd.conf"
45 #else /* G_OS_WIN32 */
46 #define USER_CONFIG_FILE_LOCATION1 ".mpdconf"
47 #define USER_CONFIG_FILE_LOCATION2 ".mpd/mpd.conf"
48 #endif
50 static GQuark
51 cmdline_quark(void)
53 return g_quark_from_static_string("cmdline");
56 static void
57 print_all_decoders(FILE *fp)
59 for (unsigned i = 0; decoder_plugins[i] != NULL; ++i) {
60 const struct decoder_plugin *plugin = decoder_plugins[i];
61 const char *const*suffixes;
63 fprintf(fp, "[%s]", plugin->name);
65 for (suffixes = plugin->suffixes;
66 suffixes != NULL && *suffixes != NULL;
67 ++suffixes) {
68 fprintf(fp, " %s", *suffixes);
71 fprintf(fp, "\n");
75 G_GNUC_NORETURN
76 static void version(void)
78 puts(PACKAGE " (MPD: Music Player Daemon) " VERSION " \n"
79 "\n"
80 "Copyright (C) 2003-2007 Warren Dukes <warren.dukes@gmail.com>\n"
81 "Copyright (C) 2008-2010 Max Kellermann <max@duempel.org>\n"
82 "This is free software; see the source for copying conditions. There is NO\n"
83 "warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
84 "\n"
85 "Supported decoders:\n");
87 print_all_decoders(stdout);
89 puts("\n"
90 "Supported outputs:\n");
91 audio_output_plugin_print_all_types(stdout);
93 #ifdef ENABLE_ENCODER
94 puts("\n"
95 "Supported encoders:\n");
96 encoder_plugin_print_all_types(stdout);
97 #endif
100 #ifdef ENABLE_ARCHIVE
101 puts("\n"
102 "Supported archives:\n");
103 archive_plugin_init_all();
104 archive_plugin_print_all_suffixes(stdout);
105 #endif
107 puts("\n"
108 "Supported protocols:\n");
109 print_supported_uri_schemes_to_fp(stdout);
111 exit(EXIT_SUCCESS);
114 static const char *summary =
115 "Music Player Daemon - a daemon for playing music.";
117 bool
118 parse_cmdline(int argc, char **argv, struct options *options,
119 GError **error_r)
121 GError *error = NULL;
122 GOptionContext *context;
123 bool ret;
124 static gboolean option_version,
125 option_no_daemon,
126 option_no_config;
127 const GOptionEntry entries[] = {
128 { "kill", 0, 0, G_OPTION_ARG_NONE, &options->kill,
129 "kill the currently running mpd session", NULL },
130 { "no-config", 0, 0, G_OPTION_ARG_NONE, &option_no_config,
131 "don't read from config", NULL },
132 { "no-daemon", 0, 0, G_OPTION_ARG_NONE, &option_no_daemon,
133 "don't detach from console", NULL },
134 { "stdout", 0, 0, G_OPTION_ARG_NONE, &options->log_stderr,
135 NULL, NULL },
136 { "stderr", 0, 0, G_OPTION_ARG_NONE, &options->log_stderr,
137 "print messages to stderr", NULL },
138 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &options->verbose,
139 "verbose logging", NULL },
140 { "version", 'V', 0, G_OPTION_ARG_NONE, &option_version,
141 "print version number", NULL },
142 { .long_name = NULL }
145 options->kill = false;
146 options->daemon = true;
147 options->log_stderr = false;
148 options->verbose = false;
150 context = g_option_context_new("[path/to/mpd.conf]");
151 g_option_context_add_main_entries(context, entries, NULL);
153 g_option_context_set_summary(context, summary);
155 ret = g_option_context_parse(context, &argc, &argv, &error);
156 g_option_context_free(context);
158 if (!ret) {
159 g_error("option parsing failed: %s\n", error->message);
160 exit(1);
163 if (option_version)
164 version();
166 /* initialize the logging library, so the configuration file
167 parser can use it already */
168 log_early_init(options->verbose);
170 options->daemon = !option_no_daemon;
172 if (option_no_config) {
173 g_debug("Ignoring config, using daemon defaults\n");
174 return true;
175 } else if (argc <= 1) {
176 /* default configuration file path */
177 char *path1;
179 #ifdef G_OS_WIN32
180 path1 = g_build_filename(g_get_user_config_dir(),
181 CONFIG_FILE_LOCATION, NULL);
182 if (g_file_test(path1, G_FILE_TEST_IS_REGULAR))
183 ret = config_read_file(path1, error_r);
184 else {
185 int i = 0;
186 char *system_path = NULL;
187 const char * const *system_config_dirs;
189 system_config_dirs = g_get_system_config_dirs();
191 while(system_config_dirs[i] != NULL) {
192 system_path = g_build_filename(system_config_dirs[i],
193 CONFIG_FILE_LOCATION,
194 NULL);
195 if(g_file_test(system_path,
196 G_FILE_TEST_IS_REGULAR)) {
197 ret = config_read_file(system_path,error_r);
198 g_free(system_path);
199 g_free(&system_config_dirs);
200 break;
202 ++i;;
204 g_free(system_path);
205 g_free(&system_config_dirs);
207 #else /* G_OS_WIN32 */
208 char *path2;
209 path1 = g_build_filename(g_get_home_dir(),
210 USER_CONFIG_FILE_LOCATION1, NULL);
211 path2 = g_build_filename(g_get_home_dir(),
212 USER_CONFIG_FILE_LOCATION2, NULL);
213 if (g_file_test(path1, G_FILE_TEST_IS_REGULAR))
214 ret = config_read_file(path1, error_r);
215 else if (g_file_test(path2, G_FILE_TEST_IS_REGULAR))
216 ret = config_read_file(path2, error_r);
217 else if (g_file_test(SYSTEM_CONFIG_FILE_LOCATION,
218 G_FILE_TEST_IS_REGULAR))
219 ret = config_read_file(SYSTEM_CONFIG_FILE_LOCATION,
220 error_r);
221 #endif
223 g_free(path1);
224 #ifndef G_OS_WIN32
225 g_free(path2);
226 #endif
228 return ret;
229 } else if (argc == 2) {
230 /* specified configuration file */
231 return config_read_file(argv[1], error_r);
232 } else {
233 g_set_error(error_r, cmdline_quark(), 0,
234 "too many arguments");
235 return false;