options: remove double-semicolon
[ncmpc.git] / src / options.c
blob000a5757a6bd7125a559edfaeb848eb9b398977b
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.
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 #ifndef NCMPC_MINI
64 .scroll = DEFAULT_SCROLL,
65 .welcome_screen_list = true,
66 .jump_prefix_only = true,
67 .second_column = true,
68 #endif
71 static const arg_opt_t option_table[] = {
72 { '?', "help", NULL, "Show this help message" },
73 { 'V', "version", NULL, "Display version information" },
74 { 'c', "colors", NULL, "Enable colors" },
75 { 'C', "no-colors", NULL, "Disable colors" },
76 #ifdef HAVE_GETMOUSE
77 { 'm', "mouse", NULL, "Enable mouse" },
78 { 'M', "no-mouse", NULL, "Disable mouse" },
79 #endif
80 { 'e', "exit", NULL, "Exit on connection errors" },
81 { 'p', "port", "PORT", "Connect to server on port" },
82 { 'h', "host", "HOST", "Connect to server on host" },
83 { 'P', "password","PASSWORD", "Connect with password" },
84 { 'f', "config", "FILE", "Read configuration from file" },
85 { 'k', "key-file","FILE", "Read key bindings from file" },
86 #ifndef NDEBUG
87 { 'K', "dump-keys", NULL, "Dump key bindings to stdout" },
88 #endif
91 static const unsigned option_table_size = sizeof(option_table) / sizeof(option_table[0]);
93 static const arg_opt_t *
94 lookup_option(int s, char *l)
96 unsigned i;
98 for (i = 0; i < option_table_size; ++i) {
99 if (l && strcmp(l, option_table[i].longopt) == 0)
100 return &option_table[i];
101 if (s && s == option_table[i].shortopt)
102 return &option_table[i];
105 return NULL;
108 static void
109 option_error(int error, const char *option, const char *arg)
111 switch (error) {
112 case ERROR_UNKNOWN_OPTION:
113 fprintf(stderr, PACKAGE ": invalid option %s\n", option);
114 break;
115 case ERROR_BAD_ARGUMENT:
116 fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
117 break;
118 case ERROR_GOT_ARGUMENT:
119 fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
120 break;
121 case ERROR_MISSING_ARGUMENT:
122 fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
123 break;
124 default:
125 fprintf(stderr, PACKAGE ": internal error %d\n", error);
126 break;
129 exit(EXIT_FAILURE);
132 static void
133 display_help(void)
135 unsigned i;
137 printf("Usage: %s [OPTION]...\n", PACKAGE);
139 for (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);
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 ENABLE_WIDE
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
217 "\n");
218 #ifndef NCMPC_MINI
219 if (strcmp("translator-credits", _("translator-credits")) != 0)
220 /* To translators: these credits are shown
221 when ncmpc is started with "--version" */
222 printf("\n%s\n", _("translator-credits"));
223 #endif
224 exit(EXIT_SUCCESS);
225 case 'c': /* --colors */
226 #ifdef ENABLE_COLORS
227 options.enable_colors = true;
228 #endif
229 break;
230 case 'C': /* --no-colors */
231 #ifdef ENABLE_COLORS
232 options.enable_colors = false;
233 #endif
234 break;
235 case 'm': /* --mouse */
236 #ifdef HAVE_GETMOUSE
237 options.enable_mouse = true;
238 #endif
239 break;
240 case 'M': /* --no-mouse */
241 #ifdef HAVE_GETMOUSE
242 options.enable_mouse = false;
243 #endif
244 break;
245 case 'e': /* --exit */
246 /* deprecated */
247 break;
248 case 'p': /* --port */
249 options.port = atoi(arg);
250 break;
251 case 'h': /* --host */
252 g_free(options.host);
253 options.host = g_strdup(arg);
254 break;
255 case 'P': /* --password */
256 g_free(options.password);
257 options.password = locale_to_utf8(arg);
258 break;
259 case 'f': /* --config */
260 g_free(options.config_file);
261 options.config_file = g_strdup(arg);
262 break;
263 case 'k': /* --key-file */
264 g_free(options.key_file);
265 options.key_file = g_strdup(arg);
266 break;
267 #ifndef NDEBUG
268 #ifndef 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 #endif
276 default:
277 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
278 break;
282 void
283 options_parse(int argc, const char *argv[])
285 int i;
286 const arg_opt_t *opt = NULL;
287 option_callback_fn_t option_cb = handle_option;
289 for (i = 1; i < argc; i++) {
290 const char *arg = argv[i];
291 size_t len = strlen(arg);
293 /* check for a long option */
294 if (g_str_has_prefix(arg, "--")) {
295 char *name, *value;
297 /* make sure we got an argument for the previous option */
298 if( opt && opt->argument )
299 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
301 /* retrieve a option argument */
302 if ((value=g_strrstr(arg+2, "="))) {
303 *value = '\0';
304 name = g_strdup(arg);
305 *value = '=';
306 value++;
307 } else
308 name = g_strdup(arg);
310 /* check if the option exists */
311 if( (opt=lookup_option(0, name+2)) == NULL )
312 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
313 g_free(name);
315 /* abort if we got an argument to the option and don't want one */
316 if( value && opt->argument==NULL )
317 option_error(ERROR_GOT_ARGUMENT, arg, value);
319 /* execute option callback */
320 if (value || opt->argument==NULL) {
321 option_cb (opt->shortopt, value);
322 opt = NULL;
325 /* check for short options */
326 else if (len>=2 && g_str_has_prefix(arg, "-")) {
327 size_t j;
329 for(j=1; j<len; j++) {
330 /* make sure we got an argument for the previous option */
331 if (opt && opt->argument)
332 option_error(ERROR_MISSING_ARGUMENT,
333 opt->longopt, opt->argument);
335 /* check if the option exists */
336 if ((opt=lookup_option(arg[j], NULL)) == NULL)
337 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
339 /* if no option argument is needed execute callback */
340 if (opt->argument == NULL) {
341 option_cb (opt->shortopt, NULL);
342 opt = NULL;
345 } else {
346 /* is this a option argument? */
347 if (opt && opt->argument) {
348 option_cb (opt->shortopt, arg);
349 opt = NULL;
350 } else
351 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
355 if (opt && opt->argument == NULL)
356 option_cb (opt->shortopt, NULL);
357 else if (opt && opt->argument)
358 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
360 if (!options.host && getenv("MPD_HOST")) {
361 g_free(options.host);
362 options.host = g_strdup(getenv("MPD_HOST"));
366 void
367 options_init(void)
369 /* default option values */
370 options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
371 options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
372 options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
373 #ifndef NCMPC_MINI
374 options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
375 #endif
378 void
379 options_deinit(void)
381 g_free(options.host);
382 g_free(options.username);
383 g_free(options.password);
384 g_free(options.config_file);
385 g_free(options.key_file);
386 g_free(options.list_format);
387 g_free(options.status_format);
388 g_strfreev(options.screen_list);
389 #ifndef NCMPC_MINI
390 g_free(options.xterm_title_format);
391 g_free(options.scroll_sep);
392 #endif