Code

remove the rest of the splash screen
[ncmpc.git] / src / options.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 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         .second_column = true,
69 #endif
70 };
72 static const arg_opt_t option_table[] = {
73         { '?', "help", NULL, "Show this help message" },
74         { 'V', "version", NULL, "Display version information" },
75         { 'c', "colors", NULL, "Enable colors" },
76         { 'C', "no-colors", NULL, "Disable colors" },
77 #ifdef HAVE_GETMOUSE
78         { 'm', "mouse", NULL, "Enable mouse" },
79         { 'M', "no-mouse", NULL, "Disable mouse" },
80 #endif
81         { 'e', "exit", NULL, "Exit on connection errors" },
82         { 'p', "port", "PORT", "Connect to server on port" },
83         { 'h', "host", "HOST", "Connect to server on host" },
84         { 'P', "password","PASSWORD", "Connect with password" },
85         { 'f', "config", "FILE", "Read configuration from file" },
86         { 'k', "key-file","FILE", "Read configuration from file" },
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 #ifndef NDEBUG
269 #ifndef NCMPC_MINI
270         case 'K': /* --dump-keys */
271                 read_configuration();
272                 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
273                 exit(EXIT_SUCCESS);
274                 break;
275 #endif
276 #endif
277         default:
278                 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
279                 break;
280         }
283 void
284 options_parse(int argc, const char *argv[])
286         int i;
287         const arg_opt_t *opt = NULL;
288         option_callback_fn_t option_cb = handle_option;
290         i=1;
291         while (i < argc) {
292                 const char *arg = argv[i];
293                 size_t len = strlen(arg);
295                 /* check for a long option */
296                 if (g_str_has_prefix(arg, "--")) {
297                         char *name, *value;
299                         /* make sure we got an argument for the previous option */
300                         if( opt && opt->argument )
301                                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
303                         /* retrieve a option argument */
304                         if ((value=g_strrstr(arg+2, "="))) {
305                                 *value = '\0';
306                                 name = g_strdup(arg);
307                                 *value = '=';
308                                 value++;
309                         } else
310                                 name = g_strdup(arg);
312                         /* check if the option exists */
313                         if( (opt=lookup_option(0, name+2)) == NULL )
314                                 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
315                         g_free(name);
317                         /* abort if we got an argument to the option and don't want one */
318                         if( value && opt->argument==NULL )
319                                 option_error(ERROR_GOT_ARGUMENT, arg, value);
321                         /* execute option callback */
322                         if (value || opt->argument==NULL) {
323                                 option_cb (opt->shortopt, value);
324                                 opt = NULL;
325                         }
326                 }
327                 /* check for short options */
328                 else if (len>=2 && g_str_has_prefix(arg, "-")) {
329                         size_t j;
331                         for(j=1; j<len; j++) {
332                                 /* make sure we got an argument for the previous option */
333                                 if (opt && opt->argument)
334                                         option_error(ERROR_MISSING_ARGUMENT,
335                                                      opt->longopt, opt->argument);
337                                 /* check if the option exists */
338                                 if ((opt=lookup_option(arg[j], NULL)) == NULL)
339                                         option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
341                                 /* if no option argument is needed execute callback */
342                                 if (opt->argument == NULL) {
343                                         option_cb (opt->shortopt, NULL);
344                                         opt = NULL;
345                                 }
346                         }
347                 } else {
348                         /* is this a option argument? */
349                         if (opt && opt->argument) {
350                                 option_cb (opt->shortopt, arg);
351                                 opt = NULL;
352                         } else
353                                 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
354                 }
355                 i++;
356         }
358         if (opt && opt->argument == NULL)
359                 option_cb (opt->shortopt, NULL);
360         else if (opt && opt->argument)
361                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
363         if (!options.host && getenv("MPD_HOST")) {
364                 g_free(options.host);
365                 options.host = g_strdup(getenv("MPD_HOST"));
366         }
369 void
370 options_init(void)
372         /* default option values */
373         options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
374         options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
375         options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
376 #ifndef NCMPC_MINI
377         options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
378 #endif
381 void
382 options_deinit(void)
384         g_free(options.host);
385         g_free(options.username);
386         g_free(options.password);
387         g_free(options.config_file);
388         g_free(options.key_file);
389         g_free(options.list_format);
390         g_free(options.status_format);
391         g_strfreev(options.screen_list);
392 #ifndef NCMPC_MINI
393         g_free(options.xterm_title_format);
394         g_free(options.scroll_sep);
395 #endif