Code

splash: removed the useless splash 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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ncurses.h>
25 #include <glib.h>
27 #include "config.h"
28 #include "ncmpc.h"
29 #include "support.h"
30 #include "options.h"
31 #include "command.h"
32 #include "conf.h"
34 #define MAX_LONGOPT_LENGTH 32
36 #define ERROR_UNKNOWN_OPTION    0x01
37 #define ERROR_BAD_ARGUMENT      0x02
38 #define ERROR_GOT_ARGUMENT      0x03
39 #define ERROR_MISSING_ARGUMENT  0x04
41  
43 typedef struct
44 {
45   int  shortopt;
46   char *longopt;
47   char *argument;
48   char *descrition;
49 } arg_opt_t;
52 typedef void (*option_callback_fn_t)   (int c, char *arg);
55 options_t options;
56  
57 static arg_opt_t option_table[] = {
58   { '?', "help",     NULL,   "Show this help message" },
59   { 'V', "version",  NULL,   "Display version information" },
60   { 'c', "colors",   NULL,   "Enable colors" },
61   { 'C', "no-colors", NULL,  "Disable colors" },
62 #ifdef HAVE_GETMOUSE
63   { 'm', "mouse",    NULL,   "Enable mouse" },
64   { 'M', "no-mouse", NULL,   "Disable mouse" },
65 #endif
66   { 'e', "exit",     NULL,   "Exit on connection errors" },
67   { 'p', "port",  "PORT", "Connect to server on port [" DEFAULT_PORT_STR "]" },
68   { 'h', "host",  "HOST", "Connect to server on host [" DEFAULT_HOST "]" },
69   { 'P', "password","PASSWORD", "Connect with password" },
70   { 'f', "config",  "FILE",     "Read configuration from file" },
71   { 'k', "key-file","FILE",     "Read configuration from file" },
72   { 'S', "no-splash", NULL, "Don't show the splash screen" },
73 #ifdef DEBUG
74   { 'K', "dump-keys", NULL,     "Dump key bindings to stdout" },
75   { 'D', "debug",   NULL,   "Enable debug output on stderr" },
76 #endif
77   { 0, NULL, NULL, NULL },
78 };
80 static arg_opt_t *
81 lookup_option(int s, char *l)
82 {
83   int i;
85   i=0;
86   while( option_table[i].descrition )
87     {
88       if( l && strcmp(l, option_table[i].longopt) == 0 )
89         return &option_table[i];;
90       if( s && s==option_table[i].shortopt )
91         return &option_table[i];;
92       i++;
93     }
94   return NULL;
95 }
97 static void
98 option_error(int error, char *option, char *arg)
99 {
100   switch(error)
101     {
102     case ERROR_UNKNOWN_OPTION:
103       fprintf(stderr, PACKAGE ": invalid option %s\n", option);
104       break;
105     case ERROR_BAD_ARGUMENT:
106       fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
107       break;
108     case ERROR_GOT_ARGUMENT:
109       fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
110       break;
111     case ERROR_MISSING_ARGUMENT:
112       fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
113       break;
114     default:
115       fprintf(stderr, PACKAGE ": internal error %d\n", error);
116       break;
117     }
118   exit(EXIT_FAILURE);
121 static void 
122 display_help(void)
124   int i = 0;
126   printf("Usage: %s [OPTION]...\n", PACKAGE);
127   while( option_table[i].descrition )
128     {
129       char tmp[MAX_LONGOPT_LENGTH];
131       if( option_table[i].argument )
132         g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s", 
133                    option_table[i].longopt, 
134                    option_table[i].argument);
135       else
136         g_strlcpy(tmp, option_table[i].longopt, 64);
138       printf("  -%c, --%-20s %s\n", 
139              option_table[i].shortopt, 
140              tmp,
141              option_table[i].descrition);
142       i++;
143     }
146 static void 
147 handle_option(int c, char *arg)
149   D("option callback -%c %s\n", c, arg);
150   switch(c)
151     {
152     case '?': /* --help */
153       display_help();
154       exit(EXIT_SUCCESS);
155     case 'V': /* --version */
156       printf("%s version: %s\n", PACKAGE, VERSION);
157       printf("build options:");
158 #ifdef DEBUG
159       printf(" debug");
160 #endif
161 #ifdef ENABLE_NLS
162       printf(" nls");
163 #endif
164 #ifdef HAVE_GETMOUSE
165       printf(" getmouse");
166 #endif
167 #ifdef ENABLE_ARTIST_SCREEN
168       printf(" artist-screen");
169 #endif
170 #ifdef ENABLE_SEARCH_SCREEN
171       printf(" search-screen");
172 #endif
173 #ifdef ENABLE_KEYDEF_SCREEN
174       printf(" key-screen");
175 #endif
176 #ifdef ENABLE_CLOCK_SCREEN
177       printf(" clock-screen");
178 #endif
179       printf("\n");
180       exit(EXIT_SUCCESS);
181     case 'c': /* --colors */
182       options.enable_colors = TRUE;
183       break;
184     case 'C': /* --no-colors */
185       options.enable_colors = FALSE;
186       break;
187     case 'm': /* --mouse */
188      options.enable_mouse = TRUE;
189       break;
190     case 'M': /* --no-mouse */
191       options.enable_mouse = FALSE;
192       break;
193     case 'e': /* --exit */
194       options.reconnect = FALSE;
195       break;
196     case 'p': /* --port */
197       options.port = atoi(arg);
198       break;
199     case 'h': /* --host */
200       if( options.host )
201         g_free(options.host);
202       options.host = g_strdup(arg);
203       break;
204     case 'P': /* --password */
205       if( options.password )
206         g_free(options.password);
207       options.password = locale_to_utf8(arg);
208       break;
209     case 'f': /* --config */
210       if( options.config_file )
211         g_free(options.config_file);
212       options.config_file = g_strdup(arg);
213       break;
214     case 'k': /* --key-file */
215       if( options.key_file )
216         g_free(options.key_file);
217       options.key_file = g_strdup(arg);
218       break;
219     case 'S': /* --key-file */
220       /* the splash screen was removed */
221       break;
222 #ifdef DEBUG
223     case 'K': /* --dump-keys */
224       read_configuration(&options);
225       write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
226       exit(EXIT_SUCCESS);
227       break;
228     case 'D': /* --debug */
229       options.debug = TRUE;
230       break;
231 #endif
232     default:
233       fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
234       break;
235     }
238 options_t *
239 options_get(void)
241   return &options;
244 options_t *
245 options_parse(int argc, const char *argv[])
247   int i;
248   arg_opt_t *opt = NULL;
249   option_callback_fn_t option_cb = handle_option;
251   i=1;
252   while( i<argc )
253     {
254       char *arg = (char *) argv[i];
255       size_t len=strlen(arg);
256       
257       /* check for a long option */
258       if( g_str_has_prefix(arg, "--") )
259         {
260           char *name, *value;
261           
262           /* make shure we got an argument for the previous option */
263           if( opt && opt->argument )
264             option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
266           /* retreive a option argument */
267           if( (value=g_strrstr(arg+2, "=")) )
268             {
269               *value = '\0';
270               name = g_strdup(arg);
271               *value = '=';
272               value++;
273             }
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);
281           
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);
285           
286           /* execute option callback */
287           if( value || opt->argument==NULL )
288             {
289               option_cb (opt->shortopt, value);
290               opt = NULL;
291             }
292         }
293       /* check for short options */
294       else if( len>=2 && g_str_has_prefix(arg, "-") )
295         {
296           int j;
298           for(j=1; j<len; j++)
299             {
300               /* make shure we got an argument for the previous option */
301               if( opt && opt->argument )
302                 option_error(ERROR_MISSING_ARGUMENT, 
303                              opt->longopt, opt->argument);
304               
305               /* check if the option exists */
306               if( (opt=lookup_option(arg[j], NULL))==NULL )
307                 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
309               /* if no option argument is needed execute callback */
310               if( opt->argument==NULL )
311                 {
312                   option_cb (opt->shortopt, NULL);
313                   opt = NULL;
314                 }
315             }
316         }
317       else
318         {
319           /* is this a option argument? */
320           if( opt && opt->argument)
321             {
322               option_cb (opt->shortopt, arg);
323               opt = NULL;
324             }
325           else 
326             option_error(ERROR_BAD_ARGUMENT, arg, NULL);          
327         }
328       i++;
329     }
330   
331   if( opt && opt->argument==NULL)
332     option_cb (opt->shortopt, NULL);
333   else if( opt && opt->argument )
334     option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
336   return  &options;
340 options_t *
341 options_init( void )
343   const char *value;
344   char *tmp;
346   memset(&options, 0, sizeof(options_t));
348   /* get initial values for host and password from MPD_HOST (enviroment) */
349   if( (value=g_getenv(MPD_HOST_ENV)) )
350     options.host = g_strdup(value);
351   else
352     options.host = g_strdup(DEFAULT_HOST);
353   if( (tmp=g_strstr_len(options.host, strlen(options.host), "@")) )
354     {
355       char *oldhost = options.host;
356       *tmp  = '\0';
357       options.password = locale_to_utf8(oldhost);
358       options.host = g_strdup(tmp+1);
359       g_free(oldhost);
360     }
361   /* get initial values for port from MPD_PORT (enviroment) */
362   if( (value=g_getenv(MPD_PORT_ENV)) )
363     options.port = atoi(value);
364   else
365     options.port = DEFAULT_PORT;
367   /* default option values */
368   options.reconnect = TRUE;
369   options.find_wrap = TRUE;
370   options.wide_cursor = TRUE;
371   options.audible_bell = TRUE;
372   options.crossfade_time = DEFAULT_CROSSFADE_TIME;
373   options.seek_time = 1;
374   options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
375   options.timedisplay_type = DEFAULT_TIMEDISPLAY_TYPE;
376   options.lyrics_timeout = DEFAULT_LYRICS_TIMEOUT;
377   options.scroll = DEFAULT_SCROLL;
378   options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
379   
380   return &options;