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 "support.h"
23 #include "command.h"
24 #include "conf.h"
26 #include <stdlib.h>
27 #include <string.h>
29 #define MAX_LONGOPT_LENGTH 32
31 #define ERROR_UNKNOWN_OPTION 0x01
32 #define ERROR_BAD_ARGUMENT 0x02
33 #define ERROR_GOT_ARGUMENT 0x03
34 #define ERROR_MISSING_ARGUMENT 0x04
36 typedef struct {
37 int shortopt;
38 const char *longopt;
39 const char *argument;
40 const char *descrition;
41 } arg_opt_t;
44 typedef void (*option_callback_fn_t)(int c, const char *arg);
47 options_t options;
49 static const arg_opt_t option_table[] = {
50 { '?', "help", NULL, "Show this help message" },
51 { 'V', "version", NULL, "Display version information" },
52 { 'c', "colors", NULL, "Enable colors" },
53 { 'C', "no-colors", NULL, "Disable colors" },
54 #ifdef HAVE_GETMOUSE
55 { 'm', "mouse", NULL, "Enable mouse" },
56 { 'M', "no-mouse", NULL, "Disable mouse" },
57 #endif
58 { 'e', "exit", NULL, "Exit on connection errors" },
59 { 'p', "port", "PORT", "Connect to server on port [" DEFAULT_PORT_STR "]" },
60 { 'h', "host", "HOST", "Connect to server on host [" DEFAULT_HOST "]" },
61 { 'P', "password","PASSWORD", "Connect with password" },
62 { 'f', "config", "FILE", "Read configuration from file" },
63 { 'k', "key-file","FILE", "Read configuration from file" },
64 { 'S', "no-splash", NULL, "Don't show the splash screen" },
65 #ifndef NDEBUG
66 { 'K', "dump-keys", NULL, "Dump key bindings to stdout" },
67 { 'D', "debug", NULL, "Enable debug output on stderr" },
68 #endif
69 };
71 static const unsigned option_table_size = sizeof(option_table) / sizeof(option_table[0]);
73 static const arg_opt_t *
74 lookup_option(int s, char *l)
75 {
76 unsigned i;
78 for (i = 0; i < option_table_size; ++i) {
79 if (l && strcmp(l, option_table[i].longopt) == 0)
80 return &option_table[i];;
81 if (s && s == option_table[i].shortopt)
82 return &option_table[i];;
83 }
85 return NULL;
86 }
88 static void
89 option_error(int error, const char *option, const char *arg)
90 {
91 switch (error) {
92 case ERROR_UNKNOWN_OPTION:
93 fprintf(stderr, PACKAGE ": invalid option %s\n", option);
94 break;
95 case ERROR_BAD_ARGUMENT:
96 fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
97 break;
98 case ERROR_GOT_ARGUMENT:
99 fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
100 break;
101 case ERROR_MISSING_ARGUMENT:
102 fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
103 break;
104 default:
105 fprintf(stderr, PACKAGE ": internal error %d\n", error);
106 break;
107 }
109 exit(EXIT_FAILURE);
110 }
112 static void
113 display_help(void)
114 {
115 unsigned i;
117 printf("Usage: %s [OPTION]...\n", PACKAGE);
119 for (i = 0; i < option_table_size; ++i) {
120 char tmp[MAX_LONGOPT_LENGTH];
122 if (option_table[i].argument)
123 g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s",
124 option_table[i].longopt,
125 option_table[i].argument);
126 else
127 g_strlcpy(tmp, option_table[i].longopt, 64);
129 printf(" -%c, --%-20s %s\n",
130 option_table[i].shortopt,
131 tmp,
132 option_table[i].descrition);
133 i++;
134 }
135 }
137 static void
138 handle_option(int c, const char *arg)
139 {
140 switch (c) {
141 case '?': /* --help */
142 display_help();
143 exit(EXIT_SUCCESS);
144 case 'V': /* --version */
145 printf("%s version: %s\n", PACKAGE, VERSION);
146 printf("build options:");
147 #ifndef NDEBUG
148 printf(" debug");
149 #endif
150 #ifdef ENABLE_NLS
151 printf(" nls");
152 #endif
153 #ifdef HAVE_GETMOUSE
154 printf(" getmouse");
155 #endif
156 #ifdef ENABLE_ARTIST_SCREEN
157 printf(" artist-screen");
158 #endif
159 #ifdef ENABLE_SEARCH_SCREEN
160 printf(" search-screen");
161 #endif
162 #ifdef ENABLE_KEYDEF_SCREEN
163 printf(" key-screen");
164 #endif
165 printf("\n");
166 exit(EXIT_SUCCESS);
167 case 'c': /* --colors */
168 options.enable_colors = TRUE;
169 break;
170 case 'C': /* --no-colors */
171 options.enable_colors = FALSE;
172 break;
173 case 'm': /* --mouse */
174 options.enable_mouse = TRUE;
175 break;
176 case 'M': /* --no-mouse */
177 options.enable_mouse = FALSE;
178 break;
179 case 'e': /* --exit */
180 options.reconnect = FALSE;
181 break;
182 case 'p': /* --port */
183 options.port = atoi(arg);
184 break;
185 case 'h': /* --host */
186 if( options.host )
187 g_free(options.host);
188 options.host = g_strdup(arg);
189 break;
190 case 'P': /* --password */
191 if( options.password )
192 g_free(options.password);
193 options.password = locale_to_utf8(arg);
194 break;
195 case 'f': /* --config */
196 if( options.config_file )
197 g_free(options.config_file);
198 options.config_file = g_strdup(arg);
199 break;
200 case 'k': /* --key-file */
201 if( options.key_file )
202 g_free(options.key_file);
203 options.key_file = g_strdup(arg);
204 break;
205 case 'S': /* --key-file */
206 /* the splash screen was removed */
207 break;
208 #ifndef NDEBUG
209 case 'K': /* --dump-keys */
210 read_configuration(&options);
211 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
212 exit(EXIT_SUCCESS);
213 break;
214 case 'D': /* --debug */
215 options.debug = TRUE;
216 break;
217 #endif
218 default:
219 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
220 break;
221 }
222 }
224 options_t *
225 options_parse(int argc, const char *argv[])
226 {
227 int i;
228 const arg_opt_t *opt = NULL;
229 option_callback_fn_t option_cb = handle_option;
231 i=1;
232 while (i < argc) {
233 const char *arg = argv[i];
234 size_t len = strlen(arg);
236 /* check for a long option */
237 if (g_str_has_prefix(arg, "--")) {
238 char *name, *value;
240 /* make shure we got an argument for the previous option */
241 if( opt && opt->argument )
242 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
244 /* retreive a option argument */
245 if ((value=g_strrstr(arg+2, "="))) {
246 *value = '\0';
247 name = g_strdup(arg);
248 *value = '=';
249 value++;
250 } else
251 name = g_strdup(arg);
253 /* check if the option exists */
254 if( (opt=lookup_option(0, name+2)) == NULL )
255 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
256 g_free(name);
258 /* abort if we got an argument to the option and dont want one */
259 if( value && opt->argument==NULL )
260 option_error(ERROR_GOT_ARGUMENT, arg, value);
262 /* execute option callback */
263 if (value || opt->argument==NULL) {
264 option_cb (opt->shortopt, value);
265 opt = NULL;
266 }
267 }
268 /* check for short options */
269 else if (len>=2 && g_str_has_prefix(arg, "-")) {
270 size_t j;
272 for(j=1; j<len; j++) {
273 /* make shure we got an argument for the previous option */
274 if (opt && opt->argument)
275 option_error(ERROR_MISSING_ARGUMENT,
276 opt->longopt, opt->argument);
278 /* check if the option exists */
279 if ((opt=lookup_option(arg[j], NULL)) == NULL)
280 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
282 /* if no option argument is needed execute callback */
283 if (opt->argument == NULL) {
284 option_cb (opt->shortopt, NULL);
285 opt = NULL;
286 }
287 }
288 } else {
289 /* is this a option argument? */
290 if (opt && opt->argument) {
291 option_cb (opt->shortopt, arg);
292 opt = NULL;
293 } else
294 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
295 }
296 i++;
297 }
299 if (opt && opt->argument == NULL)
300 option_cb (opt->shortopt, NULL);
301 else if (opt && opt->argument)
302 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
304 return &options;
305 }
307 options_t *
308 options_init( void )
309 {
310 const char *value;
311 char *tmp;
313 memset(&options, 0, sizeof(options_t));
315 /* get initial values for host and password from MPD_HOST (enviroment) */
316 if ((value = g_getenv(MPD_HOST_ENV)))
317 options.host = g_strdup(value);
318 else
319 options.host = g_strdup(DEFAULT_HOST);
321 if ((tmp = g_strstr_len(options.host, strlen(options.host), "@"))) {
322 char *oldhost = options.host;
323 *tmp = '\0';
324 options.password = locale_to_utf8(oldhost);
325 options.host = g_strdup(tmp+1);
326 g_free(oldhost);
327 }
329 /* get initial values for port from MPD_PORT (enviroment) */
330 if ((value = g_getenv(MPD_PORT_ENV)))
331 options.port = atoi(value);
332 else
333 options.port = DEFAULT_PORT;
335 /* default option values */
336 options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
337 options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
338 options.reconnect = TRUE;
339 options.find_wrap = TRUE;
340 options.wide_cursor = TRUE;
341 options.welcome_screen_list = TRUE;
342 options.audible_bell = TRUE;
343 options.crossfade_time = DEFAULT_CROSSFADE_TIME;
344 options.seek_time = 1;
345 options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
346 options.timedisplay_type = g_strdup(DEFAULT_TIMEDISPLAY_TYPE);
347 options.lyrics_timeout = DEFAULT_LYRICS_TIMEOUT;
348 options.scroll = DEFAULT_SCROLL;
349 options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
351 return &options;
352 }