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