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 "\n");
185 exit(EXIT_SUCCESS);
186 case 'c': /* --colors */
187 #ifdef ENABLE_COLORS
188 options.enable_colors = true;
189 #endif
190 break;
191 case 'C': /* --no-colors */
192 #ifdef ENABLE_COLORS
193 options.enable_colors = false;
194 #endif
195 break;
196 case 'm': /* --mouse */
197 #ifdef HAVE_GETMOUSE
198 options.enable_mouse = true;
199 #endif
200 break;
201 case 'M': /* --no-mouse */
202 #ifdef HAVE_GETMOUSE
203 options.enable_mouse = false;
204 #endif
205 break;
206 case 'e': /* --exit */
207 /* deprecated */
208 break;
209 case 'p': /* --port */
210 options.port = atoi(arg);
211 break;
212 case 'h': /* --host */
213 if( options.host )
214 g_free(options.host);
215 options.host = g_strdup(arg);
216 break;
217 case 'P': /* --password */
218 if( options.password )
219 g_free(options.password);
220 options.password = locale_to_utf8(arg);
221 break;
222 case 'f': /* --config */
223 if( options.config_file )
224 g_free(options.config_file);
225 options.config_file = g_strdup(arg);
226 break;
227 case 'k': /* --key-file */
228 if( options.key_file )
229 g_free(options.key_file);
230 options.key_file = g_strdup(arg);
231 break;
232 case 'S': /* --key-file */
233 /* the splash screen was removed */
234 break;
235 #ifndef NDEBUG
236 case 'K': /* --dump-keys */
237 read_configuration();
238 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
239 exit(EXIT_SUCCESS);
240 break;
241 #endif
242 default:
243 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
244 break;
245 }
246 }
248 void
249 options_parse(int argc, const char *argv[])
250 {
251 int i;
252 const arg_opt_t *opt = NULL;
253 option_callback_fn_t option_cb = handle_option;
255 i=1;
256 while (i < argc) {
257 const char *arg = argv[i];
258 size_t len = strlen(arg);
260 /* check for a long option */
261 if (g_str_has_prefix(arg, "--")) {
262 char *name, *value;
264 /* make shure we got an argument for the previous option */
265 if( opt && opt->argument )
266 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
268 /* retreive a option argument */
269 if ((value=g_strrstr(arg+2, "="))) {
270 *value = '\0';
271 name = g_strdup(arg);
272 *value = '=';
273 value++;
274 } else
275 name = g_strdup(arg);
277 /* check if the option exists */
278 if( (opt=lookup_option(0, name+2)) == NULL )
279 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
280 g_free(name);
282 /* abort if we got an argument to the option and dont want one */
283 if( value && opt->argument==NULL )
284 option_error(ERROR_GOT_ARGUMENT, arg, value);
286 /* execute option callback */
287 if (value || opt->argument==NULL) {
288 option_cb (opt->shortopt, value);
289 opt = NULL;
290 }
291 }
292 /* check for short options */
293 else if (len>=2 && g_str_has_prefix(arg, "-")) {
294 size_t j;
296 for(j=1; j<len; j++) {
297 /* make shure we got an argument for the previous option */
298 if (opt && opt->argument)
299 option_error(ERROR_MISSING_ARGUMENT,
300 opt->longopt, opt->argument);
302 /* check if the option exists */
303 if ((opt=lookup_option(arg[j], NULL)) == NULL)
304 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
306 /* if no option argument is needed execute callback */
307 if (opt->argument == NULL) {
308 option_cb (opt->shortopt, NULL);
309 opt = NULL;
310 }
311 }
312 } else {
313 /* is this a option argument? */
314 if (opt && opt->argument) {
315 option_cb (opt->shortopt, arg);
316 opt = NULL;
317 } else
318 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
319 }
320 i++;
321 }
323 if (opt && opt->argument == NULL)
324 option_cb (opt->shortopt, NULL);
325 else if (opt && opt->argument)
326 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
327 }
329 void
330 options_init(void)
331 {
332 const char *value;
333 char *tmp;
335 /* get initial values for host and password from MPD_HOST (enviroment) */
336 if ((value = g_getenv(MPD_HOST_ENV)))
337 options.host = g_strdup(value);
338 else
339 options.host = g_strdup(DEFAULT_HOST);
341 if ((tmp = g_strstr_len(options.host, strlen(options.host), "@"))) {
342 char *oldhost = options.host;
343 *tmp = '\0';
344 options.password = locale_to_utf8(oldhost);
345 options.host = g_strdup(tmp+1);
346 g_free(oldhost);
347 }
349 /* get initial values for port from MPD_PORT (enviroment) */
350 if ((value = g_getenv(MPD_PORT_ENV)))
351 options.port = atoi(value);
353 /* default option values */
354 options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
355 options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
356 options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
357 options.timedisplay_type = g_strdup(DEFAULT_TIMEDISPLAY_TYPE);
358 #ifndef NCMPC_MINI
359 options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
360 #endif
361 }