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
43 typedef struct
44 {
45 int shortopt;
46 char *longopt;
47 char *argument;
48 char *descrition;
49 } arg_opt_t;
52 typedef void (*option_callback_fn_t) (int c, char *arg);
55 options_t options;
57 static arg_opt_t option_table[] = {
58 { '?', "help", NULL, "Show this help message" },
59 { 'V', "version", NULL, "Display version information" },
60 { 'c', "colors", NULL, "Enable colors" },
61 { 'C', "no-colors", NULL, "Disable colors" },
62 #ifdef HAVE_GETMOUSE
63 { 'm', "mouse", NULL, "Enable mouse" },
64 { 'M', "no-mouse", NULL, "Disable mouse" },
65 #endif
66 { 'e', "exit", NULL, "Exit on connection errors" },
67 { 'p', "port", "PORT", "Connect to server on port [" DEFAULT_PORT_STR "]" },
68 { 'h', "host", "HOST", "Connect to server on host [" DEFAULT_HOST "]" },
69 { 'P', "password","PASSWORD", "Connect with password" },
70 { 'f', "config", "FILE", "Read configuration from file" },
71 { 'k', "key-file","FILE", "Read configuration from file" },
72 #ifdef DEBUG
73 { 'K', "dump-keys", NULL, "Dump key bindings to stdout" },
74 { 'D', "debug", NULL, "Enable debug output on stderr" },
75 #endif
76 { 0, NULL, NULL, NULL },
77 };
79 static arg_opt_t *
80 lookup_option(int s, char *l)
81 {
82 int i;
84 i=0;
85 while( option_table[i].descrition )
86 {
87 if( l && strcmp(l, option_table[i].longopt) == 0 )
88 return &option_table[i];;
89 if( s && s==option_table[i].shortopt )
90 return &option_table[i];;
91 i++;
92 }
93 return NULL;
94 }
96 static void
97 option_error(int error, char *option, char *arg)
98 {
99 switch(error)
100 {
101 case ERROR_UNKNOWN_OPTION:
102 fprintf(stderr, PACKAGE ": invalid option %s\n", option);
103 break;
104 case ERROR_BAD_ARGUMENT:
105 fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
106 break;
107 case ERROR_GOT_ARGUMENT:
108 fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
109 break;
110 case ERROR_MISSING_ARGUMENT:
111 fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
112 break;
113 default:
114 fprintf(stderr, PACKAGE ": internal error %d\n", error);
115 break;
116 }
117 exit(EXIT_FAILURE);
118 }
120 static void
121 display_help(void)
122 {
123 int i = 0;
125 printf("Usage: %s [OPTION]...\n", PACKAGE);
126 while( option_table[i].descrition )
127 {
128 char tmp[MAX_LONGOPT_LENGTH];
130 if( option_table[i].argument )
131 g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s",
132 option_table[i].longopt,
133 option_table[i].argument);
134 else
135 g_strlcpy(tmp, option_table[i].longopt, 64);
137 printf(" -%c, --%-20s %s\n",
138 option_table[i].shortopt,
139 tmp,
140 option_table[i].descrition);
141 i++;
142 }
143 }
145 static void
146 handle_option(int c, char *arg)
147 {
148 D("option callback -%c %s\n", c, arg);
149 switch(c)
150 {
151 case '?': /* --help */
152 display_help();
153 exit(EXIT_SUCCESS);
154 case 'V': /* --version */
155 printf("%s version: %s\n", PACKAGE, VERSION);
156 printf("build options:");
157 #ifdef DEBUG
158 printf(" debug");
159 #endif
160 #ifdef ENABLE_NLS
161 printf(" nls");
162 #endif
163 #ifdef HAVE_GETMOUSE
164 printf(" getmouse");
165 #endif
166 #ifdef ENABLE_SEARCH_SCREEN
167 printf(" search-screen");
168 #endif
169 #ifdef ENABLE_KEYDEF_SCREEN
170 printf(" key-screen");
171 #endif
172 #ifdef ENABLE_CLOCK_SCREEN
173 printf(" clock-screen");
174 #endif
175 printf("\n");
176 exit(EXIT_SUCCESS);
177 case 'c': /* --colors */
178 options.enable_colors = TRUE;
179 break;
180 case 'C': /* --no-colors */
181 options.enable_colors = FALSE;
182 break;
183 case 'm': /* --mouse */
184 options.enable_mouse = TRUE;
185 break;
186 case 'M': /* --no-mouse */
187 options.enable_mouse = FALSE;
188 break;
189 case 'e': /* --exit */
190 options.reconnect = FALSE;
191 break;
192 case 'p': /* --port */
193 options.port = atoi(arg);
194 break;
195 case 'h': /* --host */
196 if( options.host )
197 g_free(options.host);
198 options.host = g_strdup(arg);
199 break;
200 case 'P': /* --password */
201 if( options.password )
202 g_free(options.password);
203 options.password = locale_to_utf8(arg);
204 break;
205 case 'f': /* --config */
206 if( options.config_file )
207 g_free(options.config_file);
208 options.config_file = g_strdup(arg);
209 break;
210 case 'k': /* --key-file */
211 if( options.key_file )
212 g_free(options.key_file);
213 options.key_file = g_strdup(arg);
214 break;
215 #ifdef DEBUG
216 case 'K': /* --dump-keys */
217 read_configuration(&options);
218 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
219 exit(EXIT_SUCCESS);
220 break;
221 case 'D': /* --debug */
222 options.debug = TRUE;
223 break;
224 #endif
225 default:
226 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
227 break;
228 }
229 }
231 options_t *
232 options_get(void)
233 {
234 return &options;
235 }
237 options_t *
238 options_parse(int argc, const char *argv[])
239 {
240 int i;
241 arg_opt_t *opt = NULL;
242 option_callback_fn_t option_cb = handle_option;
244 i=1;
245 while( i<argc )
246 {
247 char *arg = (char *) argv[i];
248 size_t len=strlen(arg);
250 /* check for a long option */
251 if( g_str_has_prefix(arg, "--") )
252 {
253 char *name, *value;
255 /* make shure we got an argument for the previous option */
256 if( opt && opt->argument )
257 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
259 /* retreive a option argument */
260 if( (value=g_strrstr(arg+2, "=")) )
261 {
262 *value = '\0';
263 name = g_strdup(arg);
264 *value = '=';
265 value++;
266 }
267 else
268 name = g_strdup(arg);
270 /* check if the option exists */
271 if( (opt=lookup_option(0, name+2)) == NULL )
272 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
273 g_free(name);
275 /* abort if we got an argument to the option and dont want one */
276 if( value && opt->argument==NULL )
277 option_error(ERROR_GOT_ARGUMENT, arg, value);
279 /* execute option callback */
280 if( value || opt->argument==NULL )
281 {
282 option_cb (opt->shortopt, value);
283 opt = NULL;
284 }
285 }
286 /* check for short options */
287 else if( len>=2 && g_str_has_prefix(arg, "-") )
288 {
289 int j;
291 for(j=1; j<len; j++)
292 {
293 /* make shure we got an argument for the previous option */
294 if( opt && opt->argument )
295 option_error(ERROR_MISSING_ARGUMENT,
296 opt->longopt, opt->argument);
298 /* check if the option exists */
299 if( (opt=lookup_option(arg[j], NULL))==NULL )
300 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
302 /* if no option argument is needed execute callback */
303 if( opt->argument==NULL )
304 {
305 option_cb (opt->shortopt, NULL);
306 opt = NULL;
307 }
308 }
309 }
310 else
311 {
312 /* is this a option argument? */
313 if( opt && opt->argument)
314 {
315 option_cb (opt->shortopt, arg);
316 opt = NULL;
317 }
318 else
319 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
320 }
321 i++;
322 }
324 if( opt && opt->argument==NULL)
325 option_cb (opt->shortopt, NULL);
326 else if( opt && opt->argument )
327 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
329 return &options;
330 }
333 options_t *
334 options_init( void )
335 {
336 const char *value;
337 char *tmp;
339 memset(&options, 0, sizeof(options_t));
341 /* get initial values for host and password from MPD_HOST (enviroment) */
342 if( (value=g_getenv(MPD_HOST_ENV)) )
343 options.host = g_strdup(value);
344 else
345 options.host = g_strdup(DEFAULT_HOST);
346 if( (tmp=g_strstr_len(options.host, strlen(options.host), "@")) )
347 {
348 char *oldhost = options.host;
349 *tmp = '\0';
350 options.password = locale_to_utf8(oldhost);
351 options.host = g_strdup(tmp+1);
352 g_free(oldhost);
353 }
354 /* get initial values for port from MPD_PORT (enviroment) */
355 if( (value=g_getenv(MPD_PORT_ENV)) )
356 options.port = atoi(value);
357 else
358 options.port = DEFAULT_PORT;
360 /* default option values */
361 options.reconnect = TRUE;
362 options.find_wrap = TRUE;
363 options.wide_cursor = TRUE;
364 options.audible_bell = TRUE;
365 options.crossfade_time = DEFAULT_CROSSFADE_TIME;
367 return &options;
368 }