Code

show translator credits
[ncmpc.git] / src / options.c
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"
25 #include "i18n.h"
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <glib.h>
32 #define MAX_LONGOPT_LENGTH 32
34 #define ERROR_UNKNOWN_OPTION    0x01
35 #define ERROR_BAD_ARGUMENT      0x02
36 #define ERROR_GOT_ARGUMENT      0x03
37 #define ERROR_MISSING_ARGUMENT  0x04
39 typedef struct {
40         int shortopt;
41         const char *longopt;
42         const char *argument;
43         const char *descrition;
44 } arg_opt_t;
47 typedef void (*option_callback_fn_t)(int c, const char *arg);
50 options_t options = {
51         .port = DEFAULT_PORT,
52         .crossfade_time = DEFAULT_CROSSFADE_TIME,
53         .seek_time = 1,
54 #ifdef ENABLE_LYRICS_SCREEN
55         .lyrics_timeout = DEFAULT_LYRICS_TIMEOUT,
56 #endif
57         .find_wrap = true,
58         .wide_cursor = true,
59         .audible_bell = true,
60 #ifndef NCMPC_MINI
61         .scroll = DEFAULT_SCROLL,
62         .welcome_screen_list = true,
63 #endif
64 };
66 static const arg_opt_t option_table[] = {
67         { '?', "help", NULL, "Show this help message" },
68         { 'V', "version", NULL, "Display version information" },
69         { 'c', "colors", NULL, "Enable colors" },
70         { 'C', "no-colors", NULL, "Disable colors" },
71 #ifdef HAVE_GETMOUSE
72         { 'm', "mouse", NULL, "Enable mouse" },
73         { 'M', "no-mouse", NULL, "Disable mouse" },
74 #endif
75         { 'e', "exit", NULL, "Exit on connection errors" },
76         { 'p', "port", "PORT", "Connect to server on port [" DEFAULT_PORT_STR "]" },
77         { 'h', "host", "HOST", "Connect to server on host [" DEFAULT_HOST "]" },
78         { 'P', "password","PASSWORD", "Connect with password" },
79         { 'f', "config", "FILE", "Read configuration from file" },
80         { 'k', "key-file","FILE", "Read configuration from file" },
81         { 'S', "no-splash", NULL, "Don't show the splash screen" },
82 #ifndef NDEBUG
83         { 'K', "dump-keys", NULL, "Dump key bindings to stdout" },
84 #endif
85 };
87 static const unsigned option_table_size = sizeof(option_table) / sizeof(option_table[0]);
89 static const arg_opt_t *
90 lookup_option(int s, char *l)
91 {
92         unsigned i;
94         for (i = 0; i < option_table_size; ++i) {
95                 if (l && strcmp(l, option_table[i].longopt) == 0)
96                         return &option_table[i];;
97                 if (s && s == option_table[i].shortopt)
98                         return &option_table[i];;
99         }
101         return NULL;
104 static void
105 option_error(int error, const char *option, const char *arg)
107         switch (error) {
108         case ERROR_UNKNOWN_OPTION:
109                 fprintf(stderr, PACKAGE ": invalid option %s\n", option);
110                 break;
111         case ERROR_BAD_ARGUMENT:
112                 fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
113                 break;
114         case ERROR_GOT_ARGUMENT:
115                 fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
116                 break;
117         case ERROR_MISSING_ARGUMENT:
118                 fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
119                 break;
120         default:
121                 fprintf(stderr, PACKAGE ": internal error %d\n", error);
122                 break;
123         }
125         exit(EXIT_FAILURE);
128 static void
129 display_help(void)
131         unsigned i;
133         printf("Usage: %s [OPTION]...\n", PACKAGE);
135         for (i = 0; i < option_table_size; ++i) {
136                 char tmp[MAX_LONGOPT_LENGTH];
138                 if (option_table[i].argument)
139                         g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s",
140                                    option_table[i].longopt,
141                                    option_table[i].argument);
142                 else
143                         g_strlcpy(tmp, option_table[i].longopt, 64);
145                 printf("  -%c, --%-20s %s\n",
146                        option_table[i].shortopt,
147                        tmp,
148                        option_table[i].descrition);
149                 i++;
150         }
153 static void
154 handle_option(int c, const char *arg)
156         switch (c) {
157         case '?': /* --help */
158                 display_help();
159                 exit(EXIT_SUCCESS);
160         case 'V': /* --version */
161                 puts(PACKAGE " version: " VERSION "\n"
162                      "build options:"
163 #ifdef NCMPC_MINI
164                      " mini"
165 #endif
166 #ifndef NDEBUG
167                      " debug"
168 #endif
169 #ifdef ENABLE_WIDE
170                      " wide"
171 #endif
172 #ifdef ENABLE_LOCALE
173                      " locale"
174 #endif
175 #ifdef ENABLE_NLS
176                      " nls"
177 #endif
178 #ifdef ENABLE_COLORS
179                      " colors"
180 #else
181                      " no-colors"
182 #endif
183 #ifdef ENABLE_LIRC
184                      " lirc"
185 #endif
186 #ifdef HAVE_GETMOUSE
187                      " getmouse"
188 #endif
189 #ifdef ENABLE_ARTIST_SCREEN
190                      " artist-screen"
191 #endif
192 #ifdef ENABLE_HELP_SCREEN
193                      " help-screen"
194 #endif
195 #ifdef ENABLE_SEARCH_SCREEN
196                      " search-screen"
197 #endif
198 #ifdef ENABLE_SONG_SCREEN
199                      " song-screen"
200 #endif
201 #ifdef ENABLE_KEYDEF_SCREEN
202                      " key-screen"
203 #endif
204 #ifdef ENABLE_LYRICS_SCREEN
205                      " lyrics-screen"
206 #endif
207 #ifdef ENABLE_OUTPUTS_SCREEN
208                      " outputs-screen"
209 #endif
211                      "\n");
212 #ifndef NCMPC_MINI
213                 if (strcmp("translator-credits", _("translator-credits")) != 0)
214                         /* To translators: these credits are shown
215                            when ncmpc is started with "--version" */
216                         printf("\n%s\n", _("translator-credits"));
217 #endif
218                 exit(EXIT_SUCCESS);
219         case 'c': /* --colors */
220 #ifdef ENABLE_COLORS
221                 options.enable_colors = true;
222 #endif
223                 break;
224         case 'C': /* --no-colors */
225 #ifdef ENABLE_COLORS
226                 options.enable_colors = false;
227 #endif
228                 break;
229         case 'm': /* --mouse */
230 #ifdef HAVE_GETMOUSE
231                 options.enable_mouse = true;
232 #endif
233                 break;
234         case 'M': /* --no-mouse */
235 #ifdef HAVE_GETMOUSE
236                 options.enable_mouse = false;
237 #endif
238                 break;
239         case 'e': /* --exit */
240                 /* deprecated */
241                 break;
242         case 'p': /* --port */
243                 options.port = atoi(arg);
244                 break;
245         case 'h': /* --host */
246                 if( options.host )
247                         g_free(options.host);
248                 options.host = g_strdup(arg);
249                 break;
250         case 'P': /* --password */
251                 if( options.password )
252                         g_free(options.password);
253                 options.password = locale_to_utf8(arg);
254                 break;
255         case 'f': /* --config */
256                 if( options.config_file )
257                         g_free(options.config_file);
258                 options.config_file = g_strdup(arg);
259                 break;
260         case 'k': /* --key-file */
261                 if( options.key_file )
262                         g_free(options.key_file);
263                 options.key_file = g_strdup(arg);
264                 break;
265         case 'S': /* --key-file */
266                 /* the splash screen was removed */
267                 break;
268 #ifndef NDEBUG
269         case 'K': /* --dump-keys */
270                 read_configuration();
271                 write_key_bindings(stdout, KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
272                 exit(EXIT_SUCCESS);
273                 break;
274 #endif
275         default:
276                 fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
277                 break;
278         }
281 void
282 options_parse(int argc, const char *argv[])
284         int i;
285         const arg_opt_t *opt = NULL;
286         option_callback_fn_t option_cb = handle_option;
288         i=1;
289         while (i < argc) {
290                 const char *arg = argv[i];
291                 size_t len = strlen(arg);
293                 /* check for a long option */
294                 if (g_str_has_prefix(arg, "--")) {
295                         char *name, *value;
297                         /* make shure we got an argument for the previous option */
298                         if( opt && opt->argument )
299                                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
301                         /* retreive a option argument */
302                         if ((value=g_strrstr(arg+2, "="))) {
303                                 *value = '\0';
304                                 name = g_strdup(arg);
305                                 *value = '=';
306                                 value++;
307                         } else
308                                 name = g_strdup(arg);
310                         /* check if the option exists */
311                         if( (opt=lookup_option(0, name+2)) == NULL )
312                                 option_error(ERROR_UNKNOWN_OPTION, name, NULL);
313                         g_free(name);
315                         /* abort if we got an argument to the option and dont want one */
316                         if( value && opt->argument==NULL )
317                                 option_error(ERROR_GOT_ARGUMENT, arg, value);
319                         /* execute option callback */
320                         if (value || opt->argument==NULL) {
321                                 option_cb (opt->shortopt, value);
322                                 opt = NULL;
323                         }
324                 }
325                 /* check for short options */
326                 else if (len>=2 && g_str_has_prefix(arg, "-")) {
327                         size_t j;
329                         for(j=1; j<len; j++) {
330                                 /* make shure we got an argument for the previous option */
331                                 if (opt && opt->argument)
332                                         option_error(ERROR_MISSING_ARGUMENT,
333                                                      opt->longopt, opt->argument);
335                                 /* check if the option exists */
336                                 if ((opt=lookup_option(arg[j], NULL)) == NULL)
337                                         option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
339                                 /* if no option argument is needed execute callback */
340                                 if (opt->argument == NULL) {
341                                         option_cb (opt->shortopt, NULL);
342                                         opt = NULL;
343                                 }
344                         }
345                 } else {
346                         /* is this a option argument? */
347                         if (opt && opt->argument) {
348                                 option_cb (opt->shortopt, arg);
349                                 opt = NULL;
350                         } else
351                                 option_error(ERROR_BAD_ARGUMENT, arg, NULL);
352                 }
353                 i++;
354         }
356         if (opt && opt->argument == NULL)
357                 option_cb (opt->shortopt, NULL);
358         else if (opt && opt->argument)
359                 option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
362 void
363 options_init(void)
365         const char *value;
366         char *tmp;
368         /* get initial values for host and password from MPD_HOST (enviroment) */
369         if ((value = g_getenv(MPD_HOST_ENV)))
370                 options.host = g_strdup(value);
371         else
372                 options.host = g_strdup(DEFAULT_HOST);
374         if ((tmp = g_strstr_len(options.host, strlen(options.host), "@"))) {
375                 char *oldhost = options.host;
376                 *tmp  = '\0';
377                 options.password = locale_to_utf8(oldhost);
378                 options.host = g_strdup(tmp+1);
379                 g_free(oldhost);
380         }
382         /* get initial values for port from MPD_PORT (enviroment) */
383         if ((value = g_getenv(MPD_PORT_ENV)))
384                 options.port = atoi(value);
386         /* default option values */
387         options.list_format = g_strdup(DEFAULT_LIST_FORMAT);
388         options.status_format = g_strdup(DEFAULT_STATUS_FORMAT);
389         options.screen_list = g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
390         options.timedisplay_type = g_strdup(DEFAULT_TIMEDISPLAY_TYPE);
391 #ifndef NCMPC_MINI
392         options.scroll_sep = g_strdup(DEFAULT_SCROLL_SEP);
393 #endif