Code

Added status-message-time option
[ncmpc.git] / src / options.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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.
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         .status_message_time = 3,
63 #ifndef NCMPC_MINI
64         .scroll = DEFAULT_SCROLL,
65         .welcome_screen_list = true,
66         .display_time = true,
67 #endif
68 };
70 static const arg_opt_t option_table[] = {
71         { '?', "help", NULL, "Show this help message" },
72         { 'V', "version", NULL, "Display version information" },
73         { 'c', "colors", NULL, "Enable colors" },
74         { 'C', "no-colors", NULL, "Disable colors" },
75 #ifdef HAVE_GETMOUSE
76         { 'm', "mouse", NULL, "Enable mouse" },
77         { 'M', "no-mouse", NULL, "Disable mouse" },
78 #endif
79         { 'e', "exit", NULL, "Exit on connection errors" },
80         { 'p', "port", "PORT", "Connect to server on port [" DEFAULT_PORT_STR "]" },
81         { 'h', "host", "HOST", "Connect to server on host [" DEFAULT_HOST "]" },
82         { 'P', "password","PASSWORD", "Connect with password" },
83         { 'f', "config", "FILE", "Read configuration from file" },
84         { 'k', "key-file","FILE", "Read configuration from file" },
85         { 'S', "no-splash", NULL, "Don't show the splash screen" },
86 #ifndef NDEBUG
87         { 'K', "dump-keys", NULL, "Dump key bindings to stdout" },
88 #endif
89 };
91 static const unsigned option_table_size = sizeof(option_table) / sizeof(option_table[0]);
93 static const arg_opt_t *
94 lookup_option(int s, char *l)
95 {
96         unsigned i;
98         for (i = 0; i < option_table_size; ++i) {
99                 if (l && strcmp(l, option_table[i].longopt) == 0)
100                         return &option_table[i];;
101                 if (s && s == option_table[i].shortopt)
102                         return &option_table[i];;
103         }
105         return NULL;
108 static void
109 option_error(int error, const char *option, const char *arg)
111         switch (error) {
112         case ERROR_UNKNOWN_OPTION:
113                 fprintf(stderr, PACKAGE ": invalid option %s\n", option);
114                 break;
115         case ERROR_BAD_ARGUMENT:
116                 fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
117                 break;
118         case ERROR_GOT_ARGUMENT:
119                 fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
120                 break;
121         case ERROR_MISSING_ARGUMENT:
122                 fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
123                 break;
124         default:
125                 fprintf(stderr, PACKAGE ": internal error %d\n", error);
126                 break;
127         }
129         exit(EXIT_FAILURE);
132 static void
133 display_help(void)
135         unsigned i;
137         printf("Usage: %s [OPTION]...\n", PACKAGE);
139         for (i = 0; i < option_table_size; ++i) {
140                 char tmp[MAX_LONGOPT_LENGTH];
142                 if (option_table[i].argument)
143                         g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s",
144                                    option_table[i].longopt,
145                                    option_table[i].argument);
146                 else
147                         g_strlcpy(tmp, option_table[i].longopt, 64);
149                 printf("  -%c, --%-20s %s\n",
150                        option_table[i].shortopt,
151                        tmp,
152                        option_table[i].descrition);
153                 i++;
154         }
157 static void
158 handle_option(int c, const char *arg)
160         switch (c) {
161         case '?': /* --help */
162                 display_help();
163                 exit(EXIT_SUCCESS);
164         case 'V': /* --version */
165                 puts(PACKAGE " version: " VERSION "\n"
166                      "build options:"
167 #ifdef NCMPC_MINI
168                      " mini"
169 #endif
170 #ifndef NDEBUG
171                      " debug"
172 #endif
173 #ifdef ENABLE_MULTIBYTE
174                      " multibyte"
175 #endif
176 #ifdef ENABLE_WIDE
177                      " wide"
178 #endif
179 #ifdef ENABLE_LOCALE
180                      " locale"
181 #endif
182 #ifdef ENABLE_NLS
183                      " nls"
184 #endif
185 #ifdef ENABLE_COLORS
186                      " colors"
187 #else
188                      " no-colors"
189 #endif
190 #ifdef ENABLE_LIRC
191                      " lirc"
192 #endif
193 #ifdef HAVE_GETMOUSE
194                      " getmouse"
195 #endif
196 #ifdef ENABLE_ARTIST_SCREEN
197                      " artist-screen"
198 #endif
199 #ifdef ENABLE_HELP_SCREEN
200                      " help-screen"
201 #endif
202 #ifdef ENABLE_SEARCH_SCREEN
203                      " search-screen"
204 #endif
205 #ifdef ENABLE_SONG_SCREEN
206                      " song-screen"
207 #endif
208 #ifdef ENABLE_KEYDEF_SCREEN
209                      " key-screen"
210 #endif
211 #ifdef ENABLE_LYRICS_SCREEN
212                      " lyrics-screen"
213 #endif
214 #ifdef ENABLE_OUTPUTS_SCREEN
215                      " outputs-screen"
216 #endif
218                      "\n");
219 #ifndef NCMPC_MINI
220                 if (strcmp("translator-credits", _("translator-credits")) != 0)
221                         /* To translators: these credits are shown
222                            when ncmpc is started with "--version" */
223                         printf("\n%s\n", _("translator-credits"));
224 #endif
225                 exit(EXIT_SUCCESS);
226         case 'c': /* --colors */
227 #ifdef ENABLE_COLORS
228                 options.enable_colors = true;
229 #endif
230                 break;
231         case 'C': /* --no-colors */
232 #ifdef ENABLE_COLORS
233                 options.enable_colors = false;
234 #endif
235                 break;
236         case 'm': /* --mouse */
237 #ifdef HAVE_GETMOUSE
238                 options.enable_mouse = true;
239 #endif
240                 break;
241         case 'M': /* --no-mouse */
242 #ifdef HAVE_GETMOUSE
243                 options.enable_mouse = false;
244 #endif
245                 break;
246         case 'e': /* --exit */
247                 /* deprecated */
248                 break;
249         case 'p': /* --port */
250                 options.port = atoi(arg);
251                 break;
252         case 'h': /* --host */
253                 if( options.host )
254                         g_free(options.host);
255                 options.host = g_strdup(arg);
256                 break;
257         case 'P': /* --password */
258                 if( options.password )
259                         g_free(options.password);
260                 options.password = locale_to_utf8(arg);
261                 break;
262         case 'f': /* --config */
263                 if( options.config_file )
264                         g_free(options.config_file);
265                 options.config_file = g_strdup(arg);
266                 break;
267         case 'k': /* --key-file */
268                 if( options.key_file )
269                         g_free(options.key_file);
270                 options.key_file = g_strdup(arg);
271                 break;
272         case 'S': /* --key-file */
273                 /* the splash screen was removed */
274                 break;
275 #ifndef NDEBUG
276         case 'K': /* --dump-keys */
277                 read_configuration();
278                 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
279                 exit(EXIT_SUCCESS);
280                 break;
281 #endif
282         default:
283                 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
284                 break;
285         }
288 void
289 options_parse(int argc, const char *argv[])
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);
369 void
370 options_init(void)
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