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