1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
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.
9 *
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.
14 *
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 ERROR_UNKNOWN_OPTION 0x01
34 #define ERROR_BAD_ARGUMENT 0x02
35 #define ERROR_GOT_ARGUMENT 0x03
36 #define ERROR_MISSING_ARGUMENT 0x04
38 typedef struct {
39 int shortopt;
40 const char *longopt;
41 const char *argument;
42 const char *descrition;
43 } arg_opt_t;
46 typedef void (*option_callback_fn_t)(int c, const char *arg);
49 options_t options = {
50 .crossfade_time = DEFAULT_CROSSFADE_TIME,
51 .seek_time = 1,
52 #ifdef ENABLE_LYRICS_SCREEN
53 .lyrics_timeout = DEFAULT_LYRICS_TIMEOUT,
54 .lyrics_autosave = false,
55 .lyrics_show_plugin = false,
56 .text_editor_ask = true,
57 #endif
58 .find_wrap = true,
59 .scroll_offset = 0,
60 .wide_cursor = true,
61 .audible_bell = true,
62 .bell_on_wrap = true,
63 .status_message_time = 3,
64 .timeout_ms = DEFAULT_MPD_TIMEOUT,
65 #ifndef NCMPC_MINI
66 .scroll = DEFAULT_SCROLL,
67 .welcome_screen_list = true,
68 .jump_prefix_only = true,
69 .second_column = 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" },
84 { 'h', "host", "HOST", "Connect to server on host" },
85 { 'P', "password","PASSWORD", "Connect with password" },
86 { 'f', "config", "FILE", "Read configuration from file" },
87 { 'k', "key-file","FILE", "Read key bindings from file" },
88 #ifndef NDEBUG
89 { 'K', "dump-keys", NULL, "Dump key bindings to stdout" },
90 #endif
91 };
93 static const unsigned option_table_size = sizeof(option_table) / sizeof(option_table[0]);
95 static const arg_opt_t *
96 lookup_option(int s, char *l)
97 {
98 unsigned i;
100 for (i = 0; i < option_table_size; ++i) {
101 if (l && strcmp(l, option_table[i].longopt) == 0)
102 return &option_table[i];
103 if (s && s == option_table[i].shortopt)
104 return &option_table[i];
105 }
107 return NULL;
108 }
110 static void
111 option_error(int error, const char *option, const char *arg)
112 {
113 switch (error) {
114 case ERROR_UNKNOWN_OPTION:
115 fprintf(stderr, PACKAGE ": invalid option %s\n", option);
116 break;
117 case ERROR_BAD_ARGUMENT:
118 fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
119 break;
120 case ERROR_GOT_ARGUMENT:
121 fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
122 break;
123 case ERROR_MISSING_ARGUMENT:
124 fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
125 break;
126 default:
127 fprintf(stderr, PACKAGE ": internal error %d\n", error);
128 break;
129 }
131 exit(EXIT_FAILURE);
132 }
134 static void
135 display_help(void)
136 {
137 unsigned i;
139 printf("Usage: %s [OPTION]...\n", PACKAGE);
141 for (i = 0; i < option_table_size; ++i) {
142 char tmp[32];
144 if (option_table[i].argument)
145 g_snprintf(tmp, sizeof(tmp), "%s=%s",
146 option_table[i].longopt,
147 option_table[i].argument);
148 else
149 g_strlcpy(tmp, option_table[i].longopt, 64);
151 printf(" -%c, --%-20s %s\n",
152 option_table[i].shortopt,
153 tmp,
154 option_table[i].descrition);
155 }
156 }
158 static void
159 handle_option(int c, const char *arg)
160 {
161 switch (c) {
162 case '?': /* --help */
163 display_help();
164 exit(EXIT_SUCCESS);
165 case 'V': /* --version */
166 puts(PACKAGE " version: " VERSION "\n"
167 "build options:"
168 #ifdef NCMPC_MINI
169 " mini"
170 #endif
171 #ifndef NDEBUG
172 " debug"
173 #endif
174 #ifdef ENABLE_MULTIBYTE
175 " multibyte"
176 #endif
177 #ifdef HAVE_CURSES_ENHANCED
178 " wide"
179 #endif
180 #ifdef ENABLE_LOCALE
181 " locale"
182 #endif
183 #ifdef ENABLE_NLS
184 " nls"
185 #endif
186 #ifdef ENABLE_COLORS
187 " colors"
188 #else
189 " no-colors"
190 #endif
191 #ifdef ENABLE_LIRC
192 " lirc"
193 #endif
194 #ifdef HAVE_GETMOUSE
195 " getmouse"
196 #endif
197 #ifdef ENABLE_ARTIST_SCREEN
198 " artist-screen"
199 #endif
200 #ifdef ENABLE_HELP_SCREEN
201 " help-screen"
202 #endif
203 #ifdef ENABLE_SEARCH_SCREEN
204 " search-screen"
205 #endif
206 #ifdef ENABLE_SONG_SCREEN
207 " song-screen"
208 #endif
209 #ifdef ENABLE_KEYDEF_SCREEN
210 " key-screen"
211 #endif
212 #ifdef ENABLE_LYRICS_SCREEN
213 " lyrics-screen"
214 #endif
215 #ifdef ENABLE_OUTPUTS_SCREEN
216 " outputs-screen"
217 #endif
218 #ifdef ENABLE_CHAT_SCREEN
219 " chat-screen"
220 #endif
222 "\n");
223 #ifndef NCMPC_MINI
224 {
225 char *user_conf = build_user_conf_filename();
226 char *system_conf = build_system_conf_filename();
228 printf("configuration files:\n %s\n %s\n\n",
229 user_conf, system_conf);
231 g_free(user_conf);
232 g_free(system_conf);
233 }
234 if (strcmp("translator-credits", _("translator-credits")) != 0)
235 /* To translators: these credits are shown
236 when ncmpc is started with "--version" */
237 printf("\n%s\n", _("translator-credits"));
238 #endif
239 exit(EXIT_SUCCESS);
240 case 'c': /* --colors */
241 #ifdef ENABLE_COLORS
242 options.enable_colors = true;
243 #endif
244 break;
245 case 'C': /* --no-colors */
246 #ifdef ENABLE_COLORS
247 options.enable_colors = false;
248 #endif
249 break;
250 case 'm': /* --mouse */
251 #ifdef HAVE_GETMOUSE
252 options.enable_mouse = true;
253 #endif
254 break;
255 case 'M': /* --no-mouse */
256 #ifdef HAVE_GETMOUSE
257 options.enable_mouse = false;
258 #endif
259 break;
260 case 'e': /* --exit */
261 /* deprecated */
262 break;
263 case 'p': /* --port */
264 options.port = atoi(arg);
265 break;
266 case 'h': /* --host */
267 g_free(options.host);
268 options.host = g_strdup(arg);
269 break;
270 case 'P': /* --password */
271 g_free(options.password);
272 options.password = locale_to_utf8(arg);
273 break;
274 case 'f': /* --config */
275 g_free(options.config_file);
276 options.config_file = g_strdup(arg);
277 break;
278 case 'k': /* --key-file */
279 g_free(options.key_file);
280 options.key_file = g_strdup(arg);
281 break;
282 #if !defined(NDEBUG) && !defined(NCMPC_MINI)
283 case 'K': /* --dump-keys */
284 read_configuration();
285 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
286 exit(EXIT_SUCCESS);
287 break;
288 #endif
289 default:
290 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
291 break;
292 }
293 }
295 void
296 options_parse(int argc, const char *argv[])
297 {
298 int i;
299 const arg_opt_t *opt = NULL;
300 option_callback_fn_t option_cb = handle_option;
302 for (i = 1; i < argc; i++) {
303 const char *arg = argv[i];
304 size_t len = strlen(arg);
306 /* check for a long option */
307 if (g_str_has_prefix(arg, "--")) {
308 char *name, *value;
310 /* make sure we got an argument for the previous option */
311 if( opt && opt->argument )
312 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
314 /* retrieve a option argument */
315 if ((value=g_strrstr(arg+2, "="))) {
316 *value = '\0';
317 name = g_strdup(arg);
318 *value = '=';
319 value++;
320 } else
321 name = g_strdup(arg);
323 /* check if the option exists */
324 if( (opt=lookup_option(0, name+2)) == NULL )
325 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
326 g_free(name);
328 /* abort if we got an argument to the option and don't want one */
329 if( value && opt->argument==NULL )
330 option_error(ERROR_GOT_ARGUMENT, arg, value);
332 /* execute option callback */
333 if (value || opt->argument==NULL) {
334 option_cb (opt->shortopt, value);
335 opt = NULL;
336 }
337 }
338 /* check for short options */
339 else if (len>=2 && g_str_has_prefix(arg, "-")) {
340 size_t j;
342 for(j=1; j<len; j++) {
343 /* make sure we got an argument for the previous option */
344 if (opt && opt->argument)
345 option_error(ERROR_MISSING_ARGUMENT,
346 opt->longopt, opt->argument);
348 /* check if the option exists */
349 if ((opt=lookup_option(arg[j], NULL)) == NULL)
350 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
352 /* if no option argument is needed execute callback */
353 if (opt->argument == NULL) {
354 option_cb (opt->shortopt, NULL);
355 opt = NULL;
356 }
357 }
358 } else {
359 /* is this a option argument? */
360 if (opt && opt->argument) {
361 option_cb (opt->shortopt, arg);
362 opt = NULL;
363 } else
364 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
365 }
366 }
368 if (opt && opt->argument == NULL)
369 option_cb (opt->shortopt, NULL);
370 else if (opt && opt->argument)
371 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
373 if (!options.host && getenv("MPD_HOST")) {
374 g_free(options.host);
375 options.host = g_strdup(getenv("MPD_HOST"));
376 }
377 }
379 void
380 options_init(void)
381 {
382 /* default option values */
383 options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
384 options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
385 options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
386 #ifndef NCMPC_MINI
387 options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
388 #endif
389 if (getenv("MPD_TIMEOUT") != NULL)
390 /* let libmpdclient parse the environment variable */
391 options.timeout_ms = 0;
392 }
394 void
395 options_deinit(void)
396 {
397 g_free(options.host);
398 g_free(options.username);
399 g_free(options.password);
400 g_free(options.config_file);
401 g_free(options.key_file);
402 g_free(options.list_format);
403 g_free(options.status_format);
404 g_strfreev(options.screen_list);
405 #ifndef NCMPC_MINI
406 g_free(options.xterm_title_format);
407 g_free(options.scroll_sep);
408 #endif
409 #ifdef ENABLE_LYRICS_SCREEN
410 g_free(options.text_editor);
411 #endif
412 #ifdef ENABLE_CHAT_SCREEN
413 g_free(options.chat_prefix);
414 #endif
415 }