Code

Use my_wgetch() instead of wgetch(), added --[no-]mouse option
[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 ENABLE_KEYDEF_SCREEN
164       printf(" key-screen");
165 #endif
166 #ifdef ENABLE_CLOCK_SCREEN
167       printf(" clock-screen");
168 #endif
169       printf("\n");
170       exit(EXIT_SUCCESS);
171     case 'c': /* --colors */
172       options.enable_colors = TRUE;
173       break;
174     case 'C': /* --no-colors */
175       options.enable_colors = FALSE;
176       break;
177    case 'm': /* --mouse */
178      options.enable_mouse = TRUE;
179       break;
180     case 'M': /* --no-mouse */
181       options.enable_mouse = FALSE;
182       break;
183     case 'e': /* --exit */
184       options.reconnect = FALSE;
185       break;
186     case 'p': /* --port */
187       options.port = atoi(arg);
188       break;
189     case 'h': /* --host */
190       if( options.host )
191         g_free(options.host);
192       options.host = g_strdup(arg);
193       break;
194     case 'P': /* --password */
195       if( options.password )
196         g_free(options.password);
197       options.password = locale_to_utf8(arg);
198       break;
199     case 'f': /* --config */
200       if( options.config_file )
201         g_free(options.config_file);
202       options.config_file = g_strdup(arg);
203       break;
204     case 'k': /* --key-file */
205       if( options.key_file )
206         g_free(options.key_file);
207       options.key_file = g_strdup(arg);
208       break;
209 #ifdef DEBUG
210     case 'K': /* --dump-keys */
211       read_configuration(&options);
212       write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
213       exit(EXIT_SUCCESS);
214       break;
215     case 'D': /* --debug */
216       options.debug = TRUE;
217       break;
218 #endif
219     default:
220       fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
221       break;
222     }
225 options_t *
226 options_get(void)
228   return &options;
231 options_t *
232 options_parse(int argc, const char *argv[])
234   int i;
235   arg_opt_t *opt = NULL;
236   option_callback_fn_t option_cb = handle_option;
238   i=1;
239   while( i<argc )
240     {
241       char *arg = (char *) argv[i];
242       size_t len=strlen(arg);
243       
244       /* check for a long option */
245       if( g_str_has_prefix(arg, "--") )
246         {
247           char *name, *value;
248           
249           /* make shure we got an argument for the previous option */
250           if( opt && opt->argument )
251             option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
253           /* retreive a option argument */
254           if( (value=g_strrstr(arg+2, "=")) )
255             {
256               *value = '\0';
257               name = g_strdup(arg);
258               *value = '=';
259               value++;
260             }
261           else
262             name = g_strdup(arg);
264           /* check if the option exists */
265           if( (opt=lookup_option(0, name+2)) == NULL )
266             option_error(ERROR_UNKNOWN_OPTION, name, NULL);
267           g_free(name);
268           
269           /* abort if we got an argument to the option and dont want one */
270           if( value && opt->argument==NULL )
271             option_error(ERROR_GOT_ARGUMENT, arg, value);
272           
273           /* execute option callback */
274           if( value || opt->argument==NULL )
275             {
276               option_cb (opt->shortopt, value);
277               opt = NULL;
278             }
279         }
280       /* check for short options */
281       else if( len>=2 && g_str_has_prefix(arg, "-") )
282         {
283           int j;
285           for(j=1; j<len; j++)
286             {
287               /* make shure we got an argument for the previous option */
288               if( opt && opt->argument )
289                 option_error(ERROR_MISSING_ARGUMENT, 
290                              opt->longopt, opt->argument);
291               
292               /* check if the option exists */
293               if( (opt=lookup_option(arg[j], NULL))==NULL )
294                 option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
296               /* if no option argument is needed execute callback */
297               if( opt->argument==NULL )
298                 {
299                   option_cb (opt->shortopt, NULL);
300                   opt = NULL;
301                 }
302             }
303         }
304       else
305         {
306           /* is this a option argument? */
307           if( opt && opt->argument)
308             {
309               option_cb (opt->shortopt, arg);
310               opt = NULL;
311             }
312           else 
313             option_error(ERROR_BAD_ARGUMENT, arg, NULL);          
314         }
315       i++;
316     }
317   
318   if( opt && opt->argument==NULL)
319     option_cb (opt->shortopt, NULL);
320   else if( opt && opt->argument )
321     option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
323   return  &options;
327 options_t *
328 options_init( void )
330   const char *value;
331   char *tmp;
333   memset(&options, 0, sizeof(options_t));
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);
340   if( (tmp=g_strstr_len(options.host, strlen(options.host), "@")) )
341     {
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     }
348   /* get initial values for port from MPD_PORT (enviroment) */
349   if( (value=g_getenv(MPD_PORT_ENV)) )
350     options.port = atoi(value);
351   else
352     options.port = DEFAULT_PORT;
354   /* default option values */
355   options.reconnect = TRUE;
356   options.find_wrap = TRUE;
357   options.wide_cursor = TRUE;
358   options.audible_bell = TRUE;
360   return &options;