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 .lyrics_autosave = false,
58 #endif
59 .find_wrap = true,
60 .scroll_offset = 0,
61 .wide_cursor = true,
62 .audible_bell = true,
63 .bell_on_wrap = true,
64 .status_message_time = 3,
65 #ifndef NCMPC_MINI
66 .scroll = DEFAULT_SCROLL,
67 .welcome_screen_list = true,
68 .display_time = true,
69 .jump_prefix_only = true,
70 #endif
71 };
73 static const arg_opt_t option_table[] = {
74 { '?', "help", NULL, "Show this help message" },
75 { 'V', "version", NULL, "Display version information" },
76 { 'c', "colors", NULL, "Enable colors" },
77 { 'C', "no-colors", NULL, "Disable colors" },
78 #ifdef HAVE_GETMOUSE
79 { 'm', "mouse", NULL, "Enable mouse" },
80 { 'M', "no-mouse", NULL, "Disable mouse" },
81 #endif
82 { 'e', "exit", NULL, "Exit on connection errors" },
83 { 'p', "port", "PORT", "Connect to server on port [" DEFAULT_PORT_STR "]" },
84 { 'h', "host", "HOST", "Connect to server on host [" DEFAULT_HOST "]" },
85 { 'P', "password","PASSWORD", "Connect with password" },
86 { 'f', "config", "FILE", "Read configuration from file" },
87 { 'k', "key-file","FILE", "Read configuration from file" },
88 { 'S', "no-splash", NULL, "Don't show the splash screen" },
89 #ifndef NDEBUG
90 { 'K', "dump-keys", NULL, "Dump key bindings to stdout" },
91 #endif
92 };
94 static const unsigned option_table_size = sizeof(option_table) / sizeof(option_table[0]);
96 static const arg_opt_t *
97 lookup_option(int s, char *l)
98 {
99 unsigned i;
101 for (i = 0; i < option_table_size; ++i) {
102 if (l && strcmp(l, option_table[i].longopt) == 0)
103 return &option_table[i];;
104 if (s && s == option_table[i].shortopt)
105 return &option_table[i];;
106 }
108 return NULL;
109 }
111 static void
112 option_error(int error, const char *option, const char *arg)
113 {
114 switch (error) {
115 case ERROR_UNKNOWN_OPTION:
116 fprintf(stderr, PACKAGE ": invalid option %s\n", option);
117 break;
118 case ERROR_BAD_ARGUMENT:
119 fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
120 break;
121 case ERROR_GOT_ARGUMENT:
122 fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
123 break;
124 case ERROR_MISSING_ARGUMENT:
125 fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
126 break;
127 default:
128 fprintf(stderr, PACKAGE ": internal error %d\n", error);
129 break;
130 }
132 exit(EXIT_FAILURE);
133 }
135 static void
136 display_help(void)
137 {
138 unsigned i;
140 printf("Usage: %s [OPTION]...\n", PACKAGE);
142 for (i = 0; i < option_table_size; ++i) {
143 char tmp[MAX_LONGOPT_LENGTH];
145 if (option_table[i].argument)
146 g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s",
147 option_table[i].longopt,
148 option_table[i].argument);
149 else
150 g_strlcpy(tmp, option_table[i].longopt, 64);
152 printf(" -%c, --%-20s %s\n",
153 option_table[i].shortopt,
154 tmp,
155 option_table[i].descrition);
156 }
157 }
159 static void
160 handle_option(int c, const char *arg)
161 {
162 switch (c) {
163 case '?': /* --help */
164 display_help();
165 exit(EXIT_SUCCESS);
166 case 'V': /* --version */
167 puts(PACKAGE " version: " VERSION "\n"
168 "build options:"
169 #ifdef NCMPC_MINI
170 " mini"
171 #endif
172 #ifndef NDEBUG
173 " debug"
174 #endif
175 #ifdef ENABLE_MULTIBYTE
176 " multibyte"
177 #endif
178 #ifdef ENABLE_WIDE
179 " wide"
180 #endif
181 #ifdef ENABLE_LOCALE
182 " locale"
183 #endif
184 #ifdef ENABLE_NLS
185 " nls"
186 #endif
187 #ifdef ENABLE_COLORS
188 " colors"
189 #else
190 " no-colors"
191 #endif
192 #ifdef ENABLE_LIRC
193 " lirc"
194 #endif
195 #ifdef HAVE_GETMOUSE
196 " getmouse"
197 #endif
198 #ifdef ENABLE_ARTIST_SCREEN
199 " artist-screen"
200 #endif
201 #ifdef ENABLE_HELP_SCREEN
202 " help-screen"
203 #endif
204 #ifdef ENABLE_SEARCH_SCREEN
205 " search-screen"
206 #endif
207 #ifdef ENABLE_SONG_SCREEN
208 " song-screen"
209 #endif
210 #ifdef ENABLE_KEYDEF_SCREEN
211 " key-screen"
212 #endif
213 #ifdef ENABLE_LYRICS_SCREEN
214 " lyrics-screen"
215 #endif
216 #ifdef ENABLE_OUTPUTS_SCREEN
217 " outputs-screen"
218 #endif
220 "\n");
221 #ifndef NCMPC_MINI
222 if (strcmp("translator-credits", _("translator-credits")) != 0)
223 /* To translators: these credits are shown
224 when ncmpc is started with "--version" */
225 printf("\n%s\n", _("translator-credits"));
226 #endif
227 exit(EXIT_SUCCESS);
228 case 'c': /* --colors */
229 #ifdef ENABLE_COLORS
230 options.enable_colors = true;
231 #endif
232 break;
233 case 'C': /* --no-colors */
234 #ifdef ENABLE_COLORS
235 options.enable_colors = false;
236 #endif
237 break;
238 case 'm': /* --mouse */
239 #ifdef HAVE_GETMOUSE
240 options.enable_mouse = true;
241 #endif
242 break;
243 case 'M': /* --no-mouse */
244 #ifdef HAVE_GETMOUSE
245 options.enable_mouse = false;
246 #endif
247 break;
248 case 'e': /* --exit */
249 /* deprecated */
250 break;
251 case 'p': /* --port */
252 options.port = atoi(arg);
253 break;
254 case 'h': /* --host */
255 g_free(options.host);
256 options.host = g_strdup(arg);
257 break;
258 case 'P': /* --password */
259 g_free(options.password);
260 options.password = locale_to_utf8(arg);
261 break;
262 case 'f': /* --config */
263 g_free(options.config_file);
264 options.config_file = g_strdup(arg);
265 break;
266 case 'k': /* --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 #ifndef NCMPC_MINI
275 case 'K': /* --dump-keys */
276 read_configuration();
277 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
278 exit(EXIT_SUCCESS);
279 break;
280 #endif
281 #endif
282 default:
283 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
284 break;
285 }
286 }
288 void
289 options_parse(int argc, const char *argv[])
290 {
291 int i;
292 const arg_opt_t *opt = NULL;
293 option_callback_fn_t option_cb = handle_option;
295 i=1;
296 while (i < argc) {
297 const char *arg = argv[i];
298 size_t len = strlen(arg);
300 /* check for a long option */
301 if (g_str_has_prefix(arg, "--")) {
302 char *name, *value;
304 /* make sure we got an argument for the previous option */
305 if( opt && opt->argument )
306 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
308 /* retrieve a option argument */
309 if ((value=g_strrstr(arg+2, "="))) {
310 *value = '\0';
311 name = g_strdup(arg);
312 *value = '=';
313 value++;
314 } else
315 name = g_strdup(arg);
317 /* check if the option exists */
318 if( (opt=lookup_option(0, name+2)) == NULL )
319 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
320 g_free(name);
322 /* abort if we got an argument to the option and don't want one */
323 if( value && opt->argument==NULL )
324 option_error(ERROR_GOT_ARGUMENT, arg, value);
326 /* execute option callback */
327 if (value || opt->argument==NULL) {
328 option_cb (opt->shortopt, value);
329 opt = NULL;
330 }
331 }
332 /* check for short options */
333 else if (len>=2 && g_str_has_prefix(arg, "-")) {
334 size_t j;
336 for(j=1; j<len; j++) {
337 /* make sure we got an argument for the previous option */
338 if (opt && opt->argument)
339 option_error(ERROR_MISSING_ARGUMENT,
340 opt->longopt, opt->argument);
342 /* check if the option exists */
343 if ((opt=lookup_option(arg[j], NULL)) == NULL)
344 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
346 /* if no option argument is needed execute callback */
347 if (opt->argument == NULL) {
348 option_cb (opt->shortopt, NULL);
349 opt = NULL;
350 }
351 }
352 } else {
353 /* is this a option argument? */
354 if (opt && opt->argument) {
355 option_cb (opt->shortopt, arg);
356 opt = NULL;
357 } else
358 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
359 }
360 i++;
361 }
363 if (opt && opt->argument == NULL)
364 option_cb (opt->shortopt, NULL);
365 else if (opt && opt->argument)
366 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
367 }
369 void
370 options_init(void)
371 {
372 const char *value;
373 char *tmp;
375 /* get initial values for host and password from MPD_HOST (environment) */
376 if ((value = g_getenv(MPD_HOST_ENV)))
377 options.host = g_strdup(value);
378 else
379 options.host = g_strdup(DEFAULT_HOST);
381 if ((tmp = g_strstr_len(options.host, strlen(options.host), "@"))) {
382 char *oldhost = options.host;
383 *tmp = '\0';
384 options.password = locale_to_utf8(oldhost);
385 options.host = g_strdup(tmp+1);
386 g_free(oldhost);
387 }
389 /* get initial values for port from MPD_PORT (environment) */
390 if ((value = g_getenv(MPD_PORT_ENV)))
391 options.port = atoi(value);
393 /* default option values */
394 options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
395 options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
396 options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
397 options.timedisplay_type = g_strdup(DEFAULT_TIMEDISPLAY_TYPE);
398 #ifndef NCMPC_MINI
399 options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
400 #endif
401 }
403 void
404 options_deinit(void)
405 {
406 g_free(options.host);
407 g_free(options.username);
408 g_free(options.password);
409 g_free(options.config_file);
410 g_free(options.key_file);
411 g_free(options.list_format);
412 g_free(options.status_format);
413 g_strfreev(options.screen_list);
414 #ifndef NCMPC_MINI
415 g_free(options.xterm_title_format);
416 g_free(options.scroll_sep);
417 #endif
418 g_free(options.timedisplay_type);
419 }