918f3135c715abb2eabf054a34df76fd98f23007
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"
25 #include "i18n.h"
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <glib.h>
32 #define MAX_LONGOPT_LENGTH 32
34 #define ERROR_UNKNOWN_OPTION 0x01
35 #define ERROR_BAD_ARGUMENT 0x02
36 #define ERROR_GOT_ARGUMENT 0x03
37 #define ERROR_MISSING_ARGUMENT 0x04
39 typedef struct {
40 int shortopt;
41 const char *longopt;
42 const char *argument;
43 const char *descrition;
44 } arg_opt_t;
47 typedef void (*option_callback_fn_t)(int c, const char *arg);
50 options_t options = {
51 .port = DEFAULT_PORT,
52 .crossfade_time = DEFAULT_CROSSFADE_TIME,
53 .seek_time = 1,
54 #ifdef ENABLE_LYRICS_SCREEN
55 .lyrics_timeout = DEFAULT_LYRICS_TIMEOUT,
56 #endif
57 .find_wrap = true,
58 .wide_cursor = true,
59 .audible_bell = true,
60 #ifndef NCMPC_MINI
61 .scroll = DEFAULT_SCROLL,
62 .welcome_screen_list = true,
63 #endif
64 };
66 static const arg_opt_t option_table[] = {
67 { '?', "help", NULL, "Show this help message" },
68 { 'V', "version", NULL, "Display version information" },
69 { 'c', "colors", NULL, "Enable colors" },
70 { 'C', "no-colors", NULL, "Disable colors" },
71 #ifdef HAVE_GETMOUSE
72 { 'm', "mouse", NULL, "Enable mouse" },
73 { 'M', "no-mouse", NULL, "Disable mouse" },
74 #endif
75 { 'e', "exit", NULL, "Exit on connection errors" },
76 { 'p', "port", "PORT", "Connect to server on port [" DEFAULT_PORT_STR "]" },
77 { 'h', "host", "HOST", "Connect to server on host [" DEFAULT_HOST "]" },
78 { 'P', "password","PASSWORD", "Connect with password" },
79 { 'f', "config", "FILE", "Read configuration from file" },
80 { 'k', "key-file","FILE", "Read configuration from file" },
81 { 'S', "no-splash", NULL, "Don't show the splash screen" },
82 #ifndef NDEBUG
83 { 'K', "dump-keys", NULL, "Dump key bindings to stdout" },
84 #endif
85 };
87 static const unsigned option_table_size = sizeof(option_table) / sizeof(option_table[0]);
89 static const arg_opt_t *
90 lookup_option(int s, char *l)
91 {
92 unsigned i;
94 for (i = 0; i < option_table_size; ++i) {
95 if (l && strcmp(l, option_table[i].longopt) == 0)
96 return &option_table[i];;
97 if (s && s == option_table[i].shortopt)
98 return &option_table[i];;
99 }
101 return NULL;
102 }
104 static void
105 option_error(int error, const char *option, const char *arg)
106 {
107 switch (error) {
108 case ERROR_UNKNOWN_OPTION:
109 fprintf(stderr, PACKAGE ": invalid option %s\n", option);
110 break;
111 case ERROR_BAD_ARGUMENT:
112 fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
113 break;
114 case ERROR_GOT_ARGUMENT:
115 fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
116 break;
117 case ERROR_MISSING_ARGUMENT:
118 fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
119 break;
120 default:
121 fprintf(stderr, PACKAGE ": internal error %d\n", error);
122 break;
123 }
125 exit(EXIT_FAILURE);
126 }
128 static void
129 display_help(void)
130 {
131 unsigned i;
133 printf("Usage: %s [OPTION]...\n", PACKAGE);
135 for (i = 0; i < option_table_size; ++i) {
136 char tmp[MAX_LONGOPT_LENGTH];
138 if (option_table[i].argument)
139 g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s",
140 option_table[i].longopt,
141 option_table[i].argument);
142 else
143 g_strlcpy(tmp, option_table[i].longopt, 64);
145 printf(" -%c, --%-20s %s\n",
146 option_table[i].shortopt,
147 tmp,
148 option_table[i].descrition);
149 i++;
150 }
151 }
153 static void
154 handle_option(int c, const char *arg)
155 {
156 switch (c) {
157 case '?': /* --help */
158 display_help();
159 exit(EXIT_SUCCESS);
160 case 'V': /* --version */
161 puts(PACKAGE " version: " VERSION "\n"
162 "build options:"
163 #ifdef NCMPC_MINI
164 " mini"
165 #endif
166 #ifndef NDEBUG
167 " debug"
168 #endif
169 #ifdef ENABLE_MULTIBYTE
170 " multibyte"
171 #endif
172 #ifdef ENABLE_WIDE
173 " wide"
174 #endif
175 #ifdef ENABLE_LOCALE
176 " locale"
177 #endif
178 #ifdef ENABLE_NLS
179 " nls"
180 #endif
181 #ifdef ENABLE_COLORS
182 " colors"
183 #else
184 " no-colors"
185 #endif
186 #ifdef ENABLE_LIRC
187 " lirc"
188 #endif
189 #ifdef HAVE_GETMOUSE
190 " getmouse"
191 #endif
192 #ifdef ENABLE_ARTIST_SCREEN
193 " artist-screen"
194 #endif
195 #ifdef ENABLE_HELP_SCREEN
196 " help-screen"
197 #endif
198 #ifdef ENABLE_SEARCH_SCREEN
199 " search-screen"
200 #endif
201 #ifdef ENABLE_SONG_SCREEN
202 " song-screen"
203 #endif
204 #ifdef ENABLE_KEYDEF_SCREEN
205 " key-screen"
206 #endif
207 #ifdef ENABLE_LYRICS_SCREEN
208 " lyrics-screen"
209 #endif
210 #ifdef ENABLE_OUTPUTS_SCREEN
211 " outputs-screen"
212 #endif
214 "\n");
215 #ifndef NCMPC_MINI
216 if (strcmp("translator-credits", _("translator-credits")) != 0)
217 /* To translators: these credits are shown
218 when ncmpc is started with "--version" */
219 printf("\n%s\n", _("translator-credits"));
220 #endif
221 exit(EXIT_SUCCESS);
222 case 'c': /* --colors */
223 #ifdef ENABLE_COLORS
224 options.enable_colors = true;
225 #endif
226 break;
227 case 'C': /* --no-colors */
228 #ifdef ENABLE_COLORS
229 options.enable_colors = false;
230 #endif
231 break;
232 case 'm': /* --mouse */
233 #ifdef HAVE_GETMOUSE
234 options.enable_mouse = true;
235 #endif
236 break;
237 case 'M': /* --no-mouse */
238 #ifdef HAVE_GETMOUSE
239 options.enable_mouse = false;
240 #endif
241 break;
242 case 'e': /* --exit */
243 /* deprecated */
244 break;
245 case 'p': /* --port */
246 options.port = atoi(arg);
247 break;
248 case 'h': /* --host */
249 if( options.host )
250 g_free(options.host);
251 options.host = g_strdup(arg);
252 break;
253 case 'P': /* --password */
254 if( options.password )
255 g_free(options.password);
256 options.password = locale_to_utf8(arg);
257 break;
258 case 'f': /* --config */
259 if( options.config_file )
260 g_free(options.config_file);
261 options.config_file = g_strdup(arg);
262 break;
263 case 'k': /* --key-file */
264 if( options.key_file )
265 g_free(options.key_file);
266 options.key_file = g_strdup(arg);
267 break;
268 case 'S': /* --key-file */
269 /* the splash screen was removed */
270 break;
271 #ifndef NDEBUG
272 case 'K': /* --dump-keys */
273 read_configuration();
274 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
275 exit(EXIT_SUCCESS);
276 break;
277 #endif
278 default:
279 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
280 break;
281 }
282 }
284 void
285 options_parse(int argc, const char *argv[])
286 {
287 int i;
288 const arg_opt_t *opt = NULL;
289 option_callback_fn_t option_cb = handle_option;
291 i=1;
292 while (i < argc) {
293 const char *arg = argv[i];
294 size_t len = strlen(arg);
296 /* check for a long option */
297 if (g_str_has_prefix(arg, "--")) {
298 char *name, *value;
300 /* make shure we got an argument for the previous option */
301 if( opt && opt->argument )
302 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
304 /* retreive a option argument */
305 if ((value=g_strrstr(arg+2, "="))) {
306 *value = '\0';
307 name = g_strdup(arg);
308 *value = '=';
309 value++;
310 } else
311 name = g_strdup(arg);
313 /* check if the option exists */
314 if( (opt=lookup_option(0, name+2)) == NULL )
315 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
316 g_free(name);
318 /* abort if we got an argument to the option and dont want one */
319 if( value && opt->argument==NULL )
320 option_error(ERROR_GOT_ARGUMENT, arg, value);
322 /* execute option callback */
323 if (value || opt->argument==NULL) {
324 option_cb (opt->shortopt, value);
325 opt = NULL;
326 }
327 }
328 /* check for short options */
329 else if (len>=2 && g_str_has_prefix(arg, "-")) {
330 size_t j;
332 for(j=1; j<len; j++) {
333 /* make shure we got an argument for the previous option */
334 if (opt && opt->argument)
335 option_error(ERROR_MISSING_ARGUMENT,
336 opt->longopt, opt->argument);
338 /* check if the option exists */
339 if ((opt=lookup_option(arg[j], NULL)) == NULL)
340 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
342 /* if no option argument is needed execute callback */
343 if (opt->argument == NULL) {
344 option_cb (opt->shortopt, NULL);
345 opt = NULL;
346 }
347 }
348 } else {
349 /* is this a option argument? */
350 if (opt && opt->argument) {
351 option_cb (opt->shortopt, arg);
352 opt = NULL;
353 } else
354 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
355 }
356 i++;
357 }
359 if (opt && opt->argument == NULL)
360 option_cb (opt->shortopt, NULL);
361 else if (opt && opt->argument)
362 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
363 }
365 void
366 options_init(void)
367 {
368 const char *value;
369 char *tmp;
371 /* get initial values for host and password from MPD_HOST (enviroment) */
372 if ((value = g_getenv(MPD_HOST_ENV)))
373 options.host = g_strdup(value);
374 else
375 options.host = g_strdup(DEFAULT_HOST);
377 if ((tmp = g_strstr_len(options.host, strlen(options.host), "@"))) {
378 char *oldhost = options.host;
379 *tmp = '\0';
380 options.password = locale_to_utf8(oldhost);
381 options.host = g_strdup(tmp+1);
382 g_free(oldhost);
383 }
385 /* get initial values for port from MPD_PORT (enviroment) */
386 if ((value = g_getenv(MPD_PORT_ENV)))
387 options.port = atoi(value);
389 /* default option values */
390 options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
391 options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
392 options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
393 options.timedisplay_type = g_strdup(DEFAULT_TIMEDISPLAY_TYPE);
394 #ifndef NCMPC_MINI
395 options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
396 #endif
397 }