Code

options: disable the status bar clock by default
[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
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         .crossfade_time = DEFAULT_CROSSFADE_TIME,
53         .seek_time = 1,
54 #ifdef ENABLE_LYRICS_SCREEN
55         .lyrics_timeout = DEFAULT_LYRICS_TIMEOUT,
56         .lyrics_autosave = false,
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 #ifndef NCMPC_MINI
65         .scroll = DEFAULT_SCROLL,
66         .welcome_screen_list = true,
67         .jump_prefix_only = true,
68 #endif
69 };
71 static const arg_opt_t option_table[] = {
72         { '?', "help", NULL, "Show this help message" },
73         { 'V', "version", NULL, "Display version information" },
74         { 'c', "colors", NULL, "Enable colors" },
75         { 'C', "no-colors", NULL, "Disable colors" },
76 #ifdef HAVE_GETMOUSE
77         { 'm', "mouse", NULL, "Enable mouse" },
78         { 'M', "no-mouse", NULL, "Disable mouse" },
79 #endif
80         { 'e', "exit", NULL, "Exit on connection errors" },
81         { 'p', "port", "PORT", "Connect to server on port" },
82         { 'h', "host", "HOST", "Connect to server on host" },
83         { 'P', "password","PASSWORD", "Connect with password" },
84         { 'f', "config", "FILE", "Read configuration from file" },
85         { 'k', "key-file","FILE", "Read configuration from file" },
86         { 'S', "no-splash", NULL, "Don't show the splash screen" },
87 #ifndef NDEBUG
88         { 'K', "dump-keys", NULL, "Dump key bindings to stdout" },
89 #endif
90 };
92 static const unsigned option_table_size = sizeof(option_table) / sizeof(option_table[0]);
94 static const arg_opt_t *
95 lookup_option(int s, char *l)
96 {
97         unsigned i;
99         for (i = 0; i < option_table_size; ++i) {
100                 if (l && strcmp(l, option_table[i].longopt) == 0)
101                         return &option_table[i];;
102                 if (s && s == option_table[i].shortopt)
103                         return &option_table[i];;
104         }
106         return NULL;
109 static void
110 option_error(int error, const char *option, const char *arg)
112         switch (error) {
113         case ERROR_UNKNOWN_OPTION:
114                 fprintf(stderr, PACKAGE ": invalid option %s\n", option);
115                 break;
116         case ERROR_BAD_ARGUMENT:
117                 fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
118                 break;
119         case ERROR_GOT_ARGUMENT:
120                 fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
121                 break;
122         case ERROR_MISSING_ARGUMENT:
123                 fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
124                 break;
125         default:
126                 fprintf(stderr, PACKAGE ": internal error %d\n", error);
127                 break;
128         }
130         exit(EXIT_FAILURE);
133 static void
134 display_help(void)
136         unsigned i;
138         printf("Usage: %s [OPTION]...\n", PACKAGE);
140         for (i = 0; i < option_table_size; ++i) {
141                 char tmp[MAX_LONGOPT_LENGTH];
143                 if (option_table[i].argument)
144                         g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s",
145                                    option_table[i].longopt,
146                                    option_table[i].argument);
147                 else
148                         g_strlcpy(tmp, option_table[i].longopt, 64);
150                 printf("  -%c, --%-20s %s\n",
151                        option_table[i].shortopt,
152                        tmp,
153                        option_table[i].descrition);
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                 g_free(options.host);
254                 options.host = g_strdup(arg);
255                 break;
256         case 'P': /* --password */
257                 g_free(options.password);
258                 options.password = locale_to_utf8(arg);
259                 break;
260         case 'f': /* --config */
261                 g_free(options.config_file);
262                 options.config_file = g_strdup(arg);
263                 break;
264         case 'k': /* --key-file */
265                 g_free(options.key_file);
266                 options.key_file = g_strdup(arg);
267                 break;
268         case 'S': /* --key-file */
269                 /* the splash screen was removed */
270                 break;
271 #ifndef NDEBUG
272 #ifndef NCMPC_MINI
273         case 'K': /* --dump-keys */
274                 read_configuration();
275                 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
276                 exit(EXIT_SUCCESS);
277                 break;
278 #endif
279 #endif
280         default:
281                 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
282                 break;
283         }
286 void
287 options_parse(int argc, const char *argv[])
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 sure we got an argument for the previous option */
303                         if( opt && opt->argument )
304                                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
306                         /* retrieve 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 don't 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 sure 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);
367 void
368 options_init(void)
370         /* XXX
371         if ((tmp = g_strstr_len(options.host, strlen(options.host), "@"))) {
372                 char *oldhost = options.host;
373                 *tmp  = '\0';
374                 options.password = locale_to_utf8(oldhost);
375                 options.host = g_strdup(tmp+1);
376                 g_free(oldhost);
377         }
378         */
380         /* default option values */
381         options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
382         options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
383         options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
384         options.timedisplay_type = g_strdup(DEFAULT_TIMEDISPLAY_TYPE);
385 #ifndef NCMPC_MINI
386         options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
387 #endif
390 void
391 options_deinit(void)
393         g_free(options.host);
394         g_free(options.username);
395         g_free(options.password);
396         g_free(options.config_file);
397         g_free(options.key_file);
398         g_free(options.list_format);
399         g_free(options.status_format);
400         g_strfreev(options.screen_list);
401 #ifndef NCMPC_MINI
402         g_free(options.xterm_title_format);
403         g_free(options.scroll_sep);
404 #endif
405         g_free(options.timedisplay_type);