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