3f784d11cc9c00ab23b84a1be23af8f7690790ba
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://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.
18 */
20 #include "options.h"
21 #include "config.h"
22 #include "defaults.h"
23 #include "charset.h"
24 #include "command.h"
25 #include "conf.h"
26 #include "i18n.h"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <glib.h>
33 #define MAX_LONGOPT_LENGTH 32
35 #define ERROR_UNKNOWN_OPTION 0x01
36 #define ERROR_BAD_ARGUMENT 0x02
37 #define ERROR_GOT_ARGUMENT 0x03
38 #define ERROR_MISSING_ARGUMENT 0x04
40 typedef struct {
41 int shortopt;
42 const char *longopt;
43 const char *argument;
44 const char *descrition;
45 } arg_opt_t;
48 typedef void (*option_callback_fn_t)(int c, const char *arg);
51 options_t options = {
52 .port = DEFAULT_PORT,
53 .crossfade_time = DEFAULT_CROSSFADE_TIME,
54 .seek_time = 1,
55 #ifdef ENABLE_LYRICS_SCREEN
56 .lyrics_timeout = DEFAULT_LYRICS_TIMEOUT,
57 #endif
58 .find_wrap = true,
59 .wide_cursor = true,
60 .audible_bell = true,
61 #ifndef NCMPC_MINI
62 .scroll = DEFAULT_SCROLL,
63 .welcome_screen_list = true,
64 .display_time = true,
65 #endif
66 };
68 static const arg_opt_t option_table[] = {
69 { '?', "help", NULL, "Show this help message" },
70 { 'V', "version", NULL, "Display version information" },
71 { 'c', "colors", NULL, "Enable colors" },
72 { 'C', "no-colors", NULL, "Disable colors" },
73 #ifdef HAVE_GETMOUSE
74 { 'm', "mouse", NULL, "Enable mouse" },
75 { 'M', "no-mouse", NULL, "Disable mouse" },
76 #endif
77 { 'e', "exit", NULL, "Exit on connection errors" },
78 { 'p', "port", "PORT", "Connect to server on port [" DEFAULT_PORT_STR "]" },
79 { 'h', "host", "HOST", "Connect to server on host [" DEFAULT_HOST "]" },
80 { 'P', "password","PASSWORD", "Connect with password" },
81 { 'f', "config", "FILE", "Read configuration from file" },
82 { 'k', "key-file","FILE", "Read configuration from file" },
83 { 'S', "no-splash", NULL, "Don't show the splash screen" },
84 #ifndef NDEBUG
85 { 'K', "dump-keys", NULL, "Dump key bindings to stdout" },
86 #endif
87 };
89 static const unsigned option_table_size = sizeof(option_table) / sizeof(option_table[0]);
91 static const arg_opt_t *
92 lookup_option(int s, char *l)
93 {
94 unsigned i;
96 for (i = 0; i < option_table_size; ++i) {
97 if (l && strcmp(l, option_table[i].longopt) == 0)
98 return &option_table[i];;
99 if (s && s == option_table[i].shortopt)
100 return &option_table[i];;
101 }
103 return NULL;
104 }
106 static void
107 option_error(int error, const char *option, const char *arg)
108 {
109 switch (error) {
110 case ERROR_UNKNOWN_OPTION:
111 fprintf(stderr, PACKAGE ": invalid option %s\n", option);
112 break;
113 case ERROR_BAD_ARGUMENT:
114 fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
115 break;
116 case ERROR_GOT_ARGUMENT:
117 fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
118 break;
119 case ERROR_MISSING_ARGUMENT:
120 fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
121 break;
122 default:
123 fprintf(stderr, PACKAGE ": internal error %d\n", error);
124 break;
125 }
127 exit(EXIT_FAILURE);
128 }
130 static void
131 display_help(void)
132 {
133 unsigned i;
135 printf("Usage: %s [OPTION]...\n", PACKAGE);
137 for (i = 0; i < option_table_size; ++i) {
138 char tmp[MAX_LONGOPT_LENGTH];
140 if (option_table[i].argument)
141 g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s",
142 option_table[i].longopt,
143 option_table[i].argument);
144 else
145 g_strlcpy(tmp, option_table[i].longopt, 64);
147 printf(" -%c, --%-20s %s\n",
148 option_table[i].shortopt,
149 tmp,
150 option_table[i].descrition);
151 i++;
152 }
153 }
155 static void
156 handle_option(int c, const char *arg)
157 {
158 switch (c) {
159 case '?': /* --help */
160 display_help();
161 exit(EXIT_SUCCESS);
162 case 'V': /* --version */
163 puts(PACKAGE " version: " VERSION "\n"
164 "build options:"
165 #ifdef NCMPC_MINI
166 " mini"
167 #endif
168 #ifndef NDEBUG
169 " debug"
170 #endif
171 #ifdef ENABLE_MULTIBYTE
172 " multibyte"
173 #endif
174 #ifdef ENABLE_WIDE
175 " wide"
176 #endif
177 #ifdef ENABLE_LOCALE
178 " locale"
179 #endif
180 #ifdef ENABLE_NLS
181 " nls"
182 #endif
183 #ifdef ENABLE_COLORS
184 " colors"
185 #else
186 " no-colors"
187 #endif
188 #ifdef ENABLE_LIRC
189 " lirc"
190 #endif
191 #ifdef HAVE_GETMOUSE
192 " getmouse"
193 #endif
194 #ifdef ENABLE_ARTIST_SCREEN
195 " artist-screen"
196 #endif
197 #ifdef ENABLE_HELP_SCREEN
198 " help-screen"
199 #endif
200 #ifdef ENABLE_SEARCH_SCREEN
201 " search-screen"
202 #endif
203 #ifdef ENABLE_SONG_SCREEN
204 " song-screen"
205 #endif
206 #ifdef ENABLE_KEYDEF_SCREEN
207 " key-screen"
208 #endif
209 #ifdef ENABLE_LYRICS_SCREEN
210 " lyrics-screen"
211 #endif
212 #ifdef ENABLE_OUTPUTS_SCREEN
213 " outputs-screen"
214 #endif
216 "\n");
217 #ifndef NCMPC_MINI
218 if (strcmp("translator-credits", _("translator-credits")) != 0)
219 /* To translators: these credits are shown
220 when ncmpc is started with "--version" */
221 printf("\n%s\n", _("translator-credits"));
222 #endif
223 exit(EXIT_SUCCESS);
224 case 'c': /* --colors */
225 #ifdef ENABLE_COLORS
226 options.enable_colors = true;
227 #endif
228 break;
229 case 'C': /* --no-colors */
230 #ifdef ENABLE_COLORS
231 options.enable_colors = false;
232 #endif
233 break;
234 case 'm': /* --mouse */
235 #ifdef HAVE_GETMOUSE
236 options.enable_mouse = true;
237 #endif
238 break;
239 case 'M': /* --no-mouse */
240 #ifdef HAVE_GETMOUSE
241 options.enable_mouse = false;
242 #endif
243 break;
244 case 'e': /* --exit */
245 /* deprecated */
246 break;
247 case 'p': /* --port */
248 options.port = atoi(arg);
249 break;
250 case 'h': /* --host */
251 if( options.host )
252 g_free(options.host);
253 options.host = g_strdup(arg);
254 break;
255 case 'P': /* --password */
256 if( options.password )
257 g_free(options.password);
258 options.password = locale_to_utf8(arg);
259 break;
260 case 'f': /* --config */
261 if( options.config_file )
262 g_free(options.config_file);
263 options.config_file = g_strdup(arg);
264 break;
265 case 'k': /* --key-file */
266 if( options.key_file )
267 g_free(options.key_file);
268 options.key_file = g_strdup(arg);
269 break;
270 case 'S': /* --key-file */
271 /* the splash screen was removed */
272 break;
273 #ifndef NDEBUG
274 case 'K': /* --dump-keys */
275 read_configuration();
276 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
277 exit(EXIT_SUCCESS);
278 break;
279 #endif
280 default:
281 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
282 break;
283 }
284 }
286 void
287 options_parse(int argc, const char *argv[])
288 {
289 int i;
290 const arg_opt_t *opt = NULL;
291 option_callback_fn_t option_cb = handle_option;
293 i=1;
294 while (i < argc) {
295 const char *arg = argv[i];
296 size_t len = strlen(arg);
298 /* check for a long option */
299 if (g_str_has_prefix(arg, "--")) {
300 char *name, *value;
302 /* make shure we got an argument for the previous option */
303 if( opt && opt->argument )
304 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
306 /* retreive a option argument */
307 if ((value=g_strrstr(arg+2, "="))) {
308 *value = '\0';
309 name = g_strdup(arg);
310 *value = '=';
311 value++;
312 } else
313 name = g_strdup(arg);
315 /* check if the option exists */
316 if( (opt=lookup_option(0, name+2)) == NULL )
317 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
318 g_free(name);
320 /* abort if we got an argument to the option and dont want one */
321 if( value && opt->argument==NULL )
322 option_error(ERROR_GOT_ARGUMENT, arg, value);
324 /* execute option callback */
325 if (value || opt->argument==NULL) {
326 option_cb (opt->shortopt, value);
327 opt = NULL;
328 }
329 }
330 /* check for short options */
331 else if (len>=2 && g_str_has_prefix(arg, "-")) {
332 size_t j;
334 for(j=1; j<len; j++) {
335 /* make shure we got an argument for the previous option */
336 if (opt && opt->argument)
337 option_error(ERROR_MISSING_ARGUMENT,
338 opt->longopt, opt->argument);
340 /* check if the option exists */
341 if ((opt=lookup_option(arg[j], NULL)) == NULL)
342 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
344 /* if no option argument is needed execute callback */
345 if (opt->argument == NULL) {
346 option_cb (opt->shortopt, NULL);
347 opt = NULL;
348 }
349 }
350 } else {
351 /* is this a option argument? */
352 if (opt && opt->argument) {
353 option_cb (opt->shortopt, arg);
354 opt = NULL;
355 } else
356 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
357 }
358 i++;
359 }
361 if (opt && opt->argument == NULL)
362 option_cb (opt->shortopt, NULL);
363 else if (opt && opt->argument)
364 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
365 }
367 void
368 options_init(void)
369 {
370 const char *value;
371 char *tmp;
373 /* get initial values for host and password from MPD_HOST (enviroment) */
374 if ((value = g_getenv(MPD_HOST_ENV)))
375 options.host = g_strdup(value);
376 else
377 options.host = g_strdup(DEFAULT_HOST);
379 if ((tmp = g_strstr_len(options.host, strlen(options.host), "@"))) {
380 char *oldhost = options.host;
381 *tmp = '\0';
382 options.password = locale_to_utf8(oldhost);
383 options.host = g_strdup(tmp+1);
384 g_free(oldhost);
385 }
387 /* get initial values for port from MPD_PORT (enviroment) */
388 if ((value = g_getenv(MPD_PORT_ENV)))
389 options.port = atoi(value);
391 /* default option values */
392 options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
393 options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
394 options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
395 options.timedisplay_type = g_strdup(DEFAULT_TIMEDISPLAY_TYPE);
396 #ifndef NCMPC_MINI
397 options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
398 #endif
399 }