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