Code

options: don't override the libmpdclient default timeout
[ncmpc.git] / src / options.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 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.
9  *
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.
14  *
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 ERROR_UNKNOWN_OPTION    0x01
34 #define ERROR_BAD_ARGUMENT      0x02
35 #define ERROR_GOT_ARGUMENT      0x03
36 #define ERROR_MISSING_ARGUMENT  0x04
38 typedef struct {
39         int shortopt;
40         const char *longopt;
41         const char *argument;
42         const char *descrition;
43 } arg_opt_t;
46 typedef void (*option_callback_fn_t)(int c, const char *arg);
49 options_t options = {
50         .crossfade_time = DEFAULT_CROSSFADE_TIME,
51         .seek_time = 1,
52 #ifdef ENABLE_LYRICS_SCREEN
53         .lyrics_timeout = DEFAULT_LYRICS_TIMEOUT,
54         .lyrics_autosave = false,
55         .lyrics_show_plugin = false,
56         .text_editor_ask = true,
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         .timeout_ms = 0,
65 #ifndef NCMPC_MINI
66         .scroll = DEFAULT_SCROLL,
67         .welcome_screen_list = true,
68         .jump_prefix_only = true,
69         .second_column = true,
70 #endif
71 };
73 static const arg_opt_t option_table[] = {
74         { '?', "help", NULL, "Show this help message" },
75         { 'V', "version", NULL, "Display version information" },
76         { 'c', "colors", NULL, "Enable colors" },
77         { 'C', "no-colors", NULL, "Disable colors" },
78 #ifdef HAVE_GETMOUSE
79         { 'm', "mouse", NULL, "Enable mouse" },
80         { 'M', "no-mouse", NULL, "Disable mouse" },
81 #endif
82         { 'e', "exit", NULL, "Exit on connection errors" },
83         { 'p', "port", "PORT", "Connect to server on port" },
84         { 'h', "host", "HOST", "Connect to server on host" },
85         { 'P', "password","PASSWORD", "Connect with password" },
86         { 'f', "config", "FILE", "Read configuration from file" },
87         { 'k', "key-file","FILE", "Read key bindings from file" },
88 #ifndef NDEBUG
89         { 'K', "dump-keys", NULL, "Dump key bindings to stdout" },
90 #endif
91 };
93 static const unsigned option_table_size = sizeof(option_table) / sizeof(option_table[0]);
95 static const arg_opt_t *
96 lookup_option(int s, char *l)
97 {
98         unsigned i;
100         for (i = 0; i < option_table_size; ++i) {
101                 if (l && strcmp(l, option_table[i].longopt) == 0)
102                         return &option_table[i];
103                 if (s && s == option_table[i].shortopt)
104                         return &option_table[i];
105         }
107         return NULL;
110 static void
111 option_error(int error, const char *option, const char *arg)
113         switch (error) {
114         case ERROR_UNKNOWN_OPTION:
115                 fprintf(stderr, PACKAGE ": invalid option %s\n", option);
116                 break;
117         case ERROR_BAD_ARGUMENT:
118                 fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
119                 break;
120         case ERROR_GOT_ARGUMENT:
121                 fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
122                 break;
123         case ERROR_MISSING_ARGUMENT:
124                 fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
125                 break;
126         default:
127                 fprintf(stderr, PACKAGE ": internal error %d\n", error);
128                 break;
129         }
131         exit(EXIT_FAILURE);
134 static void
135 display_help(void)
137         printf("Usage: %s [OPTION]...\n", PACKAGE);
139         for (unsigned i = 0; i < option_table_size; ++i) {
140                 char tmp[32];
142                 if (option_table[i].argument)
143                         g_snprintf(tmp, sizeof(tmp), "%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         }
156 static void
157 handle_option(int c, const char *arg)
159         switch (c) {
160         case '?': /* --help */
161                 display_help();
162                 exit(EXIT_SUCCESS);
163         case 'V': /* --version */
164                 puts(PACKAGE " version: " VERSION "\n"
165                      "build options:"
166 #ifdef NCMPC_MINI
167                      " mini"
168 #endif
169 #ifndef NDEBUG
170                      " debug"
171 #endif
172 #ifdef ENABLE_MULTIBYTE
173                      " multibyte"
174 #endif
175 #ifdef HAVE_CURSES_ENHANCED
176                      " wide"
177 #endif
178 #ifdef ENABLE_LOCALE
179                      " locale"
180 #endif
181 #ifdef ENABLE_NLS
182                      " nls"
183 #endif
184 #ifdef ENABLE_COLORS
185                      " colors"
186 #else
187                      " no-colors"
188 #endif
189 #ifdef ENABLE_LIRC
190                      " lirc"
191 #endif
192 #ifdef HAVE_GETMOUSE
193                      " getmouse"
194 #endif
195 #ifdef ENABLE_ARTIST_SCREEN
196                      " artist-screen"
197 #endif
198 #ifdef ENABLE_HELP_SCREEN
199                      " help-screen"
200 #endif
201 #ifdef ENABLE_SEARCH_SCREEN
202                      " search-screen"
203 #endif
204 #ifdef ENABLE_SONG_SCREEN
205                      " song-screen"
206 #endif
207 #ifdef ENABLE_KEYDEF_SCREEN
208                      " key-screen"
209 #endif
210 #ifdef ENABLE_LYRICS_SCREEN
211                      " lyrics-screen"
212 #endif
213 #ifdef ENABLE_OUTPUTS_SCREEN
214                      " outputs-screen"
215 #endif
216 #ifdef ENABLE_CHAT_SCREEN
217                      " chat-screen"
218 #endif
220                      "\n");
221 #ifndef NCMPC_MINI
222                 {
223                         char *user_conf = build_user_conf_filename();
224                         char *system_conf = build_system_conf_filename();
226                         printf("configuration files:\n %s\n %s\n\n",
227                                user_conf, system_conf);
229                         g_free(user_conf);
230                         g_free(system_conf);
231                 }
232                 if (strcmp("translator-credits", _("translator-credits")) != 0)
233                         /* To translators: these credits are shown
234                            when ncmpc is started with "--version" */
235                         printf("\n%s\n", _("translator-credits"));
236 #endif
237                 exit(EXIT_SUCCESS);
238         case 'c': /* --colors */
239 #ifdef ENABLE_COLORS
240                 options.enable_colors = true;
241 #endif
242                 break;
243         case 'C': /* --no-colors */
244 #ifdef ENABLE_COLORS
245                 options.enable_colors = false;
246 #endif
247                 break;
248         case 'm': /* --mouse */
249 #ifdef HAVE_GETMOUSE
250                 options.enable_mouse = true;
251 #endif
252                 break;
253         case 'M': /* --no-mouse */
254 #ifdef HAVE_GETMOUSE
255                 options.enable_mouse = false;
256 #endif
257                 break;
258         case 'e': /* --exit */
259                 /* deprecated */
260                 break;
261         case 'p': /* --port */
262                 options.port = atoi(arg);
263                 break;
264         case 'h': /* --host */
265                 g_free(options.host);
266                 options.host = g_strdup(arg);
267                 break;
268         case 'P': /* --password */
269                 g_free(options.password);
270                 options.password = locale_to_utf8(arg);
271                 break;
272         case 'f': /* --config */
273                 g_free(options.config_file);
274                 options.config_file = g_strdup(arg);
275                 break;
276         case 'k': /* --key-file */
277                 g_free(options.key_file);
278                 options.key_file = g_strdup(arg);
279                 break;
280 #if !defined(NDEBUG) && !defined(NCMPC_MINI)
281         case 'K': /* --dump-keys */
282                 read_configuration();
283                 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
284                 exit(EXIT_SUCCESS);
285                 break;
286 #endif
287         default:
288                 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
289                 break;
290         }
293 void
294 options_parse(int argc, const char *argv[])
296         const arg_opt_t *opt = NULL;
297         option_callback_fn_t option_cb = handle_option;
299         for (int i = 1; i < argc; i++) {
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         }
365         if (opt && opt->argument == NULL)
366                 option_cb (opt->shortopt, NULL);
367         else if (opt && opt->argument)
368                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
370         if (!options.host && getenv("MPD_HOST")) {
371                 g_free(options.host);
372                 options.host = g_strdup(getenv("MPD_HOST"));
373         }
376 void
377 options_init(void)
379         /* default option values */
380         options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
381         options.search_format = NULL;
382         options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
383         options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
384 #ifndef NCMPC_MINI
385         options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
386 #endif
389 void
390 options_deinit(void)
392         g_free(options.host);
393         g_free(options.username);
394         g_free(options.password);
395         g_free(options.config_file);
396         g_free(options.key_file);
397         g_free(options.list_format);
398         g_free(options.search_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 #ifdef ENABLE_LYRICS_SCREEN
406         g_free(options.text_editor);
407 #endif
408 #ifdef ENABLE_CHAT_SCREEN
409         g_free(options.chat_prefix);
410 #endif