Code

9d1f8828d8d28d59dab74bbc71cf8bed4fe54801
[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.
9  *
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.
14  *
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_TEXT_EDITOR "text-editor"
81 #define CONF_SECOND_COLUMN "second-column"
83 static bool
84 str2bool(char *str)
85 {
86         return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
87                 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
88 }
90 static void
91 print_error(const char *msg, const char *input)
92 {
93         fprintf(stderr, "%s: %s ('%s')\n",
94                 /* To translators: prefix for error messages */
95                 _("Error"), msg, input);
96 }
98 static int
99 parse_key_value(char *str, char **end)
101         if (*str == '\'') {
102                 if (str[1] == '\'' || str[2] != '\'') {
103                         print_error(_("Malformed hotkey definition"), str);
104                         return -1;
105                 }
107                 *end = str + 3;
108                 return str[1];
109         } else {
110                 long value = strtol(str, end, 0);
111                 if (*end == str) {
112                         print_error(_("Malformed hotkey definition"), str);
113                         return -1;
114                 }
116                 return (int)value;
117         }
120 static int
121 parse_key_definition(char *str)
123         char buf[MAX_LINE_LENGTH];
124         char *p;
125         size_t len = strlen(str), i;
126         int j,key;
127         int keys[MAX_COMMAND_KEYS];
128         command_t cmd;
130         /* get the command name */
131         i=0;
132         j=0;
133         memset(buf, 0, MAX_LINE_LENGTH);
134         while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
135                 buf[j++] = str[i++];
136         if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
137                 /* the hotkey configuration contains an unknown
138                    command */
139                 print_error(_("Unknown command"), buf);
140                 return -1;
141         }
143         /* skip whitespace */
144         while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
145                 i++;
147         /* get the value part */
148         memset(buf, 0, MAX_LINE_LENGTH);
149         g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
150         if (*buf == 0) {
151                 /* the hotkey configuration line is incomplete */
152                 print_error(_("Incomplete hotkey configuration"), str);
153                 return -1;
154         }
156         /* parse key values */
157         i = 0;
158         key = 0;
159         p = buf;
160         memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
161         while (i < MAX_COMMAND_KEYS && *p != 0 &&
162                (key = parse_key_value(p, &p)) >= 0) {
163                 keys[i++] = key;
164                 while (*p==',' || *p==' ' || *p=='\t')
165                         p++;
166         }
168         if (key < 0)
169                 return -1;
171         return assign_keys(cmd, keys);
174 static bool
175 parse_timedisplay_type(const char *str)
177         if (strcmp(str, "elapsed") == 0)
178                 return false;
179         else if (strcmp(str, "remaining") == 0)
180                 return true;
181         else {
182                 /* translators: ncmpc supports displaying the
183                    "elapsed" or "remaining" time of a song being
184                    played; in this case, the configuration file
185                    contained an invalid setting */
186                 print_error(_("Bad time display type"), str);
187                 return false;
188         }
191 #ifdef ENABLE_COLORS
192 static char *
193 separate_value(char *p)
195         char *value;
197         value = strchr(p, '=');
198         if (value == NULL) {
199                 /* an equals sign '=' was expected while parsing a
200                    configuration file line */
201                 fprintf(stderr, "%s\n", _("Missing '='"));
202                 return NULL;
203         }
205         *value++ = 0;
207         g_strchomp(p);
208         return g_strchug(value);
211 static int
212 parse_color(char *str)
214         char *value;
216         value = separate_value(str);
217         if (value == NULL)
218                 return -1;
220         return colors_assign(str, value);
223 /**
224  * Returns the first non-whitespace character after the next comma
225  * character, or the end of the string.  This is used to parse comma
226  * separated values.
227  */
228 static char *
229 after_comma(char *p)
231         char *comma = strchr(p, ',');
233         if (comma != NULL) {
234                 *comma++ = 0;
235                 comma = g_strchug(comma);
236         } else
237                 comma = p + strlen(p);
239         g_strchomp(p);
240         return comma;
243 static int
244 parse_color_definition(char *str)
246         char buf[MAX_LINE_LENGTH];
247         char *value;
248         short color, rgb[3];
250         value = separate_value(str);
251         if (value == NULL)
252                 return -1;
254         /* get the command name */
255         color = colors_str2color(str);
256         if (color < 0) {
257                 print_error(_("Bad color name"), buf);
258                 return -1;
259         }
261         /* parse r,g,b values */
263         for (unsigned i = 0; i < 3; ++i) {
264                 char *next = after_comma(value), *endptr;
265                 if (*value == 0) {
266                         print_error(_("Incomplete color definition"), str);
267                         return -1;
268                 }
270                 rgb[i] = strtol(value, &endptr, 0);
271                 if (endptr == value || *endptr != 0) {
272                         print_error(_("Invalid number"), value);
273                         return -1;
274                 }
276                 value = next;
277         }
279         if (*value != 0) {
280                 print_error(_("Malformed color definition"), str);
281                 return -1;
282         }
284         return colors_define(str, rgb[0], rgb[1], rgb[2]);
286 #endif
288 static char *
289 get_format(char *str)
291         gsize len = strlen(str);
293         if (str && str[0]=='\"' && str[len-1] == '\"') {
294                 str[len - 1] = '\0';
295                 str++;
296         }
298         return g_strdup(str);
301 static char **
302 check_screen_list(char *value)
304         char **tmp = g_strsplit_set(value, " \t,", 100);
305         char **screen = NULL;
306         int i = 0, j = 0;
308         while( tmp && tmp[i] ) {
309                 char *name = g_ascii_strdown(tmp[i], -1);
310                 if (*name != '\0') {
311                         if (screen_lookup_name(name) == NULL) {
312                                 /* an unknown screen name was specified in the
313                                    configuration file */
314                                 print_error(_("Unknown screen name"), name);
315                                 free(name);
316                         } else {
317                                 screen = g_realloc(screen, (j+2)*sizeof(char *));
318                                 screen[j++] = name;
319                                 screen[j] = NULL;
320                         }
321                 }
322                 i++;
323         }
324         g_strfreev(tmp);
325         if( screen == NULL )
326                 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
328         return screen;
331 static int
332 get_search_mode(char *value)
334         char * test;
335         int mode;
336         mode= strtol(value, &test, 10);
337         if (*test == '\0')
338         {
339                 if (0 <= mode && mode <= 4)
340                         return mode;
341                 else
342                 {
343                         print_error(_("Invalid search mode"),value);
344                         return 0;
345                 }
346         }
347         else
348         {
349                 for (int i = 0; value[i] != '\0'; i++)
350                         value[i] = tolower(value[i]);
352                 // TODO: modify screen_search so that its own list of modes can be used
353                 // for comparison instead of specifying them here
354                 if (strcmp(value, "title") == 0)
355                         return 0;
356                 else if (strcmp(value, "artist") == 0)
357                         return 1;
358                 else if (strcmp(value, "album") == 0)
359                         return 2;
360                 else if (strcmp(value, "filename") == 0)
361                         return 3;
362                 else if (strcmp(value, "artist+album") == 0)
363                         return 4;
364                 else
365                 {
366                         print_error(_("Unknown search mode"),value);
367                         return 0;
368                 }
369         }
372 static bool
373 parse_line(char *line)
375         size_t len = strlen(line), i = 0, j = 0;
376         char name[MAX_LINE_LENGTH];
377         char value[MAX_LINE_LENGTH];
378         bool match_found;
380         /* get the name part */
381         while (i < len && line[i] != '=' && !g_ascii_isspace(line[i]))
382                 name[j++] = line[i++];
384         name[j] = '\0';
386         /* skip '=' and whitespace */
387         while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
388                 i++;
390         /* get the value part */
391         j = 0;
392         while (i < len)
393                 value[j++] = line[i++];
394         value[j] = '\0';
396         match_found = true;
398         /* key definition */
399         if (!strcasecmp(CONF_KEY_DEFINITION, name))
400                 parse_key_definition(value);
401         /* enable colors */
402         else if(!strcasecmp(CONF_ENABLE_COLORS, name))
403 #ifdef ENABLE_COLORS
404                 options.enable_colors = str2bool(value);
405 #else
406         {}
407 #endif
408         else if (!strcasecmp(CONF_SCROLL_OFFSET, name))
409                 options.scroll_offset = atoi(value);
410         /* auto center */
411         else if (!strcasecmp(CONF_AUTO_CENTER, name))
412                 options.auto_center = str2bool(value);
413         /* color assignment */
414         else if (!strcasecmp(CONF_COLOR, name))
415 #ifdef ENABLE_COLORS
416                 parse_color(value);
417 #else
418         {}
419 #endif
420         /* wide cursor */
421         else if (!strcasecmp(CONF_WIDE_CURSOR, name))
422                 options.wide_cursor = str2bool(value);
423         else if (strcasecmp(name, "hardware-cursor") == 0)
424                 options.hardware_cursor = str2bool(value);
425         /* welcome screen list */
426         else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
427                 options.welcome_screen_list = str2bool(value);
428         /* visible bitrate */
429         else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
430                 options.visible_bitrate = str2bool(value);
431         /* timer display type */
432         else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name))
433                 options.display_remaining_time = parse_timedisplay_type(value);
434                 /* color definition */
435         else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
436 #ifdef ENABLE_COLORS
437                 parse_color_definition(value);
438 #else
439         {}
440 #endif
441         /* list format string */
442         else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
443                 g_free(options.list_format);
444                 options.list_format = get_format(value);
445                 /* status format string */
446         } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
447                 g_free(options.status_format);
448                 options.status_format = get_format(value);
449                 /* xterm title format string */
450         } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
451                 g_free(options.xterm_title_format);
452                 options.xterm_title_format = get_format(value);
453         } else if (!strcasecmp(CONF_LIST_WRAP, name))
454                 options.list_wrap = str2bool(value);
455         else if (!strcasecmp(CONF_FIND_WRAP, name))
456                 options.find_wrap = str2bool(value);
457         else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
458                 options.find_show_last_pattern = str2bool(value);
459         else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
460                 options.audible_bell = str2bool(value);
461         else if (!strcasecmp(CONF_VISIBLE_BELL, name))
462                 options.visible_bell = str2bool(value);
463         else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
464                 options.bell_on_wrap = str2bool(value);
465         else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
466                 options.status_message_time = atoi(value);
467         else if (!strcasecmp(CONF_XTERM_TITLE, name))
468                 options.enable_xterm_title = str2bool(value);
469         else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
470 #ifdef HAVE_GETMOUSE
471                 options.enable_mouse = str2bool(value);
472 #else
473         {}
474 #endif
475         else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
476                 options.crossfade_time = atoi(value);
477         else if (!strcasecmp(CONF_SEARCH_MODE, name))
478                 options.search_mode = get_search_mode(value);
479         else if (!strcasecmp(CONF_HIDE_CURSOR, name))
480                 options.hide_cursor = atoi(value);
481         else if (!strcasecmp(CONF_SEEK_TIME, name))
482                 options.seek_time = atoi(value);
483         else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
484                 g_strfreev(options.screen_list);
485                 options.screen_list = check_screen_list(value);
486         } else if (!strcasecmp(CONF_HOST, name))
487                 options.host = get_format(value);
488         else if (!strcasecmp(CONF_PORT, name))
489                 options.port = atoi(get_format(value));
490         else if (!strcasecmp(CONF_PASSWORD, name))
491                 options.password = get_format(value);
492         else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
493 #ifdef ENABLE_LYRICS_SCREEN
494                 options.lyrics_timeout = atoi(get_format(value));
495 #else
496         {}
497 #endif
498         else if (!strcasecmp(CONF_SCROLL, name))
499                 options.scroll = str2bool(value);
500         else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
501                 g_free(options.scroll_sep);
502                 options.scroll_sep = get_format(value);
503         } else if (!strcasecmp(CONF_DISPLAY_TIME, name))
504 #ifdef NCMPC_MINI
505                 {}
506 #else
507                 options.display_time = str2bool(value);
508 #endif
509         else if (!strcasecmp(CONF_JUMP_PREFIX_ONLY, name))
510 #ifdef NCMPC_MINI
511                 {}
512 #else
513                 options.jump_prefix_only = str2bool(value);
514 #endif
515         else if (!strcasecmp(CONF_LYRICS_AUTOSAVE, name))
516 #ifdef ENABLE_LYRICS_SCREEN
517                 options.lyrics_autosave = str2bool(value);
518 #else
519         {}
520 #endif
521         else if (!strcasecmp(CONF_LYRICS_SHOW_PLUGIN, name))
522 #ifdef ENABLE_LYRICS_SCREEN
523                 options.lyrics_show_plugin = str2bool(value);
524 #else
525                 {}
526 #endif
527         else if (!strcasecmp(name, CONF_TEXT_EDITOR))
528 #ifdef ENABLE_LYRICS_SCREEN
529                 {
530                         g_free(options.text_editor);
531                         options.text_editor = get_format(value);
532                 }
533 #else
534                 {}
535 #endif
536         else if (!strcasecmp(CONF_SECOND_COLUMN, name))
537 #ifdef NCMPC_MINI
538                 {}
539 #else
540                 options.second_column = str2bool(value);
541 #endif
542         else
543                 match_found = false;
545         if (!match_found)
546                 print_error(_("Unknown configuration parameter"), name);
548         return match_found;
551 static int
552 read_rc_file(char *filename)
554         FILE *file;
555         char line[MAX_LINE_LENGTH];
557         if (filename == NULL)
558                 return -1;
560         file = fopen(filename, "r");
561         if (file == NULL) {
562                         perror(filename);
563                         return -1;
564                 }
566         while (fgets(line, sizeof(line), file) != NULL) {
567                 char *p = g_strchug(line);
569                 if (*p != 0 && *p != COMMENT_TOKEN)
570                         parse_line(g_strchomp(p));
571         }
573         fclose(file);
574         return 0;
577 int
578 check_user_conf_dir(void)
580         int retval;
581         char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
583         if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
584                 g_free(directory);
585                 return 0;
586         }
588 #ifndef WIN32
589         retval = mkdir(directory, 0755);
590 #else
591         retval = mkdir(directory);
592 #endif
593         g_free(directory);
594         return retval;
597 char *
598 get_user_key_binding_filename(void)
600         return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
603 int
604 read_configuration(void)
606         char *filename = NULL;
608         /* check for command line configuration file */
609         if (options.config_file)
610                 filename = g_strdup(options.config_file);
612         /* check for user configuration ~/.ncmpc/config */
613         if (filename == NULL) {
614                 filename = g_build_filename(g_get_home_dir(),
615                                             "." PACKAGE, "config", NULL);
616                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
617                         g_free(filename);
618                         filename = NULL;
619                 }
620         }
622         /* check for  global configuration SYSCONFDIR/ncmpc/config */
623         if (filename == NULL) {
624                 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
625                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
626                         g_free(filename);
627                         filename = NULL;
628                 }
629         }
631         /* load configuration */
632         if (filename) {
633                 read_rc_file(filename);
634                 g_free(filename);
635                 filename = NULL;
636         }
638         /* check for command line key binding file */
639         if (options.key_file)
640                 filename = g_strdup(options.key_file);
642         /* check for  user key bindings ~/.ncmpc/keys */
643         if (filename == NULL) {
644                 filename = get_user_key_binding_filename();
645                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
646                         g_free(filename);
647                         filename = NULL;
648                 }
649         }
651         /* check for  global key bindings SYSCONFDIR/ncmpc/keys */
652         if (filename == NULL) {
653                 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
654                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
655                         g_free(filename);
656                         filename = NULL;
657                 }
658         }
660         /* load key bindings */
661         if (filename) {
662                 read_rc_file(filename);
663                 g_free(filename);
664                 filename = NULL;
665         }
667         return 0;