Code

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