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