53f54b7ebfa099d6edc867ae7180aafdec95bdcd
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 ENABLE_COLORS
155 " colors"
156 #else
157 " no-colors"
158 #endif
159 #ifdef HAVE_GETMOUSE
160 " getmouse"
161 #endif
162 #ifdef ENABLE_ARTIST_SCREEN
163 " artist-screen"
164 #endif
165 #ifdef ENABLE_SEARCH_SCREEN
166 " search-screen"
167 #endif
168 #ifdef ENABLE_KEYDEF_SCREEN
169 " key-screen"
170 #endif
171 "\n");
172 exit(EXIT_SUCCESS);
173 case 'c': /* --colors */
174 #ifdef ENABLE_COLORS
175 options.enable_colors = true;
176 #endif
177 break;
178 case 'C': /* --no-colors */
179 #ifdef ENABLE_COLORS
180 options.enable_colors = false;
181 #endif
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 case 'S': /* --key-file */
216 /* the splash screen was removed */
217 break;
218 #ifndef NDEBUG
219 case 'K': /* --dump-keys */
220 read_configuration();
221 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
222 exit(EXIT_SUCCESS);
223 break;
224 case 'D': /* --debug */
225 options.debug = true;
226 break;
227 #endif
228 default:
229 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
230 break;
231 }
232 }
234 void
235 options_parse(int argc, const char *argv[])
236 {
237 int i;
238 const arg_opt_t *opt = NULL;
239 option_callback_fn_t option_cb = handle_option;
241 i=1;
242 while (i < argc) {
243 const char *arg = argv[i];
244 size_t len = strlen(arg);
246 /* check for a long option */
247 if (g_str_has_prefix(arg, "--")) {
248 char *name, *value;
250 /* make shure we got an argument for the previous option */
251 if( opt && opt->argument )
252 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
254 /* retreive a option argument */
255 if ((value=g_strrstr(arg+2, "="))) {
256 *value = '\0';
257 name = g_strdup(arg);
258 *value = '=';
259 value++;
260 } else
261 name = g_strdup(arg);
263 /* check if the option exists */
264 if( (opt=lookup_option(0, name+2)) == NULL )
265 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
266 g_free(name);
268 /* abort if we got an argument to the option and dont want one */
269 if( value && opt->argument==NULL )
270 option_error(ERROR_GOT_ARGUMENT, arg, value);
272 /* execute option callback */
273 if (value || opt->argument==NULL) {
274 option_cb (opt->shortopt, value);
275 opt = NULL;
276 }
277 }
278 /* check for short options */
279 else if (len>=2 && g_str_has_prefix(arg, "-")) {
280 size_t j;
282 for(j=1; j<len; j++) {
283 /* make shure we got an argument for the previous option */
284 if (opt && opt->argument)
285 option_error(ERROR_MISSING_ARGUMENT,
286 opt->longopt, opt->argument);
288 /* check if the option exists */
289 if ((opt=lookup_option(arg[j], NULL)) == NULL)
290 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
292 /* if no option argument is needed execute callback */
293 if (opt->argument == NULL) {
294 option_cb (opt->shortopt, NULL);
295 opt = NULL;
296 }
297 }
298 } else {
299 /* is this a option argument? */
300 if (opt && opt->argument) {
301 option_cb (opt->shortopt, arg);
302 opt = NULL;
303 } else
304 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
305 }
306 i++;
307 }
309 if (opt && opt->argument == NULL)
310 option_cb (opt->shortopt, NULL);
311 else if (opt && opt->argument)
312 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
313 }
315 void
316 options_init(void)
317 {
318 const char *value;
319 char *tmp;
321 memset(&options, 0, sizeof(options_t));
323 /* get initial values for host and password from MPD_HOST (enviroment) */
324 if ((value = g_getenv(MPD_HOST_ENV)))
325 options.host = g_strdup(value);
326 else
327 options.host = g_strdup(DEFAULT_HOST);
329 if ((tmp = g_strstr_len(options.host, strlen(options.host), "@"))) {
330 char *oldhost = options.host;
331 *tmp = '\0';
332 options.password = locale_to_utf8(oldhost);
333 options.host = g_strdup(tmp+1);
334 g_free(oldhost);
335 }
337 /* get initial values for port from MPD_PORT (enviroment) */
338 if ((value = g_getenv(MPD_PORT_ENV)))
339 options.port = atoi(value);
340 else
341 options.port = DEFAULT_PORT;
343 /* default option values */
344 options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
345 options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
346 options.reconnect = true;
347 options.find_wrap = true;
348 options.wide_cursor = true;
349 options.welcome_screen_list = 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);
358 }