Code

removed the clock screen
[ncmpc.git] / src / options.c
1 /* 
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include "options.h"
22 #include "config.h"
23 #include "ncmpc.h"
24 #include "support.h"
25 #include "command.h"
26 #include "conf.h"
28 #include <stdlib.h>
29 #include <string.h>
31 #define MAX_LONGOPT_LENGTH 32
33 #define ERROR_UNKNOWN_OPTION    0x01
34 #define ERROR_BAD_ARGUMENT      0x02
35 #define ERROR_GOT_ARGUMENT      0x03
36 #define ERROR_MISSING_ARGUMENT  0x04
38 typedef struct {
39         int shortopt;
40         const char *longopt;
41         const char *argument;
42         const char *descrition;
43 } arg_opt_t;
46 typedef void (*option_callback_fn_t)(int c, const char *arg);
49 options_t options;
51 static arg_opt_t option_table[] = {
52   { '?', "help",     NULL,   "Show this help message" },
53   { 'V', "version",  NULL,   "Display version information" },
54   { 'c', "colors",   NULL,   "Enable colors" },
55   { 'C', "no-colors", NULL,  "Disable colors" },
56 #ifdef HAVE_GETMOUSE
57   { 'm', "mouse",    NULL,   "Enable mouse" },
58   { 'M', "no-mouse", NULL,   "Disable mouse" },
59 #endif
60   { 'e', "exit",     NULL,   "Exit on connection errors" },
61   { 'p', "port",  "PORT", "Connect to server on port [" DEFAULT_PORT_STR "]" },
62   { 'h', "host",  "HOST", "Connect to server on host [" DEFAULT_HOST "]" },
63   { 'P', "password","PASSWORD", "Connect with password" },
64   { 'f', "config",  "FILE",     "Read configuration from file" },
65   { 'k', "key-file","FILE",     "Read configuration from file" },
66   { 'S', "no-splash", NULL, "Don't show the splash screen" },
67 #ifndef NDEBUG
68   { 'K', "dump-keys", NULL,     "Dump key bindings to stdout" },
69   { 'D', "debug",   NULL,   "Enable debug output on stderr" },
70 #endif
71   { 0, NULL, NULL, NULL },
72 };
74 static arg_opt_t *
75 lookup_option(int s, char *l)
76 {
77   int i;
79   i=0;
80   while( option_table[i].descrition )
81     {
82       if( l && strcmp(l, option_table[i].longopt) == 0 )
83         return &option_table[i];;
84       if( s && s==option_table[i].shortopt )
85         return &option_table[i];;
86       i++;
87     }
88   return NULL;
89 }
91 static void
92 option_error(int error, const char *option, const char *arg)
93 {
94   switch(error)
95     {
96     case ERROR_UNKNOWN_OPTION:
97       fprintf(stderr, PACKAGE ": invalid option %s\n", option);
98       break;
99     case ERROR_BAD_ARGUMENT:
100       fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
101       break;
102     case ERROR_GOT_ARGUMENT:
103       fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
104       break;
105     case ERROR_MISSING_ARGUMENT:
106       fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
107       break;
108     default:
109       fprintf(stderr, PACKAGE ": internal error %d\n", error);
110       break;
111     }
112   exit(EXIT_FAILURE);
115 static void 
116 display_help(void)
118   int i = 0;
120   printf("Usage: %s [OPTION]...\n", PACKAGE);
121   while( option_table[i].descrition )
122     {
123       char tmp[MAX_LONGOPT_LENGTH];
125       if( option_table[i].argument )
126         g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s", 
127                    option_table[i].longopt, 
128                    option_table[i].argument);
129       else
130         g_strlcpy(tmp, option_table[i].longopt, 64);
132       printf("  -%c, --%-20s %s\n", 
133              option_table[i].shortopt, 
134              tmp,
135              option_table[i].descrition);
136       i++;
137     }
140 static void 
141 handle_option(int c, const char *arg)
143   D("option callback -%c %s\n", c, arg);
144   switch(c)
145     {
146     case '?': /* --help */
147       display_help();
148       exit(EXIT_SUCCESS);
149     case 'V': /* --version */
150       printf("%s version: %s\n", PACKAGE, VERSION);
151       printf("build options:");
152 #ifndef NDEBUG
153       printf(" debug");
154 #endif
155 #ifdef ENABLE_NLS
156       printf(" nls");
157 #endif
158 #ifdef HAVE_GETMOUSE
159       printf(" getmouse");
160 #endif
161 #ifdef ENABLE_ARTIST_SCREEN
162       printf(" artist-screen");
163 #endif
164 #ifdef ENABLE_SEARCH_SCREEN
165       printf(" search-screen");
166 #endif
167 #ifdef ENABLE_KEYDEF_SCREEN
168       printf(" key-screen");
169 #endif
170       printf("\n");
171       exit(EXIT_SUCCESS);
172     case 'c': /* --colors */
173       options.enable_colors = TRUE;
174       break;
175     case 'C': /* --no-colors */
176       options.enable_colors = FALSE;
177       break;
178     case 'm': /* --mouse */
179      options.enable_mouse = TRUE;
180       break;
181     case 'M': /* --no-mouse */
182       options.enable_mouse = FALSE;
183       break;
184     case 'e': /* --exit */
185       options.reconnect = FALSE;
186       break;
187     case 'p': /* --port */
188       options.port = atoi(arg);
189       break;
190     case 'h': /* --host */
191       if( options.host )
192         g_free(options.host);
193       options.host = g_strdup(arg);
194       break;
195     case 'P': /* --password */
196       if( options.password )
197         g_free(options.password);
198       options.password = locale_to_utf8(arg);
199       break;
200     case 'f': /* --config */
201       if( options.config_file )
202         g_free(options.config_file);
203       options.config_file = g_strdup(arg);
204       break;
205     case 'k': /* --key-file */
206       if( options.key_file )
207         g_free(options.key_file);
208       options.key_file = g_strdup(arg);
209       break;
210     case 'S': /* --key-file */
211       /* the splash screen was removed */
212       break;
213 #ifndef NDEBUG
214     case 'K': /* --dump-keys */
215       read_configuration(&options);
216       write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
217       exit(EXIT_SUCCESS);
218       break;
219     case 'D': /* --debug */
220       options.debug = TRUE;
221       break;
222 #endif
223     default:
224       fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
225       break;
226     }
229 options_t *
230 options_parse(int argc, const char *argv[])
232         int i;
233         arg_opt_t *opt = NULL;
234         option_callback_fn_t option_cb = handle_option;
236         i=1;
237         while (i < argc) {
238                 const char *arg = argv[i];
239                 size_t len = strlen(arg);
241                 /* check for a long option */
242                 if (g_str_has_prefix(arg, "--")) {
243                         char *name, *value;
245                         /* make shure we got an argument for the previous option */
246                         if( opt && opt->argument )
247                                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
249                         /* retreive a option argument */
250                         if ((value=g_strrstr(arg+2, "="))) {
251                                 *value = '\0';
252                                 name = g_strdup(arg);
253                                 *value = '=';
254                                 value++;
255                         } else
256                                 name = g_strdup(arg);
258                         /* check if the option exists */
259                         if( (opt=lookup_option(0, name+2)) == NULL )
260                                 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
261                         g_free(name);
263                         /* abort if we got an argument to the option and dont want one */
264                         if( value && opt->argument==NULL )
265                                 option_error(ERROR_GOT_ARGUMENT, arg, value);
267                         /* execute option callback */
268                         if (value || opt->argument==NULL) {
269                                 option_cb (opt->shortopt, value);
270                                 opt = NULL;
271                         }
272                 }
273                 /* check for short options */
274                 else if (len>=2 && g_str_has_prefix(arg, "-")) {
275                         size_t j;
277                         for(j=1; j<len; j++) {
278                                 /* make shure we got an argument for the previous option */
279                                 if( opt && opt->argument )
280                                         option_error(ERROR_MISSING_ARGUMENT,
281                                                      opt->longopt, opt->argument);
283                                 /* check if the option exists */
284                                 if( (opt=lookup_option(arg[j], NULL))==NULL )
285                                         option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
287                                 /* if no option argument is needed execute callback */
288                                 if( opt->argument==NULL ) {
289                                         option_cb (opt->shortopt, NULL);
290                                         opt = NULL;
291                                 }
292                         }
293                 } else {
294                         /* is this a option argument? */
295                         if( opt && opt->argument) {
296                                 option_cb (opt->shortopt, arg);
297                                 opt = NULL;
298                         } else
299                                 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
300                 }
301                 i++;
302         }
304         if( opt && opt->argument==NULL)
305                 option_cb (opt->shortopt, NULL);
306         else if( opt && opt->argument )
307                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
309         return  &options;
312 options_t *
313 options_init( void )
315         const char *value;
316         char *tmp;
318         memset(&options, 0, sizeof(options_t));
320         /* get initial values for host and password from MPD_HOST (enviroment) */
321         if ((value=g_getenv(MPD_HOST_ENV)))
322                 options.host = g_strdup(value);
323         else
324                 options.host = g_strdup(DEFAULT_HOST);
326         if ((tmp=g_strstr_len(options.host, strlen(options.host), "@"))) {
327                 char *oldhost = options.host;
328                 *tmp  = '\0';
329                 options.password = locale_to_utf8(oldhost);
330                 options.host = g_strdup(tmp+1);
331                 g_free(oldhost);
332         }
334         /* get initial values for port from MPD_PORT (enviroment) */
335         if ((value=g_getenv(MPD_PORT_ENV)))
336                 options.port = atoi(value);
337         else
338                 options.port = DEFAULT_PORT;
340         /* default option values */
341         options.reconnect = TRUE;
342         options.find_wrap = TRUE;
343         options.wide_cursor = TRUE;
344         options.audible_bell = TRUE;
345         options.crossfade_time = DEFAULT_CROSSFADE_TIME;
346         options.seek_time = 1;
347         options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
348         options.timedisplay_type = g_strdup(DEFAULT_TIMEDISPLAY_TYPE);
349         options.lyrics_timeout = DEFAULT_LYRICS_TIMEOUT;
350         options.scroll = DEFAULT_SCROLL;
351         options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
353         return &options;