Code

0f281bc163c934ee176c7894adb8f3adc75b3053
[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 #ifdef DEBUG
73   { 'K', "dump-keys", NULL,     "Dump key bindings to stdout" },
74   { 'D', "debug",   NULL,   "Enable debug output on stderr" },
75 #endif
76   { 0, NULL, NULL, NULL },
77 };
79 static arg_opt_t *
80 lookup_option(int s, char *l)
81 {
82   int i;
84   i=0;
85   while( option_table[i].descrition )
86     {
87       if( l && strcmp(l, option_table[i].longopt) == 0 )
88         return &option_table[i];;
89       if( s && s==option_table[i].shortopt )
90         return &option_table[i];;
91       i++;
92     }
93   return NULL;
94 }
96 static void
97 option_error(int error, char *option, char *arg)
98 {
99   switch(error)
100     {
101     case ERROR_UNKNOWN_OPTION:
102       fprintf(stderr, PACKAGE ": invalid option %s\n", option);
103       break;
104     case ERROR_BAD_ARGUMENT:
105       fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
106       break;
107     case ERROR_GOT_ARGUMENT:
108       fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
109       break;
110     case ERROR_MISSING_ARGUMENT:
111       fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
112       break;
113     default:
114       fprintf(stderr, PACKAGE ": internal error %d\n", error);
115       break;
116     }
117   exit(EXIT_FAILURE);
120 static void 
121 display_help(void)
123   int i = 0;
125   printf("Usage: %s [OPTION]...\n", PACKAGE);
126   while( option_table[i].descrition )
127     {
128       char tmp[MAX_LONGOPT_LENGTH];
130       if( option_table[i].argument )
131         g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s", 
132                    option_table[i].longopt, 
133                    option_table[i].argument);
134       else
135         g_strlcpy(tmp, option_table[i].longopt, 64);
137       printf("  -%c, --%-20s %s\n", 
138              option_table[i].shortopt, 
139              tmp,
140              option_table[i].descrition);
141       i++;
142     }
145 static void 
146 handle_option(int c, char *arg)
148   D("option callback -%c %s\n", c, arg);
149   switch(c)
150     {
151     case '?': /* --help */
152       display_help();
153       exit(EXIT_SUCCESS);
154     case 'V': /* --version */
155       printf("%s version: %s\n", PACKAGE, VERSION);
156       printf("build options:");
157 #ifdef DEBUG
158       printf(" debug");
159 #endif
160 #ifdef ENABLE_NLS
161       printf(" nls");
162 #endif
163 #ifdef HAVE_GETMOUSE
164       printf(" getmouse");
165 #endif
166 #ifdef ENABLE_ARTIST_SCREEN
167       printf(" artist-screen");
168 #endif
169 #ifdef ENABLE_SEARCH_SCREEN
170       printf(" search-screen");
171 #endif
172 #ifdef ENABLE_KEYDEF_SCREEN
173       printf(" key-screen");
174 #endif
175 #ifdef ENABLE_CLOCK_SCREEN
176       printf(" clock-screen");
177 #endif
178       printf("\n");
179       exit(EXIT_SUCCESS);
180     case 'c': /* --colors */
181       options.enable_colors = TRUE;
182       break;
183     case 'C': /* --no-colors */
184       options.enable_colors = FALSE;
185       break;
186     case 'm': /* --mouse */
187      options.enable_mouse = TRUE;
188       break;
189     case 'M': /* --no-mouse */
190       options.enable_mouse = FALSE;
191       break;
192     case 'e': /* --exit */
193       options.reconnect = FALSE;
194       break;
195     case 'p': /* --port */
196       options.port = atoi(arg);
197       break;
198     case 'h': /* --host */
199       if( options.host )
200         g_free(options.host);
201       options.host = g_strdup(arg);
202       break;
203     case 'P': /* --password */
204       if( options.password )
205         g_free(options.password);
206       options.password = locale_to_utf8(arg);
207       break;
208     case 'f': /* --config */
209       if( options.config_file )
210         g_free(options.config_file);
211       options.config_file = g_strdup(arg);
212       break;
213     case 'k': /* --key-file */
214       if( options.key_file )
215         g_free(options.key_file);
216       options.key_file = g_strdup(arg);
217       break;
218 #ifdef DEBUG
219     case 'K': /* --dump-keys */
220       read_configuration(&options);
221       write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
222       exit(EXIT_SUCCESS);
223       break;
224     case 'D': /* --debug */
225       options.debug = TRUE;
226       break;
227 #endif
228     default:
229       fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
230       break;
231     }
234 options_t *
235 options_get(void)
237   return &options;
240 options_t *
241 options_parse(int argc, const char *argv[])
243   int i;
244   arg_opt_t *opt = NULL;
245   option_callback_fn_t option_cb = handle_option;
247   i=1;
248   while( i<argc )
249     {
250       char *arg = (char *) argv[i];
251       size_t len=strlen(arg);
252       
253       /* check for a long option */
254       if( g_str_has_prefix(arg, "--") )
255         {
256           char *name, *value;
257           
258           /* make shure we got an argument for the previous option */
259           if( opt && opt->argument )
260             option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
262           /* retreive a option argument */
263           if( (value=g_strrstr(arg+2, "=")) )
264             {
265               *value = '\0';
266               name = g_strdup(arg);
267               *value = '=';
268               value++;
269             }
270           else
271             name = g_strdup(arg);
273           /* check if the option exists */
274           if( (opt=lookup_option(0, name+2)) == NULL )
275             option_error(ERROR_UNKNOWN_OPTION, name, NULL);
276           g_free(name);
277           
278           /* abort if we got an argument to the option and dont want one */
279           if( value && opt->argument==NULL )
280             option_error(ERROR_GOT_ARGUMENT, arg, value);
281           
282           /* execute option callback */
283           if( value || opt->argument==NULL )
284             {
285               option_cb (opt->shortopt, value);
286               opt = NULL;
287             }
288         }
289       /* check for short options */
290       else if( len>=2 && g_str_has_prefix(arg, "-") )
291         {
292           int j;
294           for(j=1; j<len; j++)
295             {
296               /* make shure we got an argument for the previous option */
297               if( opt && opt->argument )
298                 option_error(ERROR_MISSING_ARGUMENT, 
299                              opt->longopt, opt->argument);
300               
301               /* check if the option exists */
302               if( (opt=lookup_option(arg[j], NULL))==NULL )
303                 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
305               /* if no option argument is needed execute callback */
306               if( opt->argument==NULL )
307                 {
308                   option_cb (opt->shortopt, NULL);
309                   opt = NULL;
310                 }
311             }
312         }
313       else
314         {
315           /* is this a option argument? */
316           if( opt && opt->argument)
317             {
318               option_cb (opt->shortopt, arg);
319               opt = NULL;
320             }
321           else 
322             option_error(ERROR_BAD_ARGUMENT, arg, NULL);          
323         }
324       i++;
325     }
326   
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);
332   return  &options;
336 options_t *
337 options_init( void )
339   const char *value;
340   char *tmp;
342   memset(&options, 0, sizeof(options_t));
344   /* get initial values for host and password from MPD_HOST (enviroment) */
345   if( (value=g_getenv(MPD_HOST_ENV)) )
346     options.host = g_strdup(value);
347   else
348     options.host = g_strdup(DEFAULT_HOST);
349   if( (tmp=g_strstr_len(options.host, strlen(options.host), "@")) )
350     {
351       char *oldhost = options.host;
352       *tmp  = '\0';
353       options.password = locale_to_utf8(oldhost);
354       options.host = g_strdup(tmp+1);
355       g_free(oldhost);
356     }
357   /* get initial values for port from MPD_PORT (enviroment) */
358   if( (value=g_getenv(MPD_PORT_ENV)) )
359     options.port = atoi(value);
360   else
361     options.port = DEFAULT_PORT;
363   /* default option values */
364   options.reconnect = TRUE;
365   options.find_wrap = TRUE;
366   options.wide_cursor = TRUE;
367   options.audible_bell = TRUE;
368   options.crossfade_time = DEFAULT_CROSSFADE_TIME;
369   options.seek_time = 1;
370   options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
371   options.timedisplay_type = DEFAULT_TIMEDISPLAY_TYPE;
372   
373   return &options;