1b745bb76467854c7ebc488d13797d13b228d9ad
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 #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 puts(PACKAGE " version: " VERSION "\n"
146 "build options:"
147 #ifndef NDEBUG
148 " debug"
149 #endif
150 #ifdef ENABLE_NLS
151 " nls"
152 #endif
153 #ifdef ENABLE_COLORS
154 " colors"
155 #else
156 " no-colors"
157 #endif
158 #ifdef HAVE_GETMOUSE
159 " getmouse"
160 #endif
161 #ifdef ENABLE_ARTIST_SCREEN
162 " artist-screen"
163 #endif
164 #ifdef ENABLE_SEARCH_SCREEN
165 " search-screen"
166 #endif
167 #ifdef ENABLE_KEYDEF_SCREEN
168 " key-screen"
169 #endif
170 "\n");
171 exit(EXIT_SUCCESS);
172 case 'c': /* --colors */
173 #ifdef ENABLE_COLORS
174 options.enable_colors = true;
175 #endif
176 break;
177 case 'C': /* --no-colors */
178 #ifdef ENABLE_COLORS
179 options.enable_colors = false;
180 #endif
181 break;
182 case 'm': /* --mouse */
183 options.enable_mouse = true;
184 break;
185 case 'M': /* --no-mouse */
186 options.enable_mouse = false;
187 break;
188 case 'e': /* --exit */
189 /* deprecated */
190 break;
191 case 'p': /* --port */
192 options.port = atoi(arg);
193 break;
194 case 'h': /* --host */
195 if( options.host )
196 g_free(options.host);
197 options.host = g_strdup(arg);
198 break;
199 case 'P': /* --password */
200 if( options.password )
201 g_free(options.password);
202 options.password = locale_to_utf8(arg);
203 break;
204 case 'f': /* --config */
205 if( options.config_file )
206 g_free(options.config_file);
207 options.config_file = g_strdup(arg);
208 break;
209 case 'k': /* --key-file */
210 if( options.key_file )
211 g_free(options.key_file);
212 options.key_file = g_strdup(arg);
213 break;
214 case 'S': /* --key-file */
215 /* the splash screen was removed */
216 break;
217 #ifndef NDEBUG
218 case 'K': /* --dump-keys */
219 read_configuration();
220 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
221 exit(EXIT_SUCCESS);
222 break;
223 #endif
224 default:
225 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
226 break;
227 }
228 }
230 void
231 options_parse(int argc, const char *argv[])
232 {
233 int i;
234 const arg_opt_t *opt = NULL;
235 option_callback_fn_t option_cb = handle_option;
237 i=1;
238 while (i < argc) {
239 const char *arg = argv[i];
240 size_t len = strlen(arg);
242 /* check for a long option */
243 if (g_str_has_prefix(arg, "--")) {
244 char *name, *value;
246 /* make shure we got an argument for the previous option */
247 if( opt && opt->argument )
248 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
250 /* retreive a option argument */
251 if ((value=g_strrstr(arg+2, "="))) {
252 *value = '\0';
253 name = g_strdup(arg);
254 *value = '=';
255 value++;
256 } else
257 name = g_strdup(arg);
259 /* check if the option exists */
260 if( (opt=lookup_option(0, name+2)) == NULL )
261 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
262 g_free(name);
264 /* abort if we got an argument to the option and dont want one */
265 if( value && opt->argument==NULL )
266 option_error(ERROR_GOT_ARGUMENT, arg, value);
268 /* execute option callback */
269 if (value || opt->argument==NULL) {
270 option_cb (opt->shortopt, value);
271 opt = NULL;
272 }
273 }
274 /* check for short options */
275 else if (len>=2 && g_str_has_prefix(arg, "-")) {
276 size_t j;
278 for(j=1; j<len; j++) {
279 /* make shure we got an argument for the previous option */
280 if (opt && opt->argument)
281 option_error(ERROR_MISSING_ARGUMENT,
282 opt->longopt, opt->argument);
284 /* check if the option exists */
285 if ((opt=lookup_option(arg[j], NULL)) == NULL)
286 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
288 /* if no option argument is needed execute callback */
289 if (opt->argument == NULL) {
290 option_cb (opt->shortopt, NULL);
291 opt = NULL;
292 }
293 }
294 } else {
295 /* is this a option argument? */
296 if (opt && opt->argument) {
297 option_cb (opt->shortopt, arg);
298 opt = NULL;
299 } else
300 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
301 }
302 i++;
303 }
305 if (opt && opt->argument == NULL)
306 option_cb (opt->shortopt, NULL);
307 else if (opt && opt->argument)
308 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
309 }
311 void
312 options_init(void)
313 {
314 const char *value;
315 char *tmp;
317 /* get initial values for host and password from MPD_HOST (enviroment) */
318 if ((value = g_getenv(MPD_HOST_ENV)))
319 options.host = g_strdup(value);
320 else
321 options.host = g_strdup(DEFAULT_HOST);
323 if ((tmp = g_strstr_len(options.host, strlen(options.host), "@"))) {
324 char *oldhost = options.host;
325 *tmp = '\0';
326 options.password = locale_to_utf8(oldhost);
327 options.host = g_strdup(tmp+1);
328 g_free(oldhost);
329 }
331 /* get initial values for port from MPD_PORT (enviroment) */
332 if ((value = g_getenv(MPD_PORT_ENV)))
333 options.port = atoi(value);
334 else
335 options.port = DEFAULT_PORT;
337 /* default option values */
338 options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
339 options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
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);
351 }