Code

options: don't store disabled options
[ncmpc.git] / src / options.c
1 /*
2  * (c) 2004 by Kalle Wallin <kaw@linux.se>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "options.h"
20 #include "config.h"
21 #include "defaults.h"
22 #include "charset.h"
23 #include "command.h"
24 #include "conf.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include <glib.h>
30 #define MAX_LONGOPT_LENGTH 32
32 #define ERROR_UNKNOWN_OPTION    0x01
33 #define ERROR_BAD_ARGUMENT      0x02
34 #define ERROR_GOT_ARGUMENT      0x03
35 #define ERROR_MISSING_ARGUMENT  0x04
37 typedef struct {
38         int shortopt;
39         const char *longopt;
40         const char *argument;
41         const char *descrition;
42 } arg_opt_t;
45 typedef void (*option_callback_fn_t)(int c, const char *arg);
48 options_t options = {
49         .port = DEFAULT_PORT,
50         .crossfade_time = DEFAULT_CROSSFADE_TIME,
51         .seek_time = 1,
52 #ifdef ENABLE_LYRICS_SCREEN
53         .lyrics_timeout = DEFAULT_LYRICS_TIMEOUT,
54 #endif
55         .find_wrap = true,
56         .wide_cursor = true,
57         .audible_bell = true,
58         .scroll = DEFAULT_SCROLL,
59         .welcome_screen_list = true,
60 };
62 static const arg_opt_t option_table[] = {
63         { '?', "help", NULL, "Show this help message" },
64         { 'V', "version", NULL, "Display version information" },
65         { 'c', "colors", NULL, "Enable colors" },
66         { 'C', "no-colors", NULL, "Disable colors" },
67 #ifdef HAVE_GETMOUSE
68         { 'm', "mouse", NULL, "Enable mouse" },
69         { 'M', "no-mouse", NULL, "Disable mouse" },
70 #endif
71         { 'e', "exit", NULL, "Exit on connection errors" },
72         { 'p', "port", "PORT", "Connect to server on port [" DEFAULT_PORT_STR "]" },
73         { 'h', "host", "HOST", "Connect to server on host [" DEFAULT_HOST "]" },
74         { 'P', "password","PASSWORD", "Connect with password" },
75         { 'f', "config", "FILE", "Read configuration from file" },
76         { 'k', "key-file","FILE", "Read configuration from file" },
77         { 'S', "no-splash", NULL, "Don't show the splash screen" },
78 #ifndef NDEBUG
79         { 'K', "dump-keys", NULL, "Dump key bindings to stdout" },
80 #endif
81 };
83 static const unsigned option_table_size = sizeof(option_table) / sizeof(option_table[0]);
85 static const arg_opt_t *
86 lookup_option(int s, char *l)
87 {
88         unsigned i;
90         for (i = 0; i < option_table_size; ++i) {
91                 if (l && strcmp(l, option_table[i].longopt) == 0)
92                         return &option_table[i];;
93                 if (s && s == option_table[i].shortopt)
94                         return &option_table[i];;
95         }
97         return NULL;
98 }
100 static void
101 option_error(int error, const char *option, const char *arg)
103         switch (error) {
104         case ERROR_UNKNOWN_OPTION:
105                 fprintf(stderr, PACKAGE ": invalid option %s\n", option);
106                 break;
107         case ERROR_BAD_ARGUMENT:
108                 fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
109                 break;
110         case ERROR_GOT_ARGUMENT:
111                 fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
112                 break;
113         case ERROR_MISSING_ARGUMENT:
114                 fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
115                 break;
116         default:
117                 fprintf(stderr, PACKAGE ": internal error %d\n", error);
118                 break;
119         }
121         exit(EXIT_FAILURE);
124 static void
125 display_help(void)
127         unsigned i;
129         printf("Usage: %s [OPTION]...\n", PACKAGE);
131         for (i = 0; i < option_table_size; ++i) {
132                 char tmp[MAX_LONGOPT_LENGTH];
134                 if (option_table[i].argument)
135                         g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s",
136                                    option_table[i].longopt,
137                                    option_table[i].argument);
138                 else
139                         g_strlcpy(tmp, option_table[i].longopt, 64);
141                 printf("  -%c, --%-20s %s\n",
142                        option_table[i].shortopt,
143                        tmp,
144                        option_table[i].descrition);
145                 i++;
146         }
149 static void
150 handle_option(int c, const char *arg)
152         switch (c) {
153         case '?': /* --help */
154                 display_help();
155                 exit(EXIT_SUCCESS);
156         case 'V': /* --version */
157                 puts(PACKAGE " version: " VERSION "\n"
158                      "build options:"
159 #ifndef NDEBUG
160                      " debug"
161 #endif
162 #ifdef ENABLE_NLS
163                      " nls"
164 #endif
165 #ifdef ENABLE_COLORS
166                      " colors"
167 #else
168                      " no-colors"
169 #endif
170 #ifdef HAVE_GETMOUSE
171                      " getmouse"
172 #endif
173 #ifdef ENABLE_ARTIST_SCREEN
174                      " artist-screen"
175 #endif
176 #ifdef ENABLE_SEARCH_SCREEN
177                      " search-screen"
178 #endif
179 #ifdef ENABLE_KEYDEF_SCREEN
180                      " key-screen"
181 #endif
182                      "\n");
183                 exit(EXIT_SUCCESS);
184         case 'c': /* --colors */
185 #ifdef ENABLE_COLORS
186                 options.enable_colors = true;
187 #endif
188                 break;
189         case 'C': /* --no-colors */
190 #ifdef ENABLE_COLORS
191                 options.enable_colors = false;
192 #endif
193                 break;
194         case 'm': /* --mouse */
195 #ifdef HAVE_GETMOUSE
196                 options.enable_mouse = true;
197 #endif
198                 break;
199         case 'M': /* --no-mouse */
200 #ifdef HAVE_GETMOUSE
201                 options.enable_mouse = false;
202 #endif
203                 break;
204         case 'e': /* --exit */
205                 /* deprecated */
206                 break;
207         case 'p': /* --port */
208                 options.port = atoi(arg);
209                 break;
210         case 'h': /* --host */
211                 if( options.host )
212                         g_free(options.host);
213                 options.host = g_strdup(arg);
214                 break;
215         case 'P': /* --password */
216                 if( options.password )
217                         g_free(options.password);
218                 options.password = locale_to_utf8(arg);
219                 break;
220         case 'f': /* --config */
221                 if( options.config_file )
222                         g_free(options.config_file);
223                 options.config_file = g_strdup(arg);
224                 break;
225         case 'k': /* --key-file */
226                 if( options.key_file )
227                         g_free(options.key_file);
228                 options.key_file = g_strdup(arg);
229                 break;
230         case 'S': /* --key-file */
231                 /* the splash screen was removed */
232                 break;
233 #ifndef NDEBUG
234         case 'K': /* --dump-keys */
235                 read_configuration();
236                 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
237                 exit(EXIT_SUCCESS);
238                 break;
239 #endif
240         default:
241                 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
242                 break;
243         }
246 void
247 options_parse(int argc, const char *argv[])
249         int i;
250         const arg_opt_t *opt = NULL;
251         option_callback_fn_t option_cb = handle_option;
253         i=1;
254         while (i < argc) {
255                 const char *arg = argv[i];
256                 size_t len = strlen(arg);
258                 /* check for a long option */
259                 if (g_str_has_prefix(arg, "--")) {
260                         char *name, *value;
262                         /* make shure we got an argument for the previous option */
263                         if( opt && opt->argument )
264                                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
266                         /* retreive a option argument */
267                         if ((value=g_strrstr(arg+2, "="))) {
268                                 *value = '\0';
269                                 name = g_strdup(arg);
270                                 *value = '=';
271                                 value++;
272                         } else
273                                 name = g_strdup(arg);
275                         /* check if the option exists */
276                         if( (opt=lookup_option(0, name+2)) == NULL )
277                                 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
278                         g_free(name);
280                         /* abort if we got an argument to the option and dont want one */
281                         if( value && opt->argument==NULL )
282                                 option_error(ERROR_GOT_ARGUMENT, arg, value);
284                         /* execute option callback */
285                         if (value || opt->argument==NULL) {
286                                 option_cb (opt->shortopt, value);
287                                 opt = NULL;
288                         }
289                 }
290                 /* check for short options */
291                 else if (len>=2 && g_str_has_prefix(arg, "-")) {
292                         size_t j;
294                         for(j=1; j<len; j++) {
295                                 /* make shure we got an argument for the previous option */
296                                 if (opt && opt->argument)
297                                         option_error(ERROR_MISSING_ARGUMENT,
298                                                      opt->longopt, opt->argument);
300                                 /* check if the option exists */
301                                 if ((opt=lookup_option(arg[j], NULL)) == NULL)
302                                         option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
304                                 /* if no option argument is needed execute callback */
305                                 if (opt->argument == NULL) {
306                                         option_cb (opt->shortopt, NULL);
307                                         opt = NULL;
308                                 }
309                         }
310                 } else {
311                         /* is this a option argument? */
312                         if (opt && opt->argument) {
313                                 option_cb (opt->shortopt, arg);
314                                 opt = NULL;
315                         } else
316                                 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
317                 }
318                 i++;
319         }
321         if (opt && opt->argument == NULL)
322                 option_cb (opt->shortopt, NULL);
323         else if (opt && opt->argument)
324                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
327 void
328 options_init(void)
330         const char *value;
331         char *tmp;
333         /* get initial values for host and password from MPD_HOST (enviroment) */
334         if ((value = g_getenv(MPD_HOST_ENV)))
335                 options.host = g_strdup(value);
336         else
337                 options.host = g_strdup(DEFAULT_HOST);
339         if ((tmp = g_strstr_len(options.host, strlen(options.host), "@"))) {
340                 char *oldhost = options.host;
341                 *tmp  = '\0';
342                 options.password = locale_to_utf8(oldhost);
343                 options.host = g_strdup(tmp+1);
344                 g_free(oldhost);
345         }
347         /* get initial values for port from MPD_PORT (enviroment) */
348         if ((value = g_getenv(MPD_PORT_ENV)))
349                 options.port = atoi(value);
351         /* default option values */
352         options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
353         options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
354         options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
355         options.timedisplay_type = g_strdup(DEFAULT_TIMEDISPLAY_TYPE);
356         options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);