Code

options.c: Fixed compilation with --enable-mini --enable-debug
[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         .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 [" DEFAULT_PORT_STR "]" },
82         { 'h', "host", "HOST", "Connect to server on host [" DEFAULT_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                 i++;
155         }
158 static void
159 handle_option(int c, const char *arg)
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 ENABLE_WIDE
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
219                      "\n");
220 #ifndef NCMPC_MINI
221                 if (strcmp("translator-credits", _("translator-credits")) != 0)
222                         /* To translators: these credits are shown
223                            when ncmpc is started with "--version" */
224                         printf("\n%s\n", _("translator-credits"));
225 #endif
226                 exit(EXIT_SUCCESS);
227         case 'c': /* --colors */
228 #ifdef ENABLE_COLORS
229                 options.enable_colors = true;
230 #endif
231                 break;
232         case 'C': /* --no-colors */
233 #ifdef ENABLE_COLORS
234                 options.enable_colors = false;
235 #endif
236                 break;
237         case 'm': /* --mouse */
238 #ifdef HAVE_GETMOUSE
239                 options.enable_mouse = true;
240 #endif
241                 break;
242         case 'M': /* --no-mouse */
243 #ifdef HAVE_GETMOUSE
244                 options.enable_mouse = false;
245 #endif
246                 break;
247         case 'e': /* --exit */
248                 /* deprecated */
249                 break;
250         case 'p': /* --port */
251                 options.port = atoi(arg);
252                 break;
253         case 'h': /* --host */
254                 if( options.host )
255                         g_free(options.host);
256                 options.host = g_strdup(arg);
257                 break;
258         case 'P': /* --password */
259                 if( options.password )
260                         g_free(options.password);
261                 options.password = locale_to_utf8(arg);
262                 break;
263         case 'f': /* --config */
264                 if( options.config_file )
265                         g_free(options.config_file);
266                 options.config_file = g_strdup(arg);
267                 break;
268         case 'k': /* --key-file */
269                 if( options.key_file )
270                         g_free(options.key_file);
271                 options.key_file = g_strdup(arg);
272                 break;
273         case 'S': /* --key-file */
274                 /* the splash screen was removed */
275                 break;
276 #ifndef NDEBUG
277 #ifndef NCMPC_MINI
278         case 'K': /* --dump-keys */
279                 read_configuration();
280                 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
281                 exit(EXIT_SUCCESS);
282                 break;
283 #endif
284 #endif
285         default:
286                 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
287                 break;
288         }
291 void
292 options_parse(int argc, const char *argv[])
294         int i;
295         const arg_opt_t *opt = NULL;
296         option_callback_fn_t option_cb = handle_option;
298         i=1;
299         while (i < argc) {
300                 const char *arg = argv[i];
301                 size_t len = strlen(arg);
303                 /* check for a long option */
304                 if (g_str_has_prefix(arg, "--")) {
305                         char *name, *value;
307                         /* make sure we got an argument for the previous option */
308                         if( opt && opt->argument )
309                                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
311                         /* retrieve a option argument */
312                         if ((value=g_strrstr(arg+2, "="))) {
313                                 *value = '\0';
314                                 name = g_strdup(arg);
315                                 *value = '=';
316                                 value++;
317                         } else
318                                 name = g_strdup(arg);
320                         /* check if the option exists */
321                         if( (opt=lookup_option(0, name+2)) == NULL )
322                                 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
323                         g_free(name);
325                         /* abort if we got an argument to the option and don't want one */
326                         if( value && opt->argument==NULL )
327                                 option_error(ERROR_GOT_ARGUMENT, arg, value);
329                         /* execute option callback */
330                         if (value || opt->argument==NULL) {
331                                 option_cb (opt->shortopt, value);
332                                 opt = NULL;
333                         }
334                 }
335                 /* check for short options */
336                 else if (len>=2 && g_str_has_prefix(arg, "-")) {
337                         size_t j;
339                         for(j=1; j<len; j++) {
340                                 /* make sure we got an argument for the previous option */
341                                 if (opt && opt->argument)
342                                         option_error(ERROR_MISSING_ARGUMENT,
343                                                      opt->longopt, opt->argument);
345                                 /* check if the option exists */
346                                 if ((opt=lookup_option(arg[j], NULL)) == NULL)
347                                         option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
349                                 /* if no option argument is needed execute callback */
350                                 if (opt->argument == NULL) {
351                                         option_cb (opt->shortopt, NULL);
352                                         opt = NULL;
353                                 }
354                         }
355                 } else {
356                         /* is this a option argument? */
357                         if (opt && opt->argument) {
358                                 option_cb (opt->shortopt, arg);
359                                 opt = NULL;
360                         } else
361                                 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
362                 }
363                 i++;
364         }
366         if (opt && opt->argument == NULL)
367                 option_cb (opt->shortopt, NULL);
368         else if (opt && opt->argument)
369                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
372 void
373 options_init(void)
375         const char *value;
376         char *tmp;
378         /* get initial values for host and password from MPD_HOST (environment) */
379         if ((value = g_getenv(MPD_HOST_ENV)))
380                 options.host = g_strdup(value);
381         else
382                 options.host = g_strdup(DEFAULT_HOST);
384         if ((tmp = g_strstr_len(options.host, strlen(options.host), "@"))) {
385                 char *oldhost = options.host;
386                 *tmp  = '\0';
387                 options.password = locale_to_utf8(oldhost);
388                 options.host = g_strdup(tmp+1);
389                 g_free(oldhost);
390         }
392         /* get initial values for port from MPD_PORT (environment) */
393         if ((value = g_getenv(MPD_PORT_ENV)))
394                 options.port = atoi(value);
396         /* default option values */
397         options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
398         options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
399         options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
400         options.timedisplay_type = g_strdup(DEFAULT_TIMEDISPLAY_TYPE);
401 #ifndef NCMPC_MINI
402         options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
403 #endif