Code

0403559cfe5fe34fc1098ced0d371ab18c6e31a2
[ncmpc.git] / src / conf.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "conf.h"
21 #include "config.h"
22 #include "defaults.h"
23 #include "i18n.h"
24 #include "command.h"
25 #include "colors.h"
26 #include "screen_list.h"
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <glib.h>
39 #define MAX_LINE_LENGTH 1024
40 #define COMMENT_TOKEN '#'
42 /* configuration field names */
43 #define CONF_ENABLE_COLORS "enable-colors"
44 #define CONF_SCROLL_OFFSET "scroll-offset"
45 #define CONF_AUTO_CENTER "auto-center"
46 #define CONF_WIDE_CURSOR "wide-cursor"
47 #define CONF_KEY_DEFINITION "key"
48 #define CONF_COLOR "color"
49 #define CONF_COLOR_DEFINITION "colordef"
50 #define CONF_LIST_FORMAT "list-format"
51 #define CONF_STATUS_FORMAT "status-format"
52 #define CONF_XTERM_TITLE_FORMAT "xterm-title-format"
53 #define CONF_LIST_WRAP "wrap-around"
54 #define CONF_FIND_WRAP "find-wrap"
55 #define CONF_FIND_SHOW_LAST "find-show-last"
56 #define CONF_AUDIBLE_BELL "audible-bell"
57 #define CONF_VISIBLE_BELL "visible-bell"
58 #define CONF_BELL_ON_WRAP "bell-on-wrap"
59 #define CONF_STATUS_MESSAGE_TIME "status-message-time"
60 #define CONF_XTERM_TITLE "set-xterm-title"
61 #define CONF_ENABLE_MOUSE "enable-mouse"
62 #define CONF_CROSSFADE_TIME "crossfade-time"
63 #define CONF_SEARCH_MODE "search-mode"
64 #define CONF_HIDE_CURSOR "hide-cursor"
65 #define CONF_SEEK_TIME "seek-time"
66 #define CONF_SCREEN_LIST "screen-list"
67 #define CONF_TIMEDISPLAY_TYPE "timedisplay-type"
68 #define CONF_HOST "host"
69 #define CONF_PORT "port"
70 #define CONF_PASSWORD "password"
71 #define CONF_LYRICS_TIMEOUT "lyrics-timeout"
72 #define CONF_SCROLL "scroll"
73 #define CONF_SCROLL_SEP "scroll-sep"
74 #define CONF_VISIBLE_BITRATE "visible-bitrate"
75 #define CONF_WELCOME_SCREEN_LIST "welcome-screen-list"
76 #define CONF_DISPLAY_TIME "display-time"
77 #define CONF_JUMP_PREFIX_ONLY "jump-prefix-only"
78 #define CONF_LYRICS_AUTOSAVE "lyrics-autosave"
79 #define CONF_LYRICS_SHOW_PLUGIN "lyrics-show-plugin"
80 #define CONF_SECOND_COLUMN "second-column"
82 static bool
83 str2bool(char *str)
84 {
85         return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
86                 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
87 }
89 static void
90 print_error(const char *msg, const char *input)
91 {
92         fprintf(stderr, "%s: %s ('%s')\n",
93                 /* To translators: prefix for error messages */
94                 _("Error"), msg, input);
95 }
97 static int
98 parse_key_value(char *str, char **end)
99 {
100         if (*str == '\'') {
101                 if (str[1] == '\'' || str[2] != '\'') {
102                         print_error(_("Malformed hotkey definition"), str);
103                         return -1;
104                 }
106                 *end = str + 3;
107                 return str[1];
108         } else {
109                 long value = strtol(str, end, 0);
110                 if (*end == str) {
111                         print_error(_("Malformed hotkey definition"), str);
112                         return -1;
113                 }
115                 return (int)value;
116         }
119 static int
120 parse_key_definition(char *str)
122         char buf[MAX_LINE_LENGTH];
123         char *p;
124         size_t len = strlen(str), i;
125         int j,key;
126         int keys[MAX_COMMAND_KEYS];
127         command_t cmd;
129         /* get the command name */
130         i=0;
131         j=0;
132         memset(buf, 0, MAX_LINE_LENGTH);
133         while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
134                 buf[j++] = str[i++];
135         if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
136                 /* the hotkey configuration contains an unknown
137                    command */
138                 print_error(_("Unknown command"), buf);
139                 return -1;
140         }
142         /* skip whitespace */
143         while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
144                 i++;
146         /* get the value part */
147         memset(buf, 0, MAX_LINE_LENGTH);
148         g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
149         if (*buf == 0) {
150                 /* the hotkey configuration line is incomplete */
151                 print_error(_("Incomplete hotkey configuration"), str);
152                 return -1;
153         }
155         /* parse key values */
156         i = 0;
157         key = 0;
158         p = buf;
159         memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
160         while (i < MAX_COMMAND_KEYS && *p != 0 &&
161                (key = parse_key_value(p, &p)) >= 0) {
162                 keys[i++] = key;
163                 while (*p==',' || *p==' ' || *p=='\t')
164                         p++;
165         }
167         if (key < 0)
168                 return -1;
170         return assign_keys(cmd, keys);
173 static bool
174 parse_timedisplay_type(const char *str)
176         if (strcmp(str, "elapsed") == 0)
177                 return false;
178         else if (strcmp(str, "remaining") == 0)
179                 return true;
180         else {
181                 /* translators: ncmpc supports displaying the
182                    "elapsed" or "remaining" time of a song being
183                    played; in this case, the configuration file
184                    contained an invalid setting */
185                 print_error(_("Bad time display type"), str);
186                 return false;
187         }
190 #ifdef ENABLE_COLORS
191 static char *
192 separate_value(char *p)
194         char *value;
196         value = strchr(p, '=');
197         if (value == NULL) {
198                 /* an equals sign '=' was expected while parsing a
199                    configuration file line */
200                 fprintf(stderr, "%s\n", _("Missing '='"));
201                 return NULL;
202         }
204         *value++ = 0;
206         g_strchomp(p);
207         return g_strchug(value);
210 static int
211 parse_color(char *str)
213         char *value;
215         value = separate_value(str);
216         if (value == NULL)
217                 return -1;
219         return colors_assign(str, value);
222 /**
223  * Returns the first non-whitespace character after the next comma
224  * character, or the end of the string.  This is used to parse comma
225  * separated values.
226  */
227 static char *
228 after_comma(char *p)
230         char *comma = strchr(p, ',');
232         if (comma != NULL) {
233                 *comma++ = 0;
234                 comma = g_strchug(comma);
235         } else
236                 comma = p + strlen(p);
238         g_strchomp(p);
239         return comma;
242 static int
243 parse_color_definition(char *str)
245         char buf[MAX_LINE_LENGTH];
246         char *value;
247         short color, rgb[3];
249         value = separate_value(str);
250         if (value == NULL)
251                 return -1;
253         /* get the command name */
254         color = colors_str2color(str);
255         if (color < 0) {
256                 print_error(_("Bad color name"), buf);
257                 return -1;
258         }
260         /* parse r,g,b values */
262         for (unsigned i = 0; i < 3; ++i) {
263                 char *next = after_comma(value), *endptr;
264                 if (*value == 0) {
265                         print_error(_("Incomplete color definition"), str);
266                         return -1;
267                 }
269                 rgb[i] = strtol(value, &endptr, 0);
270                 if (endptr == value || *endptr != 0) {
271                         print_error(_("Invalid number"), value);
272                         return -1;
273                 }
275                 value = next;
276         }
278         if (*value != 0) {
279                 print_error(_("Malformed color definition"), str);
280                 return -1;
281         }
283         return colors_define(str, rgb[0], rgb[1], rgb[2]);
285 #endif
287 static char *
288 get_format(char *str)
290         gsize len = strlen(str);
292         if (str && str[0]=='\"' && str[len-1] == '\"') {
293                 str[len - 1] = '\0';
294                 str++;
295         }
297         return g_strdup(str);
300 static char **
301 check_screen_list(char *value)
303         char **tmp = g_strsplit_set(value, " \t,", 100);
304         char **screen = NULL;
305         int i = 0, j = 0;
307         while( tmp && tmp[i] ) {
308                 char *name = g_ascii_strdown(tmp[i], -1);
309                 if (*name != '\0') {
310                         if (screen_lookup_name(name) == NULL) {
311                                 /* an unknown screen name was specified in the
312                                    configuration file */
313                                 print_error(_("Unknown screen name"), name);
314                                 free(name);
315                         } else {
316                                 screen = g_realloc(screen, (j+2)*sizeof(char *));
317                                 screen[j++] = name;
318                                 screen[j] = NULL;
319                         }
320                 }
321                 i++;
322         }
323         g_strfreev(tmp);
324         if( screen == NULL )
325                 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
327         return screen;
330 static int
331 get_search_mode(char *value)
333         char * test;
334         int mode;
335         mode= strtol(value, &test, 10);
336         if (*test == '\0')
337         {
338                 if (0 <= mode && mode <= 4)
339                         return mode;
340                 else
341                 {
342                         print_error(_("Invalid search mode"),value);
343                         return 0;
344                 }
345         }
346         else
347         {
348                 for (int i = 0; value[i] != '\0'; i++)
349                         value[i] = tolower(value[i]);
351                 // TODO: modify screen_search so that its own list of modes can be used
352                 // for comparison instead of specifying them here
353                 if (strcmp(value, "title") == 0)
354                         return 0;
355                 else if (strcmp(value, "artist") == 0)
356                         return 1;
357                 else if (strcmp(value, "album") == 0)
358                         return 2;
359                 else if (strcmp(value, "filename") == 0)
360                         return 3;
361                 else if (strcmp(value, "artist+album") == 0)
362                         return 4;
363                 else
364                 {
365                         print_error(_("Unknown search mode"),value);
366                         return 0;
367                 }
368         }
371 static bool
372 parse_line(char *line)
374         size_t len = strlen(line), i = 0, j = 0;
375         char name[MAX_LINE_LENGTH];
376         char value[MAX_LINE_LENGTH];
377         bool match_found;
379         /* get the name part */
380         while (i < len && line[i] != '=' && !g_ascii_isspace(line[i]))
381                 name[j++] = line[i++];
383         name[j] = '\0';
385         /* skip '=' and whitespace */
386         while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
387                 i++;
389         /* get the value part */
390         j = 0;
391         while (i < len)
392                 value[j++] = line[i++];
393         value[j] = '\0';
395         match_found = true;
397         /* key definition */
398         if (!strcasecmp(CONF_KEY_DEFINITION, name))
399                 parse_key_definition(value);
400         /* enable colors */
401         else if(!strcasecmp(CONF_ENABLE_COLORS, name))
402 #ifdef ENABLE_COLORS
403                 options.enable_colors = str2bool(value);
404 #else
405         {}
406 #endif
407         else if (!strcasecmp(CONF_SCROLL_OFFSET, name))
408                 options.scroll_offset = atoi(value);
409         /* auto center */
410         else if (!strcasecmp(CONF_AUTO_CENTER, name))
411                 options.auto_center = str2bool(value);
412         /* color assignment */
413         else if (!strcasecmp(CONF_COLOR, name))
414 #ifdef ENABLE_COLORS
415                 parse_color(value);
416 #else
417         {}
418 #endif
419         /* wide cursor */
420         else if (!strcasecmp(CONF_WIDE_CURSOR, name))
421                 options.wide_cursor = str2bool(value);
422         else if (strcasecmp(name, "hardware-cursor") == 0)
423                 options.hardware_cursor = str2bool(value);
424         /* welcome screen list */
425         else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
426                 options.welcome_screen_list = str2bool(value);
427         /* visible bitrate */
428         else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
429                 options.visible_bitrate = str2bool(value);
430         /* timer display type */
431         else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name))
432                 options.display_remaining_time = parse_timedisplay_type(value);
433                 /* color definition */
434         else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
435 #ifdef ENABLE_COLORS
436                 parse_color_definition(value);
437 #else
438         {}
439 #endif
440         /* list format string */
441         else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
442                 g_free(options.list_format);
443                 options.list_format = get_format(value);
444                 /* status format string */
445         } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
446                 g_free(options.status_format);
447                 options.status_format = get_format(value);
448                 /* xterm title format string */
449         } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
450                 g_free(options.xterm_title_format);
451                 options.xterm_title_format = get_format(value);
452         } else if (!strcasecmp(CONF_LIST_WRAP, name))
453                 options.list_wrap = str2bool(value);
454         else if (!strcasecmp(CONF_FIND_WRAP, name))
455                 options.find_wrap = str2bool(value);
456         else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
457                 options.find_show_last_pattern = str2bool(value);
458         else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
459                 options.audible_bell = str2bool(value);
460         else if (!strcasecmp(CONF_VISIBLE_BELL, name))
461                 options.visible_bell = str2bool(value);
462         else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
463                 options.bell_on_wrap = str2bool(value);
464         else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
465                 options.status_message_time = atoi(value);
466         else if (!strcasecmp(CONF_XTERM_TITLE, name))
467                 options.enable_xterm_title = str2bool(value);
468         else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
469 #ifdef HAVE_GETMOUSE
470                 options.enable_mouse = str2bool(value);
471 #else
472         {}
473 #endif
474         else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
475                 options.crossfade_time = atoi(value);
476         else if (!strcasecmp(CONF_SEARCH_MODE, name))
477                 options.search_mode = get_search_mode(value);
478         else if (!strcasecmp(CONF_HIDE_CURSOR, name))
479                 options.hide_cursor = atoi(value);
480         else if (!strcasecmp(CONF_SEEK_TIME, name))
481                 options.seek_time = atoi(value);
482         else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
483                 g_strfreev(options.screen_list);
484                 options.screen_list = check_screen_list(value);
485         } else if (!strcasecmp(CONF_HOST, name))
486                 options.host = get_format(value);
487         else if (!strcasecmp(CONF_PORT, name))
488                 options.port = atoi(get_format(value));
489         else if (!strcasecmp(CONF_PASSWORD, name))
490                 options.password = get_format(value);
491         else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
492 #ifdef ENABLE_LYRICS_SCREEN
493                 options.lyrics_timeout = atoi(get_format(value));
494 #else
495         {}
496 #endif
497         else if (!strcasecmp(CONF_SCROLL, name))
498                 options.scroll = str2bool(value);
499         else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
500                 g_free(options.scroll_sep);
501                 options.scroll_sep = get_format(value);
502         } else if (!strcasecmp(CONF_DISPLAY_TIME, name))
503 #ifdef NCMPC_MINI
504                 {}
505 #else
506                 options.display_time = str2bool(value);
507 #endif
508         else if (!strcasecmp(CONF_JUMP_PREFIX_ONLY, name))
509 #ifdef NCMPC_MINI
510                 {}
511 #else
512                 options.jump_prefix_only = str2bool(value);
513 #endif
514         else if (!strcasecmp(CONF_LYRICS_AUTOSAVE, name))
515 #ifdef ENABLE_LYRICS_SCREEN
516                 options.lyrics_autosave = str2bool(value);
517 #else
518         {}
519 #endif
520         else if (!strcasecmp(CONF_LYRICS_SHOW_PLUGIN, name))
521 #ifdef ENABLE_LYRICS_SCREEN
522                 options.lyrics_show_plugin = str2bool(value);
523 #else
524                 {}
525 #endif
526         else if (!strcasecmp(CONF_SECOND_COLUMN, name))
527 #ifdef NCMPC_MINI
528                 {}
529 #else
530                 options.second_column = str2bool(value);
531 #endif
532         else
533                 match_found = false;
535         if (!match_found)
536                 print_error(_("Unknown configuration parameter"), name);
538         return match_found;
541 static int
542 read_rc_file(char *filename)
544         FILE *file;
545         char line[MAX_LINE_LENGTH];
547         if (filename == NULL)
548                 return -1;
550         file = fopen(filename, "r");
551         if (file == NULL) {
552                         perror(filename);
553                         return -1;
554                 }
556         while (fgets(line, sizeof(line), file) != NULL) {
557                 char *p = g_strchug(line);
559                 if (*p != 0 && *p != COMMENT_TOKEN)
560                         parse_line(g_strchomp(p));
561         }
563         fclose(file);
564         return 0;
567 int
568 check_user_conf_dir(void)
570         int retval;
571         char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
573         if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
574                 g_free(directory);
575                 return 0;
576         }
578         retval = mkdir(directory, 0755);
579         g_free(directory);
580         return retval;
583 char *
584 get_user_key_binding_filename(void)
586         return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
589 int
590 read_configuration(void)
592         char *filename = NULL;
594         /* check for command line configuration file */
595         if (options.config_file)
596                 filename = g_strdup(options.config_file);
598         /* check for user configuration ~/.ncmpc/config */
599         if (filename == NULL) {
600                 filename = g_build_filename(g_get_home_dir(),
601                                             "." PACKAGE, "config", NULL);
602                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
603                         g_free(filename);
604                         filename = NULL;
605                 }
606         }
608         /* check for  global configuration SYSCONFDIR/ncmpc/config */
609         if (filename == NULL) {
610                 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
611                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
612                         g_free(filename);
613                         filename = NULL;
614                 }
615         }
617         /* load configuration */
618         if (filename) {
619                 read_rc_file(filename);
620                 g_free(filename);
621                 filename = NULL;
622         }
624         /* check for command line key binding file */
625         if (options.key_file)
626                 filename = g_strdup(options.key_file);
628         /* check for  user key bindings ~/.ncmpc/keys */
629         if (filename == NULL) {
630                 filename = get_user_key_binding_filename();
631                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
632                         g_free(filename);
633                         filename = NULL;
634                 }
635         }
637         /* check for  global key bindings SYSCONFDIR/ncmpc/keys */
638         if (filename == NULL) {
639                 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
640                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
641                         g_free(filename);
642                         filename = NULL;
643                 }
644         }
646         /* load key bindings */
647         if (filename) {
648                 read_rc_file(filename);
649                 g_free(filename);
650                 filename = NULL;
651         }
653         return 0;