Code

options: don't leak options.text_editor
[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
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 #endif
57         .find_wrap = true,
58         .scroll_offset = 0,
59         .wide_cursor = true,
60         .audible_bell = true,
61         .bell_on_wrap = true,
62         .status_message_time = 3,
63         .timeout_ms = DEFAULT_MPD_TIMEOUT,
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 key bindings 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[32];
143                 if (option_table[i].argument)
144                         g_snprintf(tmp, sizeof(tmp), "%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 HAVE_CURSES_ENHANCED
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 #if !defined(NDEBUG) && !defined(NCMPC_MINI)
269         case 'K': /* --dump-keys */
270                 read_configuration();
271                 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
272                 exit(EXIT_SUCCESS);
273                 break;
274 #endif
275         default:
276                 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
277                 break;
278         }
281 void
282 options_parse(int argc, const char *argv[])
284         int i;
285         const arg_opt_t *opt = NULL;
286         option_callback_fn_t option_cb = handle_option;
288         for (i = 1; i < argc; i++) {
289                 const char *arg = argv[i];
290                 size_t len = strlen(arg);
292                 /* check for a long option */
293                 if (g_str_has_prefix(arg, "--")) {
294                         char *name, *value;
296                         /* make sure we got an argument for the previous option */
297                         if( opt && opt->argument )
298                                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
300                         /* retrieve a option argument */
301                         if ((value=g_strrstr(arg+2, "="))) {
302                                 *value = '\0';
303                                 name = g_strdup(arg);
304                                 *value = '=';
305                                 value++;
306                         } else
307                                 name = g_strdup(arg);
309                         /* check if the option exists */
310                         if( (opt=lookup_option(0, name+2)) == NULL )
311                                 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
312                         g_free(name);
314                         /* abort if we got an argument to the option and don't want one */
315                         if( value && opt->argument==NULL )
316                                 option_error(ERROR_GOT_ARGUMENT, arg, value);
318                         /* execute option callback */
319                         if (value || opt->argument==NULL) {
320                                 option_cb (opt->shortopt, value);
321                                 opt = NULL;
322                         }
323                 }
324                 /* check for short options */
325                 else if (len>=2 && g_str_has_prefix(arg, "-")) {
326                         size_t j;
328                         for(j=1; j<len; j++) {
329                                 /* make sure we got an argument for the previous option */
330                                 if (opt && opt->argument)
331                                         option_error(ERROR_MISSING_ARGUMENT,
332                                                      opt->longopt, opt->argument);
334                                 /* check if the option exists */
335                                 if ((opt=lookup_option(arg[j], NULL)) == NULL)
336                                         option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
338                                 /* if no option argument is needed execute callback */
339                                 if (opt->argument == NULL) {
340                                         option_cb (opt->shortopt, NULL);
341                                         opt = NULL;
342                                 }
343                         }
344                 } else {
345                         /* is this a option argument? */
346                         if (opt && opt->argument) {
347                                 option_cb (opt->shortopt, arg);
348                                 opt = NULL;
349                         } else
350                                 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
351                 }
352         }
354         if (opt && opt->argument == NULL)
355                 option_cb (opt->shortopt, NULL);
356         else if (opt && opt->argument)
357                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
359         if (!options.host && getenv("MPD_HOST")) {
360                 g_free(options.host);
361                 options.host = g_strdup(getenv("MPD_HOST"));
362         }
365 void
366 options_init(void)
368         /* default option values */
369         options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
370         options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
371         options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
372 #ifdef ENABLE_LYRICS_SCREEN
373         options.text_editor = g_strdup(DEFAULT_TEXT_EDITOR);
374 #endif
375 #ifndef NCMPC_MINI
376         options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
377 #endif
378         if (getenv("MPD_TIMEOUT") != NULL)
379                 /* let libmpdclient parse the environment variable */
380                 options.timeout_ms = 0;
383 void
384 options_deinit(void)
386         g_free(options.host);
387         g_free(options.username);
388         g_free(options.password);
389         g_free(options.config_file);
390         g_free(options.key_file);
391         g_free(options.list_format);
392         g_free(options.status_format);
393         g_strfreev(options.screen_list);
394 #ifndef NCMPC_MINI
395         g_free(options.xterm_title_format);
396         g_free(options.scroll_sep);
397 #endif
398 #ifdef ENABLE_LYRICS_SCREEN
399         g_free(options.text_editor);
400 #endif